var xmlHttp = null;
var errors = new Array();

function validateField(field) {
	if (field.id == "email") {
		//validates email - make sure the user has placed a "@" and a "." after it.
		apos=field.value.indexOf("@");
		dotpos=field.value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2||field.value.length==(dotpos+1)) {
			//not valid
			return "Invalid email address.";
		} else {
			//valid
			return "";
		}
	} else if (field.id == "phone1" || field.id == "phone2" || field.id == "phone3") {
		//make sure each part of the phone number has the correct amount od digits
		phone1 = document.getElementById('phone1');
		phone2 = document.getElementById('phone2');
		phone3 = document.getElementById('phone3');
		if(phone1.value.length == 3 && phone2.value.length == 3 && phone3.value.length == 4) {
			//valid
			return "";
		} else {
			//not valid
			return "Invalid phone number. Must contain at least 10 digits.";
		}
	} else if (field.id == "age") {
		//make sure the age is a number and greater than 1
		if ( field.value == null || field.value == "" ) {
			//not valid
			return 'The "age" field is currently blank.';
		} else if ( isNaN(field.value) ) {
			//not valid
			return 'Invalid age. Must be a number.';
		} else if ( field.value < 1) {
			//not valid
			return 'Invalid age. Must be greater than 1.';
		} else {
			//valid
			return "";
		}
	} else if (field.id == "name") {
		if(field.value == null || field.value == "") {
			//not valid
			return 'The "'+field.id+'" field is currently blank.';
		} else if(field.value.split(" ").length == 1) {
			//not valid
			return 'The "'+field.id+'" field needs to have more than one word.';
		} else {
			//valid
			return "";
		}		
	} else {
		//otherwise, just check that the field is not blank
		if(field.value == null || field.value == "") {
			//not valid
			return 'The "'+field.id+'" field is currently blank.';
		} else {
			//valid
			return "";
		}
	}
}

function sendPledge(formdata) {
	with (formdata) {
		errorsFound = 0;
		
		for ( i=0; i<formdata.length; i++ ) {
			if (formdata[i].name != null && ((formdata[i].nodeName == "INPUT" && formdata[i].type == "text") || (formdata[i].nodeName == "TEXTAREA" ) ) ) {
				checkForError = validateField(formdata[i]);
				
				if (formdata[i].name.indexOf("phone") != -1) errorName = "phone";
				else errorName = formdata[i].name;
				
				if (checkForError != "") errorsFound++;
				x = document.getElementById('error_'+errorName);
				x.innerHTML = checkForError;
				x.style.display = "block";
			}
		}

		if ( errorsFound == 0 ) {
			//we're going to use ajax tech to send data to the asp file that will send the email without having to reload the page
			try {
				// Firefox, Opera 8.0+, Safari, IE7
				xmlHttp=new XMLHttpRequest();
			} catch(e) {
				// Old IE
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					alert ("Your browser does not support XMLHTTP!");
					return;  
				}
			}
			
			//start the request...
			xmlHttp.open("POST", "sendpledge.asp", true);
			
			//need to take gather the text we are sending as variables as a single text string (necessary for content-length)
			params = interests.name+"="+escape(interests.value) + "&join=" + join.checked;
			inputs = formdata.getElementsByTagName("input");
			for ( i in inputs ) {
				if ( inputs[i].type == "text" ) {
					params += "&"+inputs[i].name+"="+escape(inputs[i].value);
				}
			}
					
			//header information (required for POST method)			
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");

			//go ahead and hide the form
			document.getElementById("display-pledgeform").style.display = "none";
			document.getElementById("display-results").style.display = "block";

			//scroll to the top
			window.scrollTo(0, 0);

			//set up the function for the state changes
			xmlHttp.onreadystatechange = function() {
				if ( xmlHttp.readyState == 4 ) {
					if ( xmlHttp.status == 200 ) {
						document.getElementById("display-results-name").innerHTML = name.value;
					} else {
						document.getElementById("display-results").innerHTML = xmlHttp.status + " - " + xmlHttp.statusText + "<br/>";
					}
				}
			}
			//send the request!
			xmlHttp.send(params);
		}
	}
}

function resetForm(formdata){
	for ( i=0; i<formdata.length; i++ ) {
		if (formdata[i].name != null && ((formdata[i].nodeName == "INPUT" && formdata[i].type == "text") || (formdata[i].nodeName == "TEXTAREA" ) ) ) {
			if (formdata[i].name.indexOf("phone") != -1) errorName = "phone";
			else errorName = formdata[i].name;
			x = document.getElementById('error_'+errorName);
			x.innerHTML = "";
			x.style.display = "none";
		}
	}
}
