// AJAX & cookie retrieval

var xmlhttp;

function getCookie( c_name ){
	if( document.cookie.length > 0 ){
		c_start=document.cookie.indexOf( c_name + "=" );
		if( c_start != -1 ){
			c_start=c_start + c_name.length + 1;
			c_end=document.cookie.indexOf( ";", c_start );
			if( c_end == -1 )
				c_end=document.cookie.length;
			return unescape( document.cookie.substring( c_start, c_end ) );
		}
	}
	return "";
}

// Call XMLHttpRequest function to initiate request object 
function Ajax2(){
	var o=false;		// return object or false
	
	if( window.XMLHttpRequest ){ // if Mozilla, etc.
		o=new XMLHttpRequest();
	}
	else if( window.ActiveXObject ){ // if IE
		try {
			o=new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch( e ){
			try{
				o=new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch( e ){}
		}
	}
	return o;
}

function ajaxPostRequest( url, theData, callbackfunc ){
	xmlhttp = Ajax2();
	if( xmlhttp ){
		xmlhttp.onreadystatechange = callbackfunc;
	
		// IE6 gives an error if we abort with no pending request, so check first
		if( xmlhttp.readyState > 1 )	// if we already did a send...
			xmlhttp.abort;				// ...abort it and do a different one
			
		//alert( theData );
		
		xmlhttp.open( 'post', url, true );	// true=asynchronous
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send( theData );
	}
	else
		alert( 'Error: No request object' );
}

function ajaxResult(){
	var newData;

	if( xmlhttp.readyState == 4 ){
		if( xmlhttp.status == 200 ){
            newData = xmlhttp.responseText;

   			if( newData.substring(0,1) == '<' ){	// begins HTML tag for PHP error message
				alert( 'A PHP error occurred: ' + newData );
				return '';
			}
		 	return newData;
		}
		else{
			alert( 'Error: ' + xmlhttp.statusText );
			return '';
		}
    }
    return '';   	// data isn't ready yet
}
