var ajaxRunning = false;
jQuery(document).ready(function(){
	jQuery('#getLatLong').click(function(){
		var elmObj = jQuery(this);
		if(ajaxRunning){
			return false;
		}
		
		var add = jQuery('#address').val(); var ct= jQuery('#city').val(); var zip = jQuery('#zipcode').val(); 
		var st = '';
		if (jQuery('#lg-state').val()){
			st = jQuery('#lg-state option:selected').text();
		}
		if (add == '' || ct == '' || st == ''){
			alert(message.fullAddressMess);
			return;
		}
		ajaxRunning = true;
		jQuery.ajax({
			type: 'POST',
			url: siteUrl+'customers/getLatLong.json',
			data: { address: add, city: ct, zip:zip,state: st},
			dataType: 'json',
			beforeSend: function(){
				elmObj.after('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
				elmObj.hide();
			},
			success: function(data){
				jQuery('#loading').remove();
				ajaxRunning = false;
				elmObj.show();
				jQuery('#latitude').val(data.lat);
				jQuery('#longitude').val(data.long);
			},
			statusCode: {
				404: function() {
					alert( message.noPage );
				}
			}
		});
		return false;
	});
	
	$('#verify_selected').click(function(){
		verify_customers(0);
		return false;
	});
	$('#unverify_selected').click(function(){
		verify_customers(1);
		return false;
	});
	
});



function verify_customers(ind){
	var checkedItems = $('input[name="listitems"]:checked').length;	
	//alert(checkedItems);	
	if(checkedItems == 0){
		alert(message.selectOne);
		return false;
	}else{
		var elmObj = $('#verify_selected');
		if(ind == 1){
			var postUrl = siteUrl+'customers/customer_unverify';
		}else{
			var postUrl = siteUrl+'customers/customer_verify';
		}
		if(confirm(message.confirmMessage)){
			var chkId = '';
			$('input[name="listitems"]:checked').each(function() {
			  chkId += $(this).val() + ",";
			});
			chkId =  chkId.slice(0,-1);
			$('#deleteAllValue').val(chkId);
		
			ajaxRunning = true;
			
			jQuery.ajax({
				type: 'POST',
				url: postUrl,
				data: $('#delete-all').serialize(),
				beforeSend: function(){
					elmObj.after('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
					elmObj.hide();
				},
				success: function(data){
					jQuery('#loading').remove();
					ajaxRunning = false;
					elmObj.show();
					window.location.href = window.location.href;
				},
				statusCode: {
					404: function() {
						alert( message.noPage );
					}
				}
			});
		}
	}	
}

