/*******************************************************************************
 * default.js :: Funciones Javascript variadas
 *
 * Ugamela is under Proprietary license
 * Copyright 2008 by German Perugorria perberos@gmail.com All Rights Reserved
 ******************************************************************************/

// Si usted esta leyendo esto, significa que sabe leer.
function build_timeout(){

	var build = document.getElementById('build');
	var time_total = document.getElementById('time_total');

	if(tiemporest<0){
		//Kaboom :P
		build.parentNode.parentNode.removeChild(build.parentNode);
		if(!document.getElementById('build')){
			self.location = PHP_SELF;
		}

	}else{
		//HTMLのinnerHTML
		build.innerHTML = pretty_time(tiemporest);
		time_total.innerHTML = pretty_time(timetotal);
	}

	tiemporest = tiemporest-1;
	timetotal = timetotal-1;

	window.setTimeout("build_timeout();",999);

}

function travel_timeouts(){

	have = false;

	for (i = 1; i < thearray.length; i++){

		var counter_time = document.getElementById('counter_time'+i);

		if(thearray[i]>0){
			thearray[i] = thearray[i]-1;
			counter_time.innerHTML = pretty_time(thearray[i]);
			have = true;
		}else{
			counter_time.innerHTML = '-';
		}

	}

	if(have!=false){window.setTimeout("travel_timeouts();",999);}

}

function pretty_time(seconds){
	//Divisiones, y resto. Gracias Prody
	day = Math.floor(seconds / (24*3600));
	hs = Math.floor(seconds / 3600 % 24);
	min = Math.floor(seconds  / 60 % 60);
	seg = Math.floor(seconds / 1 % 60);

	time = '';//la entrada del time
	if(day != 0){ time += day+'d ';}
	if(hs != 0){ time += hs+'h ';}
	if(min != 0){ time += min+'m ';}
	time += seg+'s';

	return time;//regresa algo como "[[[0d] 0h] 0m] 0s"
}
//numero lindo :3
function pretty_number(n){
	var arr = new Array('0'), i=0;

	n = Math.floor(n);

	while (n>0){
		arr[i] = '' + n % 1000;
		n = Math.floor(n/1000);
		i++;
	}

	arr = arr.reverse();
	for (var i in arr)
		if(i>0)//padding zeros
			while(arr[i].length<3)
				arr[i]='0'+arr[i];

	return arr.join('.');
}

function hideId(id,level){
	document.getElementById(id).style['display'] = 'none';
}
function showId(id,level){
	document.getElementById(id).style['display'] = '';
}

function toggleId(id){

	if (document.getElementById(id).style['display']) {
		showId(id, 0);
	} else {
		hideId(id, 0);
	}


}

function ug_highlight3(name) {
	if (Name(name).type == 'radio') {
		y = 250;
		for (x=1; x <= 7; x++) {

			for (z=1; z <= 6; z++) {
				setTimeout("Name('"+name+"').parentNode.parentNode.style.border='"+z+"px dashed #8ae234';", y + 250 + z * 75);
				setTimeout("Name('"+name+"').parentNode.parentNode.style.border='"+(7-z)+"px dashed #8ae234';", y + 500 + z * 75);

			}
			setTimeout("Name('"+name+"').parentNode.parentNode.style.border='';", y + 1250);
			y += 1250;
		}
	}
}

function ug_highlight2(name) {
	if (Name(name).type == 'radio') {
		y = 250;
		for (x=1; x <= 7; x++) {

			for (z=1; z <= 7; z++) {
				setTimeout("Name('"+name+"').parentNode.parentNode.style.border='"+z+"px dashed #8ae234';", y + 250 + z * 60);
				setTimeout("Name('"+name+"').parentNode.parentNode.style.border='"+(7-z)+"px solid #8ae234';", y + 500 + z * 60);

			}
			setTimeout("Name('"+name+"').parentNode.parentNode.style.border='';", y + 1000);
			y += 1000;
		}
	}
}
function ug_highlight(name) {
	y = 250;
	fade = 250;
	count = 15;
	timeout = 1000;

	if (Name(name).type == 'radio') {
		for (x=1; x <= count; x++) {
			setTimeout("Name('"+name+"').parentNode.parentNode.style.border='1px dashed #8ae234';", y + fade);
			setTimeout("Name('"+name+"').parentNode.parentNode.style.backgroundColor='#2A383D';", y + fade);
			setTimeout("Name('"+name+"').parentNode.parentNode.style.backgroundColor='';", y + timeout);
			setTimeout("Name('"+name+"').parentNode.parentNode.style.border='';", y + timeout);
			y += timeout;
		}
	}
	else {
		for (x=1; x <= count; x++) {
			setTimeout("Name('"+name+"').parentNode.style.border='1px dashed #8ae234';", y + fade);
			setTimeout("Name('"+name+"').parentNode.style.backgroundColor='#2A383D';", y + fade);
			setTimeout("Name('"+name+"').parentNode.style.border='';", y + timeout);
			setTimeout("Name('"+name+"').parentNode.style.backgroundColor='';", y + timeout);
			y += timeout;
		}
	}
}

