function JS_Redireccionar(url) {
	document.location.href = url;
}
function JS_RedireccionarP(url) {
	window.parent.document.location.href = url;
}
function JS_RemplazarStr(str, esto, porEsto) {
	if (str == "") {	return ""; }
	return str.replace(esto, porEsto); 
}
function JS_GetNum(str) {
	if ((str==undefined) || (str=="")) { str="0"; }
	return parseFloat(str.replace(",", ".")); 
}
function JS_FormatNum(num, numDecimales) {
	return num.toFixed(numDecimales);
}
function JS_FormatMoneda(num) {
	return num.toFixed(2);
}
function JS_FormatMonedaStr(num) {
	return JS_FormatMoneda(num).replace(".", ","); 
}
function JS_FormatMonedaComa(num) {
	return JS_RemplazarStr(JS_FormatMoneda(num), ".", ",");
}
function JS_GetNumMoneda(str) {
	return JS_FormatMoneda(JS_GetNum(str));
}
function JS_AutoFormat(id) {
	return JS_Value(id, JS_FormatMonedaComa(JS_GetNum(JS_GetValue(id))));
}
function JS_SetNum(num) {
	return JS_Round(num).toString().replace(".", ","); 
}
function JS_Round(num) {
	return Math.round(num*100)/100; 
}
function JS_NS4() {   	return (document.layers); }
function JS_NS6() {   	return (document.getElementById); }
function JS_IE() {		return (document.all); }
function JS_GetPP(layerID) {
	if (JS_NS4()) {		return eval('window.parent.parent.document.' + layerID); }
	if (JS_NS6()) {		return eval('window.parent.parent.document.getElementById("' + layerID + '")'); }
	if (JS_IE()) {		return eval('window.parent.parent.document.all.' + layerID); }
}
function JS_GetPPS(layerID) {
	if (!JS_GetPP(layerID)) { return null; } 
	if (JS_NS4()) {		return eval('window.parent.parent.document.' + layerID); }
	if (JS_NS6()) {		return eval('window.parent.parent.document.getElementById("' + layerID + '").style'); }
	if (JS_IE()) {		return eval('window.parent.parent.document.all.' + layerID + '.style'); }
}
function JS_GetP(layerID) {
	if (JS_NS4()) {		return eval('window.parent.document.' + layerID); }
	if (JS_NS6()) {		return eval('window.parent.document.getElementById("' + layerID + '")'); }
	if (JS_IE()) {		return eval('window.parent.document.all.' + layerID); }
}
function JS_GetPS(layerID) {
	if (!JS_GetP(layerID)) { return null; } 
	if (JS_NS4()) {		return eval('window.parent.document.' + layerID); }
	if (JS_NS6()) {		return eval('window.parent.document.getElementById("' + layerID + '").style'); }
	if (JS_IE()) {		return eval('window.parent.document.all.' + layerID + '.style'); }
}
function JS_Get(layerID) {
	if (JS_NS4()) {		return eval('document.' + layerID); }
	if (JS_NS6()) {		return eval('document.getElementById("' + layerID + '")'); }
	if (JS_IE()) {		return eval('document.all.' + layerID); }
}
function JS_GetS(layerID) {
	if (!JS_Get(layerID)) { return null; } 
	if (JS_NS4()) {		return eval('document.' + layerID); }
	if (JS_NS6()) {		return eval('document.getElementById("' + layerID + '").style'); }
	if (JS_IE()) {		return eval('document.all.' + layerID + '.style'); }
}
function JS_AbrirPopup(pagina, titulo, w, h, conMenu, conScroll, resiz) {
	var menuStr = (conMenu)?"yes":"no";
	var scrollStr = (conScroll)?"yes":"no";
	var resizeStr = (resiz)?"yes":"no";

	winopt ="menubar="+menuStr+",scrollbars="+scrollStr+",resizable="+resizeStr+",";
	winopt +="marginwidth=0, marginheight=0, leftmargin=0, topmargin=0,";
	winopt +="width="+w+",height="+h+",";
	winopt +="left=0,top=0,screenLeft=0,screenTop=0";
	
	var vent = window.open(pagina,"Billete",winopt);
	vent.focus();
}
function JS_Tab(t, n, a, d) {
	for (var i=1; i<=n; i++) {
		JS_Display("divTab_"+i, (i==t));
		r = JS_Get("idTab_"+i);
		if (r) { r.className = (i==t) ? "css_tab_on" : "css_tab"; }
	}
	arr = d.toString().split(",");
	for (var i=0; i<arr.length; i++) {
		r = JS_Get(arr[i]); if (r) { r.width = a; }
	}
	JS_Value("hidTab", t);
}
function JS_Src(tagID, txt) {
	var r = JS_Get(tagID);
	if (r) { r.src = txt; }
}
function JS_Inner(tagID, txt) {
	var r = JS_Get(tagID);
	if (r) { r.innerHTML = txt; }
}
function JS_InnerP(tagID, txt) {
	var r = JS_GetP(tagID);
	if (r) { r.innerHTML = txt; }
}
function JS_GetValue(tagID) {
	var r = JS_Get(tagID);
	if (r) { return r.value; }
	return "err";
}
function JS_GetValueP(tagID) {
	var r = JS_GetP(tagID);
	if (r) { return r.value; }
	return "err";
}
function JS_Value(tagID, txt) {
	var r = JS_Get(tagID);
	if (r) { r.value = txt; }
}
function JS_ValueP(tagID, txt) {
	var r = JS_GetP(tagID);
	if (r) { r.value = txt; }
}
function JS_Mostrar(tagID, siNo) {
	var r = JS_GetS(tagID);
	if (r) { r.visibility = (siNo)?"visible":"hidden"; }
}
function JS_SetDisabled(tagID, siNo) {
	var r = JS_Get(tagID);
	if (r) { r.disabled = (siNo); }
}
function JS_Display(tagID, siNo) {
	var r = JS_GetS(tagID);
	if (r) { r.display = (siNo)?"block":"none"; }
}
function JS_DisplayP(tagID, siNo) {
	var r = JS_GetPS(tagID);
	if (r) { r.display = (siNo)?"block":"none"; }
}
function JS_ToggleDisplay(id) {
	var r = JS_GetS(id);
	if (r) { r.display = (r.display=="none") ? "block" : "none"; }
}
function JS_CambiarTextoCheck(lbl, siNo, a, b) {
	var r = JS_Get(lbl);
	r.innerHTML = (siNo)? a : b;
}
function JS_CambiarTextoCheckSiNo(lbl, siNo) {
	var r = JS_Get(lbl);
	r.innerHTML = (siNo)? "(Si)": "(No)";
}
function JS_ToggleCheck(id) {
	var r = JS_Get(id);
	if (r) { r.checked = !r.checked; }
}
function JS_CambiarEmpresa(sel, vble, cod) {
	var pars = (parseInt(cod) > 0) ? "&cod="+cod : "";
	JS_Redireccionar('?'+vble+'='+sel[sel.selectedIndex].value+pars);
}
function JS_FiltroSelectOnChange(sel, vble) {
	JS_Redireccionar('?'+vble+'='+sel[sel.selectedIndex].value);
}
function JS_FiltroCheckOnClick(ckb, vble) {
	JS_Redireccionar('?'+vble+'='+((ckb.checked)?"si":"no"));
}
function JS_Timer(a, b, t) {
	JS_Inner(a, b);
	setTimeout('JS_Timer(\''+a+'\',\''+b+'\','+t+')', t) ;
}
function JS_SubirFoto(n, opc) {
	JS_Display("spanActual"+n, (opc==1));
	JS_Display("spanFile"+n, (opc==2));
	JS_Display("spanPie"+n, (opc==2));
	JS_Display("spanSelec"+n, (opc==3));
}
function JS_MostrarFoto(sel, imgID, dir) {
	var foto = sel[sel.selectedIndex].value;
	JS_Src(imgID, dir+foto);
}
function JS_SetBGColor(id, color) {
	r = JS_GetS(id);
	if (r) { r.backgroundColor = color; }
}
function JS_ClassName(id, css) {
	var r = JS_Get(id);
	if (r) { r.className = css; }
}

