function postit(pcode) {
	test = pcode; 
	size = test.length;
	test = test.toUpperCase(); //Change to uppercase
	//Strip leading spaces
	while (test.slice(0,1) == " ") {
		test = test.substr(1,size-1);size = test.length
	}
	//Strip trailing spaces
	while (test.slice(size-1,size)== " ") {
		test = test.substr(0,size-1);size = test.length
	}
	pstr  = "OK";
	pcode = test; //write back to form field
	 //Code length rule
	if (size < 6 || size > 8) {
		pstr = test + " is not a valid postcode - wrong length";
		// return false;
	}
	//leftmost character must be alpha character rule
	if (!(isNaN(test.charAt(0)))) { 
		pstr = test + " is not a valid postcode - cannot start with a number";
		// return false;
	}
	if (isNaN(test.charAt(size-3))) { //first character of inward code must be numeric rule
		pstr = test + " is not a valid postcode - alpha character in wrong position";
		// return false;
	}
	if (!(isNaN(test.charAt(size-2)))) { //second character of inward code must be alpha rule
		pstr = test + " is not a valid postcode - number in wrong position";
		// return false;
	}
	if (!(isNaN(test.charAt(size-1)))) { //third character of inward code must be alpha rule
		pstr = test + " is not a valid postcode - number in wrong position";
		//return false;
	}
	if (!(test.charAt(size-4) == " ")) {//space in position length-3 rule
		pstr = test + " is not a valid postcode - no space or space in wrong position";
		// return false;
	}
	count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
	if (count1 != count2) {//only one space rule
		pstr = test + " is not a valid postcode - only one space allowed";
		// return false;
	}
	return pstr;
}

function setFocus(aField) {
	document.reaction[aField].focus();
}
function isEmpty(aTextField) {
	if((document.reaction[aTextField].value.length==0)||(document.reaction[aTextField].value==null)) {
		return true;
	} else {
		return false;
	}
}

/**
 * check space  
 * @param String str_key
 * @return Boolean;
 */
function trim(str_key) {
	str_key = rtrim(str_key);
	str_key = ltrim(str_key);
	return str_key;
}

/**
 * function to right trim a string
 * @param String str_key
 * @return Boolean;
 */
function rtrim(str_key) {
	while (str_key.charAt(str_key.length-1)==" ") {
		str_key=str_key.substr(0,str_key.length-1);
	}	
	return str_key;
}

/**
 * function to left trim a string
 * @param String str_key
 * @return Boolean;
 */
function ltrim(str_key) {
	while (str_key.charAt ==" ") {
		str_key=str_key.substr(1);
	}	
	return str_key;
}

/**
 * check spacial character validation
 * @param String elem
 * @return Boolean;
 */
function isAlphabet(elem) {  
	  var alphaExp = /^[a-zA-Z0-9- ]+$/;  
	  if (elem.match(alphaExp)) {  
		   return true;  
	  } else {  
		 return false;  
	  }  
 }
