function isset(varname){
  return(typeof(window[varname])!='undefined');
}

function getel(myelement)
{
	if(document.getElementById(myelement)) return document.getElementById(myelement);
	else return false;
}

function put_errors(texto,div,init)
{
	var mdiv=getel(div);
   	mdiv.className = "input_error";
   	if (init) mdiv.innerHTML = texto;
   	else mdiv.innerHTML += texto;
}

function estan_vacios()
{
	var error=false;
	var texto="";
	var myform = arguments[0];
	for (i=1;i<arguments.length;i++)
	{	
		var obj = arguments[i];
		var tmp = getel(obj);
		var input = "";
		if (tmp!=false) input = tmp.value;

		if ((input.trim()=="") || (input=="0000-00-00") || (tmp.type=="radio" && getSelectedRadio(myform,obj)==-1)) {
			error=true;

			var tmp2 = obj.split("-");
			var obj2 = "";
			if (tmp2[1]!="") obj2 = tmp2[1];

	//		alert("itinerario_exito " + errors["itinerario_exito"] + " " + isset(errors[obj]) + " " + obj2 + " " + isset(errors[obj2]));
			if (errors[obj]!="" && errors[obj]!=undefined) texto += '<br />' + errors[obj];
			else if (obj2!="" && errors[obj2]!="" && errors[obj2]!=undefined) texto += '<br />' + errors[obj2];
			else {

				var myobj =  "";
				for (y=1;y<tmp2.length;y++) myobj += tmp2[y];
				//myobj = obj.replace("my_","");
				if (isset(my_form_name) && my_form_name.trim()!="") myobj = myobj.replace(my_form_name,"");				
				myobj = myobj.replace(/_/gi," ");
				myobj = myobj.replace(/-/gi," ");
				myobj = myobj.replace(" id "," ");				
				texto += '<br /> ' + error_generico + ' ' + myobj;
			}
		}	
	}
	if (error) put_errors(texto,my_errors_div,false);
	return error;
}

function getSelectedRadio(myform,myradio) 
{
	var buttonGroup = document.forms[myform].elements[myradio];
	if(!buttonGroup) { alert("!buttongroup"); return -1; }
	var radioLength = buttonGroup.length;
	if(radioLength == undefined) {
		if(buttonGroup.checked) return buttonGroup.value;
		else {alert("buttongroup not checked");return -1;}
	}		
	for(var i = 0; i < radioLength; i++) {
		if(buttonGroup[i].checked) {
			return buttonGroup[i].value;
		}
	}
	return -1;
} 

