﻿var calDate;
var iYear;
var iMonth;
var iDay;
var sDepartDateID1;
var sReturnDateID2;
var sFormat;
var cHiddenControls;

var IE = document.all?true:false

if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;

var tempX = 0
var tempY = 0


function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX + 10
    tempY = e.pageY - 20
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
  return true
}


function doShowCalendar(id1, id2, fmt){

   if(doShowCalendar.arguments.length > 3){
        var i;
        cHiddenControls = new Array();
        for(i = 3; i <= doShowCalendar.arguments.length - 1; i ++){
           cHiddenControls[i - 3] = doShowCalendar.arguments[i]; 
           
           if (document.layers){
            document.layers[doShowCalendar.arguments[i]].visibility = "hidden"; 
           }
           else if(document.all){
            document.all[doShowCalendar.arguments[i]].style.visibility = "hidden"; 
           }                     
            
        }
    }
    
    sDepartDateID1 = id1;
    sReturnDateID2 = id2;
    sFormat = fmt;
    
    if(document.getElementById(id1).value != ""){
        try{
            var sDateParts;
            sDateParts = document.getElementById(id1).value.split("/");
            
            if(sFormat == "yyyy/mm/dd"){
                calDate = new Date(sDateParts[0], sDateParts[1] - 1, sDateParts[2]);
            }else{
                if(sFormat == "yy/mm/dd"){
                    calDate = new Date(2000 + Math.abs(sDateParts[0]), sDateParts[1] - 1, sDateParts[2]);
                }else{
                    if(sFormat == "dd/mm/yy"){
                        calDate = new Date(2000 + Math.abs(sDateParts[2]), sDateParts[1] - 1, sDateParts[0]);
                    }else{
                        if(sFormat == "dd/mm/yyyy"){
                            calDate = new Date(sDateParts[2], sDateParts[1] - 1, sDateParts[0]);
                        }else{
                            calDate = new Date();
                            calDate.setDate(calDate.getDate() + 3); //set the starting date to 3 days ahead of current as per the booking requirement
                        }
                    }
                }
            }
        }catch(e){
            calDate = new Date();
            calDate.setDate(calDate.getDate() + 3); //set the starting date to 3 days ahead of current as per the booking requirement
        }
    }else{
        calDate = new Date();
        calDate.setDate(calDate.getDate() + 3); //set the starting date to 3 days ahead of current as per the booking requirement
    }
    
    iYear = calDate.getFullYear();
    iMonth = calDate.getMonth();
    iDay = calDate.getDate();
    
    if (sReturnDateID2 == null)
    {
        
    }
    else{
        chkDate = new Date();
        chkDate.setDate(chkDate.getDate() + 3);        
    }
    
    chkYear = chkDate.getFullYear();
    chkMonth = chkDate.getMonth();
    chkDay = chkDate.getDate();
    
    doCalendar();
    
    var cal;
    cal = document.getElementById("cal").style;
    cal.left = tempX + "px";
    cal.top = tempY + "px";
    cal.visibility = "visible";
}

