/**
 * Constants
*/
var regExp = $H({"GENERIC": /^(\s+)?$/, //can't be empty or contain only whitespaces
				"domain": /^([a-z]+(-\w+)*\.)+[a-z]+(-\w+)*$/, //RFC1034
				"ip_v4": /\d{1,3}(\.\d{1,3}){3}$/,
				"ip_v6": /^\d{4}(:\d{4}){8}$/,
				"mail": /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\0-9=?A-Z^_a-z{|}~])*@(([a-z]+(-\w+)*\.)+[a-z]+(-\w+)*|\[\d{1,3}(\.\d{1,3}){3}\]|\[IPv6:\d{4}(\:\d{4}){7}])$/, //check RFC2822
				"tel": /^[1-9]\d+$/, //phone and area codes must be numbers, not beginning with 0
				"age": /^\d{1,2}$/ });

var hbox = $H({"obj": document.createElement("div"),
				"style": document.createElement("link"),
				"stack": Array(),
				"display": function(b,obj){
					this["obj"].style.display = b?"block":"none";
					if(obj) Position.clone(obj,this["obj"],{setWidth: false, setHeight: false})
				},
				"key": function(k){
					for(i in hbox["stack"]) if(k==i) return true;
					return false;
				}
			});
hbox["obj"].setAttribute("id","hintbox");
//Event.observe(hbox,'mouseover',function(evt){ clearTimeout(timeOutID); timeOutID = 0; });
//Event.observe(hbox,'mouseout',function(evt){ timeOutID = setTimeout('unHint()',MAX_TIME); });
hbox["style"].setAttribute("rel","stylesheet");
hbox["style"].setAttribute("type","text/css");
hbox["style"].setAttribute("href","/css/hintbox");

/**
 * Non-obtrusive popup
 * @param width popup width in px
 * @param height popup height in px
*/
function popup(width,height){
	var features = (typeof width != 'number' || typeof height != 'number')?"":
					'location=0,statusbar=0,menubar=0,scrollbars=1,width='+width+',height='+height;
	return function(evt){
		var theWindow = window.open(this.getAttribute('href'),'_blank',features);
		theWindow.focus();
		Event.stop(evt);
	}
}

Event.observe(window,"load",function(){
	$$('a.external').each(function(i){Event.observe(i,"click",popup(600,400));});
	$$('a.popup').each(function(i){Event.observe(i,"click",popup());});
	var head = document.getElementsByTagName("head");
	head = $(head[0]);
	head.appendChild(hbox["style"]);
	document.body.appendChild(hbox["obj"]);
	hbox['obj'] = $('hintbox');
	Event.observe(window,"click",function(){hbox.display(false);});
	var forms = document.getElementsByTagName("form");
	forms = $A(forms);
	if(forms.length > 0){
		forms.each(function(i){
			i.getElementsByClassName("mandatory").each(function(j){
				Event.observe(j,"mouseover",function(evt){
					var type = $(this.getAttribute("for")).getAttribute("class");
					new Ajax.Request('/AJAX/message', 
						{method: 'post',
							parameters: {'name': type},
							onLoading: function(){
								hbox['obj'].update("<p><strong>Cargando...</strong></p>");
							},
							onSuccess: function(transport){
								hbox['obj'].update(transport.responseText);
							},
							onComplete: function(){
								Element.getElementsByClassName(hbox['obj'],'external').each(function(k){
									Event.observe(k,"click",popup(600,400));
								});
							}
						}
					);
					hbox.display(true,$(this.getAttribute("for")));
				});
			});
			/*Event.observe(j,"mouseout",function(evt){ hbox.display(false); });*/
			Event.observe(i,"submit",function(evt){
				var error = false;
				this.getElementsByClassName("name").each(function(j){
					if(regExp['GENERIC'].test(j.value)) error = true;
				});
				this.getElementsByClassName("mail").each(function(j){
					if(!regExp['mail'].test(j.value)) error = true;
				});
				this.getElementsByClassName("lada").each(function(j){
					if(!regExp['tel'].test(j.value)) error = true;
				});
				this.getElementsByClassName("tel").each(function(j){
					if(!regExp['tel'].test(j.value)) error = true;
				});
				this.getElementsByClassName("age").each(function(j){
					if(!regExp['age'].test(j.value)) error = true;
				});
				if(error){
					Event.stop(evt);
					new Ajax.Request('/include/message.php', 
						{method: 'post',
							parameters: {'name': 'err'},
							onLoading: function(){
								hbox['obj'].update("<p><strong>Cargando&hellip;</strong></p>");
							},
							onSuccess: function(transport){
								hbox['obj'].update(transport.responseText);
							},
							onComplete: function(){
								Element.getElementsByClassName(hbox['obj'],'external').each(function(k){
									Event.observe(k,"click",popup(600,400));
								});
							}
						}
					);
					var subm = $$("button[type=submit]");
					hbox.display(true,subm[0]);
				}
			});
		});
	}
});