/*remove excess white space*/
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

/*check email format, returns true if valid*/
function checkEmail(emailStr)
{
	var emailSyntax = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var matchArray = emailStr.match(emailSyntax);
	if (matchArray == null)
	{
		return false;
	}
	return true;
}

/*checks a list of semicolon separated emails*/
function ParseEmail(emailStr) {
	var bValid = true;
	emailStrArr = emailStr.split(";")
	for (aCounter = 0; aCounter<emailStrArr.length ; ++aCounter)
	{
		if (!checkEmail(trim(emailStrArr[aCounter])))
		{
			bValid = false;
		}
	}
	return bValid;
}

function postit(test){ //check postcode format is valid
	size = test.length;
	test = test.toUpperCase(); //Change to uppercase
	while (test.slice(0,1) == " ") //Strip leading spaces
	{
		test = test.substr(1,size-1);size = test.length
	}
	while(test.slice(size-1,size)== " ") //Strip trailing spaces
	{
		test = test.substr(0,size-1);size = test.length
	}
	test = test; //write back to form field
	if (size < 6 || size > 8) { //Code length rule
		alert(test + " is not a valid postcode - wrong length. Please check and try again.");
		return false;
	}
	if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
		alert(test + " is not a valid postcode - cannot start with a number. Please check and try again.");
		return false;
	}
	if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
		alert(test + " is not a valid postcode. Please check and try again.");
		return false;
	}
	if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
		alert(test + " is not a valid postcode. Please check and try again.");
		return false;
	}
	if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
		alert(test + " is not a valid postcode. Please check and try again.");
		return false;
	}
	if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
		alert(test + " is not a valid postcode - no space or space in wrong position.");
		return false;
	}
	count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
	if (count1 != count2){//only one space rule
		alert(test + " is not a valid postcode - only one space allowed.");
		return false;
	}
	return true;
}

function servicehighlight(pimage, pimg) {
	document.getElementById(pimg).src = "/images/" + pimage;
}


function reloadCAPTCHA() {
	document.getElementById('CAPTCHA').src='/CAPTCHA/CAPTCHA_image.asp?'+Date();
	document.getElementById('securityCode').value='';
	document.getElementById('securityCode').focus();
}


/*resize the sections*/
function ResizeBlueSection() {
	if (parseInt(document.getElementById('blue_background').offsetHeight) < 847) {
		document.getElementById('blue_background').style.height = '847px' ;
	}
}

function ResizeYellowSection() {
	if (parseInt(document.getElementById('yellow_background').offsetHeight) < 847) {
		document.getElementById('yellow_background').style.height = '847px' ;
	}
}