function ug_highlightall(name)
{
	var objs = Names(name);

	for (var i = 0; i < objs.length; i++) {

		y = 250;
		fade = 250;
		count = 15;
		timeout = 1000;

		if (objs[i].type == 'radio') {
			for (x=1; x <= count; x++) {
				setTimeout("Names('"+name+"')["+i+"].parentNode.parentNode.style.border='1px dashed #8ae234';", y + fade);
				setTimeout("Names('"+name+"')["+i+"].parentNode.parentNode.style.backgroundColor='#2A383D';", y + fade);
				setTimeout("Names('"+name+"')["+i+"].parentNode.parentNode.style.backgroundColor='';", y + timeout);
				setTimeout("Names('"+name+"')["+i+"].parentNode.parentNode.style.border='';", y + timeout);
				y += timeout;
			}
		}
		else {
			for (x=1; x <= count; x++) {
				setTimeout("Names('"+name+"')["+i+"].parentNode.style.border='1px dashed #8ae234';", y + fade);
				setTimeout("Names('"+name+"')["+i+"].parentNode.style.backgroundColor='#2A383D';", y + fade);
				setTimeout("Names('"+name+"')["+i+"].parentNode.style.border='';", y + timeout);
				setTimeout("Names('"+name+"')["+i+"].parentNode.style.backgroundColor='';", y + timeout);
				y += timeout;
			}
		}
	}
}


function ug_login(obj)
{
	Name('submit').disabled = true;
	Name('password').value = calcMD5(Name('password').value);
	obj.action = Name('server').options[Name('server').selectedIndex].value;
}

function scpm(i,val,m){

	if(val=='+'){

		if(document.getElementsByName('buy['+i+']')[0].value<m){
			document.getElementsByName('buy['+i+']')[0].value++;
		}
	}else
	if(val=='-'){
		if(document.getElementsByName('buy['+i+']')[0].value>0){
			document.getElementsByName('buy['+i+']')[0].value--;
		}
	}else
	if(val==0){
		document.getElementsByName('buy['+i+']')[0].value=0;
	}else
	if(val==m){
		document.getElementsByName('buy['+i+']')[0].value=m;
	}

}

//
// ForEach funcion.
//
function ForEach(array, fn) {

	for(var n = 0; n < array.length; n++){

		var r = fn(array[n]);

	if(r != undefined)
		return r;
	}

	return undefined;
}
//
// Tan simple como abrir una ventana
//
function popup(url, name) {

	var new_win = window.open(url, name, 'resizable=yes,scrollbars=yes,menubar=no,toolbar=no,width=640,height=480,top=0,left=0');
	//foco!
	new_win.focus();

	return false;

}
//
// Dime lo que usas y te dire como eres.
//
function detectBrowser() {
    var ie = document.all != undefined;
    var opera = window.opera != undefined;

    if (opera) return "opera";
    if (ie) return "ie";
    if ((window) && (window.netscape) && (window.netscape.security)) {
      if (window.XML) {
        return "firefox15";
      }
      else return "firefox10";
    }
    return "ie";
}

var Browser = detectBrowser();


function firefox()
{
	if (detectBrowser() == 'ie') {
		document.write('<a href="http://www.spreadfirefox.com/?q=affiliates&am'+
		'p;id=155136&amp;t=221" onclick="this.style[\'display\'] = \'none\';" '+
		'target="Mainframe"><img border="0" alt="Use Firefox 2" title="Haga Cl'+
		'ick para ocultar esta imagen" src="./ug-themes/opengame/images/foxkeh'+
		'-fx2-100x100.png"/></a>');
	}

}

function op(URL, variant)
{
	day = new Date();
	id = day.getTime();
	eval("top.Mainframe.location='"+ URL + "" + variant + "rnd=" + id + "'");

}

