// JavaScript Document
function str_replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}

function registris(idioma){
	if(idioma =='esp'){
		alert('Para poder descargar este archivo debe estar registrado\n y activar su sesion desde la zona de clientes.\r\nGracias.');
	}else{
		alert('To download this file you must be registered\nand activate your session from the user area.\r\nThank you.');
	}
}
function actualitza_capsalera(){
	document.form_editar_pedido.submit();
}

function valida_stock(producte,stock,interval){
	elemento = "cantidad_"+producte;
	var quantitat = 0;
	var minim = interval;
	quantitat = parseInt(document.getElementById(elemento).value);
	if(quantitat>stock){
		alert("Alerta!! La cantidad seleccionada supera el stock disponible previsto.");
	} else if(quantitat<minim){
		//alert("Alerta!! La cantidad seleccionada es inferior a las unidades de embalaje.");
	} else if((quantitat%minim)>0){
		//alert("Alerta!! La cantidad seleccionada no es multiplo de las unidades de embalaje.");
	}
}

function modifica_quantitat(producte,interval,stock,tipus){
	elemento = "cantidad_"+producte;
	var nova_quantitat = 0;
	if(tipus=="mes"){
		nova_quantitat = parseInt(document.getElementById(elemento).value) + parseInt(interval);
	}
	if(tipus=="menys"){
		nova_quantitat = parseInt(document.getElementById(elemento).value) - parseInt(interval);
	}
	if(nova_quantitat>0){
		document.getElementById(elemento).value = nova_quantitat;
		document.getElementById(elemento).value = nova_quantitat;
		valida_stock(producte,stock,interval);
	}
}

function valida_preu(producte){
	var elemento = "producto_precio_vendido_"+producte;
	var precio_txt = document.getElementById(elemento).value;
	precio_txt = precio_txt.replace(/,/i, ".");

/*
var nuevo_precio_txt = "";
	var brokenstring=precio_txt.split(".");
	if(brokenstring.lenght()>1){
		nuevo_precio_txt = brokenstring[0]+"."+brokenstring[1].substring(0,2);
		alert(nuevo_precio_txt);
	}
*/	
	var precio = parseFloat(precio_txt);
//	var precio = parseFloat(nuevo_precio_txt);
	if(precio<=0){
		alert("El precio del producto no puede ser negativo, ni 0.");
		precio=0.01;
	}
	document.getElementById(elemento).value = precio;
}

function valida_dto_buscador(producte){
	// comprova que el descompte per linea està entre 0.00 i 99.99
	var elemento = "producto_descuento_"+producte;
	var elemento_form = "form_producte_"+producte;
	var dto_txt = document.getElementById(elemento).value;
//	alert(dto_txt);
	dto_txt = dto_txt.replace(/,/i, ".");
//	alert(dto_txt);
	var dto = parseFloat(dto_txt);
//	alert(dto);
	var estat = document.getElementById(elemento_form).producto_aplicar_precioneto.checked;
	if(estat==true){
		alert("ERROR: El descuento no puede ser aplicado si selecciona precio neto");
		dto = 0.00;
	} else {
		if(dto>99.99){
			alert("ERROR: El descuento no puede ser superior a 99.99%");// en "+elemento);
			//document.getElementById(elemento).value = "99.99";
			dto = 99.99;
		}
		if(dto<0){
			alert("ERROR: El descuento no puede ser negativo");// en "+elemento);
			//document.getElementById(elemento).value = "0";
			dto = 0.00;
		}
	}
	document.getElementById(elemento).value = dto;
}

function valida_dto(producte){
	// comprova que el descompte per linea està entre 0.00 i 99.99
	var elemento = "producto_descuento_f"+producte;
	var dto_txt = document.getElementById(elemento).value;
//	alert(dto_txt);
	dto_txt = dto_txt.replace(/,/i, ".");
//	alert(dto_txt);
	var dto = parseFloat(dto_txt);
//	alert(dto);
	if(dto>99.99){
			alert("ERROR: El descuento no puede ser superior a 99.99%");// en "+elemento);
			//document.getElementById(elemento).value = "99.99";
			dto = 99.99;
	}
	if(dto<0){
			alert("ERROR: El descuento no puede ser negativo");// en "+elemento);
			//document.getElementById(elemento).value = "0";
			dto = 0.00;
	}
	document.getElementById(elemento).value = dto;
	var element_form = "form_producte_linea_f"+producte;
	document.getElementById(element_form).submit();
}

function comprova_dtolinea(elemento){
	var estat = document.getElementById(elemento).producto_aplicar_precioneto.checked;
	if(estat==true){
		var dto = document.getElementById(elemento).producto_descuento.value;
		if(dto>0){
			alert("Alerta: Si activa el precio neto, no debe aplicar descuento en linea");
		}
		document.getElementById(elemento).producto_descuento.value=0;
	}
}

function obre_comanda(id_comanda){
	document.form_editar_comanda.id_pedido.value = id_comanda;
	document.form_editar_comanda.submit();
}

function afegeix_producte(id_producte){
	var element = "form_producte_"+id_producte;
	document.getElementById(element).submit();
}

function activa_pedido(id_pedido,cod_ciente){
	document.pedidos_abiertos.pedido.value = id_pedido;
	document.pedidos_abiertos.cliente.value = cod_ciente;
	document.pedidos_abiertos.submit();
}

