var dtCh= "/";
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.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)
	//strYear='20'+strYear;
	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){
	 	return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}



//------------------------Time checking

 function checkTime(field)
  { var errorMsg = ""; 
 
 // regular expression to match required time format
  re = /^(\d{1,2}):(\d{2})(:00)?( [AP]M)?$/; 
  if(field != '') 
  { 
    if(regs = field.match(re)) 
        { if(regs[4]) 
            { // 12-hour time format with am/pm
                if(regs[1] < 1 || regs[1] > 12)
                 { errorMsg = "Invalid value for hours: " + regs[1]; }
            } 
            else
             { // 24-hour time format
                if(regs[1] > 23)
                   { errorMsg = "Invalid value for hours: " + regs[1]; } 
              } 
         if(!errorMsg && regs[2] > 59) 
            { errorMsg = "Invalid value for minutes: " + regs[2]; } 
         } 
        else
         { errorMsg = "Invalid time format: " + field; } 
    } 
      if(errorMsg != "")
        {
          alert (errorMsg);
          return false;
        }
      else   
         return true; 
 }
 function DoFocus(fld) {  
    
    }
    
    
 function SetDateTime(obj,tDate)
{
  
   var CurDate=new Date(tDate);
   var CurMonth=formatValue(CurDate.getMonth()+1);
   var CurYear=CurDate.getFullYear();
   var CurDay=formatValue(CurDate.getDate()); 
  
   var len=obj.value.length
   var strHour=0;
   var strMin=0;
   var strTime;
   
   var tt;
   var strdate=CurMonth+'/'+CurDay+'/'+CurYear;
    if (obj.value=='__/__/____ __:__' || obj.value=='00/00/0000 00:00')
   {obj.value='';}
   else if (len==17)
     {}
   else if (len ==0  || isDate(tDate)==false )
     obj.value='';
   else
    {
       if (len<=3) 
       {
         strHour=obj.value.replace('/','');
        
       }
       else if (len>=4 && len<10)
       {
         var st=obj.value.split('/');
         strHour=formatValue(st[0]);
         if (st.length>1)
            strMin=formatValue(st[1]);
       }
       else if (len>=10)
       {
         var st=obj.value.split(' ');
         if (st.length>1)
           {
             var cds=st[0].split('/');
             CurDate=new Date(cds[0]+'/'+cds[1]+'/'+cds[2]);
            
             var str=st[1].split(':');
             if (str.length>0)
               {
                 strHour=str[0];
                 strMin=0;
               }
              if (str.length>1)
                strMin=str[1];
           }
         CurMonth=formatValue(CurDate.getMonth()+1);
         CurYear=CurDate.getFullYear();
         CurDay=formatValue(CurDate.getDate()); 
         strdate=CurMonth+'/'+CurDay+'/'+CurYear
         if (isDate(strdate)==false)
           {
           //obj.value=document.getElementById('ctl00_ContentPlaceHolder1_lblTrvalDateForReset').innerHTML +" "+ "12:00";
          //  alert("Invalid time "+obj.value);
            obj.focus();
           }
         else
            strdate=CurMonth+'/'+CurDay+'/'+CurDate.getFullYear();
        }
       if (strHour==0)
          {
            strHour=12;
            tt='AM';
          }
       else if (strHour<12)
           tt='AM';
       else if (strHour==12)
            tt='PM';
       else if (strHour < 24)
          {
            strHour=strHour-12;
            tt='PM'
           }
         else
           tt='';
       strTime= formatValue(strHour)+":"+formatValue(strMin)+' '+tt;
     
       if ( checkTime(strTime)==false )
        {   
       // obj.value=document.getElementById('ctl00_ContentPlaceHolder1_lblTrvalDateForReset').innerHTML +" "+ "12:00";
           //alert("Invalid time "+obj.value);
           obj.focus();return false;
          }
       else
         { obj.value=strdate+' '+strTime; return true;}
    }
 }



function CheckDateTime(obj,tDate,message)
{
  if (obj.value=='')
    {
      alert (message);
      return false;
     }
  else if (SetDateTime(obj,tDate))
     return true;
  else 
    return false;
}


function SetDate(obj) {

    //var CurDate=new Date();
    // var CurDate=new Date(document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_TabPanel5_Trip1_ucOtherDetail1_txtCurrentDate').value);
    var CurDate = new Date(document.getElementById('ctl00_ContentPlaceHolder1_txtPageDateTime').value);

    var CurMonth = formatValue(CurDate.getMonth() + 1);
    var CurYear = CurDate.getFullYear();
    var CurDay = formatValue(CurDate.getDate());

    var len = obj.value.length
    var strday = CurDay;
    var strmonth = CurMonth
    var stryear = CurYear;

    var strdate = "";
    if (len > 0) {
        if (len == 1) {
            strday = formatValue(obj.value);
            if (strday == 0)
                strday = CurDay;
        }
        if (len == 2 || len == 3) {
            strday = formatValue(obj.value.replace('/', ''));
        }
        else if (len == 4 || len == 5) {
            var st = obj.value.split('/');
            strmonth = formatValue(st[0]);
            if (st.length > 1)
                strday = formatValue(st[1]);
        }

        else if (len > 5) {
            var st = obj.value.split('/');
            strmonth = formatValue(st[0]);

            if (st.length > 2) {
                strday = formatValue(st[1]);
                stryear = formatYear(st[2]);
            }
        }
        strdate = strmonth + '/' + strday + '/' + stryear

        if (isDate(strdate) == false) {
            alert("Invalid date " + obj.value);
            obj.focus();
        }
        else
            obj.value = strdate;
    }
}

function formatYear(num) {
    var strnum = "";
    if (num == "") {
        var CurDate = new Date();
        strnum = CurDate.getFullYear();

    }
    else {
        num = num * 1;
        if (num >= 1111)
            strnum = num;

        if (num <= 9)
            strnum = "200" + (num);
        else if (num <= 99)
            strnum = "20" + (num);
        else
            strnum = num;
    }
    return strnum;
}
