/* --- Swazz Javascript Calendar ---
/* --- v 1.0 3rd November 2006
By Oliver Bryant
http://calendar.swazz.org */

function getObj(objID)
{
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function checkClick(e) {
	e?evt=e:evt=event;
	CSE=evt.target?evt.target:evt.srcElement;
	if (getObj('fc'))
	{
		if (!isChild(CSE,getObj('fc')))
		{
			getObj('fc').style.display='none';
			//getObj('fc_div').style.display='none';
		}
	}
}

function isChild(s,d) {
	while(s) {
		if (s==d) 
			return true;
		s=s.parentNode;
	}
	return false;
}

function Left(toShowInput)
{
    obj = document.getElementById(toShowInput);
	var curleft = 0;
	if(obj)
	{
	    if (obj.offsetParent)
	    {
		    while (obj.offsetParent)
		    {
			    curleft += obj.offsetLeft
			    obj = obj.offsetParent;
		    }
	    }
	    else if (obj.x)
		    curleft += obj.x;
	}
	return curleft;
}

function Top(toShowInput)
{
    obj = document.getElementById(toShowInput);
	var curtop = 0;
	if(obj)
	{
	    if (obj.offsetParent)
	    {
		    while (obj.offsetParent)
		    {
			    curtop += obj.offsetTop
			    obj = obj.offsetParent;
		    }
	    }
	    else if (obj.y)
		    curtop += obj.y;
	}
	return curtop;
}
	
document.write('<div id="fc" style="position:absolute;background:#FFFFFF;border:1px solid #ABABAB;display:none;Z-INDEX: 10">');
document.write('<table cellpadding=2 style="border-collapse:collapse;"><tr><td style="cursor:pointer" onclick="csubm()"><img src="http://www.roomrace.com/images/global_images/calendar/arrowleftmonth.gif"></td><td colspan=5 id="mns" align="center" style="font:bold 13px Arial"></td><td align="right" style="cursor:pointer" onclick="caddm()"><img src="http://www.roomrace.com/images/global_images/calendar/arrowrightmonth.gif"></td></tr>');
document.write('<tr><td align=center style="background:#ABABAB;font:12px Arial">S</td><td align=center style="background:#ABABAB;font:12px Arial">M</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">W</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">F</td><td align=center style="background:#ABABAB;font:12px Arial">S</td></tr>');
for(var kk=1;kk<=6;kk++) {
	document.write('<tr>');
	for(var tt=1;tt<=7;tt++) {
		num=7 * (kk-1) - (-tt);
		document.write('<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table></div>');

document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);


// Calendar script
var myDate=new Date();
var now = new Date;
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();

var nowMonth = now.getMonth();
var nowYear = now.getFullYear();
var endMonth = 0;
var endYear = 0;

if ((nowMonth+11)>=12)
{
	endMonth = nowMonth-1;
	endYear = nowYear+1;
}
else
{
	endMonth = nowMonth+11
	endYear = nowYear
}

var updobj;


function lcs(ielemInput,toShowInput) {
	ielem = document.getElementById(ielemInput);
	toShow = document.getElementById(toShowInput);
		
	if(ielem==null || toShow==null)
	    return;
    
	var toCheck = ielem.value.split('/');
	var myTmpDate=new Date();
	if (toCheck.length!=3)
		ielem.value = String(myTmpDate.getDate()) + String(myTmpDate.getMonth()+1) + toCorrentYear(myTmpDate.getYear());
	else
	{
		if (!isValidDate(toCheck[1] + "/" + toCheck[0] + "/" + toCheck[2]))
			ielem.value = String(myTmpDate.getDate()) + String(myTmpDate.getMonth()+1) + toCorrentYear(myTmpDate.getYear());
	}
	
	updobj=ielem;
	getObj('fc').style.left=Left(toShowInput) + 'px';
	getObj('fc').style.top=Top(toShowInput)+toShow.offsetHeight + 'px';
	getObj('fc').style.display='';
	
	// First check date is valid
	curdt=ielem.value;
	//curdt=ielem;
	curdtarr=curdt.split('/');
	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[2];
		prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
	}
	
}

function isValidDate(dateStr) {

	// dateStr: mm/dd/yyyy

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
	//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

if (month < 1 || month > 12) { // check month range
	//alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	//alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	//alert("Month "+month+" doesn`t have 31 days!")
	return false;
	}

	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day > 29 || (day==29 && !isleap)) {
	//alert("February " + year + " doesn`t have " + day + " days!");
	return false;
	}
	}
	return true; // date is valid
}

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; // defeat Safari bug
	return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}
