var swfranja = false;
var swadr = false;
var swcodi = 0;

function cridaAjax(codi,id){
	
	// variables globals
	objecte = document.getElementById(id);
	
	switch(codi){
		case "1" :
			url = "../../ajax/vf_difunts.asp?nom="+ evalstr(document.getElementById('inomDif').value) +"&cog1="+ evalstr(document.getElementById('icog1Dif').value) +"&cog2="+ evalstr(document.getElementById('icog2Dif').value);
		break;
		case "2" :
			document.getElementById('llistaDifunts').innerHTML='';
			document.getElementById('franja').innerHTML='';
			url = "../../ajax/vf_inputsDifunts.asp";
		break;	
		case "3" :
			url = "../../ajax/vf_llac.asp";
		break;	
		case "4" :
			url = "../../ajax/vf_targeta.asp";
		break;	
		case "5" :
			url = "../../ajax/vf_res.asp";
		break;
		case "6" :
			swcodi = document.getElementById('dicodiLlistat').value;
			url = "../../ajax/vf_dicodiLlistat.asp?codi="+ document.getElementById('dicodiLlistat').value;
			swfranja = true;
		break;			
		default :
			swcodi = codi;
			if(id == 'llistaDifunts'){
				document.getElementById('inputsLlistaDifunts').innerHTML='';
				url = "../../ajax/vf_difuntSeleccionat.asp?codi="+ codi;
				swfranja = true;
			}else if(id == 'franja'){
				url = "../../ajax/vf_franja.asp?codi="+ codi;
			}
		break;
	}
	
	// Inicialització de l'objecte ajax
	if(window.XMLHttpRequest){ retorn = new XMLHttpRequest(); }else if(window.ActiveXObject){ retorn = new ActiveXObject("Microsoft.XMLHTTP"); }else{ retorn = false; }
	
	if(retorn){
		retorn.onreadystatechange = florsTramitades;
		retorn.open('GET',url,true);
		retorn.send(null);
	}
	
	// Funcions
	function florsTramitades(){
		if( retorn.readyState == 4){
			if( retorn.status == 200 ){
				if(retorn.responseText.indexOf("|||")>-1){
					cridaAjax(retorn.responseText.substr(retorn.responseText.indexOf("|||")+3,6),'llistaDifunts');
				}
				objecte.innerHTML = retorn.responseText;
				if(swfranja == true){ cridaAjax(swcodi,'franja'); swfranja = false; }
			}else{
				objecte.innerHTML = vf_error_proces + retorn.status;
			}
		}else{
			objecte.innerHTML = '<br /><br /><center><img src="../../img/sistema/precarrega.gif" width="36" height="25" /></center><br /><br />';
		}
	}
}

function adr(id,codi){
	switch(id){
		case "pais" :
			
			if( codi == '100' ){
				// España, mostrem les provincies
				url = "../../ajax/vf_provincia.asp";
				objecte = document.getElementById('cprovincia');
				document.getElementById('cmunicipi').innerHTML = '';
				document.getElementById('cnomvia').innerHTML = '';
				document.getElementById('cresta').style.display = 'none';
			}else{
				// Altre país, mostrem els seus municipis
				url = "../../ajax/vf_municipi.asp?pais="+codi;
				objecte = document.getElementById('cmunicipi');
				document.getElementById('cprovincia').innerHTML = '';
				document.getElementById('cnomvia').innerHTML = '';
				document.getElementById('cresta').style.display = 'none';
			}
			
		break;
		case "provincia" :
				// mostrem els municipis
				url = "../../ajax/vf_municipi.asp?provincia="+codi;
				objecte = document.getElementById('cmunicipi');
				document.getElementById('cnomvia').innerHTML = '';
				document.getElementById('cresta').style.display = 'none';
		break;
		case "carrer" :
				// Barcelona, mostrem els municipis
				url = "../../ajax/vf_carrer.asp?municipi="+codi;
				objecte = document.getElementById('cnomvia');
				document.getElementById('cresta').style.display = 'block';
		break;						
	}
	
	// Inicialització de l'objecte ajax
	if(window.XMLHttpRequest){ retorn = new XMLHttpRequest(); }else if(window.ActiveXObject){ retorn = new ActiveXObject("Microsoft.XMLHTTP"); }else{ retorn = false; }
	
	if(retorn){
		retorn.onreadystatechange = adrOp;
		retorn.open('GET',url,true);
		retorn.send(null);
	}
	
	// Funcions
	function adrOp(){
		if( retorn.readyState == 4){
			if( retorn.status == 200 ){
				objecte.innerHTML = retorn.responseText;
			}else{
				objecte.innerHTML = vf_error_proces;
			}
		}else{
			objecte.innerHTML = '<br /><br /><center><img src="../../img/sistema/precarrega.gif" width="36" height="25" /></center><br /><br />';
		}
	}
}


