var showCloseButton = true;
// Function to create the AJAX object depending on your browser
function createXMLHttpRequest()
{
	if (window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}
}

// Function that will pass data to the AJAX script upon interacting with the application
function AJAXupdate(loadScript, nameArray, valueArray)
{
	nameArrayIn = nameArray || new Array();
	valueArrayIn = valueArray || new Array();
	
	//alert('AJAX Update Request Sent!');
	createXMLHttpRequest();
	parameters = "";
	globalVarsString = "";
	for(x in nameArrayIn)
		parameters += nameArrayIn[x] + "=" + valueArrayIn[x] + "&";
	workingPostPage = loadScript + "?" + parameters;
	xmlHttp.onreadystatechange = AJAXprocess;
	xmlHttp.open("GET",workingPostPage,true);
	xmlHttp.send(null);
}

function hideOverlay()
{
	document.getElementById("OverlayPageSpan").style.visibility = "hidden";
	document.getElementById("OverlayPageBackgroundSpan").style.visibility = "hidden";
	document.getElementById("OverlayPageTableClose").style.visibility = "hidden";
}
// Function that will analyze the XML returned from the AJAX call and call the appropriate update functions
function AJAXprocess()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			//alert(xmlHttp.responseText);
			document.getElementById("OverlayPageBackgroundSpan").style.visibility = "visible";
			document.getElementById("OverlayPageSpan").style.visibility = "visible";
			if(showCloseButton)
				document.getElementById("OverlayPageTableClose").style.visibility = "visible";
			showCloseButton = true;
			document.getElementById("OverlayPageTableContent").innerHTML = xmlHttp.responseText;
		}
		else if (xmlHttp.status == 404)
		{
			alert("404 : Page Not Found");
		}
		else
		{
			alert("Unknown Error: " + xmlHttp.status);
		}
	}
}

function verifyAndPostNewsletterRequest()
{
	names = new Array("Name","Business Name","Phone","Email Address");
	values = new Array();
	values[0] = document.getElementById("Name").value;
	values[1] = document.getElementById("BusinessName").value;
	values[2] = document.getElementById("Phone").value;
	values[3] = document.getElementById("EmailAddress").value;
	
	errorText = "The following inputs are required:\n"
	error = false;
	for(i in values)
	{
		if(values[i] == "")
		{
			errorText += "\t" + names[i] + "\n";
			error = true;
		}
		
	}
	if(error)
	{
		alert(errorText);
		return;
	}
		
	rExp = /[\t\n\r\f\v]/;
	emailAddress = values[3];
	result = emailAddress.search(rExp);
	if(result == -1)
	{
		atCount = 0;
		for(i = 0; i < emailAddress.length; i++)
		{
			temp = emailAddress.charAt(i);
			if(temp == "@")
				atCount++;
		}
		if(atCount == 0)
		{
			alert("You have entered an improperly formatted e-mail address! There is no \"@\" in your input.");
			return;	
		}
		if(atCount > 1)
		{
			alert("You have entered an improperly formatted e-mail address! There is more than one \"@\" in your input.");
			return;	
		}
		rExp = /^.+@.+[.].+$/;
		result = emailAddress.search(rExp);
		if(result == -1)
		{
			alert("You have entered an improperly formatted e-mail address!");	
			return;
		}
	}
	else
	{
		alert("You have entered an improperly formatted e-mail address! Please remove any whitespace from it before continuing.");
		return;	
	}
	params = new Array("Name","BusinessName","Address1","Address2","City","State","Zip","Phone","Fax","EmailAddress");
	values = new Array();
	for(x in params)
		values[x] = document.getElementById(params[x]).value;
	AJAXupdate('php/postNewsletter.php',params,values);
}


//************************************************************************************************************************
//code to move the spans centered on the page
var spanValues = new Array();
var ContentWidth = 0;

// Function to call other functions upon loading the page
function loadPage()
{
	MM_preloadImages('img/mainPage/mainPage_r2_c10_f2.jpg','img/mainPage/mainPage_r2_c2_f2.jpg','img/mainPage/mainPage_r2_c4_f2.jpg','img/mainPage/mainPage_r2_c6_f2.jpg','img/mainPage/mainPage_r2_c8_f2.jpg');
	storeWidths(new Array(0, 1, 2, 3), 800);
	if(!document.all)
		document.styleSheets[0].cssRules[1].style.visibility = "visible";
	else
		document.styleSheets[0].rules[1].style.visibility = "visible";
}

function storeWidths(idArray, contentWidth)
{
	
	//first store the values given by the html file
	for(x in idArray)
	{
		//alert(document.styleSheets[0].cssRules[x].style.left);
		if(!document.all)
			var tempString = document.styleSheets[0].cssRules[x].style.left;
		else
			var tempString = document.styleSheets[0].rules[x].style.left;
		var tempArray = tempString.split("p");
		spanValues[[x]] = parseInt(tempArray[0]);
	}
	ContentWidth = contentWidth;
	//then call the function to move the objects
	changePieces();
}

function changePieces()
{
	//for now assume that the content all fits on the page - this will need worked on
		
	//calculate the necessary width
	screenTransform = (document.body.clientWidth - ContentWidth)/2;
	if(screenTransform < 0)
		screenTransform = 0;
	//change all of the span values passed in
	for(x in spanValues)
	{
		//alert(spanValues[x]);
		var screenMoveVal = screenTransform + spanValues[x];
		if(!document.all)
			document.styleSheets[0].cssRules[x].style.left = screenMoveVal+"px";
		else
			document.styleSheets[0].rules[x].style.left = screenMoveVal+"px";
	}

}
function onloadSurvey()
{
	createXMLHttpRequest();
	workingPostPage = "php/survey.php";
	showCloseButton = false;
	xmlHttp.onreadystatechange = AJAXprocess;
	xmlHttp.open("GET",workingPostPage,true);
	xmlHttp.send(null);
}

function onloadNewsletter()
{
	createXMLHttpRequest();
	workingPostPage = "php/getNewsletter.php";
	showCloseButton = false;
	xmlHttp.onreadystatechange = AJAXprocess;
	xmlHttp.open("GET",workingPostPage,true);
	xmlHttp.send(null);
}

function postSurvey()
{
	createXMLHttpRequest();
	var i = 0;
	var documentObject;
	var documentObjectId;
	workingPostPage = "php/survey-post.php?";
	while(true)
	{
		documentObjectId = "SurveyQuestion-"+i;
		if(documentObject = document.getElementById(documentObjectId))
		{
			workingPostPage += documentObjectId + "=" + documentObject.value + "&";
			i++;
		}
		else
			break;
	}
	xmlHttp.onreadystatechange = AJAXprocess;
	xmlHttp.open("GET",workingPostPage,true);
	xmlHttp.send(null);
}