﻿function EsEntero(item, valor)
{
	var checkOK = "0123456789."
	var checkStr = item.value
	var allvalid = true
	var allnum = ""
	var comapunto = 0

	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i)
		if (ch == "." || ch == "," )
			comapunto++;
		for (j = 0; j < checkOK.length; j++)
		if (ch == checkOK.charAt(j))
			break
		if (j == checkOK.length)
		{
			allvalid = false
			break
		}
		allnum += ch
	}
	if (!allvalid || comapunto > 1)
	{	
		alert("Número inválido, ingrese enteros sin separador de miles")
		item.value = valor
		item.select()
		item.focus()
		return(false)
	}

	return(true)
}

function fechamayor(diai,mesi,anoi,diat,mest,anot){
	if (parseFloat(anoi)>parseFloat(anot)){
		// es mayor inicio que termino
		return true;
	}else{
		if(parseFloat(mesi)>parseFloat(mest) && parseFloat(anoi)==parseFloat(anot)){
			return true;
		}else{
			if (parseFloat(diai)>parseFloat(diat) && parseFloat(mesi)==parseFloat(mest) && parseFloat(anoi)==parseFloat(anot)){
				return true;
			}
		}
	}
	return false;
}

function Trim(strTexto)
{
var intLargo = strTexto.length;
var intInd = -1;
var intPosI = -1;
var intPosD = 0;

	if( intLargo == 0 ) { return '';}
	for( intInd = 0; intInd < intLargo; intInd++)
	{ if( strTexto.charAt(intInd) != ' ' )
		{ intPosI = intInd, intInd = intLargo }
	}
	if( intPosI == -1 ) { return '' }
	for( intInd = intLargo-1; intInd > -1; intInd-- )
	{ if( strTexto.charAt(intInd) != ' ' )
		{ intPosD = intInd; intInd = -1 }
	}
	return strTexto.substring(intPosI, intPosD+1)
}

function SelCombox(Objeto, valor){
	for (i=0;i < Objeto.options.length; i ++){
		if (Objeto.options[i].value == valor){
			Objeto.options[i].selected = true; 
		}
	}
}

function doEscape(sValue)
{
   // JavaScript doesnt seem to escape the same way than the submit
   //  method of Browsers/html-Servers:
   // For Browsers:  "+ " ---Submit---=> "%2B+" --Server--=> "+ ". Ok.
   // In JavaScript: "+ " --escape()--=> "+%20" --Server--=> "  ". NOT OK !
   var i, j, c;
   var sRet = "";

   if (sValue == null) return null;
   sValue = "" + sValue;
   for (i=0; i<sValue.length; i++)
   {
      c = sValue.charAt(i);
      if ((c >= "0" && c <= "9") ||
          (c >= "a" && c <= "z") ||
          (c >= "A" && c <= "Z"))
         sRet += c;
      else if (c=="+")
         sRet += "%2B";
      else
         sRet += escape(c);
   }

   return sRet;
}

function FechaValida(intDia, intMes, intAno)
{
	Dias = new Array(13)

	Dias[1] = 31;	Dias[2] = 28;	Dias[3] = 31;	Dias[4] = 30;	Dias[5] = 31;	Dias[6] = 30;
	Dias[7] = 31;	Dias[8] = 31;	Dias[9] = 30;	Dias[10] = 31;	Dias[11] = 30;	Dias[12] = 31;

	if (!ValidaNumero(intDia))
	{
		/*alert("DÃ­a invÃ¡lido");*/
		return(false);
	}
		
	if (!ValidaNumero(intAno))
	{
		/*alert("AÃ±o invÃ¡lido");*/
		return(false);
	}
	if (intMes == 2 && intDia == 29 && (intAno % 400) == 0 && (intAno % 100 == 0)) 
	{
		return(true);
	}
	if (intDia > Dias[intMes]) 
	{
		/*alert("Fecha invÃ¡lida");*/
		return(false);
	}
	else 
	{
		return(true);
	}
}

