//CALENDAR
var n=navigator,B=(n.userAgent+n.appName).toLowerCase(),V=parseInt(n.appVersion);
var N4=(B.indexOf("netscape")!=-1&&V==4),IE=(B.indexOf("microsoft")!=-1);

var calForm,dropDay1,dropMonth1,dropDay2,dropMonth2,currButtonNo,initDay,initMonth,initYear,endDay,endMonth,numMonths,bPressed,calendar;

var calWidth=142;
var calHeight=182;
var calendarHTML="";
var monthNames=new Array("x","x","x","x","x","x","x","x","x","x","x","x");//placeholders

if(!N4)document.onmouseup=function(e){
	//alert('DEBUG:TRYING TO HIDE THE CALENDAR ' +e);
	calendar.hide();
	if(bPressed){
		bPressed=false;
		//alert('DEBUG:CALENDAR REMOVED');
		showCalendar(e);}
		//alert('DEBUG:CALENDAR APPEAR');
}

function startUpCalendar(stDate,endDate,formName,dd1,dm1,dd2,dm2,sync,syncCal){
	
	if(N4)return;
//Following line commented out due to clash with touch clarity logging scripts - Shiraz	
//if(!document.all)document.all=document.getElementsByTagName("*");
	calForm=document.forms[formName];
	
	dropDay1=calForm[dd1];
	dropMonth1=calForm[dm1];
	
	dropDay2=calForm[dd2];
	dropMonth2=calForm[dm2];

	calendar=new LayerFrame('cIframe');
	stPos=currpos=0;

	initDate=stDate.split('/');
	initDay=initDate[0]*1,initMonth=initDate[1]*1,initYear=initDate[2];

  // Setup popup calendar date params
  if (syncCal!=false){
    // If calendar is to sync with dropdowns
    calDay=initDay=initDate[0]*1;
    calMonth=initMonth=initDate[1]*1;
    calYear=initYear=initDate[2];
  } else {
    // If calendar is not to sync with dropdowns
    //(e.g. if pre-pop date is in the future but we still want current dates to be available.)
    d=new Date()
    calDay=d.getDate();
    calMonth=d.getMonth()+1;
    calYear=d.getFullYear();    
  }
  
	endDate=endDate.split('/');
	endDay=endDate[0]*1,endMonth=endDate[1]*1;
	if(endMonth>initMonth)numMonths=endMonth-initMonth+1;
	else numMonths=(12-initMonth)+endMonth+1;
	if(endMonth==initMonth)numMonths=1;
  if (sync!=false){
  	if(dropMonth1) if(dropDay1.options[0].value!="0")setDropdowns(initDay,initMonth,1);
	  if(dropMonth2){setDropdowns(initDay,initMonth,2);currButtonNo=1;syncDropdowns();}
  }
}

function setCalendar(buttonNo){
	bPressed=true;
	currButtonNo=buttonNo;
	var ops=eval('dropMonth'+buttonNo);
	//alert(ops.selectedIndex);
	for(i=0;i<ops.length;i++){
		var opVal=ops.options[i].value;
		monthNames[opVal]=ops.options[i].text;
	}
	var currMonth=getChronMonth(eval('dropMonth'+buttonNo).value*1);
	currpos=calHeight*currMonth;
  if (calendarHREF.indexOf('?') > -1)  queryStart = '&';
  else queryStart = '?';
//	cIframe.document.write(calendarHTML);
  if (buttonNo=='2') selectedDates = '&selDay=' + dropDay1.options[dropDay1.selectedIndex].value + '&selMonth=' + dropMonth1.options[dropMonth1.selectedIndex].value;
  else selectedDates = '';
  cIframe=parent.frames['cIframe'];
  // This is needed to get around weirdness with end of month dates and calendar start dates which are in the future.
  tempDate = new Date()
  if (initMonth-1>tempDate.getMonth() && ops.selectedIndex!=0){
    addMonth = ops.selectedIndex-1
  } else {
    addMonth = ops.selectedIndex
  }
  cIframe.document.location.href = calendarHREF + queryStart + 'MONTH=' + addMonth + '&cal_period=' + numMonths + '&cal_day=' + calDay + '&cal_month=' + calMonth + '&cal_year=' + calYear + selectedDates;
  //alert('DEBUG:PROCESS');
}

function showCalendar(e){
	var button=new posElement((IE)?event.srcElement:e.target);
	posX=button.x;posY=button.y;
	doc_height=document.body.clientHeight+(IE?document.body.scrollTop:0);
	//alert("DEBUG:"+ doc_height + button);
	if(posY>doc_height-calHeight){
		//alert("DEBUG:Case 1");
		calendar.show(posX+36,posY-calHeight+20);
	}
	else{ 
		//alert("DEBUG:Case 2");
		calendar.show(posX+36,posY);
	}
}


