﻿// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;


$(function(){
    $("#vacationPlan .date-pick").blur(function(){
        var dateStr = $(this).val();
        if ( $.trim( dateStr ) != "" ){
            var dateStr = dateStr.split("/");
            
            if( dateStr[2].length < 4 ){
                dateStr[2] = "20" + dateStr[2];
            }
            
            dateStr = dateStr.join("/");
            $(this).val(dateStr);
        }
    });
    
    
    //this code prevent the enter on textboxes because in that case control was going into server side 
    //and throwing exception.
    if ( $(".gradBorderModule fieldset ul li").length > 0 ){
        $(".gradBorderModule fieldset ul li :text").unbind("keydown").bind("keydown", function(e){
            var code = getKeyCode(e);
            if(code == "13")
            {
                e.preventDefault(); 
                return false;
            }
        });
	}
});

//function to get given key ascii value
function getKeyCode(e) {
			var code;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			
			return code;
}


/***********************************************************************************************************
Function Name : isInteger
Purpose : The Method Checks that the variable entered is integer or not
Parameters : s -  variable needs to be checked             
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
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 Name : stripCharsInBag
Purpose : The Method Search through string's characters one by one and if character is not in bag, append to returnString.
Parameters : s -  string variable             
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
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 Name : daysInFebruary
Purpose : The Method Checks the no. of days in month of february i.e. whether a year is leap year or not
Parameters : year -  year as string             
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
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 Name : DaysArray
Purpose : The Method creates the array of days.
Parameters : n- month 
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
function DaysArray(n) {
	var days = [];
	for (var i = 1; i <= n; i++) {
		days[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {days[i] = 30;}
		if (i==2) {days[i] = 29;}
   } 
   return days;
}
/***********************************************************************************************************
Function Name : isDate
Purpose : The Method Checks whether the entered string is a date object or not
Parameters : dtStr -  string object              
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
function isDate(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(invalidDtFormatErrMsg)
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert(invalidDtErrMsg)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert(invalidDtErrMsg)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert(invalidDtErrMsg)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert(invalidDtErrMsg)
		return false
	}
return true
}
/***********************************************************************************************************
Function Name : compareDates
Purpose : The Method compares the two date objects
Parameters : date1, date2 which needs to be compared
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/		
function compareDates (value1, value2) {

   	var date1, date2;
	var month1, month2;
	var year1, year2;
	
	value1 = new Date(value1);
	value2= new Date(value2);
	
	month1 = value1.getMonth();
	month2 = value2.getMonth();
		
	year1 = value1.getFullYear();
	year2 = value2.getFullYear()
	
	date1 =  value1.getDate();
	date2 =  value2.getDate();

   if (year1 > year2) return -1;
   else if (year1 < year2) return 1;
   else if (month1 > month2) return -1;
   else if (month1 < month2) return 1;
   else if (date1 > date2) return -1;
   else if (date1 < date2) return 1;
   else return 0;
}
/***********************************************************************************************************
Function Name : trimValues
Purpose : The Method trims the white spaces from the string
Parameters : sString -  String needs to be trimmed             
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
function trimValues(sString)
{
	while (sString.substring(0,1) == ' ')
	{		
		sString = sString.substring(1, sString.length);		
	}
		
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
/***********************************************************************************************************
Function Name : checkForNumeric
Purpose : The Method trims the white spaces from the string
Parameters : sString -  String needs to be trimmed             
Created By : Nishit Rastogi
Created On : 30-Mar-2009
***********************************************************************************************************/
function checkForNumeric(strValue) 
{ 
  var numRegex = /^([0-9]*)$/;
 if(!strValue.match(numRegex))
  {     
      return false;
  }
 return true;
}