function ValidaNumero(valor)
{
	var checkOK = "0123456789"
	var checkStr = valor
	var error = "no"

	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i)
		for (j = 0; j < checkOK.length; j++)
		{
			error = "si"
			if (ch == checkOK.charAt(j))
			{
				error = "no"
				break
			}				
		}
		if (error == "si" )
		{
			i = checkStr.length
		}
	}
	if (checkStr == "" || error == "si" || checkStr == "null")
	{	
		return(false);
	}
	return(true)
}

function validosc(valora,validos)
{
	var i,validosc,valor;
	validosc=true;
	valor=valora;
	for (i=0;i<valor.length;i++)
	{
		a=valor.substr(i,1);
		if (!a.match(validos))
		{
			validosc=false;
			i=valor.length;
		}
	}
	return validosc;
}

function validosc2(valora,validos,extrac)
{
	var i,validosc,valor;
	validosc=true;
	valor=valora;
	for (i=0;i<valor.length;i++)
	{
		a=valor.substr(i,1);
		if (a!=extrac)
		{
			if (!a.match(validos))
			{
				validosc=false;
				i=valor.length;
			}
		}
	}
	return validosc;
}

function cambia_cab()
{
	var u=0,uu=0,largo=0,largo2=0,sw=0,largo3;
	cod_cabmat = document.forms[0].cabecera.options[document.forms[0].cabecera.options.selectedIndex].value;
	largo = document.forms[0].materias.length;
	for(u=0;u<largo;u++)
	{
		document.forms[0].materias.length--;
	}
	largo = matriz_mat.length;
	
	if (cod_cabmat==0)
	{
		largo3 = document.forms[0].materiass.length;
		for(u=0;u<largo3;u++)
		{
			document.forms[0].materiass.length--;
		}	
	}
	
	for (u=0;u<largo;u++)
	{
		if (cod_cabmat == matriz_mat[u][0])
		{
			cod_materia = matriz_mat[u][1];
			materia 		= matriz_mat[u][2];
			largo2 = document.forms[0].materiass.length;
			sw=0;
			for (uu=0;uu<largo2;uu++)
			{
				valor_s = document.forms[0].materiass.options[uu].value;
				if (valor_s == cod_materia) 
				  sw=1;
			}
			if (sw==0)
			{
				opciones = new Option(materia,cod_materia);
				document.forms[0].materias.options[document.forms[0].materias.length++] = opciones;
			}
		}
	}
}

function Asociar(selectDisponible, selectAsociado) 
{
var opcion, indiceDisponible;
var i, j;
    with (selectDisponible) 
    {
        i = 0;
        while ( i < length ) 
        {
            if (options[i].selected) 
            {
                indiceDisponible = i;
                opcion = new Option(options[i].text,options[i].value);
                selectAsociado.options[selectAsociado.length++] = opcion;

                if (i < length-1)
                   for (j = i+1; j < length; j++) {
                      opcion = new Option(options[j].text,options[j].value);
                      options[indiceDisponible] = opcion;
                      if (options[j].selected)
                         options[indiceDisponible].selected = true;
                      indiceDisponible++;
                   }
                length--;
                i = 0;
            }
            else
                i++;
        }
    }
    
    if (window.navigator.appName == "Netscape")
        history.go(0);
}

function busca_descript()
{
	var tipos;
	tipos=document.forms[0].tipos.options[document.forms[0].tipos.options.selectedIndex].value;
	largo = document.forms[0].materiass.length;
	for (i=0;i<largo;i++){
		document.forms[0].materiass.options[i].selected=true;
	}
	document.forms[0].submit()
}

