/**
 * @copyright  2008 Ian Atkin
 * @license    http://www.ianatkin.info/
 */

// ajax functions
function ajaxFunc(mode, items) {
	var ajaxRequest; // The variable that makes ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

	// functions that will receive data sent from the server

	// GET only: we add this param to prevent fetched content being brought from the cache
	var rnd = Math.random()*10000;

	if (mode == 'saveNewsletterSignup') {
		ajaxRequest.onreadystatechange = function() {
			if(ajaxRequest.readyState == 4) {
				var response = ajaxRequest.responseText;

				if(response != 'failed') {
					var attributes = {
						opacity: { to: 0 }
					};
					var anim = new YAHOO.util.Anim('nl-widget', attributes);

					anim.animate();
					anim.onComplete.subscribe(function() {
						setStyle('nl-widget', 'display', 'none');
					});
				} else {
					alert(response);
				}

				showHideBusy();

			}
		}

		showHideBusy(true);

		var postVal;

		postVal = "mode=saveNewsletterSignup";
		postVal += "&txtFirstName=" + escapeString(get('txtFirstName'));
		postVal += "&txtLastName=" + escapeString(get('txtLastName'));
		postVal += "&txtEmailAddress=" + escapeString(get('txtEmailAddress'));

		ajaxRequest.open("POST", "/includes/home.ajax.php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.send(postVal);
	}

	if (mode == 'logError') {
		ajaxRequest.onreadystatechange = function() {
			if(ajaxRequest.readyState == 4) {
				return;
			}
		}

		var postVal = "mode=logError&vars=" + items[0] + "&comment=" + items[1];

		ajaxRequest.open("POST", "/includes/client.ajax.php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.send(postVal);
	}
}
