function modifica_quantitat(producte,tipus){
	elemento = "quantitat_"+producte;
	var nova_quantitat = 0;
	if(tipus=="mes"){
		nova_quantitat = parseInt(document.getElementById(elemento).value) + 1;
	}
	if(tipus=="menys"){
		nova_quantitat = parseInt(document.getElementById(elemento).value) - 1;
	}
	if(tipus=="res"){
		nova_quantitat = parseInt(document.getElementById(elemento).value);
	}
	if(isNaN(nova_quantitat)){
		document.getElementById(elemento).value = 1;
	} else if(nova_quantitat<1){
		document.getElementById(elemento).value = 1;
	} else if(nova_quantitat>99){
		document.getElementById(elemento).value = 99;
	} else {
		document.getElementById(elemento).value = nova_quantitat;
	}
}

function compra(producte){
	modifica_quantitat(producte,'res');
	elemento = "form_producte_"+producte;
	document.getElementById(elemento).submit();
}

function update(producte){
	modifica_quantitat(producte,'res');
	elemento = "form_update_producte_"+producte;
	document.getElementById(elemento).funcio_update.value = 'actualitza';
	document.getElementById(elemento).submit();
}

function elimina(producte){
	elemento = "form_update_producte_"+producte;
	agree = confirm('¿Seguro que desea eliminar el producto?');
	if (agree) {
		document.getElementById(elemento).funcio_update.value = 'elimina';
		document.getElementById(elemento).submit();
	}
}

function anular(){
	elemento = "form_anular";
	agree = confirm('¿Seguro que desea anular el pedido?');
	if (agree) {
		document.getElementById(elemento).submit();
	}
}

function finalizar(){
	elemento = "form_finalizar";
//	agree = confirm('¿Segur que vol finalitzar aquesta comanda?');
//	if (agree) {
		document.getElementById(elemento).submit();
//	}
}

function login(lloc){
	elemento = "form_login_"+lloc;
	document.getElementById(elemento).submit();
}

function login_empreses(lloc){
	var elemento = "form_login_empreses_"+lloc;
	var seleccio = document.getElementById(elemento).seleccio.value;
	document.getElementById(elemento).action = seleccio;
	document.getElementById(elemento).submit();
}
function comprobar_inputs(lloc){
	var elemento = "form_login_empreses_"+lloc;
	var empresa = document.getElementById(elemento).empresa.value;
	var usuario = document.getElementById(elemento).usr.value;
	var password = document.getElementById(elemento).pwd.value;
	if((empresa=="")||(usuario=="")){
		if(empresa==""){
				alert('falta el camp empresa');
		}
		if(usuario==""){
				alert('falta el camp usuario');
		}
	}else{
	document.getElementById('acces').innerHTML = '<table cellpadding="0" cellspacing="0" border="0" width=100%><tr><td width="100" align="center" valign="middle" class="link12_black"><a href="https://www.pursuit.es/company/default.aspx?agency=viatgesBerga&company='+empresa+'&username='+usuario+'&pass='+password+'" class="empresa">Accedir</a></td></tr></table>';
	}
}

function login_acces(){
	var elemento = "form_login_empreses_up";
	var seleccio = document.getElementById(elemento).seleccio.value;
	var empresa = document.getElementById(elemento).empresa.value;
	var usuario = document.getElementById(elemento).usr.value;
	var password = document.getElementById(elemento).pwd.value;
	document.getElementById(elemento).action = seleccio;
	document.getElementById('acces').innerHTML = '<table cellpadding="0" cellspacing="0" border="0" width=100%><tr><td width="100" align="center" valign="middle" class="link12_black"><a href="https://www.pursuit.es/company/default.aspx?agency=viatgesBerga&company='+empresa+'&username='+usuario+'&pass='+password+'" class="empresa">Accedir</a></td></tr></table>';

}

function logout(){
	elemento = "form_logout";
	agree = confirm('¿Desea salir de la sesion activa / cambiar de usuario?\nSi sale de su cuenta perderá los pedidos en curso');
	if (agree) {
		document.getElementById(elemento).submit();
	}
}

function confirmar_pedido(){
	elemento = "form_confirmar_pedido";
	document.getElementById(elemento).submit();
}

function valida_null(campo){
	var elemento = campo;
	elementID = document.getElementById(elemento);
	if ((elementID.value==null)||(elementID.value=="")){
		return false;
	}
	return true;
}

