 function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 

var aCourseStartBook=new Array();
var idAccommodation=0;
var aCourseAccommodation=new Array();
var oDateStart=new Date();
var oDateEnd=new Date();

var oCourseEnd=new Date();
var oCourseStart=new Date();

var oAccommodationDateStart=new Date();
var oAccommodationDateEnd=new Date();


var bAcccomodationStartDate=false;
var bAcccomodationEndDate=false;

var hasDateRestriction=false;
var hasWeeksRestriction=false;

var nDay=1000*60*60*24;
var nWeek=nDay*7;

var min_weeks=0;
var max_weeks=0;
///////////////////////////////////////
//////////////////////////////////////////////////////////
function InsertDate(strDate){
	var aTmp=new Array();
	var newDate=new Date();
	aTmp=strDate.split("-");
	if (aTmp.length<2){
		aTmp=strDate.split("/");
	}
	newDate.setDate(aTmp[2]);
	newDate.setMonth(aTmp[1]-1);
	newDate.setYear(aTmp[0]);

	return newDate;
}


function fChangeDate(value, objDest){
	var aDate=value.split('-');
	objDest.value=aDate[2]+"/"+aDate[1]+"/"+aDate[0];
}

function fSetAccommodationDateEnd2(date){
	if(!bAcccomodationEndDate){
		var field = document.BookNow.date_end;
		field.value=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
	}
}

function fSetAccommodationDateEnd(date){

	var field = document.BookNow.date_end;
	bAcccomodationStartDate=true;
	var dateTmp=new Date();
	dateTmp.setTime(oCourseEnd.getTime());
	field.value=dateTmp.getFullYear()+"-"+(dateTmp.getMonth()+1)+"-"+dateTmp.getDate();
	oAccommodationDateEnd=InsertDate(field.value);
	fChangeDate(field.value, document.BookNow.show_date_end);
	bAcccomodationEndDate=true;

	feeCalculate();
}

function validateBook(obj){
	var Error="";
	if (!obj.school_id.selectedIndex){Error+="Please select the location \n"}
	if (!obj.course_level_id.selectedIndex){Error+="Please select your english level \n"}
	if (!obj.course_id.selectedIndex){Error+="Please select an english course \n"}
	if (!obj.course_start_id.selectedIndex){Error+="Please select a course start date \n"}
	if (!obj.weeks.selectedIndex){Error+="Please select the number of weeks of your course \n"}

	if (!obj.accommodation_id.selectedIndex&&idAccommodation>0){Error+="This course includes the accommodation fee, so you must select an accommodation \n"}


	if (obj.accommodation_id.selectedIndex>1){
		if (!obj.accommodation_service_id.selectedIndex){Error+="Please select a room option \n"}


		if (!bAcccomodationStartDate){Error+="Please select an accommodation start date \n"}
		if (!bAcccomodationEndDate){Error+="Please select an accommodation end date \n"}
	}


	if (Error){
		alert(Error);
		return false;
	}else{
		return true;
	}


}

function feeCalculate(){
	var obj=document.BookNow;
	var course_id=obj.course_id.options[obj.course_id.selectedIndex].value;
	var course_start_id=obj.course_start_id.options[obj.course_start_id.selectedIndex].value;
	var weeks=obj.weeks.options[obj.weeks.selectedIndex].value;
	var accommodation_id=obj.accommodation_id.options[obj.accommodation_id.selectedIndex].value;
	var accommodation_service_id=obj.accommodation_service_id.options[obj.accommodation_service_id.selectedIndex].value;
	var date=obj.date.value;
	var date_end=obj.date_end.value;
	
	obj = document.getElementById('accommodation_id');
	
	if((obj.selectedIndex) && (obj.selectedIndex!=1||(obj.selectedIndex==1&&idAccommodation>0))){
		document.getElementById('v_room').style.display='block';
		document.getElementById('v_start').style.display='block';
		document.getElementById('v_end').style.display='block';
	}else{
		document.getElementById('v_room').style.display='none';
		document.getElementById('v_start').style.display='none';
		document.getElementById('v_end').style.display='none';		
	}	
	
	var params="course_id="+course_id+"&course_start_id="+course_start_id+"&weeks="+weeks+"&accommodation_id="+accommodation_id+"&accommodation_service_id="+accommodation_service_id+"&date="+date+"&date_end="+date_end;
	getFeeCalculation(params);
}
////////////////////////////////////////////////////////////////

