var spot = {
	base: '/spot2000/',
	server: '/spot2000/spot.asp',
	
	status: {
		'130': 'Collected/Delivered',
		'129': 'Ready for collection/delivery',
		'128': 'In process'
	},
	
	templates: {
		listPickupRequest: function(r){
			LOG(r);
			var o = '';
			o += '<div class="PickupRequest PickupRequest-'+(r.ProcessedDateTime?'Processed':'Pending')+'">';
				o += '<strong class="PickupDate">'+r.PickupDate+'</strong>';
				if(!r.ProcessedDateTime) o += ' - <u class="Pending">Request Pending</u>';
				if(r.Comments) o += '<div class="Comments">'+r.Comments+'</div>';
				if(r.ProcessedDateTime){
					r.Discarded = (r.Discarded=="True");
					o += '<div class="'+(r.Discarded?'Rejected':'Accepted')+'">'+(r.Discarded?'Sorry, this request was rejected':'This request was accepted')+' on '+r.ProcessedDateTime+'</div>';
				};
			o += '</div>';
			return o;
		},
		listInvoice: function(r){
			var o = '';
			var status = (r.outDate?'PickedUp':(r.readyDate?'Processed':'Pending'));
			o += '<div class="Invoice Invoice-'+(r.outDate?'Complete':'InProcess')+'">';
				o += '<strong class="invoice">'+r.invoice+'</strong>';
				if(!r.outDate){
 				o += ' - <u class="status '+status+'">'+spot.status[r.status]+'</u>';
				};
				if(r.department) o += '<div class="department">Department: '+r.department+'</div>';
				if(r.outDate){
				 o += '<div class="outDate">Collected/Delivered on '+r.outDate+'</div>';
				}
				else{
					if(r.inDate) o += '<div class="inDate">Received on '+r.inDate+'</div>';
					if(r.readyDate) o += '<div class="readyDate">Ready on '+r.readyDate+'</div>';
				};
			o += '</div>';
			return o;
		},
		viewInvoice: function(r){
		}
	},
	
	query: function(s, c){
		if(!s.test(/&$/)) s += '&';
		s += '' + String((new Date()).getTime()).replace(/\D/gi,'') + '';
		$.getJSON(spot.server+'?format=js&'+s, function(x){
   if(c) c(x);
		});
	},
	
	action: function(s, c){
		return spot.query('a='+s+'', c);
	},
	
	login: function(phone, postcode, c){
		this.action('login&phone='+phone+'&postcode='+postcode+'&', c);
	},
	
	logout: function(c){
		this.action('logout', c);
	},
	
	LoadClientDetails: function(){
		spot.action('Client', function(x){
   var client = x.data;
			LOG(client);
		 if(!client) return;
			$('.spot-client-detail').each(function(){
    var o = $(this);
    if(!this.data) this.data = o.metadata();
			 LOG([this, this.data, client[0][this.data.field]]);
			 o.html(client[0][this.data.field]);
			});
		});
	}
	
};

