function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/* This takes a URL-form-encoded string and returns and associative array of name/value pairs like we do in Perl */

function parseResponse(responseString)
{
	var dataHash = new Array();
	var pairsArray = new Array();
	pairsArray = responseString.split('&');	// split the string on the &
	
	// Loop through all the name=value pairs; split them apart; then unescape them
	var i = 0;
	for (i = 0; i < pairsArray.length; i++)
	{
		var pieceArray = new Array(2);		// holds 2 elements: names and values
		pieceArray = pairsArray[i].split('=');	// split the name=value pairs
		pieceArray[0] = unescape(pieceArray[0]);	// convert the URL-encoded characters
		pieceArray[1] = unescape(pieceArray[1]);	// convert the URL-encoded characters
		dataHash[pieceArray[0]] = pieceArray[1];	// build the associative array
	}

	return dataHash;
}

function packageMessage(nameValues)
{
	// Loop through all the array elements; escape them; assemble them into a string
	var fieldname;
	var packagedStr = '';
	for (fieldname in nameValues)	//loop through all the array names
	{
		fieldname = escape(fieldname);			// escape the field name
		var fieldvalue = escape(nameValues[fieldname]);	// get the value and escape it
		packagedStr += fieldname + "=" + fieldvalue + "&";	// assemble the name/value pairs
	}
	packagedStr = packagedStr.substring(0,packagedStr.length-1);	//chop the last character off (trailing &)

	return packagedStr;
}


/* Post a message using AJAX */

function postMessage(url,msg,resultfunc)
{
	var http_request = false;

	if (window.XMLHttpRequest)	// Mozilla, Safari,...
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){http_request.overrideMimeType('text/xml');}
	}
	else if (window.ActiveXObject)	// IE
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
            }
	}

	if (!http_request)
	{
		alert('Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function() 
		{
			if (http_request.readyState == 4)
			{
				if (http_request.status == 200)
				{
					var forminput = new Array();
					forminput = parseResponse(http_request.responseText);
					eval(resultfunc + '(forminput);');
            		}
            		else
            		{
            		    alert('There was a problem with the request.');
            		}
			}
		};
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.send(msg);
}

function processResponse(http_request)
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			var forminput = new Array();
			forminput = parseResponse(http_request.responseText);
            }
            else
            {
                alert('There was a problem with the request.');
            }
	}
}