function swap_vis(id_capa){
	var estat = document.getElementById(id_capa).className;
	if(estat=='capa_visible'){
		document.getElementById(id_capa).className='capa_no_visible';
	} else {
		document.getElementById(id_capa).className='capa_visible';
	}
}

function elimina_producte_comanda(id_linea){
//	agree = confirm('Seguro que desea eliminar el producto ['+id_linea+']del pedido?');
	agree = confirm('Seguro que desea eliminar el producto del pedido?');
	if (agree) {
		document.form_elimina_detall.id_detall.value=id_linea;
		document.form_elimina_detall.submit();
	}
}

function actualitza_descomptes(){
	document.form_editar_pedido.dto_especial.value = document.form_descomptes.dto_especial.value;
	document.form_editar_pedido.dto_pedido.value = document.form_descomptes.dto_pedido.value;
	document.form_editar_pedido.dto_comercial.value = document.form_descomptes.dto_comercial.value;
	document.form_editar_pedido.dto_pronto_pago.value = document.form_descomptes.dto_pronto_pago.value;
	document.form_editar_pedido.submit();
}

function posa_descomptes(valor1,valor2){
	document.form_editar_pedido.dto_comercial.value = valor1;	
	document.form_editar_pedido.dto_pronto_pago.value = valor2;
	document.form_editar_pedido.submit();
}

function posa_descompte_comercial(valor){
	document.form_descomptes.dto_comercial.value = valor;
	document.form_editar_pedido.dto_comercial.value = valor;
	document.form_editar_pedido.submit();
}

function posa_descompte_pp(valor){
	document.form_descomptes.dto_pronto_pago.value = valor;
	document.form_editar_pedido.dto_pronto_pago.value = valor;
	document.form_editar_pedido.submit();
}

function actualitza_producte(id_producte, tipus){
	if(tipus=='neto'){
		var element = "form_producte_linea_"+id_producte;
	} else {
		var element = "form_producte_linea_f"+id_producte;	
	}
//	alert(element);
	document.getElementById(element).submit();
}

function actualitza_adresa(){
	var nova_adresa = document.form_editar_pedido.adresa_alternativa[document.form_editar_pedido.adresa_alternativa.selectedIndex].value;
	document.form_editar_pedido.direccion_envio.value = nova_adresa;
	actualitza_capsalera();
}

function anular_pedido(){
	agree = confirm('Seguro que desea anular el pedido?');
	if (agree) {
		document.form_editar_pedido.estado.value = 'anulado';
		document.form_editar_pedido.submit();
	}
}

function finalizar_pedido(){
	agree = confirm('Seguro que desea finalizar el pedido?');
	if (agree) {
		document.form_editar_pedido.estado.value = 'cerrado';
		document.form_editar_pedido.submit();
	}
}

function actualitza(){
	document.form1.submit();
}

function posa_data(camp,data_generada){
	var elem_data = document.getElementById(camp);
	elem_data.value=data_generada;
}

function posar_data_filtre(){
	var element = document.getElementById('form_filtre');
	var dia = element.dia.value;
	var mes = element.mes.value;
	var any = element.any.value;
	element.fecha_actividad.value = any+"-"+mes+"-"+dia;
}


function restablecer_filtros(){
	document.form_filtre.dia.value = '';
	document.form_filtre.mes.value = '';
	document.form_filtre.any.value = '';
	document.form_filtre.ph_usuario.value = '';
	document.form_filtre.fecha_actividad.value = '';
	document.form_filtre.dia_fin.value = '';
	document.form_filtre.mes_fin.value = '';
	document.form_filtre.any_fin.value = '';
	document.form_filtre.fecha_actividad_fin.value = '';
	document.form_filtre.submit();
}

function neteja_filtros(){
	document.form_filtre.ph_usuario_2.value = '';
	document.form_filtre.rango.value = '';
	document.form_filtre.paraula.value = '';
	document.form_filtre.submit();
}





function filtros(){
	//posar_data_filtre();
	document.form_filtre.submit();
}
function updata_lol_form(sid){
	alert(sid);
	element='superpedidos';
	document.getElementById(element).submit();
}

function cerrar_ventana() {
var ventana = window.self;
ventana.opener = window.self;
ventana.close();
}

function openWin(albara_actiu,expedicio_activa) {    
	var width = 700;    
	var height = 600;    
	var left = Math.floor((screen.availWidth - width) / 2);    
	var top = Math.floor((screen.availHeight - height) / 2);    
	var windowFeatures = "width=" + width + ",height=" + height + ",menubar=no,toolbar=no,scrollbars=no,resizable=no,Titlebar=no,status=no" + "left=" + left + ",top=" + top +            "screenX=" + left + ",screenY=" + top;
    
   child1 = window.open("http://77.226.243.238/kiosco/Loginauto.aspx?Username=208081308&password=208081308&cli=208&alb="+albara_actiu, "subWind", windowFeatures); 
  
	}


function desplegar(seccio_activa){
	var element = seccio_activa;
	var obj_seccio_activa = document.getElementById(element);
	obj_seccio_activa.style.visibility = "visible";
	obj_seccio_activa.style.display = "inline";
}
function plegar(seccio_activa){
	
	var element = seccio_activa;
	var obj_seccio_activa = document.getElementById(element);
	obj_seccio_activa.style.visibility = "hidden";
	obj_seccio_activa.style.display = "none";
}

function buscar(buscador){
	document.getElementById(buscador).submit();
}

//// AJAX FUNCTIONS 
var estat_ajax = 'OK';

function nuevoAjax(){
	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;
}
