//Trim spaces front and back
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

//Function to check username and password length
function length(elem,msg)
{
var str = trim(elem.value);
if(str.length <3 || str.length>10){
alert("Your "+msg+" must have minimum 3 maximum 10 characters");
elem.focus();
return false;
} else {
return true;
}
}

function special_chars(elem,msg)
{
	var str=trim(elem.value);
	var n=trim(elem.value).charAt(0);
	var arg=/[^0-9a-zA-Z ]/g;
	if(isNaN(n)==false)
	{
		alert(msg+" should not start with number");
		elem.focus();
		return false;
	}
	if( arg.test(n) )
	{
		alert("Special Characters should not come first");
		elem.focus();
		return false;
	}
	return true;
}

function special_chars_whole(elem,msg)
{
	var str=trim(elem.value);
	var n=trim(elem.value)
	var arg=/[^0-9a-zA-Z ]/g;
	if(isNaN(n)==false)
	{
		alert(msg+" should not start with number");
		elem.focus();
		return false;
	}
	else if( arg.test(n) )
	{
		alert("Special Characters should not come");
		elem.focus();
		return false;
	}
	else{
		return true;
	}
	return false;
}

//To check whether the input is empty or not
function isEmpty(elem, helperMsg){
	if(trim(elem.value).length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	else
	{
		return true;
	}
}

//Check all boxes
function checkAll(formObj, is_checked) 
{
	for (var i=0;i < formObj.length;i++) {
	fldObj = formObj.elements[i]
	if (fldObj.type == 'checkbox') {
	fldObj.checked = is_checked
		}
	}
}

//To check whether the input in numeric or not
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if( trim(elem.value).match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.value='';
		elem.focus();
		return false;
	}
}

//Email Validation
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("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}

//To validate whether the file is valid or not(Jpg)
function validateExtension( f, ext )
{//(C)2006 Stephen Chalmers

var badName="", allBlank=true, rx;

rx=new RegExp("[^\.]\."+ext+"\s*$", "i");

for( var i=0,e=f.elements; i<e.length; i++ )
{
if( e[i].type=='file' )
{
e[i].value=e[i].value.replace(/^\s+/,'').replace(/\s+$/,'');

if( e[i].value.length )
{
allBlank=false;
if( !rx.test(e[i].value) )
badName+='\n\n' + e[i].value;
}
}
}

if( allBlank )
alert("Please click on 'Choose' and select a file to upload.")
else
if( badName )
alert( 'A required .'+ext+' extension was not found in:' + badName );

return (badName || allBlank) ? false : true;
}

//Another function for checking extention doc and txt files
function checkExtension_doc(elem){
var str = elem.value.substring(elem.value.lastIndexOf('.'));
if(str==".doc"){
return true;

} 
else if(str==".pdf"){
return true;

}else {
alert("Invalid file.Only .doc and .pdf files are allowed");
elem.value='';
elem.focus();
return false;
}
}

//Another function for checking extention
function checkExtension(elem){
var str = elem.value.substring(elem.value.lastIndexOf('.'));
if(str==".gif"|| str==".jpeg" || str==".jpg"){
return true;

} else {
alert("Invalid file.Only .gif and .jpg images are allowed");
elem.value='';
elem.focus();
return false;
}
}

//Checking whether the email and alternate are same or not
function same_email(email,alternate_email)
{
	if(trim(alternate_email.value).length != 0)
	{
		if( echeck(alternate_email.value) )
		{
			if( trim(alternate_email.value) == trim(email.value) ){
				alert("Alternate Email should not be same as Email");
				alternate_email.value='';
				alternate_email.focus(); // set the focus to this input
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	return true;
}

//Checking whether the contact number and alternate contact number are same or not
function same_cno(cno,acno)
{
	if(trim(acno.value).length != 0)
	{
		if(isNumeric(acno, "Only numbers are allowed"))
		{
			if( trim(acno.value) == trim(cno.value) ){
				alert("Alternate contact number should not be same as contact number");
				acno.value='';
				acno.focus(); // set the focus to this input
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	return true;
}

//Checking whether the password and confirm password are same or not
function same(pwd,cnf_pwd)
{
	if( trim(cnf_pwd.value) != trim(pwd.value) ){
		alert("Confirm Password should be same as Password");
		cnf_pwd.focus(); // set the focus to this input
		return false;
	}
	else
	{
		return true;
	}
}

//Function to check url
function validate_url(elem) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(elem.value)) {
        alert("You must supply a valid URL.");
        return false;
    }
}
