// ==============================================================
// UNOBTRUSIVE TOGGLE SECTIONS SCRIPT: TOGGLE v1.0
// (c)2004 Sergi Meseguer http://zigotica.com/ under CC license:
// http://creativecommons.org/licenses/by-sa/2.0/
// ==============================================================
// Requires EXTRAS namespace (extras.js in this folder)
// Just set htmlclass to element to be hidden/shown and 
// it'll make a link from the previous element
// ==============================================================

TOGGLE = {
	// edit text to append before linkable element
	showtxt : "",
	hidetxt : "",
	// edit html class name
	htmlclass : "toggle",
	
	// you should not need to edit past this point
	// ======================================================
	start : function(){ 
		var togglers = EXTRAS.getElementsByClass(TOGGLE.htmlclass);
		for (var i = 0; i < togglers.length; i++) {
			var box = togglers[i].previousSibling;
			//fix for Moz and line break/tab, etc:
			if(typeof box.innerHTML != "string") box = box.previousSibling; 
			var cont = togglers[i];
			//avoids currentStyle/computedStyle:
			cont.style.display = "block"; 
			TOGGLE.prepare(box,cont);
		}
	},
	
	prepare : function(box,cont){ 
		var origtxt = box.innerHTML;
		cont.style.display = "none";
		box.innerHTML = TOGGLE.showtxt+origtxt;
		box.onclick = function(){
			TOGGLE.run(box,cont,origtxt);
		}
		// fix for IE5.x cursor property:
		if(navigator.appVersion.indexOf("MSIE 5") > -1 && !window.opera) box.style.cursor = "hand";
		else box.style.cursor = "pointer";
	},
	
	run : function(box,cont,origtxt) { 
		if(cont.style.display == "block") {
			cont.style.display = "none";
			box.innerHTML = TOGGLE.showtxt+origtxt;
		}
		else {
			cont.style.display = "block";
			box.innerHTML = TOGGLE.hidetxt+origtxt;
		}
	}
}

if (document.getElementsByTagName) EXTRAS.addEvent(window, 'load', TOGGLE.start, false);
/*function distoggle(){
	if (document.getElementsByTagName) window.onload = TOGGLE.start;
}*/


// ==============================================================
// ACCESSIBLE VALIDATION FORM SCRIPT: VALID v2.0 
// (c)2004 Sergi Meseguer http://zigotica.com/ under CC license:
// http://creativecommons.org/licenses/by-sa/2.0/
// Requires EXTRAS namespace (extras.js in this folder)
// ==============================================================
// Strongly based on the excellent article by Simon Willison at
// http://www.sitepoint.com/article/simple-tricks-usable-forms/
VALID = {
	
	compulsorytitle : " es un campo obligatorio",
	optionaltitle : " es un campo opcional",
	compulsory : "rellene todos los campos obligatorios",
	goodemail : "se requiere un correo válido: nombre@dominio.ext\nPor favor corrija el formato",
	goodphone : "se requiere un teléfono válido: solo se permiten números\nPor favor corrija el formato",

	start : function() {
		var areas = document.getElementsByTagName('textarea');
		for (var a = 0; (area = areas[a]); a++) {
			EXTRAS.addEvent(area, 'blur', VALID.bluring, false);
		}
		var inputs = document.getElementsByTagName('input');
		for (var a = 0; (input = inputs[a]); a++) {
			if(input.type == "text") {
				EXTRAS.addEvent(input, 'blur', VALID.bluring, false);			
			}
		}
		var forms = document.getElementsByTagName('form');
		for (var a = 0; (form = forms[a]); a++) {
			form.onsubmit = VALID.checkForm;
		}
		var labels = document.getElementsByTagName('label');
		for (var i = 0; i < labels.length; i++) {
			label =  labels[i];
			valor = label.innerHTML;
			valor = valor.replace(/\(\*\)/i, ""); 
			valor = valor.replace(/:/i, ""); 
			if (label.className.indexOf("required") != -1) {
				label.title = valor + VALID.compulsorytitle;
			}
			else {
				label.title = valor + VALID.optionaltitle;
			}
		}
	},

	checkForm : function(e) {
		// hacking IE events:
		var e = (e) ? e : window.event ;
		var target = (e.target) ? e.target : e.srcElement ; 

		var remaining = [];
		var requireds = EXTRAS.getElementsByClass('required problem');
		for (var i = 0; i < requireds.length; i++) {
			valor = requireds[i].innerHTML;
			valor = valor.replace(/: \(\*\)/i, ""); 
			remaining.push("\r\n"+valor);
			VALID.checkfield(document.getElementById(requireds[i].htmlFor));
		}
		var requireds = EXTRAS.getElementsByClass('required');
		for (var i = 0; i < requireds.length; i++) {
			valor = requireds[i].innerHTML;
			valor = valor.replace(/: \(\*\)/i, ""); 
			remaining.push("\r\n"+valor);
			//alert(requireds[i].htmlFor)
			VALID.checkfield(document.getElementById(requireds[i].htmlFor));
		}
		
		if(remaining.length > 0) {
			alert(VALID.compulsory+":\n" + remaining);
			return false; // stop submission!
		}
		else {
			//alert("formulario completo");
			return true; //reemplazar por return true;
			//return true; // do submit
		}
	},

	checkfield : function(target) {
		var label;
		var labels = document.getElementsByTagName('label');
		for (var i = 0; i < labels.length; i++) {
			if (labels[i].htmlFor == target.id) {
				label =  labels[i];
			}
		}
		if (target.value.length == 0 && label.className.indexOf("required") != -1) {
			label.className = 'required problem';
		} 
		else if (target.value.length != 0 && label.className.indexOf("required") != -1) {
			label.className = 'required completed';
		}
		else if (target.value.length != 0 && label.className.indexOf("required") == -1) {
			label.className = 'completed';
		}
		else if (target.value.length == 0 && label.className.indexOf("required") == -1) {
			label.className = '';
		}
		
    		// Regular Expression matching http://
    		var urlRegExp = /http:\/\//i;
    		if(target.id == "url" && target.value.length != 0 && !urlRegExp.test(target.value)) {
			target.value = "http://"+target.value;
	    	}
    		// Regular Expression matching phone number
    		var phoneRegExp = /[a-zA-Z_]/i;
    		if(target.id == "phone" && target.value.length != 0 && phoneRegExp.test(target.value)) {
			target.value = target.value.replace(/[a-zA-Z_]/gi,"-");
			alert(VALID.goodphone);
	    	}
	    	
    		// Regular Expression matching from
		// http://regexlib.com/UserPatterns.aspx?authorId=1758
    		var emailRegExp = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/i;
	    	if(target.id == "email" && target.value.length != 0 && emailRegExp.test(target.value)) {
			if (label.className.indexOf("required") != -1) {
				label.className = 'required completed';
			} 
			else {
				label.className = 'completed';
			}
	    	}
	    	else if(target.id == "email" && target.value.length != 0 && !emailRegExp.test(target.value)) {
			if (label.className.indexOf("required") != -1) {
				label.className = 'required problem';
			} 
	    		else {
	    			label.className = 'problem';
	    		}
	    		alert(VALID.goodemail);
	    	}
	},

	bluring : function(e) {
		// hacking IE events:
		var e = (e) ? e : window.event ;
		var target = (e.target) ? e.target : e.srcElement ; 

		VALID.checkfield(target);
	}
	
}


if(document.getElementsByTagName) EXTRAS.addEvent(window, 'load', VALID.start, false);
