function updateState(countryId, formId) {
	var statePath = "#" + formId + " .state";
	var cityPath = "#" + formId + " .city";
	
	displayMiddleName = (countryId == '804' || countryId == '643') ? "" : "none"; //Check is it UKRAINE OR RUSSIA
	$( "#" + formId + " .middle_name-row" ).css("display", displayMiddleName);
	
	displayMiddleName = (countryId == '804' || countryId == '643') ? "block" : "none"; //Check is it UKRAINE OR RUSSIA
	$( "#" + formId + " dd#middle_name-element ,  #" + formId + " dt#middle_name-label" ).css("display", displayMiddleName);
	
	displayMiddleName = (countryId == '804' || countryId == '643') ? "inline" : "none"; //Check is it UKRAINE OR RUSSIA
	$( "#" + formId + " tr.registration_state td.right-td span" ).css("display", displayMiddleName);
	
	boldPersonalTaxId = (countryId == '152' || countryId == '76') ? "bold" : ""; //Check is it Chile OR Brazil
	
	$( "#" + formId + " .PersonaltaxIdElement .optional" ).removeClass('bold');
	$("#" + formId + " .PersonaltaxIdElement .boldPersonalTaxId").remove();

	
	$(statePath).attr("disabled", true);
	$(cityPath).attr("disabled", true);
	url = typeof(language)  == 'undefined' ? "/locale/states?reg_country=" + countryId : language + "/locale/states?reg_country=" + countryId;
	$.get(url + '&noHelpers=true', function(data) {
		if ( data.indexOf('<option') > 0 ) {
			$(statePath).replaceWith("<select id=\"state_id\" name=\"state_id\" class=\"state\"  onchange=\"updateCity(this.value,'"+formId+"')\" ></select>");
			$(statePath).html(data);
            $("#state-label label, #state_id-label label").removeClass("optional");
            $("#state-label label, #state_id-label label").addClass("required");
            $("#state-label label").addClass("bold");
            if($("#" + formId + " #state-label span").html() == ""){
            	$("#" + formId + " #state-label span").html("*");
            };
            $("#" + formId + " .StarDecoratorForState").show();
		} else {
			$(statePath).replaceWith("<input id=\"state\"  name=\"state\" class=\"state\" />");
            $("#state-label label, #state_id-label label").removeClass("required");
            $("#state-label label, #state_id-label label").addClass("optional");
            $("#state-label label").removeClass("bold");
            $("#state-label span").html("");
            $("#" + formId + " .StarDecoratorForState").hide();
		} 
		$(cityPath).replaceWith("<input id=\"city\" name=\"city\" class=\"city\" />");
		$(cityPath).attr("disabled", false);
	});
}

function updateCity(stateId, formId){	
	
	var ButCalcWrapper = $('.bottom-orange-wrapper');
	var ButCalc = $('#bottom .text').parent();
	
	var cityPath = "#" + formId + " .city";
	$(cityPath).attr("disabled", true);
	url = "/locale/cities?reg_state=" + stateId + "&reg_country=" + $("#" + formId + " .country").val();
	url = typeof(language)  == 'undefined' ? url : language + url;
	$.get(url + '&noHelpers=true', function(data) {
		if ( (data.substring(data.indexOf("</option>")+7, data.length-1)).indexOf("<option") > 0 ) {
                        ButCalcWrapper.css({'display':'block'})
                        ButCalc.addClass('bottom-orange-disable')
			$(cityPath).parent().css({"display":"block"});
			$(cityPath).replaceWith("<select id=\"city_id\" name=\"city_id\" class=\"city\"></select>");
			$(cityPath).html(data);
		} else {
			
			ButCalcWrapper.css({'display':'none'})
			ButCalc.removeClass('bottom-orange-disable')
			
			if ($("#estimate_shiping_popup").length) {
				$(cityPath).html(data);
				$(cityPath).css("disabled") = true;
			}
			else {
				$(cityPath).replaceWith("<input id=\"city\" name=\"city\" class=\"city\" />");
			}
		}
	});
}

function updateState2(countryId, formId) {
	var ButCalcWrapper = $('.bottom-orange-wrapper');
	var ButCalc = $('#bottom .text').parent();
	var statePath = "#" + formId + " .state";
	var cityPath = "#" + formId + " .city";
	$(cityPath).html('');
	$(cityPath).parent().css({"display":"none"});
	//updateCity2("", formId);
	url = typeof(language)  == 'undefined' ? "/locale/states?reg_country=" + countryId + "&formId=" + formId : language + "/locale/states?reg_country=" + countryId + "&formId=" + formId;
	$.get(url, function(data) {
		if ( data.indexOf('<option') > 0 ) {
			ButCalcWrapper.css({'display':'block'})
			ButCalc.addClass('bottom-orange-disable')
			$(statePath).parent().css({"display":"block"});
			$(statePath).html(data);
			//$(statePath).attr("onchange", "updateCity(this.value,'"+formId+"')");
            $("#state-label label, #state_id-label label").removeClass("optional");
            $("#state-label label, #state_id-label label").addClass("required");
            $("#state-label label").addClass("bold");
            if($("#state-label span").html() == ""){
            	$("#state-label span").html("*");
            };
            $("#" + formId + " .StarDecoratorForState").show();
            $("#bottom #form").css({"height":"92px"});
            $(".row2").css({"margin-top":"3px"});
		} else {
			ButCalcWrapper.css({'display':'none'})
			ButCalc.removeClass('bottom-orange-disable')
			$(statePath).html('');
			$(statePath).parent().css({"display":"none"});
			$("#bottom #form").css({"height":"46px"});
			$(".row2").css({"margin-top":"0"});
		} 
		$('div.hint, ul.errors, div.ok', $(statePath).parent()).remove();
	});
}

function SelectCity() {
	var ButCalcWrapper = $('.bottom-orange-wrapper');
	var ButCalc = $('#bottom .text').parent();
	ButCalc.removeClass('bottom-orange-disable')
	ButCalcWrapper.css({'display':'none'})
	
}