function fSetDefaults(obj){
	//THIS FUNCTION WITH DELAY IS MADE TO HANDLE AN IE BUG 
	if(!obj.school_id.selectedIndex&&idSchool>0){
		obj.school_id.value=idSchool;
		BookSchoolChange(obj.school_id);		
	}else if(!obj.course_level_id.selectedIndex&&idCourseLevel>0&&idSchool==obj.school_id.options[obj.school_id.selectedIndex].value){
		obj.course_level_id.value=idCourseLevel;
		BookLevelChange(obj.course_level_id);		
	}else if(!obj.course_id.selectedIndex&&idCourse>0&&idCourseLevel==obj.course_level_id.options[obj.course_level_id.selectedIndex].value&&idSchool==obj.school_id.options[obj.school_id.selectedIndex].value){
		obj.course_id.value=idCourse;
		BookCourseChange(obj.course_id);
	}else if(!obj.course_start_id.selectedIndex&&idCourseStart>0&&idCourse==obj.course_id.options[obj.course_id.selectedIndex].value){
		obj.course_start_id.value=idCourseStart;
		BookDateChange(obj.course_start_id);
	}else if(obj.weeks.selectedIndex<1&&nNumberWeeks>0&&idCourseStart==obj.course_start_id.options[obj.course_start_id.selectedIndex].value){
		obj.weeks.value=nNumberWeeks;
		BookWeekChange(obj.weeks);
	}else if(!obj.accommodation_id.selectedIndex&&obj.weeks.selectedIndex>0&&idAccommodation>0){
		obj.accommodation_id.value=idAccommodation;
		BookTypeChange(obj.accommodation_id);
	}
}
/////////////////////////////////////////////////
function BookaddLevels(params) {
	new Ajax.Request("/includes/ajax/level.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerLevel,
		onFailure: failHandler
	});
}
function BookaddSchools(limitcurrency) {
	new Ajax.Request("/includes/ajax/school.php", {
		parameters : "School=1&currency="+limitcurrency,
		onSuccess : BooksuccessHandlerSchool,
		onFailure: failHandler
	});
}
function BookaddCourses(params) {
	new Ajax.Request("/includes/ajax/course.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerCourse,
		onFailure: failHandler
	});
}
function BookaddDates(params) {
	new Ajax.Request("/includes/ajax/start_date.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerDate,
		onFailure: failHandler
	});
}
function BookaddWeeks(params) {
	new Ajax.Request("/includes/ajax/weeks.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerWeek,
		onFailure: failHandler
	});
}
function getFeeCalculation(params) {
	new Ajax.Request("/includes/ajax/fee_calculation.php", {
		parameters : params,
		onSuccess : successHandlerFee,
		onFailure: failHandler
	});
}
////////////////////////////////////////////////////////////////

function BookaddAccommodationTypes(params) {
	new Ajax.Request("/includes/ajax/accommodation_type.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerAccommodationType,
		onFailure: failHandler
	});
}
function BookaddAccommodationDates(params) {
	new Ajax.Request("/includes/ajax/accommodation_date.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerAccommodationDate,
		onFailure: failHandler
	});
}
function BookaddAccommodationRooms(params) {
	new Ajax.Request("/includes/ajax/accommodation_service.php", {
		parameters : params,
		onSuccess : BooksuccessHandlerAccommodationRooms,
		onFailure: failHandler
	});
}			


////////////////////////////////////////////////////////////////
function successHandlerFee(resp, jsonObj) {
	valor=resp.responseXML;
	var value = zXPath.selectSingleNode(valor, "/xml/fee").firstChild.data;
//	alert("fee: "+value);
	document.BookNow.total_fee.value=value;
}			


