var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s)
{
	for (var i = 0; i < s.length; 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 flg=1;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	///////////////////////////////////////////////////////////////////
	//var strDay=dtStr.substring(0,pos1);
	//var strMonth=dtStr.substring(pos1+1,pos2);
	//////////////////////////////////////////////////////////////////
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	//////////////////////////////////////////////////////////////////
	var strYear=dtStr.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)
		flg=0;
	if (strMonth.length<1 || month<1 || month>12)
		flg=0;
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
    flg=0;
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
    flg=0;
    if(flg==0)
    {	//alert("Date format should be : dd/mm/yyyy");
    alert("Date format should be : mm/dd/yyyy");
    return false;
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{	
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}

    return true
    }

        //--

        function showHide(sel_ref,ctrl_id)
        {	var bWhich = (sel_ref[sel_ref.selectedIndex].value == 'Other');
        var El = document.getElementById(ctrl_id);
        if (!El) return;
        El.style.visibility = (bWhich) ? 'visible' : 'hidden';
        (bWhich) ? El.focus() : El.value = '';
        }

        function BlankCheck(val)
        {
        var char1,vals;
        vals = val.value;

        for (var i=0;i<vals.length;i++)
			{	
				char1 = vals.substring(i,i+1); 
					if ((char1==" "))
					{
						return false;
					}
			}
			return true;
		}	
	function LengthCheck(val)
	{
		var vals;
		vals = val.value;
			if (vals.length < 8 )
			{
				return false;
			}
		return true;
	}	
	function LengthPin(val)
	{
		var vals;
		vals = val.value;
			if (vals.length < 6 )
			{
				return false;
			}
		return true;
	}
	function phno(val)
	{
		var vals;
		vals = val.value;
			if (vals.length < 10 )
			{
				
				var r=isInteger(val)
				if(r==true)
				{
				return false;
				}
			}
		return true;
	}	
function SpaceCheck(val)
	{
	var char1,vals,flag;
	vals=val.value;
				
	if(vals != "") 
	{
	for (var i=0;i<vals.length;i++)
	{	 
		char1 = vals.substring(i,i+1); 
		if(char1==" ")
		{
			flag=1;
		}	
		else
		{
			flag=0;
			break;
		}	
	}		
	if (flag==1)
	{
		val.focus();
		return false;
	}
	else
	{
		return true;
	}
	}		
	else
	{	
	val.focus();	
	return false;
	}			
} 
		function Left(str, n)
		{
			if (n <= 0)    				return "";
			else if (n > String(str).length)	return str;
			else					return String(str).substring(0,n);
		}
		function Right(str, n)
		{
			if (n <= 0)				      	return "";
    		else if (n > String(str).length)		return str;
    		else 
			{
       			var iLen = String(str).length;
				return String(str).substring(iLen, iLen - n);
    		}
		}

		function bTrim(orgValue)
		{
			var tValue="";
			var i=0;
			while(orgValue.charAt(i)==' ')          
				i++;
			for( ;i<orgValue.length;i++) 
			{	
				if((orgValue.charAt(i-1)==' ') && (orgValue.charAt(i)==' ')) continue;
				else tValue=tValue.concat(orgValue.charAt(i));
			}   
			if(tValue.charAt(tValue.length-1)==' ')
				tValue=Left(tValue,tValue.length-1)
			return tValue;
		}

		
	function isEmail(mailValue)
	{
		tEmail="";
		tEmail=bTrim(mailValue);
		if (tEmail.indexOf("@")>0 && tEmail.indexOf(".")>0 && tEmail.length>=5 && tEmail.length-1 != tEmail.lastIndexOf(".") && tEmail.length != tEmail.lastIndexOf("@"))
		{
			tIndex1=tEmail.indexOf("@");
			tIndex2=tEmail.indexOf(".");
			/* This condition is to check, if first character of the email address
			   is either .(dot) or @ and if the character followed by .(dot) or @ is
			   @/.(dot) respectively then email address is not correct. Email will also
			   not correct if there are no or more than one @ sign in email and if 
			   there is a .(dot) or @ sign at the end of the email address and there 
			   is no character after it.
			*/
			if ((tEmail.charAt(0)!='@' || tEmail.charAt(0) !='.') && tEmail.charAt(tIndex1+1)!= '.'  && tEmail.charAt(tIndex1+1)!= ' ' && tEmail.indexOf("@") == tEmail.lastIndexOf("@") && tEmail.lastIndexOf("@") < tEmail.lastIndexOf("."))
			{
				var flag=0;
				for (i=0;i<tEmail.length;i++){
					/* This condition is to check, if there is any space in email or 
					   any @ sign followed by .(dot) then email address is not correct
					*/
					if (tEmail.charAt(i)== ' ' ||( tEmail.charAt(i)== '.' && tEmail.charAt(i+1)=='@'))
					{
						flag=1;
						break;
					}
				}
				if (flag==1)
					return false;
				else
					return true;
			}
			else 
				return false;
		}
		else
			return false;
	}
function SpCharCheck(val)
{
	var char1,vals,fla;
	vals=val.value;
	for (var i=0;i<vals.length;i++)
		{	
		char1 = vals.substring(i,i+1); 
		if ((char1=='>')||(char1=='"')||(char1=="'")||(char1=='<')||(char1=='@')||(char1=='!')||(char1=='~')||(char1=='#')||(char1=='$')||(char1=='%')||(char1=='&')||(char1=='*'))
		{
			fla=1;
			break;	
		}	
		else
		{
			fla=0;
		}	
		}		
	if (fla==1)
	{
		val.focus();
		return false;
	}
	else
	{
		return true;
	}
} 

function CheckDate(Day,Mon,Year) 
 {
   
   var iDD = parseInt(Day);
   var iMM = parseInt(Mon);
   var iYYYY = parseInt(Year);
   var iLeep;
   var Tiyyyy = " ";
   var TDate = " ";
   var Flag = true;
   
   
  
  
   //CheckDate = False;
      if ((iMM > 12) || (iMM <= 0)) 
           return false;
            
      if ((iYYYY < 1000) || (iYYYY > 9999)) 
              return false;
         
     
      if ((iDD > 31) || (iDD <= 0))
              return false;
      
     
      if ((iMM == 4) || (iMM == 6) || (iMM == 9) || (iMM == 11)) 
         {
          if (iDD > 30) 
            return false;
            
         }
     
      if (iMM == 2) 
         {
          
          if (iDD > 29) 
              return false;
          else
             {
               iLeep = iYYYY % 4;
               if (iLeep == 0) 
                 {
                   if ((iYYYY % 1000 == 0) || (iYYYY % 100 == 0))
                   {
                     iLeep = iYYYY % 400;
                   } 
                 }  
               if ((iLeep != 0) && (iDD > 28)) 
                   return false;
             }    
         }
     if (iDD < 10)						//For making 1 -> 01
         TDate = "0" + iDD;
     else
         TDate = iDD;
    
    
     if (iMM < 10) 					//For making 1 -> 01
         TDate = TDate + "/0" + iMM;
     else
         TDate = TDate + "/" + iMM;
    
    //MEdBox.Text = TDate & "/" & iYYYY
     return true;
   }
   
	function OnlyDigit()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if ( (iKey <48 || iKey >57) && iKey!=13)
			window.event.returnValue=false;      	
	}
	function DOBDate()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if ( (iKey <47 || iKey >57) && iKey!=13)
			window.event.returnValue=false;      	
	}
	function OnlyAlpha()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if (! ((iKey >=65 && iKey <=90) || (iKey >=97 && iKey <=122) || iKey==13) )
			window.event.returnValue=false;      	
	} 
	function OnlySpaceAlpha()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if (! ((iKey >=65 && iKey <=90) || (iKey >=97 && iKey <=122) || iKey==32 || iKey==13) )
			window.event.returnValue=false;      	
	}
	
	function OnlySpaceAlphaDigit()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if (! ((iKey >=65 && iKey <=90) || (iKey >=97 && iKey <=122) || iKey==32 || iKey==13 || !(iKey <48 || iKey >57) || (iKey==45)))
			window.event.returnValue=false;      	
	}
	function OnlyEmailAlphaDigit()
	{	var eAny_Event = window.event;
		var iKey = eAny_Event.keyCode;
		if (! ((iKey >=65 && iKey <=90) || (iKey >=97 && iKey <=122) || iKey==13 || !(iKey <48 || iKey >57) || (iKey==45) || (iKey==46) || (iKey==64) || (iKey==95)))
			window.event.returnValue=false;      	
	}
	
	function isValidName(strName)
	{	
		var ch = strName.substring(0, 1);
		if ((ch < "a" || "z" < ch)&&(ch < "A" || "Z" < ch)) 
			return false;
		for (var i = 1; i < strName.length; i++) 
		{
			var ch = strName.substring(i, i + 1);
			if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) &&(ch != ' ') && (ch != '.') ) 
				return false;
		}	  
		return true;
	}
	
	function isValidDate(dateValue)
	{	var today, dt;
		today = new Date();
		if(isDate(dateValue)==false)
			return false;
		else
			dt = new Date(dateValue);
		if(dt.getTime() > today.getTime())
			return false;
		return true;
	}
	
	function isFutureDate(dateValue)
	{	
		var today = new Date();
		var dt = new Date(dateValue);
		if(dt.getTime() > today.getTime())
			return true;
		return false;
	}
	