function popup_notes()
{

	var new_win = window.open('?mode=notes','Notes','resizable=yes,scrollbars=yes,menubar=no,toolbar=no,width=580,height=380,top=0,left=0');
	// foco!
	new_win.focus();

	return false;

}

function Hide(obj) {
    obj.style.display = 'none';
}

function virtual_toggle_column(tag, img)
{

	var x = document.getElementsByName(tag);
	var y = document.getElementsByName(img);

	if (x[0].style['display'] != 'none') {

		for (var i = 0; i < y.length; i++) {
			y[i].src = 'http://static.ugamela.com/images/expand.gif';
		}

		for (var i = 0; i < x.length; i++) {
			x[i].style['display'] = 'none';
		}

	} else {

		for (var i = 0; i < y.length; i++) {
			y[i].src = 'http://static.ugamela.com/images/collapse.gif';
		}

		for (var i = 0; i < x.length; i++) {
			x[i].style['display'] = '';
		}

	}

}

function hangar_timeout()
{

	if (time_first > 0) { // mientras no se termine el tiempo

		time_first = time_first - 1;
		time_total = time_total - 1;

		if (time_type == 0) { // metodo por cada nave
			left = Math.ceil(time_first / time_each);

			if (left != ships_count) {
				ships_count = ships_count - 1;
				document.getElementById('ships_count').innerHTML = pretty_number(left);
			}
		}

		document.getElementById('time_first').innerHTML = pretty_time(time_first);
		document.getElementById('time_total').innerHTML = pretty_time(time_total);

	} else { // se termina el tiempo

		if (!document.getElementById('time_first')){
			self.location = PHP_SELF;
		} else {
			var temp = document.getElementById('time_first');
			temp.parentNode.parentNode.removeChild(temp.parentNode);
		}

	}

	window.setTimeout("hangar_timeout();", 999);
}

function use_skin_default()
{
	document.forms[0].dpath.value = '';
}

function cb_set_pause(checkbox)
{
	var value = checkbox.checked;
	document.getElementsByName('pause')[0].checked = value;
	document.getElementsByName('pause')[1].checked = value;
}


function cb_set_bypack(checkbox)
{
	var value = checkbox.checked;
	document.getElementsByName('bypack')[0].checked = value;
	document.getElementsByName('bypack')[1].checked = value;
}

function key_pressed(i, event)
{
	return (event.keyCode == i);
}

function Id(i)
{
	return document.getElementById(i);
}

function TagName(i)
{
	return document.getElementsByTagName(i);
}

function Name(i)
{
	return document.getElementsByName(i)[0];
}

function Names(i)
{
	return document.getElementsByName(i);
}

function Class(i)
{
	return document.getElementsByClass(i);
}

function write(str)
{
	document.write(str);
}

// Funciones agregadas...
function AlertMessagesGot(i)
{
	if (parent.frames['LeftMenu']) {
		parent.frames['LeftMenu'].show_emblem_new(i);
	}
}

function RunClock(sec, arr)
{



	//buffer = buffer.replace(/\=&gt;/gi, '<font class="red">\=&gt;</font>');
	sec = sec+ 1;

	document.getElementById('local-clock').innerHTML = str;



	setTimeout('RunClock('+sec+');', 999);
}


function t(){

  v = new Date();
  n = new Date();
  o = new Date();
  for (cn = 1; cn <= anz; cn++) {
    bxx = document.getElementById('bxx' + cn);
    ss = bxx.title;
    s = ss - Math.round((n.getTime() - v.getTime()) / 1000.);
    m = 0;
    h = 0;
    if (s < 0) {
      bxx.innerHTML = "-";
    } else {
      if (s > 59) {
	m = Math.floor(s/60);
	s = s - m * 60;
      }
      if (m > 59) {
	h = Math.floor(m / 60);
	m = m - h * 60;
      }
      if (s < 10) {
	s = "0" + s;
      }
      if (m < 10) {
	m = "0" + m;
      }
      bxx.innerHTML = h + ":" + m + ":" + s + "";
    }
    bxx.title = bxx.title - 1;
  }
  window.setTimeout("t();", 999);
}

//cntchar.js :: Cuenta los caracteres solo del primer formulario.
var x = "";
var e = null;

function cntchar(m){

	if(window.document.forms[0].text.value.length > m){
		window.document.forms[0].text.value = x;
	}else{
		x = window.document.forms[0].text.value;
	}
	if(e == null){
		e = document.getElementById('cntChars');
	}else{
		e.childNodes[0].data = window.document.forms[0].text.value.length;
	}

}
// Created by Perberos. All rights no reversed (C) 2007


