
//--------------------------------------------------------------------------------------------
//AJAX Library
//--------------------------------------------------------------------------------------------

function trimlinefeed (dataStr) {
	//alert ('Called trim');
	return dataStr.replace(/[\r\n]/g, "");
}

function createAjaxRequest () {

    //alert('Called Ajax');

    var ro;
    var browser = navigator.appName;

    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }

    return ro;
}

var http = createAjaxRequest();

function deletePhoto ( p_reply_div, p_user_id) {
    //alert('Received deletePhoto : ' + p_reply_div + ' user_id: ' + p_user_id);
	var url = 'rpc.php';
	var params = 'action=dp&reply_div='+p_reply_div+'&user_id='+p_user_id;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);
}


function checkUsername( p_reply_div, p_username) {
    //alert('Received checkUsername : ' + p_reply_div + ' username: ' + p_username);
	var url = 'rpc.php';
	var params = 'action=cku&reply_div='+p_reply_div+'&username='+p_username;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);
}

function aSpam(p_spam_div, p_article_id) {
    //alert('Received aRec Ajax request: ' + p_article_id + ' div: ' + p_reply_div);

	var x=window.confirm("Are you sure you want to report this as spam?");
	if (x) {
	var url = 'rpc2.php';
	var params = 'action=spam&reply_div='+p_spam_div+'&article_id='+p_article_id;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);
	}
}

function aVote(p_reply_div, p_article_id, p_username,p_direction) {
    //alert('Received aRec Ajax request: ' + p_article_id + ' div: ' + p_reply_div);
	var url = 'rpc2.php';
	var params = 'action=rec&reply_div='+p_reply_div+'&article_id='+p_article_id+'&username='+p_username+'&direction='+p_direction;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);
}



function aRec(p_reply_div, p_article_id, p_username) {
    //alert('Received aRec Ajax request: ' + p_article_id + ' div: ' + p_reply_div);
	var url = 'rpc.php';
	var params = 'action=rec&reply_div='+p_reply_div+'&article_id='+p_article_id+'&username='+p_username;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);
}


function ajaxPostComment(p_reply_div, p_article_id, p_comment,p_pid,p_username,p_email_address,p_website) {
    //alert('Received Ajax request: ' + p_comment);
	var url = 'rpc.php';
	var params = 'action=pc&reply_div='+p_reply_div+'&article_id='+p_article_id+'&comment='+p_comment +'&pid='+p_pid+'&username='+p_username+'&email_address='+p_email_address+'&website='+p_website;

	//alert('sending url: ' + url + params);
	http.open('POST',url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handleResponse;
	http.send(params);

}


function handleResponse() {

    //alert('handleResponse called...');

    if (http.readyState == 4) {
	//alert('Processing Ajax response');

	if (http.status == 200 ) {
	//alert('status is 200');

        	var response = http.responseText;
        	var update = new Array();

        	if(response.indexOf('|') != -1) {


            		update = response.split('|');
		
			//FORMAT of UPDATE: divtag|text
			//Process the array of passed in items and setup local variables

			var newtext = trimlinefeed(update[1]);
			var divtag = trimlinefeed(update[0]);

			//This will update the div element from rpc.php
			//alert('setting text for element: ' + update[0]);
            		document.getElementById(trimlinefeed(update[0])).innerHTML = newtext;

        	}

	} else {

		alert('Ajax problem retreiving data. This is rare. Error: '+ http.statusText);
	}

    }
}




