﻿function ValidarBusqueda(url)
{


	txtBusc=document.getElementById("txtBusqueda").value;
	sBus=document.getElementById("sBusqueda").value;
	sNo=document.getElementById("sNoRegistros").value;	
	sTipo=document.getElementById("sTipoBusqueda").value;
	aBus=sBus.split("-");
	err="";
	if(aBus[0]=="S")
	{
		if(txtBusc=="")
			err="Se necesita un valor de busqueda";
		
		if(sTipo!=" like ")
			txtBusc="'"+txtBusc+"'";
	}else if(aBus[0]=="I")
	{
		if(!IsNumeric(txtBusc) || txtBusc=="")
			err="Se necesita un número";
		if(sTipo==" like ")
			sTipo="=";
	}else if(aBus[0]=="B")
	{
		if(txtBusc!="0" && txtBusc!="1")
			err="0- falso;  1- verdadero";
		if(sTipo!="=")
			sTipo="=";
	}else if(aBus[0]=="D")
	{
		if(!Isfecha(txtBusc))
			err="No es un formato de fecha valido. (dd/mm/aaaa)";
		else
		{
			afecha=txtBusc.split("/");
			txtBusc="'"+afecha[2]+"-"+afecha[1]+"-"+afecha[0]+"'";
			if(sTipo==" like ")
				sTipo="=";
		}
	}
	if(err=="")
	{
		txtBusc=Utf8.encode(txtBusc);
		showHint(url+"?TB="+txtBusc+"&C="+aBus[1]+"&TR="+sNo+"&ST="+sTipo);
	}else
	{
		alert(err);
	}
	
}
	function remplazaTodos(cadena, bloque, remplazo) {
		while (cadena.indexOf(bloque, 0)>-1) {
			var aux = cadena.substr(cadena.indexOf(bloque, 0)+bloque.length, cadena.length);
			cadena = cadena.substr(0, cadena.indexOf(bloque, 0));
			cadena += remplazo+aux;
		}
		return cadena;
	}
	
function IsTipoArchivo(nA,tipo) {
	//alert(tipo);
	tipo=tipo.toUpperCase();
	nA=nA.toUpperCase();
	if (nA != "" && nA.indexOf("."+tipo)!=-1)
	{
		p=nA.indexOf("."+tipo);
		if(nA.substr(p+1,nA.length-(p+1))==tipo)
			return true;
	}
    return false;
}

function openModal(url1,id,url2,name,tipo,w,h)
{

	if (window.showModalDialog) {
		window.showModalDialog(url1+"?TIP="+tipo+"&ID="+id,"name","dialogWidth:"+w+"px;dialogHeight:"+h+"px; status=no; scroll=no; resizable=no; center=yes;help=no;");
		
		
	} else {
		window.open(url1+"?TIP="+tipo+"&ID="+id,"name","height="+h+",width="+w+",left=100,top=100,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes");
	}
	showHint(url2+"?"+id);

}
function openModal2(url1,id,url2,name,tipo,w,h)
{

	if (window.showModalDialog) {
		window.showModalDialog(url1+"?TIP="+tipo+"&ID="+id,"name","dialogWidth:"+w+"px;dialogHeight:"+h+"px; status=no; scroll=yes; resizable=no; center=yes;help=no;");
		
		
	} else {
		window.open(url1+"?TIP="+tipo+"&ID="+id,"name","height="+h+",width="+w+",left=100,top=100,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=yes,resizable=no ,modal=yes");
	}
	showHint(url2+"?"+id);

}

function BorrarRegistro(cadena)
{
	if(confirm("Seguro quiere borrar el registro"))
		showHint(cadena);
}
function ActivarPeriodo(cadena)
{
	if(confirm("Seguro desea seleccionar este periodo"))
		showHint(cadena);
}
function ActConfig()
{
	
	vsMN=document.getElementById("sMN").value;	
	vsMRM=document.getElementById("sMRM").value;	
	vsNRD=document.getElementById("sNRD").value;	
	vsGB=document.getElementById("sGB").value;
	vsVI=document.getElementById("sVI").value;
	showHint("includes/Admin/ActConfiguracion.asp?sMN="+vsMN+"&sMRM="+vsMRM+"&sNRD="+vsNRD+"&sGB="+vsGB+"&sVI="+vsVI);
}
function ActConfigAE()
{
	
	vsEfoto=document.getElementById("sEfoto").value;	
	vsEOfic=document.getElementById("sEOfic").value;	
	vsTxTOfi=document.getElementById("txtOficios").value;
	showHint("includes/AdminEsc/ActConfiguracion.asp?sEfoto="+vsEfoto+"&sEOfic="+vsEOfic+"&sTxTOfi="+vsTxTOfi);
}

