/****************************************************************************************
AJAX
****************************************************************************************/

var timer = null;
var delay = 1000; //milliseconds

/***************************************************************************************/

function getxmlhttp()
{
	var xmlHttp;
	try {
		//firefox, opera 8.0+, safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//ie
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support this site");
				return false;
			}
		}
	}
	return xmlHttp;
}

/***************************************************************************************/

function processAjax(serverPage, obj, meth, arg)
{
	//clear timer if set
	window.clearTimeout(timer);
	
	//get the xmlhttp object
	var xmlhttp = getxmlhttp();
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById(obj).innerHTML = xmlhttp.responseText;
		}
	};
	
	//see what the form method is
	if (meth == 'get') {
		xmlhttp.open("GET", serverPage, true);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset-UTF-8");
	}
	
	//process
	xmlhttp.send(arg);
}

/***************************************************************************************/

function showProgress(obj)
{
	if (document.getElementById(obj).style.display == 'block') {
		document.getElementById(obj).style.display = 'none';
	} else {
		document.getElementById(obj).style.display = 'block';
	}
}

/***************************************************************************************/

function setProgress(obj, im, msg)
{
	var img = '';
	if (im != '') {
		img = "<img src=\"" + im + "\" border=\"0\" alt=\"" + msg + "\" />";
	}
	document.getElementById(obj).innerHTML = "<div class='progress'>" + img + " " + msg + "</div>";
}

/***************************************************************************************/

function getDisplay(obj) {
	return document.getElementById(obj).style.display;
}
function setDisplay(obj, display) {
	document.getElementById(obj).style.display = display;
}

/***************************************************************************************/

function getContent(obj, url) {
	//first we change the navigation
	var links = document.getElementById('topNav').getElementsByTagName('a');
	for (var l = 0; l < links.length; l++) {
		if (links[l].name == url) {
			links[l].className = 'on';
		} else {
			links[l].className = '';
		}
	}
	//second we display the progress
	setProgress(obj, "images/progress.gif", "Loading...");
	//third we get the content
	var serverPage = "content/" + url + ".php?cachekiller=" + new Date().getTime();
	processAjax(serverPage, obj, 'get');
}

/***************************************************************************************/

function trim(inputString)
{
	//removes leading and trailing spaces from the passed string.
	//also replaces multiple blank spaces with one space.
	//if a non-string is passed in, just return the input.
	if (typeof(inputString) == "string")
	{
		var retValue = inputString;
		var ch = retValue.substring(0, 1);
		//check for spaces at beginning of string
		while (ch == " ")
		{
			retValue = retValue.substring(1, retValue.length);
			ch = retValue.substring(0, 1);
		}
		ch = retValue.substring((retValue.length - 1), retValue.length);
		//check for spaces at end of string
		while (ch == " ")
		{
			retValue = retValue.substring(0, (retValue.length - 1));
			ch = retValue.substring((retValue.length - 1), retValue.length);
		}
		//check for multiple spaces within string
		while (retValue.indexOf("  ") != -1)
		{
			retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
		}
		//return the trimmed string
		return retValue;
	}
	else
	{
		return inputString;
	}
}

/***************************************************************************************/

function validateForm(theForm, theValue, theName, theTitle)
{
	var cont = true;
	
	//set up exceptions...
	//alert(theForm.className + " : " + theName);
	if (theForm.className == 'userForm' && theName == 'phone' || theName == '_subject' || theName == 'payment_invoice_address') {
		//do nothing (not required)
	} else if (trim(theValue) == "") {
		alert("You must fill in the " + theTitle + " field!");
		theForm.elements[theName].focus();
		cont = false;
	}
	
	return cont;
}

/***************************************************************************************/

var aok = true;

