function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		$('alertForm').innerHTML = message;
		formElement.focus();
	}else{
	$('alertForm').innerHTML = '';
	}
	
	return _isEmpty;
}

function setPaymentInfo(isChecked)
{
	with (window.document.frmCheckout) {
		if (isChecked) {
			txtPaymentFirstName.value  = txtShippingFirstName.value;
			txtPaymentLastName.value   = txtShippingLastName.value;
			txtPaymentAddress1.value   = txtShippingAddress1.value;
			txtPaymentAddress2.value   = txtShippingAddress2.value;
			txtPaymentPhone.value      = txtShippingPhone.value;
			txtPaymentState.value      = txtShippingState.value;			
			txtPaymentCity.value       = txtShippingCity.value;
			txtPaymentPostalCode.value = txtShippingPostalCode.value;
			
			txtPaymentFirstName.readOnly  = true;
			txtPaymentLastName.readOnly   = true;
			txtPaymentAddress1.readOnly   = true;
			txtPaymentAddress2.readOnly   = true;
			txtPaymentPhone.readOnly      = true;
			txtPaymentState.readOnly      = true;			
			txtPaymentCity.readOnly       = true;
			txtPaymentPostalCode.readOnly = true;			
		} else {
			txtPaymentFirstName.readOnly  = false;
			txtPaymentLastName.readOnly   = false;
			txtPaymentAddress1.readOnly   = false;
			txtPaymentAddress2.readOnly   = false;
			txtPaymentPhone.readOnly      = false;
			txtPaymentState.readOnly      = false;			
			txtPaymentCity.readOnly       = false;
			txtPaymentPostalCode.readOnly = false;			
		}
	}
}


function checkShippingAndPaymentInfo()
{
	with (window.document.frmCheckout) {
	
	var regex = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}/gi);
	//num_tel=txtShippingPhone.value;
	var regex2 = new RegExp(/^[0-9]{5}/gi);
	code=txtShippingPostalCode.value;
	var regex3 = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}/gi);
	//num_tel=txtPaymentPhone.value;
	var regex4 = new RegExp(/^[0-9]{5}/gi);
	//code=txtPaymentPostalCode.value;
	
		if (isEmpty(donate, 'Enter the amount of the donation')) {
			return false;
		} else if (isEmpty(txtShippingFirstName, 'Enter your first name')) {
			return false;
		}else if (isEmpty(txtShippingLastName, 'Enter your last name')) {
			return false;
		} else if (isEmpty(txtShippingAddress1, 'Enter your address')) {
			return false;
		} else if (isEmpty(txtShippingState, 'Enter your state')) {
			return false;
		}  else if (isEmpty(txtShippingCity, 'Enter your city')) {
			return false;
		} else if (isEmpty(txtShippingPostalCode, 'Enter your postal code')) {
			return false;
		}  
		else if (checkEmail() == false)
			{
			$('alertForm').innerHTML = "Invalid email address";
		 	return false;
		} else {
			//return true;
			
			if($('optPaypal').checked == true){var optPayment1 = 'CB';}else{var optPayment1 = 'paypal';}
			return 'donate='+$("donate").value+'&txtShippingFirstName='+encodeURIComponent($("txtShippingFirstName").value)+'&txtShippingLastName='+encodeURIComponent($("txtShippingLastName").value)+'&txtShippingAddress1='+encodeURIComponent($("txtShippingAddress1").value)+'&txtShippingState='+encodeURIComponent($("txtShippingState").value)+'&txtShippingCity='+encodeURIComponent($("txtShippingCity").value)+'&txtShippingPostalCode='+$("txtShippingPostalCode").value+'&email='+$("email").value+'&optPayment='+optPayment1;
		}
	}
};
function check(func){

var ret = func();
//alert(ret);
if (ret != false){
var http = createRequestObject();
			http.open('POST', 'checkout.php', true);
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Content-length", ret.length);
			http.setRequestHeader("Connection", "close");	
			http.onreadystatechange = function(){

				if(http.readyState == 4)
				{
				  if(http.status == 200)
					{
						$('cart').innerHTML= 'You will be redirected to our secure payment site : <br /><br />'+http.responseText;
						//$('formdon').innerHTML+= '';
						
					}
				   else
					{
						$('formdon').innerHTML+= 'error';

					}
				}
			}; //handleAJAXReturn;
			http.send(ret);
		}
}
function createRequestObject()
{
    var http;
    if(window.XMLHttpRequest)
    { // Mozilla, Safari, ...
        http = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    { // Internet Explorer
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return http;
};
function checkgra()
{
	with (window.document.frmCheckout) {
	var regex = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}/gi);
	num_tel=txtShippingPhone.value;
	var regex2 = new RegExp(/^[0-9]{5}/gi);
	code=txtShippingPostalCode.value;
	
		if (isEmpty(txtShippingLastName, 'Saisir le nom')) {
			return false;
		} else if (isEmpty(txtShippingFirstName, 'Saisir le prénom')) {
			return false;
		} else if (isEmpty(txtShippingAddress1, 'Saisir l\' adresse')) {
			return false;
		} else if (isEmpty(txtShippingPhone, 'Saisir le téléphone')) {
			return false;
		} else if (isEmpty(txtShippingCity, 'Saisir la ville')) {
			return false;
		} else if (isEmpty(txtShippingState, 'Saisir le pays')) {
			return false;
		} else if (isEmpty(txtShippingPostalCode, 'Saisir le code postal')) {
			return false;
		} else if (checkEmail() == false)
			{
			$('alertForm').innerHTML = "Adresse email incorrecte";
		 	return false;
		} else if (isEmpty(parrain, 'Saisir comment vous nous avez connus')) {
			return false;
		} else {
			return true;
		}
	}
}

