function validateForm(idAgeGroup16,idAgeGroup18,mandatoryMsg,responsibleFormFilledMsg){
      var result = true;
      var ageGroupSelected = $('#ageGroup option:selected').val();
      
	  $('input:visible').each(function(){
		  
	      if(result) 
	      if($(this).attr('id').indexOf('responsible') != -1){
	    	  
	          if(ageGroupSelected == idAgeGroup16){
	              if($(this).attr('id') == 'responsibleFormFilled' ){
	                  if(!$(this).attr('checked')){
	                     confi = confirm(responsibleFormFilledMsg);
	                     if(confi){
	                        $(this).attr('checked','checked');
	                     }else{
	                        result = false;
	                     }
	                  }
	              }else if($(this).val() == null || $(this).val() == ''){
	                  alert(mandatoryMsg);
	                  result = false;
	              }
	          }
	      }else if($(this).attr('id') != 'acceptBox'){
	          if($(this).val() == null || $(this).val() == ''){
	              alert(mandatoryMsg);
	              result = false;
	          }
	      }
	   });
	  
   if(result)
	  $('select:visible').each(function(){
		  var fieldSelectedValue = $('#'+$(this).attr('id')+' option:selected').val();
	      if(result) 
	      if($(this).attr('id').indexOf('responsible') != -1){
	          if(ageGroupSelected == idAgeGroup16){
	              if(fieldSelectedValue == null || fieldSelectedValue == ''){
	                  alert(mandatoryMsg);
	                  result = false;
	              }
	          }
	      }else{
	          if(fieldSelectedValue == null || fieldSelectedValue == ''){
	              if($(this).attr('id') == 'bd_day' || $(this).attr('id') == 'bd_month' || $(this).attr('id') == 'bd_year'){
	                  if(ageGroupSelected == idAgeGroup16 || ageGroupSelected == idAgeGroup18){
	                     alert(mandatoryMsg);
	                     result = false;
	                  }
	              }else{
	                  alert(mandatoryMsg);
	                  result = false;
	              }
	          }
	      }
	   });
   
  return result;
}
function validateAges(minAge,maxAge,day,month,year,message){

   if (maxAge < 1) maxAge = 200 ;
   
   var birthDate = new Date(year, month-1, day);
   var birthTime = birthDate.getTime() ;
   var dateNow = new Date() ;

   var now = dateNow.getTime();
   var dif = now - birthTime ;   
   var years = dif / ( 1000*60*60*24*365 ) ;

   var ok = years >= minAge && years < (maxAge+1) ;
   
   if (!ok) {
	   alert(message) ;
   }
   
   return ok ;
}
function validateAgesOld(minAge,maxAge,day,month,year,message){
   
   var maxDate = currentDate.getFullYear()-minAge;
   var minDate = currentDate.getFullYear()-maxAge;
   var birthDate = parseInt(''+year+month+day);
   
   maxDate = maxDate +''+(currentDate.getMonth() + 1> 9 ? currentDate.getMonth()+1 : '0'+(currentDate.getMonth()+1));
   maxDate = maxDate +''+(currentDate.getDate() > 9 ? currentDate.getDate() : '0'+currentDate.getDate());
   maxDate = parseInt(maxDate);
   
   minDate = minDate +''+(currentDate.getMonth() + 1> 9 ? currentDate.getMonth()+1 : '0'+(currentDate.getMonth()+1));
   minDate = minDate +''+(currentDate.getDate() > 9 ? currentDate.getDate() : '0'+currentDate.getDate());
   minDate = parseInt(minDate);
   
   var ok = true;
   
   if(minAge != 0){
      ok = !(birthDate > maxDate);
   }
   
   if(maxAge != 0 && ok){
      ok = !(birthDate < minDate);
   }
   
   if(!ok){
      alert(message);
      return false;
   }
   return true;
   
}