function cs_over(e) {
	evtTgt(EvtObj(e)).style.background='#FFCC66';
}
function cs_out(e) {
	evtTgt(EvtObj(e)).style.background='#C4D3EA';
}
function cs_click(e) {    
	updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	if (updobj.id=="txt_CheckIn" || updobj.id=="AmazeSearchUserControl_txt_CheckIn")
	{   
	    if(document.getElementById('txt_CheckIn')!=null)
	        onChangeHText('txt_CheckIn','lst_Day_CheckIn','lst_MonthsYear_CheckIn',0);
		else
		    onChangeHText('AmazeSearchUserControl_txt_CheckIn','lst_Day_CheckIn','lst_MonthsYear_CheckIn',0);
		   			
		myOnChangeDay('lst_Day_CheckIn');
	}
	else if (updobj.id=="txt_CheckOut" || updobj.id=="AmazeSearchUserControl_txt_CheckOut")
	{
	    if(document.getElementById('txt_CheckOut')!=null)
		    onChangeHText('txt_CheckOut','lst_Day_CheckOut','lst_MonthsYear_CheckOut',1);
		else
		    onChangeHText('AmazeSearchUserControl_txt_CheckOut','lst_Day_CheckOut','lst_MonthsYear_CheckOut',1);
	}
		
	getObj('fc').style.display='none';
	//getObj('fc_div').style.display='none';
}

function myLoaaaad(calId)
{
    
}




//var mn=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var 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);

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';
}

// day selected
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<=42;d++) {
		f_cps(getObj('v'+parseInt(d)));
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			dip=((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
			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;			
			calvalarr[d]=''+(d-cd)+'/'+(cm-(-1))+'/'+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';
			}
	}
}

prepcalendar('',ccm,ccy);
//getObj('fc'+cc).style.visibility='hidden';

function caddm() {
		
	//Na emfanizei mexri 12 mines meta ton arxiko!
	if (ccy==endYear && (ccm+1)>endMonth)
		return;
	
	marr=((ccy%4)==0)?mnl:mnn;	
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	cdayf();
	prepcalendar('',ccm,ccy);
}

function csubm() {
		
	//Na min emfanizei tous proigoumenous mines
	if (ccy==nowYear && (ccm-1)<nowMonth)
		return;
		
	marr=((ccy%4)==0)?mnl:mnn;
	ccm-=1;		
	if (ccm<0) {
		ccm=11;
		ccy--;
	}
	cdayf();
	prepcalendar('',ccm,ccy);
}

function cdayf() {
if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
	return;
else {
	ccy=sccy;
	ccm=sccm;
	cfd=scfd;
	}
}

//myMonths: January=0 --- Deceber=11
function maxDayOfMonth(myYear,myMonth)
{	
	marr=((myYear%4)==0)?mnl:mnn;	
	return marr[myMonth];
}

function dayOfWeek(myDate)
{
	//var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var dayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	return dayNames[myDate.getDay()];
}

function monthOfYear(myDate)
{
	return mn[myDate.getMonth()]
}

function myOnChangeDay(myDaysList)
{
    myOnChangeDay(myDaysList, null);
}

