// Creates and returns an AJAX request handler
function ajaxCreate () {
	// Patch for MSIE/Win to simulate native XMLHttpRequest method
	if (window.XMLHttpRequest === undefined) {
		window.XMLHttpRequest = function () {
			var types = new Array ();
			types[0] = "Microsoft.XMLHTTP";
			types[1] = "MSXML2.XMLHTTP.5.0";
			types[2] = "MSXML2.XMLHTTP.4.0";
			types[3] = "MSXML2.XMLHTTP.3.0";
			types[4] = "MSXML2.XMLHTTP";

			for (var i = 0; i < types.length; i++) {
				try {
					return new ActiveXObject (types[i]);
				} catch (e) {
					continue;
				}
			}
			return undefined;
		}
	}
	return new XMLHttpRequest ();
}

// Makes an HTTP request via AJAX and returns the response to a callback function
// @params:
//   url - URL that the request will be made to
//   args - Query string values sent during request
//   method - Method to be used for the request, GET by default
//   onSuccess - Callback function to invoke upon request success
//   onFailure - Callback function to invoke upon request error
function ajaxRequest (params) {
	// Define AJAX request handler
	request = ajaxCreate ();
	if (!request) {
		if (params.onFailure) params.onFailure ("Request Exception: XMLHttpRequest failed to instantiate.");
		return;
	}
	// Define AJAX status callback
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			if (request.status == 200) {
				
				// make the google urchin call here
				if(typeof urchinTracker == 'function')
				{				
					var _uacct = "UA-1152868-1";
				  urchinTracker(params.url);
				}
				// make the omni call if it exists
				if(typeof omni_track_ajax == 'function') {
					omni_track_ajax(params.url,params.args);
				}
				
				if (params.onSuccess) params.onSuccess (request.responseText);
			} else {
				if (params.onFailure) params.onFailure ("Request Exception: Server responded with '" + request.status + "'");
			}
		}
	}

	var url = params.url;
	var args = params.args || null; // Default to no query string values
	var method = params.method || "GET"; // Default to GET request
	if (method == "GET" && args) {
		url += url.match (/\?/)? "&" : "?";
		url += args;
	}

	request.open (method, url, true);
	if (method == "POST") request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	request.send (args);
}

// Takes a form object or its id and returns an ampersand-separated list of key-value pairs
function formValues (form) {
	// Check if an object or an id was passed
	if (typeof (form) !== "object") form = document.getElementById (form);
	
	// Get the form data
	var args = new String;
	for (var i = 0; i < form.elements.length; i++) {
		field = form.elements[i];
		// Exclude unnamed form elements
		if (field.name) {
			// Only submit values for checked radio buttons and checkboxes
			if ((field.type === "radio" || field.type === "checkbox") && field.checked === false) continue;
			// Handle multiple selection inputs
			else if (field.type === "select-multiple") {
				for (var j = 0; j < field.options.length; j++) {
					if (field.options[j].selected === true) args += escape (field.name) + '=' + escape (field.options[j].value) + '&';
				}
			}
			else args += escape (field.name) + '=' + escape (field.value) + '&';
		}
	}

	return args;
}

// Uses AJAX to process a form request
// form - The form object to be processed or its id
// @logic:
//		onSuccess - Callback function to process a successful request
//		onFailure - Callback function to process an unsuccessful request
// url - The URL of the script that will process the form data
// USAGE: <form id='foo' method='post' onSubmit=\"return formProcess (this, myLogic, 'process_foo.phtml')\">
// see NeoDoc article "JavaScript: formProcess" for more information
function formProcess (form, logic, url) {
	// Check if an object or an id was passed
	if (typeof (form) !== "object") form = document.getElementById (form);
	
	// Build a request object to pass to ajaxRequest
	var request = new Object;
	request.url = url;
	request.method = form.method.toUpperCase ();
	request.args = formValues (form);
	// Define the function that will handle a successful AJAX request
	request.onSuccess = logic.onSuccess;
	// Define the function that will handle an unsuccessful AJAX request
	request.onFailure = logic.onFailure;

	ajaxRequest (request);

	// Prevent the form from refreshing the page
	return false;
}

