
function populateMakes (objForm, makelist){    
    var anySelected = false;
    var makeSelected = false;
    for (x=0; x<objForm.make.length; x++)  {
        var val = objForm.make[x].value.toUpperCase();            
        for (y=0; y<makelist.length; y++) {
            if (makelist[y].toUpperCase() == val){  
                makeSelected = true;
                objForm.make[x].selected = true;
                if (x == 0){
                    anySelected = true;
                }
            }
        }
    }      
    // dont highlight ANY if we have selected makes
    if (!anySelected && makeSelected){
        objForm.make[0].selected = false;
    }
}



function validate( form ){
   if (validatePostcode(form)){    
        return validateMake(form); 
   }else{
        return false;
   }
}

function validateMake( form ){
    
    var anySelected = false;
    var makeSelected = false;
    var res = true;
    var dealerName = form.dealerName.value;
    var distance = form.miles.value;
    
    for (i=0; i<form.make.length; i++){
      
        if (form.make[i].selected == true){
            if (form.make[i].value == 'ANY'){
                anySelected = true;                
            }else{
                makeSelected = true;
            }
        }
    }        
    
    if ( (anySelected == true) && (makeSelected == false) 
            && (dealerName == '') 
            && (distance >= 500)){
        res = false;
        alert ("A national 'Any Make' search can only be used with a dealer name.");
    }
    return res;
}


function validatePostcode( form ){
  var myPostCode = form.postcode.value;
  var msg = "";
  //if ( myPostCode == "" ){
  //  return true;
  //}
  if (checkPostCode (myPostCode)) {
    form.postcode.value = checkPostCode (myPostCode)
    //alert ("Postcode has a valid format");
    return true;
  } // end if
  else {
    
    if (myPostCode == ''){
        msg = "Please enter a valid postcode."            
    }else{
        msg = myPostCode + " is an invalid postcode.\n\nPlease enter a valid postcode."    
    }
    
    alert (msg);
    return false;
   } // end else
}

function checkPostCode (toCheck) {

// This routine checks the value of the form element specified by the parameter
// for a valid postcode. The space between the inward part and the outward part
// is optional, although is inserted if not there nas it is part of the postcode.

// The definition of a valid postcode has been taken from:
// http://www.royalmail.com/docContent/other/Downloadable_Files/PAF_Digest_Issue_5_0.pdf

// If the element is a valid postcode, the function returns it correctly 
// formatted. Otherwise it returns the original input.

  // This array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1,2})(\s*)([0-9]{1}[abdefghijlnpqrstuwxyz][a-z])$/i);

  // Expression for postcodes: ANA NAA, and AANA  NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1}[a-z]{1})(\s*)([0-9]{1}[abdefghijlnpqrstuwxyz][a-z])$/i);
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against both post codes
  for (i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop

      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function doSubmit( form ){
  if (validate(form)){
    form.submit();
  }
  else {
     return false;  
  }
}

function doUnvalidatedSubmit( form ){
    form.submit();
}

function postCodeCheck(){
    var users_postcode = document.getElementsByName("postcode")[0].value;
    
    if (!users_postcode || users_postcode == '') {
       users_postcode =  getcookieField("PC");       
       if (users_postcode && users_postcode != '') {
    	   document.getElementsByName("postcode")[0].value = users_postcode.replace('+', ' ');
       }
    }
}

function getcookieField(FLDval)
{
	var ourcookie = "user";
	var allcookies = document.cookie;
	var FIELD = null;

	if (allcookies != "")
	{

		var start = allcookies.indexOf(ourcookie + "=");
		if (start != -1)
		{
			start += ourcookie.length + 1;
			var end = allcookies.indexOf(";",start);
			if (end == -1) end = allcookies.length;
			var usercookie = allcookies.substring(start,end);
			var celements = usercookie.split('&')
			for (cnt=0;cnt<celements.length;cnt++)
			{
				celements[cnt] = celements[cnt].split('=');
				if (celements [cnt][0] == FLDval)
				FIELD = unescape(celements[cnt][1]);
			}
		}
	}
	return FIELD;
}
