function LTrim( value ) 
{
         var re = /\s*((\S+\s*)*)/;
          return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value ) {
          var re = /((\s*\S+)*)\s*/;
          return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
          return LTrim(RTrim(value));
}
function len(field,ll,nm){
	if(field.value.length>ll){
		alert(nm)
		field.focus()
		return false
	}
	return true
 }

//Start Date validation

var dtCh= "/";
var tmCh= ":";
var dtTm=" ";
var minYear=1900;
var maxYear=2100;

/*function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
*/
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.value.indexOf(dtCh)
	var pos2=dtStr.value.indexOf(dtCh,pos1+1)
	var pos4=dtStr.value.indexOf(tmCh)
	var pos3=dtStr.value.indexOf(dtTm)
	
	var strDay=dtStr.value.substring(0,pos1)
	var strMonth=dtStr.value.substring(pos1+1,pos2)
	var strYear=dtStr.value.substring(pos2+1,pos3)
	var strHr=dtStr.value.substring(pos3+1,pos4)
	var strMn=dtStr.value.substring(pos4+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	if (strHr.charAt(0)=="0" && strHr.length>1) strHr=strHr.substring(1)
	if (strMn.charAt(0)=="0" && strMn.length>1) strMn=strMn.substring(1)
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	hour=parseInt(strHr)
	minute=parseInt(strMn)
	if (pos1==-1 || pos2==-1 || pos3==-1 || pos4==-1  ){
		alert("The date/time format should be : dd/mm/yyyy HH:MM")
		dtStr.select()
		return false
	}
	
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		dtStr.select()
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		dtStr.select()
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		dtStr.select()
		return false
	}
	if (strHr.length<0 || hour<0 || hour>23){
		alert("Please enter a valid hour")
		dtStr.select()
		return false
	}
	if (strMn.length<0 || minute<0 || minute>59){
		alert("Please enter a valid minute")
		dtStr.select()
		return false
	}
	//alert("Abhishek")
	//if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
//		dtStr.select()
//		return false
//	}
	
return true
}
function isDate1(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.value.indexOf(dtCh)
	var pos2=dtStr.value.indexOf(dtCh,pos1+1)
	var strDay=dtStr.value.substring(0,pos1)
	var strMonth=dtStr.value.substring(pos1+1,pos2)
	var strYear=dtStr.value.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		dtStr.select()
		return false
	}
	
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		dtStr.select()
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		dtStr.select()
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		dtStr.select()
		return false
	}
	//alert("Abhishek")
	//if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
//		dtStr.select()
//		return false
//	}
	
return true
}
//End date validation

function len(field,ll,nm){
	if(field.value.length>ll){
		alert(nm)
		field.focus()
		return false
	}
	return true
 }


function required(field,nm)
{

if(field.value == ""){
		alert(nm)
		field.focus()
		return false
	}
	return true

}


function blank(field,nm)
{
if(field.value!=""){

	var crlf="\r\n"
	text=false
	
	var arr=new Array()
	arr=field.value.split(crlf)
	for(j=0;j<arr.length&&!text;j++){
		a=arr[j]
		if ((a!=crlf)){
			for(i=0;i<a.length&&!text;i++){
				str=a.substring(i,i+1)
				if((str!=" ")&&(str!=""))
					text=true
			}
		}
	}

	if (!text){
		alert(nm)
		field.focus()
		return false;
	}
	return true
}
return true
}

function lenword(field,nm){
	if (field.value!=""){
		var arr=new Array()
		arr=field.value.split(" ")
		for(i=0;i<arr.length;i++){
			if(arr[i].length>70){
				//alert("Sorry, the word '"+arr[i]+"' is too long.  Please enter another word. [max 70 char]")
				alert("Sorry, the word is too long.  Please enter another word. [one word max 70 char]")
				field.focus()
				return false;
			}
		}
	}

	return true
 }
 
 
function dateval(sdate){

var val=sdate.value;
if(val != "")
{

if((val.length>10) ||(val.length<8))
{
	alert(" Please enter the date in (mm/dd/yyyy) format. ")
	sdate.focus();
	return false;
}
else {
	slash1=val.indexOf("/")
	if(slash1==-1)
	{
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		sdate.focus();
		return false;
	}	
	slash2=val.lastIndexOf("/")
	if((slash2==-1)||(slash2==slash1))
	{
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		sdate.focus();
		return false;
	}	
	
	var two=(val.substring(0,slash1));
	if ((two>=0)&&(two<=12))
	{

		mm=two;
	}
	else
	{	
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		
		sdate.focus();
		return false;
	}
	two=(val.substring(slash1+1,slash2));

	if ((two>=0)&&(two<=31))
	{
		dd=two;
	}
	else
	{	
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		
		sdate.focus();
		return false;
	}
	two=(val.substring(slash2+1,slash2+5));
	if ((two>=1900)&&(two<=2075))
	{
		yy=two;
	}
	else
	{	
		alert(" Please enter the date in (mm/dd/yyyy) format.")

		sdate.focus();
		return false;
	}

	if(((dd==29)&&(mm==02))||((dd==29)&&(mm==2)))
	{
	
		if(((yy%4)==0)||((yy%400)==0))
		{
		//valid
		}
		else
		{
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		sdate.focus();
		return false;
		}
	}
	if (((dd>29)&&(mm==2))||((dd==31)&&((mm==2)||(mm==4)||(mm==6)||(mm==9)||(mm==11))))
	{	
		alert(" Please enter the date in (mm/dd/yyyy) format.")
		sdate.focus();
		return false;
	}
}	
return true;
}	
return true;
}


