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 advertCheck(elem){

	//Function to check that an advert is selected if a choice has been made to have an advert

	var errorName = 'error_'+elem.name;
	if((elem.value.length == 0) && (document.getElementById("AdvertRequired").checked == true)){

		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 looseInsertCheck(elem1,elem2){

	//Function to check that if an advert type of "loose inserts" advert is selected then a number has been specified

	var errorName = 'error_'+elem2.name;
	if((elem2.value.length == 0) && (document.getElementById(elem1.name).options[14].selected == true)){

		document.getElementById(errorName).style.display='block';	//Make the red asterisk show up next to the error field
		elem2.focus();

		return false;
	}

	if((elem2.value.length > 0) && (elem2.value > 3)){
		elem2.value = 3;
		return true;
	}

	if((elem2.value.length > 0) && (elem2.value == 0)){
		elem2.value = 1;
		return true;
	}

	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_recycling_form(){

	// Make quick references to our fields

	var val_customer_name = document.getElementById('customer_name');
	var val_phone = document.getElementById('phone');
	var val_email = document.getElementById('email');
	var val_street_address = document.getElementById('street_address');
	var val_name = document.getElementById('name');	//Name of the item!
	var val_type = document.getElementById('type');
	var val_password = document.getElementById('password');
	var val_terms_and_conditions = document.getElementById('terms_and_conditions');


	//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(notChecked(val_terms_and_conditions)){
		if(notEmpty(val_customer_name)){
			if(notEmpty(val_phone)){
				if(notEmpty(val_email)){
					if(notEmpty(val_street_address)){
						if(notEmpty(val_name)){
							if(notEmpty(val_type)){
								if(notEmpty(val_password)){
									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;
}