function myOnChangeDay(myDaysList, savedCheckOut)
{
	var mySelectedDay = document.getElementById(myDaysList).options[document.getElementById(myDaysList).selectedIndex].value;			
    	
	txt_CheckIn = document.getElementById('txt_CheckIn');
	if(txt_CheckIn==null)
	    txt_CheckIn = document.getElementById('AmazeSearchUserControl_txt_CheckIn');
	    
    txt_CheckOut = document.getElementById('txt_CheckOut');
    if(txt_CheckOut==null)
        txt_CheckOut = document.getElementById('AmazeSearchUserControl_txt_CheckOut');
	
	
	if (myDaysList=="lst_Day_CheckIn")
	{
		if (document.getElementById('lst_MonthsYear_CheckOut').selectedIndex==document.getElementById('lst_MonthsYear_CheckIn').selectedIndex)
		{
			var myIndex = document.getElementById(myDaysList).selectedIndex;
			if ((myIndex+1)>=document.getElementById(myDaysList).options.length)
			{
				document.getElementById('lst_MonthsYear_CheckOut').selectedIndex++;
				myOnChangeMonth('lst_MonthsYear_CheckOut','lst_Day_CheckOut',0);
			}
			else
			{
			    if(!savedCheckOut)
			    {   
				    document.getElementById('lst_Day_CheckOut').selectedIndex = myIndex+1;
				}
				else
				{   
				    document.getElementById('lst_Day_CheckOut').selectedIndex = getLstIndexByValue('lst_Day_CheckOut',savedCheckOut);
				}
			}
			mySelectedDay = document.getElementById(myDaysList).options[document.getElementById(myDaysList).selectedIndex].value;
			txt_CheckIn.value = mySelectedDay;
			txt_CheckOut.value = document.getElementById('lst_Day_CheckOut').options[document.getElementById('lst_Day_CheckOut').selectedIndex].value;
		}
	}
	else
		txt_CheckOut.value = mySelectedDay;
}

function lstSelectByValue(listName,myValue)
{
    listObj = document.getElementById(listName);
    if(listObj)
    {
        for(i=0;i<listObj.length;i++)
        {
            if(listObj.options[i].value==myValue)
            {
                listObj.selectedIndex=i;
                break;
            }
        }
    }
}

function getLstIndexByValue(listName,myValue)
{
    listObj = document.getElementById(listName);
    if(listObj)
    {
        for(i=0;i<listObj.length;i++)
        {
            if(listObj.options[i].value==myValue)
            {
                //listObj.selectedIndex=i;
                //break;
                return i;
            }
        }
    }
}

