// JavaScript Document

IE = document.all?true:false;
if (typeof(ErrorHolder)=="undefined"){
	ErrorHolder = "ErrorDiv";
}

if (typeof(SuccessHolder)=="undefined"){
	SuccessHolder = "SuccessDiv";
}

if (typeof(BadInputMessage)=="undefined"){
	BadInputMessage = "Please be sure you have filled out each field with a * next to it, and entered all information accurately."
}

if (typeof(SuccessMessage)=="undefined"){
	SuccessMessage = "<b>Your message has been sent. Thank you for your input!</b><IMG src='http://inbound.linktrust.com/redirconsume.aspx?CID=2476' width='1' height='1' border='0'>";

}

if (typeof(FailureMessage)=="undefined"){
	FailureMessage = "<b>Was unable to connect to server, please verify your connection.</b>";
}

if (typeof(SendScript)=="undefined"){
	SendScript = "SendMail.php";
}

if (typeof(OkColor)=="undefined"){
	OkColor = "#000000";
}

if (typeof(BadColor)=="undefined"){
	BadColor = "red";
}

Extra = new Object();

function SubmitForm() {
	var FormLabels = document.getElementsByTagName("div");
	var Query = "";
	for (Obj in Extra) {
		Query += Obj + "=" + Extra[Obj] + "&";
	}
	var IsOk = true;
	for (var n=0;n<=FormLabels.length-1;n++) {
		if (FormLabels[n].id.match(/Label$/)) {
			var Label = FormLabels[n];
			var LabelText = Label.firstChild.nodeValue;
			var LabelId = Label.id.substring(0, Label.id.indexOf("Label"));
			var InputBox = document.getElementById(LabelId);
			var RegExBox = document.getElementById(LabelId + "RegEx");
			if (RegExBox) {
				var MyRegEx = eval(RegExBox.value);
			}
			var RequiredField = (LabelText.indexOf("*") > -1)?true:false;
			if (InputBox.nodeName.toLowerCase() == "select") {
				var InputData = InputBox[InputBox.selectedIndex].value;
			} else if (InputBox.type.toLowerCase() == "checkbox" || InputBox.type.toLowerCase() == "radio") {
				var InputData = InputBox.checked;
			} else {
				var InputData = InputBox.value;
			}

			var HasEntery = (InputData.length>0)?true:false;
			if (((HasEntery || RequiredField) && RegExBox && !MyRegEx.test(InputData)) || (!HasEntery && RequiredField)) {
				IsOk = false;
				Label.style.color = BadColor;
			} else {
				Label.style.color = OkColor;
				Query += LabelId + "=" + InputData + "&";
			}
		}
	}

	Query = Query.substr(0, Query.length - 1);
	if (IsOk) {
		try {
			SendFailed = false;
			SFSendRequest(SendScript, Query, false, false, "Success", "Failure", "GET");
		} catch (e) {
			Failure();
		}
	} else {
		var MyErrorHolder = document.getElementById(ErrorHolder);
		MyErrorHolder.innerHTML = BadInputMessage;
	}

}

function Success() {
	var MyErrorHolder = document.getElementById(ErrorHolder);
	var MySuccessHolder = document.getElementById(SuccessHolder);
	MyErrorHolder.innerHTML = "";
	MySuccessHolder.innerHTML = SuccessMessage;
}

function Failure(Returned) {
	//alert ("WhatBack:" + Returned.responseText);
	SendFailed = true;
	var MyErrorHolder = document.getElementById(ErrorHolder);
	MyErrorHolder.innerHTML = FailureMessage;
}


