function longEnough(elem){

	var errorName = 'error_'+elem.name;
	if (elem.value.length < 6){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	document.getElementById(errorName).style.display='none';
	return true;
}

function notEmpty(elem){

	var errorName = 'error_'+elem.name;
	if((elem.value.length == 0) || (elem.value == ' ')){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	document.getElementById(errorName).style.display='none';
	return true;
}

function notEmptyTwo(elem1, elem2){
	if((elem1.value.length == 0) && (elem2.value.length == 0)){

		var errorName = 'error_'+elem2.name;

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem2.focus();

		return false;
	}
	return true;
}

function sameValue(elem1,elem2){

	var errorName = 'error_'+elem2.name;
	if(elem1.value != elem2.value){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem2.focus();
		return false;
	}

	document.getElementById(errorName).style.display='none';
	return true;
}

function validate_email_address(elem) {

	var errorName = 'error_'+elem.name;

	//Return blank email addresses as true - they are checked separately for a value
	if (elem.value == ""){
		return true;
	}

	var at="@"
	var dot="."
	var lat=elem.value.indexOf(at)
	var lelem=elem.value.length
	var ldot=elem.value.indexOf(dot)

	if (elem.value.indexOf(at)==-1){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	if (elem.value.indexOf(at)==-1 || elem.value.indexOf(at)==0 || elem.value.indexOf(at)==lelem){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	if (elem.value.indexOf(dot)==-1 || elem.value.indexOf(dot)==0 || elem.value.indexOf(dot)==lelem){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	 if (elem.value.indexOf(at,(lat+1))!=-1){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	 }

	 if (elem.value.substring(lat-1,lat)==dot || elem.value.substring(lat+1,lat+2)==dot){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	 }

	 if (elem.value.indexOf(dot,(lat+2))==-1){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	 }

	 if (elem.value.indexOf(" ")!=-1){
		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	 }

	document.getElementById(errorName).style.display='none';
	return true;

}

function notChecked(elem){

	var errorName = 'error_'+elem.name;

	if(elem.checked == false){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem.focus();

		return false;
	}

	document.getElementById(errorName).style.display='none';
	return true;
}

function isInteger(elem, sText)
{

   var ValidChars = "0123456789-";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {

			var errorName = 'error_'+elem.name;
			document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
			elem.focus();
	        IsNumber = false;

         }
      }
   return IsNumber;
}

function moneyValidator(elem){
	var moneyExp = /^[0-9\.\-]+\.[0-9]{2,4}$/;
	if(elem.value.match(moneyExp)){
		return true;
	}else{
		var errorName = 'error_'+elem.name;

		document.getElementById(errorName).style.visibility='visible';	//Make the red asterisk show up next to the error field
		elem.focus();
	}
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function validate_demographics(){

	// Make quick references to our fields

	var val_firstname = document.getElementById('firstname');
	var val_surname = document.getElementById('surname');
	var val_address_address = document.getElementById('address_address');
	var val_address_postalCode = document.getElementById('address_postalCode');
	var val_address_city = document.getElementById('address_city');
	var val_phoneNumber = document.getElementById('phoneNumber');
	var val_email = document.getElementById('email');
	var val_password = document.getElementById('password');
	var val_password_again = document.getElementById('password_again');
	var val_secret_question = document.getElementById('secret_question');
	var val_secret_answer = document.getElementById('secret_answer');

	//Not checked:

	//building
	//floor
	//intercom_access_code
	//intercom
	//company
	//service
	//notes
	//phoneNumber_additional

	//Hide the asterisks prior to any of them showing up (otherwise they persist)

	// Check each input in the booking that it appears in the form!

//	if(notChecked(val_terms_and_conditions)){

		if(notEmpty(val_firstname)){
			if(notEmpty(val_surname)){
				if(notEmpty(val_address_address)){
					if(notEmpty(val_address_postalCode)){
						if(notEmpty(val_address_city)){
							if(notEmpty(val_phoneNumber)){
								if((validate_email_address(val_email)) && (notEmpty(val_email))){
									if((notEmpty(val_password)) && (sameValue(val_password,val_password_again))){
										if((longEnough(val_password))){
											if(notEmpty(val_secret_question)){
												if(notEmpty(val_secret_answer)){
								//					if (jcap()){
												   		return true;
								//					}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

//	}
	return false;
}

function validate_order_form(){

	// Make quick references to our fields
	var val_name = document.getElementById('Name');
	var val_business_name = document.getElementById('BusinessName');
	var val_phone = document.getElementById('Phone');
	var val_advert_type = document.getElementById('AdvertType');
	var val_loose_inserts = document.getElementById('LooseInserts');

	//Hide the asterisks prior to any of them showing up (otherwise they persist)
	//document.getElementById('error_name').style.display='block';

	// Check each input in the booking that it appears in the form!

	if(notEmpty(val_name)){
		if(notEmpty(val_business_name)){
			if(notEmpty(val_phone)){
				if(advertCheck(val_advert_type)){
					if(looseInsertCheck(val_advert_type,val_loose_inserts)){
						if (jcap()){
					   		return true;
						}
					}
				}
			}
		}
	}
	return false;
}

function validate_call_me_back(){

	// Make quick references to our fields
	var val_name = document.getElementById('Name');
	var val_phone = document.getElementById('Telephone');

	//Hide the asterisks prior to any of them showing up (otherwise they persist)
	//document.getElementById('error_name').style.display='block';

	// Check each input in the booking that it appears in the form!

	if(notEmpty(val_name)){
		if(notEmpty(val_phone)){
			if (jcap()){
		   		return true;
			}
		}
	}
	return false;
}

function validate_special_offers(){

	// Make quick references to our fields
	var val_name = document.getElementById('Name');
	var val_email = document.getElementById('Email');

	//Hide the asterisks prior to any of them showing up (otherwise they persist)
	//document.getElementById('error_name').style.display='block';

	// Check each input in the booking that it appears in the form!

	if(notEmpty(val_name)){
		if(notEmpty(val_email)){
			if (jcap()){
		   		return true;
			}
		}
	}
	return false;
}

/*
function validate_email() {
var where = document.getElementById("news_email").value.indexOf("@");

if (document.getElementById("news_email").value.length < 1) {
  window.alert("You must include your e-mail address.");
  return false;
}
if (where < 0) {
  window.alert("You must enter a valid email address");
  return false;
}
return true;
}
*/

function validate_login(){

	// Make quick references to our fields
	var email = document.getElementById('email');
	var password = document.getElementById('password');

	if (email.value != 'test'){

		// Check each input appears in the form!
	   	if((validate_email_address(email)) && (notEmpty(email))){
			if(notEmpty(password)){
			   		return true;
			}
		}
	}else{
		document.getElementById('password').value='password';
		return true;
	}

	return false;
}

//changed by Dave
function order_below_min_value() {

    alert("Nous sommes d\u00e9sol\u00e9s, le minimum de commande de 20 eur n\'est pas atteint");
    return false;

}
//end changes