function JS_SeleccionarFila(nombre, n) {
	var modoEdicion = parseInt(JS_GetValue("hidFilaEstaModoEdicion_"+n));
	if (modoEdicion) { return false; }
	JS_SetBGColor(nombre + n, 0xE2CE94);

	var codAnt = JS_GetValue("hidFilaSelecAnterior");
	
	deseleccionar = (codAnt == n);
	JS_SetBGColor(nombre+n, ((deseleccionar)?"":0xE2CE94))

	JS_Value("hidFilaSelecAnterior", ((deseleccionar)?"":n));
	if ((codAnt != "") && (!deseleccionar)) { JS_SetBGColor(nombre+codAnt, ""); }
	
	return deseleccionar;
}
function JS_ModoEdicion(cod, modoEdicion, num) {
	JS_Value("hidFilaEstaModoEdicion_"+cod, (modoEdicion?1:0));
	for (var i=1; i<=num; i++) {
		JS_Display("divEdicion_"+i+"_"+cod+"_on", modoEdicion);
		JS_Display("divEdicion_"+i+"_"+cod+"_off", !modoEdicion);
	}
}
function JS_ModoEdicionP(cod, modoEdicion, num) {
	JS_ValueP("hidFilaEstaModoEdicion_"+cod, (modoEdicion?1:0));
	for (var i=1; i<=num; i++) {
		JS_DisplayP("divEdicion_"+i+"_"+cod+"_on", modoEdicion);
		JS_DisplayP("divEdicion_"+i+"_"+cod+"_off", !modoEdicion);
	}
}
function JS_EditarFila(cod, num) {
	var modoEdicion = !parseInt(JS_GetValue("hidFilaEstaModoEdicion_"+cod));
	JS_ModoEdicion(cod, modoEdicion, num);
	codAnt = parseInt(JS_GetValue("hidFilaEditAnterior"));
	esLaMisma = (codAnt == cod);
	if (!esLaMisma) {
		JS_Value("hidFilaEditAnterior", ((esLaMisma)?"":cod));
		JS_ModoEdicion(codAnt, false, num);
	}
	//JS_SeleccionarFila("trFila_"+cod, cod);
}
function JS_EditarFilaP(cod, num) {
	var modoEdicion = !parseInt(JS_GetValueP("hidFilaEstaModoEdicion_"+cod));
	JS_ModoEdicionP(cod, modoEdicion, num);
	codAnt = parseInt(JS_GetValueP("hidFilaEditAnterior"));
	esLaMisma = (codAnt == cod);
	if (!esLaMisma) {
		JS_ValueP("hidFilaEditAnterior", ((esLaMisma)?"":cod));
		JS_ModoEdicionP(codAnt, false, num);
	}
	//JS_SeleccionarFila("trFila_"+cod, cod);
}
function JS_QuitarTagsHTML(Word) {
	a = Word.indexOf("<");
	b = Word.indexOf(">");
	len = Word.length;
	c = Word.substring(0, a);
	if(b == -1)
	b = a;
	d = Word.substring((b + 1), len);
	Word = c + d;
	tagCheck = Word.indexOf("<");
	if(tagCheck != -1)
	Word = JS_QuitarTagsHTML(Word);
	return Word;
}
function JS_AbrirSelector(data,t,f,w,h,hid,a,b,c,d,i,r,s) {
	JS_Display(a, true);
	JS_Inner(b, '<iframe id="'+i+'" name="'+i+'" src="'+r+'comun/inc_selector_ventana.php?r='+r+'&data='+data+'&w='+w+'&h='+h+'&hid='+hid+'&a='+a+'&b='+b+'&c='+c+'&d='+d+'&i='+i+'&t='+t+'&f='+f+'&buscar='+JS_GetValue(s)+'" allowtransparency=true width='+w+' height='+h+' frameborder=0 scrolling=no></iframe>');
}
function JS_MensajeOver(msg) {
	window.status = msg; return true;
}
function JS_MensajeOut() {
	window.status = ""; return true;
}

