var country=new country();
$(document).ready(function () {
    $('.main_menu li a[rel=business]').addClass('active');
    $('#businessName').focus();
    listCity();
    $('#businessName').blur(function () {
        if ($('#businessName').val() != '' && $('#businessKey').val() == '') {
            busunessURLKey();
        }
    }); 
    $("#locCountryID").change(function () {
        listCity();
    });
    var countryLocID = $('#locCountryID').val();
        var state = $('#statehidValue').val(); 
        listStates(countryID, state);
         if (countryLocID) {
            listStates(countryLocID, state);
         }
         	if($('#countryID').length >0){
			var countryID = $('#countryID').val();
		}else{
	// business add page          
		var countryID = $('#locCountryID').val();
	}
	var city = $('#cityhidValue').val();
});
function getStateHiddenValue(val) {
    $('#statehidValue').val(val);
}

function listCity() {
    var dataArray = new Array();
    dataArray[0] = $("#locCountryID").val();
    dataArray[1] = $("#citySelectedID").val();
    if (dataArray[0] != '') {
            $.ajax({
                url: GLOBAL_BASE_PATH + "/ajax/ajaxCity/",
                type: "POST",
                cache: false,
                async: false,
                global: false,
                data: {
                    ajax_request: dataArray
                },
                success: function (data) {
                    //country objects are declared in common.plugin.js file in(common javascript folder)
                    //country.saveState(dataArray[0], $.trim(data));
                    $("#div_err_formBusinessProfile_locCity").addClass("hide");
                    $("#div_err_formBusinessProfile_locProvince").addClass("hide");
					$(".choosen").chosen();
                   if (dataArray[0] != '') {
                        if (data != '') {
                            $("#locCity").empty();
                            $("#locCity").append('<option value="">' + lblSelect + '</option>' + data);
							$('#locCity').trigger("liszt:updated");
                            $("#spanCity").show();
                            $("#spanCityProvince").hide();
                            $("#locProvince").attr('name','');
                            $("#citySelectedType").val("city");
                            $("#locProvince").val("");
                        } else {
                            $("#spanCity").hide();
                            $("#spanCityProvince").show();
                            $("#locProvince").attr('name','locCity');
                            $("#citySelectedType").val("province");
                        }
                    } 
					else {
                        $("#locCity").empty();
                        $("#locCity").append('<option value="">' + lblSelect + '</option>');
                    }
                }
            });
    }
}
function busunessURLKey() {
    var businessName = $("#businessName").val();
    var businessKey = $.trim(businessName).replace(/[^a-zA-Z0-9]+/g, "-").toLowerCase();
    $("#businessKey").val(businessKey);
}
function getLangLong() {
    if ($('#frmLat').val() != '' && $('#frmLon').val() != '') {
        var myLatlng = new google.maps.LatLng($('#frmLat').val(), $('#frmLon').val());
        myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var loc = new google.maps.LatLng($('#frmLat').val(), $('#frmLon').val());
        map.setCenter(loc);
        var marker = new google.maps.Marker({
            map: map,
            position: loc
        });
        marker.setDraggable(true);
        google.maps.event.addListener(marker, 'dragend', function (event) {
            // Pan to this position (doesn't work!)
            var point = marker.getPosition();
            //map.panTo (marker.getPosition());
            map.panTo(point);
            document.getElementById("frmLat").value = point.lat();
            document.getElementById("frmLon").value = point.lng();
        });
    } else {
        return false;
    }
}
$("#businessPassword").blur(function () {
    $("#businessPasswordnew").trigger("blur")
});
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();
                $('#locStateID').empty();
                $("#stateProvinceInput").hide();
                $("#locStateID").append('<option value="">Select</option>' + msg);
                $("#stateSelectedType").val("state");
                $("#locProvince").val('');
                $("#div_err_frmBusiness_provincehidden").css("display", "none");
            } else {
                $("#stateInput").hide();
                $("#stateProvinceInput").show();
                $("#locStateID").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();
                    $('#locStateID').empty();
                    $("#stateProvinceInput").hide();
                    $("#locStateID").append('<option value="">Select</option>' + msg);
                    $("#stateSelectedType").val("state");
                    $("#locProvince").val('');
                    $("#div_err_frmBusiness_provincehidden").css("display", "none");
                } else {
                    $("#stateInput").hide();
                    $("#stateProvinceInput").show();
                    $("#locStateID").val('');
                    $("#stateSelectedType").val("province");
                    $("#div_err_frmBusiness_statehidden").css("display", "none");
                }
            });
        }
    }
}