var country=new country();
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function () {
	/*making password as mandatory for user registration*/
	if(!$("#userID").val()){ 
		$(".add-user").addClass('mandatory');
	}
	/*making password as mandatory for user registration*/
    var countryID = $('#countryID').val();
    var city = $('#cityhidValue').val();
    var countryLocID = $('#countryID').val();
    var state = $('#statehidValue').val(); 
    listStates(countryID, state);
    listCities(countryID, city);
     if (countryLocID) {
            listStates(countryLocID, state);
         }
	    //business list 
		if($('#countryID').length >0){
			var countryID = $('#countryID').val();
		}else{
	// business add page          
		var countryID = $('#locCountryID').val();
	}
	var city = $('#cityhidValue').val();
    // list city
    //change password
    $(document).on("click","#changePassword",function () {
        $("#changePassword").hide();
        $("#paswdChanged").val(1);
        $("#passwordNotify").show();
        $("#userNewPassword").prop("readonly", "");
        $("#userNewPassword").removeClass("disabled");
    });
    if ($("#paswdChanged").val() != '0') {
        $("#changePassword").hide();
        $("#passwordNotify").show();
        $("#userNewPassword").prop("readonly", "");
        $("#userNewPassword").removeClass("disabled");
    }
});
//ajax state populating
function listCities(countryID, userCity) {
    if (countryID) {
        $("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
    }
    $('#cityhidValue').val(userCity);
    $.ajax({
        type: "POST",
        url: GLOBAL_BASE_PATH + "/user/citiesAjax/",
        data: "countryID=" + countryID + "&userCity=" + userCity,
        success: function (msg) {
            $("#loadProcessImage").empty();
			$(".choosen").chosen();
            if (msg != '') {
                $("#cityInput").show();
                $('#userCity').empty();
                $("#cityProvinceInput").hide();
                $("#userCity").append('<option value="">Select</option>' + msg);
				$('#userCity').trigger("liszt:updated");
                $("#citySelectedType").val("city");
                $('#userProvince').val('');
                $("#div_err_formUserSignup_provincehidden").css("display", "none");
            } else {
                $("#cityInput").hide();
                $("#cityProvinceInput").show();
                $("#citySelectedType").val("province");
                $('#userCity').val(0);
                $("#div_err_formUserSignup_statehidden").css("display", "none");
            }
        }
    });
}
function getCityHiddenValue(val) {
    $('#cityhidValue').val(val);
}
//focus on first element
$.fn.focus_first = function () {
    var elem = $('input:visible', this).get(0);
    var select = $('select:visible', this).get(0);
    if (select && elem) {
        if (select.offsetTop < elem.offsetTop) {
            elem = select;
        }
    }
    if (elem) {
        elem.focus();
    }
    return this;
}
function getStateHiddenValue(val) {
    $('#statehidValue').val(val);
}
function listStates(countryID, stateID) {
    $('#statehidValue').val(stateID);
	$("#stateProvinceInput").hide();
    if (countryID != '') {
        if (country.getState(countryID) != 'no_data') {
            //country objects are declared in common.plugin.js file in(common javascript folder)
            var msg = country.getState(countryID);
            if (msg != '') {
                $("#stateInput").show();
                $('#stateID').empty();
                $("#stateProvinceInput").hide();
                $("#stateID").append('<option value="">Select</option>' + msg);
                $("#stateSelectedType").val("state");
                $("#locProvince").val('');
                $("#div_err_frmBusiness_provincehidden").css("display", "none");
            } else {
                $("#stateInput").hide();
                $("#stateProvinceInput").show();
                $("#stateID").val('');
                $("#stateSelectedType").val("province");
                $("#div_err_frmBusiness_statehidden").css("display", "none");
            }
        } else {
            $.ajax({
                async: false,
                type: "POST",
                url: GLOBAL_BASE_PATH + "/city/ajaxState/",
                beforeSend: function () {
                    $("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
                },
                data: {
                    "countryID": countryID,
                    "stateID": stateID
                }
            }).done(function (msg) {
                country.saveState(countryID, $.trim(msg));
                $("#loadProcessImage").empty();
                if (msg != '') {
                    $("#stateInput").show();
                    $('#stateID').empty();
                    $("#stateProvinceInput").hide();
                    $("#stateID").append('<option value="">Select</option>' + msg);
                    $("#stateSelectedType").val("state");
                    $("#locProvince").val('');
                    $("#div_err_frmBusiness_provincehidden").css("display", "none");
                } else {
                    $("#stateInput").hide();
                    $("#stateProvinceInput").show();
                    $("#stateID").val('');
                    $("#stateSelectedType").val("province");
                    $("#div_err_frmBusiness_statehidden").css("display", "none");
                }
            });
        }
    }
}