
var sentTimer;
var pos; // variable for posting information

// XML Connection functions
function loadXMLPosDoc(url,posData) {
    // branch for native XMLHttpRequest object
	try {
	    if (window.XMLHttpRequest) {
			pos = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			pos = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return false;
		}
	} catch (e) {
		alert("There was an error communicating with the server. Please check back in a few minutes");
	}
	
	pos.onreadystatechange = processPosChange;
	pos.open("POST", url, false);
	pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	pos.send(posData);
	
	// Fired just in case the object fires before the function has a chance to be assigned.
	//processPosChange();
}
function grabPosXML (tagName) {
	return pos.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
}
function processPosChange() {
    // page loaded "complete"
    if (pos.readyState == 4) {
        // page is "OK"
        if (pos.status == 200) {
			if ( grabPosXML("status") == 'NOTOK' ) { 
				alert('There were problems Sending Email. Please check back in a couple minutes');
			} else {
				evalSubmitReturn();
			}
		}
	}
}

// Contact form submission functions
function submitContactForm() {
	// First, Validate our form
	if(!validateForm()) return false;
	
	dcsMultiTrack("DCS.dcsuri", "/contactUsNow.asp", "WT.ti", "Contact%20Form%20Submit","DCS.dcsref", "/contactUs.asp");
	
	// Get the requisite data
	if(window.location.href.indexOf("dev.elliance.com") > -1) {
		var page		=	"http://elliance.dev.elliance.com/contactUs.asp?contact=true&xml=true";	
	} else {
		var page		=	"http://www.elliance.com/contactUs.asp?contact=true&xml=true";	
	}
	var form_data	=	getFormData();
	
	// Rearrange our page as necessary.
	hideForm();
	hideMessage();
	showLoadbar();
	
	// Set Timeout
	sentTimer = setTimeout("evalSubmitReturn()",6000);
	// Send the form data
	loadXMLPosDoc(page,form_data);
}

function evalSubmitReturn() {
	clearTimeout(sentTimer);

	clearForm();
	setMessage();
	showMessage();
	showForm();
	hideLoadbar();
	
	var ret = google_handle_conversion();
	ret	+=	"<img src='" + cc_imageUrl + "' />";
	var my_div			=	document.getElementById("conversionDiv");
	my_div.innerHTML	=	ret;
}

// Form Management Functions
function showForm() {
	var form	=	document.getElementById('contactForm');
	var inputs	=	form.getElementsByTagName('input');
	var tAreas	=	form.getElementsByTagName('textarea');
	
	for (i=0; i < inputs.length; i++) {
		inputs[i].disabled = "";
		inputs[i].style["backgroundcolor"] = "#FFFFFF";
	}
	for (j=0; j < tAreas.length; j++ ) {
		tAreas[j].disabled = "";
		tAreas[j].style["backgroundcolor"] = "#FFFFFF";
	}
	// Garbage Collection
	form = null;
	inputs = null;
	tAreas = null;
}
function hideForm() {
	var form	=	document.getElementById('contactForm');
	form.disabled = "disabled";
	form.style.display = "none";
	/*
	var inputs	=	form.getElementsByTagName('input');
	var tAreas	=	form.getElementsByTagName('textarea');
	
	for (i=0; i < inputs.length; i++) {
		inputs[i].disabled = "disabled";
		inputs[i].style["background-color"] = "#666666";
	}
	for (j=0; j < tAreas.length; j++ ) {
		tAreas[j].disabled = "disabled";
		tAreas[j].style["background-color"] = "#666666";
	}*/
	
	// Garbage Collection
	form = null;
	inputs = null;
	tAreas = null;
}
function clearForm() {
	var form	=	document.getElementById('contactForm');
	var inputs	=	form.getElementsByTagName('input');
	var tAreas	=	form.getElementsByTagName('textarea');
	
	for (i=0; i < inputs.length; i++) {
		switch(inputs[i].type) {
			case "text":
				inputs[i].value = "";
				break;
			case "checkbox":
				inputs[i].checked = false;
				break;
		}
	}
	for (j=0; j < tAreas.length; j++ ) {
		tAreas[j].value = "";
	}
	
	// Garbage Collection
	form = null;
	inputs = null;
	tAreas = null;
}
function validateForm() {
	var reg1	=	/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2	=	/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	var regws	=	/^[\s]+$/;

	var fname		=	document.getElementById('0e5f1df873a9660542c29954be4581d3');
	var lname		=	document.getElementById('0f9943d8588c587a5a6b94d1c5f9ec1d');
	var email		=	document.getElementById('9728554c10cea3449ab1538a31a666b9');
	
	// Test for valid name
	if (fname.value=='' || regws.test(fname.value) || lname.value=='' || regws.test(lname.value)) {	// if syntax is valid
		alert("Please provide your full name, so we can properly address you.");
		return false;
	}
	
	// Test for valid email
	if (reg1.test(email.value) || !reg2.test(email.value)) {	// if syntax is valid
		alert("Please enter a valid contact email address.");
		return false;
	}
	
	// Garbage Collection
	reg1 = null;
	reg2 = null;
	regws = null;
	fname = null;
	lname = null;
	email = null;
	
	return true;
}
function getFormData () {
	var title			=	document.getElementById('8aeeeecb27b6ed0225a18f0ed5fe4051');
	var fname			=	document.getElementById('0e5f1df873a9660542c29954be4581d3');
	var lname			=	document.getElementById('0f9943d8588c587a5a6b94d1c5f9ec1d');
	var company			=	document.getElementById('54b98bb8214c45f7ae715eec8994b782');
	var email			=	document.getElementById('9728554c10cea3449ab1538a31a666b9');
	var phone			=	document.getElementById('66291291a51fba352945d73957776af6');
	var comments		=	document.getElementById('e634e2586bd39835304213823ac99010');
	var interest		=	document.getElementById('59c308d316922d949ba93f661cbfb54c');
	
	// Compose Query String
	var val_str	=	"8aeeeecb27b6ed0225a18f0ed5fe4051="			+	prepString(title.value) +
					"&0e5f1df873a9660542c29954be4581d3="			+	prepString(fname.value) +
					"&0f9943d8588c587a5a6b94d1c5f9ec1d="			+	prepString(lname.value) +
					"&54b98bb8214c45f7ae715eec8994b782="			+	prepString(company.value) +
					"&9728554c10cea3449ab1538a31a666b9="			+	prepString(email.value) +
					"&66291291a51fba352945d73957776af6="			+	prepString(phone.value) +
					"&e634e2586bd39835304213823ac99010="		+	prepString(comments.value) +
					"&59c308d316922d949ba93f661cbfb54c="		+	prepString(interest.value);
	
	// Garbage Collection
	title			=	null;
	fname			=	null;
	lname			=	null;
	company			=	null;
	email			=	null;
	phone			=	null;
	comments		=	null;
	interest		=	null;
	
	// Return the value.
	return val_str;
}

