//Trim fuction remove space from string
//Take string as argument and alert( string

function allvalidation(s,l)
{
var str="";
if(Trim(s) == "")
return "Can not be blank";

if(s.charAt(0) =='_')
return "First character can not be underscore";
if(s.charAt(0) =='.')
return "First character can not be dot";
if(CheckLogin(s) != "true")
return "special quote are not allowed except underscore and dot";
if(s.length > l)
return "Maximum length can not be greater then"+l;
return "true";
}
///////////////////////////////////////////////
function allvalidation(s)
{
var str="";
if(Trim(s) == "")
return "Can not be blank";

if(s.charAt(0) =='_')
return "First character can not be underscore";
if(s.charAt(0) =='.')
return "First character can not be dot";
if(CheckLogin(s) != "true")
return "special Character are not allowed except underscore and dot";
return "true";
}
//////////////////////////////////////////////
function checkdes(s)
{

if(Trim(s) == "")
return "Can not be blank";

if(singlequote(s) != "true")
return "single Character are not allowed ";
return "true";
}
//////////////////////////////////////////////
function intvalidation(i)
{
if(Trim(i) == "")
return "Can not be blank";

i=Trim(i);
if(isNaN(i))
return "can not be string";
else
return "true";
}
/////////////////////////////////////////////////////
function phonevalidation(i)
{
if(Trim(i) == "")
return "Can not be blank";

if(CheckPhone(i) != "true")
return "only allow number,comma and round bracket()";

if(i.length < 10)
return "Can not be less than 10 characters";

return "true";
}
/////////////////////////////////////////////////////
function checkvaliddate(dd,mm,yy)
{

if(checkDate(dd,mm,yy) != "true")
return "invalid date";
return "true";

}
//////////////////////////////////////////////////////
function checkmail(i)
{
if(Trim(i) == "")
return "Can not be blank";

if(emailcheck(i) != "true")
return "Invalide Email ID";
return "true";

}
function LTrim (s)          
{
 	var i=0;
	while(s.charAt(i)==' ')
 	i++;
                s=s.substring(i,s.length);
        	 return s;
}

//Trim function righthand spaces
//Take string as argument and return string

function RTrim (s)         
	{
		if (s.length >0) 
		{
			while(''+s.charAt(s.length-1)==' ') 
				s = s.substring(0,s.length-1);
			return s;
		}
	}

function Trim_for_empty (s)
	{         
		
		var i=0;
		var ch="";
		var j=0;
		for(j=0;j<s.length;j++)
		for(i=0;i<s.length;i++)
		{ ch=s.substring(0,i);
		while(s.charAt(i)==' ')
 	   	        i++;
                 		 s=s.substring(i,s.length);
         		s=ch+s;
		}
		return  s;
		if (s.length > 0) 
		{
			while(''+s.charAt(s.length-1)==' ') 
			s = s.substring(0,s.length-1);
			return s;
		}
	}
function Trim(s)
	{
		if(Trim_for_empty(s) == "")
		{
			return "";
		}
		else
		{		
			s=RTrim(s)
			s=LTrim(s)
			return s;
		}
	}


//CheckLogin function check the login string
//it take string as argument and give alert message in case of wrong  string

function CheckLogin(txtB)
{
var str,lnth,chrCode,nStr
str = txtB
lnth = str.length
nstr=""
	for(i=0;i<lnth;i++)
	{
		chrCode=str.charCodeAt(i)
		
		if((chrCode>=33 && chrCode<=45)||(chrCode==47)||(chrCode>=58 && chrCode<=64)||(chrCode>=91 && chrCode<=94)||(chrCode==96)||(chrCode>=123 && chrCode<=126))

		{
			return "false"
			txtB.value=nstr
			break;
		}
		nstr=nstr+ str.charAt(i)
	}
return "true"
}

function CheckPhone(txtB)
{
	var str,lnth,chrCode,nStr
	str = txtB
	lnth = str.length
	nstr=""
	for(i=0;i<lnth;i++)
	{
		chrCode=str.charCodeAt(i)
		// Only ( ) , and numbers are allowed
		if((chrCode <= 39) ||(chrCode >= 42 && chrCode <=43)||(chrCode >= 45 && chrCode <= 47)||(chrCode >= 58))
		{
			return "false"
			txtB.value=nstr
			break;
		}
		nstr=nstr+ str.charAt(i)
	}
	return "true"
}

function singlequote(txtB)
{
var str,lnth,chrCode,nStr

str = txtB

lnth = str.length
nstr=""

	for(i=0;i<lnth;i++)
	{
		chrCode=str.charCodeAt(i)
		
		if(chrCode==96 || chrCode==39)

		{
			return "false"
			txtB.value=nstr
			break;

		}
		nstr=nstr+ str.charAt(i)
	}

return "true"
}

//function use for textboxes to check blank
function Checktrim(str)
{
var lnth,chrCode
lnth = str.length

	for(i=0;i<lnth;i++)
	{
		chrCode=str.charCodeAt(i)
		
		if((chrCode>=48 && chrCode<=57)||(chrCode>=65 && chrCode<=90)||(chrCode>=97 && chrCode<=122))
		{
			return true			
			break;
		}		
	}
return false
}

