//Global elements
var nextMonthImage = document.createElement("img");
var lastMonthImage = document.createElement("img");
nextMonthImage.setAttribute("src", "http://affiliates.hotelscombined.com/StyleImages/arrowrightmonth.gif");
lastMonthImage.setAttribute("src", "http://affiliates.hotelscombined.com/StyleImages/arrowleftmonth.gif");
//Global vars
var dayNames = null; 
var mn = null; 
try
{
    if (hcCultureDays != null) {
        dayNames = hcCultureDays;
    }

    if (hcCultureMonths != null) {
        mn = hcCultureMonths;
    }
}
catch (err)
{
    dayNames = new Array("S", "M", "T", "W", "T", "F", "S");
    mn = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
}
var mnn = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
var mnl = new Array('31', '29', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
var calvalarr = new Array(42);
var calvalarr1 = new Array(42);
var seperator = '/';
//Constraints
var enableDateFrom = new Date();
var enableDateTo = new Date();
enableDateFrom.setDate(enableDateFrom.getDate() - 1);
enableDateTo.setFullYear(enableDateTo.getFullYear() + 1);
//Styles
var cellHoverColor = '#FFCC66';
var cellColor = '#C4D3EA';
var calendarStyle = 'position:absolute;top:10px;left:100px;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;z-index:999;display:none';
var monthStyle = "font:bold 12px Arial";
var dayStyle = "background:#ABABAB;font:12px Arial";
function f_cps(obj) {
    obj.style.background = '#C4D3EA';
    obj.style.font = '10px Arial';
    obj.style.color = '#333333';
    obj.style.textAlign = 'center';
    obj.style.textDecoration = 'none';
    obj.style.border = '1px solid #6487AE';
    obj.style.cursor = 'pointer';
}

function f_cpps(obj) {
    obj.style.background = '#C4D3EA';
    obj.style.font = '10px Arial';
    obj.style.color = '#ABABAB';
    obj.style.textAlign = 'center';
    obj.style.textDecoration = 'line-through';
    obj.style.border = '1px solid #6487AE';
    obj.style.cursor = 'default';
}

function f_hds(obj) {
    obj.style.background = '#FFF799';
    obj.style.font = 'bold 10px Arial';
    obj.style.color = '#333333';
    obj.style.textAlign = 'center';
    obj.style.border = '1px solid #6487AE';
    obj.style.cursor = 'pointer';
}
//Initializers
var updobj;
var now = new Date;
var sccm = now.getMonth();
var sccy = now.getFullYear();
var ccm = now.getMonth();
var ccy = now.getFullYear();
//Global Functions

function checkClick(e) {
	e?evt=e:evt=event;
	CSE = evt.target ? evt.target : evt.srcElement;
	if (!isChild(CSE, getObj('fc')) && CSE != updobj)
	        getObj('fc').style.display = 'none';
}


function evtTgt(e) {
    var el;
    if (e.target) el = e.target;
    else if (e.srcElement) el = e.srcElement;
    if (el.nodeType == 3) el = el.parentNode;
    return el;
}
function EvtObj(e) { if (!e) e = window.event; return e; }
function cs_over(e) { evtTgt(EvtObj(e)).style.background = cellHoverColor; }
function cs_out(e) { evtTgt(EvtObj(e)).style.background = cellColor; }
function cs_click(e) {
    updobj.value = calvalarr[evtTgt(EvtObj(e)).id.substring(1, evtTgt(EvtObj(e)).id.length)];
    updobj.setAttribute("mydate",calvalarr1[evtTgt(EvtObj(e)).id.substring(1, evtTgt(EvtObj(e)).id.length)]);
    document.getElementById(updobj.id.replace("2","")).value = calvalarr1[evtTgt(EvtObj(e)).id.substring(1, evtTgt(EvtObj(e)).id.length)];
    getObj("fc").style.display = "none";
}
//Main Functions
function lcs(ielem) {
    var calendarTable = getObj("fc");
    if (!calendarTable)
        calendarTable = createCalendar(ielem);
    updobj = ielem;
    var objectLocation = getXY(ielem);
    calendarTable.style.top = objectLocation.y + "px";
	calendarTable.style.left = objectLocation.x + "px";
	calendarTable.style.display = '';
	//curdt=ielem.value;
	curdt=ielem.getAttribute("mydate");
	curdtarr=curdt.split(seperator);
	isdt=true;
	for(var k=0;k<curdtarr.length;k++) {
		if (isNaN(curdtarr[k]))
			isdt=false;
	}
	if (isdt&(curdtarr.length==3)) {
		ccm=curdtarr[1]-1;
		ccy=curdtarr[0];
		prepcalendar(curdtarr[2],curdtarr[1]-1,curdtarr[0]);
	}
}
function prepcalendar(hd,cm,cy) {
	now=new Date();
	sd=now.getDate();
	td=new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd = td.getDay();
	getObj('mns').innerHTML=mn[cm]+ ' ' + cy;
	marr=((cy%4)==0)?mnl:mnn;
	for (var d = 1; d <= calvalarr.length; d++) {
		f_cps(getObj('v'+parseInt(d)));
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			var currentDate = new Date(cy, cm, d-cd);
			dip = !((currentDate >= enableDateFrom) && (currentDate <= enableDateTo));
			htd=((hd!='')&&(d-cd==hd));
			if (dip){
				f_cpps(getObj('v'+parseInt(d)));
			}
			else if (htd) {
				f_hds(getObj('v'+parseInt(d)));
			}
			else {    
				f_cps(getObj('v'+parseInt(d)));
            }
			getObj('v'+parseInt(d)).onmouseover=(dip)?null:cs_over;
			getObj('v'+parseInt(d)).onmouseout=(dip)?null:cs_out;
			getObj('v'+parseInt(d)).onclick=(dip)?null:cs_click;
			getObj('v' + parseInt(d)).innerHTML = d - cd;
			var currentMonth = (cm - (-1));
			var currentMonthString = "";
			if (currentMonth < 10) {
			    currentMonthString = "0" + currentMonth;
			}
			else {
			    currentMonthString = currentMonth;
			}
			calvalarr1[d]=''+cy+seperator+currentMonthString+seperator+(d-cd);
			calvalarr[d]=''+(d-cd)+seperator+currentMonthString+seperator+(""+cy);
		}
		else {
			getObj('v'+d).innerHTML='&nbsp;';
			getObj('v'+parseInt(d)).onmouseover=null;
			getObj('v'+parseInt(d)).onmouseout=null;
			getObj('v'+parseInt(d)).style.cursor='default';
			}
	}
}
function createCalendar(obj) {
    var calendarTable = document.createElement("table");
    calendarTable.setAttribute("id", "fc");
    calendarTable.setAttribute('cellpadding', '2');
    //calTableB4Tbody.setAttribute('style', calendarStyle);
    calendarTable.style.position = "absolute";
    calendarTable.style.top = "10px";
    calendarTable.style.left = "100px";
    calendarTable.style.borderCollapse = "collapse";
    calendarTable.style.background = "#FFFFFF";
    calendarTable.style.border = "1px solid #ABABAB";
    calendarTable.style.zIndex = "999";
    calendarTable.style.display = "none";

    var tBody = document.createElement("tbody");

    var calMonthRow = document.createElement("tr");

    var lastMonthCell = document.createElement("td");
    lastMonthCell.style.cursor = "pointer";
    lastMonthCell.onclick = csubm;
    lastMonthCell.appendChild(lastMonthImage);
    calMonthRow.appendChild(lastMonthCell);

    var mnsCell = document.createElement("td");
    mnsCell.setAttribute("colSpan", "5");
    mnsCell.setAttribute("id", "mns");
    mnsCell.setAttribute("align", "center");
    mnsCell.style.font = "bold 12px Arial";
    calMonthRow.appendChild(mnsCell);

    var nextMonthCell = document.createElement("td");
    nextMonthCell.setAttribute("align", "right");
    nextMonthCell.style.cursor = "pointer";
    
    nextMonthCell.onclick = caddm;
    nextMonthCell.appendChild(nextMonthImage);
    calMonthRow.appendChild(nextMonthCell);
    tBody.appendChild(calMonthRow);

    var daysRow = document.createElement("tr");
    for (var i = 0; i < dayNames.length; i++) {
        var dayCell = document.createElement("td");
        dayCell.setAttribute("align", "center");
        dayCell.style.background = "#ABABAB";
        dayCell.style.font = "12px Arial";
        dayCell.innerHTML = dayNames[i];
        daysRow.appendChild(dayCell);
    }
    tBody.appendChild(daysRow);
    
    for (var kk = 1; kk <= 6; kk++) {
        var calRow = document.createElement('tr');
        for (var tt = 1; tt <= 7; tt++) {
            num = 7 * (kk - 1) - (-tt);
            var calCell = document.createElement('td');
            calCell.setAttribute("id", "v" + num);
            calCell.style.width = "18px";
            calCell.style.height = "18px";
            calCell.innerHTML = "&nbsp;";
            calRow.appendChild(calCell);
        }
        tBody.appendChild(calRow);
    }
    calendarTable.appendChild(tBody);

    obj.ownerDocument.body.appendChild(calendarTable);
    prepcalendar('', ccm, ccy);
    obj.ownerDocument.all ? obj.ownerDocument.attachEvent('onclick', checkClick) : obj.ownerDocument.addEventListener('click', checkClick, false);
    return calendarTable;
}
//Functionality Functions
function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	prepcalendar(0,ccm,ccy);
}
function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;
	ccm-=1;
	if (ccm<0) {
		ccm=11;
		ccy--;
	}
	prepcalendar(0,ccm,ccy);
}