function myOnChangeMonth(myMonthList,myDaysList, daysShift, onLoad)
{   
	var now = new Date;
	var iStart=1;
		
	txt_CheckIn = document.getElementById('txt_CheckIn');
	if(txt_CheckIn==null)
	    txt_CheckIn = document.getElementById('AmazeSearchUserControl_txt_CheckIn');
	    
    txt_CheckOut = document.getElementById('txt_CheckOut');
    if(txt_CheckOut==null)
        txt_CheckOut = document.getElementById('AmazeSearchUserControl_txt_CheckOut');
	
	if (document.getElementById(myMonthList)==null)
	    return;
	
	//Elenxos kai keno dayShift	
	if (daysShift==null)
	{
		if (myMonthList=="lst_MonthsYear_CheckOut")
			daysShift=1;
		else
			daysShift=0;
	}	
	now.setDate(now.getDate()+daysShift);
		
	//Metaferei ton mina kai sto check out!
	if (myMonthList=="lst_MonthsYear_CheckIn")
	{	
		if (onLoad==0 || onLoad==null)
		{
			document.getElementById('lst_MonthsYear_CheckOut').selectedIndex = document.getElementById(myMonthList).selectedIndex;
		}		
	}
	
	//Gia na min emfanizei tis hmeres xwris na exei epilegei prwta enas minas:
	if (document.getElementById(myMonthList)!=null)
	{
	    if (document.getElementById(myMonthList).selectedIndex==0 && onLoad==null)
	        onLoad=1;
	}

	var myNowMonth=now.getMonth();
	var myNowYear=toCorrentYear(now.getYear());
	var myNowDay=now.getDate();
			
	clearAllOptions(myDaysList);
	
	
	
	//alert(myMonthList +":" + );
	
	var mySelectedValue = document.getElementById(myMonthList).options[document.getElementById(myMonthList).selectedIndex].value;
	var splited = mySelectedValue.split("-")			
	if (splited[0]==myNowYear && splited[1]==myNowMonth)
		iStart=myNowDay;
	
	myDate.setFullYear(splited[0],splited[1],iStart);
	AddItem(myDaysList,'Day','-1');	
	if (!onLoad)
	{
	    for(var i=iStart;i<=maxDayOfMonth(splited[0],splited[1]);i++)
	    {
		    AddItem(myDaysList,dayOfWeek(myDate) + " " + i,String(i)+"/"+String(parseInt(splited[1])+1)+"/"+splited[0]);
		    myDate.setFullYear(splited[0],splited[1],i+1);
	    }
	}	
	
	if (myMonthList=="lst_MonthsYear_CheckIn")
	{
		if (onLoad==1)		
			txt_CheckIn.value = "dd/mm/yy";
		else
		{
			txt_CheckIn.value = iStart + "/" + String(parseInt(splited[1])+1) + "/" + splited[0];			
			if (document.getElementById(myMonthList).selectedIndex!=0)
				document.getElementById(myDaysList).selectedIndex = 1;
		}
		
		if (onLoad==null)
			myOnChangeMonth('lst_MonthsYear_CheckOut','lst_Day_CheckOut',0,0);
		else
		    myOnChangeMonth('lst_MonthsYear_CheckOut','lst_Day_CheckOut',0,onLoad);
	}
	else
	{
		if (onLoad==1)
			txt_CheckOut.value = "dd/mm/yy";
		else
		{
			txt_CheckOut.value = iStart + "/" + String(parseInt(splited[1])+1) + "/" + splited[0];
			//alert(txt_CheckOut.value );
			if (document.getElementById(myMonthList).selectedIndex!=0)
				document.getElementById(myDaysList).selectedIndex = 1;
		}
	}	
	
	if (myMonthList=="lst_MonthsYear_CheckOut")
	{	
		//Bazoume 2, epeidi 1 8a einai h hmerominia kai 1 to "-- Day --"
		//Se periptwsi pou afere8ei to 8a prepei na ginei == 1!
		if (document.getElementById('lst_Day_CheckIn').options.length==2)
		{
			document.getElementById(myMonthList).selectedIndex++;
		}
		else
		{
			var myTmpSelectedIndex = document.getElementById('lst_Day_CheckIn').selectedIndex;
			var mySplited = document.getElementById('lst_Day_CheckIn').value.split('/');
			
			//Ean einai i telaiutaia mera tou mina tote
			//paei stin arxi tou epomenou.
			//alert(myTmpSelectedIndex + " - " + document.getElementById('lst_Day_CheckIn').length);
			var isLast=false;
			if ((myTmpSelectedIndex+1)==document.getElementById('lst_Day_CheckIn').length)
			{   
				iStart = 1;
				isLast=true;
			}
			else
			{   
				//pairnoume tin hmera tou check-in kai pros8etoume mia mera!
				//alert(mySplited.length);
				if (mySplited.length==3)
				{				    
					iStart = parseInt(mySplited[0])+1;
				}
				else
					iStart = 2;
			}
	
			var myTmpDate = txt_CheckIn.value;
			var splitedDate = myTmpDate.split('/');
						
			now.setFullYear(splitedDate[2],splitedDate[1],iStart);
			if (!isLast && document.getElementById('lst_Day_CheckIn').selectedIndex!=0)
			{
			    document.getElementById(myDaysList).selectedIndex++;
			    //txt_CheckOut.value = iStart + "/" + splitedDate[1] + "/" + "splitedDate[2]";
			    //alert(iStart + "/" + splitedDate[1] + "/" + splitedDate[2]);
			    txt_CheckOut.value=iStart + "/" + splitedDate[1] + "/" + splitedDate[2];
			}
		}
	}	
}

