﻿jQuery(function($) {
$(document).ready(function(){
	
	$('.confirm').click(function(e){
		if (!confirm('Are you sure?')){
			e.preventDefault();
		}
		
	});//end click 
	
		
});//end ready

jQuery.fn.forceNumericOnly =
	function()
	{
	    return this.each(function()
	    {
	        $(this).keydown(function(e)
	        {
	            var key = e.charCode || e.keyCode || 0;
	            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
	            return (
	                key == 8 || 
	                key == 9 ||
	                key == 46 ||
	                (key >= 37 && key <= 40) ||
	                (key >= 48 && key <= 57) ||
	                (key >= 96 && key <= 105));
	        });
	    });
	};


});


	


jQuery.fn.tmAutoComplete = function(datasource){
	
	switch(datasource){
	case 'product':
		$(this).autocomplete({
			
		 source: function( request, response ) {
			$.ajax({
				url: "/global/jqueryAutoCompleteHelper.php",
				dataType: "json",
				data: 'term='+ encodeURI(request.term)+'&datasource='+ datasource,			
				success: function( data ) {					
					response( $.map( data, function(item ) {
						return {
							label: item.text,
							value: item.value
						}
					}));
				}
			});
		},
		minLength: 1
			
		})
		.focus(function(){this.select();})		
		;
	}//end switch 
	
	
};//end tmAutoCompelete

function callTMAjaxService(dataStr){
	var ret;
	$.ajax({
		url: "/global/jqueryServices.php",
		dataType: "json",
		type:"POST",
		data: encodeURI(dataStr),
		async:false,
		success: function( data ) {					
			ret = data;
			
		}
	});
	
	return ret;
}

function callTMAjaxServiceAsync(dataStr,func){

	$.ajax({
		url: "/global/jqueryServices.php",
		dataType: "json",
		type:"POST",
		data: encodeURI(dataStr),
		async:true,
		success: function( data ) {					
			func(data);
			
		}
	});
	
	
}



function getProductById(id){
	var dataStr = 'id='+ id+ '&method=getProductById';
	return callTMAjaxService(dataStr);
}
function getProductPrice(id){
	var dataStr = 'id='+ id+ '&method=getProductPrice';
	return callTMAjaxService(dataStr);
	
}

function getLoginCustomerData(handler){

	 callTMAjaxServiceAsync("method=getLoginedCustomer",handler);
 }

function getSessionGeoInfo(handler){
	var dataStr = 'method=getSessionGeoInfo';
	
	callTMAjaxServiceAsync(dataStr,handler);
	
}



function authenticateCustomer(email,password){
	var dataStr = "email="+email + "&password="+password+"&method=authenticateCustomer";
	return $.ajax({
		url: "/global/jqueryServices.php",
		dataType: "json",
		type:"POST",
		data: encodeURI(dataStr)
		
	}); 
	
}




/*function getEmptyOrderProductsCs(){
	var dataStr = 'method=getEmptyOrderProductsCs';
	return callTMAjaxService(dataStr)
	
}*/