function validateAge(responsible,idAgeGroupL16,idAgeGroupL18,idAgeGroupM18){
  
  var currentDate = new Date();
  var validated = true;
  if(responsible){
	 var responsibleDay = $('#responsible_bd_day option:selected').val();
	 var responsibleMonth = $('#responsible_bd_month option:selected').val();
	 var responsibleYear =  $('#responsible_bd_year option:selected').val();
	 
     if(responsibleDay != '' && responsibleMonth != '' && responsibleYear != ''){
        validated = validateAges(18,0,responsibleDay,responsibleMonth,responsibleYear,'O respons�vel deve ser maior de idade!');
     }
  }else{
	 var day = $('#bd_day option:selected').val();
	 var month = $('#bd_month option:selected').val();
	 var year = $('#bd_year option:selected').val();
	 var ageGroupSelected = $('#ageGroup option:selected').val();
	  
     if(day != '' && month != '' && year != ''){
        if(ageGroupSelected == idAgeGroupL16){
           validated = validateAges(0,15,day,month,year,'A data de nascimento deve ser de acordo com a faixa et�ria!');
        }else if(ageGroupSelected == idAgeGroupL18){
           validated = validateAges(16,17,day,month,year,'A data de nascimento deve ser de acordo com a faixa et�ria!');
        }else if(ageGroupSelected == idAgeGroupM18){
           validated = validateAges(18,0,day,month,year,'A data de nascimento deve ser de acordo com a faixa et�ria!');
        }
    }
  }
  return validated;
}

function validateAgeGroup(idAgeGroupL16,idAgeGroupL18,idAgeGroupM18) {
	
	var ageGroupSelected = $('#ageGroup option:selected').val();
	
	if (ageGroupSelected == idAgeGroupL16 || ageGroupSelected == idAgeGroupL18) {
		$('#bdate').attr('class', 'style14');
	} else {
		$('#bdate').attr('class', 'style11');
	}
	if (ageGroupSelected == idAgeGroupL16) {
		$('#responsibleDiv').show();
		$('#responsibleMsg').show();
		$('#permitedGames').hide();
		$('#tr_bdate').show();
	} else if (ageGroupSelected == idAgeGroupL18) {
		$('#tr_bdate').show();
		$('#permitedGames').show();
		$('#responsibleDiv').hide();
		$('#responsibleMsg').hide();
	} else {
		$('#tr_bdate').hide();
		$('#permitedGames').hide();
		$('#responsibleDiv').hide();
		$('#responsibleMsg').hide();
	}
	validateAge(false,idAgeGroupL16,idAgeGroupL18,idAgeGroupM18);
	
}

// valid characters
// 2, 3, 4, 5, 6, 7, 8, 9
// A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Y, Z
// Format: UUUU-UPPP-XXDD
function promoCodeMask(v) {
	
	v = v.toUpperCase();
	v = v.replace(/[^23456789ABCDEFGHJKLMNPQRSTUVWXYZ]/g,"");
	v = v.replace(/(\w{4})(\w)/,"$1-$2");
	v = v.replace(/(\w{4})(\w)/,"$1-$2");
	return v;
}

function mascara(o,f){
	v_obj=o;
	v_fun=f;
	setTimeout("execmascara()",1);
}

function execmascara(){
	v_obj.value=v_fun(v_obj.value);
}

function checkCode(){
	
    code = $('#promotionalCode').val();
    nicke = $('#nick').val();
    
    $.get("/ajax/checkPromoCode.jsp",
    	{ promoCode: code, nick: nicke },
		function(data) {
    
			if(data.indexOf('<ok>')!=-1) {
				$('#codeConfirm').attr('style','color: blue');
				$("#nextPageButtonDiv").show();
				$("#validateButtonDiv").hide();
			} else { 
				$('#codeConfirm').attr('style','color: red');
			}
			
			$("#codeConfirm").show().text(data.replace('<ok>',''));
			
    	} );
    
}

function checkPass() {
	
	var pass = $('#pass').val();
	var nicke = $('#nick').val();
	if(nicke == "" || pass == "") {
		return;
	}
	
	var bd_day = $('#bd_day').val();
	var bd_month = $('#bd_month').val();
	var bd_year = $('#bd_year').val();
	
	var bdate = "";
	
	if( bd_day != "" && bd_month != "" && bd_year != "" ) {
		bdate = bd_day + bd_month + bd_year;
	}
	
	$.get("/ajax/check_pass.jsp",
        { nick: nicke, password: pass, birthdate: bdate },
    	function( data ) {
        	
        	if(data.indexOf('<ok>')!=-1) {
        		$('#passValidate').attr('class','txtGreenBold');
    		} else { 
    			$('#passValidate').attr('class','txtRed');
    		}
    		
    		$("#passValidate").show().text(data.replace('<ok>',''));
   	} );
	
}

function checkNickCharacters(obj) {
	var ch = obj.value;
	ch = ch.replace(/[^a-zA-Z0-9]/g,'');
	obj.value = ch;
}

