function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim(value) {
	return LTrim(RTrim(value));
}

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid Email ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid Email ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid Email ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid Email ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid Email ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid Email ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid Email ID")
		    return false
		 }

 		 return true					
	}

function fncMinNumeric(obj){
	var str=obj.value;
	var intCnt=0;
	var intMinNum=1;
	var arrNum=new Array(1,2,3,4,5,6,7,8,9,0);
	for(i=0;i<(arrNum.length-1);i++){
		if(str.indexOf(arrNum[i])!=-1){
			++intCnt;
		}
	}
	if (intCnt > intMinNum-1){
		return true;
	}
	alert('Please enter minimum ' + intMinNum + ' numeric values!');
	obj.focus();
	return false;
}

function getKeyCode(e) {
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return null;
}

function IsNumeric(strString) {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++) {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}


function keyRestrict(e, validchars) {
	var key='', keychar='';
	key = getKeyCode(e);
	if (key == null) return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)
		return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
		
	return false;
}

function validateForm(minpswlen) {
	if (trim(document.frmmain.firstname.value)==""){
		alert("First name missing!");
		document.frmmain.firstname.focus();
		return false;
	}
	
	if (trim(document.frmmain.lastname.value)==""){
		alert("Last Name missing!");
		document.frmmain.lastname.focus();
		return false;
	}
	
	if (!validateUser()){
		return false;
	}



	
	if (minpswlen > 0) {
		if (!validatePwd()){
			return false;
		}
	}
	var emailID=document.frmmain.email;
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Email ID missing!");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.focus();
		return false;
	}

	if (document.frmmain.city.value=="") {
		alert("City name missing!");
		return false;
    }
	
	if (document.frmmain.countryid.selectedIndex==0) {
		alert("Country name missing!");
		return false;
    }
	
	if(document.frmmain.countryid.value==222 && document.frmmain.stateid.selectedIndex==0){
		alert("Please select state");	
		return false;
	}
	
	if(document.frmmain.mphone.value !="" && (!IsNumeric(document.frmmain.mphone.value) || document.frmmain.mphone.value.length!=10)){
		alert("Your mobile number must be 10 digits");
		return false;	
	}
	
	if(document.frmmain.postalcode.value != "" && !IsNumeric(document.frmmain.postalcode.value)){
		alert("Postal Code must be numeric");
		return false;	
	}
	
	return true
}

function validatePwd() {
	var invalid = " "; 
	var minLength = 5;
	var pw1 = document.frmmain.custpassword.value;
	var pw2 = document.frmmain.custpassword2.value;

	if (document.frmmain.custpassword.value.length < minLength) {
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		document.frmmain.custpassword.focus();
		return false;
	}

	if (document.frmmain.custpassword.value.indexOf(invalid) > -1) {
		alert("Sorry, spaces are not allowed.");
		document.frmmain.custpassword.focus();
		return false;
	}
	else {
		if (pw1 == '' || pw2 == '') {
			alert('Please enter your password twice.');
			document.frmmain.custpassword2.focus();
			return false;
		}
		if (pw1 != pw2) {
			alert ("You did not enter the same new password twice. Please re-enter your password.");
			document.frmmain.custpassword2.focus();
			return false;
		}
	}

	return true;
}

function validateUser() {
	var invalid = " "; 
	var minLength = 5;
	var pw1 = document.frmmain.custusername.value;

	if (trim(document.frmmain.custusername.value)==""){
		alert("Username cannot be blank!");
		document.frmmain.custusername.focus();
		return false;
	}
	
	if (document.frmmain.custusername.value.length < minLength) {
		alert('Username must be at least ' + minLength + ' characters long. Try again.');
		document.frmmain.custusername.focus();
		return false;
	}

	if (document.frmmain.custusername.value.indexOf(invalid) > -1) {
		alert("Sorry, spaces are not allowed.");
		document.frmmain.custusername.focus();
		return false;
	}

	return true;
}