function posElement(element)
{
	// get position relative to... 
	this.x=element.offsetLeft;
	this.y=element.offsetTop;
  var pElement=element.offsetParent;
  while(pElement && pElement.id!='master_center')
		{
    this.x+=pElement.offsetLeft;
    this.y+=pElement.offsetTop;
    pElement=pElement.offsetParent;
		}
	if (navigator.appName == 'Netscape' && !N4) this.y += calHeight - 20;
}

function LayerFrame(id){
	this.style=eval(document.getElementById(id)).style;
	this.reset=cReset;
	
	this.show=function(x,y){cReset();this.style.left=x+"px";this.style.top=y+"px";}
			
	this.hide=function(){this.style.top="-2000px";}
	this.next=scrollDown;
	this.previous=scrollUp;
}

function cReset(){
	//cIframe.scroll(0,stPos);currpos=stPos;
	//Modification: Joseph Wilk 29/10/2004@15:50
	//DEPRECIATED: cIframe.scroll(0,0);//keep state
	//This scroll trys to move within the iframe
	//this caused the iframe to scroll to the white space
	//lossing the focus of the calendar
}

function scrollDown(){
	currpos+=calHeight;
	cIframe.scroll(0,currpos);
}

function scrollUp(){
	currpos-=calHeight;
	if(currpos>0)cIframe.scroll(0,currpos);
	else{currpos=stPos;calendar.reset();}
}

function displayDate(dt){
	calendar.hide();

	var DMY=dt.split("/");
	var day=DMY[0];
	var month=DMY[1];

	setDropdowns(day,month,currButtonNo);
	if(dropDay2)syncDropdowns();
}

function syncDropdowns(){
	var d1=dropDay1.value*1;
	var d2=dropDay2.value*1;
	var m1=dropMonth1.value*1;
	var m2=dropMonth2.value*1;
	var cm1=getChronMonth(m1),cm2=getChronMonth(m2);
	var todayDate = new Date();
	var todayMonth = todayDate.getMonth()+1;
	var todayDay = todayDate.getDate();
	//console.log(todayDay + '/' + todayMonth)

	if( (cm1>cm2 && m1!=todayMonth)||(cm1==cm2&&d1>=d2)||(m2==todayMonth&&m1!=todayMonth)){
		//console.log(d1+'/'+cm1 + '||' + d2+'/'+cm2)	
		var tg,setDay,setMonth;
	
		if(currButtonNo==1){
			setDay=d1+1;
			setMonth=m1;
			if(!hasOptionVal(dropDay1,setDay)){
				setMonth+=1;if(setMonth>12)setMonth=1;setDay=1;
			}
			
			if(m1==endMonth&&d1==endDay){
				setMonth=m1;setDay=d1;
			}
			//console.log(setMonth)
			setDropdowns(setDay,setMonth,2);
		}
		
		if(currButtonNo==2){
			setDay=d2;setMonth=m2;
			setDropdowns(setDay,setMonth,1);
		}
	}
}

function setDropdowns(dv,mv,bn){
	var drDay=eval("dropDay"+bn);
	var drMonth=eval("dropMonth"+bn);
		selOptionVal(drMonth,mv);
		setDays(bn);
		selOptionVal(drDay,dv);
}

function selOptionVal(ob,val){
	var ops=new Object();
	for(var i=0;i<ob.length;i++)ops[ob[i].value]=ob[i];
	if(ops[val])ops[val].selected=true;
}

function hasOptionVal(ob,val){
	var ops=new Object();
	for(var i=0;i<ob.length;i++)ops[ob[i].value]=ob[i];
	if(ops[val])return true;
	else return false;
}

function getChronMonth(month){
	var cM=month-initMonth;
	return (cM<0)?cM+12:cM;
}

