//modified by bobsuk 20090625
var debugMode = false;

//var URL = "ajax.xml.php";
var URL = "tmp/ajax/multi_tag_group.xml";

var xmlHttpGetData = createXmlHttpRequestObject();
//var cache = new Array();

/* creates an XMLHttpRequest instance */
//-----------------------------------------------
function createXmlHttpRequestObject() 
{
	var xmlHttp;// will store the reference to the XMLHttpRequest object
		try // this should work for all browsers except IE6 and older
		{
			xmlHttp = new XMLHttpRequest();// try to create XMLHttpRequest object
		}
		catch(e)
		{// assume IE6 or older
			var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
			for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) // try every prog id until one works
			{
				try 
				{ 
					xmlHttp = new ActiveXObject(XmlHttpVersions[i]);// try to create XMLHttpRequest object
				}
				catch (e) {}
			}
		}
	// return the created object or display an error message
	if (!xmlHttp)	alert("Error creating the XMLHttpRequest object.");
	else return xmlHttp;
}//function createXmlHttpRequestObject() 
//-----------------------------------------------

function makeRequest(mode,a1,a2,a3)
{
	params =	"mode=" + mode +
				"&a1=" + encodeURIComponent(a1) + 
				"&a2=" + encodeURIComponent(a2) + 
				"&a3=" + encodeURIComponent(a3);
	return params;
	//cache.push(params);
}//function makeRequest(mode,a1,a2,a3)

function addParam(field,val)
{
	return field+"="+encodeURIComponent(val);
}//function addParam(mode,a1,a2,a3)

//-----------------------------------------------------------------------------
/* makes asynchronous request to retrieve new messages, post new messages, delete messages */
function requestFromServ(url,params,handle)
{
	if(xmlHttpGetData)// only continue if xmlHttpGetData isn't void
	{
		try
		{
			// don't start another server operation if such an operation 
			// is already in progress 
			if (xmlHttpGetData.readyState == 4 || 
					xmlHttpGetData.readyState == 0) 
			{
				//alert('params='+params);
				xmlHttpGetData.open("POST", url, true);
				xmlHttpGetData.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttpGetData.onreadystatechange = eval(handle);
				xmlHttpGetData.send(params);
			}
			else
			{
				// we will check again for new messages 
				//if (!stopChatRequests)
				//setTimeout("requestData();", updateInterval);
			}
		}
		catch(e)
		{
			displayError(e.toString());
		}
		return true;
	}
}//function requestFromServ()
//-----------------------------------------------------------------------------
function GETrequestFromServ(url,params,handle)
{
	if(xmlHttpGetData)// only continue if xmlHttpGetData isn't void
	{
		try
		{
			// don't start another server operation if such an operation 
			// is already in progress 
			if (xmlHttpGetData.readyState == 4 || 
					xmlHttpGetData.readyState == 0) 
			{
				//alert('params='+params);
				xmlHttpGetData.open("GET", url+"?"+params, true);
				xmlHttpGetData.onreadystatechange = eval(handle);
				xmlHttpGetData.send(null);
			}
			else
			{
				// we will check again for new messages 
				//if (!stopChatRequests)
				//setTimeout("requestData();", updateInterval);
			}
		}
		catch(e)
		{
			displayError(e.toString());
		}
		return true;
	}
}//function requestFromServ()
//-----------------------------------------------------------------------------


//DOLLARS PICTURES

/* function that handles the http response when updating messages */
function handleReceivingPictires() 
{
	if (xmlHttpGetData.readyState == 4) // continue if the process is completed
	{
		if (xmlHttpGetData.status == 200) // continue only if HTTP status is "OK"
		{
			try // process the server's response
			//----------------------------------
			{ readPictures(); }
			//----------------------------------
			catch(e) // display the error message
			{ displayError(e.toString()); }
		}
		else // display the error message
		{ displayError(xmlHttpGetData.statusText); }
	}
}//function handleReceivingData() 

//-----------------------------------------------


/* function that processes the server's response when updating messages */
function readPictures()
{  
	// retrieve the server's response 
	var response = xmlHttpGetData.responseText;
	// server error?
	if (response.indexOf("ERRNO") >= 0 
		|| response.indexOf("error:") >= 0
		|| response.length == 0)
		throw(response.length == 0 ? "Void server response." : response);
	// retrieve the document element
	response = xmlHttpGetData.responseXML.documentElement;
	//getdata
	pidArr = response.getElementsByTagName("pid");
	didArr = response.getElementsByTagName("did");
	pictureArr = response.getElementsByTagName("picture");
	summaArr = response.getElementsByTagName("summa");
	//display data
	displayPictures(pidArr, didArr, pictureArr, summaArr);
} //function readData()

//-----------------------------------------------------------------------------