function BooksuccessHandlerSchool(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.BookNow.school_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.BookNow.school_id.disabled=false;
	document.BookNow.school_id.options[0].disabled=true;
	
	setTimeout("fSetDefaults(document.BookNow)",1);

}			
function BooksuccessHandlerLevel(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

	removeOptions(document.BookNow.course_level_id);
	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.BookNow.course_level_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.BookNow.course_level_id.disabled=false;
	document.BookNow.course_level_id.options[0].disabled=true;
	
	setTimeout("fSetDefaults(document.BookNow)",1);

}

function BooksuccessHandlerCourse(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");
	aCourseAccommodation=new Array();
	
	removeOptions(document.BookNow.course_id);
	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.BookNow.course_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
		aCourseAccommodation[parseInt(zXPath.selectSingleNode(nodes[i], 'value').firstChild.data)]=zXPath.selectSingleNode(nodes[i], 'accommodation_id').firstChild.data;
	}
	document.BookNow.course_id.disabled=false;
	document.BookNow.course_id.options[0].disabled=true;

	setTimeout("fSetDefaults(document.BookNow)",1);

}

function BooksuccessHandlerDate(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/year");

	var obj=document.BookNow.course_start_id;
//	feeCalculate();

	var sCourseStartOptionTxt=obj.options[0].text;

	obj.innerHTML="";
	var optionOne=document.createElement("OPTION");
	var textOne=document.createTextNode(sCourseStartOptionTxt);
	optionOne.appendChild(textOne);
	obj.appendChild(optionOne);
	
	for (var i = 0; i < nodes.length; i++) {
		var yearTxt= zXPath.selectSingleNode(nodes[i], 'value').firstChild.data;
		months= zXPath.selectNodes(nodes[i], 'month');
		for (var ii = 0; ii < months.length; ii++) {
			var month= zXPath.selectSingleNode(months[ii], 'value').firstChild.data;
			var txtMonth=month+"/"+yearTxt;
			var optionGroup=document.createElement("OPTGROUP");
			optionGroup.label=txtMonth;
			obj.appendChild(optionGroup);
			var days= zXPath.selectNodes(months[ii], 'day');
			for (var iii = 0; iii < days.length; iii++) {
				aCourseStartBook[zXPath.selectSingleNode(days[iii], 'id').firstChild.data]=yearTxt+"/"+(month)+"/"+zXPath.selectSingleNode(days[iii], 'value').firstChild.data;
				var option=document.createElement("OPTION");
				var sTxt=zXPath.selectSingleNode(days[iii], 'value').firstChild.data+"/"+month;
				
				var oTxt=document.createTextNode(sTxt);
				option.value=zXPath.selectSingleNode(days[iii], 'id').firstChild.data;
				option.appendChild(oTxt);
				
				optionGroup.appendChild(option);
				
			}
			
		}
	}
	document.BookNow.course_start_id.disabled=false;
	document.BookNow.course_start_id.options[0].disabled=true;
	
	setTimeout("fSetDefaults(document.BookNow)",1);
}
///////////////////////////////////////////