// Venda flors autocomplete
function suggest(inp,cap,e){
	var input = document.getElementById(inp);
	var capa = document.getElementById(cap);
	document.getElementById('icarrer').value = '';
	if(input.value.length>2){
		autocompletar(inp,cap,e);
	}else{
		capa.innerHTML='';
		capa.style.display='none';
	}
}

function autocompletar(inp,cap,e){
	var keycode;
	if (window.event){
		keycode = window.event.keyCode;
	}else if(e){ 
		keycode = e.which;
	}
	//if(keycode == 40){if(document.getElementById('selectAutoCompletar')){}}
	var capa = document.getElementById(cap);
	var input = document.getElementById(inp);
	var url = "../../ajax/vf_autocompletar.asp?texto="+input.value;
	// Inicialització de l'objecte ajax
	if(window.XMLHttpRequest){ retorn = new XMLHttpRequest(); }else if(window.ActiveXObject){ retorn = new ActiveXObject("Microsoft.XMLHTTP"); }else{ retorn = false; }
	if(retorn){ retorn.onreadystatechange = completar; retorn.open('GET',url,true); retorn.send(null); }
	// Funcions
	function completar(){	
		if( retorn.readyState == 4){
			if( retorn.status == 200 ){
				capa.style.top = (input.offsetTop+20);
				capa.style.left = (input.offsetLeft);
				capa.style.display = 'block';
				capa.innerHTML = retorn.responseText;
			}else{
				capa.innerHTML = vf_error_proces + retorn.status;
			}			
		}else{
			capa.innerHTML = '<br /><br /><center><img src="../../img/sistema/precarrega.gif" width="36" height="25" /></center><br /><br />';
		}
	}
}

function asignarCarrer(codi,carrer){
	document.getElementById('icarrer').value = codi;
	document.getElementById('literalcarrer').value = carrer;
	document.getElementById('cautocompletar').style.display = 'none';
	document.getElementById('literalcarrer').style.border = '1px solid #999999';
	validateStreet('literalcarrer','icarrer');
}

function validateStreet(inp,inpHid){
	if(document.getElementById(inpHid).value==''){
		document.getElementById(inp).style.border = '2px solid #BB0000';
	}else{
		document.getElementById(inp).style.border = '1px solid #999999';
	}
}

function foraInputCarrer(){
}

function resetInputsCarrers(){
	document.getElementById('literalcarrer').value='';
	document.getElementById('icarrer').value='';
}

function ocultarCapaAutoCompletar(){
	document.getElementById('cautocompletar').style.display = 'none';
}

function detallFlors(){
	var evt = (evt) ? evt : ( (window.event) ? event : null);
	nouTop = evt.clientY-200; nouLeft = evt.clientX-400;
	document.getElementById('detallFlors').style.top = nouTop+"px";
	document.getElementById('detallFlors').style.left = nouLeft+"px";
	fade(1);
}


function fade(opcio){
	if(opcio == 1){
		document.getElementById('detallFlors').style.display='block';
		for (i=0;i<11;i++){ setTimeout('setOpacity('+i+','+opcio+')',50*i); }
	}else{
		for (i=0;i<11;i++){
			var i2 = new Array(10,9,8,7,6,5,4,3,2,1,0);
			setTimeout('setOpacity('+i2[i]+','+opcio+')',50*i);
		}
		setTimeout('tancarDisplay()',1000);
	}
}

function tancarDisplay(){document.getElementById('detallFlors').style.display='none';}
	
function setOpacity(value,opcio) {
	if(opcio == 1){
		document.getElementById('detallFlors').style.opacity = value/10;
		document.getElementById('detallFlors').style.filter = 'alpha(opacity=' + value*10 + ')';		
	}else{
		document.getElementById('detallFlors').style.opacity = value/0.1;
		document.getElementById('detallFlors').style.filter = 'alpha(opacity=' + value*10 + ')';
	}
}