function getCalendarHTML(){
	calendarHTML="";
	var currDate=new Date(Date.UTC(initYear,initMonth-1,initDay));//yyyy/mm from 0/d from 1

	calendarHTML+='<html><head><title>Calendar</title>';
  calendarHTML+='<style type="text/css">\
		body{font-family:Verdana,sans-serif;font-size:13px;}\
		a{text-decoration:none;}\
		a:hover{text-decoration:underline;}\
		td{font-family:Verdana,Helvetica,sans-serif;font-size:10px;text-align:center;color:#cccccc}\
		td a{color:#000000;}\
		b{color:#000000;}\
		.white{color:#FFFFFF;font-weight:bold;}\
		</style></head><body bgcolor="#000000" leftmargin="1" topmargin="1">'

	for(i=1;i<=numMonths;i++){//For each month

		var month=currDate.getMonth()+1;//mm is from 0
		var numDays=getNumDays(month);
		var year=currDate.getYear();if(year<2000)year+=1900;

		currDate.setDate(1);//set to first of the month and get day of the week
		var firstDay=currDate.getDay()-1;//Sun is 0 from getDay() but we run from Mon
		if(firstDay<0)firstDay=6;

	//Compile HTML
		var monthHTML='\
			<table border="0" cellspacing="0" cellpadding="0" height="180" width="140" bgcolor="#FFFFFF">\
			<tr><td height="20" width="15" bgcolor="#E43784">';
		if(i>1)
			monthHTML+='<a href="javascript:top.calendar.previous()"><img src="http://www.lastminute.com/lmn/calendar/images/arrowl.gif" hspace="2" width="15" height="13" border="0"></a>';
		else
			monthHTML+='<img src="http://www.lastminute.com/lmn/calendar/images/trans.gif" width="20" height="13" border="0">';

			monthHTML+='</td><td colspan="5" width="100" bgcolor="#E43784" class="white">'+ monthNames[currDate.getMonth()+1] +
			' ' + year + '</td><td bgcolor="#E43784" width="20">';
			
		if(i<numMonths)
			monthHTML+='<a href="javascript:top.calendar.next()"><img src="http://www.lastminute.com/lmn/calendar/images/arrowr.gif" hspace="2" width="15" height="13" border="0"></a>';
		else
			monthHTML+='<img src="http://www.lastminute.com/lmn/calendar/images/trans.gif" width="15" height="13" border="0">';

			monthHTML+='</td></tr><tr><td height="20" width="20"><b>M</b></td><td width="20"><b>T</b></td><td width="20"><b>W</b></td>\
			<td width="20"><b>T</b></td><td width="20"><b>F</b></td><td width="20"><b>S</b></td><td width="20"><b>S</b></td></tr>';

		for(j=0;j<42;j+=7){//for 6 week slots in month

			monthHTML+='<tr>';

			for(k=1;k<8;k++){//for each day of week

				var slotNo=j+k;
				var countFromFirstDay=slotNo-firstDay;
				var isLink=(slotNo>firstDay)&&(countFromFirstDay<=numDays);
				var preDays=(month==initMonth&&countFromFirstDay<initDay);
				if(currButtonNo==2){
					var M=dropMonth1.value*1,D=dropDay1.value*1;
					var M1=getChronMonth(M),cM=getChronMonth(month);
					preDays=(cM<M1)||(cM==M1&&countFromFirstDay<D);}
				var postDays=(month==endMonth&&countFromFirstDay>endDay);
				var dateToShow=(isLink)?countFromFirstDay:'';

			//compile displayDate('dd/mm/yyyy') argument for link
				var dateDay=countFromFirstDay;
				var dateMonth=currDate.getMonth()+1;
				var dateYear=currDate.getYear();
				var dateString=dateDay+"/"+dateMonth+"/"+dateYear;

			//Write table cell with link
				monthHTML+='<td height="20">';
				if(isLink&&!preDays&&!postDays)
				monthHTML+='<a href="javascript:parent.displayDate(\''+dateString+'\')">';
				monthHTML+=dateToShow;
				if(isLink&&!preDays&&!postDays)
				monthHTML+='</a>';
				monthHTML+='</td>';
			}
			monthHTML+='</tr>';
		}
			monthHTML+='\
			<tr><td height="20" bgcolor="#E43784" class="white">&nbsp;</td>\
			<td colspan="5" bgcolor="#E43784"><a href="javascript:top.calendar.hide()" class="white">Close</a></td>\
			<td bgcolor="#E43784" class="white">&nbsp;</td></tr>\
			<tr><td colspan="7" bgcolor="#000000"><img src="http://www.lastminute.com/lmn/calendar/images/trans.gif" width="140" height="2"></td></tr></table>'
	
		calendarHTML+=monthHTML+'</body></html>';

		currDate.setMonth(currDate.getMonth()+1);
	}
}

function getNumDays(M){
	if(M==2){
		// currentMonth defined to work around DP search being 14 days ahead (initMonth therefore being March in some cases)
		var currentMonth = new Date().getMonth()+1;
		var febYear=(initMonth<=2 || currentMonth<=2)?initYear:initYear*1+1;
		var febDate=new Date(Date.UTC(febYear,1,29));//yyyy/mm from 0/d from 1
		return (febDate.getMonth()==1)?29:28;
	}
	else return (M==9||M==4||M==6||M==11)?30:31;
}

//DROPDOWNS--
function setDays(buttonNo){
	if(N4)return;
	var D=eval("dropDay"+buttonNo).value*1;
	var M=eval("dropMonth"+buttonNo).value*1;
	var daysDD=eval("dropDay"+buttonNo);
	//lm.log('run: '+buttonNo+' Month: '+M);
 	//if(daysDD.length>=1){
		 if (daysDD.options[0].value=="0"){
			 daysDD.length=1;
			 for(i=1;i<getNumDays(M)+1;i++)daysDD.options[i]=new Option(i+1,i+1);
		} else {
			 daysDD.length=0;
			 for(i=0;i<getNumDays(M);i++) {
			 daysDD.options[i]=new Option(i+1,i+1);
			 }
		}
		if(hasOptionVal(daysDD,D))selOptionVal(daysDD,D);//keep day selected
		else daysDD[0].selected=true;//or set to first
	//}
}

function setDates(buttonNo){//onchange from month dropdown
	if(N4)return;
	setDays(buttonNo);
	currButtonNo=buttonNo;
	if(dropDay2)syncDropdowns();	
}