var strDeshabilitat = "#D4D0C8";
var strHabilitat = "#FFFFFF";

function obrirInscripcio(strHref, strTarget) {
	return window.open(strHref, strTarget, "width=800,height=600,scrollbars,resizable");
}

function comprovarEnviarAmic(objForm) {
	var arrErronis = new Array();
	var arrMissatgeError = new Array();
	var blnFormOK = false;
	var intErronis = 0;
	var objAvisLegal = objForm["AvisLegal"];
	var strError = "ERROR!\n\n";

	if (objAvisLegal.checked) {
		arrMissatgeError['C'] = "Els següents camps són obligatoris";
		arrMissatgeError['E'] = "Los siguientes campos son obligatorios";
		arrMissatgeError['W'] = "The following fields are required";
		var objMail_Para = objForm["##Mail_Para"];
		var objMail_De = objForm[1];
		//var objMail_De = objForm["##Mail_De"];
		//var objMail_Asunto = objForm["##Mail_Asunto"];
		//var objPagina_Web = objForm["##2#Pàgina web"];

		if (!checkEmail(objMail_Para.value)) intErronis = arrErronis.push(objMail_Para.title);
		if (!checkEmail(objMail_De.value)) intErronis = arrErronis.push(objMail_De.title);
		//if (objMail_Asunto.value == "")  intErronis = arrErronis.push(objMail_Asunto.title);
		//if (objPagina_Web.value == "") intErronis = arrErronis.push(objPagina_Web.title);
		blnFormOK = intErronis == 0;
		if (!blnFormOK) alert(strError + arrMissatgeError[objForm["lang"].value] + ":\n\n- " + arrErronis.join("\n- "));
		//else window.open("", "resultatEnviament", "width=512,height=384");
//		return false;
	} // if (objForm["AvisLegal"].checked)
	else alert(strError + objAvisLegal.title);
	return blnFormOK;
} // comprovarEnviarAmic

function IsExplorer() {
	var rexMSIExplorer = /Microsoft Internet Explorer/i;
	return (navigator.appName.search(rexMSIExplorer) != -1);
} // IsExplorer

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 testNumero(strNumero) {
	var rexCIF = /^[A-HK-NPQS]{1}[0-9]{7}[A-J,0-9]{1}$/i;
	var rexNIF = /^[0-9]{8}[A-Z]{1}$/i;
	var strLetraCIF = " ABCDEFGHIJ";
	var strLetraDNI = "TRWAGMYFPDXBNJZSQVHLCKE";
	var blnNumeroOK = false;

	// "Limpiar" un poco el número
	strNumero = strNumero.toUpperCase().replace('-', '');
	// Si es un código NIE eliminamos la X y lo tratamos como un NIF
	if (strNumero.charAt(0) == 'X') strNumero = '0' + strNumero.substr(1);
	if (strNumero.search(rexNIF) != -1) {
		intPosicion = parseInt(strNumero.substr(0, 8), 10) % 23;
		blnNumeroOK = strNumero.charAt(8) == strLetraDNI.substr(intPosicion, 1);
	} // 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) || (intI == 4) || (intI == 6)) intPares += intNumero;
				else {
					intTempImpar = intNumero * 2;
					if (intTempImpar > 9) intTempImpar = parseInt(intTempImpar / 10) + parseInt(intTempImpar % 10);
					intImpares += intTempImpar;
				} // else if ((intI == 2) || (intI == 4) || (intI == 6))
		} // for (var intI = 1; intI < 8; intI ++)
		strSuma = (intPares + intImpares).toString();
		intControl = 10 - parseInt(strSuma.substr(strSuma.length - 1, 1), 10);
		blnNumeroOK = (intControl == parseInt(chrUltimo)) || (strLetraCIF.charAt(intControl) == chrUltimo);
	} // if (strNumero.search(rexCIF) != -1)
	return blnNumeroOK;
} // testNumero

function checkEmail(strEmail) {
		var intArroba = strEmail.indexOf('@');
	var intPunto = strEmail.lastIndexOf('.');
		return ((strEmail.value != "") && (intArroba > 0) & (intArroba != (strEmail.length - 1)) && (intPunto > intArroba) && (strEmail.length - (intPunto + 1) >= 2));
} // checkEmail

function conmutarVisibilitat(strNomCapa) {
	var objCapa = new Object(document.getElementById(strNomCapa));

	with (objCapa.style) {
		if (display.length == 0) display = "none"; else display = "";
	} // with (objCapa.style)
} // conmutarVisibilitat

/*************************************************************************/
/* Secció dedica a la barra d'eines (/escolasert/templates/Toolbar.html  */
/*************************************************************************/
strAccio = "";
function prepararEnviament(objForm) {
	with (document) {
		var objBase = getElementById("base");
		var objURL = getElementById("url");
		var objTitle = getElementById("title");
		var objParm1 = getElementById("parm1");
		var objParm2 = getElementById("parm2");
	} // with (document)

	switch (strAccio) {
		case "del.icio.us":
		case "digg":
		case "fresqui":
		case "meneame":
		case "reddit":
			objURL.name = "url";
			if (strAccio != "meneame") objTitle.name = "title"; else objTitle.name = "";
			if (strAccio != "digg") objParm1.name = ""; else { objParm1.name = "phase"; objParm1.value = "2"; }
			objParm2.name = "";
			break;
		case "yahoo":
			objURL.name = "u";
			objTitle.name = "t";
			objParm1.name = "";
			objParm2.name = "";
			break;
		case "technorati":
			objURL.name = "";
			objTitle.name = "";
			objParm1.name = "";
			objParm2.name = "";
			break;
        case "enviaramic":
        	objURL.name = "url";
            objTitle.name = "titol";
			break;
	} // switch (strAccio)

	switch (strAccio) {
		case "enviaramic": objForm.target = "Enviament"; objForm.method = "POST"; break;
		default: objForm.target = "_blank"; objForm.method = "POST";
	} // switch (strAccio)

	objForm.action = "http://";
	switch (strAccio) {
		case "del.icio.us": objForm.action += "del.icio.us/post"; break;
		case "meneame": objForm.action += "meneame.net/submit.php"; break;
		case "fresqui": objForm.action += "tec.fresqui.com/post"; break;
		case "digg": objForm.action += "digg.com/submit"; break;
		case "yahoo": objForm.action += "myweb2.search.yahoo.com/myresults/bookmarklet"; break;
		case "reddit": objForm.action += "reddit.com/submit"; break;
		case "technorati": objForm.action += "technorati.com/search/" + objBase.value; break;
		case "enviaramic": objForm.action = "enviar.php"; window.open(objForm.action, "Enviament", "width=768,height=576,scrollbars"); break;
	} // switch(strAccio);
	if (objURL.name != "") objURL.value = "http://" + objBase.value;
	//alert("Action: " + objForm.action + "\nURL: [" + objURL.name + "]->" + objURL.value + "\nTitle: [" + objTitle.name + "]->" + objTitle.value + "\nParm1: [" + objParm1.name + "]->" + objParm1.value + "\nParm2: [" + objParm2.name + "]->" + objParm2.value + "\nBase [" + objBase.name + "]->" + objBase.value);
	return true; // De momento
} // prepararEnviament
/*************************************************************************/


/*************************************************************************
	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 (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 (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;
}