function inicia_busqueda()
{
	var nodo,cant=0,descf="";
	var diai="",mesi="",anoi="",combo="";
	var diat="",mest="",anot="",combo_desc="";
	var dia_i="",mes_i="",ano_i="";
	var dia_t="",mes_t="",ano_t="",objeto1="",objeto2="";
	var nodo_padre,strTexto="",sltCombosM="",sw=0;
	var validos = /[a-zA-Z0-9_áéíóúäëïöüàèìòùñÑ.,:;*+?¿$ ]/;
	var validosn = /[0-9]/;

	if (typeof(document.forms[0].txt_parametro) != 'undefined') {
		if(document.forms[0].txt_parametro.value == 'Búsqueda..') {
			document.forms[0].txt_parametro.value = '';
		}
	}
	
	if (typeof(document.forms[0].nombre) != 'undefined') {
		document.forms[0].nombre.value = Trim(document.forms[0].nombre.value);
		if (document.forms[0].nombre.value != ""){
			if (parseInt(document.forms[0].nombre.value.length)<3){
				alert("El texto a buscar debe ser mayor a 3 carácteres");
				document.forms[0].nombre.focus();
				return;
			}
		}
	}

	if (typeof(document.forms[0].materiass) != 'undefined') {
		largo_mat = document.forms[0].materiass.length;
		document.forms[0].dia_pi.value = Trim(document.forms[0].dia_pi.value);
		dia_pi= document.forms[0].dia_pi.value;
		mes_pi=document.forms[0].mes_pi.options[document.forms[0].mes_pi.options.selectedIndex].value;
		document.forms[0].ano_pi.value = Trim(document.forms[0].ano_pi.value);
		ano_pi=document.forms[0].ano_pi.value;
	
		document.forms[0].dia_pf.value = Trim(document.forms[0].dia_pf.value);
		dia_pf=document.forms[0].dia_pf.value;
		mes_pf=document.forms[0].mes_pf.options[document.forms[0].mes_pf.options.selectedIndex].value;
		document.forms[0].ano_pf.value = Trim(document.forms[0].ano_pf.value);
		ano_pf=document.forms[0].ano_pf.value;
	
		cabecera = document.forms[0].cabecera.options[document.forms[0].cabecera.options.selectedIndex].value;
		if ((document.forms[0].nombre.value.length<3 && document.forms[0].tipos.options[document.forms[0].tipos.options.selectedIndex].value==0) && cabecera<1 && largo_mat==0 && (dia_pi.length==0 && dia_pf.length==0 && ano_pi.length==0 && ano_pf.length==0)) {
			alert("Debe ingresar parámetros de búsqueda");
			return;
		}
	
		if (dia_pi.length>0 || dia_pf.length>0 || ano_pi.length>0 || ano_pf.length>0) {
			if (dia_pi.length==0)
			{alert("Debe ingresar día");document.forms[0].dia_pi.focus();return;}
			if (dia_pf.length==0)
			{alert("Debe ingresar día");document.forms[0].dia_pf.focus();return;}
			if (ano_pi.length<4)
			{alert("Debe ingresar año");document.forms[0].ano_pi.focus();return;}
			if (ano_pf.length<4)
			{alert("Debe ingresar año");document.forms[0].ano_pf.focus();return;}
		
		  if (validosc(dia_pi,validosn)==false) {
		  	alert("Día no válido");
				document.forms[0].dia_pi.focus()
			  return
			}
			if (validosc(ano_pi,validosn)==false) {
		  	alert("Año no válido");
				document.forms[0].ano_pi.focus()
		  	return
	  	}
			if (validosc(dia_pf,validosn)==false) {
		  	alert("Día no válido");
				document.forms[0].dia_pf.focus()
		  	return
			}
			if (validosc(ano_pf,validosn)==false) {
		  	alert("Año no válido");
				document.forms[0].ano_pf.focus()
		  	return
	  	}
			if(!FechaValida(dia_pi,mes_pi,ano_pi))
			{alert("Fecha incorrecta");document.forms[0].dia_pi.focus();return}
			if(!FechaValida(dia_pf,mes_pf,ano_pf))
			{alert("Fecha incorrecta");document.forms[0].dia_pf.focus();return}
			if (parseFloat(ano_pf)<parseFloat(ano_pi))
			{alert("La fecha de inicio debe ser anterior a la de término");document.forms[0].ano_pi.focus();return}
			if (parseFloat(ano_pf)==parseFloat(ano_pi) && parseFloat(mes_pf)<parseFloat(mes_pi))
			{alert("La fecha de inicio debe ser anterior a la de término");document.forms[0].mes_pi.focus();return}
			if (parseFloat(ano_pf)==parseFloat(ano_pi) && parseFloat(mes_pf)==parseFloat(mes_pi) && parseFloat(dia_pf)<parseFloat(dia_pi))
			{alert("La fecha de inicio debe ser anterior a la de término");document.forms[0].dia_pi.focus();return}
		}
	}
	if (typeof(document.forms[0].nombre) != 'undefined') {
		if (validosc2(document.forms[0].nombre.value,validos,"-")==false){
			alert("La palabra contiene caracteres no válidos");
			document.forms[0].nombre.focus();
			return;
		}
	}
	for (nI = 0 ; nI < document.forms[0].elements.length; nI++) {
		//alert(document.forms[0].elements[nI].type)
		//if (nI>9){
		if (document.forms[0].elements[nI].type == 'text' || document.forms[0].elements[nI].type == 'select-one' || document.forms[0].elements[nI].type == 'select-multiple') {
		   //alert(document.forms[0].elements[nI].name);
		   if (document.forms[0].elements[nI].name.substr(0,3)=="dia"){	
	   			///// Rango de Inicio
	   			objeto1=document.forms[0].elements[nI].name;
				codigo_desc=objeto1.substring(5,10);
				diai=document.forms[0].elements[nI].value;
				combo = document.forms[0].elements[nI+1].name;
				mesi=eval("document.forms[0]."+combo+".options[document.forms[0]."+combo+".options.selectedIndex].value");
				anoi=document.forms[0].elements[nI+2].value;
				objeto11 = document.forms[0].elements[nI+2].name;
				swi=0;
				
				if (diai.length>0 || anoi.length>0){	
					if ((diai!="00" && diai!="0" && parseFloat(anoi)>=1900)){
						if(FechaValida(diai,mesi,anoi)){
							swi=1;
						}
					}
					
					if (swi==0){
						alert("Fecha incorrecta")
						eval("document.forms[0]."+objeto1+".focus()");
						return;
					}
					
					descf=descf + "&descf=" + codigo_desc
					dia_i=dia_i + "&" + objeto1 + "=" + diai;
					mes_i=mes_i + "&" + combo + "=" + mesi;
					ano_i=ano_i + "&" + objeto11 + "=" + anoi;
				}
				//////////// Rango de Termino
				objeto2=document.forms[0].elements[nI+3].name;
				diat=document.forms[0].elements[nI+3].value;
				combo = document.forms[0].elements[nI+4].name;
				mest=eval("document.forms[0]."+combo+".options[document.forms[0]."+combo+".options.selectedIndex].value");
				anot=document.forms[0].elements[nI+5].value;
				objeto22 = document.forms[0].elements[nI+5].name;
				swt=0;
				if (diat.length>0 || anot.length>0){
					if ((diat!="00" && diat!="0" && parseFloat(anot)>=1900)){
						if(FechaValida(diat,mest,anot)){
							swt=1
						}
					}
					
					if (swt==0){
						alert("Fecha incorrecta")
						eval("document.forms[0]."+objeto2+".focus()")
						return
					}
					dia_t=dia_t + "&" + objeto2 + "=" + diat;
					mes_t=mes_t + "&" + combo + "=" + mest;
					ano_t=ano_t + "&" + objeto22 + "=" + anot;
				}	
				if (parseFloat(anoi)>0 || parseFloat(anot)>0){
					if (anoi.length==0){
						alert("Debe ingresar fecha de inicio");
						eval("document.forms[0]."+objeto1+".focus()");
						return;
					}
					if (anot.length==0){
						alert("Debe ingresar fecha de término");
						eval("document.forms[0]."+objeto2+".focus()");
						return;
					}
					
					if (fechamayor(diai,mesi,anoi,diat,mest,anot)){
						alert("La fecha inicio rango debe ser anterior a la de término");
						eval("document.forms[0]."+objeto1+".focus()");
						return;
					}
				}
				nI=nI+5;
		   }else{
	   		   // texto libre
			   if (document.forms[0].elements[nI].type == "text" && document.forms[0].elements[nI].name != "ops-nombre" && document.forms[0].elements[nI].name != "txt_parametro" && document.forms[0].elements[nI].name != "nombre")
			   {
				  if (document.forms[0].elements[nI].value.length>=1 && document.forms[0].elements[nI].name.substr(0,3)!="mes" )
					{		
					
						if (document.forms[0].elements[nI].name.substr(0,3) == "tan"){
							if (!EsEntero(document.forms[0].elements[nI], document.forms[0].elements[nI].value)){
								return;
							}
							if (!EsEntero(document.forms[0].elements[nI+1], document.forms[0].elements[nI+1].value)){
								return;
							}
							strTexto = strTexto+"&NM="+document.forms[0].elements[nI].name;
							nI = nI + 1;
						}else{
							if (validosc2(document.forms[0].elements[nI].value,validos,"-")==false)	
							{
							   alert("La palabra contiene caracteres no válidos");
								document.forms[0].elements[nI].focus()
							  return
							}
						    strTexto = strTexto+"&TL="+document.forms[0].elements[nI].name;
						}
					}
				}
			   
				// normalizado seleccion
				if (document.forms[0].elements[nI].type == "select-one"){ 
					cant=cant+1
					sw=0;
			  		objeto = document.forms[0].elements[nI].name;
					if (objeto.substr(0,3)=="bat"){
						if (eval("document.forms[0]."+objeto+".length")>0 && eval("document.forms[0]."+objeto+".options[document.forms[0]."+objeto+".options.selectedIndex].value") != ''){
							sw=1;
							combo_desc=combo_desc+"&combo_desc="+objeto.substr(4);
								 sltCombosM = sltCombosM + "&combo"+cant+"="+eval("document.forms[0]."+objeto+".options[document.forms[0]."+objeto+".options.selectedIndex].value");
						}
					}
					if (sw==0){
						cant=cant-1;
					}					  
			  	}
					
		   	    // normalizado multiple seleccion
				if (document.forms[0].elements[nI].type == "select-multiple"){ 
					cant=cant+1
					sw=0;
			  		objeto = document.forms[0].elements[nI].name;
					if (objeto.substr(0,3)=="bat"){
						if (eval("document.forms[0]."+objeto+".length")>0){
							sw=1;
							combo_desc=combo_desc+"&combo_desc="+objeto.substr(4);
							for (Kr = 0 ; Kr < eval("document.forms[0]."+objeto+".length") ; Kr++){ 
								 eval("document.forms[0]."+objeto+".options[Kr].selected=true");
								 sltCombosM = sltCombosM + "&combo"+cant+"="+eval("document.forms[0]."+objeto+".options["+Kr+"].value");
							}
						}
					}else{
						for (Kr = 0 ; Kr < eval("document.forms[0]."+objeto+".length") ; Kr++){
							eval("document.forms[0]."+objeto+".options[Kr].selected=false") 
						}
					}
					
					if (sw==0){
						cant=cant-1;
					}					  
			  	}
			}   
		}
	}
	
	if (typeof(document.forms[0].materiass) != 'undefined') {
		largo = document.forms[0].materiass.length
		for (i=0;i<largo;i++){
			document.forms[0].materiass.options[i].selected=true
		}
	}
	cSendPage = '';
	if (typeof(document.forms[0].glb_send_page) != 'undefined') {
		document.forms[0].glb_send_page.value = 'METADATA_BARRA';
	}
	else {
		cSendPage = '&glb_send_page=METADATA_BARRA';
	}
	//var c_url = document.forms[0].action;
	var c_url = '' + top.location;
	var cLink = '' + top.location;
	
	if (cLink.indexOf('?') != -1) {
 	  cLink = cLink.substring(0, cLink.indexOf('?'));
	}
	if (c_url.indexOf('?') != -1) {
 	  c_url = c_url.substring(0, c_url.indexOf('?'));
	}
	
	if (typeof(document.forms[0].nombre) != 'undefined') {
  	if (cant == 0 && document.forms[0].nombre.value == '') {
	 	  alert('Debe seleccionar una opción');
		  return;
	  }
	}
	else {
  	if (cant == 0) {
	 	  alert('Debe seleccionar una opción');
		  return;
	  }
	}
	
	if (c_url.indexOf('opensite_20080408165106.aspx') != -1) {
		c_url = 'opensite_20080506105136.aspx';
	}

	c_url = "/busca_tu_combo_viajero";
	
	document.forms[0].action = c_url + "?cant=" + cant + strTexto + sltCombosM + descf + combo_desc + cSendPage;
	document.forms[0].submit()
}

