function isMail(obj){
	
	if((""+obj.value).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == 0){
		Error=true;
	}
	else Error=false;

	return Error;
}

function afegirServei(oForm){



	if(!oForm.legal.checked){
		alert("Ha de llegir i acceptar les clàusules de consentiment");
	}
	else if(!isMail(document.getElementById("ent_email"))) {
	alert(cons_missatgejs_06);
	return false;
}
else{



 if(!formWithErrors(oForm)){

	var strTarget = "salida";
	var URL = "./app_fe_afegeix_entitat.php";
	var strMethod = "POST";


	if(document.getElementById("ent_codentitat").value == "" && document.getElementById("fileupload").value != ""){
		oForm.action="./app_fe_afegeix_entitat.php";
		oForm.method="POST";
		oForm.submit();
	}
	else{

	document.getElementById(strTarget).innerHTML = "<blockquote><p>Carregant dades...</p></blockquote>";

	var myConn = new XHConn();
	var fnWhenDone = function (oXML) {

		var codEntitat = oXML.responseText;

		if(codEntitat != "0"){
			document.getElementById(strTarget).innerHTML = "<blockquote><p>S'ha desat el servei número " + iQuants + " correctament.<br />Si vol, pot introduïr un altre servei.</p></blockquote>";
			document.getElementById("fs_entitats").style.display = "none";

			buidaCamps(oForm);

			document.getElementById("ent_codentitat").value = codEntitat;

			iQuants++;
		}
		else{
			document.getElementById(strTarget).innerHTML = "<blockquote><p>No s'ha trobat cap entitat amb aquest NIF. Verifiqui el valor introduït i torni-ho a provar.</p></blockquote>";
		}

	};

	if (!myConn){
		alert(cons_missatgejs_1);
	}
	
		c = 0;
		x = 0;
		var objSelDest = document.getElementById('llistat_ambits');
		l = objSelDest.options.length;
		var arrAmbits = new Array();
		while(c<l){
			if(objSelDest.options[c].selected == true){
				arrAmbits[x] = objSelDest.options[c].value
				x++;
			}
			c++;
		}

		c = 0;
		x = 0;
		var objSelDest = document.getElementById('llistat_destinataris');
		l = objSelDest.options.length;
		var  arrDestinataris = new Array();
		while(c<l){
			if(objSelDest.options[c].selected == true){
				 arrDestinataris[x] = objSelDest.options[c].value
				x++;
			}
			c++;
		}
		
		c = 0;
		x = 0;
		var objSelDest = document.getElementById('ens_codsuport');
		l = objSelDest.options.length;
		var arrSupport = new Array();
		while(c<l){
			if(objSelDest.options[c].selected == true){
				arrSupport[x] = objSelDest.options[c].value
				x++;
			}
			c++;
		}

		

		//Assignem els valors dels arrays als hiddens.
		document.getElementById('ambits').value = arrAmbits;
		document.getElementById('destinataris').value = arrDestinataris;
		document.getElementById('support').value = arrSupport;



	strVars = returnVars(oForm);

	myConn.connect(URL, strMethod, strVars, fnWhenDone);

	}
    }
    else{
	alert(cons_missatgejs_01);
    }
	}
}

function formWithErrors(oForm){
	bError = false;

	if(document.getElementById('ent_codentitat').value == ""){

		if(oForm.ent_nom.value == ""){
			bError = true;
		}

		if(oForm.ent_telefon.value == ""){
			bError = true;
		}

		if(!isMail(oForm.ent_email)){
			bError = true;
		}

		if(oForm.ent_codcomarca.options.selectedIndex == 0){
			bError = true;
		}

		if(oForm.ent_codpoblacio.options.selectedIndex == 0){
			bError = true;
		}
		if(oForm.ent_descripcio.value.length > 1000){
			bError = true;
		}
		
	}


	if(oForm.ens_titol.value == ""){
		bError = true;
	}

	if(oForm.ens_descripcio.value == ""){
		bError = true;
	}

	c = 0;
	x = 0;
	var objSelDest = document.getElementById('llistat_ambits');
	l = objSelDest.options.length;
	while(c < l){
		if(objSelDest.options[c].selected == true){
			x++;
		}
		c++;
	}
	if(x == 0){
		bError = true;
	}

	return bError;

}


