function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function ajax_links_hbl(capa,PASO,cadena){
	AjaxLinks=objetoAjax();
	AjaxLinks.open("POST",PASO,true);
	AjaxLinks.onreadystatechange=function() {
		if (AjaxLinks.readyState==4) 
			{document.getElementById(capa).innerHTML = AjaxLinks.responseText

			}
		} 
	AjaxLinks.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxLinks.send("valor="+cadena)
}

function ajax_links_booking(capa,PASO,cadena){
	AjaxLinksB=objetoAjax();
	AjaxLinksB.open("POST",PASO,true);
	AjaxLinksB.onreadystatechange=function() {
		if (AjaxLinksB.readyState==4) 
			{document.getElementById(capa).innerHTML = AjaxLinksB.responseText

			}
		} 
	AjaxLinksB.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxLinksB.send("valor="+cadena)
}

function genericAjax(capa,PASO,cadena){

	genAjaxA=objetoAjax();
	genAjaxA.open("POST",PASO,true);
	genAjaxA.onreadystatechange=function() {
		if (genAjaxA.readyState==4) 
			{document.getElementById(capa).innerHTML = genAjaxA.responseText

			}
		} 
	genAjaxA.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	genAjaxA.send("valor="+cadena)
}

function LstAjax(capa,PASO){

	AjaxLst=objetoAjax();
	AjaxLst.open("POST",PASO,true);
	AjaxLst.onreadystatechange=function() {
		if (AjaxLst.readyState==4) 
			{document.getElementById(capa).innerHTML = AjaxLst.responseText
 if(document.getElementById('folio_documento')){
 document.getElementById('folio_documento').click()
 }
			}
		} 
	AjaxLst.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxLst.send(null)
}

function LstAjax1(){

	AjaxLst1=objetoAjax();
	AjaxLst1.open("POST",'partidas_bl.php?booking='+document.getElementById('h_booking').value,true);
	AjaxLst1.onreadystatechange=function() {
		if (AjaxLst1.readyState==4) 
			{document.getElementById('divPartidas').innerHTML = AjaxLst.responseText

			}
		} 
	AjaxLst1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxLst1.send(null)
}

function AjaxCargo(capa,PASO,cadena){

	AjaxCarg=objetoAjax();
	AjaxCarg.open("POST",PASO,true);
	AjaxCarg.onreadystatechange=function() {
		if (AjaxCarg.readyState==4) 
			{document.getElementById(capa).innerHTML = AjaxCarg.responseText

			}
		} 
	AjaxCarg.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxCarg.send("valor="+cadena)
}

function AjaxContenedor(capa,PASO,cadena){
	AjaxCont=objetoAjax();
	AjaxCont.open("POST",PASO,true);
	AjaxCont.onreadystatechange=function() {
		if (AjaxCont.readyState==4) 
			{document.getElementById(capa).innerHTML = AjaxCont.responseText
			}
		} 
	AjaxCont.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	AjaxCont.send("valor="+cadena)
}

function valida_importe(e) {

	tecla=(document.all) ? e.keyCode : e.which;

	if((tecla < 48 || tecla > 57) && (tecla != 46))

		return false;

}

function valida_entero(e) {

	tecla=(document.all) ? e.keyCode : e.which;

	if((tecla < 48 || tecla > 57))

		return false;

}

function valida_alfanumerico(e) {

	tecla=(document.all) ? e.keyCode : e.which;

	if((tecla >= 65 && tecla <= 90))

		return true;

	else if ((tecla >= 97 && tecla <= 122))

		return true;

	else if ((tecla >= 48 && tecla <= 57))

		return true;

	else if ((tecla == 32 || tecla == 44 || tecla == 58 || tecla == 46))

		return true;

	else

		return false;

}

function EndsWith(texto,criterio) {

	var liPos = texto.lastIndexOf(criterio);

	if ( liPos < 0 )

		return false;

	if ( criterio.length + liPos == texto.length )

		return true;

	return false;

}

function formato_importe(texto) {

	if (texto) {

		return "$" + number_format(texto,2,".",",");

	} else

		return "$0.00";

}

