﻿// JScript File

function JF_Date()
{
    var js_frmDate, js_toDate, js_dateCtl,js_date;
    var js_fromdateLabel, js_toDateLabel;
    var daysArray = [0,1,2,3,4,5,6,7,8,9];
    var jsMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
    var dayOption="Future";
    var dtCh= "-";
    var minYear=1900;
    
    this.findInArray=findInArray;
    this.GetMonth=GetMonth;
    this.GetAlphaMonth=GetAlphaMonth;
    this.LV_CheckParseInt=LV_CheckParseInt;
    this.JF_SetDay=JF_SetDay;
    this.isDate=isDate;
    this.JF_Removspace=JF_Removspace;
    this.GetDateObj=GetDateObj;
    this.DateDiff=DateDiff;
    this.NumOfNights=NumOfNights;
    this.FormatDeptDate=FormatDeptDate;
    
    js_date=new Date();
    
    this.CurrentSrvrDate=function(val){
                js_date = GetDateObj(val);
    }
    
    this.DayFormatOption=function(val){
                        
                            switch(val.toUpperCase())
                            {
                                case "F":dayOption="Future";break;
                                case "P":dayOption="Past";break;
                            }
                        }
    this.GetObjectById=function(id){
                            return document.getElementById(id);
                        }
    this.FromDate=function(obj){
                        this.js_frmDate=this.GetObjectById(obj);
                    }
    this.FromDateLabel=function(val){
                        js_fromdateLabel=val;
                    }
    this.ToDateLabel=function(val){
                        js_toDateLabel=val;
                    }
    this.ToDate=function(obj){
                        this.js_toDate=this.GetObjectById(obj);
                    }
    function FormatDeptDate(fromDateId, toDateId){
                this.FromDate(fromDateId);
                this.ToDate(toDateId);
                var js_frmdt, js_todt; 
                js_todt =   js_date;
                js_frmdt    = js_date;
                
                if(isDate(this.js_frmDate.value)){ 
                
                    if(this.isDateMoreThanYear(fromDateId)){
                    
                        this.js_frmDate.value = JF_SetDay(js_frmdt.getDate())+"-"+GetAlphaMonth( js_frmdt.getMonth()+1)+"-"+ js_frmdt.getFullYear();
                        js_todt.setFullYear(js_frmdt.getFullYear(), js_frmdt.getMonth(), js_frmdt.getDate()+1);
                        this.js_toDate.value = JF_SetDay(js_todt.getDate())+"-"+ GetAlphaMonth(js_todt.getMonth()+1)+"-"+ js_todt.getFullYear();
                    }
                    else{
                            js_frmdt = GetDateObj(this.js_frmDate.value);
                            js_todt.setFullYear(js_frmdt.getFullYear(), js_frmdt.getMonth(), js_frmdt.getDate()+1);
                    
                            if(isDate(this.js_toDate.value)){                                     
//                                    if(GetDateObj(this.js_frmDate.value) >= GetDateObj(this.js_toDate.value))
//                                    { 
                                            this.js_toDate.value = JF_SetDay(js_todt.getDate())+"-"+GetAlphaMonth(js_todt.getMonth()+1)+"-"+ js_todt.getFullYear();
                                     //} 
                                }
                                else{
                                    this.js_toDate.value = JF_SetDay(js_todt.getDate())+"-"+ GetAlphaMonth(js_todt.getMonth()+1)+"-"+ js_todt.getFullYear();
                                }
                        }
                   } 
                   else{
                        this.js_frmDate.value = JF_SetDay(js_frmdt.getDate())+"-"+GetAlphaMonth( js_frmdt.getMonth()+1)+"-"+ js_frmdt.getFullYear();
                        js_todt.setFullYear(js_frmdt.getFullYear(), js_frmdt.getMonth(), js_frmdt.getDate()+1);
                        this.js_toDate.value = JF_SetDay(js_todt.getDate())+"-"+ GetAlphaMonth(js_todt.getMonth()+1)+"-"+ js_todt.getFullYear();
                   }
    }
    
    this.isDateMoreThanYear=function(id){
                         var js_dt;
                         var dt=new Date();
                         js_dt =  GetDateObj(this.GetObjectById(id).value) ;
                         var yr, js_month;
                         yr = js_date.getFullYear();
                         var curday=js_date.getDate();
                         js_month = js_date.getMonth();     
                                      
                          yr=yr+2;                           
                         
                         var daysInMonth = jsMonthArray[js_month]
                         if(daysInMonth==28){
		                        if(this.isLeapYr(yr))daysInMonth=29;
	                        }
                         dt.setFullYear(yr, js_month, curday );
                         if(js_dt>dt)
                            return true;
                         else
                            return false;   
                    }
    this.isLeapYr=function(inputYear)
                    {
	                    if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
	                    return false;
                    }
    this.isValidFutureDate=function(id){
                             if(!this.isValidDate(id))
                                 return false;
                            else if(this.FormatToCompare(js_dateCtl.value)< js_date)
                                return false;
                            else
                                return true;
                     } 
    this.isValidPastDate=function(id){
                             if(!this.isValidDate(id))
                                 return false;
                            else if(this.FormatToCompare(js_dateCtl.value)> js_date)
                                return false;
                            else
                                return true;
                     } 
    this.FormatToCompare=function(val){
                        var js_arr=val.split(dtCh)
                        var js_temp= new Date()
                        js_temp.setFullYear(js_arr[2],(GetMonth(js_arr[1])-1),js_arr[0])
                        return js_temp;
                    }
      
    this.isValidFromAndToDate=function(fromDateId, fromDateLbl, toDateId, toDateLbl){
                    
                    this.FromDate(fromDateId);
                    this.FromDateLabel(fromDateLbl);
                    this.ToDate(toDateId);    
                    this.ToDateLabel(toDateLbl);
                    
                    if(!this.isValidDate(fromDateId)){
                        alert(js_fromdateLabel + " is not a valid date.");
                         return false;
                    }
                    else if(!this.isValidDate(toDateId)){
                     alert(js_toDateLabel + " is not a valid date.");
                         return false;
                    }
                    else if(GetDateObj(this.js_frmDate.value) >= GetDateObj(this.js_toDate.value)) 
                    { 
                        alert(js_fromdateLabel + " should be less than " + js_toDateLabel); 
                        this.js_frmDate.focus();
                        return false;  
                    } 
                    return true;
                }
     this.isValidDate=function(id)  {
                            js_dateCtl=this.GetObjectById(id);
                            if(js_dateCtl){
                                    if(this.isEmpty(js_dateCtl.value)){//alert("Invalid Date"); 
                                    return false;}
                                    else if(isDate(js_dateCtl.value,id)==false)
				                    {
					                    return false;
				                    }
				                    return true
                                }
                               else{
                                    //alert(id+" is null object.");
                                    return false;
                               }
                     }
     
     this.FormatControlVal=function(id){
                       js_dateCtl=this.GetObjectById(id);
                       var js_newVal=this.FormatDateValue(js_dateCtl.value)
                       if(js_newVal=="")
                       {
                           js_dateCtl.value= JF_SetDay(js_date.getDate())+"-"+Lf_GetAlphaMonth(js_date.getMonth()+ 1)+"-"+js_date.getFullYear();
                       }
                       else
                        js_dateCtl.value=js_newVal;
                    }
          
     this.FormatDateValue=function (val){
     
            var valIndex= findInArray(daysArray,val.substring(0,1));
            var day, month, year, todays;
            todays=new Date();
            day=todays.getDate();
            month=todays.getMonth();
            year=todays.getFullYear();
            if(valIndex>-1)
            {
                if(val.length>2)
                {
                    var thirdChar = val.substring(2, 3);
                    var dateLength = val.length;
                    if (val.indexOf("-")>-1)
                    {
                        
                        var nextChar = "";
                        var tempArr = val.split('-');
                        day = parseInt(LV_CheckParseInt(tempArr[0]));
                        nextChar = tempArr[1].substring(0, 1);
                        if (findInArray(daysArray,nextChar)>-1)
                            {
                                if (tempArr.length > 1)
                                {
                                    month = parseInt(LV_CheckParseInt(tempArr[1]));
                                    month-=1;
                                }
                            }
                            else if (tempArr.length > 1)
                            {
                                month = GetMonth(tempArr[1]);
                                month-=1;
                            }
                        if (tempArr.length > 2)
                         {
                           if (tempArr[2].length < 4)
                            {
                                year = 2000;
                                year += parseInt(LV_CheckParseInt(tempArr[2]));
                            }
                            else
                                year = parseInt(LV_CheckParseInt(tempArr[2]));
                            
                             var dt=new Date();
                                dt.setFullYear(year,month,day);
                                month+=1;
                            if(dayOption=="Future")
                            {
                                if ( dt< new Date())
                                {
                                    //alert("Allowed only future dates.");
                                    return "";
                                }
                            }
                            else if(dayOption=="Past")
                            {
                                if ( dt > new Date())
                                {
                                    //alert("Allowed only previous dates.");
                                    return "";
                                }
                            }
                         }
                         else
                         {
                             var dt=new Date();
                                dt.setFullYear(year,month,day);
                                month+=1;
                            if(dayOption=="Future")
                            {
                                if ( dt<= new Date())
                                {
                                    year+=1;
                                }
                            }
                            else if(dayOption=="Past")
                            {
                                if ( dt > new Date())
                                {
                                    year-=1;
                                }
                            }
                         }
                    }
                    else if(findInArray(daysArray,thirdChar)>-1)
                    {
                        day = parseInt(LV_CheckParseInt(val.substring(0, 2)));
                        if (dateLength >= 4)
                            month = parseInt(LV_CheckParseInt(val.substring(2, 4)));
                        else
                            month = parseInt(LV_CheckParseInt(val.substring(2, 3)));
                            month-=1;
                        if (dateLength > 4)
                        {
                            var tempYear = parseInt(LV_CheckParseInt(val.substring(4)));
                            if (tempYear < 1000)
                            {
                                year = 2000;
                                year += tempYear;
                            }
                            else
                                year = tempYear;
                            var dt=new Date();
                            dt.setFullYear(year,month,day);
                            month+=1;
                            if(dayOption=="Future")
                            {
                                if ( dt< new Date())
                                {
                                    //alert("Allowed only future dates.");
                                    return "";
                                }
                            }
                            else if(dayOption=="Past")
                            {
                                if ( dt > new Date())
                                {
                                    //alert("Allowed only previous dates.");
                                    return "";
                                }
                            }
                        }
                        else
                        {
                            var dt=new Date();
                                dt.setFullYear(year,month,day);
                                month+=1;
                            if(dayOption=="Future")
                            {
                                if ( dt<= new Date())
                                {
                                    year+=1;
                                }
                            }
                            else if(dayOption=="Past")
                            {
                                if ( dt > new Date())
                                {
                                    year-=1;
                                }
                            }
                        }
                        
                    }
			        else
			        {
				        var tempDay = "";
				        var tempMonth = "";
				        var tempYear = "";
				        var mode = "D";
				        for (var i = 0; i < dateLength; i++)
				        {
					        var tempChar = val.substring(i, 1);

					        if (findInArray(daysArray,tempChar)>-1)
					        {
						        if (mode == "D")
							        tempDay += tempChar;
						        else
							        tempYear += tempChar;
					        }
					        else
					        {
						        mode = "Y";
						        tempMonth += tempChar;
					        }
				        }

				        if (tempDay != "")
					        day = parseInt(LV_CheckParseInt(tempDay));
				        if (tempYear != "")
					        year = parseInt(LV_CheckParseInt(tempYear));
				        if (tempMonth != "")
					        month = GetMonth(tempMonth);

					        if (year < 1000)
					        year += 2000;

				        var dt=new Date();
				        dt.setFullYear(year,month,day);
				        if(dayOption=="Future")
                        {
			                if ( dt< new Date())
			                {
				                //alert("Allowed only future dates.");
				                return "";
			                }
			            }
			            else if(dayOption=="Past")
                        {
                            if ( dt > new Date())
                            {
                                //alert("Allowed only previous dates.");
                                return "";
                            }
                        }
        			
			        }
                }
		        else
		        {
			        day = parseInt(LV_CheckParseInt(val));
			        var dt=new Date();
			        dt.setFullYear(year,month,day);
			        month+=1;
			        if(dayOption=="Future")
                    {
				        if ( dt< new Date())
				        {
					        year+=1;
				        }
				    }
				    else if(dayOption=="Past")
                    {
                        if ( dt > new Date())
                        {
                            year-=1;
                        }
                    }
		        }
		        var rtDate = new Date();
			        rtDate.setFullYear(year, month, day);
		            if(rtDate=="NaN")
                      {
                       
                        val="";
                      }
                      else{
                         val= JF_SetDay(day)+"-"+ GetAlphaMonth(month)+"-"+year;
                          val= (isDate(val))?val: JF_SetDay(js_date.getDate())+"-"+GetAlphaMonth( js_date.getMonth()+1)+"-"+ js_date.getFullYear();
                         }
          
            }
	        else
              return "";
              
              
        return val;
        }
     
     
     function GetAlphaMonth(iMonth)
        {
            switch (iMonth)
            {
                case 1:
                    return "Jan";
                case 2:
                    return "Feb";
                case 3:
                    return "Mar";
                case 4:
                    return "Apr";
                case 5:
                    return "May";
                case 6:
                    return "Jun";
                case 7:
                    return "Jul";
                case 8:
                    return "Aug";
                case 9:
                    return "Sep";
                case 10:
                    return "Oct";
                case 11:
                    return "Nov";
                case 12:
                    return "Dec";
                default:
                    return "";
            }
        }
        function GetMonth(sMonth)
        {
            switch (sMonth.toUpperCase())
            {
                case "JANUARY":
                    return 1;
                case "FEBRUARY":
                    return 2;
                case "MARCH":
                    return 3;
                case "APRIL":
                    return 4;
                case "MAY":
                    return 5;
                case "JUNE":
                    return 6;
                case "JULY":
                    return 7;
                case "AUGUST":
                    return 8;
                case "SEPTEMBER":
                    return 9;
                case "OCTOBER":
                    return 10;
                case "NOVEMBER":
                    return 11;
                case "DECEMBER":
                    return 12;
                case "JAN":
                    return 1;
                case "FEB":
                    return 2;
                case "MAR":
                    return 3;
                case "APR":
                    return 4;
                case "JUN":
                    return 6;
                case "JUL":
                    return 7;
                case "AUG":
                    return 8;
                case "SEP":
                    return 9;
                case "OCT":
                    return 10;
                case "NOV":
                    return 11;
                case "DEC":
                    return 12;
                default:
                    return (new Date()).getMonth();
            }
        }
        function isDate(dtStr,ID)
			{
				var js_data=JF_Removspace(dtStr.toLowerCase());
		        var js_chkdata="";
		        if(js_data !="")
		        {
			        var RegExPattern = /^((31(?!([-])(feb|apr|june?|sep|nov)))|((30|29)(?!([-])feb))|(29(?=([-])feb([-])(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])([-])(jan|feb|ma(r|y)|apr|ju(l|n)|aug|oct|(sep|nov|dec))([-])((1[6-9]|[2-9]\d)\d{2})$/;
        	      
			        if ((js_data.match(RegExPattern))) 
				        return true;
			        else 
			          return false ;
		         }
		         else
			          return false ;
			}
	this.isEmpty=function(val){
     
                            for(var i = 0; i < val.length; i++) {
                                    var c = val.charAt(i);
                                    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
                                }
                                return true;
                        }
	    function JF_Removspace(val)
	    {	
		    var ji_ln=val.length;
		    var js_charac='',js_substr;
		    
		    for(i=0 ; i<ji_ln; i++)	
		    {
			    js_substr=val.substring(i,i+1);
			    if(!(js_substr==' ')) 
				    js_charac+=js_substr;
		    }
		     return js_charac;
	    }
        function JF_SetDay(val)
        {
            var rtnVal="";
            rtnVal=val.toString();
            return ((rtnVal.length<=1)?"0"+val:val);
        }
        function LV_CheckParseInt(val)
        {
            switch(val)
            {
                case "08":val=8;break;
                case "09":val=9;break;
                default:break;
            }
            return val;
        }
        function findInArray(obj,val)
        {
            for(var i=0;i<obj.length;i++){
                if(obj[i]==val){
                return i;
                }
             }
             return -1;
        }
        function NumOfNights(frmDatId, toDatId){
                            
                            var js_frmDate, js_toDate;
                             if(!this.isValidDate(frmDatId)){
                                 this.GetObjectById(frmDatId).focus();
                                 return 0;
                             }
                             
                             if(!this.isValidDate(toDatId)){
                                 this.GetObjectById(toDatId).focus();
                                 return 0;
                             }
                            js_frmDate = GetDateObj( this.GetObjectById(frmDatId).value );
                            js_toDate = GetDateObj( this.GetObjectById(toDatId).value );
                            return DateDiff(js_frmDate, js_toDate);
                            
        }
        
        function DateDiff(dateObj1,dateObj2){
                
                var daysCount1, timeTick1, daysCount2, timeTick2;
                  var js_Min = 1000 * 60;
                  var js_Hr = js_Min * 60;
                  var js_Day = js_Hr * 24;
                  timeTick1 = dateObj1.getTime();
                  daysCount1 = Math.round(timeTick1 / js_Day);	
                  timeTick2 = dateObj2.getTime();
                  daysCount2 = Math.round(timeTick2 / js_Day);	
                  return(daysCount2-daysCount1);
        }
        
        function GetDateObj(val){
            var dtArr = val.split('-');
            var rtnDate = new Date();
            var js_month = GetMonth(dtArr[1])-1;
            rtnDate.setFullYear(dtArr[2], js_month, dtArr[0]);
            return rtnDate;
        }
                
}



