var da = "d";
var ha = "h";
var ma = "m";
var sa = "s";
var newwindow = '';

function secondsToTime(seconds) {
	var days = Math.floor(seconds / 86400);
	var aux = seconds % 86400;
	var hours=Math.floor(aux / 3600);
	aux = aux % 3600;
	var minutes = Math.floor(aux / 60);
	aux = aux % 60;
	if(days) {
		if(hours < 10) hours = "0" + hours;
		if(minutes < 10) minutes = "0" + minutes;
		if(aux < 10) aux = "0" + aux;
		return days + da + " " + hours + ha + " " + minutes + ma + " " + aux + sa;
	} else {
		if(hours) {
			if(minutes < 10) minutes = "0" + minutes;
			if(aux < 10) aux = "0" + aux;
			return hours + ha + " " + minutes + ma + " " + aux + sa;
		} else {
			if(minutes) {
				if(aux < 10) aux = "0" + aux;
				return minutes + ma + " " + aux + sa;
			} else {
				return aux + sa;
			}
		}
	}
}
 
function popup(width, height, url) {
	var agt = navigator.userAgent.toLowerCase();
	if(agt.indexOf("msie") != -1 && agt.indexOf("opera") == -1 && agt.indexOf("msie 6.") != -1)  {
		if(top.main) top.main.location.href = url;
		else document.location.href = url;
		return;
	}
	
	if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = url;
	} else {
		newwindow = window.open(url,'name','height='+height+',width='+width+', scrollbars=1');
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) newwindow.focus();
}

function resizePopup(w, l) {
	if(window.name == "name") window.resizeTo(w, l);
}

function showHide(id) {
	var thing = document.getElementById(id).style;
	if(thing.display == '') thing.display = 'none';
	else thing.display = '';
}

function changeDisplay(id, show) {
	var thing = document.getElementById(id).style;
	if(show) thing.display = '';
	else thing.display = 'none';
}

function getText(id) {
	var str = document.getElementById(id).innerHTML;
	while(str.indexOf('.') != -1)
		str = str.replace('.', '');
	return (str * 1);
}

function setText(id, val) {
	//alert(id + " " + val);
	var box = document.getElementById(id);
	val = val + "";
	//var size = val.length;
	val = val.replace('.', ',');
	var size = val.lastIndexOf(',');
	if(size < 0) size = val.length;
	
	var res = "";
	
	for(var i = size - 1; i >= 0; i--) {
		if(((size-i) % 3) - 1 == 0) res = "." + res;
		res = val.substring(i, i + 1) + res;
	}
	if(res[0] == '-' && res[1] == '.') {
		res = "-" + res.substring(2, res.length);
	}

	res = res.substring(0, res.length-1);
	if(size != val.length) {
		var last;
		if(size + 4 < val.length) last = size + 4;
		else last = val.length;
		res = res + val.substring(size, last);
	}
	box.innerHTML = res;
	//res = res.substring(0, res.length-1);
	//document.getElementById(id).innerHTML = res;
}

function setRealText(id, res) {
	document.getElementById(id).innerHTML = res;
}

function getValue(id) {
	val = Math.floor(document.getElementById(id).value * 1);
	if(isNaN(val) || val < 1) return 0;
	return val;
}

function setValue(id, val) {
	document.getElementById(id).value = val;
}

function setStyle(id, style) {
	document.getElementById(id).className = style;
}

//Retorna o width e height da janela do browser
function returnSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return new Array(myWidth, myHeight);
}

function needSilver() {
	alert("A funcionalidade pedida só pode ser usada por membros Silver ou Gold.\nAdquira um destes pacotes para desbloquear esta opção.");
	return false;
}

var xmlHttp, helpother;
function MakeXmlHttpObject(){
	var xmlHttp=null;
	try{ //Para browsers de jeito: Firefox, Opera, Safari e derivados.
		xmlHttp = new XMLHttpRequest();
	} catch (e){ //IE
		try{ //IE6+
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){ //IE6-
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function showHelp(msg) {
	changeDisplay("loading" + helpother, 0);
	setRealText(("helpt" + helpother), msg);
	changeDisplay("helpt" + helpother, 1);
}

function helpDone() {
	if(xmlHttp.readyState == 4) showHelp(xmlHttp.responseText);
}

function getHelp(str, o, a, l){
	xmlHttp = MakeXmlHttpObject();
	var url = "../include/ajax.php?t=1&h=" + str + "&o=" + o + "&a=" + a + "&l=" + l;	
	helpother = o;
	if (xmlHttp == null){
		showHelp("Browser incompativel com Ajax!");
		window.open(url,'name','height=300,width=500, scrollbars=1');
		return;
	}
	xmlHttp.onreadystatechange = helpDone;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}