function utilCreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function utilReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function utilEraseCookie(name) {
	utilCreateCookie(name,"",-1);
}

//This method will format a millisecond object and return a time format in
//HH:MM:SS
function formatTimeString(iMilliIn, spanEleToUpdate) {
	var crHours = Math.floor(iMilliIn/(3600000));
	var crMinutes = Math.floor((iMilliIn-(crHours*3600000))/(60000));
	var crSeconds = Math.floor((iMilliIn-(crHours*3600000)-(crMinutes*60000))/1000);
	spanEleToUpdate.innerHTML = (crHours < 10 ? '0'+crHours : crHours) + ':' + (crMinutes < 10 ? '0'+crMinutes : crMinutes) + ':' + (crSeconds < 10 ? '0'+crSeconds : crSeconds);
}


//======================================================
//	AJAX Functions Below
//======================================================

var xmlHttpReq;
var strAppServerURL = '';
var strMethodSilent = 'JS_SILENT';

//Define app server
/*
var prefixRegExp = /www./;
if (string1.search(prefixRegExp)) {
	strAppServerURL = 'http://www.barplow.com/';
	//strAppServerURL = 'http://www.chriscrew.com/';
}
else {
	strAppServerURL = 'http://barplow.com/';
	//strAppServerURL = 'http://chriscrew.com/';
}
*/
//alert(window.location);
//alert(strAppServerURL);

function doAjaxPost(strUrlIn, strPostParamsIn) {
	try {
		if (window.XMLHttpRequest)
		{
			//code for IE7+, Firefox, Chrome, Opera, Safari
			xmlHttpReq=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			// code for IE6, IE5
			xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
		}
		var timestamp = new Date();
		strPostParamsIn = strPostParamsIn + '&ts=' + timestamp.getTime();
		xmlHttpReq.open("POST", strAppServerURL+strUrlIn, true);
		xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttpReq.onreadystatechange = ajaxCallback;
		//alert('Sending to URL: '+strAppServerURL+strUrlIn +'* With Post Params: '+strPostParamsIn);
		xmlHttpReq.send(strPostParamsIn);
	}
	catch (e) {
		alert('There was an AJAX error connecting to the server.');
		alert(e);
	}
}
function ajaxCallback() {
    if (xmlHttpReq.readyState == 4) {
        if (xmlHttpReq.status == 200) {
        	try {
        		var jsonAjaxRtn;
        		eval('jsonAjaxRtn = ('+xmlHttpReq.responseText+')');
        		//alert(xmlHttpReq.responseText);
        		var jsMethod = jsonAjaxRtn.serverStatus.jsMethod;
        		//alert(jsMethod);
        		if (jsMethod == strMethodSilent) {
        			//if (!isServerStatusOk(jsonAjaxRtn)) showPIErrorDialog('An error occured on the server while processing your request.');
        		}
        		else {
        			//First check if for stream publishing
        			try {eval(jsMethod+'(jsonAjaxRtn);');}
        			catch(e) {
        				alert(e);
        			}
        		}
        	}
        	catch (e) {
        		alert('Ajax Error: '+e);
        		//showPIErrorDialog('There was an error processing the ajax request.\n'+e);
        	}
        }
    }
}

function unknownMethod(jsonAjaxIn) {
	//Alert user of method error
	alert('Error:\n\nUnknown ajax method: \''+jsonAjaxIn.serverStatus.ajaxMethod+'\'.');
}