function checkPaymentInfo()
{
	with (window.document.frmCheckout) {
		var regex = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}/gi);
		num_tel=txtPaymentPhone.value;
		var regex2 = new RegExp(/^[0-9]{5}/gi);
		code=txtPaymentPostalCode.value;
		if (isEmpty(txtPaymentLastName, 'Saisir le nom')) {
			return false;
		} else if (isEmpty(txtPaymentFirstName, 'Saisir le prénom')) {
			return false;
		} else if (isEmpty(txtPaymentAddress1, 'Saisir l\' adresse')) {
			return false;
		} else if (isEmpty(txtPaymentPhone, 'Saisir le téléphone')) {
			return false;
		} else if (isEmpty(txtPaymentState, 'Ente')) {
			return false;
		} else if (isEmpty(txtPaymentCity, 'Saisir la ville')) {
			return false;
		} else if (isEmpty(txtPaymentPostalCode, 'Saisir le code postal')) {
			return false;
		}else if (regex.test(num_tel) ==false)
			{
			alert("Le numéro de téléphone doit correspondre à un numéro français");
			return false;
		} else if (regex2.test(code) == false)
			{
			alert("Le code postal est incorrect");
		 	return false;
		} else if (checkEmail() == false)
			{
			alert("Adresse email incorrecte");
		 	return false;
		}else {
			return true;
		}
	}
}

function checkEmail()
{
	with (window.document.frmCheckout) {
	
		
		arobase="";
		point="";
		var arobase = email.value.indexOf("@");
		var point = email.value.lastIndexOf(".");
		if((arobase < 3)||(point + 2 > email.value.length)||(point < arobase+3))
		{
		return false;
		}
		else {
        return true
		}		

}
}

function step()
{
step=2;
return step;
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}



function Mot4(formElement, message) {
	formElement.value = trim(formElement.value);
	valeur = formElement.value;
	_Mot4 = false;
	if (valeur.length < 4) {
		_Mot4 = true;
		$('alertForm').innerHTML = message;
		formElement.focus();
	}else{
	$('alertForm').innerHTML = '';
	}
	return _Mot4;
}