function getFormValues(theForm, valFunc)
{
	aok = true;
	var str = '';
	var checkStr;
	var val;
	//alert('theForm = ' + theForm + ' with the name: ' + theForm.name + ' and this many elements: ' + theForm.elements.length);
	//run thru form's objects
	for (var i = 0; i < theForm.elements.length; i++)
	{
		if (valFunc)
		{
			if (aok == true)
			{
				val = valFunc(theForm, theForm.elements[i].value, theForm.elements[i].name, theForm.elements[i].title);
				if (val == false) {
					aok = false;
				}
			}
		}
		//see if our element is a checkbox
		if (theForm.elements[i].type == 'checkbox') {
			checkStr = '';
			//for (var c = 0; c < theForm.elements[i].length; c++) {
			//	if (theForm.elements[i][c].checked == true) {
			//		checkStr += escape(theForm.elements[i][c].value) + ',';
			//	}
			//}
			//alert(theForm.elements[i].name + ' is a checkbox');
			if (theForm.elements[i].checked == true) {
				checkStr += theForm.elements[i].value + ',';
			//	alert(theForm.elements[i].name + ' is checked as ' + theForm.elements[i].value);
			}
			checkStr = checkStr.substring(0, checkStr.length-1);
			str += theForm.elements[i].name + "=" + escape(checkStr) + "&";
		} else {
			//populate the str variable
			str += theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";
		}
	}
	//alert(str);
	return str;
}

/***************************************************************************************/

function submitForm(theForm, serverPage, objID, valFunc)
{
	//alert('we are in submitForm()');	
	var ourForm = document.getElementById(theForm);
	var numElements = ourForm.elements.length;
	
	var arg = getFormValues(ourForm, valFunc);
	//alert(arg);
	
	//if everything validated
	if (aok == true)
	{
		showProgress(objID);
		//alert('we are aok, so processAjax()');
		processAjax(serverPage, objID, 'post', arg);
	}
}

/***************************************************************************************/

function popUpImg(obj, img, imgW, imgH) {
	var popUpLoc = document.getElementById(obj);
	//alert(obj + ', ' + popUpLoc);
	//clear the popup window
	popUpLoc.innerHTML = '';
	//screen width/height
	var screenW;
	var screenH;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		screenW = window.innerWidth,
		screenH = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
	 && typeof document.documentElement.clientWidth !=
	 'undefined' && document.documentElement.clientWidth != 0)
	{
	    screenW = document.documentElement.clientWidth,
		screenH = document.documentElement.clientHeight
	}
	// older versions of IE
	else
	{
	    screenW = document.getElementsByTagName('body')[0].clientWidth,
		screenH = document.getElementsByTagName('body')[0].clientHeight
	}
	//alert('screen width = ' + screenW + ', screen height = ' + screenH);
	//user scroll positions
	var scrollX = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
  	var scrollY = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
	//alert('scrollX='+scrollX+', scrollY='+scrollY);
	//position the popup window
	var midX = Math.floor(screenW/2) + scrollX;
	var midY = Math.floor(screenH/2) + scrollY;
	var imgMidX = Math.floor(imgW/2);
	var imgMidY = Math.floor(imgH/2);
	var posLeft = midX - imgMidX;
	var posTop = midY - imgMidY;
	//alert('midX='+midX+', midY='+midY+', imgMidX='+imgMidX+', imgMidY='+imgMidY+', posLeft='+posLeft+', posTop='+posTop);
	//set popup window properties
	popUpLoc.style.top = posTop + 'px';
	popUpLoc.style.left = posLeft + 'px';
	popUpLoc.style.width = imgW + 'px';
	//popUpLoc.style.height = (imgH + 10) + 'px';
	popUpLoc.innerHTML = '<div align="right"><a href="#" onclick="hide(\''+obj+'\'); return false;">[close]</a></div><a href="#" onclick="hide(\''+obj+'\'); return false;"><img src="'+img+'" width="'+imgW+'" height="'+imgH+'" border="0" alt="click to close" title="click to close" /></a>';
	//display the popup window
	popUpLoc.style.display = 'block';
}
function hide(obj) {
	document.getElementById(obj).style.display = 'none';
}

/***************************************************************************************/
