// JavaScript Document
var blnCargarProvincia = false;
var strAlerta = "ERROR!\n\nCal omplir correctament els següents camps:\n\n- ";

function agafarNumero(strDNI, chrTipus) {
	var blnNumeroOK = new Boolean(false);
	var chrLletraNIE = new String();
	var chrPrimero = new String();
	var chrSeparador = new String();
	var chrUltimo = new String();
	var intControl = new Number();
	var intImpares = new Number();
	var intNumero = new Number();
	var intPares = new Number();
	var intPosicion = new Number();
	var intTempImpar = new Number();
	var rexCIF = /^[A-HJ-NPQS]{1}[0-9]{7}[A-J0-9]{1}/i;
	var rexNIF = /^[0-9]{8}[A-Z]{1}/i;
	var rexSeparador = /[^0-9a-z]/i;
	var strLetraCIF = new String(" ABCDEFGHIJ");
	var strLetraDNI = new String("TRWAGMYFPDXBNJZSQVHLCKE");
	var strNumero = new String();
	var strNumeroIniciNIE = new String();
	var strSuma = new String();

	if (chrTipus == null) chrTipus = 'N'; // Si no se especifica nada se trata como un NIF/NIE;
	strDNI = strDNI.toUpperCase();
	// "Limpiar" un poco el número
	//if ((chrSeparador = strDNI.match(rexSeparador)) != null) strDNI = strDNI.replace(chrSeparador, '');
	while ((chrSeparador = strDNI.match(rexSeparador)) != null) strDNI = strDNI.replace(chrSeparador, '');
	strNumero = strDNI;
	// Si es un código NIE, cambiamos la primera letraa por su numero y lo tratamos como un NIF
	chrLletraNIE = strNumero.charAt(0);
	switch (chrLletraNIE) {
		case 'T':
		case 'X':
			strNumeroIniciNIE = '0';
			break;
		case 'Y': strNumeroIniciNIE = '1'; break;
		case 'Z': strNumeroIniciNIE = '2'; break;
		default: strNumeroIniciNIE = chrLletraNIE;
	} // switch (chrLletraNIE)
	strNumero = strNumeroIniciNIE + strNumero.substr(1);
	if (strNumero.search(rexNIF) != -1) {
		intPosicion = parseInt(strNumero.substr(0, 8), 10) % 23;
		blnNumeroOK = (strNumero.charAt(8) == strLetraDNI.substr(intPosicion, 1)) && (chrTipus == 'N');
	} // if (strNumero.search(regexpNIF) != -1)
	if (strNumero.search(rexCIF) != -1) {
		chrPrimero = strNumero.charAt(0);
		chrUltimo = strNumero.charAt(8);
		intImpares = 0;
		intPares = 0;
		intTempImpar = 0;
		intNumero = 0;
		for (var intI = 1; intI < 8; intI++) {
			intNumero = parseInt(strNumero.charAt(intI), 10);
			if (!(intI % 2)) intPares += intNumero;
			else {
				intTempImpar = intNumero * 2;
				if (intTempImpar > 9) intTempImpar = parseInt(intTempImpar / 10, 10) + parseInt(intTempImpar % 10, 10);
				intImpares += intTempImpar;
			} // else (!(intI % 2))
		} // for (var intI = 1; intI < 8; intI++)
		strSuma = (intPares + intImpares).toString();
		intControl = (10 - parseInt(strSuma.charAt(strSuma.length - 1), 10)) % 10;
		blnNumeroOK = ((intControl == parseInt(chrUltimo, 10)) || (strLetraCIF.charAt(intControl) == chrUltimo)) && (chrTipus == 'C');
	} // if (strNumero.search(rexCIF) != -1)
	if (blnNumeroOK) return strDNI; else return blnNumeroOK;
} // agafarNumero

function agafarNumCol(strNumCol) {
	//return strNumCol.match(/^([0-9]{1,})|([-|\/][0-9]{1})$/i); // Pendiente de ajustar
	return strNumCol.match(/^[0-9]{1,}/i);
} // testNumCol

