$(document).ready(function(){	
	if($('#selectedCountries').val()){
		selctdCntry=$('#selectedCountries').val();		
                $("#ajax_new_countryDiv").show();
	}	
$('#submitGeozone').click(function() 
{       
   nameExists($('#geoZoneName').val());
   res =$('#countExists').val();
   if(res==1){       
        return false;        
    }else {
	var formErrorsGeoZoneName	=	$('#geoZoneName').val();
	var formErrorsGeoZoneDescription	=	$('#geoZoneDescription').val();
	if(geoZoneName=="" || formErrorsGeoZoneDescription==""){
            $("#geoZoneName").trigger('blur');
            $("#geoZoneDescription").trigger('blur');
            $("#countryCount").trigger('change');
		return false;
	} 

	if($('#selectedCountries').val()=='' || $('#selectedCountries').val()==0) {
		jAlert(lblSelectOneCountry);
	    return false;
	}	
		var selectedCountries         = $('#selectedCountries').val();
		selCountries_array=selectedCountries.split(',');
		if(selCountries_array.length>=1){
		 	for(i=0;i<selCountries_array.length;i++){
			  	if(selCountries_array[i]!=0){
				if($(".statearray_"+selCountries_array[i]+":checked").size()==0){
						  var countryName=$('#hidCountryName_'+selCountries_array[i]).val();
						  if(countryName!=''){
							  jAlert(msgAddState+'"'+countryName+'"');
							  return false;
						  }
					}
			   }
		   }
	   }
	
    }
});
	
$(document).on('blur keyup',"#geoZoneDescription",function() {
	maxLen = 200; // max number of characters allowed
    if ($("#geoZoneDescription").val().length > maxLen) {
			var msg = lblMaxCharaLength;
			jAlert(msg);
			$("#geoZoneDescription").val($("#geoZoneDescription").val().substring(0, maxLen)) ;
	}
});
$(document).on('change',"#AddCountry",function(){
            $('.div-city').hide();
            var countryID = $(this).val();
            if (countryID) {
            listStates(countryID);
            }else {
                $("#stateInput").html('');		
            }
        }); 
		
 $(document).on('click',"#GeoShowCountry",function(){
             $('.div-city').slideToggle("slow");
             $(this).find(".arw-icon").toggleClass("arw-top");
        }); 		
 $(document).on("change",'#geoZoneName',function(){
      nameExists($(this).val());
 });			
});
function nameExists(name)
{
     var geoZoneID = $('#geoZoneID').val();  
       dataArray			= new Array();
       dataArray[0] 			= geoZoneID;   
        dataArray[1] 			= name;
        if($.trim(name)!='') {  
	   $.ajax({
				   	type: "POST",
                                        async:"false",
					url: GLOBAL_BASE_PATH+"/geozone/ajaxExists/",		      		
		           	data: { ajax_request: dataArray},
				   	success: function(msg){ 
					if(msg=='1'){
                                          $('#countExists').val("1");                                         
                                          $('#div_err_formGeoZone_validateState').removeClass("hide");
                                          
                                        }else {
                                            $('#countExists').val("0");
                                        }                                      	
				}
	   });
        }else {
            $('#countExists').val(0);
            $('#div_err_formGeoZone_validateState').addClass("hide"); 
        } 
        $("#countExists").trigger('blur');
}
function selectAllState(B,A,check,countryID){    
	
        if(check.checked){
            $('#SelectAll_'+countryID).attr('title',lblDeselectAll);
            $("li[id^='left_"+B+countryID+"_']").find("input").prop('checked',true);}
        else {
            $('#SelectAll_'+countryID).attr('title',lblSelectAll);	
            $("li[id^='left_"+B+countryID+"_']").find("input").prop('checked',false);}
	
};

