/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | phpBBToWeb20                                                         |
// +----------------------------------------------------------------------+
// | Author:  Dmitry Sheiko <sheiko@cmsdevelopment.com>	                  |
// | Copyright (c) 2007 Dmitry Sheiko               					  |
// | www.cmsdevelopment.com 			               					  |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

// Short name of function
function $(divName) {
	return document.getElementById(divName);
}

/** 
* Browser indentification
*/ 				
if(document.implementation && document.implementation.createDocument) var isMozilla=true;
else var isMozilla=false;

// Get post habra-voting panel
function requestPP(post_id) {
	serverRequest("pp_controller.php", "call=get&post_id="+post_id, processPPDelivery, post_id);
}
// Vote
function votePP(post_id, vote_action) {
	serverRequest("pp_controller.php", "call="+vote_action+"&post_id="+post_id, processPPDelivery, post_id);
}
// Get karma
function getKarma(username) {
	serverRequest("pp_controller.php", "call=getkarma&user_name="+username, processPPDelivery, username);
}

// Processing 
var processPPDelivery= function(obj, post_id) { 
	if(obj.responseText.substr(0,1)=="{") {
		var respondStructure = eval( '(' + obj.responseText + ')' ); 
		if(respondStructure.Body) 
			$("ppanel_"+post_id).innerHTML=respondStructure.Body;
	}
}	

/** 
* Create Request Object for various platforms
*/ 				

function createRequestObject() {
    var request = null;
    if(!request) try {
        request=new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e){}
    if(!request) try {
        request=new ActiveXObject('Microsoft.XMLHTTP');
    } catch (e){}
    if(!request) try {
        request=new XMLHttpRequest();
    } catch (e){}
    return request;
}  

/** 
* Make server request
* 
* @param POST-request performing
* @param url  - Request address
* @param data - Parameters as a string
* @param  callback - (facultative) a callback-function
*/ 				

function serverRequest(url, data, callback, callback_argument) {
    var request = createRequestObject();
    if(!request) return false;
    request.onreadystatechange  = function() { 
            if(request.readyState == 4 && callback) callback(request, callback_argument);
    };

    request.open('POST', url, true);
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	try {
		request.send(data);
	} catch (e) {
		alert('The server does not respond');
	}
    return true;
} 