function emoticon(chars)
{
	var textarea = document.getElementById('text');

	if (textarea.createTextRange && textarea.caretPos)
	{
		var caretPos = textarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text+' '+chars+'  ' : caretPos.text+chars+' ';
	}
	else
	{
		textarea.value += ' '+chars+' ';
	}

	textarea.focus();

}

// para la seccion de mensajes
function invert_select() {

	var input = document.getElementsByTagName('input');

	for (i = 0; i <= input.length - 1; i++) {

		if (input[i].type == 'checkbox' && !input[i].id) {

			input[i].click();

		}

	}

	document.getElementById('deletemessages').options[0].selected='true';
	document.getElementById('deletemessages2').options[0].selected='true';
}

function select_all() {

	var input = document.getElementsByTagName('input');

	for (i = 0; i <= input.length - 1; i++) {

		if (input[i].type == 'checkbox' && !input[i].id) {

			input[i].checked = true;

		}

	}

	document.getElementById('deletemessages').options[0].selected='true';
	document.getElementById('deletemessages2').options[0].selected='true';
}

function ug_messages_emblem_clear() {
	if (parent.frames['LeftMenu']) {
		parent.frames['LeftMenu'].clear_emblem_new();
	}
}

function ug_avatar_select(obj) {
	switch (obj.selectedIndex) {
		case (0):
			Id('avatar-file').style['display'] = '';
			Id('avatar-url').style['display'] = 'none';
			Id('avatar-gr').style['display'] = 'none';
			break;
		case (1):
			Id('avatar-file').style['display'] = 'none';
			Id('avatar-url').style['display'] = '';
			Id('avatar-gr').style['display'] = 'none';
			break;
		case (2):
			Id('avatar-file').style['display'] = 'none';
			Id('avatar-url').style['display'] = 'none';
			Id('avatar-gr').style['display'] = '';
			break;
		case (3):
			Id('avatar-file').style['display'] = 'none';
			Id('avatar-url').style['display'] = 'none';
			Id('avatar-gr').style['display'] = 'none';
			break;
	}
}

// TODO: mejorar esto... es de muy novato lol
function ug_vacation_mode_timer(t) {
	if (t > 0) {
		t--;
		document.getElementsByName('vacation_timer')[0].innerHTML = pretty_time(t);
	}

	window.setTimeout("ug_vacation_mode_timer("+t+");", 999);
}

function ug_delete_mode_timer(t) {
	if (t > 0) {
		t--;
		document.getElementsByName('delete_timer')[0].innerHTML = pretty_time(t);
	}

	window.setTimeout("delete_mode_timer("+t+");",999);
}

// para buscar en el foro o blog
function ug_search()
{
	if (Name('type').value == 'forum') {
		location = 'http://ugamela.com/board/search.php?q=' + Name('text').value;
		return false;
	} else if (Name('type').value == 'blog') {
		location = 'http://ugamela.com/?s=' + Name('text').value;
		return false;
	} else if (Name('type').value == 'google') {
		location = 'http://www.google.com.ar/search?q=' + Name('text').value;
		return false;
	} else if (Name('type').value == 'yahoo') {
		location = 'http://search.yahoo.com/search?p=' + Name('text').value;
		return false;
	}

	return true;
}

// Menu de planeta alterno
menuactivated = false;

function show_menu()
{
	menuactivated = true;
	obj = document.getElementById('dropmenu');

	obj.style['display'] = '';
}

function hide_menu()
{
	obj = document.getElementById('dropmenu');

	obj.style['display'] = 'none';
}

function sethiden()
{
	if (!menuactivated)
		hide_menu();
}

function leave_main()
{
	menuactivated = false;
	setTimeout('sethiden();', 2999);
}

function set_ok()
{
	menuactivated = true;
}

function set_nok()
{
	menuactivated = false;
	setTimeout('sethiden();', 1999);
}

// colores
function colorit(obj)
{
	obj.style['border'] = '1px solid red';
}
function decolorit(obj)
{
	obj.style['border'] = '1px solid black';
}

function ug_ranking_timer(t)
{
	if (t > 0) {
		t--;
		Name('ug_ranking_timer').innerHTML = pretty_time(t);
	}

	window.setTimeout("ug_ranking_timer("+t+");",999);
}

// Ajax
function HttpRequest()
{
	var xmlhttp = false;

	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

/* Copyright 2008 by German Perugorria perberos@gmail.com All Rights Reserved */