function ValidEmail(string) {
    var len=100;
     if(string.length > len)
     return "false";
    if(!string) return "false";
    if(string.charAt(0) =='_') 	   return "false";
    if(string.indexOf(".-") != -1)      return "false";
    if(string.indexOf("-.") != -1)      return "false";
    if(string.indexOf("._") != -1)      return "false";
    if(string.indexOf("_.") != -1)      return "false";
    if(string.indexOf("_-") != -1)      return "false";
    if(string.indexOf("-_") != -1)      return "false";

    if(string.charAt(0) =='0')			return "false";
    if(string.charAt(0) =='1')			return "false";
    if(string.charAt(0) =='2')			return "false";
    if(string.charAt(0) =='3')			return "false";
    if(string.charAt(0) =='4')			return "false";
    if(string.charAt(0) =='5')			return "false";
    if(string.charAt(0) =='6')			return "false";
    if(string.charAt(0) =='7')			return "false";
    if(string.charAt(0) =='8')			return "false";
    if(string.charAt(0) =='9')			return "false";
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)    
        return "true";
    else
        return "false";
}
function checkDate(dd,mm,yy)
{

	var cday,cmonth,cyear
	var d,m,y
	
	var theDate = new Date()
	
	cday = theDate.getDate()
	cmonth = theDate.getMonth() + 1
	cyear = theDate.getFullYear()
	
	day = dd.value
	month = mm.value
	year = yy.value

	if(year < cyear){alert("Invalid Date"); return "false";}
	else if(month < cmonth && year <= cyear){alert("Invalid Date");return "false";}
	else if((day<cday && month<=cmonth) && year<=cyear){alert("Invalid Date");return "false";}

	else if(month==4||month==6||month==9||month==11)
	{
		if(day>30)
		{
			alert("Invalid Date");
			return "false";
		}
	}
	else if(month==2)
	{

		if(year%4==0)
		{
			if(day>29)
			{
				alert("Invalid Date")
				return "false";
			}
		}
	
		else if(year%4!=0)
		{
			if (day>28)
			{
				alert("Invalid Date")
				return "false";
			}
		}
	}
	
        return "true";
	
}
function emailcheck(emailStr) 
{
	var FormData=document.frm
	var dot=0
	var at2=0
	var at
  	
	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 seems incorrect (check @ and .'s)")
		return "false"
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) 
	{
    	alert("The username 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("Destination IP address is invalid!")
			return "false"
	    	}
    	}   		
	}

	var domainArray=domain.match(domainPat)

	if (domainArray==null) 
	{
		alert("The domain name doesn't seem to be valid.")
		document.frmsubscribe.txtemail.focus();
    		return "false"
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length

	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
        	{
	    	alert("The address must end in a three-letter domain, or two letter country.")
   		return "false"
	}

	if (len<2) 
	{
   		var errStr="This address is missing a hostname!"
   		alert(errStr)
		return "false"
	}
	return "true" 

}

function replaceNL(s)
{
	var startindex=0, nlindex;
	while ( ( nlindex = s.indexOf ( "\r\n", startindex) ) != -1 )
	{
	      s = s.substring( 0, nlindex )+ " " + s.substring ( nlindex + 2 );
	      startindex = nlindex + 2;
	}

	startindex = 0;
	while ( ( nlindex = s.indexOf ( "\r\n", startindex) ) != -1 )
	{
	      s = s.substring( 0, nlindex )+ " " + s.substring ( nlindex + 2 );
	      startindex = nlindex + 2;
	}
	return s;
}

function replaceChars(entry,out,add ) 
	{
		//out = "+"; // replace this
		//add = " "; // with this
		temp = "" + entry; // temporary holder
		while (temp.indexOf(out)>-1) 
		{
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add + 
			temp.substring((pos + out.length), temp.length));
		}
		return temp;
	}
	function checkDateArrival(dd,mm,yy,msg,dd1,mm1,yy1)
	{
	var cday=0,cmonth=0,cyear=0;		
	var d,m,y;
	var message="true";
	var theDate = new Date(mm1+"/"+dd1+"/"+yy1);
	cday = theDate.getDate();	
	cmonth = theDate.getMonth() + 1;	
	cyear = theDate.getFullYear();	
	
	day = dd.value;
	month = mm.value;
	year = yy.value;

	if(year < cyear)
		{
			message=msg+" date should be greater than 2 days from current date"; 
			//error=0 ; 
			return message;
		}
	if(month < cmonth && year <= cyear)
	{
		message=msg+" date should be greater than 2 days from current date"; 
		//error=0 ; 
		return message;
	}
	if((day<cday+2 && month<=cmonth) && year<=cyear)
	{
		message=msg+" date should be greater than 2 days from current date"; 
		//error=0 ; 
		return message;
	}
	if(month==4||month==6||month==9||month==11)
	{
		if(day>30)
		{
			message="Invalid "+msg+" Date";
			//error=0;
			return message;
		}
	}
	if(month==2)
	{

		if(year%4==0)
		{
			if(day>29)
			{
				message="Invalid "+msg+" Date";
				//error=0;
				return message;
			}
		}
		if(year%4!=0)
		{
			if (day>28)
			{
				message="Invalid "+msg+" Date";
				//error=0;
				return message
			}
		}
	}
	return message;
}