function Isfecha(valor) 
{
	if (valor.indexOf("/")>-1)
	{
		aJecha=valor.split("/");
		if (aJecha.length==3)
		{
			var dia=aJecha[0];
			var mes=aJecha[1];
			var ano=aJecha[2];
			if (IsNumeric(ano) && IsNumeric(mes) && IsNumeric(dia) && ano!="" && aJecha[1].length==2 && aJecha[0].length==2)
			{ 
				ano=parseInt(ano,10);
				mes=parseInt(mes,10);
				dia=parseInt(dia,10);
				if ((ano==0) || (ano<1900) || (ano>2100)) 
					{return false; }
				if (mes ==2) 
				{ 
					if ((ano%4 == 0) && (dia<=29) && (dia>0)) 
						{return true;}
					else if((dia<=28) && (dia>0))
						{return true;}
					else
						{return false; }
				} 
				else if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && (dia<=31) && (dia>0)) 
				{return true; } 
				else if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && (dia<=30) && (dia>0)) 
				{return true; }
				else 
					{return false; }
			} 
			else 
				{return false; } 
		}
		else 
			{return false; }
	}else 
		{return false; }
}


function IsNumeric(valor) 
{ 
	var log=valor.length; var sw="S"; 
	for (x=0; x<log; x++) 
	{ 
		v1=valor.substr(x,1); 
		v2 = parseInt(v1); 
		//Compruebo si es un valor numérico 
		if (isNaN(v2)) { sw= "N";} 
	} 
	if (sw=="S") 
		{return true;} 
	else 
		{return false; } 
} 

var primerslap=false; 
var segundoslap=false; 
function formateafecha(fecha) 
{ 
	var long = fecha.length; 
	var dia; 
	var mes; 
	var ano; 
	if ((long>=2) && (primerslap==false)) 
	{ 
		dia=fecha.substr(0,2); 
		if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) 
		{ 
			fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); 
			primerslap=true;
		} 
		else { 
			fecha=""; 
			primerslap=false;
		} 
	} 
	else 
	{ 
		dia=fecha.substr(0,1); 
		if (IsNumeric(dia)==false) 
			{fecha="";} 
		if ((long<=2) && (primerslap=true)) 
		{
			fecha=fecha.substr(0,1); primerslap=false;
		} 
	} 
	if ((long>=5) && (segundoslap==false)) 
	{ 
			mes=fecha.substr(3,2); 
			if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) 
				{ 
					fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; 
				} 
			else { fecha=fecha.substr(0,3);; segundoslap=false;} 
	} 
	else 
	{ 
		if ((long<=5) && (segundoslap=true)) 
		{	
			fecha=fecha.substr(0,4); segundoslap=false;
		} 
	} 
	if (long>=7) 
	{
		ano=fecha.substr(6,4); 
		if (IsNumeric(ano)==false) 
		{ 
			fecha=fecha.substr(0,6); 
		} 
		else 
		{ 
			if (long==10)
			{ 
				if ((ano==0) || (ano<1900) || (ano>2100)) 
				{ 
					fecha=fecha.substr(0,6); 
				} 
			} 
		} 
	} 
	if (long>=10) 
	{ 
		fecha=fecha.substr(0,10); 
		dia=fecha.substr(0,2); 
		mes=fecha.substr(3,2); 
		ano=fecha.substr(6,4); 
		// Año no viciesto y es febrero y el dia es mayor a 28 
		if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
	} 
	return (fecha); 
}



function num_aleatorio(inicio, fin)
{
    return Math.round((fin-inicio)*Math.random()) + inicio;
}	


function num_aleatDif(Inicio, Fin, Diferente) 
{
	var resultado = Inicio + parseInt((Fin-Inicio+1)*Math.random(),10); 
	while (resultado == Diferente) 
	{
		resultado = Inicio+ parseInt((Fin-Inicio+1)*Math.random(),10); 
	}
	return resultado;
}
	