function validateREG() {
	
	form			= document.reaction;
	first_name		= document.reaction.first_name.value;
	last_name		= form.last_name.value;
	screen_name		= form.screen_name.value;
	gender			= form.gender.value;
	bday			= form.bday.value;
	bmonth			= form.bmonth.value;
	byear			= form.byear.value;
	telephone		= form.telephone.value;
	email			= form.email.value;
	postcode		= form.postcode.value;
	password		= form.password.value;
	cpassword		= form.cpassword.value;
	security_code	= form.security_code.value;
	security_code_temp	= form.security_code_temp.value;
	first_name		= trim(first_name);
	last_name		= trim(last_name);
	screen_name		= trim(screen_name);
	checkFirstName	= isAlphabet(first_name);
	checkLastName	= isAlphabet(last_name);
	chkRadio		= jQuery("input[@name='yes']:checked").val();
	if (chkRadio == 1) {
		if (isEmpty("email" || email == "")) {
			alert("Please enter email address.");
			setFocus("email");
			return false;
		}
		if(email!='') {
			var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
			var regex = new RegExp(emailReg);
			email_valid = (regex.test(email)) ? 1 : 0;

			if( emailCheck(email) == false) {
				setFocus("email");
				return false;
			}
			if (!email_valid) {
				alert("Please enter a valid email address.");
				setFocus("email");
				return false;
			}
		}
	}
	if (isEmpty("screen_name" || screen_name == "")) {
		alert("Please enter screen name.");
		setFocus("screen_name");
		return false;
	}

	if (isEmpty("password")) {
		alert("Please enter a password.");
		setFocus("password");
		return false;
	}

	if (isEmpty("cpassword")) {
		alert("Please enter a confirm password.");
		setFocus("cpassword");
		return false;
	}	
	if (cpassword != password) {
		alert("Password not matching with the confirm password.");
		setFocus("cpassword");
		return false;
	}	
	if (isEmpty("telephone")) {
		alert("Please enter a phone no.");
		setFocus("telephone");
		return false;
	}

	var phoneReg	= '^[0-9 ]+$';
	var regex		= new RegExp(phoneReg);
	name_valid		= (regex.test(telephone)) ? 1 : 0;
	if (!name_valid) {
		alert("Your phone no. must be in digit (0-9)");
		setFocus("telephone");
		return false;
	}		
	if (isEmpty("postcode")) {
		alert("Please enter postcode.");
		setFocus("postcode");
		return false;
	}
	pstr = postit(postcode);
	if (pstr!='OK') {
		str = pstr + "\n Enter valid UK postcode.";// + pstr;//"\n Special charachers are not allowed in postcode";
		alert(str);
		setFocus("postcode");
		return false;
	}
	if (isEmpty("first_name") || first_name == "") {
		alert("Please enter first name.");
		setFocus("first_name");
		return false;
	}
	if (checkFirstName == false) {
		alert("Invalid first name.");
		setFocus("first_name");
		return false;
	}
	if (isEmpty("last_name") || last_name == "") {
		alert("Please enter surname.");
		setFocus("last_name");
		return false;
	}
	if (checkLastName == false) {
		alert("Invalid surname.");
		setFocus("last_name");
		return false;
	}
	if (isEmpty("gender")) {
		alert("Please select gender.");
		setFocus("gender");
		return false;
	}
	if (isEmpty("bday")) {
		alert("Please select day.");
		setFocus("bday");
		return false;
	}
	if (isEmpty("bmonth")) {
	alert("Please select month.");
	setFocus("bmonth");
	return false;
	}
	if (isEmpty("byear")) {
		alert("Please select year.");
		setFocus("byear");
		return false;
	}	
	var curdate = new Date();
	var month	= curdate.getMonth();
	month		= month+1;
	day			= curdate.getDate();
	year		= curdate.getFullYear();
	if (byear > year) {
		alert("Year should be Less than or equal to current Year.");
		setFocus("byear");
		return false;
	} else if(byear >= year) {
		if (bmonth > month) {
			alert("Date of birth should be Less than current.");
			setFocus("bmonth");
			return false;	
		} else if(bmonth == month) {
			if(bday >= day) {
				alert("Date should be Less than current Date.");
				setFocus("bday");
				return false;		
			}
		}
	}
	if (isEmpty("security_code")) {
		alert("Please enter security code");
		setFocus("security_code");
		return false;
	}
	if (security_code_temp!= '' && security_code != security_code_temp) {
		alert("Please enter corect code");
		setFocus("security_code");
		return false;
	}
	return true;
}

jQuery(document).ready(function(){
	id = jQuery("input[@name='yes']:checked").val();
	mode = jQuery("input[@name='mode']").val();
	email = jQuery("input[@name='email']").val();
	if(mode == 'update'){
		if(email == '') {
			jQuery("#email").attr('disabled',true);
			jQuery("#no").attr('checked',true);
		} else {
			jQuery("#email").attr('disabled',false);
			jQuery("#yes").attr('checked',true);
		}
	} else {
		if (id == 0) {
			jQuery("#email").attr('disabled',true);
		} else {
			jQuery("#email").attr('disabled',false);
		}
	}
});

function emailTextBocChk(id) {
	if (id == 0) {
		jQuery("#email").attr('disabled',true);
	} else {
		jQuery("#email").attr('disabled',false);
	}
	return true;
}
