
var http_request = false;


function showTopIcon(iconid)
{
	if(iconid == topicon)
	{
		return;
	}
	_getObj(topicon).style.display = "none";
	topicon = iconid;
	_getObj(topicon).style.display = "block";
}
  function show_hint ( p_hint_text, p_span ) {

    document.getElementById(p_span).innerHTML = p_hint_text ;

  }

	function getList(f, t)
	{
		makePOSTRequest(base_url + "getList.php", "f="+f + "&t=" + t, "alist");
	}
	function makeAJAX(url, method, parameters, onSuccess,onFailed)
	{
		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 XMLHTTP instance');

    		return false;

    	}

  

    	http_request.onreadystatechange = function() {
			
			//alert(http_request.readyState);
    		if (http_request.readyState == 4) {

    		if (http_request.status == 200) {
				onSuccess(http_request.responseText);
    //alert(http_request.responseText);

    //result = http_request.responseText;
//alert(result);
    //document.getElementById(SpanName).innerHTML = result;

    //document.getElementById('status').innerHTML = 'Ready'; 

    	} else {

    		alert('There was a problem with the request.');

   		}

    	}

    };

    	http_request.open(method, url, true);
		
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    		http_request.setRequestHeader("Content-length", parameters.length);

    		http_request.setRequestHeader("Connection", "close");

    		http_request.send(parameters);
		
    	
	}
  function makePOSTRequest(url, parameters, SpanName) {

    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 XMLHTTP instance');

    return false;

    }

  

    http_request.onreadystatechange = function() {

    if (http_request.readyState == 4) {

    if (http_request.status == 200) {

    //alert(http_request.responseText);

    result = http_request.responseText;
//alert(result);
	if(_getObj(SpanName))
	{
		_getObj(SpanName).innerHTML = result;
	}
    //document.getElementById(SpanName).innerHTML = result;

    //document.getElementById('status').innerHTML = 'Ready'; 

    } 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.setRequestHeader("Content-length", parameters.length);

    http_request.setRequestHeader("Connection", "close");

    http_request.send(parameters);

  }





  function Contact(obj,SpanName,DivName) {

    var curDateTime = new Date(); //For IE 

    var SpanName = SpanName; 





    if(DivName == 'tf'){

    var poststr = "sendersname=" + encodeURI( document.getElementById("sendersname").value ) +

  	  				"&sendersemail=" + encodeURI( document.getElementById("sendersemail").value ) +

  	  				"&friendsemail=" + encodeURI( document.getElementById("friendsemail").value ) +

  	  				"&about=" + encodeURI( document.getElementById("about").value ) +

  	  				"&gamename=" + encodeURI( document.getElementById("gamename").value ) +

  					  "&gameid=" + encodeURI( document.getElementById("gameid").value );

    

    //alert (SpanName); 

    makePOSTRequest(base_url + 'index.php?action=tellafriendajax', poststr, SpanName);

    }    

    if(DivName == 'tfv'){

    var poststr = "sendersname=" + encodeURI( document.getElementById("sendersname").value ) +

  	  				"&sendersemail=" + encodeURI( document.getElementById("sendersemail").value ) +

  	  				"&friendsemail=" + encodeURI( document.getElementById("friendsemail").value ) +

  	  				"&about=" + encodeURI( document.getElementById("about").value ) +

  	  				"&gamename=" + encodeURI( document.getElementById("gamename").value ) +

  	  				"&verifycode=" + encodeURI( document.getElementById("tafverifycode").value ) +

  					  "&gameid=" + encodeURI( document.getElementById("gameid").value );

    

    //alert (SpanName); 

    makePOSTRequest(base_url + 'index.php?action=tellafriendajax', poststr, SpanName);

    }    



    if(DivName == 'cf'){

    var poststr = "newcomment=" + encodeURI( document.getElementById("newcomment").value ) +

  	  				"&id=" + encodeURI( document.getElementById("id").value );

    

    //alert (SpanName); 

    makePOSTRequest(base_url + 'index.php?action=addcommentajax', poststr, SpanName);

    }

    if(DivName == 'cfv'){

    var poststr = "newcomment=" + encodeURI( document.getElementById("newcomment").value ) +

  	  				"&id=" + encodeURI( document.getElementById("id").value ) +

  					"&verifycode=" + encodeURI( document.getElementById("cverifycode").value );

    

    //alert (SpanName); 

    makePOSTRequest(base_url + 'index.php?action=addcommentajax', poststr, SpanName);

    }

   
	if(_getObj(DivName))
	{
		_getObj(DivName).style.display = 'none';
	}
   // document.getElementById(DivName).style.display = 'none';

  } 
  function rateGame(type, gameid)
  {
	  var successHandler = function(res)
	{
    	eval("response="+ res);
		alert(response.msg);
		if(response.code == "1")
		{
			//window.location.reload(true);
            var data = _getObj('ratedata').innerHTML;
            data = data.split(",");
            var t = parseInt(data[0]);
            var d = parseInt(data[1]);
            t ++;
            if(type == "digg")
            {
            	d ++;
            }
            var dp = d / t;
            var bp = 1 - dp;
            var dh = dp * 60;
            var bh = bp * 60;
            _getObj('diggShow').style.height = dh  + "px";
            _getObj('diggShow').style.marginTop = bh + "px";
            _getObj('buryShow').style.height = bh + "px";
            _getObj('buryShow').style.marginTop = dh + "px";
            dp = Math.round(dp * 10000) / 100;
            bp = Math.round(bp * 10000) / 100; 
            _getObj('diggInfo').innerHTML = dp + "%[" + d + "]";
            _getObj('buryInfo').innerHTML = bp + "%[" + (t-d) + "]";
            _getObj('ratedata').innerHTML = t + "," + d;
		}
	}
	var parameter = "";
	//alert("<?php echo _getObjbase_url;?>index.php?action=rategame&gameid=" + gameid + "&type=" + type);
	//return;
	makeAJAX("/index.php?action=rategame&gameid=" + gameid + "&type=" + type, "GET", parameter, successHandler);
  }
  function showWarning(title, msg)
  {
	  var alertObj = _getObj('alertForm');
	  _getObj('alertTitle').innerHTML = title;
	  _getObj('alertContent').innerHTML = msg;
	  var pos = getClientWH();
	  var w = parseInt(alertObj.style.width);
	  var h = parseInt(alertObj.style.height);
	  alertObj.style.left = (pos.w - w)/2 + "px";
	  alertObj.style.top = ((pos.h - h) / 2 + pos.st) + "px";
	  pmwin("open", null, 'alertForm');
  }
  function getClientWH()
  {
	  var clientWidth = document.body.clientWidth;
	  var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	  var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	  return {w:clientWidth, h:clientHeight, st:scrollTop};
  }
  function sendPrivateMsg(e,uid,uname)
  {
	  var cursorPos = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
	  var sendMsgForm = _getObj('sendMsg');
	  var clientPos = getClientWH();
	  sendMsgForm.style.top = (cursorPos[1] + clientPos.st - 20) + 'px';
	  sendMsgForm.style.left = (cursorPos[0] + 50) + 'px';
	  _getObj('tousername').value = uname;
	  sendMsgForm.style.display = 'block';
  }
  function closeSendMsg()
{
	_getObj('sendMsg').style.display = "none";
}
function makeFriend(userid)
{
	//alert(e);
	
	var successHandler = function(res)
	{
		//alert(res);
		eval("response=" + res);
		//var response = 
		//alert(response.status);
		//alert(response.status == "1");
		if(response.status == "1")
		{
			alert(response.msg);
		}
		else
		{
			showWarning("Error Ocurrened", response.msg);
		}
	}
	var parameter = "";
	makeAJAX(base_url + "index.php?action=friendrequest&userid=" + userid, "GET", parameter, successHandler);
}
	function addComment()
	{
		if(_getObj("newcomment").value == "")
		{
			alert("blank comment is not allowed!");
			return;
		}
		var commentDIV = _getObj('commentlist');
		//commentDIV.innerHTML = "<div style=\"text-align:center;\"><img src=\"./templates/st_boygames_tpl/images3/loading.gif\" /></div>";
		var poststr = "newcomment=" + encodeURI( _getObj("newcomment").value ) +

  	  				"&id=" + encodeURI( _getObj("id").value );
		var successHandler = function(res)
		{
			if(res == "<center><b>Unable To Add Comment</b></center>")
			{
				alert("Unable To Add Comment");
			}
			else
			{
				commentDIV.innerHTML = res + commentDIV.innerHTML;
				_getObj("newcomment").value = "";
			}
		}
		makeAJAX(base_url + 'index.php?action=addcommentajax', "POST", poststr, successHandler);
		
	}
	function addFavorite(url, parameters)
	{
		var successHandler = function(res)
		{
			alert(res);
		}
		makeAJAX(url, "GET", parameters, successHandler);
	}

var hiddenobj = new Array();
var pmwinposition = new Array();
function _getObj(id) {
	return document.getElementById(id);
}
var IMGDIR = "./templates/st_boygames_tpl/images3";
var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}
function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}
function pmwin(action, param, objid) {
	if(!objid)
	{
		objid = 'pmlayer';
	}
	var objs = document.getElementsByTagName("OBJECT");
	if(action == 'open') {
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].style.visibility != 'hidden') {
				objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
				objs[i].style.visibility = 'hidden';
			}
		}
		var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var pmwidth = 800;
		var pmheight = clientHeight * 0.9;
		if(!_getObj(objid)) {
			div = document.createElement('div');
			div.id = 'pmlayer';
			div.style.width = pmwidth + 'px';
			div.style.height = pmheight + 'px';
            
			div.style.left = ((clientWidth - pmwidth) / 2) + 'px';
			div.style.position = 'absolute';
			div.style.zIndex = '999';
			_getObj('append_parent').appendChild(div);
			//alert(objid);
			_getObj(objid).innerHTML = '<div style="width: 800px; background: #666666; margin: 5px auto; text-align: left">' +
				'<div style="width: 800px; height: ' + pmheight + 'px; padding: 1px; background: #FFFFFF; border: 1px solid #7597B8; position: relative; left: -6px; top: -3px">' +
				'<div onmouseover="pmwindrag(this)" style="cursor: move; position: relative; left: 0px; top: 0px; width: 800px; height: 30px; margin-bottom: -30px;"></div>' +
				'<a href="###" onclick="pmwin(\'close\')"><img style="position: absolute; right: 20px; top: 15px" src="' + base_url +'templates/st_boygames_tpl/images3/close.gif" title="close" /></a>' +
				'<div id="pmwinmask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe id="pmframe" name="pmframe" style="width:' + pmwidth + 'px;height:100%" allowTransparency="true" frameborder="0"></iframe></div></div>';
		}
		_getObj(objid).style.display = '';
		//_getObj('pmlayer').style.top = ((clientHeight - pmheight) / 2 + scrollTop) + 'px';
		//_getObj(objid).style.top = ((clientHeight - pmheight) / 2 + 0) + 'px';
		
		if(objid =='pmlayer')
		{_getObj(objid).style.top = ((clientHeight - pmheight) / 2 + scrollTop) + 'px';
			if(!param) {
				pmframe.location = base_url + 'templates/st_boygames_tpl/viewprofile.php';
			} else {
				pmframe.location = base_url + 'templates/st_boygames_tpl/viewprofile.php?' + param;
			}	
		}
		
	} else if(action == 'close') {
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].attributes['oldvisibility']) {
				objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;
				objs[i].removeAttribute('oldvisibility');
			}
		}
		hiddenobj = new Array();
		_getObj(objid).style.display = 'none';
	}
}

