function showSurveyCaptureForm()
{
	var elem = document.getElementById('survey-capture-data-form');
	var button = document.getElementById('survey-capture-submit-button');
	if(elem && button)
	{
		elem.style.display = "block";
		button.style.display = "block";
	}
}

function hideSurveyCaptureForm()
{
	var elem = document.getElementById('survey-capture-data-form');
	var button = document.getElementById('survey-capture-submit-button');
	if(elem && button)
	{
		elem.style.display = "none";
		button.style.display = "block";
	}
}

function submitSurveyCaptureForm()
{
	if(!ajaxCheck())	//	Make sure AJAX is supported on this browser
	{
		alert("ERROR: Could not send form. Your browser does not support AJAX.");
	}
	
	//	Hide button
	writeToRelatedGroupDiv("survey-capture-submit-button", "Submitting...");
	
	//	Set event to listen for data return
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4)
		{
			var ID = "survey-capture-submit-button";
			var htmlContent = xmlHttp.responseText;
			writeToRelatedGroupDiv(ID, htmlContent);
		}
	}
	
	//	Build parameters
	var ID = document.surveycaptureform.ID.value;
	var polloption;
	for(i = 0; i < document.surveycaptureform.polloption.length; i++)
	{
		if(document.surveycaptureform.polloption[i].checked == true)
		{
			polloption = document.surveycaptureform.polloption[i].value;
			break;
		}
	}

	var params = "ID=" + escape(ID) + "&polloption=" + escape(polloption);
	
	if(document.surveycaptureform.surveycapturename.value)
	{
		var name = document.surveycaptureform.surveycapturename.value;
		var email = document.surveycaptureform.surveycaptureemail.value;
		var phone = document.surveycaptureform.surveycapturephone.value;
		var company_name = document.surveycaptureform.surveycapturecompany_name.value;
		var position = document.surveycaptureform.surveycaptureposition.value;
		var misc_1 = document.surveycaptureform.surveycapturemisc_1.value;
		params += "&name=" + escape(name) + "&email=" + escape(email) + "&phone=" + escape(phone) + "&company_name=" + escape(company_name) + "&position=" + escape(position) + "&misc_1=" + escape(misc_1);
	}
	
	//	Set data to PHP script
	xmlHttp.open("POST","/wp-content/plugins/survey-capture/survey-capture-ajax.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//	Write to div
function writeToRelatedGroupDiv(ID, htmlContent)
{
	//	Populate the HTML page
	if (document.layers) 
	{
		var oLayer;
		oLayer = document.layers[ID].document;
		oLayer.open();
		oLayer.write(htmlContent);
		oLayer.close();
	}
	else if(parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") 
	{
		document.getElementById(ID).innerHTML = htmlContent;
	}
	else if(document.all) 
	{
		document.all[ID].innerHTML = htmlContent;
	}
}

function ajaxCheck()
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		return true;
	}
  	catch(e)
	{
		// Internet Explorer
		try
		{
			  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			  return true;
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				return true;
			}
			catch(e)
			{
				return false;
			}
		}
	}
}
