var ajax_error = "An error has occured retrieving site xml content";

//read in paramaters from calling webpage request
function ajax_request(){
	this.load_file;
	this.mode;
	this.generate_results = generate_results;
}

//dynamically display the results into the specified element
function generate_results(){
	mode = this.mode;
	load_web_document(this.load_file);
}

function load_web_document(url){
	xml_http = null;

	if(window.XMLHttpRequest){
		xml_http = new XMLHttpRequest();
	} else if(window.ActiveXObject){
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(xml_http != null){
		xml_http.onreadystatechange = on_response;
		xml_http.open("GET",url,true);
		xml_http.send(null);
	}
	else{
		//an error occured trying to receive an ajax reponse request
		alert(ajax_error);
	}
}

//check status of ajax retrieval
function check_ready_state(obj){
	if(obj.readyState == 4){
		if(obj.status == 200){
			return true;
		}
		else{
			//problem parsing xml file
			//an error occured trying to receive an ajax reponse request
			alert(ajax_error);
		}
	}
}

//process ajax content once successfully in browser memory
function on_response(){
	if(check_ready_state(xml_http)){
		switch(mode){
			case "application_form":
				if(xml_http.responseText == "true") {
					//document.write(xml_http.responseText);
					application_successful();
				}
				
				break;			
			case "registration_form":				
				if(xml_http.responseText == "true"){
					registration_successful();
				}
			
				break;
			case "developments":
				get_element("developments_content").style.textAlign = "left";
				get_element("developments_content").innerHTML = xml_http.responseText;
				var title_tag = xml_http.responseText.match(/<h3.+h3>/).toString();
				var title_text = title_tag.match(/>.+</).toString();
				document.title = "See what we are building - " + title_text.substring(1,title_text.length-1);
				break;
		}	
	}				
}


//reset priority registration form
function registration_successful(){
	var obj_form = get_element("registration_form");
	
	obj_form.title.selectedIndex = 0;
	obj_form.first_name.value = "";
	obj_form.last_name.value = "";
	obj_form.country.selectedIndex = 0;
	obj_form.nationality.selectedIndex = 0;
	obj_form.email.value = "";
	obj_form.statement_01.checked = false;
	obj_form.statement_02.checked = false;
	obj_form.statement_03.checked = false;
	obj_form.statement_04.checked = false;
	
	//display success message
	get_element("form_message").innerHTML = "&nbsp;<strong>Registration successful</strong>";
	setTimeout("clear_form_message()", 3000);
}

function clear_form_message() {
	get_element("form_message").innerHTML = "";
}

//reset application form
function application_successful(){
	var obj_form = get_element("application_form");
	
	obj_form.title.selectedIndex = 0;
	obj_form.first_name.value = "";
	obj_form.last_name.value = "";
	obj_form.nationality.selectedIndex = 0;
	obj_form.day_of_birth.selectedIndex = 0;
	obj_form.month_of_birth.selectedIndex = 0;
	obj_form.year_of_birth.selectedIndex = 0;
	obj_form.address_line_1.value = "";
	obj_form.address_line_2.value = "";
	obj_form.city.value = "";
	obj_form.state.value = "";
	obj_form.postcode.value = "";
	obj_form.country.selectedIndex = 0;
	obj_form.telephone.value = "";
	obj_form.email.value = "";
	obj_form.language_01.selectedIndex = 0;
	obj_form.language_02.selectedIndex = 0;
	obj_form.language_03.selectedIndex = 0;
	obj_form.visa_type.selectedIndex = 0;
	obj_form.day_of_visa_expiry.selectedIndex = 0;
	obj_form.month_of_visa_expiry.selectedIndex = 0;
	obj_form.year_of_visa_expiry.selectedIndex = 0;
	obj_form.education_level.value = "";
	obj_form.degree.selectedIndex = 0;
	obj_form.field.value = "";
	obj_form.additional_info.value = "";
	obj_form.company_name.value = "";
	obj_form.designation_title.value = "";
	obj_form.day_of_employment.selectedIndex = 0;
	obj_form.month_of_employment.selectedIndex = 0;
	obj_form.year_of_employment.selectedIndex = 0;
	obj_form.responsibilities.value = "";
	obj_form.reasons_for_leaving.value = "";
	obj_form.interviewed_by_nakheel.value = "";
	obj_form.day_of_interview.selectedIndex = 0;
	obj_form.month_of_interview.selectedIndex = 0;
	obj_form.year_of_interview.selectedIndex = 0;
	obj_form.interviewed_position.value = "";
	
	//display success message
	get_element("form_message").innerHTML = "&nbsp;<strong>Application successful</strong>";
	setTimeout("clear_form_message()", 3000);
}

//account for the way Firefox treats whitespace/line feeds when returning xml nodes
function get_value_of_child(nodeList,childPosition){
	var item = (is_ie) ? nodeList.item(childPosition) : nodeList.item(childPosition).nextSibling;

	if(item.childNodes.length>0){
		return item.childNodes[0].nodeValue;
	}
	
	return null;
}