/* $Id: landing.js,v 1.20 2011/05/16 21:47:17 vanessah Exp $ */


function callback(){
	FB.Facebook.apiClient.users_hasAppPermission('email', function(has_perms) {
	    if(has_perms) {
	       window.location.href = '/facebook/';
	    } else {
			FB.Connect.showPermissionDialog("email", function(perms) { window.location.href = '/facebook/'; });
	    }
	});
}
var ie  = /*@cc_on!@*/false;



var SignupCountry = {
	setCountry: function (country){
		$('#country').val(country);
		return country;
	},
	getTowns: function(){
	        var country = $("#country-select").val();
			country = SignupCountry.setCountry(country);
            data = {data:{} };
            data.action = 'zip-valid';
            data['data[zip]'] = $("#country-zip").val();
            data['data[country]'] = country;
            data.random = Math.random() * 100;
            $('#continue-button').trigger('disableCountryForm');
    		$.get(
    			'/async/requests/validation.php',
    			data,
    			function(data,status,request)
    			{
    				
    				$('#town-select').empty();
					if(data.valid == 0)
					{
			            $("#warning-message").show();
			            $("#warning-message").html(data.message);
                    } 
                    else
                    { 
                     $("#warning-message").html('');
                     $("#warning-message").hide();
		    			if( data.towns) 
		    			{
			    			$('#continue-button').trigger('enableCountryForm');
			            	var opt = false;
			            	
			            	for(var t in data.towns)
			            	{
			            		opt = new Option(data.towns[t],t);
			            		if (ie) 
			            		{
			            			$('#town-select')[0].add(opt);
			            		}
			            		else 
			            		{
			                		$('#town-select')[0].add(opt,null);
			            		}
			            			
			            	}
			            }
			    	}
    			},'json');
	}
};

