/* javascript functions */

var req; /*XMLHttpRequest object */
var template_path = '';

function loadXMLdoc(url, vars)
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');			
		req.send(vars);
    // branch for IE/Windows ActiveX version
  }
	else if	(window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req)
		{
		    req.onreadystatechange = processReqChange;
		    req.open("POST", url, true);
		    req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');			
		    req.send(vars);
		}
  }
}

function processReqChange()
{
  // only if req shows "loaded"
  if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// alert(req.responseText);
			if (req.responseXML) {
				var message_type = false;
				try {
					message_type = req.responseXML.getElementsByTagName("message_type")[0];
					message_type_value = get_value(message_type);
				}
				catch(e) {
					// just do nothing and return
					return;
				}
				finally	{
					switch(message_type_value) {
						case "user_values":
							checkout();
							break;
						case "ccard_check":
							ccard_process();
							break;
							
						case "paypal_check":
							paypal_process();
							break;
							
						case "coupon_check":
							coupon_process();
							break;
							
						case "userinfo":
							check_userinfo();
							break;
					} // end switch
				} // end finally
			} // end req.responseXML	
		} // end if "OK"
		else {
			alert("There was a problem communicating:\n" + req.statusText);
		}
	} // end "loaded"
}

function get_value(result)
{	
	if (result) {
		// get text, accounting for possible
		// whitespace (carriage return) text nodes 
	  if (result.childNodes.length > 1) {
		  value = result.childNodes[1].nodeValue;
	  }
	  else {
		  value = result.firstChild.nodeValue;    		
	  }
	}
	return value;
}

function init(path)
{
	template_path = path;
}
