window.onload = init;

function init() {
	getWidth();
	//alert('loaded');
	var ajax = getXMLHttpRequestObject();
	if (ajax) {
		if (document.getElementById('formmodulearea')) {
			document.getElementById('form_module').onsubmit = function() {
				ajax.open('post', '?page_id=43');
				ajax.onreadystatechange = function() {
					//alert('handle');
					handleResponse(ajax);
				}
				var fields = ['firstname','lastname','city','state','zip','email','subject','fromemail'];
				for (var i=0; i < fields.length; i++) {
					fields[i] = fields[i] + "=" + encodeURIComponent(document.getElementById(fields[i]).value);
				}
				
				var values = fields.join('&');
				
				// start custom
				
				//alert(document.getElementById('cb1').status);
				var optin = "";
				if (document.getElementById('cb1').checked) optin = document.getElementById('cb1').value + ", ";
				if (document.getElementById('cb2').checked) optin += document.getElementById('cb2').value;
				values += "&optin=" + encodeURIComponent(optin);
				
				// end custom
				ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				ajax.send(values);
				return false;
			}
		}
	}
	//initLightbox();
}

function handleResponse(ajax) {
				
	if (ajax.readyState == 4) {
		if ((ajax.status == 200) || (ajax.status == 304)) {
			var results = document.getElementById('formmodulearea');
			var data = ajax.responseText;
			results.innerHTML = data;
			hideLoader();
			//results.style.display = 'block';
		} else {
			document.getElementById('form_module').submit();
		}
	}
}

function showLoader() {
	document.getElementById('loadingarea').style.display = "block";
}

function hideLoader() {
	document.getElementById('loadingarea').style.display = "none";
}

function highlightOn(element) {
	document.getElementById(element + "_label").style.color = "#000000";
	document.getElementById(element + "_label").style.fontWeight = "bold";
}

function highlightOff(element) {
	document.getElementById(element + "_label").style.color = "#3a6e8f";
	document.getElementById(element + "_label").style.fontWeight = "normal";
}

function doSubmit() {
	document.getElementById('formmodulearea').innerHTML = "<p style=\"text-align: center;\">Thank You!<br />Your information has been submitted.<\/p>";
}