function  displayPictures(pidArr, didArr, pictureArr, summaArr)
{
	var odd=true;
	var htmlMessage = "";
	for(var i=0; i<pidArr.length; i++)
	{
		// get the message details
		var pid = pidArr.item(i).firstChild.data.toString();
		var did = didArr.item(i).firstChild.data.toString();
		var picture = trim(pictureArr.item(i).firstChild.data.toString());
		var summa = summaArr.item(i).firstChild.data.toString();
		htmlMessage += "<div class='paycheck'>";
		htmlMessage += "<table width=100% cellpadding=0 cellspacing=0 border=0><tr><td>&nbsp;</td></tr></table>";
		htmlMessage += "<center><a href='imgs/dollars/"+picture+"'  rel='lightbox[pictures]'  title=''>";
		htmlMessage += "<img src='imgs/dollars/thumb/"+picture+"' border='0' title='Показать PAYCHECK' alt='Показать PAYCHECK'/></a>";
		htmlMessage += "</center></div>";
		odd=!odd;
	}
	showData(htmlMessage);
}//function  displayPictures(pidArr, didArr, pictureArr, summaArr)


//DOLLARS STORY

/* function that handles the http response when updating messages */
function handleReceivingStory() 
{
	if (xmlHttpGetData.readyState == 4) // continue if the process is completed
	{
		if (xmlHttpGetData.status == 200) // continue only if HTTP status is "OK"
		{
			try // process the server's response
			//----------------------------------
			{ readStory(); }
			//----------------------------------
			catch(e) // display the error message
			{ displayError(e.toString()); }
		}
		else // display the error message
		{ displayError(xmlHttpGetData.statusText); }
	}
}//function handleReceivingStory() 

//-----------------------------------------------


/* function that processes the server's response when updating messages */
function readStory()
{  
	// retrieve the server's response 
	var response = xmlHttpGetData.responseText;
	// server error?
	if (response.indexOf("ERRNO") >= 0 
		|| response.indexOf("error:") >= 0
		|| response.length == 0)
		throw(response.length == 0 ? "Void server response." : response);
	// retrieve the document element
	response = xmlHttpGetData.responseXML.documentElement;
	//getdata
	descrArr = response.getElementsByTagName("descr");
	//display data
	displayStory(descrArr);
} //function readStory()

//-----------------------------------------------------------------------------

function  displayStory(descrArr)
{
	var odd=true;
	var htmlMessage = "";
	for(var i=0; i<descrArr.length; i++)
	{
		// get the message details
		var descr = trim(descrArr.item(i).firstChild.data.toString());
		htmlMessage += descr;
		odd=!odd;
	}
	showData(htmlMessage);
}//function  displayStory(pidArr, didArr, pictureArr, summaArr)


//-----------------------------------------------------------------------------
// function that displays an error message
function displayError(message)
{
  // display error message, with more technical details if debugMode is true
  showData("Error accessing the server! "+
                 (debugMode ? "<br/>" + message : ""));
}

//-----------------------------------------------------------------------------
/* removes leading and trailing spaces from the string */
function trim(s)
{
    return s.replace(/(^\s+)|(\s+$)/g, "");
}


//-----------------------------------------------------------------------------
//var gid1="test"; // output element's id
function showData(html)
{
	var mydiv=_gid(gid);
	mydiv.innerHTML=html;
}
//------------------------------------------------------------------------------
// vote.bs.inc.php
//------------------------------------------------------------------------------
function doVote(postfix)
{
	var table="contest_vote";
	var vo;
	//if(postfix == undefined) var postfix = '';
	for (var i=1;i<6 ;i++ )
	{
		if(postfix != undefined){
		vo=_gid('vote'+i+postfix);
		}else vo=_gid('vote'+i);
		if (vo.checked)
		{
			var params='';
			//requestFromServ('/vote.xml.php',addParam('table',table)+"&"+addParam('rate',vo.value)+"&"+addParam('cid',cid)+"&"+addParam('bid',bid)+"&"+addParam('lid',lid)+"&"+addParam('gid',gid)+"&"+addParam('uid',uid)+"&"+addParam('uid2',uid2),'handle_vote_data');
			requestFromServ('/vote.xml.php',addParam('table',table)+"&"+addParam('rate',vo.value)+"&"+addParam('cid',_gid('cid'+postfix).value)+"&"+addParam('bid',_gid('bid'+postfix).value)+"&"+addParam('lid',_gid('lid'+postfix).value)+"&"+addParam('gid',_gid('gid'+postfix).value)+"&"+addParam('uid',_gid('uid'+postfix).value)+"&"+addParam('uid2',_gid('uid2'+postfix).value)+"&"+addParam('postfix',postfix),'handle_vote_data');
		}
	}
}
function doVoteMulti()
{
	var postfix = _gid('postfix').value;
	var table="contest_vote";
	var vo;
	//if(postfix == undefined) var postfix = '';
	for (var i=1;i<6 ;i++ )
	{
		if(postfix != undefined){
		vo=_gid('vote'+i+postfix);
		}else vo=_gid('vote'+i);
		if (vo.checked)
		{
			var params='';
			//requestFromServ('/vote.xml.php',addParam('table',table)+"&"+addParam('rate',vo.value)+"&"+addParam('cid',cid)+"&"+addParam('bid',bid)+"&"+addParam('lid',lid)+"&"+addParam('gid',gid)+"&"+addParam('uid',uid)+"&"+addParam('uid2',uid2),'handle_vote_data');
			requestFromServ('/vote.xml.php',addParam('table',table)+"&"+addParam('rate',vo.value)+"&"+addParam('cid',_gid('cid'+postfix).value)+"&"+addParam('bid',_gid('bid'+postfix).value)+"&"+addParam('lid',_gid('lid'+postfix).value)+"&"+addParam('gid',_gid('gid'+postfix).value)+"&"+addParam('uid',_gid('uid'+postfix).value)+"&"+addParam('uid2',_gid('uid2'+postfix).value)+"&"+addParam('postfix',postfix),'handle_vote_data_multi');
		}
	}
}

