
// initialization

var firstSelection       = 0;
var secondSelection      = 0;
var state                = 0;
var s;
var n                    = 0;
var ss;

// calendar colors

var freeColor            = '#008800';
var halfFreeColor        = '#008900'; // same as confirmed + 1 for 1st day of booked dates
var confirmColor         = '#a10000';
var heldColor            = '#ff9900';
var unconfirmColor       = '#ffcc00';
var inquiryColor         = '#3355ff';

// selection colors

var selectColor          = '#ffff00';
var selectConfirmColor   = '#ffff01';
var selectHeldColor      = '#ffff02';
var selectUnconfirmColor = '#ffff03';
var selectInquiryColor   = '#ffff04';
var selectHalfFreeColor  = '#ffff05';
var selectChinColor      = '#ffff06';

var minstay              = 1;
var days                 = 999999; // was 365

var checkin              = "Pick Date";
var checkout             = "Pick Date";

function isFreeDay(dayID){
	var s=document.all.item("td"+dayID).bgColor;

	if(s==freeColor || s==halfFreeColor || s== selectColor || s== selectHalfFreeColor || s== selectChinColor || s== unconfirmColor || s== inquiryColor ){
		return 1;
	}else{
		return 0;
	}
}

function isSelectedDay(dayID){

	//1=the day is selected, 0=the day is not selected
	//the day may be selected if it already selected or if it free or half free

	var s=document.all.item("td"+dayID).bgColor;
	if(s==selectColor || s==selectHalfFreeColor || s== selectChinColor || s== selectUnconfirmColor || s== selectInquiryColor ){
		return 1;
	}else{
		return 0;
	}
}

function setSelection(dayID){
	var s=document.all.item("td"+dayID).bgColor;
	if(s==freeColor || s==selectChinColor){
		document.all.item("td"+dayID).bgColor=selectColor;
	}else if(s==halfFreeColor){
		document.all.item("td"+dayID).bgColor=selectHalfFreeColor;
	}else if(s==unconfirmColor){
		document.all.item("td"+dayID).bgColor=selectUnconfirmColor;
	}else if(s==inquiryColor){
		document.all.item("td"+dayID).bgColor=selectInquiryColor;
	}
	return;
}

function reliseSelection(dayID){
	var s=document.all.item("td"+dayID).bgColor;
	if(s==selectColor || s==selectChinColor){
		document.all.item("td"+dayID).bgColor=freeColor;
	}else if(s==selectHalfFreeColor){
		document.all.item("td"+dayID).bgColor=halfFreeColor;
	}else if(s==selectUnconfirmColor){
		document.all.item("td"+dayID).bgColor=unconfirmColor;
	}else if(s==selectInquiryColor){
		document.all.item("td"+dayID).bgColor=inquiryColor;
	}
}

function overCell(i,date){

	//select the start date

	if(state==0||state==1){
		if(state==1) reliseSelection(firstSelection);

		//check is the date free

		if(isFreeDay(i)==1){ //the date is free
			if(i+minstay<=days){//control that user can reserve at least minstay days on the calendar
				var flag=true;//if in closest minstay days only free or selected days
				for(k=i+1;k<i+minstay+1;k++){
					if(isFreeDay(k)==0){
						flag=false;
						break;
					}
				
				}
				if(flag==true){
					setSelection(i);
					state=1;
					firstSelection=i;
				}else{
					resinfo.innerHTML="You have to stay for at least "+minstay+" nights but these nights are booked.";
					state=0;
				}
			}else{
				resinfo.innerHTML="You have to stay for at least "+minstay+" nights so the Departure date is out of scope.";
				state=0;
			}
		}else{
			resinfo.innerHTML="Choose another date.<br>This date is booked.";
			state=0;
		}
	}	


	if(state==2||state==3){
		if(state==3){
			for(k=firstSelection;k<secondSelection+1;k++){
				reliseSelection(k);
			}
		}
		//is the date range valid
		var flag=true;
		if(i>firstSelection){
			if(i-firstSelection+1>minstay){
				for(k=firstSelection+minstay;k<i+1;k++){
					if(isFreeDay(k)==0){
						flag=false;
						resinfo.innerHTML="There are busy dates between the dates you selected.";
						break;
					}
				}
			}else{
				flag=false;
				resinfo.innerHTML="The Arrival date is "+checkin+" and you have to stay for at least "+minstay+" nights,<br>select another Departure date.";
				arrival.innerHTML=checkin;
			}
		}else{
			flag=false;
			resinfo.innerHTML="The Arrival date is "+checkin+" The Departure date must be later,<br>select another Departure date.";
			arrival.innerHTML=checkin;
		}
		if(flag==true){
			secondSelection=i;
			for(k=firstSelection;k<i+1;k++){
				setSelection(k);
			}
			state=3;
			resinfo.innerHTML="The Arrival date is "+checkin+"<br>and the Departure date will be "+date+".";	
			arrival.innerHTML=checkin;
			departure.innerHTML=date;
		}
	}

}

function clickCell(i,propid,date){

	if(state==0||state==1){

		//set the start date
		//check if the date is valid

		if(isSelectedDay(i)==0){
			resinfo.innerHTML="The date you clicked isn't available. Please choose another date.";

		}else if(days-i-minstay<0){
			resinfo.innerHTML="The date you clicked isn't available. You have to stay for at least "+minstay+" nights.";

		}else{
			var flag=true;
			for(k=i+1;k<i+minstay+1;k++){
				if(isFreeDay(k)==0){
					flag=false;
					break;
				}
			}		
			if(flag==false){
				resinfo.innerHTML="The date you clicked isn't available.<br>You have to stay for at least "+minstay+" nights but these nights are busy.";
			}else{
				checkin=date;
				state=2;
			}
		}

	}else if(state==3||state==2){

		//set the finish date
		//check if the date is valid and free

		if(isSelectedDay(i)==1){ 

			//the date is free

			if(i==firstSelection){
				resinfo.innerHTML="The Arrival and Departure dates may not be the same.<br>Please choose another date.";

			}else{
				state=4;
				checkout=date;

				if(format=='small'){
					var link = "cal?action=button&format=small&propid="+propid+"&checkin="+checkin+"&checkout="+checkout;
					loadPage("resinfo",link);
				}else{
					resinfo.innerHTML="The Arrival date is "+checkin+"<br>and the Departure date is "+checkout+".";
					arrival.innerHTML=checkin;
					departure.innerHTML=checkout;
					var link = "cal?action=calc&propid="+propid+"&checkin="+checkin+"&checkout="+checkout;
					loadPage("rescost",link);
				}
			}
		}else{
			resinfo.innerHTML="The date you clicked isn't available.<br>Please choose another date.";
		}
		
	}else if(state==4){

		//set a new start date
		
		for(k=firstSelection;k<secondSelection+1;k++){
			reliseSelection(k);

		}

		//check if the date is valid

		s=document.all.item("td"+i).bgColor;
		var n=i+1;

		if(isFreeDay(i)==0){
			resinfo.innerHTML="The date you clicked isn't available.<BR>Please choose another date.";
		}else if(n>days){
			resinfo.innerHTML="The date you clicked isn't available - the next date is out of scope and may be busy.<BR>Please choose another date.";
		}else if(isFreeDay(n)==0){
			resinfo.innerHTML="The date you clicked isn't available - the next date is busy.<BR>Please choose another date.";
		}else{
			state=2;
			checkin=date;
			firstSelection=i;
			setSelection(i);
			resinfo.innerText="The arrival date is "+checkin;
			arrival.innerHTML=date;
		}

	}

}