////////////////////////////////////////////////////////////
// Form Processing Functions
////////////////////////////////////////////////////////////

function getPOSTParams(theForm)
{
	var params = '';
	for (var x = 0; x < theForm.elements.length; x++)
	{
		if (theForm.elements[x].type == 'radio' || theForm.elements[x].type == 'checkbox') {
			if (theForm.elements[x].checked) {
				params += theForm.elements[x].name+'='+encodeURI(theForm.elements[x].value)+'&';
			}
		}
		else {
			params += theForm.elements[x].name+'='+encodeURI(theForm.elements[x].value)+'&';
		}
	}
	params += 'ajax=1';
	return params;
}

function getLabel(str)
{
	found = str.match(/^([a-z\s]+)/i);
	return found[1];
}

function submitPoll(theForm)
{
	var uAnswer = '';
	for (x = 0; x < theForm.answer.length; x++)
	{
		if (theForm.answer[x].checked == true)
			uAnswer = encodeURI(theForm.answer[x].value);
	}
	if (uAnswer.length == 0) {
		alert('You must select an answer first.');
		return false;
	}
	callAHAH('/bd/studies/poll_process.php', getPOSTParams(theForm), 'PollWrapper'+theForm.sid.value);
	return false;
}

function submitSurvey(theForm, req, ajax)
{
	if (req == '1') {
		var fields = ['firstname','lastname','company','position','phone','address','city','state','country','zip','email'];
		for (var f = 0; f < fields.length; f++) {
			if (theForm[fields[f]] && theForm[fields[f]].value == '') {
				label = document.getElementById(fields[f]).innerHTML;
				alert('The field "'+getLabel(label)+'" must not be empty.');
				theForm[fields[f]].focus();
				return false;
			}
		}
		if (theForm.companytype && theForm.companytype.selectedIndex == 0) {
			label = document.getElementById('companytype').innerHTML;
			alert('The field "'+getLabel(label)+'" must be completed.');
			theForm.companytype.focus();
			return false;
		}
		else {
			var re = /[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})/;
			emailvalid = theForm.email.value.search(re);
			if (emailvalid == -1) {
				alert('Please enter a valid email address.');
				theForm.email.focus();
				return false;
			}
		}
	}
	if (ajax == 1) {
		callAHAH('/bd/studies/survey_process.php', getPOSTParams(theForm), 'SurveyWrapper'+theForm.sid.value);
		return false;
	}
	return true;
}

////////////////////////////////////////////////////////////
// Core Ajax Functions
////////////////////////////////////////////////////////////

function callAHAH(url, params, pageElement, callMessage, errorMessage) {
	if (callMessage)
		document.getElementById(pageElement).innerHTML = callMessage;
	try {
		req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
			} catch (E) {
				req = false;
			} 
		} 
	}
	req.onreadystatechange = function() {responseAHAH(pageElement, errorMessage);};
	req.open('POST',url,true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");
	req.send(params);
}

function responseAHAH(pageElement, errorMessage) {
	var output = '';
	if(req.readyState == 4) {
		if(req.status == 200) {
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		} else {
			if (errorMessage)
				document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
		}
	}
}