function buidaCamps(objForm){
	if(objForm){
		var c = 0;
		
		while(c<objForm.elements.length){
			if(	objForm.elements[c].type == "text" || 
				objForm.elements[c].type == "textarea" || 
				objForm.elements[c].type == "password"){
					
				objForm.elements[c].value = "";
			}
			
			if(objForm.elements[c].type == "select-multiple"){
				x = 0;
				y = objForm.elements[c].options.length;
				while(x < y){
					objForm.elements[c].options[x].selected = false;
					x++;
				}
			}

			c++;
		}			
	}	
}



/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn(){
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.bComplete = "";
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
      
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function isNif(dni) {
  numero = dni.substr(0,dni.length-1);
  let = dni.substr(dni.length-1,1);
  numero = numero % 23;
  letra='TRWAGMYFPDXBNJZSQVHLCKET';
  letra=letra.substring(numero,numero+1);
  if (letra!=let) return false;
  else return true;
} 

function isTel(strTel){
	var bSalida = false;

	strTel = strTel.value;

	if(strTel != ""){

		if((strTel.length == 9 || strTel.length == 11) && !isNaN(strTel)){
			bSalida = true;
		}
	}

	return bSalida;
}

function verificaTel(strObj){

	if(isTel(strObj)){

	strValue = strObj.value;

	var strTarget = "salida";
	var URL = "./app_fe_verifica_tel_entitat.php";
	var strMethod = "GET";

	document.getElementById(strTarget).innerHTML = "<blockquote><p>Carregant dades...</p></blockquote>";

	var myConn = new XHConn();
	var fnWhenDone = function (oXML) {
		codEntitat = oXML.responseText;
		if(codEntitat != "0"){

			document.getElementById(strTarget).innerHTML = "<blockquote><p>Ara ja pot omplir el formulari d'introducció de serveis.</p></blockquote>"
			document.getElementById("fs_entitats").style.display = "none";

			document.getElementById("ent_codentitat").value = codEntitat;

		}
		else{
			document.getElementById(strTarget).innerHTML = "<blockquote><p>No s'ha trobat cap entitat amb aquest número de telèfon. Verifiqui el valor introduït i torni-ho a provar.</p></blockquote>";
		}

	};

	if (!myConn){
		alert(cons_missatgejs_1);
	}
	
	var strVars = "tel=" + strValue;

	myConn.connect(URL, strMethod, strVars, fnWhenDone);

	}
	else{
		alert(cons_missatgejs_05);
	}

}




function returnVars(objForm){
	var cadena 	= "";
	var c = 0;

	while(c<objForm.elements.length){
		if(	objForm.elements[c].type == "text" || 
			objForm.elements[c].type == "textarea" || 
			objForm.elements[c].type == "password" || 
			objForm.elements[c].type == "select-one" ||
			(objForm.elements[c].type == "radio" && objForm.elements[c].checked) ||
			objForm.elements[c].type == "hidden" ||
			(objForm.elements[c].type == "checkbox" && objForm.elements[c].checked)
			){

			if(objForm.elements[c].type != "select-one"){				
				cadena += objForm.elements[c].name + "=" + escape(objForm.elements[c].value) + "&";
			}
			else{
				cadena += objForm.elements[c].name + "=" + escape(objForm.elements[c].options[objForm.elements[c].selectedIndex].value) + "&";
			}
		}
		
		c++;	
	}

	cadena = cadena.substr(0, (cadena.length - 1));

	return cadena;
}



function recarregaMunicipis(codcomarca, strObjDest){

	var strTarget = "llista_municipis";
	var objDest = document.getElementById(strObjDest);
	var URL = "./app_fe_retorna_municipis.php";
	var strMethod = "GET";


	document.getElementById(strTarget).innerHTML = "Carregant dades...";


	var myConn = new XHConn();
	var fnWhenDone = function (oXML) {
		document.getElementById(strTarget).innerHTML = oXML.responseText;
	};

	if (!myConn){
		alert(cons_missatgejs_1);
	}
	
	var strVars = "nomselect=" + strObjDest + "&codcomarca="+codcomarca;

	myConn.connect(URL, strMethod, strVars, fnWhenDone);
}


function isMail(obj){
	
	if((""+obj.value).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == 0){
		Error=true;
	}
	else Error=false;

	return Error;
}