function BooksuccessHandlerAccommodationType(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");
	feeCalculate();

	removeOptions(document.BookNow.accommodation_id);
	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.BookNow.accommodation_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.BookNow.accommodation_id.disabled=false;
	document.BookNow.accommodation_id.options[0].disabled=true;

	setTimeout("fSetDefaults(document.BookNow)",1);

}			
function BooksuccessHandlerAccommodationRooms(resp, jsonObj) {
	feeCalculate();

	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

	removeOptions(document.BookNow.accommodation_service_id);
	for (var i = 0; i < nodes.length; i++) {
		appendOptionWithTitle(document.BookNow.accommodation_service_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.BookNow.accommodation_service_id.disabled=false;
	document.BookNow.accommodation_service_id.options[0].disabled=true;
	if(idAccommodation>0){
		document.BookNow.accommodation_service_id.selectedIndex=1;
		BookServiceChange(document.BookNow.accommodation_service_id);
	}
}
function BooksuccessHandlerAccommodationDate(resp, jsonObj) {
	var Warm="";

	//feeCalculate();

	bAcccomodationStartDate=true;
	bAcccomodationEndDate=false;				

	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml");
	var StartDate=zXPath.selectSingleNode(nodes[0], 'start').firstChild.data;
	var EndDate=zXPath.selectSingleNode(nodes[0], 'end').firstChild.data;
	min_weeks=zXPath.selectSingleNode(nodes[0], 'min_weeks').firstChild.data;
	max_weeks=zXPath.selectSingleNode(nodes[0], 'max_weeks').firstChild.data				
	obj=document.BookNow;

	var weeks_course=obj.weeks.options[obj.weeks.selectedIndex].value;
	oCourseStart=InsertDate(aCourseStartBook[obj.course_start_id.options[obj.course_start_id.selectedIndex].value]);
	//alert (weeks_course);
	oCourseEnd.setTime((oCourseStart.getTime()+1000*60*60*24*7*weeks_course));

	if (parseInt(min_weeks)||parseInt(max_weeks)){
		hasWeeksRestriction=true;
	}else{
		hasWeeksRestriction=false;
	}

	if (StartDate!='0'){
		hasDateRestriction=true;
		oDateStart=InsertDate(StartDate);
		oDateEnd=InsertDate(EndDate);
		if (oCourseStart.getTime()>=oDateStart.getTime()&&oCourseStart.getTime()<=oDateEnd.getTime()&&oCourseEnd.getTime()>=oDateStart.getTime()&&oCourseEnd.getTime()<=oDateEnd.getTime()){
			var oCourseStartTmp=new Date();
			oCourseStartTmp.setTime(oCourseStart.getTime()-nDay);
			if(oCourseStartTmp.getDay()!=0){
				oCourseStartTmp.setTime(oCourseStartTmp.getTime()-nDay*oCourseStartTmp.getDay());
			}
			
			obj.date.value=oCourseStartTmp.getFullYear()+"-"+(oCourseStartTmp.getMonth()+1)+"-"+oCourseStartTmp.getDate();
		}else{
			var oCourseStartTmp=new Date();
			oCourseStartTmp.setTime(oDateStart.getTime());
			
			if(oCourseStartTmp.getDay()!=0){
				oCourseStartTmp.setTime(oCourseStartTmp.getTime()-nDay*oCourseStartTmp.getDay());
			}			
			obj.date.value=oCourseStartTmp.getFullYear()+"-"+(oCourseStartTmp.getMonth()+1)+"-"+oCourseStartTmp.getDate();
			Warm+="The course that you has choiced ("+oCourseStart.getDate()+"/"+(oCourseStart.getMonth()+1)+"/"+oCourseStart.getFullYear()+"-"+oCourseEnd.getDate()+"/"+(oCourseEnd.getMonth()+1)+"/"+oCourseEnd.getFullYear()+") is not within the dates of the accommodation ("+oDateStart.getDate()+"/"+(oDateStart.getMonth()+1)+"/"+oDateStart.getFullYear()+"-"+oDateEnd.getDate()+"/"+(oDateEnd.getMonth()+1)+"/"+oDateEnd.getFullYear()+").\n";
		}
	}else{
		hasDateRestriction=false;
		var aDateTmp=new Date();
		aDateTmp=InsertDate(aCourseStartBook[obj.course_start_id.options[obj.course_start_id.selectedIndex].value]);
		aDateTmp.setTime(aDateTmp.getTime()-nDay);
		if(aDateTmp.getDay()!=0){
			aDateTmp.setTime(aDateTmp.getTime()-nDay*aDateTmp.getDay());
		}
		obj.date.value=aDateTmp.getFullYear()+"-"+(aDateTmp.getMonth()+1)+"-"+aDateTmp.getDate();
	}

	if (parseInt(weeks_course)<parseInt(min_weeks)&&parseInt(min_weeks)){
		Warm+="The minimum of weeks ("+min_weeks+") of that accommodation is bigger than the course length ("+weeks_course+").\n";
	}else if(parseInt(weeks_course)>parseInt(max_weeks)&&parseInt(max_weeks)){
		Warm+="The maximum of weeks ("+max_weeks+") of that accommodation is smaller than the course length ("+weeks_course+").\n";
	}



	fChangeDate(obj.date.value, obj.show_date_start);


	oAccommodationDateStart=InsertDate(obj.date.value);

	if(idAccommodation>0){
		fSetAccommodationDateEnd(oAccommodationDateStart);
	}else{
		var oDateTmp=new Date();
		if(min_weeks>0){
			oDateTmp.setTime(oAccommodationDateStart.getTime()+(nWeek*min_weeks)-nDay);
		}else{
			oDateTmp.setTime(oAccommodationDateStart.getTime()+nDay);
		}
		fSetAccommodationDateEnd2(oDateTmp);
	}	
	if (Warm){
		alert(Warm+"\nYou may want to select another accommodation.");
	}
	//obj.setStart.disabled=false;
}
function BooksuccessHandlerWeek(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

	removeOptions(document.BookNow.weeks);
	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.BookNow.weeks,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.BookNow.weeks.disabled=false;
	document.BookNow.weeks.options[0].disabled=true;
	
	setTimeout("fSetDefaults(document.BookNow)",1);
	
}			
///////////////////////////////////////
function BookSchoolChange(obj){
	disableNext(obj);
	obj.form.total_fee.value=0;
	id=obj.options[obj.selectedIndex].value;
	txt="school_id="+id;
	BookaddLevels(txt);				
}
function BookLevelChange(obj){
	disableNext(obj);
	
	oForm=obj.form;
	id=obj.options[obj.selectedIndex].value;
	idschool=oForm.school_id.options[oForm.school_id.selectedIndex].value;
	txt="course_level_id="+id+"&school_id="+idschool;
	BookaddCourses(txt);				
}
function BookCourseChange(obj){
	disableNext(obj);
	
	id=obj.options[obj.selectedIndex].value;
	txt="course_id="+id;
	
	idAccommodation=aCourseAccommodation[parseInt(id)];
	
	
	BookaddDates(txt);				
}
function BookDateChange(obj){
	disableNext(obj);

//	alert(obj.options[obj.selectedIndex].value);
	
	oForm=obj.form;
	id=obj.options[obj.selectedIndex].value;
	idcourse=oForm.course_id.options[oForm.course_id.selectedIndex].value;
	if(!isInt(id)) return;
	txt="start_date_id="+id+"&course_id="+idcourse;
	BookaddWeeks(txt);				
}
////////////////////////////////////////////////////////////////
				
///////////////////////////////////////
function BookWeekChange(obj){
	disableNext(obj);
	oForm=obj.form;
	id=obj.options[obj.selectedIndex].value;
	idcourse=oForm.course_id.options[oForm.course_id.selectedIndex].value;
	idstart=oForm.course_start_id.options[oForm.course_start_id.selectedIndex].value;
	idschool=oForm.school_id.options[oForm.school_id.selectedIndex].value;
	txt="weeks="+id+"&course_id="+idcourse+"&course_start_id="+idstart+"&school_id="+idschool+"&accommodation_id="+idAccommodation;
	BookaddAccommodationTypes(txt);				
}
function BookTypeChange(obj){
	disableNext(obj);
	if(obj.selectedIndex!=1||(obj.selectedIndex==1&&idAccommodation>0)){
		oForm=obj.form;
		id=obj.options[obj.selectedIndex].value;
		idcourse=oForm.course_id.options[oForm.course_id.selectedIndex].value;
		idstart=oForm.course_start_id.options[oForm.course_start_id.selectedIndex].value;
		idschool=oForm.school_id.options[oForm.school_id.selectedIndex].value;
		txt="accommodation_id="+id+"&course_id="+idcourse+"&course_start_id="+idstart+"&school_id="+idschool;
		BookaddAccommodationRooms(txt);
		document.getElementById('v_room').style.display='block';
		document.getElementById('v_start').style.display='block';
		document.getElementById('v_end').style.display='block';
	}else{
		document.getElementById('v_room').style.display='none';
		document.getElementById('v_start').style.display='none';
		document.getElementById('v_end').style.display='none';		
		feeCalculate();
	}
}
function BookServiceChange(obj){

	//	obj.form.setStart.disabled=true;
	//	obj.form.setEnd.disabled=true;
	obj.form.date.value="";
	obj.form.date_end.value="";				
	id=obj.options[obj.selectedIndex].value;
	txt="accommodation_service_id="+id;
	BookaddAccommodationDates(txt);				
}
