$(function() {
	
    $("input[name='cmdSubmit']").click(function() {
    	var country_id = $('#country_select option:selected').val();
    	var state_v = $('#region_of_'+country_id+' option:selected').val();
        if(country_id == 'other'){
        	state_v = $('#region_of_'+country_id).val();
        } 
        $("input[name=state]").val(state_v);
    	
        $(this).attr('disabled', 'disabled');
        $('#loader').css('display', 'inline');
        $('#error').remove();
        
        
        var result = true;
        $.ajax({
          type: 'POST',
          async: false,
          url: '/createAccount.php?submit=true',
          data: $('#processRegistrationForm').serialize(),
          success: function(response) {
        	  	$("input[name=userName]").attr('disabled', 'disabled');
        	  	$("input[name=passwd]").attr('disabled', 'disabled');
                $("input[name=c_password]").attr('disabled', 'disabled'); 
                $("input[name=security_code]").attr('disabled', 'disabled');
                //$("input[name='cmdSubmit']").removeAttr('disabled');
                if(response != '') {
                    if(response.indexOf('http://') == -1) {
                        result = false;
                        $('#loader').css('display', 'none');
                        $('#processRegistrationForm').find('table').append(
                            $('<tr />').attr('id', 'error').append(
                                $('<td />').attr('colspan', '2').append(
                                    $('<span id="span_error" />').css('color', 'red').text(response)
                                )
                            )
                        );
                        $("input[name=userName]").removeAttr('disabled');
                        $("input[name=passwd]").removeAttr('disabled');
                        $("input[name=c_password]").removeAttr('disabled'); 
                        $("input[name=security_code]").removeAttr('disabled');
                        $("input[name='cmdSubmit']").removeAttr('disabled');
                    }
                    else{
                        $("input[name='redirect']").val(response);
                        $("#processRegistrationForm").attr('action',response);
                        window.location = response;
                    }
                }                
            }
        });
        
        return result;
    });
})

