/* Javascript Helper Functions */
function closePopup(){
	document.getElementById('popupTxt').innerHTML='';
	document.getElementById('popupinner').style.display='none';
}

function popupLayer(txt){
	document.getElementById('popupTxt').innerHTML=txt;
	document.getElementById('popupinner').style.display = '';
}

function preloadImages(httphost){
	checkbox1 = new Image(); 
	checkbox1.src="http://"+httphost+"/images/checkbox_on.png"; 
	
	checkbox2 = new Image(); 
	checkbox2.src="http://"+httphost+"/images/checkbox_off.png"; 

}
function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
	 var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
	  var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
	    if (parseInt(navigator.appVersion) >= 4) {
	      obj_window.window.focus();
	    }
	}

/* Function to check UK post codes */
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);
	  
	  // Overseas Territories
	  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/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 to check UK phone numbers */
function checkUKTelephone (telephoneNumber) {

	  // Convert into a string and check that we were provided with something
	  var 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 "+")
	  var 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;
	  }
		
		// Disallow numbers allocated for dramas.
		 
	  // Array holds the regular expressions for the drama telephone numbers
	  var tnexp = new Array ();
		tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
		tnexp.push (/^02079460[0-9]{3}$/);
		tnexp.push (/^01914980[0-9]{3}$/);
		tnexp.push (/^02890180[0-9]{3}$/);
		tnexp.push (/^02920180[0-9]{3}$/);
		tnexp.push (/^01632960[0-9]{3}$/);
		tnexp.push (/^07700900[0-9]{3}$/);
		tnexp.push (/^08081570[0-9]{3}$/);
		tnexp.push (/^09098790[0-9]{3}$/);
		tnexp.push (/^03069990[0-9]{3}$/);
		
		for (var i=0; i<tnexp.length; i++) {
	    if ( tnexp[i].test(telnum) ) {
	      telNumberErrorNo = 5;
	      return false;
	    }
		}
	  
	  // Finally check that the telephone number is appropriate.
	  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
		if (exp.test(telnum) != true) {
	     telNumberErrorNo = 5;
	     return false;
	  }
	  
	  // Telephone number seems to be valid - return the stripped telehone number  
	  return telnum;
	}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
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";