

function Trim(TXT){
	return TXT.replace(/(^\s+)|(\s+$)/g,"");
}



/*==============================================================================
  if (checkPostCode (myPostCode)) {
    alert ("Postcode has a valid format")
  } 
  else {alert ("Postcode has invalid format")};
                    
------------------------------------------------------------------------------*/

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 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 = '';
	var displayVal ="";
	
	for (x = 0; x < theform.elements.length; x++){
		
		var el = theform.elements[x];
		var valer = 0;
		var chkField = false;
		var ctrlMessage = '';
		
		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.lang != '') {					
				var idVal = el.lang;
				
                arrData = idVal.split('@');
                arrChk = arrData[0].split('|');

                if (arrData.length > 1) {
                    lblTxt = arrData[1];
                } 				
				
                if (arrData.length > 2) {
                    ctrlMessage = arrData[2];
                } 
                else {
                    ctrlMessage = "";
                }
				
				displayVal == "";
				
				if (arrData.length > 3) {
                    displayVal = arrData[3];
                }

				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 == "" || el.value == displayVal){
									valer = 1;
									showCommonMsg = true;
									if(ctrlMessage != ''){
										msg +=  "=> '"+ctrlMessage+"'  \n";
									}
									else{
										msg +=  "=> Enter the value for '"+lblTxt+"'  \n";
									}
								}
								else{
									valer = 0;
								}
							}// text field val
							else if(el_type == "select-multiple") {
								var flag = 0;
								var ctrlName = el.name;
								ctrlName = ctrlName.substring(0,ctrlName.length - 2);
								if(document.getElementById('lbl_'+ctrlName)) {				
									lblCtrl = document.getElementById('lbl_'+ctrlName);
									if(document.all){
										 lblTxt = lblCtrl.innerText ;
									} 
									else{
										lblTxt = lblCtrl.textContent ;
									}
								}
								
								if(el.options.length > 0) {
									for(var i=0; i < el.options.length; i++){
										if(el.options[i].selected){
											flag = 1;
											break;											
										}
									}
									if(flag == 0){
										valer = 1;
										showCommonMsg = true;
										if(ctrlMessage != ''){
											msg +=  "=> '"+ctrlMessage+"'  \n";
										}
										else{
											msg +=  "=> Select atleast one value for '"+lblTxt+"'  \n";
										}
									}
								}
							}
							else if(el_type == "checkbox"){
								if(el.checked == false){
									valer = 1;
									showCommonMsg = true;
									if(ctrlMessage != ''){
										msg +=  "=> '"+ctrlMessage+"'  \n";
									}
									else{
										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-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/ ;
							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 "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){	
						if(el.className.indexOf("inputError") == -1) {
							var className = el.className ;
							el.className = className + ' inputError';
						}
						//alert(el.className);
						cht = 1;						
						if(lblCtrl) {
							lblCtrl.className = "errorText";							
						}
						
						if(selectSpan)
							selectSpan.className = "inputError";
							
						break;
					}
					else{
						if(chkField) {
							if(el.className.indexOf("inputError") != -1) {
								var className = el.className ;
								el.className = className.substring(0,className.length - 11);
							}
							
							if(lblCtrl) {
								lblCtrl.className = "normal";
							}
							if(selectSpan)
								selectSpan.className = "";
						}
					}
					
					
				}
				valer = 0;
			}

		}
	} 
	
	if (cht == 1){
		//msg = (showCommonMsg == true ? commonMsg+'\n'+msg : msg);
		msg = commonMsg + msg;
		alert(msg);
		return false;
	}
	else{
		return true;	   
	}               

}



function checkEmail(strng){
	var error = "";
	var emailFilter=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	if (strng == "") {
		error = "You didn't enter an email-address.\n";
	}
	
	else if (!(emailFilter.test(strng))){ 
		error = "Please enter a valid email address.\n";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (strng.match(illegalChars)){
		error = "The email address contains illegal characters.\n";
	}
	return error;
}