function evalstr(literal){
	literal = literal.replace('"','');
	literal = literal.replace("'","");
	return literal;
}

function validarPagament(){
	
	// Variables
	form = document.getElementById('fvendaflors');
	
	// difunt
	if( document.getElementById('idifunt') ){
		if( document.getElementById('idifunt').value == "" ){ alert(vf_difunt_no_seleccionat); return false; }
	}else{
		alert(vf_difunt_no_seleccionat);
		return false;
	}

	// quantitat
	if( document.getElementById('quantitat') ){
		if( document.getElementById('quantitat').value == "" ){ alert(fr_quantitat_no_posada); return false; }
	}else{
		alert(fr_quantitat_necesaria);
		return false;
	}
		
	// llaç
	if( (form.lt[0].checked == false) && (form.lt[1].checked == false) && (form.lt[2].checked == false)){ alert(vf_dedicatoria_no_indicat); return false; }
		
	// franjes
	if( form.ifranja ){
		if(form.ifranja.value=='nodisponible'){ alert(vf_franja_no_disponble); return false; }
	}
	if( form.sfranja ){
		if( form.sfranja.options[form.sfranja.selectedIndex].value == 0 ){ alert(vf_franja_no_valida); return false; }
	}else{
		return false;
	}
		
	// pagador
	if(form.tipus[1].checked == true){
		if(document.getElementById('iempresa').value == ""){ alert(vf_nom_empresa_obligatori); return false; }
	}else{
		if(document.getElementById('inom').value == ""){ alert(vf_nom_pagador_obligatori); return false; }
		if(document.getElementById('icog1').value == ""){ alert(vf_cognom1_pagador_obligatori); return false; }
		if(document.getElementById('icog2').value == ""){ alert(vf_cognom2_pagador_obligatori); return false; }	
	}
	if( form.stipdoc ){ if( form.stipdoc.options[form.stipdoc.selectedIndex].value == 0 ){ alert(vf_tipdoc_obligatori); return false; } }
	if(document.getElementById('inif').value == ""){ alert(vf_nif_obligatori); return false; }
	if(document.getElementById('itelefon').value == ""){ alert(vf_telefon_obligatori); return false; }
	//if(document.getElementById('iemail').value != ""){}
	
	
	// Adreça
	if( form.spais ){ if( form.spais.options[form.spais.selectedIndex].value == 0 ){ alert(vf_pais_obligatori); return false; } }
	if( form.sprovincia ){ if( form.sprovincia.options[form.sprovincia.selectedIndex].value == 0 ){ alert(vf_provincia_obligatoria); return false; } }
	if( form.smunicipi ){ if( form.smunicipi.options[form.smunicipi.selectedIndex].value == 0 ){ alert(vf_municipi_obligatori); return false; } }
	if( form.icarrer.value == "" ){
		if( form.literalcarrer ){
			alert(vf_carrer_valid_barcelona); return false;
		}else{
			alert(vf_carrer_obligatori); return false;
		}
	}else{
		//if( form.tipusvia == "0" ){ return false; }
		//if(){}
	}
	if( form.inumero.value == "" ){ alert(vf_numero_obligatori); return false; }
	if( isNaN(form.inumero.value) == true){ alert(vf_numero_nomesnumeros); return false; }

	// anònim de veritat????
	if( form.lt[2].checked == true ){
		if( !confirm(vf_enviar_anonim) ){ return false;}
	}
		
	return true;
}


