// JavaScript Document
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function EjecutarConEspera(accion)
{
	var t = setTimeout(accion,1000);
}

function IrA(url)
{
	document.location.href=url;
}

function valcorreo(valor) {
if (/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/.test(valor)){
return (true)
} else {
return (false);
}
}

function Abrir_ventana(pagina,ancho,alto)
{
	var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width="+ancho+", height="+alto+", top=" + ((screen.height/2)-(ancho/2)) + ", left=" + ((screen.width/2)-(alto/2));
	window.open(pagina,"",opciones);
}

function soloNumeros(evt)
{
	//asignamos el valor de la tecla a keynum
	if(window.event){// IE
	keynum = evt.keyCode;
	}else{
	keynum = evt.which;
	}
	//comprobamos si se encuentra en el rango
	if((keynum>47 && keynum<58) || (keynum == 8)){
	return true;
	}else{
	return false;
	}
}

function HanPulsadoEnter(evt)
{
	//asignamos el valor de la tecla a keynum
	if(window.event){// IE
	keynum = evt.keyCode;
	}else{
	keynum = evt.which;
	}
	//comprobamos si se encuentra en el rango
	if(keynum == 13){
	return true;
	}else{
	return false;
	}
}

function getElementLeft(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
            } while (obj = obj.offsetParent);
      }
      return curleft;
}


function getElementTop(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return curtop;
}


//============== Help =============
//=================================
var enProcesoHelp = false;
var httpHelp = GeneraAjax();

function ShowTipHelp(img, id, aleatorio, idioma, imagen)
{
	if (!enProcesoHelp && httpHelp) {
		if(imagen != false)
		{	
			//Cambiamos el icono de help a seleccionado
			img.src = "/images/ayuda/helpsel.gif";
		}
		
		//Configuramos
		varwidth = 300+ "px";
		varheight = "auto";
		if((window.screen.width - getElementLeft(img)) < 300){
			varleft = (getElementLeft(img)-307) + "px";
		}
		else{
			varleft = (getElementLeft(img)+20) + "px";	
		}
		vartop = getElementTop(img) + "px";
		
		//Creamos la caja de ayuda
		if(document.getElementById("ContentHelp"+id+"-"+aleatorio) == null)
		{		
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id', "ContentHelp"+id+"-"+aleatorio);
			
			newdiv.style.width = varwidth;
			newdiv.style.height =  varheight;
			newdiv.style.position = "absolute";
			newdiv.style.left = varleft ;
			newdiv.style.top = vartop;
			newdiv.style.background = "#fff";
			newdiv.style.border = "1px solid #000";
			newdiv.style.overflow = "auto";
			newdiv.style.padding = "5px";
			
			newdiv.innerHTML = "<img src='/images/loading/loading4.gif' />";
			
			document.body.appendChild(newdiv);
		}
		else
		{
			document.getElementById("ContentHelp"+id+"-"+aleatorio).style.display = "";
			document.getElementById("ContentHelp"+id+"-"+aleatorio).innerHTML = "<img src='/images/loading/loading4.gif' />";
		}

		var url = "/accion/LoadHelp.asp?id="+id+"&aleatorio="+aleatorio+"&idioma="+idioma;   
	   	httpHelp.open("GET", url, true);
	   	httpHelp.onreadystatechange = handleHttpResponseHelp;
	   	enProcesoHelp = true;
	   	httpHelp.send(null);
	}	
}

function handleHttpResponseHelp()
{
	if (httpHelp.readyState == 4) {
    	if (httpHelp.status == 200) {
          	if (httpHelp.responseText.indexOf('invalid') == -1) {
            	resultsForm = httpHelp.responseText.split("||");
			 	if (resultsForm.length > 1) {
					document.getElementById("ContentHelp"+resultsForm[0]+"-"+resultsForm[3]).innerHTML = "<b>"+resultsForm[1]+"</b><br />"+resultsForm[2];
			 	}
				else
				{
					document.getElementById("ContentHelp"+resultsForm[0]+"-"+resultsForm[3]).innerHTML = "";
					document.getElementById("ContentHelp"+resultsForm[0]+"-"+resultsForm[3]).style.display = "none";					
			 	}		 
             	enProcesoHelp = false;
          	}
       	}
    }
}

function HideTipHelp(img, id, aleatorio, imagen)
{
	if(imagen != false)
	{
		//Cambiamos el icono de help a normal
		img.src = "/images/ayuda/help.gif";
	}
	
	//Borramos el contenido del div
	if(document.getElementById("ContentHelp"+id+"-"+aleatorio) != null){
		document.getElementById("ContentHelp"+id+"-"+aleatorio).innerHTML = "";
		document.getElementById("ContentHelp"+id+"-"+aleatorio).style.display = "none";
	}
}

function formatNumber(num,prefix){
   prefix = prefix || '';
   num += '';
   var splitStr = num.split('.');
   var splitLeft = splitStr[0];
   var splitRight = splitStr.length > 1 ? '.' + splitStr[1] : '';
   var regx = /(\d+)(\d{3})/;
   while (regx.test(splitLeft)) {
      splitLeft = splitLeft.replace(regx, '$1' + ',' + '$2');
   }
   return prefix + splitLeft + splitRight;
}

function unformatNumber(num) {
   return num.replace(/([^0-9\.\-])/g,'')*1;
}

// This function decodes the any string
// that's been encoded using URL encoding technique
function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}