function doCalendar(){
	var sFirstDay, iFirstWeekDay, iMonthLength;
	var y = iYear;
	var m = iMonth;
	var i , j;
	
	var sHTML;
	
	//Create the month 'selected' array;
	var sMonthSelect = new Array();
	for(i = 0; i < 12; i ++){
	    if(i == iMonth){
	        sMonthSelect[i] = " selected='selected'";
	    }else{
	        sMonthSelect[i] = "";
	    }
	}
	
	sHTML = "<table class='calendar' cellspacing='0' cellpadding='0' border='0' width='130' style='background-color:white;border:1px solid #D3D3D3;'>";
	sHTML += "<tr><td><select id='cal_month' class='select' style='width:80px' onchange='doMonthChange();'>";
	sHTML += "<option value='0'" + sMonthSelect[0] + ">January</option>";
	sHTML += "<option value='1'" + sMonthSelect[1] + ">February</option>";
	sHTML += "<option value='2'" + sMonthSelect[2] + ">March</option>";
	sHTML += "<option value='3'" + sMonthSelect[3] + ">April</option>";
	sHTML += "<option value='4'" + sMonthSelect[4] + ">May</option>";
	sHTML += "<option value='5'" + sMonthSelect[5] + ">June</option>";
	sHTML += "<option value='6'" + sMonthSelect[6] + ">July</option>";
	sHTML += "<option value='7'" + sMonthSelect[7] + ">August</option>";
	sHTML += "<option value='8'" + sMonthSelect[8] + ">September</option>";
	sHTML += "<option value='9'" + sMonthSelect[9] + ">October</option>";
	sHTML += " <option value='10'" + sMonthSelect[10] + ">November</option>";
	sHTML += "<option value='11'" + sMonthSelect[11] + ">December</option>";
	sHTML += "</select>";
	sHTML += "</td><td>";
	sHTML += "<select id='cal_year' class='select' style='width:50px' onchange='doYearChange();'>";
	
	var dNow = new Date();
	for(i = dNow.getFullYear(); i < dNow.getFullYear() + 3; i ++){
	    if(i == iYear){
	        sHTML += "<option value='" + i + "' selected='selected'>" + i + "</option>";
	    }else{
	        sHTML += "<option value='" + i + "'>" + i + "</option>";
	    }
	}
				    
	sHTML += "</td></tr><tr><td id='calendar' align='center' colspan='2'></td></tr>";
	sHTML += "<tr><td colspan='2'>";
	sHTML += "<table class='calendar' cellspacing=0 cellpadding=0 border=0 width='130'>";
	sHTML += "<tr align='center'>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>M</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>T</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>W</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>T</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>F</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>S</b></td>";
	sHTML += "<td style='border-bottom: silver 1px solid'><b>S</b></td>";	
	sHTML += "</tr><tr>";

	sFirstDay = new Date(y, m, 1);
	iFirstWeekDay = sFirstDay.getDay();
	iMonthLength = getMonthLength(y, m);

	if (iFirstWeekDay == 0){
		for (j = 0; j < 6; j++){
			sHTML += "<td width=20>&nbsp;</td>"
		}
	}else{
		for (j = 0; j < iFirstWeekDay - 1; j++){
			sHTML += "<td width=20>&nbsp;</td>"
		}
	}

	for (j = 1; j < iMonthLength + 1; j++){
		if ((j + iFirstWeekDay - 2) % 7 == 0){
			sHTML += "</tr><tr>";
		}
		if(y == iYear && m == iMonth && j == iDay){
		
		    if(m < chkMonth && y == chkYear){
		        sHTML += "<td id=date_" + y + "_" + m + "_" + j + " width=20 style='border:solid 1px white; color:gray;' align=center>" + j + "</td>";
		    }
		    else{
			    sHTML += "<td id=date_" + y + "_" + m + "_" + j + " width=20 style='background:#FFCC00; cursor:pointer; font-weight:bold;' align=center onclick=doDay('" + y + "_" + m + "_" + j + "')>" + j + "</td>";
			}
		}else{
				  
		    if((y == chkYear && m == chkMonth && j < chkDay) || (y == chkYear && m < chkMonth))
		    {
		     sHTML += "<td id=date_" + y + "_" + m + "_" + j + " width=20 style='border:1px solid white; color:gray;' align=center>" + j + "</td>";
		    }
		    else{		    
			sHTML += "<td id=date_" + y + "_" + m + "_" + j + " width=20 style='border:1px solid white; cursor:pointer; font-weight:bold;' align=center onclick=doDay('" + y + "_" + m + "_" + j + "')>" + j + "</td>";
			}
		}
	}
	sHTML += "</tr>";
	sHTML += "</table>";
	sHTML += "</td></tr>";
	sHTML += "<tr><td colspan='2'>";
	sHTML += "<table style='width:130px;'>";
	sHTML += "<tr><td align='right'><input type='button' class='calButton' value='Cancel' onclick='doCancel();' style='height:15px; font-size:7.5pt;'/></td></tr>";
	sHTML += "</table>";
	sHTML += "</td></tr>";
	sHTML += "</table>";
	
	document.getElementById("cal").innerHTML = sHTML;
	document.getElementById("date_" + iYear + "_" + iMonth + "_" + iDay).style.border = "1px solid #008BC2";
}

function doYearChange(){
    iYear = document.getElementById("cal_year").value;
    doCalendar();
}

function doMonthChange(){
    iMonth = document.getElementById("cal_month").value;
    doCalendar();
}

function doDay(dt){
	iYear = dt.substr(0, 4);
	iMonth = dt.substr(5, 2).replace("_", "");
	iDay = dt.substr(dt.lastIndexOf("_") + 1);

	doSubmit();
}

function getMonthLength(y, m){
	if (m == 0 | m == 2 | m == 4 | m == 6 | m == 7 | m == 9 | m == 11){
		return(31);
	}else{
		if (m == 3 | m == 5 | m == 8 | m == 10){
			return(30);
		}else{
			if (m == 1){
				if (y % 4 == 0){
					return(29);
				}else{
					return(28);
				}
			}		
		}
	}
}

function doSubmit(){
	var sDate, sYear, sMonth, sDay;
	
	if(iYear.toString().length < 3){
	    sYear = 2000 + Math.abs(iYear);
	}else{
	    sYear = iYear.toString();
	}
	
	sMonth = Math.abs(iMonth) + 1;
	if(sMonth.toString().length == 1){
	    sMonth = "0" + sMonth;
    }
    
    if(iDay.toString().length == 1){
        sDay = "0" + iDay;
	}else{
	    sDay = iDay;
	}
	
	sDate = sFormat;
	sDate = sDate.replace("dd", sDay);
	sDate = sDate.replace("mm", sMonth);
	
	if(sFormat.indexOf("yyyy") != -1){
	    sDate = sDate.replace("yyyy", sYear);
	}else{
	    sDate = sDate.replace("yy", sYear.substring(2));
	}

	
	document.getElementById(sDepartDateID1).value = sDate;

    if(sReturnDateID2 != null){
        removeDateTip(sReturnDateID2);
        document.getElementById(sReturnDateID2).value = sDate;
    }
    
    document.getElementById("cal").style.visibility = "hidden";
    doShowHiddenControls();
}

function doCancel(){
	document.getElementById("cal").style.visibility = "hidden";
	doShowHiddenControls();
}

function doShowHiddenControls(){
    var i;
    for(i = 0; i < cHiddenControls.length; i ++){
        document.getElementById(cHiddenControls[i]).style.visibility = "visible";
    }
    cHiddenControls = null;
}
