var validateAttributesReq;
var validateAttributesDom;

/*******************************************************************************
 * getValidateAttributesRequestXML - Create an XML ValidateAttributesRequest string from the controls in the form
 *******************************************************************************/
function getValidateAttributesRequestXML(form)
{
	var xml = '';

// Start the request
	xml += '<'+"?xml version=\"1.0\" xml:lang=\"UTF-8\"?>\n";
	xml += "<maverick>\n";

// Get the base form specs
	var base_job_attribute = getAttributesFromForm(form);

// Add a request for each quantity
	var quantity;
	var qty = getControlValue(form,'qty');
	qty = (qty && qty.length) ? qty[0] : '';
	if (qty.indexOf('/') > -1)
	{
		quantity = qty.split('/');
	}
	else
	{
		quantity = new Array(qty);
	}
	var len = quantity.length;
	for (var i=0;i<len;i++)
	{
		q = quantity[i];

	// Start the request
		xml += "<ValidateAttributesRequest>\n";

	// Add the quantity
		xml += "<Quantity>"+q+"</Quantity>\n";

	// Add the product
		var product = getControlValue(form,'product');
		product = (product && product.length) ? product[0] : '';
		xml += "<Product>"+product+"</Product>\n";

	// Add the specs
		var job_attribute = base_job_attribute;
		xml += getAttributeList(job_attribute);

	// End request
		xml += "</ValidateAttributesRequest>\n";
	}
	xml += "</maverick>\n";

	return xml;
}

/*******************************************************************************
 * validateAttributes - use XMLHttpRequest to validate the form
 *******************************************************************************/
function validateAttributes(form)
{
// Create the XML query
	var xml_request = getValidateAttributesRequestXML(form);
//alert(xml_request);

// Send the XML query
	var url = '/xml/validateAttributes.php';
	if (window.XMLHttpRequest)
	{
	// Non-IE browsers
		validateAttributesReq = new XMLHttpRequest();
//		validateAttributesReq.onreadystatechange = processStateChangeValidateAttributes;
		try {
			validateAttributesReq.open("POST", url, false);
		} catch (e) {
			alert(e);
		}
		validateAttributesReq.setRequestHeader('Content-Type', 'text/xml');
		validateAttributesReq.send(xml_request);
	}
	else if (window.ActiveXObject)
	{
	// IE
		validateAttributesReq = new ActiveXObject("Microsoft.XMLHTTP");
		if (validateAttributesReq)
		{
//			validateAttributesReq.onreadystatechange = processStateChangeValidateAttributes;
			validateAttributesReq.open("POST", url, false);
			validateAttributesReq.setRequestHeader('Content-Type', 'text/xml');
			validateAttributesReq.send(xml_request);
		}
	}

// Wait for it to come back
	var date = new Date();
	var timeout = date.getTime*10000;
	while (validateAttributesReq.readyState != 4 && timeout > date.getTime())
		date = new Date();
	if (validateAttributesReq.readyState == 4)
	{
	// Complete
		if (validateAttributesReq.status == 200)
		{
		// OK response
			if (typeof validateAttributesReq.responseXML != 'undefined')
				validateAttributesDom = validateAttributesReq.responseXML;
			else
				parseXMLIntoDOM(validateAttributesReq.responseText);
			if (!validateAttributesDom)
				return;
//alert(validateAttributesReq.responseText);

		// Get the response
			var maverick = validateAttributesDom.getElementsByTagName('maverick')[0];
			var response = maverick.getElementsByTagName('ValidateAttributesResponse')[0];
			var errors = response.getElementsByTagName('ErrorResponse');
			if (errors && errors.length > 0)
			{
				var msg = '';
				for (var i=0; i<errors.length; i++)
				{
					var err = errors[i];
					var errmsg = err.getElementsByTagName('ErrorMessage');
					errmsg = errmsg[0];
					errmsg = errmsg.firstChild.data+'\n';
					if (msg.indexOf(errmsg) < 0)
						msg += errmsg;
				}
				alert(msg);
				return false;
			}
		}
	}

	return true;
}
