// JavaScript Document

// isAlpha() check is alpha...this fucntion return true whether input is non alpha
// isSplChar() check is Special character  ... this function retuen ture wheher input contain special character 
// emailCheck() check is Email...this fucntion return true whether input is non Email
// trim() trim alpha or nuneric character
// trim() trim alpha or nuneric character 
// ReplaceAll(Source,stringToFind,stringToReplace)

function ReplaceAll(Source,stringToFind,stringToReplace){

  var temp = Source;

    var index = temp.indexOf(stringToFind);

        while(index != -1){

            temp = temp.replace(stringToFind,stringToReplace);

            index = temp.indexOf(stringToFind);

        }

        return temp;

}

// repalce all function

// check alpha character start
function isAlpha(data){
	var numStr="1234567890-=\~!@#$%^&*()_+|?><:{},`;";
	var thisChar;
	var counter=0;
	for(var i=0; i < data.length; i++)
		{
			thisChar=data.substring(i,i+1);
			if(numStr.indexOf(thisChar)==-1)
			{counter++;}
		}
		if(counter==data.length)
		{return true;}
		else
		return false;
}

// check alpha character end


/*Check for a special character'***/
function isSplChar(str)
{	
	var spchar, getChar, SpecialChar;	
	spchar="!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	getChar='Empty';
	SpecialChar='No';
	var spchars ="!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	for(var i=0; i < str.length; i++)
	{
		for(var j=0; j< spchars.length; j++)
		{			
			if(str.charAt(i)== spchar.charAt(j))
			{			
				SpecialChar='Yes';
				break;
			}
			else
			{
				if (str.charAt(i)!=' ')
				getChar='Normal';
			}
		}		
	}
	if (SpecialChar == 'Yes')
	{
		alert('Please do not enter any of the following characters: \n ' + spchars);	
		return true;
	}
	else if (SpecialChar == 'No')
	{
		return false;
	}
} 
//trim Special cheracter End

//trim functionality start

function trim(s)
{
	return rtrim(ltrim(s));
}

function ltrim(s)
{
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s)
{
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}

// trim functionality


// email validation for all forms
function emailCheck(emailStr) {
	
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:!\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");	
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null){
		//alert("Email address is incorrect (check @ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];	
	for (i=0; i<user.length; i++){
		if (user.charCodeAt(i)>127){
			alert("Email doesn't seem to be valid.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++){
		if (domain.charCodeAt(i)>127){
			alert("Email domain name contains invalid characters.");
			return false;
		}
	}
	if (user.match(userPat)==null){
		alert("Email doesn't seem to be valid.");
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null){
		for (var i=1;i<=4;i++){
			if (IPArray[i]>255){
				alert("Email Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++){
		if (domArr[i].search(atomPat)==-1){
			alert("Email domain name does not seem to be valid.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1){
		alert("Email address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	if (len<2){
		alert("Email address is missing a hostname!");
		return false;
	}
	return true;
}
//email check end

// function to check the password length
function checkPassword(){
	var password = document.getElementById("password_dem0").value;

	var status = false;
	if(password.length < 6){
		alert("Password should be more than 6 characters!");
		status = false;
	}else{
		status = true;
	}
	return status;
}


//back up reffrence file end
