// JavaScript Document

function toggle_visibility(id) { // toogles a dinamyc item bettew css display visible/hidden 
       var e = document.getElementById(id);
       if(e.style.overflow == 'visible')
          e.style.overflow = 'hidden';
       else
          e.style.overflow = 'visible';
    }

//Field onfocus handling 
function clean(thefield){ //Any input textfield onfocus cleanup
		if(thefield.defaultValue==thefield.value) {
		thefield.value = "";
		}
	}

function fill(thefield){ //Any input textfield onfocus fill with its default value
    if (thefield.value == "")
	thefield.value=thefield.defaultValue
//	document.getElementById("helppopup").style.display='none';
	}
	
function showfield(thefield){ //Change textfield into Password hiding css

	
	    if(thefield == 'passwordtext'){ 
		    document.getElementById("a1").style.display='none'; 
			document.getElementById("a2").style.display='';
			document.getElementById('password').focus();
		}
		
		else if(thefield == 'passwordgo'){ 
		    document.getElementById("a1").style.display='none'; 
			document.getElementById("a2").style.display='';
			document.getElementById('passwordgo').focus();
		}
		
		else if(thefield == 'passwordconfirmtext'){ 
		    document.getElementById("b1").style.display='none'; 
			document.getElementById("b2").style.display='';
			document.getElementById('passwordconfirm').focus();
		}
	
	}
	
function hidefield(thefield){ //Change Password into Textdfield in case they are empty

		 
	if(thefield == 'password'){ 
	      if(document.getElementById('password').value==''){
			  document.getElementById('a1').style.display='';
	    	  document.getElementById('a2').style.display='none';	
			  document.getElementById("passwordhint").style.display='none';
			  }
	   }
	   else if(thefield == 'passwordfinal'){ 
	      if(document.getElementById('passwordfinal').value==''){
			  document.getElementById('a1').style.display='';
	    	  document.getElementById('a2').style.display='none';	
			  }
	   }
	   
	   else if(thefield == 'passwordconfirm'){ 
	      if(document.getElementById('passwordconfirm').value==''){
			  document.getElementById('b1').style.display='';
	    	  document.getElementById('b2').style.display='none';	
			  document.getElementById("passwordconfirmhint").style.display='none';
			  }
	   }
    
    }
	
// help Minipopup
function popup(entidad,grupo,errores){


	
	var i,j;
	
	
	for (j = 0 ; j < errores.length ; j++){
	
		if ((document.getElementById(errores[j]) !==null) && (document.getElementById(errores[j]).style.display=='block')  ) {
			for (i = 0 ; i < grupo.length ; i++){
				document.getElementById(grupo[i]).style.display='none';	
			}
			return true;
	}
	}
		for (i = 0 ; i < grupo.length ; i++){
		
		if (grupo[i]==(entidad+'hint')) {
			
			document.getElementById(entidad+'hint').style.display='block';
			}
		
		else {
		
			document.getElementById(grupo[i]).style.display='none';		
		}
		}
		
}


/**
 * 
 * @param form object
 * @param values array
 * @param values_error array
 * @param username string
 * @return Boolean 
 */
function validate(form,values,values_error,username) {
	popup('alias',values,values_error);
	if ((trim(form.username.value).length >5) &&(validar_caracteres(form.username.value)) &&(form.username.value !=username)){
		if (validatepass(form.password.value,form.passwordconfirm.value)) {
			if (is_valid_email(form.email.value)) {
				if (daysInMoth(form.birthYear.value,form.birthDay.value,form.birthMonth.value)) {
					if ((form.gender[0].checked) || (form.gender[1].checked)) {
						if (form.conditions.checked) return true;
						else {
							document.getElementById('termserror').style.display='block';
							popup('alias',values,values_error);
							return false;
						}
						
					} else {
						document.getElementById('gendererror').style.display='block';
						popup('alias',values,values_error);
						
						return false;		
					}
						
				} 
				else {
					document.getElementById('birtherror').style.display='block';
					popup('alias',values,values_error);
					form.birthDay.focus();
					return false;		
				}
			}
			else {
				document.getElementById('emailerror').style.display='block';
				popup('alias',values,values_error);
				form.email.focus();
				return false;	
				
			}
		}
		else {
			document.getElementById('passworderror').style.display='block';
			popup('alias',values,values_error);
			form.password.focus();
			return false;
		}
	}
	else {
		document.getElementById('usernameerror').style.display='block';
		popup('alias',values,values_error);
	form.username.focus();
	return false;
	}
	

		
	


	
	
	 
}



