// JavaScript Document


function validateForm(name, company, phoneNumber, email, locations) {
	
	var error = "";
	
	if (name == "") {
		error += "Name field is blank\n";
	}
	if (company == "") {
		error += "Company field is blank\n";
	}
	if ((phoneNumber != "") && !(checkInternationalPhone(phoneNumber))) {
		error += "Please use your whole phone number\n";
	}
	if (email == "") {
		error += "Email field is blank\n";
	} else if (echeck(email) == false) {
		error += "Email is invalid\n";
	}
	
	if(locations == "0") {
		error += "Please select a location\n";
	}
	
	if (error != "") {
		//var validate = document.getElementById("validate");
		//replaceText(validate, error);
		alert (error);
		
		return false;
	} 
	return true;
	
}

function sendRequestText() {
	// this just displays the "sending" text
	var maincontent = document.getElementById("maincontent");
	var corpForm = maincontent.parentNode;
	
	divholder = document.createElement("div");
	divholder.className = "maincontent";
	
	headerEl = document.createElement("h1");
	headerTextNode = document.createTextNode("Sending request...");
	pElement = document.createElement("p");
	textNode = document.createTextNode("Your request is being sent.");
	
	pElement.appendChild(textNode);
	headerEl.appendChild(headerTextNode);
	
	// replace with the text
	corpForm.replaceChild(divholder,maincontent);
	
	divholder.appendChild(headerEl);
	divholder.appendChild(pElement);
}

function sendMembershipInfo() {
	
	var name = document.getElementById("name").value;
	var company = document.getElementById("company").value;
	var phoneNumber = document.getElementById("phone").value;
	var email = document.getElementById("email").value;
	var locations = document.getElementById("locations").value;
	var comments = document.getElementById("comments").value;
	
	// if the form checks out, create the ajax request and send it via post
	if (validateForm(name, company, phoneNumber, email, locations)) {
	 
	 // divert to sending text in case it takes a while
	 sendRequestText();
	 
	 var url = "membership_send.php";
	 request = new Ajax.Request(url,
								{
									method: 'post',
									parameters: {name:name, company:company, phone:phoneNumber,email:email,locations:locations, comments:comments},
									onSuccess: updatePage
								});
	}
}

function updatePage(request) {
	 var response = request.responseText;

		// if the response is good, forward to the thank you page
		if (response == "Good") {
			window.location = "membership_thankyou.htm";
		} else {
			alert ("Error: "+response);
		}
		


}