/*function SFSendRequest(Script, Value, Sync, ContentType, HandlerFunction, FailureFunction, Method) {

	if (!Sync) {
		Sync = false;
	}
	if (!Method) {
		Method = "GET";
	}
	if (!ContentType) {
		ContentType = "application/x-www-form-urlencoded";
	}

	if (Method == "GET") {
		Script += "?" + Value;
		Value = null;
	}
	var RanID = Math.floor(Math.random()* 9999999999);
	if (window.XMLHttpRequest) {
		var http = eval("http" + RanID + " = new XMLHttpRequest()");
	} else if (window.ActiveXObject) {
		var http = eval("http" + RanID + " = new ActiveXObject('Microsoft.XMLHTTP')");
	}

	if (http) {
		http.onreadystatechange = function () {
			if (http.readyState == 4) {
				if (http.status == 200) {
					if (HandlerFunction) {
						eval(HandlerFunction)(http);
					}
				} else {
					if (FailureFunction) {
						eval(FailureFunction)(http);
					}
				}
			}
		}
	} else {
		eval(FailureFunction)(http);
	}
	try {
		http.open(Method, Script, Sync);
		if (ContentType){
			http.setRequestHeader("Content-Type", ContentType);
		}
		http.send(Value);
	} catch(e) {
		if (FailureFunction) {
			eval(FailureFunction)(http);
		}
	}
}*/

// Remember to test out the new ajax function in other scripts in both IE and FF.

function SFSendRequest(Script, Value, Sync, ContentType, HandlerFunction, FailureFunction, Method, SessionName, Extra) {
	//alert ("Bout to post: " + Value);

	if (!Sync) {
		Sync = false;
	}
	if (!Method) {
		Method = "GET";
	}
	if (!ContentType) {
		ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
	}

	if (Method == "GET") {
		Script += "?" + Value;
		Value = null;
	}
	var RanID = Math.floor(Math.random()* 9999999999);
	if (!SessionName) {
		SessionName = "http" + RanID;
	}
	if (!this[SessionName]) {
		if (window.XMLHttpRequest) {
			var http = eval(SessionName + " = new XMLHttpRequest()");
		} else if (window.ActiveXObject) {
			var http = eval(SessionName + " = new ActiveXObject('Microsoft.XMLHTTP')");
		}

		if (http) {
			var PassToCheck = "'" + HandlerFunction + "','" + FailureFunction + "','" + SessionName + "'";
			if (Extra) {
				PassToCheck += "'" + Extra + "'";
			}
			this[SessionName + "Interval"] = setInterval("CheckSession(" + PassToCheck + ")", 100);
			/*http.onreadystatechange = function () {
				if (http.readyState == 4) {
					if (http.status == 200) {
						if (HandlerFunction) {
							eval(HandlerFunction)(http);
						}
						//alert("Ajax Function Says pre: " + (this["xmlhttpCheckForConf"] == http));
						//http.close();
						http = undefined;
						delete http;
						//alert("Ajax Function Says Post: " + (this["xmlhttpCheckForConf"] == http));
						//alert("Ajax Function Says1: " + typeof(this["xmlhttpCheckForConf"]));
						//alert("Ajax Function Says3: " + SessionName);
						//alert("Ajax Function Says4: " + this[SessionName]);
					} else {
						if (FailureFunction) {
							eval(FailureFunction)(http);
						}
						//http.close();
						http = undefined;
						delete http;
					}
				}
			}*/
		} else {
			eval(FailureFunction)(http);
		}
		try {
			http.open(Method, Script, Sync);
			if (ContentType){
				http.setRequestHeader("Content-Type", ContentType);
			}
			http.send(Value);
		} catch(e) {
			if (FailureFunction) {
				eval(FailureFunction)(http);
			}
		}
	}
}

function CheckSession(HandlerFunction, FailureFunction, SessionName, Extra) {
	this[SessionName];
	if (this[SessionName].readyState == 4) {
		if (this[SessionName].status == 200) {
			KillInterval(SessionName);
			if (HandlerFunction) {
				eval(HandlerFunction)(this[SessionName], Extra);
			}
			KillSession(SessionName);
		} else {
			KillInterval(SessionName);
			if (FailureFunction) {
				eval(FailureFunction)(this[SessionName], Extra);
			}
			KillSession(SessionName);
		}
	}
}

function KillSession(SessionName) {
	this[SessionName] = undefined;
	try {
		delete this[SessionName];
	} catch(e) {
	}
}

function KillInterval(SessionName) {
	clearInterval(this[SessionName + "Interval"]);
	this[SessionName + "Interval"] = undefined;
	try {
		delete this[SessionName + "Interval"];
	} catch(e) {
	}
}