// Formulari especial per la validació de la flor del record
function validarPagamentFlor(){

	// Variables
	form = document.getElementById('fvendaflors');
	
	// difunt
	if( document.getElementById('idifunt') ){
		if( document.getElementById('idifunt').value == "" ){ alert(vf_difunt_no_seleccionat); return false; }
	}else{
		alert(vf_difunt_no_seleccionat);
		return false;
	}

	// quantitat
	if( document.getElementById('quantitat') ){
		if( document.getElementById('quantitat').value == "" ){ alert(fr_quantitat_no_posada); return false; }
	}else{
		alert(fr_quantitat_necesaria);
		return false;
	}

	// pagador
	if(form.tipus[1].checked == true){
		if(document.getElementById('iempresa').value == ""){ alert(vf_nom_empresa_obligatori); return false; }
	}else{
		if(document.getElementById('inom').value == ""){ alert(vf_nom_pagador_obligatori); return false; }
		if(document.getElementById('icog1').value == ""){ alert(vf_cognom1_pagador_obligatori); return false; }
		if(document.getElementById('icog2').value == ""){ alert(vf_cognom2_pagador_obligatori); return false; }	
	}
	if( form.stipdoc ){ if( form.stipdoc.options[form.stipdoc.selectedIndex].value == 0 ){ alert(vf_tipdoc_obligatori); return false; } }
	if(document.getElementById('inif').value == ""){ alert(vf_nif_obligatori); return false; }
	if(document.getElementById('itelefon').value == ""){ alert(vf_telefon_obligatori); return false; }
	//if(document.getElementById('iemail').value != ""){}
	
	
	// Adreça
	if( form.spais ){ if( form.spais.options[form.spais.selectedIndex].value == 0 ){ alert(vf_pais_obligatori); return false; } }
	if( form.sprovincia ){ if( form.sprovincia.options[form.sprovincia.selectedIndex].value == 0 ){ alert(vf_provincia_obligatoria); return false; } }
	if( form.smunicipi ){ if( form.smunicipi.options[form.smunicipi.selectedIndex].value == 0 ){ alert(vf_municipi_obligatori); return false; } }
	if( form.icarrer.value == "" ){
		if( form.literalcarrer ){
			alert(vf_carrer_valid_barcelona); return false;
		}else{
			alert(vf_carrer_obligatori); return false;
		}
	}else{
		//if( form.tipusvia == "0" ){ return false; }
		//if(){}
	}	
	if( form.inumero.value == "" ){ alert(vf_numero_obligatori); return false; }
	if( isNaN(form.inumero.value) == true){ alert(vf_numero_nomesnumeros); return false; }	
			
	return true;
}


function empresa(radio){
	if( radio.checked == true ){
		document.getElementById('capaEmpresa').style.display = 'block';
		document.getElementById('capaiempresa').style.display = 'block';
		document.getElementById('inom').disabled=true;
		document.getElementById('inom').className="vf_inputDisabled";
		document.getElementById('icog1').disabled=true;
		document.getElementById('icog1').className="vf_inputDisabled";	
		document.getElementById('icog2').disabled=true;
		document.getElementById('icog2').className="vf_inputDisabled";
	}
}

function particular(radio){
	if( radio.checked == true ){
		document.getElementById('capaEmpresa').style.display = 'none';
		document.getElementById('capaiempresa').style.display = 'none';		
		document.getElementById('inom').disabled=false;
		document.getElementById('inom').className="vf_input";
		document.getElementById('icog1').disabled=false;
		document.getElementById('icog1').className="vf_input";	
		document.getElementById('icog2').disabled=false;
		document.getElementById('icog2').className="vf_input";		
	}
}

/* Flor del record */
function validarQuantitat(camp){
	// camp ==> fa referència a l'objecte input on està la quantitat.
	if(camp.value==''){
		// Per ara passem, però aquí té que haver-hi un número en breu.
	}else{
		if(isNaN(camp.value)){
			alert(fr_posar_numero);
		}else{
			if(camp.value>0){
				// Variables en funció dels inputs
				var quantitat = camp.value;
				var preu_unitari = document.getElementById('preu_unitari').value;
				var iva = document.getElementById('iva').value;
				//alert(quantitat+'\n\n'+preu_unitari+'\n\n'+iva+'\n\n'+import_final);
				// Canviem els divs en funció de la nova quantitat
				document.getElementById('fr_preu').innerHTML = (preu_unitari*quantitat).toFixed(2)+'&nbsp;€';
				document.getElementById('fr_iva').innerHTML = (iva/100*preu_unitari*quantitat).toFixed(2)+'&nbsp;€';
				document.getElementById('fr_import').innerHTML = Math.round((((preu_unitari*quantitat)) + ((iva/100*preu_unitari*quantitat))).toFixed(2)).toFixed(2) + '&nbsp;€';
				// Canviem els inputs en funció de la nova quantitat
				//document.getElementById('preu_unitari').value = (preu_unitari*quantitat).toFixed(2);
				//document.getElementById('iva').value = (iva/100*preu_unitari*quantitat).toFixed(2);
				document.getElementById('total').value = Math.round((((preu_unitari*quantitat)) + ((iva/100*preu_unitari*quantitat))).toFixed(2)).toFixed(2);
				document.getElementById('import').value = quantitat * preu_unitari;
			}else{
				// Número més gran que 0!!
				alert(fr_major_0);
			}
		}
	}
}































