var ContactForm = new Object({
	"Config":{
		"AjaxImage":{
			"Height":24,
			"Id":"cFormAjaxActionImage",
			"Uri":"/images/ajax.gif",
			"Width":24
		},
		"Captcha":{
			"CgiScript":"/cgi-bin/captcha.pl",
			"CookieKey":"cFormHash",
			"ElementId":"contactFormCaptchaImage",
			"Expire":300,
			"Height":40,
			"Size":6,
			"Width":180
		},
		"Form":{
			"Action":"/cgi-bin/ajax-contact.pl",
			"Name":"contactForm",
			"LabelWidth":120,
			"TextAreaHeight":200,
			"Xml":''
		},
		"Lightbox":{
			"Height":480,
			"Width":500
		},
		"RecipientTitles":{
			"adnieman":"A. D. Nieman",
			"cdavis":"C. Davis",
			"gerholdfrizzle":"R. S. Gerhold-Frizzle",
			"comments":"Questions &amp; Comments",
			"info":"General Information",
			"support":"Customer Support",
			"webmaster":"Webmaster"
		}
	},
	"Data":{
		"AjaxResponse":{},
		"ErrorCounter":0,
		"MaxErrorRetries":3
	},
	"CaptchaChanger": function(b){
		document.getElementById(b.ElementId).style.background='url("' + b.CgiScript + '?key=' + b.CookieKey + '&size=' + b.Size + '&height=' + b.Height + '&expire=' + b.Expire + '&rand=' + randomHexidecimal(16) + '") no-repeat';
	},
	"Create": function(recipient){
		/* Make a copy of the form XML data */
		var formXml = ContactForm.Config.Form.Xml;
		/* Process the &#44; codes into single quote characters */
		formXml = formXml.replace(/&#39;/g, "\'")
		/* Calculate form INPUT width based on  */
		var InputWidth = ContactForm.Config.Lightbox.Width-(30+ContactForm.Config.Form.LabelWidth);
		/* Calculate form BUTTON width based on 1/3 form INPUT width */
		var LightboxButtonWidth = Math.floor(InputWidth/3)-3;
		/* Write form into the Lightbox */
		eval("document.getElementById('lightbox').innerHTML = '" + LightboxRoundedCornerStyle + formXml + "';\n");
		/* Set CAPTCHA image */
		ContactForm.CaptchaChanger(ContactForm.Config.Captcha);
		/* Render Lightbox */
		lightboxNotification(true, ContactForm.Config.Lightbox.Width, ContactForm.Config.Lightbox.Height);
		/* Give focus to the Name field in the form */
		document.getElementById(ContactForm.Config.Form.Name).contactFormName.focus();
		/* Exit the function */
		return false;
	},
	"PostMessage": function(uri, form){
		var a = ajax();
		if(!a){alert("Your browser supports JavaScript but does not AJAX!\n\nPlease download a better browser."); return}
		a.onreadystatechange = function(){
			if(a.readyState == 1){try {a.send(form)} catch(err){}}
			else if(a.readyState == 4){
				if(a.status == 200){ContactForm.Data.AjaxResponse = Object(a)}
				ContactForm.StatusHandler();
				a = undefined;
			}
		}
		a.open('POST',uri,true);
	},
	"StatusHandler": function(){
		var ajax = new Object({});
		var errmsg = "An error has occurred and the message\ncould not be sent. Click 'CANCEL' to\nabort sending this message or 'OK' to\ncontinue trying.";
		if(ContactForm.Data.AjaxResponse.status !== 200){
			if(ContactForm.Data.ErrorCounter < ContactForm.Data.MaxErrorRetries){
				if(confirm(errmsg)){ContactForm.Data.ErrorCounter++; ContactForm.SubmitMessage()}
				else {ContactForm.Data.ErrorCounter = 0; ContactForm.Data.AjaxResponse = Object({}); lightboxNotification(false,0,0)}
				return;
			}
			ContactForm.Data.ErrorCounter = 0;
			alert("An unrecoverable error has occurred and\nthe message is being aborted. Sorry for\nthe inconvenience.");
			ContactForm.Data.AjaxResponse = Object({});
			lightboxNotification(false,0,0);
			return;
		}
		if(ContactForm.Data.AjaxResponse.responseText){eval('ajax = Object(' + ContactForm.Data.AjaxResponse.responseText + ')')}
		if(!ajax.status){
			alert("An unrecoverable error has occurred and\nthe message is being aborted. Sorry for\nthe inconvenience.");
			ContactForm.Data.AjaxResponse = Object({});
			lightboxNotification(false,0,0);
			return;
		}
		if(ajax.status !== 200){
			if(ajax.status == 401){
				alert("CAPTCHA Error\n\n"+ajax.message+"\n\nPlease try again.");
				ContactForm.CaptchaChanger(ContactForm.Config.Captcha);
				document.getElementById('contactFormCaptcha').value='';
				document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='hidden';
				return false;
			}
			if(ajax.status == 402){
				alert("The following fields are blank:\n\n"+ajax.message+"\n\nPlease try again.");
				ContactForm.CaptchaChanger(ContactForm.Config.Captcha);
				document.getElementById('contactFormCaptcha').value='';
				document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='hidden';
				return false;
			}
			if(ajax.status == 403){
				alert("Did you forget to uncheck the checkbox\nto verify that you are a real person?\n\nPlease try again.");
				ContactForm.CaptchaChanger(ContactForm.Config.Captcha);
				document.getElementById('contactFormCaptcha').value='';
				document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='hidden';
				return false;
			}
			if(ContactForm.Data.ErrorCounter < ContactForm.Data.MaxErrorRetries){
				ContactForm.Data.AjaxResponse = Object({});
				if(confirm(errmsg)){ContactForm.Data.ErrorCounter++; ContactForm.SubmitMessage()}
				else {ContactForm.Data.ErrorCounter = 0; lightboxNotification(false,0,0)}
				return;
			}
			ContactForm.Data.AjaxResponse = Object({});
			ContactForm.Data.ErrorCounter = 0;
			alert("An unrecoverable error has occurred and\nthe message is being aborted. Sorry for\nthe inconvenience.");
			lightboxNotification(false,0,0);
			return;
		}
		document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='hidden';
		ContactForm.Data.AjaxResponse = Object({});
		lightboxNotification(false,0,0);
		alert('Your message has been sent.');
		return;
	},
	"SubmitMessage": function(){
		var form = document.getElementById(ContactForm.Config.Form.Name);
		var items = new Array('contactFormRecipient','contactFormName', 'contactFormEmail', 'contactFormSubject', 'contactFormMessage', 'contactFormCaptcha');
		var formData = new Array();
		var msg = "";
		var ef = false;
		document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='visible';

		// Name
		if(form.contactFormName.value.match(/^\s*$/)){msg += "\n    - Name can not be blank."}
		else if(form.contactFormName.value.match(/(\t|\`|\"|\')/)){msg += "\n    - Name contains illegal characters."}
		if(ef == false && msg){form.contactFormName.focus(); ef = true}
		// Email address
		if(form.contactFormEmail.value.match(/^\s*$/) || !form.contactFormEmail.value.match(/^\S+\@\w.+?\.\w{2,5}$/)){msg += "\n    - Email address is not valid."}
		else if(form.contactFormEmail.value.match(/(\s|\`|\!|#|\$|\%|\^|\&|\*|\(|\)|\=|\[|\]|\\|\;|\'|\,|\/|\{|\}|\||\:|\"|\<|\>|\?)/)){msg += "\n    - Email address contains illegal characters."}
		if(ef == false && msg){form.contactFormEmail.focus(); ef = true}
		// Subject
		if(form.contactFormSubject.value.match(/^\s*$/)){msg += "\n    - Subject can not be blank."}
		if(ef == false && msg){form.contactFormSubject.focus(); ef = true}
		// Message
		if(form.contactFormMessage.value.match(/^\s*$/)){msg += "\n    - Message can not be blank."}
		else if(form.contactFormMessage.value.length > 1500){msg += "\n    - Message is too long. 1500 Characters or less."}
		if(ef == false && msg){form.contactFormMessage.focus(); ef = true}
		// CAPTCHA
		if(!form.contactFormCaptcha.value.match(/^\w+$/i)){msg += "\n    - CAPTCHA can not be blank."}
		if(ef == false && msg){form.contactFormCaptcha.focus(); ef = true}

		if(msg){alert("The following errors must be corrected:\n" + msg); document.getElementById(ContactForm.Config.AjaxImage.Id).style.visibility='hidden'; return false}

		if(form.contactFormPersonCheck.checked){formData.push(escape('contactFormPersonCheck') + "=" + escape('NOT_REAL'))}
		for(var c = 0; c < items.length; c++){formData.push(escape(items[c]) + "=" + escape(document.getElementById(items[c]).value))}
		ContactForm.PostMessage(ContactForm.Config.Form.Action+'?cmnd=send&rand='+randomHexidecimal(16), formData.join("&"));
		return false;
	}
});


