// *****************************************************************************
// **  Javascripts include file - Interactive Law 2007 - V1.001				 ***
// **  Created by Michael Thompson - Virtual Life							 ***
// *****************************************************************************

preload1 = new Image();
preload1.src = "images/b1-dn.gif"; 

preload2 = new Image();
preload2.src = "images/b2-dn.gif"; 

preload3 = new Image();
preload3.src = "images/b3-dn.gif"; 

preload4 = new Image();
preload4.src = "images/b4-dn.gif"; 

preload5 = new Image();
preload5.src = "images/b5-dn.gif"; 

browser_name = navigator.appName;
browser_version = parseFloat(navigator.appVersion); 

if (browser_name == "Netscape" && browser_version < 2.0) {
	roll = 'false';
} 
else if (browser_name == "Netscape" && browser_version == 2.0) {
	roll = 'false';
}
else if (browser_name == "Netscape" && browser_version >= 3.0) {
	roll = 'true';
}
else if (browser_name == "Microsoft Internet Explorer" && browser_version <= 2.0) {
	roll = 'false';
}
else if (browser_name == "Microsoft Internet Explorer" && browser_version >= 3.0) {
	roll = 'true';
}
else if (browser_version >= 4.0) {
	roll = 'true';
}
else {
	roll = 'false';
}

function msover(img, ref) {
	if (roll == 'true') {
		document.images[img].src = ref;
	}
}
        
function msout(img, ref) {
	if (roll == 'true'){
		document.images[img].src = ref;
	}
}
        

function doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = ""
	}
}
 
function validate(){

	var strFieldName, objFieldName;
	var strNameValue, strPhoneValue, strEmailValue
	var strNotesValue, strPostcode
 
	strFieldName = "document.quickform.namex";
 	objFieldName = eval(strFieldName);
	strNameValue = objFieldName.value;
	
	if (strNameValue =="") {
		alert("Please enter your name to send this enquiry.");
		return false;
	}

	strFieldName = "document.quickform.emailx";
 	objFieldName = eval(strFieldName);
	strEmailValue= objFieldName.value;

	if (strEmailValue=="") {
		alert("Please enter your email to send this enquiry.");
		return false;
	}

	strFieldName = "document.quickform.phonex";
 	objFieldName = eval(strFieldName);
	strPhoneValue= objFieldName.value;
	
	if (strPhoneValue=="") {
		alert("Please enter your contact phone number to send this enquiry.");
		return false;
	}

	strFieldName = "document.quickform.detailsx";
 	objFieldName = eval(strFieldName);
	strNotesValue= objFieldName.value;
	
	if (strNotesValue=="") {
		alert("Please tell us some details of your accident to send this enquiry.");
		return false;
	}

	strFieldName = "document.quickform.postcodex";
 	objFieldName = eval(strFieldName);
	strPostcode= objFieldName.value;
	
	if (strPostcode=="") {
		alert("Please enter your postcode to send this enquiry.");
		return false;
	}
	else {
		if (checkPostCode (strPostcode)) {
 			var iCheck = 1;
  		} 
  		else {
  			alert ("Postcode has invalid format");
  			return false;
  		}		
	}
}

 function validateEnquiry(){

	var strFieldName, objFieldName;
	var strFirstNameValue, strLastNameValue, strEmailValue
	var strAddressValue, strPostcodeValue
 
	strFieldName = "document.enquiry.firstname";
 	objFieldName = eval(strFieldName);
	strFirstNameValue = objFieldName.value;
	
	if (strFirstNameValue=="") {
		alert("Please enter your full name to send this enquiry.");
		return false;
	}

	strFieldName = "document.enquiry.lastname";
 	objFieldName = eval(strFieldName);
	strLastNameValue = objFieldName.value;

	if (strLastNameValue=="") {
		alert("Please enter your full name to send this enquiry.");
		return false;
	}

	strFieldName = "document.enquiry.postcode";
 	objFieldName = eval(strFieldName);
	strPostcodeValue = objFieldName.value;
	
	if (strPostcodeValue=="") {
		alert("Please enter your postcode to send this enquiry.");
		return false;
	}
	else {
		if (checkPostCode (strPostcodeValue)) {
 			var iCheck = 1;
  		} 
  		else {
  			alert ("Postcode has invalid format");
  			return false;
  		}		
	}

	strFieldName = "document.enquiry.email";
 	objFieldName = eval(strFieldName);
	strEmailValue = objFieldName.value;
	
	if (strEmailValue=="") {
		alert("Please enter your email address to send this enquiry.");
		return false;
	}

	strFieldName = "document.enquiry.details";
 	objFieldName = eval(strFieldName);
	strNotesValue = objFieldName.value;
	
	if (strNotesValue=="") {
		alert("Please tell us some details of your problem to send this enquiry.");
		return false;
	}
}


function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // 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 (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/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 the types of post codes
  for ( var 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();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // 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 doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = ""
	}
}