function canviarVisibilitat(objCapa) {
	with (objCapa.style) {
		if (display == "") display = "none"; else display = "";
		if (visibility == "show") visibility = "hidden"; else visibility = "show";
	} // with (objCapa.style)
} // canviarVisibilidtat

function marcarDesplegable(objDesplegable, strOpcio) {
	var blnTrobat;

	for (var i = 0, f = objDesplegable.length, blnTrobat = false; (i < f) && !blnTrobat; i++) {
		if (objDesplegable[i].value == strOpcio) {
			blnTrobat = true;
			objDesplegable[i].selected = true;
		} // if (objDesplegable[i].value == strOpcio)
	} // for (i = 0, f = objDesplegable.length; (i < f) && !blnTrobat; i++)
	return blnTrobat;
} // marcarDesplegable

function resetejarForm(objForm) {
	var intI = 0;
	var intL = 0;
	var objElement = null;

	for (intI = 0, intL = objForm.length, objElement = objForm.elements[intI]; intI < intL; intI++) {
		switch (objElement.type) {
			case "file":
			case "password":
			case "text":
			case "textarea":
				objElement.value = "";
				break;
			case "radio":
				objElement.radiobutton[0].checked = true;
				break;
			case "select-one":
			case "select-multiple":
				objElement.selectedIndex = 0;
				break;
			default:
		} // switch (type)
	} // for (intI = 0, intL = objForm.length; intI < intL; intI++)
} // resetejarForm

function testEmail(strEmail) {
	var rexEmail = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	return (strEmail.search(rexEmail) != -1);
} // testEmail

