// General scripts

function showPopup(url,name,width,height,scrollbars) {
	var x = (screen.availWidth - width) / 2;
	var y = (screen.availHeight - height) / 2;
	x = (x > 0) ? x : 0;
	y = (y > 0) ? y : 0;
	if (typeof scrollbars == 'undefined')  scrollbars = false;
	var specs = "width=" + width + ",height=" + height + ",left=" + x + ",top=" + y
				  + ",scrollbars=" + (scrollbars ? "1" : "0");
	var oWin = window.open( url, name, specs );
}

function showFullscreenPopup(url,name) {
	var width = screen.availWidth;
	var height = screen.availHeight;
	var specs = "width=" + width + ",height=" + height + ",left=0,top=0,scrollbars=0";
	var oWin = window.open( url, name, specs );
}


function dateChanged(fieldName) {
	var field = document.getElementById(fieldName);
	var dia = document.getElementById(fieldName + ".dia");
	var mes = document.getElementById(fieldName + ".mes");
	var anio = document.getElementById(fieldName + ".anio");
	
	if (field==null || dia==null || mes==null || anio==null) return true;
	
	var hora = document.getElementById(fieldName + ".hora");
	var minuto = document.getElementById(fieldName + ".minuto");
	var segundo = document.getElementById(fieldName + ".segundo");
	
	if (isNaN(dia.value)) dia.value = '';
	if (isNaN(anio.value)) anio.value = '';
	
	var x;
	var valMes = '00' + mes.selectedIndex;
	valMes = valMes.substring(valMes.length-2);
	var valAnio = '0000';
	var valDia = '00' ;

	x = parseInt(anio.value,10);
	if (!isNaN(x)) {
		if (x >=0 && x <= 30) {
			valAnio = ('0000' + (2000 + x));
			valAnio = valAnio.substr(valAnio.length-4);
		}
		else if (x >=31 && x <= 99) {
			valAnio = ('0000' + (1900 + x));
			valAnio = valAnio.substr(valAnio.length-4);
		}
		else if (x >=100 && x <= 9999) {
			valAnio = ('0000' + x);
			valAnio = valAnio.substr(valAnio.length-4);
		}
	}
	if (anio.value != valAnio) anio.value = valAnio;

	x = parseInt(dia.value,10);
	if (!isNaN(x) && x >=1 && x <= 31) valDia = '00' + x;
	valDia = valDia.substring(valDia.length-2);
	if (dia.value != valDia) dia.value = valDia;

	var valHora = '00';
	var valMinuto = '00';
	var valSegundo = '00';

	if( hora != null ) {
		x = parseInt(hora.value,10);
		if (!isNaN(x) && x >=0 && x <= 23) valHora = '00' + x;
		valHora = valHora.substring(valHora.length-2);
		if (hora.value != valHora) hora.value = valHora;
	}
	if( minuto != null ) {
		x = parseInt(minuto.value,10);
		if (!isNaN(x) && x >=0 && x <= 59) valMinuto = ('00' + x);
		valMinuto = valMinuto.substring(valMinuto.length-2);
		if (minuto.value != valMinuto) minuto.value = valMinuto;
	}
	if( segundo != null ) {
		x = parseInt(segundo.value,10);
		if (!isNaN(x) && x >=0 && x <= 59) valSegundo = '00' + x;
		valSegundo = valSegundo.substring(valSegundo.length-2);
		if (segundo.value != valSegundo) segundo.value = valSegundo;
	}

	field.value = valAnio + "-" + valMes + "-" + valDia + " " + valHora + ":" + valMinuto + ":" + valSegundo;
	
	return true;
}

function ShowHide(divName,btn) {
	var oDiv = document.getElementById(divName);
	if (oDiv != null) {
		if (oDiv.style.display=="none") {
			oDiv.style.display = "block";
			btn.value = "+";
		}
		else {
			oDiv.style.display = "none";
			btn.value = "-";
		}
	}
}


function getXmlHttp() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				xmlHttp=null;
			}
		}
	}
	return xmlHttp;
} // function getXmlHttp()



function getFrameDocById(frameId) {
	var f;
	try {
		f = document.frames(frameId).document;
	}
	catch (e) {
		f = document.getElementById(frameId).contentDocument;
	}
	return f;
}


function getDateInput(fieldName,value) {
	return "<input type=\"text\" name=\"" + fieldName + "\" id=\"" + fieldName +
			"\" value=\"" + value + "\" maxlength=\"10\" size=\"10\" class=\"box\" />&nbsp;" +
			"<input type=\"button\" value=\"...\" id=\"btn_" + fieldName + 
			"\" class=\"btn\" onmouseover=\"this.className='btnhov'\" onmouseout=\"this.className='btn'\" />\r\n" 
}

function getNumInput(fieldName,value,valMin,valMax) {
	var len = Math.max(valMin.length,valMax.length);
	var size = Math.max(len,6);
	return "<input type=\"text\" name=\"" + fieldName + "\" id=\"" + fieldName +
			"\" value=\"" + value + "\" maxlength=\"" + len + "\" size=\"" + size + "\" class=\"box\" />";
}


