// JavaScript Document

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

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

if (typeof(BadInputMessage)=="undefined"){
	BadInputMessage = "Please fill out all fields"
}

if (typeof(SuccessMessage)=="undefined"){
	SuccessMessage = "<b>Your message has been sent. Thank you for your input!</b>";
}

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

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

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

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



ShowHideLoadingImage(false);

QueryOut = false;
Extra = new Object();

function SubmitForm() {
	if (!QueryOut) {
		var FormLabels = document.getElementsByTagName("span");
		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 LabelText = Label.innerHTML;
                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) {
			QueryOut = true;
			try {
				ShowHideLoadingImage(true);
				SendFailed = false;
				SFSendRequest(SendScript, Query, true, false, "Success", "Failure", "POST");
			} catch (e) {
				Failure();
			}
		} else {
			var MyErrorHolder = document.getElementById(ErrorHolder);
			MyErrorHolder.innerHTML = BadInputMessage;
		}
	}
}

function Success(returned) {
	var MyErrorHolder = document.getElementById(ErrorHolder);
	var MySuccessHolder = document.getElementById(SuccessHolder);
	var RegExErrorTest = /^Error/
	var RegExSuccessTest = /^Success/
	var Response = returned.responseText;
	QueryOut = false;
	ShowHideLoadingImage(false);
    
	if (RegExErrorTest.test(Response)) {
		if (MyErrorHolder) {
			MyErrorHolder.innerHTML = Response;
		}
	} else if (RegExSuccessTest.test(Response)) {
		if (MyErrorHolder) {
			MyErrorHolder.innerHTML = "";
		}
		if (MySuccessHolder) {
			MySuccessHolder.innerHTML = Response;
		}
	} else {
		if (MyErrorHolder) {
			MyErrorHolder.innerHTML = "";
		}
		if (MySuccessHolder) {
			MySuccessHolder.innerHTML = SuccessMessage;
		}
	}
}

function Failure(Returned) {
	QueryOut = false;
	SendFailed = true;
	ShowHideLoadingImage(false);
	var MyErrorHolder = document.getElementById(ErrorHolder);
	MyErrorHolder.innerHTML = FailureMessage;
}

function SFSendRequest(Script, Value, Sync, ContentType, HandlerFunction, FailureFunction, Method, SessionName, Extra) {
	
	if (!Sync) {
		Sync = false;
	}
	if (!Method) {
		Method = "GET";
	}
	
	if (Value.length > 255) {
		Method = "POST";
	}
	
	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);

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

function ShowHideLoadingImage(Show) {
	if (typeof(LoadingImg)=="undefined"){
		if (document.getElementById("LoadingGraphic")) {
			LoadingImg = "LoadingGraphic";
		} else {
			LoadingImg = false;
		}
	}
	var CurLoadingImg = document.getElementById(LoadingImg);
	if (CurLoadingImg) {
		
		if (Show) {
			CurLoadingImg.style.display = "";
		} else {
			CurLoadingImg.style.display = "none";
		}
	} else {
		setTimeout("ShowHideLoadingImage('" + Show + "')", 500);
	}
}