function BusquedaAvanzada() {
var frm = document.forms[0];
var cUrl = document.forms[0].action;
var cPalabra = '';

	frm.glb_send_page.value = 'BUSAVA';
	if (BusquedaAvanzada.arguments[0] == 'buscar') {
		if (Trim(frm.txt_busava_palabra.value) == '') {
			alert('Debe una palabra a buscar');
			frm.txt_busava_palabra.focus();
			return;
		}
		cPalabra = frm.txt_busava_palabra.value;
		cPalabra = cPalabra.replace(/[+]/gi, '');
		cPalabra = cPalabra.replace(/,/gi, '');
		cPalabra = cPalabra.replace(/ and /gi, ' ');
		cPalabra = cPalabra.replace(/ or /gi, ' ');
		var arrPalabra = cPalabra.split(' ');
		cPalabra = '';
		for (var i = 0; i < arrPalabra.length; i++) {
			if (arrPalabra[i].length > 0) {
				if (cPalabra == '') {
					cPalabra += arrPalabra[i];
				}
				else {
					cPalabra += ' ' + arrPalabra[i];
				}
			}
		}
		frm.txt_busava_palabra.value = cPalabra;
		frm.hdd_bavanzada.value = 'buscar';
	}
	frm.submit();
}

function goBuscarMasMt() {
var cId = goBuscarMasMt.arguments[0];
var cIdBoton = goBuscarMasMt.arguments[1];

	for(var index = 0; index < 1000; index++) {
		if(document.getElementById('bhidden' + index)) {
    	document.getElementById('bhidden' + index).style.display = 'none';
			document.getElementById('bclick' + index).style.display = 'block';
		}
		else
			break;
	}
	o = document.getElementById(cId);
	o.style.display = 'block';

	o = document.getElementById(cIdBoton);
	o.style.display = 'none';
}
function gobmtRecorrer(cAccion) {
var frm = document.forms[0];

	if (cAccion == '0') {
	    frm.hdd_bmtPosicion.value = '0';
	}
	if (cAccion == '1') {
	    frm.hdd_bmtPosicion.value = parseInt(frm.hdd_bmtPosicion.value) - 1;
	}
	if (cAccion == '2') {
	    frm.hdd_bmtPosicion.value = parseInt(frm.hdd_bmtPosicion.value) + 1;
	}
	if (cAccion == '3') {
	    frm.hdd_bmtPosicion.value = frm.hdd_bmtPagina.value - 1;
	}
	frm.action = '';
	frm.submit();
}