//Validate mail





//help functions for date validation

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
	
function daysInFebruary(year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
   return  (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function daysInMoth(year,day,month) {

	if ((isInteger(year))  && (isInteger(day)) && (isInteger(month))) {
	switch (month)
	{
	case '04':
	case '06':
	case '09':
	case '11':

		days=30;
		break;
	case '02':
		days =daysInFebruary(year);
		break;
	default:
		days=31;
	}
	
if (day<=days) return true;
else return false
	} else return false;	
	
	
}


// Validamos email y  bloqueamos ciertos dominios 

function is_valid_email(email)
{
fakes=new Array ("mailinator","sogetthis",'mailin8r','spam','thisisnotmyrealemail','test','example');

for (i=0;i<fakes.length;i++){
   if (email.indexOf(fakes[i]) !=-1) return false 
}
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);

}

function validatepass(pass,pass2) {

	if (((trim(pass).length >=5) || (trim(pass2).length >=5))&& (pass==pass2) && (validar_caracteres(pass)) &&(validar_caracteres(pass2)))  return true
	else  return false
}



//Comprobacion de caracteres validos
function validar_caracteres(data) {
	
	return /^([a-zñçáéíóúàèìòùäëïüA-ZÇÑÀÈÌÒÙÄËÏÖÜ0-9_\.\-])+$/.test(data);	
	
}


function trim(str){
	 var start = -1,
	  end = str.length;
	 while( str.charCodeAt(--end) < 33 );
	 while( str.charCodeAt(++start) < 33 );
	 return str.slice( start, end + 1 );
	};



	
	
/*	
	
	document.observe('dom:loaded', function() {
		 
		$('remember_pass').observe('click', showRememberForm);
	$('form').className = 'login';

	});
	
	
	
function mola(event){
	
	alert('he cambiado');
	
	
}
*/		
	function showRememberForm(event) {
		
		Event.stop(event);
		
		Lightview.show({
			href: '/sites/www.zatbingo.com/es/password.php',
			rel: 'ajax',
			options: {
				topclose: true,
				width: 570,
				height:180,
				ajax: {
					onComplete: function(){
						$('passform').observe('submit', submitRememberForm);
					}
				}
			}
		});
	}

	function submitRememberForm(event) {
		// block default form submit
		Event.stop(event);
			  
		Lightview.show({
			href: '/sites/www.zatbingo.com/es/password2.php',
			rel: 'ajax',
			options: {
				title: 'results',
				menubar: false,
				topclose: true,
				autosize: true,
				width: 500,
				
				ajax: {
					parameters: Form.serialize('passform')
				}
			}
		});
	}
	
	
	/*
	 * form de support
	 */
	
	
	function support(form) {
		
		var j,message
		
		message=new Array('msg01','msg02','msg03','msg04','msg05','msg06','msg08');
		
		for (j = 0 ; j < message.length ; j++){
			
			if ((document.getElementById(message[j]) !==null) && (document.getElementById(message[j]).style.display=='block')  ) {
				
					document.getElementById(message[j]).style.display='none';	
				
				
		}
		}
		
		
		
		
		
		if ((trim(form.name.value).length >3)) {
			 if (is_valid_email(form.email.value)) {
				 if (isInteger(form.to.value)) {
					 if ((trim(form.subject.value).length >9)) {
					
						 if ((trim(form.message.value).length >20) && (form.message.vale !==form.def.value)) {
					return true;	 
						 
					 } else {
						 document.getElementById('msg05').style.display='block';
							document.getElementById('message').className='full error';  
						 
					 }
					 }
					 else {
						 document.getElementById('msg04').style.display='block';
							document.getElementById('subject').className='textbox full error'; 
						 return false
					 }
					 
				 } else {
					 document.getElementById('msg03').style.display='block';
					return false; 
				 }
			
			 
			 
			 } else {
				 document.getElementById('msg02').style.display='block';
					document.getElementById('email').className='textbox full error'; 
				 return false
			 }
		
	} else {
		
		document.getElementById('msg01').style.display='block';
		document.getElementById('name').className='textbox full error';
		return false
	}
	
	}
	
	
document.observe("lightview:loaded", function() { //lanzamos el popup OnSubmit
  $("login").observe('submit', function() {
    //Lightview.show({ href: 'anotherElementId', rel: 'inline' });
	  Lightview.show({
    href: '#waitwhileload',
    rel: 'inline',
    options: {
      autosize: false,
      topclose: false,
	  closeButton:false,
	  width:500,
	  height:200
      }
  });

  });
});





