// JavaScript Document

function isDefined(obj) {
  return (obj != null);
}

function isUndefined(obj) {
  return (obj == null);
}

function alertVar(obj) {
  alert(getVarContent(obj));
}

function getVarContent(obj) {
  var result = "";
  for (var varId in obj) result += varId + "=" + obj[varId] + "\n";
  return result;
}

function addOnLoad(new_function, before) {
  var old_onload = (window.onload) ? window.onload : function(){};
 	var first = (before) ? new_function : old_onload;
	 var second = (before) ? old_onload : new_function;
  window.onload = function() {
		  first();
		  second();
  };
}

function focusControl(control) {
  if (isDefined(control.length)) control = control[0];
  if (isDefined(control.focus)) control.focus();
}

function selectControl(control) {
  focusControl(control);
  if (isDefined(control.select)) control.select();
}

function imgsrc(id, src) {
	 document.getElementById(id).src = src;
}

function display(id, st) {
	 document.getElementById(id).style.display = st;
}

function redondear(num, d) {
  var num = parseFloat(num);
  var d = parseInt(d);
  d = (!d ? 2 : d);
  return Math.round(num * Math.pow(10, d)) / Math.pow(10, d);
}

function completarDecimales(num, d) {
  var d = parseInt(d);
  d = (!d ? 2 : d);
  var n = new String(redondear(num)).split(".");
		if (isUndefined(n[1])) n[1] = "";
		while (n[1].length < d) n[1] += "0";
  return n[0] + "." + n[1];
}

function openWindow(theURL, winName, myWidth, myHeight, isCenter, features) {
  features += ((features != "") ? "," : "") + "width=" + myWidth + ",height=" + myHeight;
  if (isCenter && window.screen) {
    var myLeft = (screen.width - myWidth) / 2;
    var myTop = (screen.height - myHeight) / 2;
    features += ",left=" + myLeft + ",top=" + myTop;
  }
  return window.open(theURL, winName, features);
}

function openSimpleWindow(theURL, winName, myWidth, myHeight, isCenter) {
  return openWindow(theURL, winName, myWidth, myHeight, isCenter, "toolbar=0,resizable=0,scrollbars=0,menubar=0,status=0");
}

String.prototype.pad = function(length, padString, isLeftPad) {
  isLeftPad = isUndefined(isLeftPad) || isLeftPad;
  var lpad = isLeftPad ? padString : "";
  var rpad = isLeftPad ? "" : padString;
  var buffer = this;
  while (buffer.length < length) buffer = lpad + buffer + rpad;
  return buffer;
}