/**
 * @author Paulo Lopes
 */

function openPopup($file) 
{
	window.open($file, 'popup', 'width=800, height=600, top=0, left=0, scrollbars=yes')
}

function closePopup() 
{
	window.close();
}

function printWindow()
{
	window.print();
}

function goBack() 
{ 
	window.history.back(); 
}

function sendForm(page){
	
	obj = document.getElementById('send_mail');
		
	if (page == '1'){
		obj.action = '../../php_paypal/process.php';
		obj.submit();
	} 
	if (page == '2'){		
		obj.action = '';
	}
}

function envia_form_recuperar(send_mail, page)
{
		
	oElement = document.getElementById(send_mail);
	
	if (oElement.fname.value.length == 0) { 
		alert("First Name must be filled!"); 
		return false; 
	} 
	if (oElement.lname.value.length == 0) { 
		alert("Last Name must be filled!"); 
		return false; 
	} 	
	if (oElement.email.value.length == 0) { 
		alert("E-mail must be filled!"); 
		return false; 
	} 
	
	if(sendForm(page)){
		return true;	
	}

	//oElement.send.disabled = true;
	return true;
}

/*function envia_form(send_mail, emField)
{
	oElement = document.getElementById(send_mail);
	
	if (oElement.first_name.value.length == 0) { 
		alert("First Name must be filled!"); 
		return false; 
	} 
	if (oElement.last_name.value.length == 0) { 
		alert("Last Name must be filled!"); 
		return false; 
	} 	
	if (oElement.email.value.length == 0) { 
		alert("E-mail must be filled!"); 
		return false; 
	} 	
	if (oElement.address.value.length == 0) { 
		alert("Address must be filled!"); 
		return false; 
	} 	
	if (oElement.city.value.length == 0) { 
		alert("City must be filled!"); 
		return false; 
	} 				
	
	 return checkEmail(document.send_mail.email);

	//oElement.send.disabled = true;
	return true;
}	*/



// fun��o que verifica se o endereco de mail � valido
function checkEmail(emField){ //reference to email field passed as argument

	var fieldValue = emField.value; // store field's entire value in variable
	
	// Begin Valid Email Address Tests
	
	//if field is not empty
	if(fieldValue != ""){
		
		var atSymbol = 0;
		
		//loop through field value string
		for(var a = 0; a < fieldValue.length; a++){
		
			//look for @ symbol and for each @ found, increment atSymbol variable by 1
			if(fieldValue.charAt(a) == "@"){
				atSymbol++;
			}
	
		}
	
		// if more than 1 @ symbol exists
		if(atSymbol > 1){
			// then cancel and don't submit form
			alert("Please put a correct e-mail.");
			return false;
		}
		
		// if 1 @ symbol was found, and it is not the 1st character in string
		if(atSymbol == 1 && fieldValue.charAt(0) != "@"){
			
			//look for period at 2nd character after @ symbol
			var period = fieldValue.indexOf(".",fieldValue.indexOf("@")+2);
			
			// "." immediately following 1st "." ?
			var twoPeriods = (fieldValue.charAt((period+1)) == ".") ? true : false;
			
			//if period was not found OR 2 periods together OR field contains less than 5 characters OR period is in last position
			if(period == -1 || twoPeriods || fieldValue.length < period + 2 || fieldValue.charAt(fieldValue.length-1)=="."){
				// then cancel and don't submit form
				alert("Please put a correct e-mail.");
				return false;
			}
		
		}
		// no @ symbol exists or it is in position 0 (the first character of the field)
		else{
			// then cancel and don't submit form
			alert("Please put a correct e-mail.");
				return false;
		}
	}
	// if field is empty
	else{
		// then cancel and don't submit form
		alert("Please put a correct e-mail.");
		return false;
	}
		
	// all tests passed, submit form, vallid email addreess
	return true;
	
}