function verificarRegistre(objForm) {
	var arrErronisPersonals = new Array();
	var arrErronisProfessionals = new Array();
	var arrErronisConnexio = new Array();
	var arrErronisGeneral = new Array();
	var blnFormOK = new Boolean(false);
	var objNIFtmp = new Object();
	var strErronis = new String();

	eliminarEspais(objForm);
	if (!blnCargarProvincia) {
		with (objForm) {
			// Part de les dades personals
			if (!Nom.value.length) arrErronisPersonals.push(Nom.title);
			if (!Cognoms.value.length) arrErronisPersonals.push(Cognoms.title);
			if (!Tipus_Document.selectedIndex) arrErronisPersonals.push(Tipus_Document.title);
			else if ((Tipus_Document.value != 'P') && (NIF.value.length > 8)) {
					objNIFtmp = agafarNumero(NIF.value);
					if (objNIFtmp == false) arrErronisPersonals.push("NIF/NIE incorrecte"); else NIF.value = objNIFtmp;
				 } // if (Tipus_Document.value != 'P')
			if (!NIF.value.length) arrErronisPersonals.push(NIF.title);
			if (!Any_Naixement.value.length) arrErronisPersonals.push(Any_Naixement.title);
			if (!Nacionalitat.selectedIndex) arrErronisPersonals.push(Nacionalitat.title);
			if (!Pais.selectedIndex) arrErronisPersonals.push(Pais.title);
			if (!Tipus_Carrer.selectedIndex) arrErronisPersonals.push(Tipus_Carrer.title);
			if (!Adreca.value.length) arrErronisPersonals.push(Adreca.title);
			if (!Numero_Adreca.value.length) arrErronisPersonals.push(Numero_Adreca.title);
			if (!Municipi.selectedIndex) arrErronisPersonals.push(Municipi.title);
			if (!Provincia.selectedIndex) arrErronisPersonals.push(Provincia.title);
			if (!Codi_Postal.value.length) arrErronisPersonals.push(Codi_Postal.title);
			if (!Telefon.value.length) arrErronisPersonals.push(Telefon.title);

			// Part de les dades professionals
			if (document.getElementById("DadesProfessionals").style.display == "") {
				Dades_Professionals.value = "S";
				if (!Tipus_Carrer_Professional.selectedIndex) arrErronisProfessionals.push(Tipus_Carrer_Professional.title);
				if (!Adreca_Professional.value.length) arrErronisProfessionals.push(Adreca_Professional.title);
				if (!Numero_Adreca_Professional.value.length) arrErronisProfessionals.push(Numero_Adreca_Professional.title);
				if (!Municipi_Professional.selectedIndex) arrErronisProfessionals.push(Municipi_Professional.title);
				if (!Provincia_Professional.selectedIndex) arrErronisProfessionals.push(Provincia_Professional.title);
				if (!Codi_Postal_Professional.value.length) arrErronisProfessionals.push(Codi_Postal_Professional.title);
				if (!Telefon_Professional.value.length) arrErronisProfessionals.push(Telefon_Professional.title);
			} // if (document.getElementById("DadesProfessionals").style.display == "")
			else Dades_Professionals.value = "N";

			// Part de les dades de connexió
			if (!Usuari.value.length) arrErronisConnexio.push(Usuari.title);
			if (!Email.value.length || !testEmail(Email.value)) arrErronisConnexio.push(Email.title);
			if (!Contrassenya.value.length) arrErronisConnexio.push(Contrassenya.title)
			if (!Confirmar_Contrassenya.value.length) arrErronisConnexio.push(Confirmar_Contrassenya.title);
			if (Contrassenya.value != Confirmar_Contrassenya.value) arrErronisConnexio.push("Contrassenya i confirmar contrassenya no són iguals!");

			// Part de l'advertiment legal
			if (!AcceptaAdvertimentLegal.checked) arrErronisGeneral.push(AcceptaAdvertimentLegal.title)
		} // with (objForm)
		blnFormOK = !(arrErronisPersonals.length || arrErronisProfessionals.length || arrErronisConnexio.length || arrErronisGeneral.length );
		if (!blnFormOK) {
			if (arrErronisPersonals.length) strErronis += "Dades personals:\n\t* " + arrErronisPersonals.join("\n\t* ") + "\n\n";
			if (arrErronisProfessionals.length) strErronis += "Dades profesionals:\n\t* " + arrErronisProfessionals.join("\n\t* ") + "\n\n";
			if (arrErronisConnexio.length) strErronis += "Dades de connexió:\n\t* " + arrErronisConnexio.join("\n\t* ") + "\n\n";
			if (arrErronisGeneral.length) strErronis += "Altres:\n\t* " + arrErronisGeneral.join("\n\t* ") + "\n\n";
			alert(strAlerta + strErronis);
		} // if (!blnFormOK)
	} // if (!blnCargarProvincia)
	else {
		blnCargarProvincia = false;
		blnFormOK = true;
	} // else if (!blnCargarProvincia)
	return blnFormOK;
} // verificarRegistre

function comprovarRegistre(objForm) {
	var arrErronis = new Array();
	var blnFormOK = new Boolean(false);
	var intIndex = new Number();
	var intTotalCamps = new Number();
	var strNIF = new String();
	var strClau = new String("Cumplir el min. i màx de: ");

	eliminarEspais(objForm);
	with (objForm) {
		if (Nom_contacte.value.length == 0) arrErronis.push(Nom_contacte.alt);
		if ((Email_contacte.value.length == 0) || !testEmail(Email_contacte.value)) arrErronis.push(Email_contacte.alt);
		if (Nom_institucio.value.length == 0) arrErronis.push(Nom_institucio.alt);
		strNIF = agafarNumero(NIF_institucio.value);
		if ((NIF_institucio.value.length == 0) || !strNIF) arrErronis.push(NIF_institucio.alt); else NIF_institucio.value = strNIF;
		if ((Clau_contacte.value.length < 6) || (Clau_contacte.value.length > 50)) arrErronis.push(strClau + Clau_contacte.alt);
		if ((Confirmar_clau_contacte.value.length < 6) || (Confirmar_clau_contacte.value.length > 50)) arrErronis.push(strClau + Confirmar_clau_contacte.alt);
		if (Clau_contacte.value != Confirmar_clau_contacte.value) arrErronis.push("La clau i la seva confirmació no són iguals");
		if (!AdvertimentLegal.checked) arrErronis.push(AdvertimentLegal.alt);
	} // with (objForm)
	blnFormOK = arrErronis.length == 0;
	if (!blnFormOK) alert(strAlerta + "- " + arrErronis.join("!\n- ") + "!\n");
	return blnFormOK;
	//return false; // Per fer proves de funcionament
} // comprovarRegistre