function closeAlert()
{
	pmwin("close",null, 'alertForm');
}
var pmwindragstart = new Array();
function pmwindrag(obj, objid, maskid) {
	if(!objid)
	{
		objid = 'pmlayer';
	}
	if(!maskid)
	{
		maskid = 'pmwinmask';
	}
	obj.onmousedown = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return false;
			}
		}
		pmwindragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		pmwindragstart[2] = parseInt(_getObj(objid).style.left);
		pmwindragstart[3] = parseInt(_getObj(objid).style.top);
		if(maskid)
		_getObj(maskid).style.display = '';
		doane(e);
	}
	_attachEvent(document.body, 'mousemove', function(e) {
		if(pmwindragstart[0]) {
			var pmwindragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
			with(_getObj(objid)) {
				//alert(_getObj(objid).style.left);
				style.left = (pmwindragstart[2] + pmwindragnow[0] - pmwindragstart[0]) + 'px';
				style.top = (pmwindragstart[3] + pmwindragnow[1] - pmwindragstart[1]) + 'px';
			}
			doane(e);
		}
	});
	obj.onmouseup = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return true;
			}
		}
		pmwindragstart = [];
		if(maskid)
		_getObj(maskid).style.display = 'none';
		doane(e);
	}
	obj.onmouseover = null;
}
function checkAndSendMsg()
{
	_getObjsubject = _getObj('messagesubject').value;
	_getObjto = _getObj('tousername').value;
	_getObjmsgbody = _getObj('messagebody').value;
	if(_getObjsubject == "")
	{
		_getObjsubject = 'Untiled';
	}
	if(_getObjto == "")
	{
		alert("contact username can't be empty!");
		return;
	}
	if(_getObjmsgbody == "")
	{
		alert("You must input some words!");
		return false;
	}
	var handlerSuccess = function(res)
	{
		eval("response=" + res);
		if(response.code != 1)
		{
			showWarning("Send Message Failed", response.message);
		}
		else
		{
			pmwin('close',null,'sendMsg');
			alert(response.message);
		}
	}
	var parameters = "messagesubject=" + _getObjsubject
					+ "&tousername=" + _getObjto
					+ "&messagebody="+_getObjmsgbody;
	makeAJAX(base_url + "index.php?action=sendmessage","POST", parameters, handlerSuccess);
}
function showPic(e, num)
{
	if(window.event)
	{
		e = window.event;
	}
	var obj = _getObj("img" + num);
	obj.style.position = "absolute";
	var clientPos = getClientWH();
	obj.style.top = (e.clientY + clientPos.st - 30) + "px";
	obj.style.left = (e.clientX + 20) + "px";
	_getObj("img" + num).style.display = "block";
}
function hidePic(num)
{
	_getObj("img" + num).style.display = "none";
}// JavaScript Document