function check_hora(input)
{
	if (input.trim()!="")
	{
		var tmp=input.split(":");
		if ((tmp[0] >= 0) && (tmp[0] < 24) && (tmp[1] >= 0) && (tmp[1] < 60)) return true;
	}	
	return false;
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function visibilidad(idCapa) 
{
   var objeto = document.layers ? document.layers[idCapa] :
   document.getElementById ?  document.getElementById(idCapa).style :
   document.all[idCapa].style;
   var actual = objeto.visibility;
   objeto.visibility = document.layers ? (actual=="hide" ? "show" : "hide") :
   (actual=="hidden" ? "visible" : "hidden");
}

function show_hide(idCapa, fuerzaVisibilidad) 
{
	var capa = document.getElementById(idCapa);
	if (capa) 
	{ 
		if (fuerzaVisibilidad && fuerzaVisibilidad!="") {
			if (fuerzaVisibilidad=="visible") capa.style.display="";
			else capa.style.display="none";
		}
		else 
		{
			if (capa.style.display == "none") {
				capa.style.display = "";
			} else {
				capa.style.display = "none";
			}
		}
	}
}

function show_loading() {
  var sx=document.body.scrollLeft;
  var sy=document.body.scrollTop;
  var wx=window.innerWidth;
  var wy=window.innerHeight;
  if(isUndefined(wx)) {
    wx=screen.width;
    wy=screen.height-200;
  }
  div=document.getElementById('loading_div');
  div.style.left=-1000;
  div.style.top=-1000;
  div.style.display='block';
  var pi=pos_and_dim(div);
  div.style.left=sx+wx/2-pi.w/2;
  div.style.top=sy+wy/2-pi.h/2;
}

function alertame(texto)
{
    var alertas= document.getElementById("alertas"); 
    alertas.innerHTML=texto;
}

function isUndefined(a) {
  return typeof a == 'undefined';
}

function pos_and_dim(obj) {
  var x=0;
  var y=0;
  var w=obj.offsetWidth;
  var h=obj.offsetHeight;
  while(obj !=null) { 
    x+=obj.offsetLeft;
    y+=obj.offsetTop;
    obj=obj.offsetParent;
  }
  return {x:x,y:y,w:w,h:h}
}

function AJAX(url, funcion) 
{
    var peticion = inicia();
    peticion.onreadystatechange = procesaPeticion;
        
    function inicia() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function procesaPeticion () {
      // readyState 4 = peticion completa
	  // status 200 = llamada http completa
      if ((peticion.readyState == 4) && (peticion.status == 200)) {
          if (funcion) funcion(peticion.responseXML);
        }
    }

    this.doGet = function() {
      peticion.open("GET", url, true);
      peticion.send(null);
    }
}

function validateCallback(responseXML,divId,submitId) 
{
   var msg = responseXML.getElementsByTagName("valido")[0].firstChild.nodeValue;
   var texto = responseXML.getElementsByTagName("mensaje")[0].firstChild.nodeValue;
   var enlace_texto = responseXML.getElementsByTagName("enlace_texto")[0].firstChild.nodeValue;
   var enlace_url = responseXML.getElementsByTagName("enlace_url")[0].firstChild.nodeValue;
   var hayerrores = responseXML.getElementsByTagName("errores")[0].firstChild.nodeValue;
   var mdiv = document.getElementById(divId);
   //var submitBtn = document.getElementById(submitId);

   //var minputString = divId.replace("message-",""); 
   //var minput = document.getElementById(minputString);
   
   var enlace="";
   if (enlace_texto.trim()!="" && enlace_url.trim()!="") enlace='<a href="' + enlace_url + '">' + enlace_texto + '</a>';
   if (enlace!="") texto = texto + ' - ' + enlace;
   
   if (msg == "false") 
   {
       // set the style on the div to invalid
       mdiv.className = "input_error";
       mdiv.innerHTML = texto;
       return "false";
    } else {
       // set the style on the div to valid
       mdiv.className = "input_correcto";
       mdiv.innerHTML = texto;
       return "true";
    }
}

function validateJS(response,divId,texto)
{
   var mdiv = document.getElementById(divId);
   if (response == "false") 
   {
       mdiv.className = "input_error";
       mdiv.innerHTML = texto;
    } else {
       mdiv.className = "input_correcto";
       mdiv.innerHTML = texto;
    }	
}

function add_input(divId,input_name,input_type,default_value)
{
   var mdiv = document.getElementById(divId);
   if (mdiv)
   {
	   var myHtml = mdiv.innerHTML;
	   var texto = '';

   	   var last=get_last_input(input_name);
	   if (last>0) input_name=input_name + "-" + (last + 1);
	alert(input_name);
	   if (input_type=="textarea") texto='<textarea name="' + input_name + '" id="' + input_name + '" class="form" style="width:350px;height:200px;">'+ default_value +'</textarea>';
	   else if (input_type=="text" || input_type=="submit" || input_type=="button") texto='<input type="' + input_type + '" name="' + input_name + '" id="' + input_name + '" value="' + default_value + '" class="form">';		

	   if (texto!='') mdiv.innerHTML = myHtml + texto;
   }
}

function get_last_input(input_name)
{
	var i=1;
	var mas_de_uno=false;
	while (lastInput = document.getElementById(input_name + "-" + i))
	{
		i=i+1;
		mas_de_uno = true;
	}
	if (mas_de_uno) return i - 1;
	else return 0;
}

function multi_seleccionar(arg1,arg2,arg3,seleccion) {
  var orig=document.getElementById(arg1+'_'+arg3);
  var dest=document.getElementById(arg2+'_'+arg3);
  for(i=0;i<orig.length;i++) {
    if ((seleccion!="" && orig.options[i].value==seleccion) || (seleccion=="" && orig.options[i].selected)) {
      orig.options[i].selected=false;
      dest.appendChild(orig.options[i]);
      i--;
    }
  }
  var sel=document.getElementById('sel_'+arg3);
  var sel2=document.getElementById(arg3);
  var lista='';
  for(i=0;i<sel.length;i++) {
    if(i>0) lista=lista+',';
    lista=lista+sel.options[i].value;
  }
  sel2.value=lista;
}

String.prototype.trim = function(x) 
{
	if (x=='left')
		return this.replace(/^\s*/,'');
	if (x=='right')
		return this.replace(/\s*$/,'');
	if (x=='normalize')
		return this.replace(/\s{2,}/g,' ').trim();

	return this.trim('left').trim('right');
}

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


function findPosX(obj)
{
//  var ojb = document.getElementById(myObj);
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
//  var ojb = document.getElementById(myObj);
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

function setLayer(div,divOrigen)
{
	var x = document.getElementById(div);
	x.style.top = (findPosY(divOrigen) + 20 ) + 'px';
	x.style.left = (findPosX(divOrigen) - 120 ) + 'px';
}

