function validateSelection(select,errorMessage)
{
	if (select.options == null || select.options[select.selectedIndex].value == "") {
		alert(errorMessage);
		return false;
	} 
	return true;
}

function validateEmail(field,errorMessage)
{
	if (field.value == null) {
		alert(errorMessage);
		return false;
	}
	
	aPos = field.value.indexOf("@");
	dotPos = field.value.indexOf(".");
	if (aPos < 1 || dotPos < 2) {
		alert(errorMessage);
		return false;
	}
	return true;
}

function validateField(field,errorMessage)
{
	if (field.value == null || field.value == "") {
		alert(errorMessage);
		return false;
	}  
	return true;
}

function validInputs(formName) {
	thisForm = document.forms[formName];
	
	var valid = true;
	valid = valid && validateField(thisForm.first_name,'Please provide a first name.');
	valid = validateField(thisForm.last_name,'Please provide a last name.') && valid;
	valid = validateField(thisForm.title,'Please provide a title.') && valid;
	valid = validateField(thisForm.company,'Please provide a company.') && valid;
	valid = validateEmail(thisForm.email,'Please provide a valid email.') && valid;
	valid = validateField(thisForm.phone,'Please provide a phone number.') && valid;
	valid = validateField(thisForm.zip,'Please provide a zip.') && valid;
	valid = validateField(thisForm.country,'Please provide a country.') && valid;
		
	return valid;
}

function buildPost(formDescription,formName) {
	theForm = document.forms[formName];
	var qs = '';
	var newValue = '';
	var newName = '';
	for (e=0;e<theForm.elements.length;e++) {
		if (theForm.elements[e].name!='') {
			var name = theForm.elements[e].name;
			var type = theForm.elements[e].type;
			if (type == 'select-multiple') {
				newName = name + '[]';
				newValue = newName + '=';
				for (i=0; i < theForm.elements[e].length; i++) 
					if (theForm.elements[e].options[i].selected) {
						if (newValue == newName)
							newValue += escape(theForm.elements[e].options[i].value);
						else
							newValue += '&' + newName + '=' + escape(theForm.elements[e].options[i].value);
					}
				qs+=(qs=='')?'':'&'
				qs+= newValue;	
			} else {
				qs+=(qs=='')?'':'&'
				qs+= name+'='+escape(theForm.elements[e].value);
			}
		}
	}	
	qs+='&formDescription='+escape(formDescription);
	return qs
}

function postForm(formDescription,formName,id) {

	// check to make sure all fields have been filled out
	// properly
	if (!validInputs(formName))
		return false;

	// grab all the form element names and values submitted.
	var pars = buildPost(formDescription,formName);
	var url = 'http://www.openlogic.com/survey/wrap/saveFormData.php';

	// uncomment this to capture the URL that sends all the fields and values
	// to a php page for saving to db.  Useful for debugging.
	// var temp = prompt('AJAX URL',(url + '?' + pars));
	
	// create an Ajax call so we can submit our values to a php script
	// that will in turn save the values to the DB.
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: addComplete.bind(this, id),
			onLoading: addLoading.bind(this, id),
			onFailure: reportError.bind(this, id)
		});		
		
	// wait a few seconds for the ajax to fire
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < 2000); // wait 2 seconds

	return true;
}

function addLoading(id, theResponse) {
	var theElement = document.getElementById(id);
	theElement.innerHTML = "posting form...";
}

function addComplete(id, theResponse) {
	var theElement = document.getElementById(id);
	theElement.innerHTML = "form posted form...<br />" +
		theResponse.responseText;
}

function reportError(id, theResponse)
{
	var theElement = document.getElementById(id);
	theElement.innerHTML = "form not posted...";
}