//Validates a date when a event fired like 'onblur' or 'onchange'
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=9999;

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){

//alert(dtStr);
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	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){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		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
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function fnValidateDate(ctrDate)
{
	//~
	//Purpose     : This is called when event is fired
	//Input       : ctrDate   Control 
	//Output      : None
	//Author      : Gopal Dhyani
	//Date        : July 15th 1999
	//Version     : 1.0
	//Revised by  :
	//Revised on  :
	//~
	var vEnteredValue = fnTrim_New(ctrDate.value);
	
	if(vEnteredValue.replace(/(_|\/)/g,'').length == 0) 
	    return true;
	
	if (vEnteredValue.length==0)
	{
		return true;
	}
	var vCheckValid = isDate(vEnteredValue);
	if (vCheckValid)
	{
		return true;
	}
	else
	{
		alert ("Please, enter a valid date in format (mm/dd/yyyy)\nEntered Value is :"+vEnteredValue);
		ctrDate.value=""
		ctrDate.focus();
		return false;
	}
}

//Validates a date when a event fired like 'onblur' or 'onchange'
//created for table maint.
function fnValidateDateMonth(ctrDate)
{
	//~
	//Purpose     : This is called when event is fired
	//Input       : ctrDate   Control 
	//Output      : None
	//Author      : Pawan
	//Date        : Oct 16th 2002
	//Version     : 1.0
	//Revised by  :
	//Revised on  :
	//~
	var vEnteredValue = ctrDate.value+"2002";
	
	
	if (vEnteredValue.length==0)
	{
		return true;
	}
	var vCheckValid = fnValidDate(vEnteredValue);
	if (vCheckValid)
	{
		return true;
	}
	else
	{
		alert ("Please, enter a valid date in format (mm/dd/)\nEntered Value is :"+ctrDate.value);
		ctrDate.value=""
		ctrDate.focus();
		return false;
	}
}

// This function is called from fnValidateDate()

function fnValidDate (vDateEntered)
{
	//~
	//Purpose     : This function checks if the date is valid or not.
	//Input       : ctrDate   Control 
	//Output      : None
	//Author      : Gopal Dhyani
	//Date        : July 15th 1999
	//Version     : 1.0
	//Revised by  :
	//Revised on  :
	//~
	var vLeapYear = false;
	var vDate;
	var vMonth;
	var vYear;
	var vValidDate;
	
	vValidDate=true;
	//If any space found then return false
	for (var i=0;i<vDateEntered.length;i++)
	{
		if (' '==vDateEntered.charAt(i))
		{
			return false;
		}
	}
	
	var count=0;
	for (var i=0;i<vDateEntered.length;i++)
	{
		if ('/'==vDateEntered.charAt(i))
		{
			count+=1;
		}
	}
	
	if (count!=2)
	{
		return false;
	}
	else
	{
		//If first "/" is on third place
		if (vDateEntered.substring(1,2) == "/")
		{
		  vMonth=vDateEntered.substring(0,1);
			
			//If second "/" is on fourth place
		  if (vDateEntered.substring(3,4) == "/")
		  {
		    vDate=vDateEntered.substring(2,3);
		    vYear=vDateEntered.substring(4,vDateEntered.length);
		  }
			//If second "/" is on fifth place
		  else
		  {
		    vDate=vDateEntered.substring(2,4);
		    vYear=vDateEntered.substring(5,vDateEntered.length);
		  }
		}
		//If first "/" is on second place
		else
		{
		  vMonth=vDateEntered.substring(0,2);
			//If second "/" is on sixth place
		  if (vDateEntered.substring(4,5) == "/")
		  {
		    vDate=vDateEntered.substring(3,4);
		    vYear=vDateEntered.substring(5,vDateEntered.length);
		  }
			//If second "/" is on fifth place
		  else
		  {
		    vDate=vDateEntered.substring(3,5);
		    vYear=vDateEntered.substring(6,vDateEntered.length);
		  }
		}
		//Check for numeric and minimum values
		if(isNaN(vYear)||(vYear < 1)||isNaN(vMonth)||(vMonth < 1)|| isNaN(vDate)||(vDate < 1))
		{
			vValidDate=false;
			return false;
		}
		//If length of year in not four digits
		if(vYear.length != 4 )
		{
			vValidDate=false;
			return false;
		}
		//If month is greater than 12 or less than 1
		if ((vMonth > 12) || (vMonth < 1))
		{
			vValidDate=false;
			return false;
		}
		else
		//Check for leap year
		{
			if (( (vYear % 4 == 0) && (vYear % 100 !=0)) || (vYear % 400 == 0))
			{
			  vLeapYear = true;
			}
			//Check for 31 days in the month
			if ((vMonth == 1)||(vMonth == 3)||(vMonth == 5)||(vMonth == 7)||(vMonth == 	8)||(vMonth == 10)||(vMonth == 12))
			{
			  if (vDate > 31)
		  	{ 
		  	  vValidDate=false;
		  	  return false;
  			}
			}
			//Check for 30 days in the month
			else if ((vMonth == 4)||(vMonth == 6)||(vMonth == 9)||(vMonth == 11))
			{
			  if (vDate >30)
			  {
			    vValidDate=false;
			    return false;        
			  }
			}
			//Check for feb
			else if (vMonth == 2)
			{
			  if ((vLeapYear == true) && (vDate >29))
			  {
			    vValidDate=false;
			    return false;
			  }
			  else
			  {
			    if ((vLeapYear == false) && (vDate >28))
			    {
				    vValidDate=false;
				    return false;
    			}
  			}
			}
		}
		
		if (vValidDate)
		{
			 return true;
		}
	}
}


function fnIsValidateYear(ctrYear)
{
	//~
	//Purpose     : This function checks if the year is of 4 digits or not.
	//Input       : ctrYear   Control 
	//Output      : None
	//Author      : Rohini Jain
	//Date        : 2nd Nov 1999
	//Version     : 1.0
	//Revised by  :
	//Revised on  :
	//~	

	//Validate if the value entered is numeric or not.
	var DataCheck=fnValidateNumber(ctrYear);
	if (DataCheck)
	{
		//Validate if the length of the value entered is less than 4.
		if (ctrYear.value.length!=4 && ctrYear.value.length>0)
		{
			alert ("Year cannot be less than 4 digits");
			ctrYear.value="";
			ctrYear.focus();
			return false;
		}
		if (ctrYear.value=='0000')
		{
			alert ("Not a Valid Year");
			ctrYear.value="";
			ctrYear.focus();
			return false;
		}
	}
	else
	{
		alert ("Please, enter a Valid number\nEntered Value is :"+ctrYear.value);
		ctrYear.value="";
		ctrYear.focus();
		return false;
	}
	
	return true;
	
}

//Validates a date when a event fired like 'onblur' or 'onchange'
function fnValidateIntThruDate(vEnteredValue)
{
	//~
	//Purpose     : This is called when event is fired
	//Input       : ctrDate   Control 
	//Output      : None
	//Author      : Gopal Dhyani
	//Date        : July 15th 1999
	//Version     : 1.0
	//Revised by  :
	//Revised on  :
	//~

	
	if (vEnteredValue.length==0)
	{
		return true;
	}
	var vCheckValid = fnValidDate(vEnteredValue);
	if (vCheckValid)
	{
		return true;
	}
	else
	{
		return false;
	}
}
 
 
 function fnTrim_New(What2Trim)
{
//~
//Purpose     : This function removes the spaces of any string from left and right side
//Input       : What2Trim							string
//Output      : Trimed string
//Author      : Anil Madan
//Date        : Nov 01th 1999
//Version     : 1.0
//Revised by  :
//Revised on  :
//~
	var i=0
	var ProcessingStr
	
	if (isNaN(What2Trim))
		ProcessingStr=What2Trim
	else
		ProcessingStr=What2Trim.toString()
	
	for(i=0;i<ProcessingStr.length;++i)
	{
		if (ProcessingStr.charAt(i)!=' ') 
			break;
		if (i == ProcessingStr.length-1)
		{
			ProcessingStr=""
			return (ProcessingStr);
		}
	}
	ProcessingStr=ProcessingStr.substr(i)
	for(i=ProcessingStr.length-1;i>-1;--i)
	{
		if (ProcessingStr.charAt(i)!=' ') 
			break;
	}
	ProcessingStr=ProcessingStr.substr(0,i+1)
	return (ProcessingStr);
	
}
