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';}}
}


function isChild(s,d) {
						while(s) {
							if (s==d){ return true;}
							s=s.parentNode;
								}
						return false;
					  }

function Left(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;
}
	
function Top(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{	
			curtop += obj.offsetTop ;
			obj = obj.offsetParent;
		}
		
	}
	else if (obj.y){ curtop += obj.y;}

	if ((updobj.name=="SecondCheckin") ||(updobj.name=="SecondCheckOut"))
		{curtop=504;}
	if ((updobj.name=="ThirdCheckin") ||(updobj.name=="ThirdCheckOut"))
		{curtop=594;}
		if ((updobj.name=="FourthCheckin") ||(updobj.name=="FourthCheckOut"))
		{curtop=684;}

	return curtop;
}

document.write('<div id="javalayer" style="left:0; top:0; position:absolute; z-index:30; width:110px;">');
document.write('<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;display:none" cellpadding=2>');
document.write('<tr><td style="cursor:pointer" onclick="csubm()"><img src="http://www.keytours.com/search/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.keytours.com/search/arrowrightmonth.gif"></td></tr>');document.write('<tr><td align=center style="background:#eceded;font:12px Arial; textDecoration:line-through"><b>S</b></strike></td><td align=center style="background:#E9E3CD;font:12px Arial">M</td><td align=center style="background:#E9E3CD;font:12px Arial">T</td><td align=center style="background:#E9E3CD;font:12px Arial">W</td><td align=center style="background:#E9E3CD;font:12px Arial">T</td><td align=center style="background:#E9E3CD;font:12px Arial">F</td><td align=center style="background:#eceded;font:12px Arial"><b>S</b></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>');
}
//added by huseyin yilmaz for selecting month
document.write('<tr><td align="center" style="cursor:pointer;background:#FFFFFF" colspan="7" > <select name="selectdate1" id="selectdate1" size="1" onchange="cmonth()" >');
document.write('<option value="0">Month</option><option value="1">January</option><option value="2">February</option>');
document.write('<option value="3">March</option><option value="4">April</option><option value="5">May</option>');
document.write('<option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option>');
document.write('<option value="10">October</option><option value="11">November</option><option value="12">December</option></select>');
//added by huseyin yilmaz for selecting year
document.write('&nbsp;<select name="selectyear" id="selectyear"  size="1"  onchange="cyear()" >');
document.write('<option value="0">Year</option>');
document.write('<option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option>');
document.write('<option value="2011">2011</option><option value="2012">2012</option><option value="2013">2013</option><option value="2014">2014</option>');
document.write('<option value="2015">2015</option><option value="2016">2016</option></select>');
document.write('</td></tr>');
document.write('</table>');	
document.write('</div>');

document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);


// Calendar script
var now = new Date;
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();

var updobj;


function lcs(ielem) {
	
	updobj=ielem;
	getObj('fc').style.left=Left(ielem);
	getObj('fc').style.top=Top(ielem)+ielem.offsetHeight;
	getObj('fc').style.display='';

	// First check date is valid
	curdt=ielem.value;
	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[0]-1;
		ccy = curdtarr[2];
		prepcalendar(curdtarr[1],curdtarr[0]-1,curdtarr[2]);
	}
	
}

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='#FFFFFF';
}

function AddDatetoCity(GetDateText,AddDateT)
{

	var myDate = new Date(GetDateText);
	myDate.setDate(myDate.getDate()+parseInt(AddDateT));
	return myDate.getMonth()+1+"/"+myDate.getDate()+"/"+myDate.getFullYear() ;
}

