function rollover(image,source,folder,path,extension) {
	document.getElementById(image).src=path+'/'+'images/'+folder+'/'+source+'_over.'+extension;
}
function rollout(image,source,folder,path,extension) {
	document.getElementById(image).src=path+'/'+'images/'+folder+'/'+source+'.'+extension;
}

function rollover2(image,source,folder,path) {
	document.getElementById(image).src=path+'/'+'images/'+folder+'/'+source+'.gif';
}
function rollout2(image,source,folder,path) {
	document.getElementById(image).src=path+'/'+'images/'+folder+'/'+source+'.gif';
}
function preloadImages() {
	var d=document; 
	if(d.images) { 
		d.preload=new Array();
		var i;
		var a=preloadImages.arguments;
		for(i=0; i<a.length; i++) {
			d.preload[i]=new Image;
			d.preload[i].src=a[i];
		}
	}
}

function textFieldCheck (mandatory) {
	check=false;
	message='';
		
	for (i in mandatory) {
		if (document.getElementById(i).value=='') {
			message=message+mandatory[i]+', ';
    		//alert ('One or more mandatory fields have not been completed. Please correct this to continue.');
    		check=false;
    		//break;
        }
        else {
        	check=true;
        }
	}
	if (message!='') {
		message='The following required fields have not been completed: \n\n'+message;
		message_object=new String(message);
		message=message_object.substr(0,message_object.length-2);
		alert(message);
		check=false;
	}
	return check;
}

function passwordMatch(password, confirm_password) {
	if(password == confirm_password) {
		return true;
	} else {
		alert('Please ensure that your password and confirmed password match');
		return false;
	}
}


function copyAddress() {
  if (document.getElementById('different_delivery').checked) {
    document.getElementById('d_salutation').value = document.getElementById('b_salutation').value;
    document.getElementById('d_first_name').value = document.getElementById('b_first_name').value;
    document.getElementById('d_last_name').value = document.getElementById('b_last_name').value;
    document.getElementById('d_telephone').value = document.getElementById('b_telephone').value;
    document.getElementById('d_address_line_1').value = document.getElementById('b_address_line_1').value;
    document.getElementById('d_address_line_2').value = document.getElementById('b_address_line_2').value;
    document.getElementById('d_town_city').value = document.getElementById('b_town_city').value;
    document.getElementById('d_county_state').value = document.getElementById('b_county_state').value;
    document.getElementById('d_postcode_zip').value = document.getElementById('b_postcode_zip').value;
    document.getElementById('d_country').value = document.getElementById('b_country').value;
  }
  else
  {
  	//document.getElementById('d_salutation').value = "";
    document.getElementById('d_first_name').value = "";
    document.getElementById('d_last_name').value = "";
    document.getElementById('d_telephone').value = "";
    document.getElementById('d_address_line_1').value = "";
    document.getElementById('d_address_line_2').value = "";
    document.getElementById('d_town_city').value = "";
    document.getElementById('d_county_state').value = "";
    document.getElementById('d_postcode_zip').value = "";
    //document.getElementById('d_country').value = "";
  }
}

function compareStartAndEndDates() {
	var start_date_month = document.getElementById('start_date_month').value-1;
	var start_date_year = document.getElementById('start_date_year').value;	
	var startDate = new Date();
	startDate.setFullYear(start_date_year,start_date_month,1);

	var end_date_month = document.getElementById('end_date_month').value-1;
	var end_date_year = document.getElementById('end_date_year').value;			
	var endDate = new Date();
	endDate.setFullYear(end_date_year,end_date_month,1);
	
	if (startDate>=endDate) {
		alert('End date must be greater than start date');
		return false;
	}
	else {
		return true;
	}
}

function emailCheckForMultipleEmails (emailStr) {

	var emailError = null;	
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		emailError = "Email address seems incorrect (check @ and .'s)";
		return emailError;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) 
	{
		emailError = "The username doesn't seem to be valid.";
		return emailError
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				emailError = "Destination IP address is invalid!";
				return emailError;		
			}
		}
		return null;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null) 
	{
		emailError = "The domain name doesn't seem to be valid.";
		return emailError;
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
	{
		emailError = "The address must end in a three-letter domain, or two letter country.";
		return emailError
	}
	if (len<2) 
	{
		emailError = "This address is missing a hostname.";	   
		return emailError
	}

	return null;
}

function emailCheck (emailStr) {
	
	var emailPat=/^(.+)@(.+)$/
	//var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]\\/\\?\\!\\£\\$\\%\\&\\#\\~\\*\\'\\|\\`\\=\\¬\\¦\\}\\{\\+\\^"
	//var validChars="\[\\s" + specialChars + "\]"
	var validChars="[a-zA-Z0-9_-]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
    		alert("Email address doesn't seem to be valid.")
    		return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Email address has an invalid destination IP.")
		return false
	    }
    	}
   	 return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("Email address has an invalid domain name")
   		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
    		domArr[domArr.length-1].length>4) {
  		alert("Email address must end in a three-letter or four-letter domain, or two letter country.")
   		return false
	}
	if (len<2) {
  		 var errStr="Email address doesn't have a hostname."
  		 alert(errStr)
   		return false
		}
	return true;
}



// SEARCH FOCUS

function searchfocus(inputField, focus){
	if (focus){
		if(inputField.value=="Search..."){
			inputField.value = "" ;
			inputField.className = "" ;
		}
	} else {
		if(inputField.value=="Search..." || inputField.value=="") {
			inputField.value = "Search..." ;
			inputField.className = "searchgrey" ;
		}
	} 
}

function yourEmailFocus(inputField, focus){
	if (focus){
		if(inputField.value=="your email"){
			inputField.value = "" ;
			//inputField.className = "" ;
		}
	} else {
		if(inputField.value=="your email" || inputField.value=="") {
			inputField.value = "your email" ;
			//inputField.className = "searchgrey" ;
		}
	} 
}