function ValidateEMail(objName)
{
	var sobjValue;
	var iobjLength;
	
	sobjValue=objName.value;
	
	iobjLength=sobjValue.length;
	
	iFposition=sobjValue.indexOf("@");
	iSposition=sobjValue.indexOf(".");
	iTmp=sobjValue.lastIndexOf(".");	
	
	if (iobjLength!=0)
	{
		if ((iFposition == -1)||(iSposition == -1))
		{
			alert("Please enter the E-Mail address in the proper format")
			objName.focus();
			return false;
		}
		else if(sobjValue.charAt(0) == "@" || sobjValue.charAt(0)==".")
		{
			alert("Please enter the E-Mail address in the proper format")
			objName.focus();
			return false;				
		}
		else if(sobjValue.charAt(iobjLength) == "@" || sobjValue.charAt(iobjLength)==".")
		{
			alert("Please enter the E-Mail address in the proper format");
			objName.focus();
			return false;				
		}	
		else if((sobjValue.indexOf("@",(iFposition+1)))!=-1)
		{	
			alert("Please enter the E-Mail address in the proper format")
			objName.focus();
			return false;
		}
		else if ((iobjLength-(iTmp+1)<2)||(iobjLength-(iTmp+1)>3))
		{
			alert("Please enter the E-Mail address in the proper format")
			objName.focus();
			return false;
		}
		else if(sobjValue.indexOf(" ") != -1)
		{	
			alert("Please remove the space between the words")
			objName.focus();
			return false;
		}
		
		else
		{
			return true;
		}		
	}		
}    


function year_validation(field)
{
var valid = "1234567890."
if(field.value!="")
{
		var len=field.value.length;
		var chk=field.value;
		for(i=0;i<len;i++)
		{
			x = chk.charAt(i)
			if(valid.indexOf(x) == -1)
			{
				field.focus()
				field.select()
				alert("Please enter only numeric values in year field.")
				return false;
			}
		}
		if((chk<1900)||(chk>2075)){
			field.focus()
			field.select()
			alert("Please enter the Year between 1900 to 2075.")
			return false;
		}
		
}
return true;
}

function numeric(field)
{
var valid = "1234567890-,."
if(field.value!="")
{
		var len=field.value.length;
		var chk=field.value;
		for(i=0;i<len;i++)
		{
			x = chk.charAt(i)
			if(valid.indexOf(x) == -1)
			{
				field.focus()
				field.select()
				alert("Please enter only numeric values")
				return false;
			}
		}
}
return true;
}  

function isapostrophe(field) {
var str
str = field.value 
var len =str.length
//alert(str+len)
	var i
	for(i=0; i<len;++i){
		if(str.charAt(i)=="'"){
		field.focus()
		field.select()
		alert("cannot insert  APOSTROPHE ( ' ) into any field.")
		return true
		}
	}
	return false
}


function specialcheck(field,nm)
{    
								
	var mikExp = /[$?\\@\\\#%\^\&\*\(\)\[\]\+\_\!\{\}\`\~\=\|]/;
	var strPass = field.value;
	var sFldval = field.value;
	var strLength = strPass.len;
				
	var lchar = strPass.charAt((strLength) - 1);
					
	var lchar1 = strPass.charAt(0)
					
	if((lchar.search(mikExp) != -1)|| (lchar1.search(mikExp) != -1))
	{
	alert("Please enter the valid characters only!");
	field.focus()
	return false;
	}
					
	if(sFldval.indexOf('"') != -1) 
	{
		alert("Please remove the double Quotes");
		field.focus()
		return false;
	}
	if(sFldval.indexOf("'") != -1) 
	{
		alert("please remove the single Quote");
		field.focus()
		return false;
	}
					
	if(sFldval.indexOf(" ") != -1) 
	{
		alert("Please remove the space between the words");
		field.focus()
		return false;
	}
	return true
} 

  
  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 isNum(aStr)
	{
	   
		var reNum=/^[0-9.]+$/;
		if(!reNum.test(aStr)) {
			return false;
		}
		return true;
	}



function invalidNumber(field, message)
{	
		if (!isNum(field.value)){
			alert(message);
			field.focus()
			return true;
		}
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


