﻿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) {
		alert("proccessing");
		idObj = document.getElementById(divID);
		process(script, idObj, "POST", strPost);
		
	}
	
}

function submitLink(script, divID) {

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

function submitContact(form, script, divID, errID) {
	var idObj;
	var strPost;
	
	if(checkContact(form, errID)) {
		strPost = getFormValues(form, false);
		idObj = document.getElementById(divID);
		process(script, idObj, "POST", strPost);
	}
	
	return false;
}

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);
	}
}