// 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 false;
	}
	}
		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';		
		}
		}
	return true;	
}


/**
 * 
 * @param form object
 * @param values array
 * @param values_error array
 * @param username string
 * @return Boolean 
 */
function validate(form,values,values_error,username) {
	var j;
	
	for (j = 0 ; j < values_error.length ; j++){

		if (document.getElementById(values_error[j]) !==null) { 
			document.getElementById(values_error[j]).style.display='none' ;  
		}
	}
	
	for (j = 0 ; j < values.length ; j++){

		if (document.getElementById(values[j]) !==null) { 
			document.getElementById(values[j]).style.display='none' ;  
		}
	}
	
	if ((trim(form.username.value).length >4) &&(validar_caracteres(form.username.value)) &&(form.username.value !=username)){
		if (validatepass(form.password.value,form.passwordconfirm.value,form.username.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('termerror').style.display='block';
							
							return false;
						}
						
					} else {
						document.getElementById('gendererror').style.display='block';
						
						
						return false;		
					}
						
				} 
				else {
					document.getElementById('birtherror').style.display='block';
				
					form.birthDay.focus();
					return false;		
				}
			}
			else {
				document.getElementById('emailerror').style.display='block';
		
				form.email.focus();
				return false;	
				
			}
		}
		else {
			
			
			form.password.focus();
			return false;
		}
	}
	else {
	
		document.getElementById('usernameerror').style.display='block';
	
	form.username.focus();
	return false;
	}
	

		
	


	
	
	 
}









//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) {
var days;
	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)
{
var fakes,i,domain;
fakes=new Array ("mailinator","sogetthis",'mailin8r','spam','thisisnotmyrealemail','example','yopmail','trashmail','10x9.com');


	if( /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)) {
		
	domain= email.substr(email.indexOf('@'));	
		
		for (i=0;i<fakes.length;i++){
			   if (domain.indexOf(fakes[i]) !=-1) return false 
			}
		return true;
		
	}
	else {
	return false;	
	}

	
	
}

function validatepass(pass,pass2,username) {
if (trim(pass)==trim(username)) {
	document.getElementById('userpass').style.display='block';	
	return false;
	
} else {
	if (trim(pass).length>=5) {
		if (pass==pass2) {
			if 	(validar_caracteres(pass)) {
				return true;
			}
			else {
			//Pass caracteres no validos	
				document.getElementById('passworderror').style.display='block';	
				return false;
			}
		} else {
		//PASS NO SON IGUALES	
			document.getElementById('repeatpassworderror').style.display='block';	
			return false;
		}
	}
	else {
		//Pass corto
		document.getElementById('passworderror').style.display='block';	
		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 );
	};