function cs_click(e) 
{	updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
		var objectname=updobj.name;

		if (Checkbutton.SelectionType[0].checked)
		{
				switch(objectname)
				{
					case "FirstCheckin":
									SearchForm.FirstCheckOut.value = AddDatetoCity(SearchForm.FirstCheckin.value,1);
									SearchForm.SecondCheckin.value = AddDatetoCity(SearchForm.FirstCheckin.value,1);
									SearchForm.SecondCheckOut.value = AddDatetoCity(SearchForm.SecondCheckin.value,1);
									SearchForm.ThirdCheckin.value = AddDatetoCity(SearchForm.SecondCheckin.value,1);
									SearchForm.ThirdCheckOut.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckin.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break;
					case "FirstCheckOut":
									SearchForm.SecondCheckin.value = updobj.value;
									SearchForm.SecondCheckOut.value = AddDatetoCity(updobj.value,1);
									SearchForm.ThirdCheckin.value = AddDatetoCity(SearchForm.SecondCheckin.value,1);
									SearchForm.ThirdCheckOut.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckin.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break;
					 case "SecondCheckin":
									SearchForm.SecondCheckOut.value = AddDatetoCity(SearchForm.SecondCheckin.value,1);
									SearchForm.ThirdCheckin.value = AddDatetoCity(SearchForm.SecondCheckin.value,1);
									SearchForm.ThirdCheckOut.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckin.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break;
					 case "SecondCheckOut":
									SearchForm.ThirdCheckin.value = updobj.value;
									SearchForm.ThirdCheckOut.value = AddDatetoCity(updobj.value,1);
									SearchForm.FourthCheckin.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break;
					   case "ThirdCheckin":
									SearchForm.ThirdCheckOut.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckin.value = AddDatetoCity(SearchForm.ThirdCheckin.value,1);
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break;
					   case "ThirdCheckOut":
									SearchForm.FourthCheckin.value = updobj.value;
									SearchForm.FourthCheckOut.value = AddDatetoCity(updobj.value,1);
					  break;
						case "FourthCheckin":
									SearchForm.FourthCheckOut.value = AddDatetoCity(SearchForm.FourthCheckin.value,1);
					  break; 
					  
					  
					default:
  
				}
		}
		if (Checkbutton.SelectionType[2].checked)
		{
			if (objectname=="FirstCheckin") OnlineHotelForm.FirstCheckOut.value = AddDatetoCity(updobj.value,1);
		}
		//SearchForm.FirstCheckOut.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	getObj('fc').style.display='none';
}

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='#FFFFFF';
	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='#eceded';
	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]=''+(cm-(-1))+'/'+(d-cd)+'/'+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';
			}
	}

	var select_year;
	//alert(cm+" "+cy);

		
		document.getElementById("selectdate1").options[cm+1].selected=true;
		 switch(cy) {
		case 2007: {select_year='1'; break;}
		case "2007": {select_year='1'; break;}
		case 2008: {select_year='2'; break;}
		case "2008": {select_year='2'; break;}
		case 2009: {select_year='3'; break;}
		case "2009": {select_year='3'; break;}
		case 2010: {select_year='4'; break;}
		case "2010": {select_year='4'; break;}
		case 2011: {select_year='5'; break;}
		case "2011": {select_year='5'; break;}
		case 2012: {select_year='6'; break;}
		case "2012": {select_year='6'; break;}
		case 2013: {select_year='7'; break;}
		case "2013": {select_year='7'; break;}
		case 2014: {select_year='8'; break;}
		case "2014": {select_year='8'; break;}
		case 2015: {select_year='9'; break;}
		case "2015": {select_year='9'; break;}
		case 2016: {select_year='10'; break;}
		case "2016": {select_year='10'; break;}
		default: result='00'
		}
		document.getElementById("selectyear").options[select_year].selected=true; 
}

prepcalendar('',ccm,ccy);
//getObj('fc'+cc).style.visibility='hidden';


function cyear() // select month
{

	marr=((ccy%4)==0)?mnl:mnn;
	
		ccm = (document.getElementById("selectyear").selectedIndex-1);
		
	ccy =document.getElementById("selectyear").options[document.getElementById("selectyear").selectedIndex].value
	//ccy = selectyear.options[selectyear.selectedIndex].value ;
	
	//if (ccm>=12) {ccm=0;ccy++;}
	cdayf();
	prepcalendar('',ccm,ccy);
}


function cmonth() // select month
{
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm = (document.getElementById("selectdate1").selectedIndex-1);
	
	//if (ccm>=12) 
	//{ccm=0;	ccy++;}
	cdayf();
	prepcalendar('',ccm,ccy);
}


function caddm() 
{
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	
	cdayf();
	
	prepcalendar('',ccm,ccy);
}

function csubm() {
	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 
{
	//if older date then new function 
	
	ccy=sccy;
	ccm=sccm;
	document.getElementById("selectdate1").options[ccm+1].selected=true;
	 switch(ccy) {
				case 2007: {select_year='1'; break;}
				case 2008: {select_year='2'; break;}
				case 2009: {select_year='3'; break;}
				case 2010: {select_year='4'; break;}
				case 2011: {select_year='5'; break;}
				case 2012: {select_year='6'; break;}
				case 2013: {select_year='7'; break;}
				case 2014: {select_year='8'; break;}
				case 2015: {select_year='9'; break;}
				case 2016: {select_year='10'; break;}
				default: result='00'
		}
		document.getElementById("selectyear").options[select_year].selected=true; 
	
	//cfd=scfd;
	}
}
