var country=new country();
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function () { 
	// business request URL Key
	if ($('#businessReqID').val() > 0) {
		busunessURLKey();
	}
        var countryLocID = $('#locCountryID').val();
        var state = $('#statehidValue').val(); 
        listStates(countryID, state);
//	if (countryLocID) {
//       listCities(countryLocID, 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();
	//listCities(countryID, city);
	//listing page
	// business locations page
	
	if ($('#businessAction').val() != 'Edit') {
		$('#businessName').blur(function () {
			busunessURLKey();
		});
	}
	$('#adNewBusinessLocation').hide();
	$('#showAddNewBusinessLocation').click(function () {
		$('#adNewBusinessLocation').show();
		findAddress("US", '');
		$('#businessLocID').val('');
	});
	$(document).on("click","#changePassword", function () {
		$("#changePassword").hide();
		$("#passwordNotify").show();
		$("#paswdChanged").val('1');
		$("#businessPassword").prop("readonly", "");
		$("#businessPassword").removeClass("disabled");
	});
	if ($("#paswdChanged").val() != '0') {
		$("#changePassword").hide();
		$("#passwordNotify").show();
		$("#businessPassword").prop("readonly", "");
		$("#businessPassword").removeClass("disabled");
	}
	$("ul li:first-child").addClass("first-one");
	$("ul li:last-child").addClass("last-one");
	// active/inactive btn
	//check box validation to update status
	$(".update_btn_table").click(function () {
		var ListAllCheckedCount = $(".ListAll:checked").size();
		var businessStatus = $('#businessStatusList').val();
		if ($(".ListAll:checked").size() == 0) {
			jAlert(msgSelectItem);
			return false;
		} else if (businessStatus == "") {
			jAlert(lblSelectAction);
			return false;
		}
		if (businessStatus == 'Deleted') {
			jConfirm(msgBusinessDelConfirmation, 'Alert', function (r) {
				if (r == true) {
					$('#formBusiness').submit();
				}
			});
		} else {
			return true;
		}
		return false;
	});
	//check box validation to update status
	$("#submitSearch").click(function () { 
			if ($('#businessTypeID').val() == "" && jQuery.trim($('#businessName').val()) == "" && jQuery.trim($('#city').val()) == "" && jQuery.trim($('#locProvince').val()) == "" && $('#countryID').val() == "") {
			jAlert(msgSelectSearchCriteria);
			return false;
		}
		return true;
	});
        $("#editAgree").click(function () {
            $("#showAgreeSection").toggle(this.checked);
        });
	//initialise fancybox
	$("#ListStatusLinkCheckbox").prop('checked', false);
	$('.side_wrapper ul li a[rel=business]').addClass('selected');
	if($("#editAgree:checked").length>0)
	$("#showAgreeSection").show();
});
function getCityHiddenValue(val) {
	$('#cityhidValue').val(val);
}
function listCities(countryID, cityName) {
 
        if(countryID == $('#countryhidValue').val())
        {
            $('#locProvince').val($('#cityhidname').val());
        }else{
            $('#locProvince').val('');
        }
	 $('#cityhidValue').val(cityName);
        
	if (countryID != '') {
               $("#cityProvinceInput").hide();
          	$.ajax({
			async: false,
			type: "POST",
			url: GLOBAL_BASE_PATH + "/city/ajaxCity/",
			beforeSend: function () {
				$("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
			},
			data: {
				"countryID": countryID,
				"cityName": cityName
			}
		}).done(function (msg) {
			$("#loadProcessImage").empty();
			$(".choosen").chosen();
			if (msg != '') {
				$("#cityInput").show();
				$('#locCity').empty();
				$("#cityProvinceInput").hide();
                                $("#locCity").append('<option value="">Select</option>' + msg);
				$('#locCity').trigger("liszt:updated");
				$("#citySelectedType").val("city");
				$("#locProvince").val('');
                                $("#locProvince").attr('name','');
				$("#div_err_frmBusiness_provincehidden").css("display", "none");
			} else {
                                
				$("#cityInput").hide();
				$("#cityProvinceInput").show();
                                $("#locProvince").attr('name','locCity');
				$("#locCity").val('');
                                $("#citySelectedType").val("province");
				$("#div_err_frmBusiness_statehidden").css("display", "none");
			}
		});
	}
}
function listStatesSearch(countryID, stateID) {
	if (countryID) {
		$("#loaderInput").append('<span ><img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  /></span>');
	}
	$.ajax({
		type: "POST",
		url: GLOBAL_BASE_PATH + "/ajax/statesAjax/",
		data: "countryID=" + countryID + "&stateID=" + stateID,
		success: function (msg) {
			$('#stateInput').html(msg);
			$("#loaderInput").empty();
		}
	});
}
function makeEditable(id) {
	$('.view_' + id).hide()
	$('.edit_' + id).show()
	$('.disedit' + id).hide()
	$("#locationSave").hide();
	$('#stateInput').html('');
	$(".disedit").each(function () {
		var curid = $(this).attr('id');
		$("#" + curid).hide();
	})
}
function showToolTip(businessID, section) {
	$("#" + section + "_tooltip_" + businessID).show();
}
function hideToolTip(businessID, section) {
	$("#" + section + "_tooltip_" + businessID).hide();
}
function busunessURLKey() {
	var businessName = $("#businessName").val();
	var businessKey = $.trim(businessName).replace(/[^a-zA-Z0-9]+/g, "-").toLowerCase();
	$("#businessKey").val(businessKey);
}
$("[task=delete]").click(function () {
	var ID = $(this).attr('businessid');
	jConfirm(confirmation, 'Alert', function (r) {
		if (r == true) {
			$.ajax({
				type: "POST",
				url: GLOBAL_BASE_PATH + "/business/destroy/",
				data: "businessID=" + ID,
				success: function (msg) {
					location.reload();
				}
			});
		}
	});
});
$(".status-active,.status-inactive").click(function () {
	var obj = $(this);
	var data = new Object();
	data.businessID = $(this).attr('businessID');
	if ($(this).hasClass('status-active')) {
		data.newClass = 'status-inactive icon-set';
		data.businesstatus = 'Inactive';
		$(this).closest('tr').find('td:first').find('input').removeClass('ListActive').addClass('ListInactive');
	} else if ($(this).hasClass('status-inactive')) {
		data.newClass = 'status-active icon-set';
		data.businesstatus = 'Active';
		$(this).closest('tr').find('td:first').find('input:checkbox').removeClass('ListInactive').addClass('ListActive');
	}
	$.ajax({
		type: "POST",
		url: GLOBAL_BASE_PATH + "/business/ajaxchangestatus/",
		data: data,
	}).done(function (msg) {
	//if (msg.trim() == "") {
			jNotifyShow(statusChange, "common_message", "message_success");
			obj.attr('class', data.newClass + ' hide-txt');
			//obj.attr('title', data.charitystatus);
		//}
	});
});
$(function () {
	$(".add-link").hover(function () {
		$(this).parent().find(".tooltip").css("visibility", "visible");
	}, function () {
		$(this).parent().find(".tooltip").css("visibility", "hidden");
	});
});
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();
                $('#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");
                }
            });
        }
    }
}