$(document).ready(function()
{
	// TEXT field validation rules
	var SpryFirstName = new Spry.Widget.ValidationTextField("SpryFirstName", "none", {validateOn:["change"]});
	var SpryLastName = new Spry.Widget.ValidationTextField("SpryLastName", "none", {validateOn:["change"]});
	var SpryTitle = new Spry.Widget.ValidationTextField("SpryTitle", "none", {validateOn:["change"]});
	var SpryOrgName = new Spry.Widget.ValidationTextField("SpryOrgName", "none", {validateOn:["change"]});
	var SpryEmail = new Spry.Widget.ValidationTextField("SpryEmail", "email", {validateOn:["change"], hint:"name@company.com"});
	var SpryPhone = new Spry.Widget.ValidationTextField("SpryPhone", "none", {validateOn:["change"], hint:"(555) 321-1234"});
	var SpryCity = new Spry.Widget.ValidationTextField("SpryCity", "none", {validateOn:["change"]});

	// SELECT field validation
	var SpryOrgType = new Spry.Widget.ValidationSelect("SpryOrgType", {validateOn:["change", "blur"]});
	var SpryState = new Spry.Widget.ValidationSelect("SpryState", {validateOn:["change", "blur"]});
	var SpryInterest = new Spry.Widget.ValidationSelect("SpryInterest", {validateOn:["change", "blur"]});

	// prepare Options Object for AJAX form
	var options =
	{
		dataType: 	'xml',
		success:	processXml,
		error:		handleError,
		beforeSubmit: validate,
	};

	// cross-domain XHR is breaking (was it ever working?); disabling for now
	//$('#frmACDCapture').ajaxForm(options);
});

// look through XML to find out if data was accepted
function processXml(responseXML)
{
    // find <return_val> node in our XML doc
    var return_val = $('return_val', responseXML).text();

	// if our XML doc <return_val> node contains number 0, go ahead and say thank you
	if ( return_val == '0' )
	{
		location.href = 'RequestForm_Confirmation.html';
		//alert('Sucess!');
	}
	// otherwise, show data that needs to be corrected
	else
	{
		$('#form_message').empty();
		$('#form_message').append('<p><strong>You must correct the following information:</strong></p><ul></ul>');

		$(responseXML).find('detail').each(function()
		{
			var item_text = $(this).text();

			$('#form_message > ul').append('<li>' + item_text + '</li>');
		});

		$('#form_message').addClass('active');
	}
}

// retutn errror messages if we couldn't retrieve the XML
function handleError()
{
	alert('Error loading XML document');
	return false;
}

function validate(formData, jqForm, options)
{
	// jqForm[0];
	var myForm = document.getElementById('frmACDCapture');
	var result = Spry.Widget.Form.validate(myForm);
	return result;
}
