var telNumberErrorNo = 0;
telNumberErrors = new Array (0);
telNumberErrors[0] = "Valid UK telephone number.";
telNumberErrors[1] = "Please enter your telephone number in the space provided.";
telNumberErrors[2] = "Please eneter a UK telephone number without the country code.";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits.";
telNumberErrors[4] = "The telephone number should start with a 0.";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate.";

function checkUKTelephone(telephoneNumber) {
  var telNum;
  // Convert into a string and check that we were provided with a number
  telNum = telephoneNumber + " ";
  if (telNum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telNum.length = telNum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telNum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telNum.indexOf(" ")!= -1)  {
    telNum = telNum.slice (0,telNum.indexOf(" ")) + telNum.slice (telNum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telNum.indexOf("-")!= -1)  {
    telNum = telNum.slice (0,telNum.indexOf("-")) + telNum.slice (telNum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telNum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telNum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Now check that the telephone number is appropriate.
  exp = /^(01|02|05|070|077|078|079)[0-9]+$/;
  if (exp.test(telNum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Seems to be valid - return the stripped telephone number
  
  return telNum;
}

/**
 * DHTML date validation script for dd/mm/yyyy.
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function uncheckRadioGroup(radGroup) {
	intRecords = radGroup.length;
	if (intRecords == undefined) intRecords = 1;		
	if (intRecords == 1) {
		radGroup.checked = false
	}
	else
	{
		for (intCounter = 0; intCounter < intRecords; intCounter++) {
			radGroup[intCounter].checked = false
		}
	}
}

function validateCCNum(intCCNum) {
     return true; 
}

function validateEmail(thisEmailField) {
  if(-1 == thisEmailField.value.indexOf("@")) { 
     alert("Your email address must have a '@'."); 
     return false; 
  }
  if(-1 == thisEmailField.value.indexOf(".")) { 
     alert("Your email address must have a '.'."); 
     return false; 
  }
  if(-1 != thisEmailField.value.indexOf(",")) { 
     alert("Your email address must not have a ',' in it"); 
     return false; 
  }
  if(-1 != thisEmailField.value.indexOf("#")) { 
     alert("Your email address must not have an '#' in it." ); 
     return false; 
  }
  if(-1 != thisEmailField.value.indexOf("!")) { 
     alert("Your email address must not have a '!' in it." ); 
     return false; 
  }
  if(-1 != thisEmailField.value.indexOf(" ")) { 
     alert("Your email address must not have a space in it." ); 
     return false; 
  }
  if(thisEmailField.value.length == (thisEmailField.value.indexOf("@")+1) ) {
     alert("Your email address must have a domain name after the '@'.");
     return false;
  }
  return true;
}

function validateTelNumber(thisTelField) {
  var telNum = thisTelField.value;
  // If invalid number, report back error
  if (!checkUKTelephone(telNum)) {
     alert(telNumberErrors[telNumberErrorNo]);
	 return false;
  }
}

function validatePostcode(thisPostcodeField) { //check postcode format is valid

  test = thisPostcodeField.value;
  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
  }

 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
  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");
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   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;
}

//dv functions
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//other
function validateNewsletter()
{
with (window.document.newsletterform)
  {
		if (maillistEmail.value.length == 0){
		maillistEmail.focus(); 
		alert("You must enter a valid email address in the space provided."); 
		return false;
		}
		if (maillistEmail.value.length != 0){
		if (validateEmail(maillistEmail) == false){
		return false;}
       }
    submit();	
  }
}

function validateProductSearch()
{
	with (window.document.productsearchform)
	{
		if (ProductSearch.value.length == 0) {
			ProductSearch.focus();
			alert("Please enter a product name or the start of a product name in the space provided.");
			return false;
		}
	submit();	
	}
}
