/**
 * This script contains the JS code used from the edit_profile action (view) of the Users controller.
 */

$(document).ready(function() {
	
	// Autocomplete
	autoready = 0;
	
    // Get the states for the selected country.
    getStateByCountry();
    
    // Get the city for the selected zipcode.
    getCityByZipcode();

    // Call the function to show/hide the company-name field.
    showHideCompanyField();

    // On change of the country, get the states for the selected country.
    $('#UserCountryId').change(function() {
    	$("#UserCity").val("");
    	$("#UserZipcode").val("");
    	getStateByCountry();
    });
    
    $('#UserCity').bind( 'keypress', function() {
    	//return false;
    });

    // On changing of member-type field, show/hide the company-name field.
    $('#UserMemberType').change(function () {
        showHideCompanyField();
    });
});


function log(event, data, formatted) {
	$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
}

/**
 * This function is used to get all the states for the passed country-id (AJAX).
 */
function getStateByCountry() 
{
    // Store the country-id to a var.
    var country_id = $('#UserCountryId').val();

    // If country-id is not set then return false.
    if ('' == country_id) {
        return false;
    }

    $('#UserStateId').attr('disabled', true);

    // Send an AJAX request to get all the states.
    $.get(base + "states/get_states_by_country/" + country_id, function(response)
    {
        // Append the response to the div.
        $("#allStates").html('');
        $("#allStates").html(response);
        
        $('#StateStateId').attr('name', "data[User][state_id]");
        $('#StateStateId').attr('id', 'UserStateId');

        // Enable the state drop-down.
        $('#UserStateId').attr('disabled', false);
        $('#UserStateId').val(stateId);
    });    
    
    
	getCityByZipcode();
}

	function formatItem(row) {
		return row[0] + " " + row[1] + "";
	}
	function formatResult(row) {
		
		//$("#UserCity").val(row[1].replace(/(<.+?>)/gi, ''));
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	function getCity(code) {
			$("#UserCity").val(code);
	}

/**
 * This function is used to get city for the zipcode select (AJAX).
 */
function getCityByZipcode() 
{
    // Store the country-id to a var.
   	var country_id = $('#UserCountryId').val();

	if( autoready == 0 ) {
		$("#UserZipcode").autocomplete( base + "zipcodes/get_city_by_zipcode/" + country_id + "/", {
			width: 390,
			multiple: false,
			matchContains: true,
			mustMatch: true,
			cacheLength: 1,
			formatItem: formatItem,
			formatResult: formatResult
		});	
		
		autoready = 1;	
	} else {
		$("#UserZipcode").setOptions({ url: base + "zipcodes/get_city_by_zipcode/" + country_id + "/" });	
	}

    // If country-id is not set then return false.
    if ( 208 != country_id) {
	   	$('#UserCity').unbind( 'keypress' );
		$("#UserZipcode").setOptions({ mustMatch: false });
    } else {    
	    $('#UserCity').bind( 'keypress', function() {
	    	//return false;
	    });
	   	$("#UserZipcode").setOptions({ mustMatch: true });
    }

    return false;

}
 

/**
 * This function is used to show-hide the companay-name field, as per the selected type of the member.
 */
function showHideCompanyField()
{
    // If user-member-type field is set then display the company-name if the 'Company' type is selected
    if ($('#UserMemberType') && 'Private' == $('#UserMemberType').val()) {
        $('#company_name').hide();
    } else {
        // Otherwise hide the field.
        $('#company_name').show();
    }
}

