/*
signUp.js 
JS for the sign up form.
Service for the signup formâ it points to /services/citizen-profile/create-account 
 * */

//			onload
$(function(){
	// sign up
	if($("#signup").length > 0){
		$("#signup").signUpForm();
	}
        
        if($.fn.organizationDisplayLogic)
            $.fn.organizationDisplayLogic();
	
	
	// fancybox popups
        if($.fn.fancybox){
            $("#moreInfoCitizens").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $("#moreInfoPoliticians").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $("#moreInfoOrganizations").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $("#voteiqHomeLearnMorePage1Button").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $(".voteiqHomeLearnMorePage1Link").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $(".voteiqHomeLearnMorePage2Link").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $(".voteiqHomeLearnMorePage3Link").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
            $(".voteiqHomeLearnMorePage4Link").fancybox({
		titleShow:true,
                    'scrolling':'no'
            });
        }//end if
});

// decide to show gender radio buttuns based on the entity type
$.fn.organizationDisplayLogic = function(){
	$("#entity_type").change(function(){
		// check the value of the entity type select box
		if($('#entity_type').val() == 3) { // 3 = organization
    		// hide gender
    		$(".gender").hide();
    		$("label[for=lastName]:first").closest('li').hide();
    		$("label[for=firstName]:first").text('Organization');
    		$("label[for=month]:first").closest('li').closest('ol').closest('fieldset').hide();
    		
    		var element = $("#signup").get(0);
    		$.removeData(element, 'validator');
    		
    		delete createAcctOptions.rules.lastName;
    		$("#signup").validate(createAcctOptions);
		} else {
			// show gender, first name, last name, birth date
			$(".gender").show();
			$("label[for=lastName]:first").closest('li').show();
			$("label[for=firstName]:first").text('First Name');
			$("label[for=month]:first").closest('li').closest('ol').closest('fieldset').show();
			
    		var element = $("#signup").get(0);
    		$.removeData(element, 'validator');
    		
			createAcctOptions.rules.lastName = 'required';
			$("#signup").validate(createAcctOptions);
		}
	});
};

//			signUpForm
$.fn.signUpForm = function(){
	var options = {
		trigger : [ 'none', 'none' ],
		beforeSubmit : showRequest, // pre-submit callback
		success : showResponse,
		url : '/ajax/citizen-profile/create-account' // override for form's
	};

	// pre-submit callback
	function showRequest(formData, jqForm, options) {
		var queryString = $.param(formData);
		if($('#signup').validate().form()){
			$('#signup').mask('Your account is being created...');
			return true;
		}else{
			return false;
		}
	}
	
	// post-submit callback
	function showResponse(responseText, statusText, xhr, $form) {
		var response = eval('(' + responseText + ')');

		if(response.success) {
			//window.location = '/citizen/profile';
			window.location = response.url;
			return;
		}else{
			$('#signup').unmask();
			$('#messageBox .error').html(response.message).slideDown();
		}	
	}
	
	// set the form to be an ajax form
	return this.each(function() {
		$(this).ajaxForm(options);
	});

};
