//Valida los datos utilizados por el usuario
function validate(to_validate){
	Array_to_validate = to_validate.split(",");
	var name = "";
	var required = "";
	var field_type = "";
	var Errors = "";
	
	for(var i=0; i<Array_to_validate.length; i=i+4){
		name = Array_to_validate[i];
		required = Array_to_validate[i+1];
		field_type = Array_to_validate[i+2];
		field_name = Array_to_validate[i+3];
				
		if(document.getElementById(field_name) == null) continue;
			
		if(required == 1 && (isEmpty(document.getElementById(field_name))
							||  (
								document.getElementById(field_name).type=="select-one" 
								&& document.getElementById(field_name).value==0
								)								 
				   		     )
		   ){
			Errors = Errors + name + " és obligatori\n";						
		}
		
		if(field_type == "int" && !isNumber(document.getElementById(field_name))){
			Errors = Errors + name + " ha de ser un número\n";			
		}else if(field_type == "email" && !isEmail(document.getElementById(field_name))){
			Errors = Errors + name + " no és un email correcte\n";			
		}
	}
	
	if(Errors != ""){
		alert("S'han trobat els següents errors:\n--------------------------------------\n\n" + Errors);
		return false;
	}else{
		return true;
	}
	
}

//Valida si el valor del objeto es un n£mero
function isNumber(obj){
	 return !isNaN(obj.value);	
}

//Valida si el valor del objeto es un  email
function isEmail(obj){
	var s = obj.value;
	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))	return true;
	else return false;
}

//Valida que el objeto est  vacio
function isEmpty(obj){
	return obj.value.replace(/ /g, '')=="";
}

function verCapa(layer){
	if(document.getElementById(layer).style.display==""){
		document.getElementById(layer).style.display='none';
	}else{
		document.getElementById(layer).style.display="";
	}
}

function CalPop(sInputName, idioma){
	window.open('includes/mini_calendario.asp?idioma=' + idioma, 'CalPop', 'toolbar=0,width=225,height=225');
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}else {
		countfield.value = maxlimit - field.value.length;
	}
}

function enviarFecha(fecha){
	var parent = window.opener.document;
	parent.getElementById('reserva_fecha').value = fecha;
	window.close();	
}

//window.onload = function()
function carga(){		
	var myHeight = new Array();
	var divs_ap = document.getElementsByClassName('ap');
	var divs_con = document.getElementsByClassName('con');	
	var myDivs = document.getElementsByClassName('supcon');
	var myLinks = document.getElementsByClassName('supap');
		
	var myAccordion = new fx.Accordion(myLinks, myDivs, {opacity: false});
			
	
	for(i=0;i<myLinks.length;i++){
		var id_ap = myLinks[i].id.substring(8,myLinks[i].id.length);			
		myHeight[id_ap] = new fx.Height(myDivs[i].id);			
		$(myLinks[i].id).onclick = function() {
			myHeight[this.id.substring(8,this.id.length)].toggle();				
		};
		myHeight[id_ap].toggle();
	}	
	
	//myAccordion.showThisHideOpen(myDivs[0]);
}//;

function posicRatonXY(e) {
	if (IE) { posicX = event.clientX + document.body.scrollLeft;
	posicY = event.clientY + document.body.scrollTop}
	else { posicX = e.pageX; posicY = e.pageY };
	if (posicX < 0) {posicX = 0};
	if (posicY < 0) {posicY = 0};
}

function hiddeLayer(){
	var capa = document.getElementById("AJUDA");
	if (capa == null) return;
	capa.style.visibility = 'hidden';	
}

function showLayer(){
	var capa = document.getElementById("AJUDA");
	if (capa == null) return;
	capa.style.visibility = 'visible';
	capa.style.left = (x-250)+'px';
	capa.style.top = (y+12)+'px';
}

function showPrivacidad(){
	var capa = document.getElementById("privacidad");
	if (capa == null) return;
	capa.style.visibility = 'visible';
	capa.style.left = (x-250)+'px';
	capa.style.top = (y+12)+'px';
}

function hiddePrivacidad(){
	var capa = document.getElementById("privacidad");
	if (capa == null) return;
	capa.style.visibility = 'hidden';	
}


function mouseMoveHandler (evt) {
	x = ( typeof(evt.pageX) == 'number' ) ? evt.pageX : 
				( evt.clientX + (document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft) );
	y = ( typeof(evt.pageY) == 'number' ) ? evt.pageY : 
				( evt.clientY + (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop) );	
}


if( document.layers && document.captureEvents && Event.MOUSEMOVE ) {
	document.captureEvents(Event.MOUSEMOVE);
}


if(document.addEventListener) {
	document.addEventListener('mousemove', mouseMoveHandler, false);
} else if( document.attachEvent ) {
	document.attachEvent('onmousemove', mouseMoveHandler);
} else {
	document.onmousemove = mouseMoveHandler;
}

var x = 0;
var y = 0;