function onChangeHText(myField,myDaysList,myMonthList,daysShift)
{
	myDList = document.getElementById(myDaysList);
	myMList = document.getElementById(myMonthList);
				
	var myDValue = document.getElementById(myField).value;
	var splited = myDValue.split('/');
	var myMValue = splited[2] + "-" + String(parseInt(splited[1])-1);
			
	for (var j=0;j<myMList.options.length;j++)
	{
		var myTmpValue = myMList.options[j].value;
		//alert(myMValue + " " + myTmpValue);
		
		if (myTmpValue==myMValue)
		{					
			myMList.options[j].selected = true;
			myOnChangeMonth(myMonthList,myDaysList,daysShift);
			break;
		}
	}		
	for(var i=0;i<myDList.options.length;i++)
	{
		var myTmpValue = document.getElementById(myDaysList).options[i].value;				
		if (myTmpValue == myDValue)
		{					
			document.getElementById(myDaysList).options[i].selected = true;
			document.getElementById(myField).value = myDValue;
			break;
		}
	}
}

function toCorrentYear(myYear)
{
	if (document.all)
	{
		//IE4+ code 
		return myYear
	}
	//else if (document.layers)
	//{
		//NS4+ code
		
	//}
	else if (document.getElementById)
	{
		//NS6+ code
		return myYear+1900;
	}
}		
function clearAllOptions(MyList)
{
	if (document.getElementById(MyList)==null)
		return;
	
	for (var i = (document.getElementById(MyList).options.length-1); i >= 0; i--)
	{
		    document.getElementById(MyList).options[i]=null;
	}
}

function AddItem(listName,Text,Value)
{
	// Create an Option object
	var opt = document.createElement("option");

	// Add an Option object to Drop Down/List Box
	document.getElementById(listName).options.add(opt);

	// Assign text and value to Option object
	opt.text = Text;
	opt.value = Value;
}


function dateToDetails(inputDate)
{
    //inputDate Format: dd/mm/yy
    if(inputDate)
    {
        var args=inputDate.split('/');
        if (args.length==3)
        {
            var day = args[0];
            var month = String(parseInt(args[1])-1);
            var year = args[2];
            
            return {day : day, month : month, year : year};
        }
        else
            return null;
    }
    else
        return null;
}

function myOnLoadCalendar(myMonthList,myDaysList, daysShift)
{
    myOnLoadCalendar(myMonthList,myDaysList, daysShift, 1)
}
		
function myOnLoadCalendar(myMonthList,myDaysList, daysShift, createDays)
{
    
    var myDate;    
//    if(checkInputDate(inputDate))
//    {   
//        var dateDetails = dateToDetails(inputDate)
//        if(dateDetails)
//            myDate=new Date(dateDetails.year, dateDetails.month, dateDetails.day);
//        else
//            myDate=new Date();
//    }
//    else
//    {
        myDate=new Date();
//    }
    
    if (createDays==null)
        createDays=1;
	
	var myMonth=myDate.getMonth();
	
	AddItem(myMonthList,'Month','-1');
	var i=0;
	for(i=0;i<12;i++)
	{
	
		var myYear
		AddItem(myMonthList,monthOfYear(myDate) + " '" + String(toCorrentYear(myDate.getYear())).substring(2),String(toCorrentYear(myDate.getYear())) + "-" + String(myMonth));
		myMonth++;
		if (myMonth>=12)
		{
			myMonth=0;
			myDate.setFullYear(toCorrentYear(myDate.getYear()+1),myMonth,1)
		}
		else
			myDate.setFullYear(toCorrentYear(myDate.getYear()),myMonth,1)
	}		
	    
	myOnChangeMonth(myMonthList,myDaysList, daysShift,1,createDays);
}

function checkInputDate(myValue)
{
	var toCheck = myValue.split('/');
	if (toCheck.length!=3)
		return false;
	else
	{
		if (!isValidDate(toCheck[1] + "/" + toCheck[0] + "/" + toCheck[2]))
			return false;
		else
			return true;
	}
}

function selectElem(elem)
{		    
    if(elem.text!=null)
    {
        elem.select();
    }
    else
    {
        myElem = document.getElementById(elem);
        if(myElem)
        {
            myElem.select();
        }
    }
}

