function popUp(uri) {
	window.open(uri,"Popup","height=600,width=600,scrollbars=1");
}

function changeWhatEver(field,value,table, id) {
	new Ajax.Request("http://www.yellowphone.de/ajax_changeWhatEver.php",{
		parameters: { field: field, value: value, table: table, id: id },
		onComplete: function() {
			location.reload();
		}
	});
}

function doEntfern(id,table) {
	var conf = confirm("Soll der Datensatz wirklich entfernt werden?");
	if(conf) {
		new Ajax.Request("http://www.yellowphone.de/ajax_doEntfern.php",{
			parameters: {id: id, table: table},
			onComplete: function() {
				location.reload();
			}
		});
	}
}

function edit(id, where) {
	var loc = document.location.href;
	new Ajax.Updater('changeInhalt','http://www.yellowphone.de/ajax_editform.php', {
		parameters: { id: id, where: where, from: loc }
	});
	document.getElementById("changeMe").style.display = "";
}

function closeEditWindow() {
	document.getElementById("changeMe").style.display = "none";
}

function openWindow(id) {
	document.getElementById(id).style.display = "none";
}

function closeWindow(id) {
	document.getElementById(id).style.display = "";
}

function conf(url,text) {
	var confi = window.confirm(text);
	if(confi) {
		abschicken(url);
	}
}

function abschicken(url) {
	document.location.href = url;
}

function editField(edit_id) {
	if(document.getElementById("anz_name_"+edit_id).style.display == "") {
		document.getElementById("anz_name_"+edit_id).style.display = "none";
		document.getElementById("edit_name_"+edit_id).style.display = "";
		document.getElementById("edit_field_"+edit_id).focus();
	} else {
		document.getElementById("anz_name_"+edit_id).style.display = "";
		document.getElementById("edit_name_"+edit_id).style.display = "none";
	}
}

function switchLang(lang) {
	new Ajax.Request("http://www.yellowphone.de/switchLang.php?lang="+lang,{
		asynchronous: false,
		onComplete: function() {
			location.reload();
		}
	});
}

function toggle(id,allOtherZu) {
	var obj = document.getElementById(id);
	if(allOtherZu) {
		var mo = id.split("_");
		closeAllFieldsets(mo[0],id);
	}
	if(obj.style.display == "none") {
		document.getElementById(id).style.display = "";
		if($('t_'+id)) $('t_'+id).update("(weiter)");
	} else {
		document.getElementById(id).style.display = "none";
		if($('t_'+id)) $('t_'+id).update("(einblenden)");
	}
}

function closeAllFieldsets(name,except) {
	var cont = true;
	var zaehler = 1;
	while(cont) {
		var tmp = document.getElementById(name + "_" + zaehler);
		if(tmp) {
			if(except != name+"_"+zaehler) {
				$('t_'+ name + "_" + zaehler).update("(einblenden)");
				document.getElementById(name + "_" + zaehler).style.display = "none";
			}
		} else {
			cont = false;
		}
		zaehler++;
	}
}

function openAllFieldsets() {
	for(var i=1; i<4; i++) {
		document.getElementById("fieldset_"+i).style.display = "";
		$('t_fieldset_'+i).update("(weiter)");
	}
}

function logout() {
	document.cookie = 'userid=; expires=Thu, 01-Jan-1970 00:00:01 GMT;';
	var ref = document.location;
//	document.location.href = "http://www.yellowphone.de/logout/?ref=" + ref;
	document.location.href = "http://www.yellowphone.de/logout/";
}

function makeLogin() {
	var daten = $('login_form').serialize();
	//alert(daten);
	new Ajax.Request("http://www.yellowphone.de/ajax_login.php?"+daten,{
		asynchronous: false,
		onComplete: function(r) {
			if(r.responseText == "notfound") {
				alert("E-Mailadresse oder Kennwort falsch! Bitte nochmal versuchen.");
			} else {
				if(daten.indexOf("login_cookie") != -1) {
					var a = new Date();
					a = new Date(a.getTime() +1000*60*60*24*365);
					document.cookie = 'userid='+r.responseText+'; expires='+a.toGMTString()+';';
				}
				var ref = document.location;
//				document.location.href = ref;
				document.location.href = "http://www.yellowphone.de/";
			}
		}
	});
}

function removeStopper() {
	$('stopper').remove();
}

function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

document.observe("dom:loaded", function() {
  removeStopper();
});