/*************************************************************************
	Altres funcions utils
*************************************************************************/
function ltrim(s) {
	return s.replace(/^\s+/, "");
} // ltrim

function rtrim(s) {
	return s.replace(/\s+$/, "");
} // trim

function trim(s) {
	return rtrim(ltrim(s));
} // trim

function eliminarEspais(objForm) {
	var intI = new Number();
	var intTotal = objForm.length;
	var objElements = objForm.elements;
	var objElement = new Object();

	for (intI = 0; intI < intTotal; intI++) {
		objElement = objElements[intI];
		if ((objElement.type == "text") && (objElement.value.search(/\s/g) != -1)) objElement.value = trim(objElement.value);
	} // for (intI = 0; intI < intTotal; intI++)
} // eliminarEspais

function seeContest(_idContest, _idDelegation, _idIdioma) {
	  document.location.href = "/barcelona/concursos/cat/fitxa_concurs.jsp?idNum=" + _idContest + "&idDel=" + _idDelegation + "&idLan=" + _idIdioma + "&from=list_contest";
} // seeContest



/*************************************************************************
	This code is from Dynamic Web Coding at http://www.dyn-web.com/
	See Terms of Use at http://www.dyn-web.com/bus/terms.html
	regarding conditions under which you may use this code.
	This notice must be retained in the code as is!
*************************************************************************/

function getDocHeight(doc) {
	var docHt = 0, sh, oh;
	if (doc.height) docHt = doc.height;
	else if (doc.body) {
		if (doc.body.offsetHeight) docHt = sh = doc.body.offsetHeight;
		if (doc.body.clientHeight) docHt = oh = doc.body.clientHeight;
		if (doc.body.scrollHeight) docHt = oh = doc.body.scrollHeight;
		if (sh && oh) docHt = Math.max(sh, oh);
	}
	return docHt;
}
function setIframeHeight(iframeName) {
	var iframeWin = window.frames[iframeName];
	var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
	if ( iframeEl && iframeWin ) {
		iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous	
		var docHt = getDocHeight(iframeWin.document);
		// need to add to height to be sure it will all show
		if (docHt) iframeEl.style.height = docHt + 30 + "px";
	}
}

function getDocWidth(doc) {
	var docWh = 0, sw, ow;
	if (doc.width) docWh = doc.width;
	else if (doc.body) {
		if (doc.body.offsettWidth) docWh = sw = doc.body.offsetWidth;
		if (doc.body.clientWidth) docWh = ow = doc.body.clientWidth;
		if (doc.body.scrollWidth) docWh = ow = doc.body.scrollWidth;
		if (sw && ow) docWh = Math.max(sw, ow);
	}
	return docWh;
}

function setIframeWidth(iframeName) {
	var iframeWin = window.frames[iframeName];
	var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
	if ( iframeEl && iframeWin ) {
		iframeEl.style.width = "auto"; // helps resize (for some) if new doc shorter than previous	
		var docWh = getDocWidth(iframeWin.document);
		// need to add to height to be sure it will all show
		if (docWh) iframeEl.style.width = docWh + 30 + "px";
	}
}

function loadIframe(iframeName, url) {
	if ( window.frames[iframeName] ) {
		window.frames[iframeName].location = url;	 
		return false;
	}
	else return true;
}