function checkselectAll(countryID)
{
	var total_checkboxes=$(".stateCheckbox_"+countryID+" ").size();
	var checked=$(".stateCheckbox_"+countryID+" :checked").size();
	if(total_checkboxes==checked){		
		$('#SelectAll_'+countryID).attr('title',lblDeselectAll);		
		 $('#SelectAll_'+countryID).find("input").prop('checked',true);	
	} else {
		$('#SelectAll_'+countryID).attr('title',lblSelectAll);	
                 $('#SelectAll_'+countryID).find("input").prop('checked',false);
	}
    // if(checked==0) $('#ajax_new_countryDiv').hide();
};
function checkselectAddedAll(countryID)
{
	var checked=$(".statearray_"+countryID+":checked").size();	
        if(checked==0) {
            $('#AjaxCountryExists_'+countryID).remove(); 
            $("#stateInput").html('');setCountry();
            var selectedCountries=$('#selectedCountries').val();
			selCountries_array=selectedCountries.split(',');
			countryCount	=	$('#countryCount').val();
			countryCount	=	countryCount-1;
			$('#countryCount').val(countryCount);
			var res_country_list=0;
			for(i=0;i<selCountries_array.length;i++){
				if(selCountries_array[i]!=countryID && selCountries_array[i]!=0){
					 res_country_list=res_country_list+','+selCountries_array[i];
				}
			}                      
			$('#selectedCountries').val(res_country_list);
                         if(res_country_list=='0'){
                            $('#ajax_new_countryDiv').hide();
                        }
        }
};
function addCountry(selectbox)
{       
   
	removeIcon=$('#removeIcon').val();	
	count=$('#count').val();
	var count=parseInt(count);
	count1=count+1;
	 
	countryCount	=	$('#countryCount').val();
	countryCount	=	parseInt(countryCount)+1;
	dataArray			= new Array();
	var country_id         = $('#AddCountry').val();
	 if(country_id==''){
             jAlert(errSelectCountry);
              return false;
         }
	var selectedCountries         =0;
        if($('#selectedCountries').val()){
	 	selectedCountries         = $('#selectedCountries').val();
	 } 
	 if(country_id){
		 selectedCountries		=	selectedCountries+','+country_id;
	 }
          var selectedstates =  '0';            
           $('.stateCheckbox_'+country_id+' input:checkbox:checked').each(function(){
                selectedstates		=	selectedstates+','+$(this).val();              
           });
          var stateselectedCount =    $('.stateCheckbox_'+country_id+' input:checkbox:checked').size();
          if(stateselectedCount==0   && $('#AjaxAllStates_'+country_id).val()=='0'){
              jAlert(msgAddState + $('#AddCountry').find("option:selected").text());
              return false;
          }     
        $('#selectedCountries').val(selectedCountries);
   
	 if(country_id!=''){
	   $('#cityLoc_loader').removeClass("hide");
	   dataArray[0] 			= country_id;
	   dataArray[1] 			= count;
	   dataArray[2] 			= removeIcon;	  
            dataArray[3] 			= selectedstates;          
	   $.ajax({
				   	type: "POST",
					url: GLOBAL_BASE_PATH+"/geozone/ajax/",
		      		
		           	data: { ajax_request: dataArray,selids: selectedCountries },
				   	success: function(msg){ 
					if(msg!='1')
					{
						$('#cityLoc_loader').addClass("hide");
                                                $("#ajax_new_countryDiv").show();
                                                resarray=msg.split('*');                                              
                                                if(resarray[0]=='1'){
                                                    $("#AjaxCountryExists_"+country_id).remove();
                                                } 
                                                $("#ajax_new_country").append(resarray[1]);                                              
                                                 if($('#AjaxAllStates_'+country_id).val()=='1'){setCountry();}
                                                $('#count').val(count1);
                                                $('#countryCount').val(countryCount);
                                                $('.div-city').slideToggle("slow");

                                        }
					
			
					}
	   });
	} else {
		jAlert(errSelectCountry);
	}	 
}
function delStateAjax(countryID,stateID){
    $('#check_'+stateID).prop('checked', false);
    $("li[id^='left_state"+countryID+"_']").find('#check_'+stateID).prop('checked',false); 
    $('#right_state'+countryID+'_'+stateID).hide();
    checkselectAddedAll(countryID);
    checkselectAll(countryID);
    return false;

}		 
function delCountry(country_id)
{
	var state_checked = [];
	$(".statearray_"+country_id).each(function(index) {
		if($(this).prop('checked')==true){
			state_checked.push($(this).val());
		}
	});
	jConfirm(lblCountryAlongState, 'Delete', function(r) {
    if (r)
	{
			$('#AjaxCountryExists_'+country_id).remove();			
			checkselectAll(country_id);
                        checkselectAddedAll(country_id);			
			
	}
		if (!r)	
			for(i=0;i<state_checked.length;i++){
				$("#right_state"+country_id+"_"+state_checked[i]).show();
				$("#left_state"+country_id+"_"+state_checked[i]).hide();
				$('#check_'+state_checked[i]).prop('checked',true);
			}
			return false;	
 	 });  
}
function setCountry()
{
	 $("#stateInput").html('');
         $('#addNewCountry').show();
          $('#AddCountry').val('');
}
function listStates(country_id){
	removeIcon=$('#removeIcon').val();	
	count=$('#count').val();
	var count=parseInt(count);
	count1=count+1;	 
	countryCount	=	$('#countryCount').val();
	countryCount	=	parseInt(countryCount)+1;
	dataArray			= new Array();
        $('#addNewCountry').hide();	
	 if(country_id!=''){   
           var  slctdStates    =   '';
           $(".statearray_"+country_id+":checked").each(function(){
               slctdStates=slctdStates+','+$(this).val();    
           });
	   $('#cityLoc_loader').removeClass("hide");
	   dataArray[0] 			= country_id;           
	   $.ajax({
				   	type: "POST",
					url: GLOBAL_BASE_PATH+"/geozone/ajaxState/",		      		
		           	data: { ajax_request: dataArray,slctdStates:slctdStates },
				   	success: function(msg){ 
					if(msg!='1')
					{
						$('#cityLoc_loader').addClass("hide");
				   	 	$("#stateInput").html(msg);
                                                $('.div-city').hide();
						$('#count').val(count1);
						$('#countryCount').val(countryCount);						
				    }
                                    $('#addNewCountry').show();
				}
	   });
	 } else {
              jAlert(errSelectCountry);
         }
       
}