function JS_ValidarHayTexto(txt, msg) {
	if (txt.value != "") {
		return true;
	}
	alert(msg);		
	txt.focus();
	return false;
}
function JS_ValidarEsNumero(txt) {
	if (parseFloat(JS_RemplazarStr(txt.value, ",", "."))) { return true; }
	if ((txt.value == "0") || (JS_RemplazarStr(txt.value, ",", ".") == "0.00")) { return true; }
	alert("Debe introducir un valor numérico.");		
	txt.focus();
	txt.select();
	return false;
}
function JS_ValidarEsNumeroPositivo(txt) {
	if (!JS_ValidarEsNumero(txt)) {
		return false;
	}
	if (parseFloat(JS_RemplazarStr(txt.value, ",", ".")) < 0) {
		alert("Debe introducir un número positivo.");		
		txt.focus();
		txt.select();
		return false;
	}
	return true;
}
function JS_ValidarHayNumero(txt, msg) {
	if (!JS_ValidarHayTexto(txt, msg)) {
		return false;
	}
	if (!JS_ValidarEsNumero(txt)) {
		return false;
	}
	return true;
}
function JS_ValidarOpcionSelec(sel, msg) {
	if ((sel.selectedIndex > 0) || (sel.selectedIndex == -2)) {
		return true;
	}
	alert(msg);		
	sel.focus();
	return false;
}
function JS_NM_Fechas_Val_Calendar(txt, msg) {
	if (txt.value != "") {
		if (!calendar.parseDate(txt.value)) {
			alert(msg);
			txt.focus();
			txt.select();
			return false;
		}
	}
	return true;
}
function JS_ActualizarDatosSelector(sel, ifrID, divID, doit) {
	cod = sel[sel.selectedIndex].value;
	JS_Src(ifrID, "../comun/"+doit+".php?cod="+cod+"&div="+divID);
}
function JS_FiltrarPorDia(ckbID, txtID, vble1, vble2) {
	var fecha = "";
	var filtrarDia = 0;
	var ckb = JS_Get(ckbID);
	var txt = JS_Get(txtID);
	if (txt) { fecha = txt.value; }
	if (ckb) { filtrarDia = (ckb.checked) ? 1 : 0; }
	if (!filtrarDia) { 
		fecha = "null"; 
	} else {
		if (fecha == "") { alert("Seleccione el día por el que desea filtrar."); return; }
	}
	JS_Redireccionar("?"+vble1+"="+filtrarDia+"&"+vble2+"="+fecha);
}
function JS_ActualizarImporteFacturaP() {
	r = JS_GetP("txtBase");
	base = JS_GetNum(r.value);
	
	r = JS_GetP("txtExento");
	exento = JS_GetNum(r.value);
	
	r = JS_GetP("hidRetencionImporte");
	retenciones = JS_GetNum(r.value);
	
	r = JS_GetP("selecIva");
	valor = r[r.selectedIndex].value;
	arr = valor.split("#", 2);
	idIva = JS_GetNum(arr[0]);
	ivaPorciento = JS_GetNum(arr[1]);
	if (ivaPorciento < 0) { ivaPorciento = 0; }
	
	r = JS_GetP("selecRetencion");
	valor = r[r.selectedIndex].value;
	arr = valor.split("#", 2);
	idPorciento = JS_GetNum(arr[0]);
	retencionPorciento = JS_GetNum(arr[1]);
	if (retencionPorciento < 0) { retencionPorciento = 0; }

	r = JS_GetP("ckbRetenciones");
	tieneRetenciones = r.checked;
	retencionBase = (parseFloat(base) + parseFloat(exento));
	if (!tieneRetenciones) {
		retencionPorciento = 0;
		retencionImporte = 0;
	} else {
		retencionImporte = parseFloat(retencionBase) * parseFloat(retencionPorciento) / 100;
	}

	ivaImporte = parseFloat(base) * parseFloat(ivaPorciento) / 100;
	totalFactura = (parseFloat(base) + parseFloat(ivaImporte) + parseFloat(exento) - parseFloat(retencionImporte));

	retencionBase = (parseFloat(base) + parseFloat(exento));
	retencionImporte = parseFloat(retencionBase) * parseFloat(retencionPorciento) / 100;

	JS_InnerP("divRetencionBase",	JS_SetNum(retencionBase));
	JS_InnerP("divIvaImporte",		JS_SetNum(ivaImporte));
	JS_ValueP("hidIvaImporte", 		JS_SetNum(ivaImporte));
	JS_InnerP("divRetencionImporte",	JS_SetNum(retencionImporte));
	JS_ValueP("hidRetencionImporte", 	JS_SetNum(retencionImporte));
	JS_InnerP("divTotalFactura", 	JS_SetNum(totalFactura));
	JS_ValueP("hidTotalFactura", 	JS_SetNum(totalFactura));
}
function JS_ActualizarImporteFactura() {
	r = JS_Get("txtBase");
	base = JS_GetNum(r.value);
	
	r = JS_Get("txtExento");
	exento = JS_GetNum(r.value);
	
	r = JS_Get("hidRetencionImporte");
	retenciones = JS_GetNum(r.value);

	r = JS_Get("selecIva");
	valor = r[r.selectedIndex].value;
	arr = valor.split("#", 2);
	idIva = JS_GetNum(arr[0]);
	ivaPorciento = JS_GetNum(arr[1]);
	if (ivaPorciento < 0) { ivaPorciento = 0; }
	
	r = JS_Get("selecRetencion");
	valor = r[r.selectedIndex].value;
	arr = valor.split("#", 2);
	idPorciento = JS_GetNum(arr[0]);
	retencionPorciento = JS_GetNum(arr[1]);
	if (retencionPorciento < 0) { retencionPorciento = 0; }
	
	r = JS_Get("ckbRetenciones");
	tieneRetenciones = r.checked;
	retencionBase = (parseFloat(base) + parseFloat(exento));
	if (!tieneRetenciones) {
		retencionPorciento = 0;
		retencionImporte = 0;
	} else {
		retencionImporte = parseFloat(retencionBase) * parseFloat(retencionPorciento) / 100;
	}

	ivaImporte = parseFloat(base) * parseFloat(ivaPorciento) / 100;
	totalFactura = (parseFloat(base) + parseFloat(ivaImporte) + parseFloat(exento) - parseFloat(retencionImporte));

	JS_Inner("divRetencionBase",	JS_SetNum(retencionBase));
	JS_Inner("divIvaImporte",		JS_SetNum(ivaImporte));
	JS_Value("hidIvaImporte", 		JS_SetNum(ivaImporte));
	JS_Inner("divRetencionImporte",	JS_SetNum(retencionImporte));
	JS_Value("hidRetencionImporte", JS_SetNum(retencionImporte));
	JS_Inner("divTotalFactura", 	JS_SetNum(totalFactura));
	JS_Value("hidTotalFactura", 	JS_SetNum(totalFactura));
}

function JS_ValidarEmail(c) { 
	return !(c.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig));
}
function JS_SlideShow(a, b) {
	r = JS_Get("hidSlideShow");
	if (r) { kb = r.value; }
	//if (NM_EsSafari()) { NM_Display("divVistaSlide", true); NM_Display("divVistaNormal", false); }
	JS_Src("iframeSlideShow",'../inc/slideshow.php?opc='+a+'&id='+b+'&kb='+kb);
	//alert ("hola");
}
function JS_SlideShowLink(a) {
	JS_SlideShow("pause", a);
}

