function headerMsg(msg) {
	if (location.search=="?error") {
		document.getElementById("oMsg").className="error";
		document.write("<p><strong>A ocurrido un error inesperado mientras se procesaba, por favor intentalo de nuevo mas tarde.</strong></p>");
	} else {
		document.write(msg);
	}
}

function validate(oForm) {
	var aReq=[];
	for (var iElem=0;iElem<oForm.length;iElem++) {
		var oInp=oForm[iElem];
		if (oInp.getAttribute("req") && ((oInp.type.indexOf("select")!=0 && oInp.value.trim()=="") || (oInp.type.indexOf("select")==0 && oInp.options[oInp.selectedIndex].text.trim()==""))) {
			aReq[aReq.length]=getLabel(oInp).innerHTML.replace(":","");
			markEls(oInp);
		}
	}
	if (aReq.length) {
		showError("Los siguientes campos son necesarios: "+aReq.join(", ")+".");
		return false;
	} else {
		if (oForm.Email) {
			if (!/^[^@]+@([0-9a-z\-]+\.){1,}[a-z]{2,}$/i.test(oForm.Email.value.trim())) {
				showError("La direccion de correo que has introducido es incorrecta, por favor intentalo de nuevo.");
				markEls(oForm.ctEmail);
				return false;
			}
			if (/@(yahoo|hotmail|gmail|excite|aol)\.com$/i.test(oForm.Email.value.trim())) {
				showError("Por favor, utiliza el correo de tu empresa.");
				markEls(oForm.ctEmail);
				return false;
			}
		}
	}
	return true;
}

function getLabel(oInp) {
	var oFind=oInp.parentNode.previousSibling;
	while (oFind.nodeType!=1) oFind=oFind.previousSibling;
	
	oFind=oFind.firstChild;
	while (oFind.nodeType!=1) oFind=oFind.firstChild;
	return oFind;
}

function showError(sErr) {
	var oMsg=document.getElementById("oMsg");
	oMsg.style.color="maroon";
	oMsg.innerHTML=sErr;
}

function markEls() {
	for (var iEl=0;iEl<arguments.length;iEl++) {
		var oEl=arguments[iEl];
		getLabel(oEl).style.color="#d00000";
		oEl.style.backgroundColor="#eae2d8";
		oEl.onfocus=removeMark;
	}
}

function removeMark() {
	this.style.backgroundColor="";
	getLabel(this).style.color="";
	this.onfocus=null;
}

String.prototype.trim=function () { return this.replace(/^\s+|\s+$/g,""); }