// Loadbar Management Functions
function showLoadbar () {
	var loader = document.getElementById('loadbar');
	loader.style["display"] = 'block';
	loader = null;
}
function hideLoadbar () {
	var loader = document.getElementById('loadbar');
	loader.style["display"] = 'none';
	loader = null;
}

// Return Message Management Functions
function showMessage() {
	var message = document.getElementById('contactsuccess');
	message.style["display"] = 'block';
	message = null;
}
function hideMessage() {
	var message = document.getElementById('contactsuccess');
	message.style["display"] = 'none';
	message = null;
}
function setMessage() {
	var message = document.getElementById('contactsuccess');
	message.innerHTML = grabPosXML("confirmation");
	message = null;
}

// Utility Functions
function prepString(str) {
	str = String(str).replace(/&/g,"**am**");
	str = str.replace(/=/g,"**eq**");
	str = str.replace(/\+/g,"**pl**");
	str = str.replace(/ /g,"**sp**");
	return str;
}


function ajaxContact() {
	var form = document.getElementById('contactForm');
	addEvent(form, 'submit', submitContactForm, false);
	form.onsubmit = function() { return false; }
	form = null;
}
addEvent(window, 'load', ajaxContact, false);

/* ---------------------------------------------------------------------------- */
// Google/Overture tracking info

// Google ####################
var google_conversion_id		=	1067574062;
var google_conversion_language	=	"en_US";
var google_conversion_format	=	"1";
var google_conversion_color		=	"FFFFFF";
if (1) {
	var google_conversion_value = 1;
}
var google_conversion_label = "Lead";

// Overture ####################
var cc_tagVersion	=	"1.0";
var cc_accountID	=	"9084419140";
var cc_marketID		=	"0";
var cc_protocol		=	"http";
var cc_subdomain	=	"convctr";
if(location.protocol == "https:") {
	cc_protocol		=	"https";
	cc_subdomain	=	"convctrs";
}
var cc_queryStr = "?" + "ver=" + cc_tagVersion + "&aID=" + cc_accountID + "&mkt=" + cc_marketID +"&ref=" + escape(document.referrer);
var cc_imageUrl = cc_protocol + "://" + cc_subdomain + ".overture.com/images/cc/cc.gif" + cc_queryStr;

function overture_handle_conversion() {
	return "<img src='" + cc_imageUrl + "' />";
}