function number_format( number, decimals, dec_point, thousands_sep ) {

    var n = number, prec = decimals, dec = dec_point, sep = thousands_sep;

    n = !isFinite(+n) ? 0 : +n;

    prec = !isFinite(+prec) ? 0 : Math.abs(prec);

    sep = sep == undefined ? ',' : sep;

 

    var s = n.toFixed(prec),

        abs = Math.abs(n).toFixed(prec),

        _, i;

 

    if (abs > 1000) {

        _ = abs.split(/\D/);

        i = _[0].length % 3 || 3;

 

        _[0] = s.slice(0,i + (n < 0)) +

              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

 

        s = _.join(dec || '.');

    }

 

    return s;

}

function dame_importe(texto) {

	texto = texto.replace("$", "");

	texto = texto.replace(",", "");

	return texto;

}

function dame_porcentaje(texto) {

	return texto.replace("%", "");

}

function dameFecha(texto) {

	// fecha en yyyy-mm-dd

	var re_dt = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/;

	// check format

	if (!re_dt.test(texto))

		return null;

	var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1), RegExp.$1);

	return dt_test;

}

function dameTiempo(texto) {

	// Tiempo en HH:MM

	var re_tm = /^(\d{1,2})\:(\d{1,2})$/;

	// check format

	if (!re_tm.test(texto))

		return null;

	var dt_time = new Date();

	dt_time.setTime(0);

	dt_time.setHours(RegExp.$1, RegExp.$2, 0, 0);

	return dt_time;

}

function valores_forma(forma,objeto) {

	var laDatos = [];

	var liIdx;

	

	// Información del campo

	if ( objeto ) {

		var loObjCampo = [];

		loObjCampo["name"] = "campo";

		loObjCampo["value"] = objeto.name;

		laDatos.push( loObjCampo );

	}

	

	// Pasa los datos de la forma a objeto

	if ( forma ) {

		for ( liIdx=0 ; liIdx<forma.elements.length ; liIdx++ ) {

			// Para checkboxes 0 o 1 como valor

			if (forma.elements[liIdx].type == "checkbox")

				forma.elements[liIdx].value = ( forma.elements[liIdx].checked ? "1" : "0" );

			laDatos.push( forma.elements[liIdx] );

		}

	}

	return laDatos;

}

/* valida numeros */
function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;
	var isValid = true;
	var decCount = 0;		// number of decimal points in the string
	// convert to a string for performing string comparisons.
	numstr += "";	
	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
   	return isValid;
}  // end IsNum

function loadin(input,Tspan,msg){
		if(input!=''){
			if (document.getElementById(input).value!='') { 
				if(document.getElementById(Tspan)){
				document.getElementById(Tspan).innerHTML=''+msg+' <img src="../images/loading.gif" align="absbottom" />';
				}
			}
		}else{
			if(document.getElementById(Tspan)){
			document.getElementById(Tspan).innerHTML=''+msg+' <img src="../images/loading.gif" align="absbottom" />';
			}
		}
}

function loadin_t(input,Tspan,msg){
	document.getElementById(Tspan).innerHTML='';
}

function unloadin(Tspan){
	if(document.getElementById(Tspan)){
	document.getElementById(Tspan).innerHTML='';
	}
}
function unloadin_(Tspan){
	if(document.getElementById(Tspan)){
	document.getElementById(Tspan).innerHTML='Mensaje:';
	}
}
function loadin_(input,Tspan,msg,img){
		if(input!=''){
			if (document.getElementById(input).value!='') { 
				if(document.getElementById(Tspan)){
				document.getElementById(Tspan).innerHTML=''+msg+' <img src="'+img+'" align="absbottom" />';
				}
			}
		}else{
			if(document.getElementById(Tspan)){
			document.getElementById(Tspan).innerHTML=''+msg+' <img src="'+img+'" align="absbottom" />';
			}
		}
}

function loadin_t_(input,Tspan,msg){
	document.getElementById(Tspan).innerHTML='';
}

function unloadin_(Tspan){
	if(document.getElementById(Tspan)){
	document.getElementById(Tspan).innerHTML='';
	}
}

function asign(objeto,valor){
	if(objeto) objeto.value=valor
	//alert(document.getElementById(objeto))
}


