
function ValidateForm(oForm)
{
	var form = document.getElementById(oForm);
	var err = "";
	
	for(i=0; i<form.elements.length; i++)
	{
		if(form.elements[i].getAttribute("required") == "true")
		{
			if(form.elements[i].value == "")
			{
				err += form.elements[i].getAttribute("info") + "\n";
			}
			
			if(form.elements[i].name.match("email") && form.elements[i].value != "")
			{
				if(!eCheck(form.elements[i].value))
				{
					err += "Email address is not valid\n";
				}
			}
			
			if(form.elements[i].getAttribute("type") == "checkbox")
			{
				if(!form.elements[i].checked)
				{
					err += form.elements[i].getAttribute("info") + "\n";
				}
			}
		}
	}
	
	return err;
}


function CollectInfo(oForm)
{
	var form = document.getElementById(oForm);
	var err = "";
	var array = new Array(form.elements.length);
	
	for(i=0; i<form.elements.length; i++)
	{
		if(form.elements[i].getAttribute("type") == "checkbox")
		{
			if(form.elements[i].checked)
			{
				array[i] = form.elements[i].name + "=" + form.elements[i].value;
			}
		}
		else
		{
			if(form.elements[i].value != "")
			{
				array[i] = form.elements[i].name + "=" + form.elements[i].value;
			}
				
			if(form.elements[i].name.match("email"))
			{
				if(!eCheck(form.elements[i].value))
				{
					array[i] = form.elements[i].name + "=" + form.elements[i].value;
				}
			}
		}
	}
	
	return array;
}


function LockForm(oForm)
{
	var form = document.getElementById(oForm);
	
	for(i=0; i<form.elements.length; i++)
	{
		form.elements[i].disabled = true;
	}
}

function UnLockForm(oForm)
{
	var form = document.getElementById(oForm);
	
	for(i=0; i<form.elements.length; i++)
	{
		form.elements[i].disabled = false;
	}
}


function EmailFriend(propref, event)
{
	var error = ValidateForm('emailafriend');
	
	if(error == "")
	{
		LockForm('emailafriend');
		var array = CollectInfo('emailafriend');		
		var url= "/includes/emailafriend.php?email=true&propref=" + propref;
			
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		if(xmlHttp.responseText == "true")
		{
			customToolTip(event, "Thank you, your email has been sent")
		}
		else
		{
			UnLockForm('emailafriend');
		}
	}
	else
	{
		var error2 = error.replace(/end/g, "");
		alert("The following errors occured:\n\n" + error2);
	}
}

function EmailPref(event, uns)
{
	var eve;
	if(document.all)
	{
		eve = window.event;
	}
	else
	{
		eve = event;
	}
	
	var error = ValidateForm(uns);
	
	if(error == "")
	{
		LockForm(uns);
		var array = CollectInfo(uns);		
		var url= "/includes/request.php?form=" + uns;
			
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		if(xmlHttp.responseText == "true")
		{
			alert("Thank you, your request settings have been saved.");
		}
		else
		{
			UnLockForm(uns);
		}
	}
	else
	{
		var error2 = error.replace(/end/g, "<br />");
		customToolTip(event, "The following information is missing:<br /><br />" + error2, 200, -250);
	}
}

function Unsubscribe(uns)
{
	var error = ValidateForm(uns);
	
	if(error == "")
	{
		LockForm(uns);
		var array = CollectInfo(uns);		
		var url= "/includes/request.php?form=" + uns;
			
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		if(xmlHttp.responseText == "true")
		{
			alert("Thank you, you will be removed from our email list.");
		}
		else
		{
			UnLockForm(uns);
		}
	}
	else
	{
		var error2 = error.replace(/end/g, "");
		alert("The following errors occured:\n\n" + error2);
	}
}

function ContactUs(formname)
{
	var error = ValidateForm(formname);
	
	if(error == "")
	{
		LockForm(formname);
		
		var array = CollectInfo(formname);		
		var url= "/includes/request.php?form=" + formname;
			
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		//UnLockForm(formname);
		
		try
		{
			if(document.getElementById("brochure").checked)
			{
				//
				// Google tracking
				urchinTracker('/request/brochure');
				//alert("testing");
			}
		}
		catch(e)
		{
		
		}
		//alert(xmlHttp.responseText);
		if(xmlHttp.responseText == "true")
		{
			//alert("Thank you, you email has been sent.");
			
			UnLockForm(formname);
			
			if(formname == "request_a_brochure")
			{
				window.location = ROOTURL + "information/request-a-brochure-thanks.html";
			}
			else
			{
				window.location = ROOTURL + "information/thanks.html";
			}
			
		}
		else
		{
			UnLockForm(formname);
			alert("Sorry, there was a problem sending your message, please call us and we will be happy to help.");
		}
	}
	else
	{
		var error2 = error.replace(/end/g, "");
		alert("The following errors occured:\n\n" + error2);
	}
}