function xnum_aleatorioD(inicio, fin, Diferente) {
	
		var resultado = Math.round((fin-inicio)*Math.random()) + inicio;
		while (resultado == Diferente) {
			resultado = Math.round((fin-inicio)*Math.random()) + inicio;
		}
		return resultado;
	}
	function xnum_aleatorioD2(inicio, fin, Diferente,diferente2) {
	
		var resultado = Math.round((fin-inicio)*Math.random()) + inicio;
		while (resultado == Diferente || resultado == diferente2) {
			resultado = Math.round((fin-inicio)*Math.random()) + inicio;
		}
		return resultado;
	}
	function Ordena(Algo)
{
	var NArr = Algo;
	for(var i = 1; i< NArr.length; i++)
	{
		for (var j=0; j < i; j++)
			if (NArr[i] < NArr[j])
			{
				var Cmd = NArr[i];
				for (var k=i; k>= j+1; k--)
					NArr[k] = NArr[k-1];
				NArr[j] = Cmd;
			}
	}
	return NArr; 
}
function mayuscula1ra(texto,indice) 
{
	var aux=(texto.substr(indice, 1)).toUpperCase()+texto.substr((indice+1), texto.length);
//	alert(texto.substr(1, 1).toUpperCase());

	return (aux);
}
function modo_moneda(cadena1) 
{
	var cadena = cadena1+"";
	var pos=0;
	if (cadena.indexOf(".", 0) == -1) {
		pos = cadena.length;
		cadena += ".00";
	} else {
		pos = cadena.indexOf(".", 0);
		 var tlong = cadena.length;
		var temp=2-(tlong-(pos+1));
		if (temp>=0) {
			for (var c = 0; c<2-(tlong-(pos+1)); c++) {
				cadena += "0";
			}
		} else {
			cadena = cadena.substr(0, pos+3);
		}
	}
	var apunto = "";
	var tlong = cadena.length-3;
	var dpunto = cadena.substr(pos, cadena.length);
	for (var xc = 0; xc<tlong; xc++) {
		apunto += cadena.substr(xc, 1);
		if ((tlong-(xc+1))%3 == 0 && xc != (tlong-1)) {
			apunto += "*";
		}
	}
	return (apunto+""+dpunto);
}

function Desordena(Arr)
{
	var Desorden = new Array();

	var Temp = GeneArrALea(Arr.length,0,Arr.length-1);
	for (var i=0; i<Arr.length; i++)
	{
		Desorden[i] = Arr[Temp[i]];
	}
	return Desorden;
}

function GeneArrALea(Dimension,Inicio,Fin)
{   

	var NumRnd = Inicio + parseInt((Fin-Inicio+1)*Math.random(),10); 		
	var xArreglo = new Array();
	for(var i = 0; i < Dimension; i++)
	{
		while (Pertenece(NumRnd,xArreglo))	
		{
			NumRnd = Inicio + parseInt((Fin-Inicio+1)*Math.random(),10); 
		}
		xArreglo[i] = NumRnd;
		NumRnd = Inicio + parseInt((Fin-Inicio+1)*Math.random(),10); 
	}
	return xArreglo;  
}

function Pertenece(NumRnd, xArreglo) 
{	
	var Pertenece = false;
	for(var i=0; i < xArreglo.length; i++) if (xArreglo[i] == NumRnd) Pertenece = true;
	return Pertenece;
}


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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es un campo obligatorio.\n'; }
  } if (errors) alert('Los siguientes campos no están completados correctamente:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showHint(url)
{
	if (url.length==0)
	{
		document.getElementById("hintCuerpo").innerHTML="";
		//$("hintCuerpo").html="";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("El navegador no soporta AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);
}
function SetContainerHTML(id,html,processScripts)
{
	mydiv = document.getElementById(id);
	mydiv.innerHTML = html;
	if(processScripts!=false)
	{
		
		var elementos = mydiv.getElementsByTagName('script');
		
		for(i=0;i<elementos.length;i++) 
		{
			var elemento = elementos[i];
			nuevoScript = document.createElement('script');
			nuevoScript.text = elemento.innerHTML;
			if(elemento.src!=null && elemento.src.length>0)
			{
				nuevoScript.src = elemento.src;
			}
			elemento.parentNode.replaceChild(nuevoScript,elemento);
		}
	}
}

function stateChanged()
{
	
	
	if (xmlHttp.readyState==4)
	{
		
		//SetContainerHTML("hintCuerpo",xmlHttp.responseText,true);
		//alert(xmlHttp.responseText);
		document.getElementById("hintCuerpo").innerHTML=xmlHttp.responseText;
		//$("#hintCuerpo").html(xmlHttp.responseText);
		$(function() {
		$("#tablesorter")
			.tablesorter({widthFixed: true, widgets: ['zebra']})
			.tablesorterPager({container: $("#pager")});
		});
		$("#slider").easySlider({
			orientation:'vertical'
		});
	}/*else
	{
		document.getElementById("hintCuerpo").innerHTML="<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"img/loading-p2.gif\"/>&nbsp;&nbsp;&nbsp;<p class=\"Estilo6\">Cargando....<br/>";
	}*/
}

function GetXmlHttpObject()
{
      var xmlHttp=null;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        }
      return xmlHttp;
} 

