/* ===================================================================================== *\
	----------------------------------> GLOBAL Functions <--------------------------------
\* ===================================================================================== */
// ########	Send a new Ajax Request  ######## \\
//  @param url = file to post to
//  @param vars = variables to post ['id=1&name=john']
//  @param destination = Javascript function when the data gets returned 
// ########################################## \\
function zbmajax(url, vars, destination, destDiv) {
	vars = vars + '&destinationurl='+url;
	if (destination === undefined) { destination = 'doNothing'; }
	if (destDiv === undefined) { destDiv = ''; }
	var xmlhttp = new xmlhttpObject();
	xmlhttp.onreadystatechange = function () { stateChange(); };
	xmlhttp.open('POST', 'request/', true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  	xmlhttp.send(vars);
	 
	function stateChange() {
		if (xmlhttp.readyState == 4) {
 			if (xmlhttp.status == 200) {
 				if(destDiv=='') {
					destination(xmlhttp.responseText);
 				} else {
					destination(xmlhttp.responseText,destDiv);
 				}
 			}
 		}
	}
}
function xmlhttpObject() {
	if (window.XMLHttpRequest) {
 		return new XMLHttpRequest();
	}
	return new ActiveXObject("Microsoft.XMLHTTP");
}
function doNothing() { }

// function to send AJAX Response to browser specific DOM element
function listStateChanged(response,divID) {
	if(divID === undefined) { divID = 'ajax_div'; }
	document.getElementById(divID).innerHTML=response;
}

/* ===================================================================================== *\
	-------------------------------------> VIDEOS <-------------------------------------
\* ===================================================================================== */
function loadVideo(vid,divID,elementID) {
	if(divID === undefined) { divID = 'ajax_div'; }
	var vars = "vid=" + vid;
	$$('a.vselect').invoke('removeClassName','vselect');
	$(elementID).addClassName('vselect');
	zbmajax("videos.php", vars, listStateChanged, divID);
}

/* ===================================================================================== *\
	---------------------------> Tell Me More - Email Signup <--------------------------
\* ===================================================================================== */
function tellMeMore(email,divID) {
	if(divID === undefined) { divID = 'ajax_div'; }
	var vars = "email=" + email;
	$('ajaxmessage').innerHTML = 'Registering your email. Please wait...<br />';
	$('tmm_form_content').hide();
	zbmajax("tellmemore.php", vars, tmmStateChanged, divID);
}
function tmmStateChanged(response,divID) {
	if(divID === undefined) { divID = 'ajax_div'; }
	var responseArray = response.split('-||-');
	var result = responseArray[0];
	var message = responseArray[1];
	
	// handle results
	if(result == 'SUCCESS') {
		window.location.href=window.location.href + '?e=1';
	} else if(result == 'ERROR') {
		$('tmm_form_content').show();
		$('ajaxmessage').innerHTML = message;
	} else if(result == 'INVALID') {
		var messages = message.split('+||+');
		$('tmm_form_content').show();
		$('email').value = messages[0]; // keep value as-is
		$('ajaxmessage').innerHTML = messages[1];
	} else {
		$('ajaxmessage').innerHTML = '<span style="color:yellow;">The AJAX request produced an error.</span>';
	}
}

