// password - between 6-15 chars, uppercase, lowercase, and numeral

function checkPassword (strng,formn,value1) {
var error = "";
 var illegalChars = /[\W_]/; // allow only letters and numbers
  var strTemp = strng;
 strTemp = trimAll(strTemp);
 formn.value=strTemp;  
if(strTemp.length <= 0){
		   error ="Can't avoid, please fill your "+value1+" here.";
   }else  if ((strTemp.length < 6) || (strTemp.length > 15)) {
       error = value1+" should have 6 to 15 characters.";
    }
    else if (illegalChars.test(strTemp)) {
      error = "Your password can't have space";
    } 
    else if (!((strTemp.search(/(a-z)+/)) && (strTemp.search(/(A-Z)+/)) && (strTemp.search(/(0-9)+/)))) {
       error = value1+" Must Contain At Least One Uppercase Letter, One Lowercase Letter, And One Numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng,formn,value1) {
var error = "";
 var illegalChars = /\W/; // allow letters, numbers, and underscores
 var strTemp = strng;
 strTemp = trimAll(strTemp);
 formn.value=strTemp;
if(strTemp.length <= 0){
		   error ="Can't avoid, please fill your "+value1+" here.";
}else if (illegalChars.test(strTemp)) {
    error = "Please Don't Use Special Character In "+value1+".";
}else if((strTemp.length < 4) || (strTemp.length > 20)) {
       error = value1+" should have 4 to 20 characters.";
    }
    
return error;
}       

// email
function checkEmailAddress(mailids,formn)
{

			var error="";
			var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro'
,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
'.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
'.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
'.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
'.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
'.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
'.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
'.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
'.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
'.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
'.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
'.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
'.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
'.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
'.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
'.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
'.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
'.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
'.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
'.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw');

		var mai = mailids;
		var val = true;
		var beforeat="";
		var afterat1="";
		var afterat="";
		var afterat2="";

		var dot=mai.lastIndexOf(".");
		var con=mai.substring(dot, mai.length);
		con=con.toLowerCase();
		con=con.toString();
		//alert(con);
		for(var i=0;i<(arr.length);i++)
		{

			if(con == arr[i])
			{
				val=true;
			
				break;
			}
			else
			{
				val=false;
            }



}
if(val==false){
error = "Please Enter Valid Email Address.\n";
return error;
}


var att=mai.lastIndexOf("@");
beforeat=mai.substring(0,att);
beforeat=beforeat.toLowerCase();
beforeat=beforeat.toString();
var asci1=beforeat.charCodeAt(0);

afterat1=mai.substring(att+1, dot);
afterat1=afterat1.toLowerCase();
afterat1=afterat1.toString();

afterat2=mai.substring(att+1, mai.length);
afterat2=afterat2.toLowerCase();
afterat2=afterat2.toString();

var bool = false;
var dot1="";
var con1="";
if(afterat1.indexOf(".")!=-1)
{
	dot1=afterat1.lastIndexOf(".");
	con1=afterat1.substring(dot1, afterat1.length);
	con1=con1.toLowerCase();
	con1=con1.toString();	
	//alert(con1);
	for(var j=0;j<(arr.length);j++)
	{
		if(con1 == arr[j])
		{
			val=true;
			break;
		}
		else
		{
			val=false;
			
		}
	}
	if(val==false){
error = "Please Enter Valid Email Address.\n";
return error;
}

	
	afterat=afterat1.substring(0, dot1);
	afterat=afterat.toLowerCase();
	afterat=afterat.toString();	
	
	var st = afterat2.substring(afterat1.length);
	if(st.length>3)
	bool = true;
	
}
else
{
	afterat=afterat1;
}

if(beforeat=="" || afterat=="" || beforeat.length>30)
	{
	val=false;
   }
	
if(beforeat.indexOf("..")!=-1)
	{
	val=false;
  }
	//alert(beforeat.substring(beforeat.length-1,beforeat.length));
if(beforeat.substring(beforeat.length-1,beforeat.length)==".")
   {
	val=false;
  }

if(afterat2.length>64 || afterat.length<2)
	{
	val=false;
   }
if(bool==true)	
	{
	val=false;
   }
if((afterat.charCodeAt(0))==45 || (afterat.charCodeAt(afterat.length-1))==45)
	{
	val=false;
   }

if(val==true)
{
	if(asci1 > 47 && asci1 < 58)
		{
	val=false;
  }
		
	if(asci1 < 48 || asci1 > 57)
	{
		
		for(var i=0; i<=beforeat.length-1; i++)
		{
			var asci2=beforeat.charCodeAt(i);
		
			if((asci2<=44 || asci2==47) || (asci2>=58 && asci2<=94) || (asci2==96) || (asci2>=123 && asci2<=127))
			{
				
	val=false;
  	break;
			}
		}
			
		for(var j=0; j<=afterat.length-1; j++)
		{
				
			var asci3=afterat.charCodeAt(j);
			
			if((asci3<=44) || (asci3==46) || (asci3==47) || (asci3>=58 && asci3<=96) || (asci3>=123 && asci3<=127))
			{
				
	val=false;
   	break;
			}
		}	
	}
}
var strTemp = mai;

   strTemp = trimAll(strTemp);
   formn.value=strTemp;
      if(strTemp.length <= 0){
	   error =  "Please Enter Email Address.\n";
   } else if(val==false)
	{
	
	error = "Please Enter Valid Email Address.\n";
return error;
}
return error; 
}