function FormInsert(formname)
{
	var error = ValidateForm(formname);
	
	if(error == "")
	{
		LockForm(formname);
		
		var array = CollectInfo(formname);		
		var url= "/includes/forminsert.php?form=" + formname;
		
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		//alert(url);
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		if(xmlHttp.responseText == "true")
		{
			var form = document.getElementById(formname);
			
			try
			{
				// If there is a forward node, then use that location to redirect, else use the tooltip.
				if(form.forward.value != "")
				{
					window.location = form.forward.value;
				}
				else
				{
					centerToolTip(form.response.value, 0, 0);
				}
			}
			catch(e)
			{
				centerToolTip(form.response.value, 0, 0);
			}
		}
		else
		{
			UnLockForm(formname);
			centerToolTip("Sorry, there was a problem inserting your information, please check and try again.", 0, 0);
		}
	}
	else
	{
		centerToolTip("The following information is still required:\n\n" + error, 0, 0);
	}
}

function EmailForm(formname)
{
	var error = ValidateForm(formname);
	
	if(error == "")
	{
		LockForm(formname);
		
		var array = CollectInfo(formname);		
		var url= "/includes/emailform.php?form=" + formname;
		
		for(i=0; i<array.length; i++)
		{
			url = url + "&" + array[i];
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
			
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		if(xmlHttp.responseText == "true")
		{
			var form = document.getElementById(formname);
			
			try
			{
				// If there is a forward node, then use that location to redirect, else use the tooltip.
				if(form.forward.value != "")
				{
					window.location = form.forward.value;
				}
				else
				{
					centerToolTip(form.response.value, 0, 0);
				}
			}
			catch(e)
			{
				centerToolTip(form.response.value, 0, 0);
			}
		}
		else
		{
			UnLockForm(formname);
			centerToolTip("Sorry, there was a problem submitting your information, please check and try again.");
		}
	}
	else
	{
		centerToolTip("The following information is missing:<br /><br />" + error);
	}
}

function PopulateDropDown(type, value, element)
{
	if(value != "other")
	{
		if(document.getElementById(element))
		{
			if(document.getElementById(element).nodeName.toLowerCase() == "select")
			{
				xmlHttp=GetXmlHttpObject()
				
				if (xmlHttp==null)
				{
					alert ("Browser does not support HTTP Request")
					return
				}
				
				var url="/includes/getDropDownData.php"
				url = url + "?type=" + type
				url = url + "&value=" + value
				url = url + "&element=" + element
				url = url + "&rand=" + Math.floor(Math.random()*10000000)
				
				xmlHttp.onreadystatechange=popChanged
				xmlHttp.open("GET",url,true)
				xmlHttp.send(null)
			}
			else
			{
				var child = document.getElementById(element);
				var parent = document.getElementById(element).parentNode;
				parent.removeChild(child);
				
				var child = document.getElementById("which");
				var parent = document.getElementById("which").parentNode;
				parent.removeChild(child);
				
				var selectbox = document.createElement("select");
				selectbox.setAttribute("id", element);
				parent.appendChild(selectbox);
				PopulateDropDown(type, value, element);
			}
		}
	}
	else
	{
		if(document.getElementById(element))
		{
			var child = document.getElementById(element);
			var parent = document.getElementById(element).parentNode;
			parent.removeChild(child);
			
			var textbox = document.createElement("input");
			textbox.setAttribute("id", element);
			textbox.setAttribute("name", element);
			textbox.setAttribute("value", "OTH");
			textbox.setAttribute("type", "hidden");
			
			parent.appendChild(textbox);
			
			var textbox = document.createElement("input");
			textbox.setAttribute("id", "which");
			textbox.setAttribute("name", "which");
			textbox.setAttribute("value", "");
			textbox.setAttribute("type", "text");
			
			parent.appendChild(textbox);
		}
	}
	
	
	return true;
}

function populateList(ele, options)
{
	for(var i=0; i<options.length; i++)
	{
		var tempOption = options[i].split("++");
		
		if(tempOption.length > 0)
		{
			var oOption = document.createElement("OPTION");
			oOption.setAttribute("value", tempOption[0]);
			oOption.text = tempOption[1];
			
			if(document.all)
			{
				ele.add(oOption);
			}
			else
			{
				ele.appendChild(oOption);
			}
		}
	}
}

function popChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var temp = xmlHttp.responseText.split("$$$");
		var options = temp[0].split("|");
		
		var ele = document.getElementById(temp[1]);
		
		if(ele.hasChildNodes())
		{
			while(ele.hasChildNodes())
			{
				ele.removeChild(ele.firstChild);
			}
		}
		
		populateList(ele, options);
	} 
}

function NormalDropDown(type, value, element)
{
	if(value == "other")
	{
		if(document.getElementById(element))
		{
			var child = document.getElementById(element);
			var parent = document.getElementById(element).parentNode;
			
			var textbox = document.createElement("input");
			textbox.setAttribute("id", "which");
			textbox.setAttribute("name", "which");
			textbox.setAttribute("value", "");
			textbox.setAttribute("type", "text");
			
			parent.appendChild(textbox);
		}
	}
	else
	{
		try
		{
			var child = document.getElementById("which");
			var parent = document.getElementById("which").parentNode;
			parent.removeChild(child);
		}
		catch(e){}
	}
}