////////////////////////////////////
// LINGUA M?N - Casa de les Lleng?es
////////////////////////////////////


function centerPopup(txtURL, nameWindow, w, h) { 
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'location=no,scrollbars=yes,resizable=yes,width='+w+',height='+h+',top='+wint+',left='+winl+'';
	window.open(txtURL, nameWindow, winprops);
} 

function chkDate(txtDate,txtNomCamp) {
	var x=0;
	var camp = document.getElementById(txtNomCamp);
	while(txtDate.indexOf('/')!=-1) {
		txtDate = txtDate.replace('/','-');
		x++;
	}
	if (txtDate.length>0) {
		if (txtDate.length!=10 || x!=2) { alert('La data no te un format correcte.'); camp.focus(); camp.select(); }
	}
}

function chkPropostaBP() {
	var titol = document.frmSend.titol;
	var personaContacte = document.frmSend.persona_contacte;
	var correuContacte = document.frmSend.correu_contacte;
	
	if (titol.value && personaContacte.value && correuContacte.value) {
		if (correuContacte.value.indexOf('@')!=-1 && correuContacte.value.indexOf('.')!=-1) {
			return(true);
		}else{
			alert("L'adre?a de correu electronic no és correcta.");
			return(false);
		}
	}else{
		alert("El titol, la persona de contacte i el correu electronic son dades requerides.");
		return(false);
	}
}

function chkPropostaCentre() {
	var nom = document.frmSend.nom;
	var adreca = document.frmSend.adreca;
	var municipi = document.frmSend.municipi;
	var provincia = document.frmSend.provincia;
	var comarca = document.frmSend.comarca;
	var cp = document.frmSend.cp;
	var telefon_1 = document.frmSend.telefon_1;
	var email_adress = document.frmSend.email_adress;
	var pcontacte = document.frmSend.pcontacte;
	
	if (nom.value && adreca.value && municipi.value && provincia.value  && comarca.value  && cp.value  && telefon_1.value  && email_adress.value  && pcontacte.value) {
		if (correuContacte.value.indexOf('@')!=-1 && correuContacte.value.indexOf('.')!=-1) {
			return(true);
		}else{
			alert("L'adre&ccedil;a de correu electronic no és correcta.");
			return(false);
		}
	}else{
		alert("Falten dades requerides.");
		return(false);
	}
	return(false);
}


function chkSearchBP() {
	var diesPerMes = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var VdataInici = document.frmSearch.dataInici;
	var VdataAcabament = document.frmSearch.dataAcabament;
	var VmesInici = '';
	var VdiaInici = '';
	var VanoInici = ''; 
	var VmesAcabament = '';
	var VdiaAcabament = '';
	var VanoAcabament = ''; 
	
	if (document.frmSearch.titol.value!="" || document.frmSearch.destinataris.value!="" || document.frmSearch.tipus.value!="" || document.frmSearch.identificacio.value!="" || document.frmSearch.dataInici.value!="" || document.frmSearch.dataAcabament.value!="") {
		if (VdataInici.value!='' && VdataInici.value!=null || VdataAcabament.value!='' && VdataAcabament.value!=null) {
			if (VdataInici.value!='' && VdataInici.value.indexOf('/')!=-1 && VdataInici.value.length==10) {
				VdiaInici = VdataInici.value.substring(0, VdataInici.value.indexOf("/"));
				VmesInici = VdataInici.value.substring(VdataInici.value.indexOf("/")+1, VdataInici.value.lastIndexOf("/"));
				VanoInici = VdataInici.value.substring(VdataInici.value.lastIndexOf("/")+1, VdataInici.value.indexOf(" "));
			}
			if (VdataAcabament.value!='' && VdataAcabament.value.indexOf('/')!=-1 && VdataAcabament.value.length==10) {
				VdiaAcabament = VdataAcabament.value.substring(0, VdataAcabament.value.indexOf("/"));
				VmesAcabament = VdataAcabament.value.substring(VdataAcabament.value.indexOf("/")+1, VdataAcabament.value.lastIndexOf("/"));
				VanoAcabament = VdataAcabament.value.substring(VdataAcabament.value.lastIndexOf("/")+1, VdataAcabament.value.indexOf(" "));
			}
			if (VdiaInici!='' && VmesInici!='' && VanoInici!='' || VdiaAcabament!='' && VmesAcabament!='' && VanoAcabament!='') {
				if (VdiaInici>diesPerMes[VmesInici-1] || VdiaAcabament>diesPerMes[VmesAcabament-1] || VmesInici>12 ||  VmesAcabament>12) {
					alert("La data indicada no existeix.");
					if (VdiaInici>diesPerMes[VmesInici-1] || VmesInici>12) VdataInici.select();
					if (VdiaAcabament>diesPerMes[VmesAcabament-1] || VmesAcabament>12) VdataAcabament.select();
					return(false);
				}else{
					if (VdataInici.value=='') VdataInici.value = VdataAcabament.value;
					if (VdataAcabament.value=='') VdataAcabament.value = VdataInici.value;
					return(true);
				}
			}else{
				alert('La data no te un format correcte.');
				return(false);
			}
		}
		return(true);
	}else{
		alert("No s'ha d'indicat cap criteri per a la cerca.");
		return(false);
	}
}

function transOption(origen,destino) {
	var oOption;
	if (origen.options.length>0) {
		for (i=0; i<origen.options.length; i++) {
			if (origen.options[i].selected) {
				oOption = document.createElement("OPTION");
				oOption.text=origen.options[i].text;
				oOption.value=origen.options[i].value;
				destino.add(oOption);
				origen.options.remove(i);
			}
		}
	}
}

function changeEditLanguage(idi) {
	var urlActual = document.location.href;
	urlActual = urlActual.substring(0, urlActual.indexOf('idioma=')) + "idioma="+idi;
	document.location.replace(urlActual);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- El camp '+nm+' no cont? una adre?a de correu electr?nic v?lida.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- El camp '+nm+' ha de ser un nombre.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- El camp '+nm+' ha de ser un nombre entre '+min+' i '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- El camp '+nm+' és una dada requerida.\n'; }
  } if (errors) alert('S\'han trobat els seg?ents errors:\n\n'+errors);
  document.MM_returnValue = (errors == '');
}