// phone number - strip out delimiters and check for 10 digits

function checkphone(strng,formn,value1) {
var error = "";
var illegalChars=   /[^0-9\s\-\+\(\)]/ //strip out acceptable non-numeric characters
var strTemp = strng;
strTemp = trimAll(strTemp);
 formn.value=strTemp;
     // if(strTemp.length <= 0){
  // error = ;
//}else
if(strTemp.length > 0){
if (strng.match(illegalChars)) {
       error = "Please Enter Valid "+value1;
  
    }else if ((strTemp.length < 7) || (strTemp.length > 20)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
}
return error;
}

// non-empty textbox for first and last name

function isEmptyName(strng,value1,formn) {
var error = "";
var illegalChars = /[^A-Za-z\s\.]/;
var strTemp = strng;

   strTemp = trimAll(strTemp);
   formn.value=strTemp;
      if(strTemp.length <= 0){
	   error ="You Can't Leave The "+ value1 +" Blank";
   } else if((strTemp.length < 2) || (strTemp.length > 20)) {
       error = value1+" should have 2 to 20 characters.";
    } 
   else if (illegalChars.test(strng)) {
      error = "Please Don't Use Special Characters And Numbers In "+value1 ;
    }
return error;	  
}
// non-empty textbox

function isEmpty(strng,value1,formn) {
var error = "";
var illegalChars = /[^A-Za-z0-9\s\.]/;
var strTemp = strng;

   strTemp = trimAll(strTemp);
   formn.value=strTemp;
      if(strTemp.length <= 0){
	  error ="You Can't Leave The "+ value1 +" Blank";
   }  
   else if (illegalChars.test(strng)) {
      error = "Please Don't Use Special Characters In "+value1 ;
    }
return error;	  
}

// non-empty textbox Address

function isEmptyAdd(strng,value1,formn) {
var error = "";
var illegalChars = /[^A-Za-z0-9\s\.\,\'\"]/;
var strTemp = strng;

   strTemp = trimAll(strTemp);
   formn.value=strTemp;
      if(strTemp.length <= 0){
	  error ="You Can't Leave The "+ value1 +" Blank";
   }  
   else if (illegalChars.test(strng)) {
      error = "Please Don't Use Special Characters In "+value1 ;
    }
return error;	  
}

// non-empty textbox Address

function isEmptySpecial(strng,value1,formn) {
var error = "";
var illegalChars = /[^A-Za-z0-9\s\.\,\'\"\@\:\;\<\>\?\/\+\-\(\)\{\}\[\]\*\=\_\#\`\&\$]/;
var strTemp = strng;

   strTemp = trimAll(strTemp);
   formn.value=strTemp;
      if(strTemp.length <= 0){
	  error ="You Can't Leave The "+ value1 +" Blank";
   }  
   else if (illegalChars.test(strng)) {
      error = "Please Don't Use Special Characters In "+value1 ;
    }
return error;	  
}


// non-empty file fields

function isEmptyFields(strng,value,formn) {
var error = "";
 var strTemp = strng;
  if(strTemp==""){
	   error ="You Can't Leave The "+ value +" Blank";
   }  
  
return error;	  
}
// non-empty textbox

function isEmptyText(strng,value,formn) {
var error = "";

 var strTemp = strng;
   strTemp = trimAll(strTemp);
    formn.value=strTemp;
   if(strTemp.length <= 0){
	   error ="You Can't Leave The "+ value +" Blank";
   }  
  
return error;	  
}

// postal code
function isPostal(strng,value,formn) {
var error = "";
var illegalChars = /[^A-Za-z0-9\s]/;
var strTemp = strng;
   strTemp = trimAll(strTemp);
    formn.value=strTemp;
   if(strTemp.length <= 0){
	   error ="You Can't Leave The "+ value +" Blank";
   }else if (illegalChars.test(strng)) {
      error = "Please Don't Use Special Characters In "+value ;
    }else if(strTemp.length < 2)
           error = value+" cannot be less than 2 digits.";
  
return error;	  
}
// valid selector from dropdown list

function checkDropdown(choice1,value1) {
	
var error = "";
    if (choice1 == 0) {
    error = "Please select "+value1;
    }    
return error;
}  

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// multiple radio button

function radio_button_checker(radiovalue)
{
var error = "";
// set var radio_choice to false
var radio_choice = false;
// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < radiovalue.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (radiovalue[counter].checked){
radio_choice = true;
}
}

if (!radio_choice)
{
	
// If there were no selections made display an alert box 
error ="Please select a radio button.";

}
return error;
}

// multiple check button

function isChecked(theForm){
var cnt=0;
var tot=0;
var error= "";	
	for(z=0; z<theForm.length;z++)
	   {
		
		  if(theForm[z].type =='checkbox')
		  {
		  	
		   if(theForm[z].checked == true)
		   {
		   		cnt++;
		   }		   
		  
		  }
	   }
	  
	   if(cnt==0)
	   {
	    error="Please Select Features.\n";
	  }
	return error;
}
//checkbox

function checkBox(checkvalue){
var error= "";
var bool="false";
if(checkvalue)
	{
	bool="true";
	}
if(bool=="false")
{
error="Please Select Terms & Conditions.\n";
}
return error;
}

//compaire password and confirm password

function verPassword(pass,verpass)
{
	var error="";
if(pass!=verpass)
{
error="Confirm Password Is Not Match With Password.\n";
}
return error;
}

//url validation
function isValidURL(url)
{
var error = "";
if(url!=""){
var RegExp =/^((ftp|Http|HTTP|https?):\/\/)+((www|WWW\.))?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
if(!RegExp.test(url))
{
error="Please Insert Valid Url";
}
}
return error;

}



function rightTrim( strValue ) {
/************************************************
DESCRIPTION: Trims trailing whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed.  
      
RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed
   
RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
	
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

//** nalidation for integer use onKeyPress="return validate_for_integers(this, event)" 
function validate_for_integers(inputfield, inputevent) {
	var key;
	var keychar;

	if (window.event) {
		key = window.event.keyCode;
	} else if (inputevent) {
		key = inputevent.which;
	} else {
		return true;
	}
	
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
    		(key==9) || (key==13) || (key==27)) {
		return true;

	// numbers
	} else if (("0123456789.").indexOf(keychar) > -1) {
		return true;
		
	} else {
		return false;
	}
} // end of validate_for_integers()