var aCourseStart=new Array();


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

function addLevels(params) {
	new Ajax.Request("/includes/ajax/level.php", {
		parameters : params,
		onSuccess : successHandlerLevel,
		onFailure: failHandler
	});
}
function addSchools() {
	new Ajax.Request("/includes/ajax/school.php", {
		parameters : "School=1",
		onSuccess : successHandlerSchool,
		onFailure: failHandler
	});
}
function addCourses(params) {
	new Ajax.Request("/includes/ajax/course.php", {
		parameters : params,
		onSuccess : successHandlerCourse,
		onFailure: failHandler
	});
}
function addDates(params) {
	new Ajax.Request("/includes/ajax/start_date.php", {
		parameters : params,
		onSuccess : successHandlerDate,
		onFailure: failHandler
	});
}
function addWeeks(params) {
	new Ajax.Request("/includes/ajax/weeks.php", {
		parameters : params,
		onSuccess : successHandlerWeek,
		onFailure: failHandler
	});
}
///////////////////////////////////////
function failHandler(){
	alert('There was an error.')
}
///////////////////////////////////////
function successHandlerSchool(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

	for (var i = 0; i < nodes.length; i++) {
		appendOption(document.QuickSearch.school_id,  zXPath.selectSingleNode(nodes[i], 'name').firstChild.data, zXPath.selectSingleNode(nodes[i], 'value').firstChild.data);
	}
	document.QuickSearch.school_id.disabled=false;
	document.QuickSearch.school_id.options[0].disabled=true;
	setTimeout("fSetDefaultsQuick(document.QuickSearch)",1);
	
}			
function successHandlerLevel(resp, jsonObj) {
	valor=resp.responseXML;
	var nodes = zXPath.selectNodes(valor, "/xml/elem");

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

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

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

	setTimeout("fSetDefaultsQuick(document.QuickSearch)",1);

}

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

	var obj=document.QuickSearch.course_start_id;

	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++) {
				aCourseStart[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.QuickSearch.course_start_id.disabled=false;
	document.QuickSearch.course_start_id.options[0].disabled=true;

	setTimeout("fSetDefaultsQuick(document.QuickSearch)",1);

}

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

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

	setTimeout("fSetDefaultsQuick(document.QuickSearch)",1);

}			
///////////////////////////////////////
function SchoolChange(obj){
	disableNext(obj);
	
	id=obj.options[obj.selectedIndex].value;
	txt="school_id="+id;
	addLevels(txt);				
}
function LevelChange(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;
	addCourses(txt);				
}
function CourseChange(obj){
	disableNext(obj);
	
	id=obj.options[obj.selectedIndex].value;
	txt="course_id="+id;
	addDates(txt);				
}
function DateChange(obj){
	disableNext(obj);
	
	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;
	addWeeks(txt);				
}
///////////////////////////////////////////////////
function validateQuick(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 (Error){
		alert(Error);
		return false;
	}else{
		return true;
	}

	
}

////////////////////////////////////
///////////////////////////////////////
function fSetDefaultsQuick(obj){
	//THIS FUNCTION WITH DELAY IS MADE TO HANDLE AN IE BUG 
	
	if(obj.school_id.selectedIndex<1&&idSchool>0){
		obj.school_id.value=idSchool;
		SchoolChange(obj.school_id);		
	}else if(obj.course_level_id.selectedIndex<1&&idCourseLevel>0&&idSchool==obj.school_id.options[obj.school_id.selectedIndex].value){
		obj.course_level_id.value=idCourseLevel;
		LevelChange(obj.course_level_id);		
	}else if(obj.course_id.selectedIndex<1&&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;
		CourseChange(obj.course_id);
	}else if(obj.course_start_id.selectedIndex<1&&idCourseStart>0&&idCourse==obj.course_id.options[obj.course_id.selectedIndex].value){
		obj.course_start_id.value=idCourseStart;
		DateChange(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;
	}
}
