String.prototype.trim = function() { return this.replace(/^[\s]+|[\s]+$/g,'') }
String.prototype.numOfCharacters = function(character){
	var rep = 0;
	for(var i=0; i<=this.length-1;i++) if(this.charAt(i) == character) rep++;
	return rep;
}

var e = {
	addEvent : function(obj, evType, fn, useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		}else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		}else {
			return false;
		}
	}
}

var fixes={
	setLinkPrint:function(){	
		var obj = document.getElementById("pageOptions");
		var firstChild = obj.getElementsByTagName("li")[0];
		var liElement = document.createElement("li");
		var aElement = document.createElement("a");
		aElement.href = "javascript:window.print()";
		aElement.appendChild(document.createTextNode("Imprimir"))
		liElement.appendChild(aElement);
		liElement.className = "print";
		obj.insertBefore(liElement,firstChild);		
	},
	calendar:{
		init:function(){
			var obj = $("#calendar");
			obj.find(".sel").each(function(){
				var mes = $(this).attr("id");
				if(mes == "enero" || mes == "febrero" || mes == "marzo" || mes == "abril" || mes == "mayo" || mes == "junio"){
					$(this).append(fixes.calendar.createUp())
					
				}else{
					$(this).append(fixes.calendar.createDown())
				}
			})
		},
		createUp:function(){
			var span = $("<span class='fixUp sp'>&nbsp;</span>")
			return span;
		},
		createDown:function(){
			var span = $("<span class='fixDown sp'>&nbsp;</span>")
			return span;			
		}
	}
}

var curves={
	
	createMainCurves:function(){
		var obj = document.getElementById("wrapperContent");
		var obj2 = document.getElementById("wrapperFooter");
		var cTopLeft = cTopRight = cBottomLeft = cBottomRight = null;			
		cBottomLeft = curves.createElementsCurves("cLeftBottom");
		cBottomRight = curves.createElementsCurves("cRightBottom");
		obj2.appendChild(cBottomLeft);
		obj2.appendChild(cBottomRight);
	},
	createElementsCurves:function(style){
		var element = document.createElement("div");
		element.className = style + " sp";
		element.appendChild(document.createTextNode(" "));
		return element;
	}
}



var behaviours = {
	
}

/* validaciones de formularios */
var formsValidations = {
	setMsgError:function(txt, form){
		var parentForm = form.parent();
		var msgError = parentForm.find(".msgError");
		var divElement = (msgError.length != 0) ? msgError.eq(0) : document.createElement("div");		
		var ulElement = document.createElement("ul");
		var liElement = null;		
		var errors = txt.split("|");				
		$(divElement).attr("class", "msgError");
		if($(divElement).find("ul").length != 0) $(divElement).empty();
		for(var i = 0; i < errors.length - 1; i++){
			liElement = document.createElement("li");
			liElement.appendChild(document.createTextNode(errors[i]));
			ulElement.appendChild(liElement);
		}
		$(divElement).append(ulElement);		
		if(msgError.length == 0) form.before($(divElement));
	},
	validaContactForm:function(obj){
		var errorTxt = "";
		var f = $(obj);
		if(!f.find("input[@name='nombre']").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input[@name='apellidos']").attr("value")) errorTxt += literal["contact"][1];
		if(!f.find("input[@name='mail']").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input[@name='mail']").attr("value")))  errorTxt += literal["contact"][3];
		}				
		if(!f.find("textarea[@name='motivo']").val()) errorTxt += literal["contact"][4];		
		else{
			if(f.find("textarea[@name='motivo']").val().length > 250)  errorTxt += literal["contact"][5];
		}
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else return true;
	}
}

/* expresiones regulares para validar formularios */
var regularExpressions = {	
	isValidEmail:function (str){
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		return (filter.test(str));
	},
	esCadena:function(c) { return /^[0-9A-Za-z-\/Ññ?É?ÓÚáéíóúÜüÄäËë?ïÖö´,'/\\t\n\r\s]+$/.test(c); },
	esAlfabetico:function(c){return /^([a-zA-Z])+$/.test(c);},
	esNumero:function(c){return /^[0-9]+$/.test(c);},
	esTelefono:function(c){return /^[0-9\s\+\-)(]+$/.test(c)}	
}


jQuery(document).ready(function() {	
	curves.createMainCurves();		
	if($("#formContact").length != 0) $("#formContact").submit(function(){return formsValidations.validaContactForm($(this))});
	if($("#calendar").length != 0) fixes.calendar.init();
});	

