// $Id: common.js,v 1.00 2009/12/03 15:15:29 i.hrustalev Exp $
function gebi(id) {
	return document.getElementById(id);
}

function refresh() {
	window.location.reload(true);
}

function refresh_iframe(id){
	document.getElementById(id).src += '';
}

function showPunishmentInfo(nick) {
	var url = "/punishment_info.php?nick="+nick;
	window.open(url, "", "width=730,height=550,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");

}

function showArtifactInfo(artifact_id,artikul_id,set_id) {
	var url = "/artifact_info.php";
	if (artifact_id) url += "?artifact_id="+artifact_id;
	else if (artikul_id) url += "?artikul_id="+artikul_id;
	else if (set_id) url += "?set_id="+set_id;
	else return;
	window.open(url, "", "width=730,height=550,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showGiftInfo(gift_id) {
	var url = "/gift_info.php?gift_id="+gift_id;
	window.open(url, "", "width=730,height=550,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}


function showUserInfo(nick) {
	var url = "/user_info.php?nick="+nick;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showUrlWindow(url) {
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showBotInfo(bot_artikul_id, fight_bot_id, fight_id) {
	var url = "/bot_info.php?";

	if (bot_artikul_id) {
		url += "artikul_id="+bot_artikul_id;
	} else {
		url += "fight_id="+fight_id+"&fight_bot_id="+fight_bot_id;
	}

	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showFightInfo(fight_id) {
	var url = "/fight_info.php?fight_id="+fight_id;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showBgInfo(instance_id) {
	var url = "/bg_info.php?instance_id="+instance_id;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showSlaughterInfo(instance_id) {
	var url = "/slaughter_info.php?instance_id="+instance_id;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showClanInfo(clan_id) {
	var url = "/clan_info.php?clan_id="+clan_id;
	window.open(url, "", "width=920,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showAchievementInfo(achievement_id) {
	var url = "/achievement_info.php?id="+achievement_id;
	window.open(url, "", "width=730,height=550,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showCompanionInfo(companion_id) {
	var url = "/companion_info.php?companion_id="+companion_id;
	window.open(url, "", "width=720,height=500,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function getCoords(obj){
	var o=typeof(obj) == 'string' ? gebi(obj) : obj;
	var ret={'l':o.offsetLeft,'t':o.offsetTop,'w':o.offsetWidth,'h':o.offsetHeight};
	while(o=o.offsetParent){
		ret.l+=o.offsetLeft;
		ret.t+=o.offsetTop;
	}
	return ret;
}

function common_is_email_valid(email,all) {
	if (!email && !all) {
		return true;
	}
	var re = '';
	if (all) {
		re = /^([A-z0-9_\-]+\.)*[A-z0-9_\-]+@([A-z0-9][A-z0-9\-]*[A-z0-9]\.)+[A-z]{2,5}$/i;
	} else {
		re = /^([A-z0-9_\-]+\.)*[A-z0-9_\-]+(@)?([A-z0-9][A-z0-9\-]*[A-z0-9]\.)*(\.)?[A-z]{0,5}$/i;
	}
	if (!re.test(email)) {
		return false;
	}
	return true;
}

function common_is_mailru_email(email) {
	if (!email && !all) {
		return true;
	}
	var re = '';
	re = /^([A-z0-9_\-]+\.)*[A-z0-9_\-]+@(mail|list|bk|inbox|corp\.mail)\.ru$/i;
	if (!re.test(email)) {
		return false;
	}
	return true;
}


function disableScroll(obj) {
	if (!obj) return;
	var evtFunc = function (event) {
		var event = window.event || event;
		if (event.preventDefault) {
			event.preventDefault();
		}
		return false;
	}
	
	var mouseWheelEvt = (navigator.product == 'Gecko' ? "DOMMouseScroll" : "mousewheel");	 
	if (obj.attachEvent) {
		obj.attachEvent("on" + mouseWheelEvt, evtFunc);
	} else if (obj.addEventListener) {
		obj.addEventListener(mouseWheelEvt, evtFunc, false)
	}
}

// center panel
function centerPanel(o) {
var bodyH = (document.documentElement.clientHeight || document.body.clientheight);
	var panelH = gebi(o);
	var panel = gebi(o);
	panelH = panelH.offsetHeight;
	if (bodyH > panelH) {
		panel.style.marginTop = (bodyH - panelH) / 2 + 'px';
	}
	panel.style.visibility = 'visible';
}

function isInClient() {
  return window.navigator.userAgent.indexOf("Client/") > 0;
}

function resizeContainerH(h) {
	obj = gebi('container');
	obj.style.height = h ? (h + 'px') : 'auto';
}

function clientReceive(data, swf_name) {
	var swf_name = (swf_name) ? swf_name : 'SwfMain'; 
	var obj = (navigator.appName.indexOf("Microsoft") != -1) ? window[swf_name] : window.document[swf_name];

	if (obj && obj.ClientTransfer) {
		obj.ClientTransfer(data);
	}
}


var in_instance = false;

function saveInstanceState(value)
{
	in_instance = value;
}

function isInInstance()
{
	return in_instance;
}


var client_exchange_store;

function clientExchangePut(text) {
	if (!isInClient()) return false
	if(!client_exchange_store) {
		client_exchange_store = new Array();
	}
	return client_exchange_store.push(text);
}

function clientExchangeGet() {
	if (!isInClient()) return false;
	if(client_exchange_store && client_exchange_store.length) {
		return client_exchange_store.shift();
	}
	return null;
}

function doPost(actionUrl, params) {
	var newF = document.createElement("form");
	newF.action = actionUrl;
	newF.method = 'POST';
	var parms = params.split('&');
	for (var i=0; i<parms.length; i++) {
	  var pos = parms[i].indexOf('=');
	  if (pos > 0) {
		   var key = parms[i].substring(0,pos);
		   var val = parms[i].substring(pos+1);
		   var newH = document.createElement("input");
		   newH.name = key;
		   newH.type = 'hidden';
		   newH.value = val;
		   newF.appendChild(newH);
	  }
	}
	document.getElementsByTagName('body')[0].appendChild(newF);
	newF.submit();
}

function premiumReload(){
	top.frames[0].frames[5].location.reload();
}