//Anyadir Filas Dinámicamente en HTML by jotaerre.net
          function addRow(tableID) {

               var table = document.getElementById(tableID);

 

               var rowCount = table.rows.length;

               var row = table.insertRow(rowCount);
			   


               var cell1 = row.insertCell(0);

               var element1 = document.createElement("input");

               element1.type = "checkbox";

               cell1.appendChild(element1);

 

               var cell2 = row.insertCell(1);

               var element2 = document.createElement("input");

               element2.type = "text";
			   
			   element2.id = 'exportador[]';
			   
			   element2.name = 'exportador[]';
			   
			   element2.size = 40;
			   
               cell2.appendChild(element2);
			   
			   
			   var cell3 = row.insertCell(2);

               var element3 = document.createElement("input");

               element3.type = "text";
			   
			   element3.size = 5;
			   
   			   element3.id = 'cantidad_bultos[]';
			   
			   element3.name = 'cantidad_bultos[]';

               cell3.appendChild(element3);
			   
	 
			   
			   
			   var cell4 = row.insertCell(3);

               var element4 = document.createElement("input");

               element4.type = "text";
			   
			   element4.size = 10;
			   
			   element4.id = 'fecha_entrada[]';
			   
			   element4.name = 'fecha_entrada[]';

               cell4.appendChild(element4);
			   
			   
			   var cell5 = row.insertCell(4);

               var element5 = document.createElement("input");

               element5.type = "text";
			   
			   element5.size = 10;
			   
			   element5.id = 'peso_neto[]';
			   
			   element5.name = 'peso_neto[]';

               cell5.appendChild(element5);
			   
			   
			   var cell6 = row.insertCell(5);

               var element6 = document.createElement("input");

               element6.type = "text";
			   
  		       element6.size = 10;
			   
			   element6.id = 'peso_neto[]';
			   
			   element6.name = 'peso_neto[]';

               cell6.appendChild(element6);
			   
			   
			   
			   var cell7 = row.insertCell(6);

               var element7 = document.createElement("input");

               element7.type = "text";
			   
			   element7.size = 10;
			   
			   element7.id = 'flash_point[]';
			   
			   element7.name = 'flash_point[]';
               
			   cell7.appendChild(element7);
			   
			   
			   var cell8 = row.insertCell(7);

               var element8 = document.createElement("input");

               element8.type = "text";
			   
			   element8.size = 5;
			   
			   element8.id = 'imo[]';
			   
			   element8.name = 'imo[]';

               cell8.appendChild(element8);
			   
			   
			   var cell9 = row.insertCell(8);

               var element9 = document.createElement("input");

               element9.type = "text";
			   
			   element9.size = 5;
			   
			   element9.id = 'un[]';
			   
			   element9.name = 'un[]';

               cell9.appendChild(element9);
			   
			   
			   var cell10 = row.insertCell(9);

               var element10 = document.createElement("input");

               element10.type = "text";
			   
			   element10.size = 5;
			   
			   element10.id = 'packing_group[]';
			   
			   element10.name = 'packing_group[]';

               cell10.appendChild(element10);
			   
			   
			   var cell11 = row.insertCell(10);

               var element11 = document.createElement("input");

               element11.type = "text";
			   
			   element11.size = 5;
			   
			   element11.id = 'riesgo_secundario[]';
			   
			   element11.name = 'riesgo_secundario[]';

               cell11.appendChild(element11);
			   
			   
			   var cell12 = row.insertCell(11);

               var element12 = document.createElement("input");

               element12.type = "text";
			   
			   element12.size = 40;
			   
			   element12.id = 'nombre_producto[]';
			   
			   element12.name = 'nombre_producto[]';

               cell12.appendChild(element12);

          }

 

          function deleteRow(tableID) {

               try {

               var table = document.getElementById(tableID);

               var rowCount = table.rows.length;

 

               for(var i=0; i<rowCount; i++) {

                    var row = table.rows[i];

                    var chkbox = row.cells[0].childNodes[0];

                    if(null != chkbox && true == chkbox.checked) {

                         table.deleteRow(i);

                         rowCount--;

                         i--;

                    }

               }

               }catch(e) {

                    alert(e);

               }

          }

 