function getListMultiSelectOld(fieldName,filterId) {
	var xmlHttp = getXmlHttp();
	var sOptions = "";
	
	if (xmlHttp != null) {
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4){
				sOptions = xmlHttp.responseText;
			}
	  } // xmlHttp.onreadystatechange=function()
		xmlHttp.open("GET","getcombodata.php?id=" + filterId,false);
		xmlHttp.send(null);
	} // if
	
	return "<div>\r\n" +
	"\t<select name=\"" + fieldName + "[]\" id=\"" + fieldName + "\" size=\"5\" multiple=\"multiple\" style=\"width:350px\">\r\n" + 
	sOptions + "\t</select>\r\n" + 
	"</div>\r\n";
}


function getListMultiSelect(fieldName,filterId) {
	return "<div>\r\n" +
	"\t<iframe id=\"" + fieldName + "_iframe\" name=\"" + fieldName + "_iframe\" frameborder=\"0\" width=\"100%\" height=\"100\" " + 
	"style=\"border-style:solid;border-width:1px;border-color:#999999\" src=\"getmultiselect.php?id=" + filterId + 
	"\"></iframe>\r\n" + 
	"</div>\r\n";
}


function getHiddenInput(fieldName,value) {
	return "<input type=\"hidden\" name=\"" + fieldName + "\" id=\"" + fieldName + "\" value=\"" + value + "\" />";
}


function onKeyPressNum(e) {
	var keynum
	var keychar
	var numcheck
	
	if(window.event) { // IE
		keynum = e.keyCode
	}
	else if(e.which) { // Netscape/Firefox/Opera
		keynum = e.which
	}
	keychar = String.fromCharCode(keynum)
	numcheck = /\d/
	return numcheck.test(keychar)
} // function onKeyPressNum


function salir() {
	if (confirm('¿Está seguro que quiere salir?'))
		window.location = "index.php";
}

function selectWorkspace() {
	var selector = document.getElementById("workspaceselector");
	if (selector) {
		selector.style.display = "block";
	}
}

function okWorkspace() {
	var selector = document.getElementById("workspaceselector");
	var selectedWorkspace = document.getElementById("selectedWorkspace");
	if (selectedWorkspace) {
		idWorkspace = selectedWorkspace.options[selectedWorkspace.selectedIndex].value;
		xajax_selectWorkspace(idWorkspace);
	}
	if (selector) {
		selector.style.display = "none";
	}
}

function cancelWorkspace() {
	var selector = document.getElementById("workspaceselector");
	if (selector) {
		selector.style.display = "none";
	}
}


function getFlashMovie(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function repositoryFieldToUrl(data) {
	if (data==null || data.length==0)
		return "";
	
	var arr = data.split("|");
	if (arr.length < 2) {
		return "";
	}
	else if (arr[0] == null || arr[0].length==0 || arr[1] == null || arr[1].length==0) {
		return "";
	}
	else {
		return "repository/getfile.php" + arr[0] + "/" + arr[1];
	}
}


function dumpObj(obj, name, indent, depth) {
	var MAX_DUMP_DEPTH = 10;
	if (indent==undefined) indent = '';
	if (depth > MAX_DUMP_DEPTH) {
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	if (typeof obj == "object") {
	      var child = null;
	      var output = indent + name + "\n";
	      indent += "\t";
	      for (var item in obj) {
	            try {
	                   child = obj[item];
	            } catch (e) {
	                   child = "<Unable to Evaluate>";
	            }
	            if (typeof child == "object") {
	                   output += dumpObj(child, item, indent, depth + 1);
	            } else {
	                   output += indent + item + ": " + child + "\n";
	            }
	      }
	      return output;
	} else {
	      return obj;
	}
}


function centerObject(o) {

	if (typeof o=="string") o = document.getElementById(o);

	//var Xwidth = parseInt(o.style.width);
	//var Yheight = parseInt(o.style.height);

	o.style.display = "block";
	var Xwidth = o.offsetWidth;
	var Yheight = o.offsetHeight;

	// First, determine how much the visitor has scrolled

	var scrolledX, scrolledY;
	if( self.pageYOffset ) {
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} else if( document.body ) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}

	// Next, determine the coordinates of the center of browser's window

	var centerX, centerY;
	if( self.innerHeight ) {
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}

	// Xwidth is the width of the div, Yheight is the height of the
	// div passed as arguments to the function:
	var leftOffset = scrolledX + (centerX - Xwidth) / 2;
	var topOffset = scrolledY + (centerY - Yheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	var r=o.style;
	r.position='absolute';
	r.top = topOffset + 'px';
	r.left = leftOffset + 'px';
	r.display = "block";
//	window.alert(dumpObj(o.style));
//	window.alert('tagName='+o.tagName+'\r\nid='+o.id+'\r\nstyle.display='+o.style.display+'\r\nstyle.position='+o.style.position+'\r\nstyle.left='+o.style.left+
//					 '\r\nstyle.top='+o.style.top+'\r\nstyle.width='+o.style.width+'\r\nstyle.height='+o.style.height);
}

function urlEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
        output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

