/*
	**********************************************************************
    PROGRAM:
      datePicker.js


     DESCRIPTION:
      basic Javascript datepicker
      
      also contains extra date validation and comparison functions


     CREATED BY   : DTurner
     CREATED ON   : 04/06/2006


**********************************************************************
Notes: AT THIS TIME THE	DATEPICKER ONLY SUPPORTS DATE IN THE FOLLOWING FORMAT MM/DD/YYYY
       version 2 will include an extra argument for the date format 

*/


var myMonth;
var myYear;
var myMonthDay;
var myWeekDay;
var myBrowser;

var myObj;
var myDateString;
var destinyObj;
var isFocus=false;


// initiaze helper functions
function _isNumber(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 _daysInFeb (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 _isDate(val){
 
  
 // intialize VARS
  var dtCh= "/";
  var minYear=1920;
  var maxYear=2100;
  
  var daysInMonth = new Array();
  daysInMonth[0] = 0;
  daysInMonth[1] = 31;
  daysInMonth[2] = 29;
  daysInMonth[3] = 31;
  daysInMonth[4] = 30;
  daysInMonth[5] = 31;
  daysInMonth[6] = 30;
  daysInMonth[7] = 31;
  daysInMonth[8] = 31;
  daysInMonth[9] = 30;
  daysInMonth[10] = 31;
  daysInMonth[11] = 30;
  daysInMonth[12] = 31;
  
	var pos1=val.indexOf(dtCh)
	var pos2=val.indexOf(dtCh,pos1+1)
	var strMonth=val.substring(0,pos1)
	var strDay=val.substring(pos1+1,pos2)
	var strYear=val.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){
		return false
	}
	if (month<1 || month>12){
		return false
	}
	if (day<1 || day>31 || (month==2 && day> _daysInFeb(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (val.indexOf(dtCh,pos2+1)!=-1 || _isNumber(stripCharsInBag(val, dtCh))==false){
		return false
	}
return true
}

function _getYear(param){
  if(!_isDate(param))
    return "";
  
  var dtCh= "/";
  
  var pos1=param.indexOf(dtCh)
	var pos2=param.indexOf(dtCh,pos1+1)
	var strMonth=param.substring(0,pos1)
	var strDay=param.substring(pos1+1,pos2)
	var strYear=param.substring(pos2+1)
	strYr=strYear
	year=parseInt(strYr)
  return year;
}

function _getMonth(param){
  
  if(!_isDate(param))
    return "";
  
  var dtCh= "/";
  
  var pos1=param.indexOf(dtCh)
	var pos2=param.indexOf(dtCh,pos1+1)
	var strMonth=param.substring(0,pos1)
	var strDay=param.substring(pos1+1,pos2)
	var strYear=param.substring(pos2+1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	month=parseInt(strMonth);
	 month = month - 1;
	return month;
}

function _getDay(param){
  
  if(!_isDate(param))
    return "";
  
  var dtCh= "/";
  
  var pos1=param.indexOf(dtCh)
	var pos2=param.indexOf(dtCh,pos1+1)
	var strMonth=param.substring(0,pos1)
	var strDay=param.substring(pos1+1,pos2)
	var strYear=param.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	day=parseInt(strDay)
	return day;
}

function _isDateGreater(dt, dtCmp){
  if(! _isDate(dt) || ! _isDate(dtCmp)) 
    return false;
    
  if( _getYear(dt) > _getYear(dtCmp))
   return true;
   
  if( _getYear(dt) < _getYear(dtCmp))
   return false;
   
  // must be the same year
  
  if( _getMonth(dt) > _getMonth(dtCmp))
    return true;
    
  if( _getMonth(dt) < _getMonth(dtCmp))
    return false;
    
  // must be the same month
  
  if( _getDay(dt) > _getDay(dtCmp))
    return true;
  else
    return false; // if equal he is not greater than 
}

function _isDateEqual(dt, dtCmp){
  if(! _isDate(dt) || ! _isDate(dtCmp)) 
    return false;
   
  if( _getYear(dt) > _getYear(dtCmp))
   return false;
   
  if( _getYear(dt) < _getYear(dtCmp))
   return false;
   
  // must be the same year
  
  if( _getMonth(dt) > _getMonth(dtCmp))
    return false;
    
  if( _getMonth(dt) < _getMonth(dtCmp))
    return false;
    
  // must be the same month
  
  if( _getDay(dt) == _getDay(dtCmp))
    return true;
  else
    return false; // if equal he is not greater than 
}


function findPosX(obj)
{
 var curleft = 0;
 if (obj.offsetParent){
  while (obj.offsetParent){
    curleft += obj.offsetLeft
    obj = obj.offsetParent;
  }
 }
 else if (obj.x)
  curleft += obj.x;
 
 return curleft;
}// end  findPosX

function findPosY(obj)
{
var curtop = 0;
if (obj.offsetParent){
  while (obj.offsetParent){
    curtop += obj.offsetTop
    obj = obj.offsetParent;
  }
}
else if (obj.y)
  curtop += obj.y;
  
 //if(myBrowser == "IE")
   curtop = curtop + 25;
 
return curtop;
} // end findPosY

  function getMonthTitle(val){
    var arMth = ["Jan", "Feb", "Mar", "Apr", "May", "June","July", "Aug", "Sept", "Oct", "Nov", "Dec"];
    return arMth[val];
  }
  function getWeekDayTitle(val){
    var arDy = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat"];
    return arDy[val];
  }
  
  function getMaxDays(mth, yr){
    arDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if(isLeapYear(yr) && mth ==2)
      return 29;
    else
      return arDays[mth];
  }
  
  function isLeapYear(yr){
   if (isNaN(yr) || yr.length !=4 )
      return false
   else{
    var val = parseInt(yr);
    var dateVal = new Date(yr, 1, 29);
    if( dateVal.getMonth() == 1)
      return true;
    else
     return false;
   }
  } // end function
  function firstDayOfWeek(mth, yr){
    var tmpDate = new Date(yr, mth, 1);
    return tmpDate.getUTCDay();
  }
  
  function setDays(yr, mth, dy){
    // get max days per 
    tmpDy = dy
    if(dy > getMaxDays(mth, yr))
      tmpDy = getMaxDays(mth, yr);
      
    var tmpDate = new Date(yr, mth, tmpDy);
    myWeekDay = tmpDate.getUTCDay();
    myMonthDay = tmpDate.getUTCDate();
    return tmpDate.getUTCDay();
  }
  
  function assign(dy, mth, yr, val){
    var newDt = (mth + 1) + "/" + dy+"/" + yr
    //alert(newDt);
    document.getElementById(destinyObj).value = newDt;
    myMonthDay = stripDay(val);
    document.getElementById(val).style.backgroundColor = "#D1D1D1";
    // looop through all the other days and set style to not selected
    setDays(myYear, myMonth, myMonthDay);
    for(var x = 1; x<=getMaxDays(myMonth);x++){
        document.getElementById("day" + x).style.backgroundColor = "#FFFFFF";
    }
    document.getElementById("day" + myMonthDay).style.backgroundColor = "#D1D1D1";
     document.getElementById("dateTitle").innerHTML = getWeekDayTitle(myWeekDay) + "&nbsp;" + getMonthTitle(myMonth) + "&nbsp;" +  myMonthDay + "&nbsp;" + myYear;
    return false;
  }
  
  function stripDay(val){
    var myVal = val.slice(3, val.length);
    if (isNaN(parseInt(myVal)))
      return 0;
    else
      return myVal;
  }
  
  function setMyTitle(val){
    myTitle = getWeekDayTitle(myWeekDay) + "&nbsp;" + getMonthTitle(myMonth) + "&nbsp;" +  myMonthDay + "&nbsp;" + myYear;
    //alert(val);
    document.getElementById(val).innerHTML = myTitle;
    return false
  }
  
  function hideCalendar(i){
      isFocus = false;
      if(document.getElementById("calendar").style.visibility="visible"){
        // wait 2 seconds and hide the calendar
        window.setTimeout('executeHide()', i); 
      }
     }
     
     function showCalendar(){
      isFocus = true;
     }
     
     function executeHide(){
      if(! isFocus){
        document.getElementById("calendar").style.visibility="hidden";
      }
     }
  
  function build(){
    
    var oStyle =  "style='font-size:9px;border:0px;font-size:9px;'";
    var iStyle =  "style='font-size:9px;background-color:#D1D1D1;border:1px solid #C6C6C6'";
    var arDaysinWeek = ["Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    
    var gWin = "<table style='border:0px;' border='0'>";
    gWin += "<tr><td bgcolor='#C6C6C6'>";
    gWin += "<table width='100%' bgcolor='#ffffff' cellpadding='0' cellspacing='0' style='border:0px;'>";
    gWin += "<tr><td nowrap align='center' style='padding:2px;border:0px;'>";
    gWin += "<b><a href='javascript:browseCalendar(1)'>&lsaquo;</a></b>&nbsp;&nbsp;&nbsp;&nbsp;<span id='dateTitle' style='font-size:10px;' >" 
           + getWeekDayTitle(myWeekDay) + "&nbsp;" + getMonthTitle(myMonth) + "&nbsp;" +  myMonthDay + "&nbsp;" + myYear
           + "</span>&nbsp;&nbsp;&nbsp;&nbsp;<b><a href='javascript:browseCalendar(2)'>&rsaquo;</a></b>";
    gWin += "</td></tr>";
    gWin += "<tr bgcolor='#000000'><td height=1 colspan=2></td></tr>";
    gWin += "<tr><td nowrap align='center'>";
    gWin += "<table width='100%' bgcolor='#ffffff' style='border:1px;'>";
    gWin += "<tr>";
      for(x = 0; x < arDaysinWeek.length; x++){
        gWin = gWin + "<td " + oStyle + " align='center'>" + arDaysinWeek[x] + "</td>";
      }
      gWin += "</tr><tr>";
      // populate month 
      // figure the blank days
      for(var x = 0; x < firstDayOfWeek(myMonth,myYear); x++){
        gWin += "<td " + oStyle + ">&nbsp;</td>";
      }
      for(var x = 1; x <= getMaxDays(myMonth, myYear); x++){
      var myStyle = oStyle;
        if(x == myMonthDay)
          myStyle = iStyle;
        gWin = gWin + "<td id='day" + x + "' " + myStyle + " align='center' " + 
                      "onmouseover=\"this.style.backgroundColor='#D1D1D1'\" " + 
                      "onmouseout=\"if(myMonthDay != " + x + ")this.style.backgroundColor='#ffffff';\" " + 
                      "onclick='javascript:assign(" + x + ", myMonth, myYear, this.id)'> " +
                      "<a href='javascript:void 0' style='text-decoration:none;color:#000000'>" +
                       x + "</a></td>";
          
        var tmpDt = new Date(myYear, myMonth, x);
        if(tmpDt.getUTCDay() == 6)
          gWin += "</tr><tr>";
      }
      tmpDt = new Date(myYear, myMonth, (x -1));
      if(tmpDt.getUTCDay() < 6)
        for(x=tmpDt.getUTCDay(); x < 6; x++ ){
          gWin += "<td " + oStyle + " align='center'></td>";
        }
      gWin += "</tr></table></td></tr></table></td></tr></table>";
    document.getElementById(myObj).innerHTML = gWin;
  }
  
  function initGlobals(obj){
  if(navigator.userAgent.indexOf("MSIE")< 0)
    myBrowser = "MOZ";
  else
    myBrowser = "IE";
   
   
   if(myDateString == ""){
     var now = new Date();  
     myMonth = now.getMonth();
     if (myBrowser == "IE")
       myYear = now.getYear();
     else
       myYear = now.getYear() + 1900;
      
     myMonthDay = now.getUTCDate() ;
     myWeekDay = now.getUTCDay() ;
   }else{
    
    myMonth = _getMonth(myDateString);
    myYear = parseInt(_getYear(myDateString));
    myMonthDay = _getDay(myDateString);
    
    var myDate=new Date();
    myDate.setFullYear(myYear,myMonth,myMonthDay);
    myWeekDay = myDate.getUTCDay() ;
    
   }
   
    myObj = obj
  }
  

  
  function browseCalendar(val){
    if (val == 1){
      if(myMonth == 0){
        myMonth = 11;
        myYear = myYear - 1;
        setDays(myYear, myMonth, myMonthDay);
      }
      else
        myMonth = myMonth - 1;
        setDays(myYear, myMonth, myMonthDay);
    }
    else if(val == 2){
      if(myMonth == 11){
        myMonth = 0;
        myYear = myYear + 1;
        setDays(myYear, myMonth, myMonthDay);
      }
      else
        myMonth = myMonth + 1;
        setDays(myYear, myMonth, myMonthDay);
    }
    var now = new Date(myYear, myMonth, myMonthDay);
    build();  
    var newDt = (myMonth + 1) + "/" + myMonthDay+"/" + myYear
    document.getElementById(destinyObj).value = newDt;
  }
function initDatePicker(obj){
    initGlobals(obj);
    build();
}

function handleDatePicker(calendarDiv, caller,destiny){
  
  destinyObj = destiny;
  if(document.getElementById(calendarDiv).style.visibility=='hidden'){
    var o = document.getElementById(caller)
    var x,y
    x = findPosX(o);
    y = findPosY(o);
    
    document.getElementById(calendarDiv).style.top = y; //(document.getElementById(calendarDiv).style.top);
    document.getElementById(calendarDiv).style.left = x - 130;
    document.getElementById(calendarDiv).style.visibility='visible';
    document.getElementById(caller).value="Hide";
  }
  else{
    document.getElementById(calendarDiv).style.visibility='hidden';
    document.getElementById(caller).value="Date";
  }
  
  
  if(_isDate(document.getElementById(destiny).value))
    myDateString=document.getElementById(destiny).value;
   else{
    myDateString = "";
    document.getElementById(destiny).value = "";
   }
  
  initDatePicker(calendarDiv);
}
