function Trim(TXT){
	return TXT.replace(/(^\s+)|(\s+$)/g,"");
}

function checkForm(theform){
	cht = 0;
	//var commonMsg = "Please fill out all the field(s) in red before submitting.";
	var commonMsg = "Please correct following errors before submitting.\n\n";
	var showCommonMsg = false;
	var msg = '';
		
	for (x = 0; x < theform.elements.length; x++){
		
		var el = theform.elements[x];
		var valer = 0;
		var chkField = false;
		
		if(el.type) {
			el_type = el.type.toLowerCase();			
			
			if(document.getElementById('lbl_'+el.name)) {				
				var lblCtrl = document.getElementById('lbl_'+el.name);
				if(document.all){
					 var lblTxt = lblCtrl.innerText ;
				} 
				else{
					var lblTxt = lblCtrl.textContent ;
				}
			}
			else {
				var lblCtrl = false;
				var lblTxt = '' ;
			}
			
			if(document.getElementById('span_'+el.name)) {
				var selectSpan = document.getElementById('span_'+el.name);
			}
			else{	
				var selectSpan = '' ;
			}
			
			var arrChk ;
			if(el.id != '') {				
				var idVal = el.id;
				arrChk = idVal.split('|');
				for(var i = 0; i < arrChk.length; i++) {
					var type = arrChk[i];					
					if(type.indexOf('inrange') != -1) {						
						var p = type.indexOf(':');
						var minVal = type.substring(8,p);
						var maxVal = type.substring(p+1);
						type = 'inrange';
					}
					
					switch (type) {						
						case "required" :
							chkField = true;
							el.value = Trim(el.value)  ;
							if(el_type == "text" || el_type == "textarea" || el_type == "select-one" || el_type == "password" || el_type == "file"){	
								if(el.value == ""){
									valer = 1;
									showCommonMsg = true;
									msg +=  "=> Enter the value for '"+lblTxt+"'  \n";
								}
								else{
									valer = 0;
								}
							}// text field val
							else if(el_type == "checkbox"){
								if(el.checked == false){
									valer = 1;
									showCommonMsg = true;
									msg +=  "=> Enter the value for '"+lblTxt+"'  \n";
								}
								else{
									valer = 0;
								}
							}
							
							break;
							
						case "isemail" : // email
							chkField = true;

							var emailFilter=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

							if (!(emailFilter.test(el.value))) {
								msg +=  "=> Please enter a valid email address  \n";
								valer = 1;
							}
							
							var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
							if (el.value.match(illegalChars)) {
								msg +=  "=> Email cannot contains illegal characters  \n";
								valer = 1;
							}
							break;
						
						case "nospecialchars" : // No Special Chars
							var objRegExp = /[\s|\,|\"]/ ;
							if(objRegExp.test(el.value) ) {
								msg +=  "=> llegal characters such as space, double quotes etc. are not allowed in "+lblTxt+"  \n" ;	
								valer = 1;
							}
							break;
							
						case "inrange" : // In range
							var num = el.value.length;
							if (num < minVal || maxVal < num) {								
								msg +=  "=> "+lblTxt+' must contain a number between '+minVal+' and '+maxVal+"  \n" ;
								valer = 1;
							}
							break;

						case "isdate" : // Is date							
							if(el.value != ''){								
								retVal = checkValidDate(el.value);
								if(retVal != ''){
									msg +=  "=> "+retVal+"\n" ;
									valer = 1;
								}
							}
							break;

						case "ispostcode" : // In range							
							var validPostCode = checkPostCode(el.value);
							if(!validPostCode){
								msg +=  "=> Please enter a valid postcode\n" ;
								valer = 1;
							}
							else{
								el.value = validPostCode ;
							}
							break;

						default:
							break;
					}
					
					if (valer == 1){	
						el.className='inputError';
						cht = 1;						
						if(lblCtrl) {
							lblCtrl.className = "error";							
						}
						if(selectSpan)
							selectSpan.className = "inputError";
							
						break;
					}
					else{
						if(chkField) {
							el.className='inputNormal'; 
							if(lblCtrl) {
								lblCtrl.className = "normal";
							}
							if(selectSpan)
								selectSpan.className = "";
						}
					}
					
				}
				valer = 0;
			}

		}
	} 
	
	if (cht == 1){
		msg = commonMsg + msg;
		alert(msg);
		return false;
	}
	else{
		return true;	   
	}               

}

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;
}