$(document).ready(function(){
    initFB();
    //-----------| gender |------------//
	$("#continueButton").click(function(){
        var validSeeking = $('#seeking').val() == 'both' ||  $('#seeking').val() == 'men' ||  $('#seeking').val() == 'women';
        var validGender =  $('#gender').val() == 'M' ||  $('#gender').val() == 'F';        
 
        var minAge = $('#age-min').val();
        if(!minAge || minAge == ''){
        	minAge = '18';
        }
        
        var maxAge = $('#age-max').val();
        if(!maxAge || maxAge == ''){
        	maxAge = '99';
        }
        
        var postalCode = $('#zip').val();
        var validPostalCode = !( (!postalCode) || (postalCode == 'Postal') || (postalCode == 'Zip Code') ||
        						( $('#country').val() == 3 && ( $('#town').val() == '' || $('#town').val() == 0 ) ) );
        
        if(validSeeking && validGender && validPostalCode){
            $("#entryForm").submit();
        }else{
	       	if(!validSeeking){
	            $("#warning-message").html("To continue registering please select what you are looking for.");
	        } else if(!validGender){
	            $("#warning-message").html("To continue registering please choose a gender.");
	        }else if(!validPostalCode){
	            $("#warning-message").html("Please enter a Zip/Postal Code");
	        }
        	$("#country-hide").hide();
            $("#recaptcha_widget").show();
            $("#warning-message").show();
        }
        return false;
	});	
	
	$('#close_btn').click(function(){
        $('#recaptcha_widget').hide();
        return false;
    });
    $('#continue-button').click(function(){
    	if( $('#continue-button').data('disabled') ){
    		return false;
    	}
        var newCountry = $('#country-select').val();
        
        if(newCountry != ''){
			SignupCountry.setCountry(newCountry);
        	if( newCountry == 3){
	    		$('#zip').val($('#country-zip').val());
	    		$('#town').val($('#town-select').val());
	    	}else if(newCountry == 1){
	    		$('#town').val(0);
	    		$('#zip').val('');
	    		$("#zip").defaultvalue("Zip Code");
	    	}else {
	    		$('#town').val(0);
	    		$('#zip').val('');
	    		$("#zip").defaultvalue("Postal");
	    	}
        	
        }else{
        	$('#country').val(newCountry);
        	$("#zip").defaultvalue("Zip Code");
        }    
        $('#recaptcha_widget').hide();        
    });
    $('#country-zip').blur(function(){
    	if($('#country-select').val() == 3){
			SignupCountry.getTowns();
    	}
    });
    if( $('#country-select').val() == 3){
    		if($('town-select').val()){
	    		$('#continue-button').trigger('enableCountryForm');
    		} else {
    			$('#continue-button').trigger('disableCountryForm');
    		}
    		$('#country-zip').show();
    		$('#town-select').show();
    		$('#label-country-zip').show();
    		$('#label-town-select').show();
    		
    }
    $('#country-select').change(function(){
    	$("#warning-message").html('');
        $("#warning-message").hide();
    	if( $(this).val() == 3){
    		if($('town-select').val()){
	    		$('#continue-button').trigger('enableCountryForm');
    		} else {
    			$('#continue-button').trigger('disableCountryForm');
    		}
    		$('#country-zip').show();
    		$('#town-select').show();
    		$('#label-country-zip').show();
    		$('#label-town-select').show();
    		
    	} else {
    		$('#continue-button').trigger('enableCountryForm');
    		$('#country-zip').hide();
    		$('#town-select').hide();
    		$('#label-country-zip').hide();
    		$('#label-town-select').hide();
    	}
    });
    //-------------| age |-------------//
    
    var agemin = $("#age-min").val();
	if(agemin == ''){
		$("#age-min").defaultvalue("18");
	}
	$('#age-min').blur(function() {
		var agemin = $("#age-min").val();
		if(agemin == ''){
			$("#age-min").defaultvalue("18");
		} else if( isNaN(agemin) ){
			$("#age-min").val( '18' );
		} else if($('#age-max').val() < agemin ){
			$("#age-min").val( $('#age-max').val() );
		} else if( agemin <18){
			$("#age-min").val('18');
		}		
	});
	var agemax = $("#age-max").val();
	if(agemax == ''){
		$("#age-max").defaultvalue("99");
	}
	$('#age-max').blur(function() {
		var agemax = $("#age-max").val();
		if(agemax == ''){
			$("#age-max").defaultvalue("99");
		}else if( isNaN(agemax) ){
			$("#age-max").val( '99' );
		}else if($('#age-min').val() > agemax ){
			$("#age-max").val( $('#age-min').val() );
		} else if( agemax >99){
			$("#age-max").val('99');
		}
	});
	
	//-------------| zip |-------------//
    var zip = $("#zip").val();
	if(zip == ''){
		$("#zip").defaultvalue("Zip Code");
	} else if( $("#country").val()== 3 ){
		SignupCountry.getTowns();
	}
	$('#zip').blur(function() {
		var zip = $("#zip").val();
		if(zip == ''){
            if( $("#country").val() != 1 ){
				$("#zip").defaultvalue("Postal");
			} else {
				$("#zip").defaultvalue("Zip Code");
			}
		}
	});
	
	$("#country-show").click(function(){
    	$("#country-hide").show();
    	$("#recaptcha_widget").show();
    	$("#warning-message").hide();
    	return false;
    });
    
   $('#continue-button').bind('disableCountryForm',function(){
   	 $('#continue-button').data('disabled',true);
     $('#continue-button').css('background-image',$('#continue-button').css('background-image').replace('continue-button.png','continue-button-disabled.png') );
   });
   $('#continue-button').bind('enableCountryForm',function(){
   	 $('#continue-button').data('disabled',false);
     $('#continue-button').css('background-image',$('#continue-button').css('background-image').replace('continue-button-disabled.png','continue-button.png') );
   });
   
   if( errorMessage ){
   		$("#country-hide").hide();
	    $("#recaptcha_widget").show();
	    $("#warning-message").show();
	    $("#warning-message").html(errorMessage);
   }
});