function valida_form_ralizar_pago(){
	var campos = new Array("nif_cif","facturacion_nombre","facturacion_apellidos","facturacion_direccion","facturacion_cod_postal","facturacion_ciudad");
	var nom_campos = new Array("Nif/Cif","Nombre","Apellidos","Dirección","Código postal","Ciudad");
	var elemento = "form_realizar_pago";
	var aceptoID = document.getElementById(elemento).acepto_condiciones_compra;
	if(aceptoID.checked == false){
		alert('Para poder finalizar su pedido\r\nacepte las condiciones de compra\r\nmarcando la casilla de verificación.\r\n\r\nGracias.');
		return false;
	}
	
	// comprobem nulls als camps
	for (i=0; i<campos.length; i++){
		var validat = valida_null(campos[i]);
		if(validat==false){
			alert('Error en validación de datos. Compruebe:'+nom_campos[i]);
			return false;
		}
	}
	return true;
}

function pagar_tarjeta() { 
	vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
	document.compra.submit();
	// enviem l'update de la comanda
	//elemento = "form_realizar_pago";
	//document.getElementById(elemento).submit();
	document.form_realizar_pago.submit();
}

function pagar_transf() { 
	// enviem l'update de la comanda
	//elemento = "form_realizar_pago";
	//document.getElementById(elemento).submit();
	document.form_realizar_pago.submit();
}

function realizar_pago(){
	var elemento = "forma_pago";
	var fpago = document.getElementById(elemento);
	valid = valida_form_ralizar_pago();
	if(valid==true){
		if(fpago[fpago.selectedIndex].value == 'transferencia'){
			pagar_transf();
		} else {
			pagar_tarjeta();
		}
	} else {
		//alert('Error en validación de datos. Compruébelos y vuleva a confirmar.');
	}
}

/*
function calc() { 
	vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
	document.compra.submit();
}
*/

function valida_form_registro(idioma){
	var campos = new Array("usr_registro","pwd_registro","nombre","apellidos","direccion","cod_postal","ciudad","telefonos","nif_cif");
	var nom_campos_esp = new Array("Usuario","Password","Nombre","Apellidos","Dirección","Código postal","Ciudad","Teléfono","Nif/Cif");	
	var nom_campos_cat = new Array("Usuari","Password","Nom","Cognoms","Direcció","Codi postal","Ciutat","Telèfon","Nif/Cif");	
	var elemento = "form1";
	var aceptoID = document.getElementById(elemento).acepto_terminos_tpsport;
	if(aceptoID.checked == false){
		if(idioma=="esp"){
		alert('Para poder finalizar su solicitud\r\nacepte las condiciones de compra\r\nmarcando la casilla de verificación.\r\n\r\nGracias.');
		return false;
		} else if(idioma=="cat"){
		alert('Per poder finalitzar la seva solicitut\r\naccepti les condicions de compra\r\nmarcant la casella de verificació.\r\n\r\nGràcies.');
		return false;
		}
	}
	// comprobem nulls als camps
	for (i=0; i<campos.length; i++){
		var validat = valida_null(campos[i])
		if(validat==false){
			if(idioma=="esp"){
				alert('Error en validación de datos. Compruebe:'+nom_campos_esp[i]);
			return false;
			} else if(idioma=="cat"){
				alert('Error en la validació de dades. Comprovi:'+nom_campos_cat[i]);
			return false;
			}
		}
	}
	document.getElementById(elemento).submit();
}


function posa_usr_pwd(usuari,contrasenya){
	document.form1.usr_registro.value=usuari;
	document.form1.pwd_registro.value=contrasenya;
}

function copiar_datos(){
	document.form_realizar_pago.envio_nombre.value				= document.form_realizar_pago.facturacion_nombre.value;
	document.form_realizar_pago.envio_apellidos.value 			= document.form_realizar_pago.facturacion_apellidos.value;
	document.form_realizar_pago.envio_empresa.value 			= document.form_realizar_pago.facturacion_empresa.value;
	document.form_realizar_pago.envio_direccion.value 			= document.form_realizar_pago.facturacion_direccion.value;
	document.form_realizar_pago.envio_cod_postal.value			= document.form_realizar_pago.facturacion_cod_postal.value;
	document.form_realizar_pago.envio_ciudad.value 				= document.form_realizar_pago.facturacion_ciudad.value;
	document.form_realizar_pago.envio_provincia.selectedIndex	= document.form_realizar_pago.facturacion_provincia.selectedIndex ;
	document.form_realizar_pago.envio_pais.selectedIndex		= document.form_realizar_pago.facturacion_pais.selectedIndex;
}

function obre_carret(numero_carret){
	var elemento = "form_obre_carret_"+numero_carret;
	document.getElementById(elemento).submit();	
}

function obre_pago(numero_carret){
	var elemento = "form_obre_pago_"+numero_carret;
	document.getElementById(elemento).submit();	
}