function Votemulti(btn,cid,uid,uid2,lid,postfix){
	var vote = _gid('vote_bodyx');
	vote.style.top = GetTop(btn)+18;
	vote.style.left = GetLeft(btn);
	/*if(vote.style.display == "block"){
		vote.style.display = "none";	
	}else 	*/
		vote.style.display = "block";
	_gid("vote_body"+postfix).style.display = "block";
	_gid("vote_msg"+postfix).style.display = "none";
	_gid('cid'+postfix).value = cid;
	_gid('uid'+postfix).value = uid;
	_gid('uid2'+postfix).value = uid2;
	_gid('lid'+postfix).value = lid;
	_gid('postfix').value = postfix;
}

function handle_vote_data() 
{
	if (xmlHttpGetData.readyState == 4) // continue if the process is completed
	{
		if (xmlHttpGetData.status == 200) // continue only if HTTP status is "OK"
		{
			try // process the server's response
			//----------------------------------
			{ vote() }
			//----------------------------------
			catch(e) // display the error message
			{ displayError(e.toString()); }
		}
		else // display the error message
		{ displayError(xmlHttpGetData.statusText); }
	}
} 
function handle_vote_data_multi() 
{
	if (xmlHttpGetData.readyState == 4) // continue if the process is completed
	{
		if (xmlHttpGetData.status == 200) // continue only if HTTP status is "OK"
		{
			try // process the server's response
			//----------------------------------
			{ vote_multi() }
			//----------------------------------
			catch(e) // display the error message
			{ displayError(e.toString()); }
		}
		else // display the error message
		{ displayError(xmlHttpGetData.statusText); }
	}
} 
function vote()
{  
	// retrieve the server's response 
	var response = xmlHttpGetData.responseText;
	// server error?
	if (response.indexOf("ERRNO") >= 0 
		|| response.indexOf("error:") >= 0
		|| response.length == 0)
		throw(response.length == 0 ? "Void server response." : response);
	// retrieve the document element
	response = xmlHttpGetData.responseXML.documentElement;
	//getdata
	var result = response.getElementsByTagName("result");
	var msg = response.getElementsByTagName("msg");
	var postfix = response.getElementsByTagName("postfix");
	if(result.item(0).firstChild.data.toString()=="yes")
	{
		_gid('vote_msg'+postfix.item(0).firstChild.data.toString()).innerHTML = msg.item(0).firstChild.data.toString();
		_gid('vote_body'+postfix.item(0).firstChild.data.toString()).style.display = 'none';
	}else {
		_gid('vote_msg'+postfix.item(0).firstChild.data.toString()).innerHTML = msg.item(0).firstChild.data.toString();
	}
}
function vote_multi()
{  
	// retrieve the server's response 
	var response = xmlHttpGetData.responseText;
	// server error?
	if (response.indexOf("ERRNO") >= 0 
		|| response.indexOf("error:") >= 0
		|| response.length == 0)
		throw(response.length == 0 ? "Void server response." : response);
	// retrieve the document element
	response = xmlHttpGetData.responseXML.documentElement;
	//getdata
	var result = response.getElementsByTagName("result");
	var msg = response.getElementsByTagName("msg");
	var postfix = response.getElementsByTagName("postfix");
	var lid = response.getElementsByTagName("lid");
	if(result.item(0).firstChild.data.toString()=="yes")
	{
		_gid('btn_'+lid.item(0).firstChild.data.toString()).style.display = "none";
		var votemsg = _gid('vote_msg'+postfix.item(0).firstChild.data.toString());
		votemsg.style.display = "block";
		votemsg.innerHTML = msg.item(0).firstChild.data.toString();
		_gid('vote_body'+postfix.item(0).firstChild.data.toString()).style.display = 'none';
	}else {
		_gid('vote_msg'+postfix.item(0).firstChild.data.toString()).innerHTML = msg.item(0).firstChild.data.toString();
	}
}
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
