/*

  ajax.js

  usage:
    Form tag action document.getElementById() and name="" must be the same e.g.:

      <form action="javascript:get(document.getElementById('FormName'));" name="FormName">

    The form is submitted with a normal submit button
    Server response will be sent where id="ajax_response" by default:

      <div name="ajax_response" id="ajax_response"></div>

    Set the ajax_id in the onClick method to write output to specific id e.g.

      onclick="javascript:get(document.getElementById('form_id')); ajax_id='div_id';\">

  This will replace the default id="ajax_response" with the specifiec ajax_id='div_id'

  TODO: Handle checkboxes like the radio buttons

*/


// Reset http_request
var http_request = false;
var ajax_id = "ajax_response";

// The standard send xml request function
function makeRequest(url, parameters) {
	http_request = false;

	// XMLHttpRequest : Mozilla/Safari
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();

		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}

	// ActiveXObject : Internet Explorer
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}

	// Unsupported Browser Type
	if (!http_request) {
		//alert('Sorry, your browser type is out dated or unsupported. We recommend downloading the latest version of Firefox at mozilla.com/firefox');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}


// Generic browser alert
function alertContents() {
	var destination = ajax_id; //document.getElementById("ajax_id").value;

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			//alert(destination);
			result = http_request.responseText;
			document.getElementById(ajax_id).innerHTML = result;            
		} else {
			//alert('Sorry, your browser type is out dated or unsupported. We recommend downloading the latest version of Firefox at mozilla.com/firefox');
		}
	}
}


// Get values by tag from browser
function get(obj) {

	var get_string = "?";

	// Get total input elements
	var input_tags = obj.getElementsByTagName("input").length;
	var text_areas = obj.getElementsByTagName("textarea").length;
	var select_fields = obj.getElementsByTagName("select").length;

	//document.write();

	// Get text areas
	for (i=0; i<text_areas; i++) {
		get_string += obj.getElementsByTagName("textarea")[i].name + "=" + escape(obj.getElementsByTagName("textarea")[i].value) + "&";
	} 

	// Get input elements
	for (i=0; i<input_tags; i++) {
		// Case between radio buttons / standard input
		if (obj.getElementsByTagName("input")[i].type == "radio") {
			if (obj.getElementsByTagName("input")[i].checked) {
				get_string += obj.getElementsByTagName("input")[i].name + "=" + escape(obj.getElementsByTagName("input")[i].value) + "&";
			}
		} else {
			get_string += obj.getElementsByTagName("input")[i].name + "=" + escape(obj.getElementsByTagName("input")[i].value) + "&";
		}
	}

	// Get select fields 
	for (i=0; i<select_fields; i++) {
		get_string += obj.getElementsByTagName("select")[i].name + "=" + escape(obj.getElementsByTagName("select")[i].value) + "&";
	}

	// Send string to makeRequest()
	makeRequest('/ajax.php', get_string);
}

// Layer functions
function showLayer(id) {
  document.getElementById(id).style.visibility = 'visible';
}

function hideLayer(id) {
  document.getElementById(id).style.visibility = 'hidden';
}

