var requestObj = false;

function displayConfirmation()
{
	if (requestObj.readyState == 4)
	{
		if (requestObj.status == 200)
		{
			var response = requestObj.responseXML.getElementsByTagName("response")[0];
			var messageNode = response.getElementsByTagName("message")[0];
			var message = messageNode.childNodes[0].nodeValue;
			var confirmationNode = response.getElementsByTagName("confirmation")[0];
			var confirmation = confirmationNode.childNodes[0].nodeValue;
			
			if(message == "Error")
			{
				alert("Error submitting request, contact administrator");
			}
			else
			{
				var div = document.getElementById("rightcolumn");
				div.innerHTML = "";
				div.innerHTML = "<div class=\"submitStatus\"><span class=\"contentFont4\">Your request has been submitted, your confirmation number is: <font class=\"contentFont7\">" + 
								 confirmation + "</font><br><br> <a href=\"requestProposal.php\">Send another request</a> | <a href=\"index.php\">Back to Home page</a></span></div>";
			}
		}
		else if (requestObj.status == 404)
		{
			alert("Request URL does not exist");
		}
		else
		{
			alert("We are experiencing technical problems, please try again later");
		}
	}	
}

function getParams()
{
	var firstName = document.getElementById("FirstNameText").value;
	var lastName = document.getElementById("LastNameText").value;
	var company = document.getElementById("CompanyText").value;
	var phone = document.getElementById("PhoneText").value;
	var extention = document.getElementById("ExtentionText").value;
	var email = document.getElementById("EmailText").value;
	var country = document.getElementById("CountryDropDown").value;
	var responseNeeded = document.getElementById("ResponseUrgencyDropDown").value;
	var requesting = document.getElementById("RequestingDropDown").value;
	var address = document.getElementById("MailingAddressText").value;
	var city = document.getElementById("CityText").value;
	var state = document.getElementById("StateText").value;
	var zip = document.getElementById("ZipText").value;
	var fax = document.getElementById("FaxText").value;
	var customerType = document.getElementById("CustomerTypeDropDown").value;
	var application = document.getElementById("ApplicationDropDown").value;
	var productService = document.getElementById("ProductServiceDropDown").value;
	var projectName = document.getElementById("ProjectNameText").value;
	var projectLocation = document.getElementById("ProjectLocationText").value;
	var minFlowRate = document.getElementById("MinFlowRateText").value;
	var minFlowRateType = document.getElementById("MinFlowRateDropDown").value;
	var avgFlowRate = document.getElementById("AvgFlowRateText").value;
	var avgFlowRateType = document.getElementById("AvgFlowRateDropDown").value;
	var maxFlowRate = document.getElementById("MaxFlowRateText").value;
	var maxFlowRateType = document.getElementById("MaxFlowRateDropDown").value;
	var comments = document.getElementById("CommentsText").value;
	var params = "?firstName=" + firstName + "&lastName=" + lastName + "&company=" + company + "&phone=" + 
				   phone + "&extention=" + extention + "&email=" + email + "&country=" + country + "&responseNeeded=" + responseNeeded + 
				   "&requesting=" + requesting + "&address=" + address + "&city=" + city + "&state=" + state + "&zip=" + zip + "&fax=" + fax +
				    "&customerType=" + customerType + "&application=" + application + "&productService=" + productService + "&projectName=" + projectName + 
					"&projectLocation=" + projectLocation + "&minFlowRate=" + minFlowRate + "&minFlowRateType=" + minFlowRateType + 
					"&avgFlowRate=" + avgFlowRate + "&avgFlowRateType=" + avgFlowRateType + "&maxFlowRate=" + maxFlowRate + "&maxFlowRateType=" + maxFlowRateType +
					"&comments=" + comments;
	return params;
}

function submitForm()
{
	document.getElementById("SubmitButton").disabled = "true";
	document.getElementById("mainForm").submit();
}

function submitRequest()
{	
	try
	{
		requestObj = new XMLHttpRequest();
	} 
	catch (trymicrosoft)
	{
		try 
		{
			requestObj = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (othermicrosoft) 
		{
			try 
			{
				requestObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed) 
			{
				requestObj = false;
			}
		}
	}
	
	params = getParams();
	
	var url = "submitRequest.php" + params;
	requestObj.open("GET", url, true);
	requestObj.onreadystatechange = displayConfirmation;
	requestObj.send(null);
}

function validateForm()
{
	//Required fields
	var firstName = document.getElementById("FirstNameText").value;
	var lastName = document.getElementById("LastNameText").value;
	var company = document.getElementById("CompanyText").value;
	var phone = document.getElementById("PhoneText").value;
	var email = document.getElementById("EmailText").value;
	var country = document.getElementById("CountryDropDown").value;
	var responseNeeded = document.getElementById("ResponseUrgencyDropDown").value;
	var requesting = document.getElementById("RequestingDropDown").value;
	
	if(firstName == "")
	{
		alert("First Name is a required field");
	}
	else if(lastName == "")
	{
		alert("Last Name is a required field");
	}
	else if(company == "")
	{
		alert("Company is a required field");
	}
	else if(phone == "")
	{
		alert("Phone number is a required field");
	}
	else if(country == "")
	{
		alert("Country is a required field");
	}
	else if(email == "")
	{
		alert("Email address is a required field");
	}
	else if(responseNeeded == "")
	{
		alert("Response Needed is a required field");
	}
	else if(requesting == "")
	{
		alert("I am requesting is a required field");
	}
	else
	{
		submitForm();
	}
}