﻿var boolProcess;

function GetXmlHttpObject() {

	var xmlHttp = false;

	try {

		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	
	}

	catch (e) {

		try {

			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	
		}

		catch (E) {

			xmlHttp = false;
	
		}

	}

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {

		xmlHttp = new XMLHttpRequest();
	
	}
	return xmlHttp;
}

function submitForm(form, script, divID, funcValidate) {
	
	var idObj;
	var strPost;
	
	strPost = getFormValues(form, funcValidate);
	
	if(boolProcess) {
		idObj = document.getElementById(divID);
		process(script, idObj, "POST", strPost);
		
	}
	
}

function submitLink(script, divID) {

	idObj = document.getElementById(divID);
	process(script, idObj, "GET", null);
		
}

function submitPage(script, divID, id, node) {

	rebuildMenu(id, node);
	
	idObj = document.getElementById(divID);
	process(script, idObj, "GET", null);
}

function rebuildMenu(id, node) {

	var name = Array(5);
	var path = Array(5);
	var html;
	
	name[0] = "Home";
	name[1] = "Hearing Loss Facts";
	name[2] = "Hearing Device Information";
	name[3] = "About Us";
	name[4] = "Contact Us";
	
	path[0] = node + "index.php?intID=0";
	path[1] = node + "pages/hlf.php?intID=1";
	path[2] = node + "pages/hai.php?intID=2";
	path[3] = node + "pages/au.php?intID=3";
	path[4] = node + "pages/cu.php?intID=4";
	
	html = "<table class=\"menuTable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
	
	for(var i = 0; i < name.length; i++) {
		var n = name[i];
		var p = path[i];
				
		if(i == id) {
			html+= "<tr>\n<td class=\"menu\"><a class=\"act\" href=\"javascript:submitPage('" + p + "', 'pageBody'," + i + ", '" + node + "')\">" + n + "</a></td>\n</tr>\n";
		}
		else {
			html += "<tr>\n<td class=\"menu\"><a class=\"norm\" href=\"javascript:submitPage('" + p + "', 'pageBody'," + i + ", '" + node + "')\">" + n + "</a></td>\n</tr>\n";
		}
	}
	
	html += "</table>\n";
	
	document.getElementById("navigation").innerHTML = html;
}

function getFormValues(form, funcValidate) {

	var boolValid;
	var i;
	var str = "";
	
	boolProcess = true;
	
	for(i = 0; i < form.elements.length; i++) {
	
		if(funcValidate) {
		
			if(boolProcess) {

				boolValid = funcValidate(form, form.elements[i].value, form.elements[i].name);
			
				if(!boolValid) {
				
					boolProcess = false;
				}
				
			}
		
		}
		if(form.elements[i].type == "checkbox" && !form.elements[i].checked) {
			continue;
		}
		else if(form.elements[i].type == "select-multiple") {
			for(var k = 0; k < form.elements[i].options.length; k++) {
				if(form.elements[i].options[k].selected) {
					str += form.elements[i].name + "=" + escape(form.elements[i].options[k].value) + "&";
				}
			}
		}
		else if(form.elements[i].type == "radio" && !form.elements[i].checked) {
			continue
		}
		else {
			str += form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
		}
	
	}
	
	return str;

}

function process(script, idObj, method, strPost) {
	
	xmlHttp=GetXmlHttpObject();
	if(method == "GET") {
	
		xmlHttp.open("GET", script);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState==200) { 
 				idObj.innerHTML = xmlHttp.responseText;
 			}
 		}
 		xmlHttp.send(null);
	
	}
	else {
	
		xmlHttp.open("POST", script, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", strPost.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState==200) { 
 				idObj.innerHTML = xmlHttp.responseText;
 			}
 		}
 		
		xmlHttp.send(strPost);
	}
}
