Locker = {
	reserve : function(type){
		
		q = {Method : "Locker.reserve"};
		
		if(Building.Current === undefined) {
			alert("Select a building first");
			UI.showStep(1);
			return false;
		} else {
			q.Building = Building.Current.Id;
		}
		
		if(Section.Current != undefined)
			q.Section = Section.Current.Id;
		
		switch(type) {
			case('accessible'):
			q.Type = 'accessible';
			break;
			case('half'):
			q.Type = 'half';
			break;
			default:
			q.Type = 'full';
			break;
		}

		API.query(q, Locker.display);
	},
	reserveByNumber : function(batch, number, type){
		q = {Method : "Locker.reserve"};
		q.Locker = [batch,number];
		q.Type = type;
		
		API.query(q, function(json){
			document.location = API.BASEURL;
		});

		
	},
	display : function(json){

		Locker.Current = json.Locker;
		
		if(Locker.Current.Batch === undefined)
			Batch = "";
		else
			Batch = Locker.Current.Batch;
		
		Timer.reset();
		UI.showStep(3);

	},
	displayResults : function(json){
	
		html = "<thead><tr>";
		html += "<th>Number</th> \
				<th>State</th> \
				<th>Building</th> \
				<th>Section</th> \
				<th>Type</th> \
				<th>View</th>";
				
		html += "</tr></thead>"
		
		if(json.Locker != undefined && json.Locker.Batch != null) {
			
			L = json.Locker;
			
			if(Building.BuildingNames[L.Building_Id] == undefined) {
				
				Building.get(L.Building_Id, function(building){
					Building.BuildingNames[L.Building_Id] = building.Building.Name;
					Locker.displayResults(json);
				});
				
			} else if(Building.SectionNames[L.Section_Id] == undefined) {
				
				Building.getSections(L.Building_Id, function(sections){
					
					for(i=0;i<sections.Sections.length;i++)
						Building.SectionNames[sections.Sections[i].Id] = sections.Sections[i].Name;
						
					Locker.displayResults(json);
				})
				
			} else {
				html += "<tr>";
				html += "<td>" + L.Batch + L.Number +"</td>";
				html += "<td>" + L.State + "</td>";
				html += "<td>" + Building.BuildingNames[L.Building_Id] + "</td>";
				html += "<td>" + Building.SectionNames[L.Section_Id] + "</td>";
				html += "<td>" + L.Type + "</td>";
					
				html += "<td><a href=\"#\" onclick=\"Locker.get("+L.Id+");return false;\">View Details</a>";
				
				if(L.Registrant_Id != undefined)
					html += " | <a href=\"#\" onclick=\"Registrant.get("+L.Registrant_Id+");return false;\">edit registrant</a>";
						
				html += "</td></tr>";
			}

			
		} else {
			html += "<tr>";
			html += "<td colspan=\"7\">No Lockers Found</td>";
			html += "</tr>";
		}
		
		$("#search_data").html(html);
			
	},
	get : function(id){
		q = {Method : "Locker.get", Id : id};
		API.query(q, function(json){
			Tab.show("#EditLocker");
			Locker.displayBackendLocker(json);
		});
	},
	displayBackendLocker : function(json){
		
		L = json.Locker;
		
		if(Building.BuildingNames[L.Building_Id] == undefined) {
			
			Building.get(L.Building_Id, function(building){
				Building.BuildingNames[L.Building_Id] = building.Building.Name;
				Locker.displayBackendLocker(json);
			});
			
		} else if(Building.SectionNames[L.Section_Id] == undefined) {
			
			Building.getSections(L.Building_Id, function(sections){
				
				for(i=0;i<sections.Sections.length;i++)
					Building.SectionNames[sections.Sections[i].Id] = sections.Sections[i].Name;
					
				Locker.displayBackendLocker(json);
			});
			
		} else {

		
		html = "<div class=\"clearfix\">"

		if(L.Registrant_Id != undefined)
			html += "<a class=\"wide_btn textcenter left\" href=\"#\" onclick=\"Locker.unregister("+L.Id+");return false;\">Unregister Locker</a>";
			
		if(L.State == "free") {
			html += "<a class=\"wide_btn textcenter left\" href=\"#\" onclick=\"Session.clear(function(){Locker.reserveByNumber('" + L.Batch + "','" + L.Number+"','" + L.Type+"')});return false;\">Register Locker</a>";
		}
			
			
			
		html += "</div><a class=\"ui_btn previous\" href=\"#Registrant\" onclick=\"\">&larr; Back to search results</a>";
		html += "<h3>Locker Information</h3>";
		html += "<strong>Locker Number: </strong>"+L.Batch + L.Number+"<br />";
		
		switch(L.State){
			case("free"):
			html += "<strong>State: </strong>\
			<label for=\"state\">Free<input onclick=\"Locker.setFree("+L.Id+");return true;\" class=\"radio\" name=\"state\" type=\"radio\" value=\"free\" checked=\"checked\" /></label>\
			<label for=\"state\">Reserved<input onclick=\"Locker.setReserved("+L.Id+");return true;\" class=\"radio\" name=\"state\" type=\"radio\" value=\"reserved\" /></label>\
			<br />";
			break;
			case("reserved"):
			html += "<strong>State: </strong>\
			<label for=\"state\">Free<input onclick=\"Locker.setFree("+L.Id+");return true;\" class=\"radio\" name=\"state\" type=\"radio\" value=\"free\" /></label>\
			<label for=\"state\">Reserved<input onclick=\"Locker.setReserved("+L.Id+");return true;\" class=\"radio\" name=\"state\" type=\"radio\" value=\"reserved\" checked=\"checked\" /></label>\
			<br />";
			break;
			default:
			break;
		}
		
		html += "<strong>Building: </strong>"+Building.BuildingNames[L.Building_Id]+"<br />";
		html += "<strong>Section: </strong>"+Building.SectionNames[L.Section_Id]+"<br />";
		html += "<strong>Type: </strong>"+L.Type+"<br /><br />";
		
		html += "<strong>Locker Combination: </strong> <form onsubmit=\"Locker.setCombination("+L.Id+");return false;\"><input class=\"text\" id=\"Combination\" name=\"Combination\" value=\"\" /><label class=\"error\" id=\"error_Combination\" for=\"Combination\"></label></form><br />";
		html += "<a class=\"wide_btn textcenter left\" href=\"#\" onclick=\"Locker.setCombination("+L.Id+");return false;\">Update Combination</a>";
		html += "<a class=\"wide_btn textcenter left\" href=\"#\" onclick=\"Locker.getCombination("+L.Id+");return false;\">Show Combination</a>";
			
		$("#LockerDetails").html(html);
		
		}
		
	},
	uploadFile : function(){
		q = new Object();
		q.Method = "Locker.import";
		if(Session.check())
			q.Session = Cookie.read("Session");
			
		q = $.toJSON(q);
		
		$("#q").val(q);
		
		if($('#fileToImport').val() != ' ') {
			$('#F').attr({ target: "iframeUpload" });
			$('#iframeUpload').load(function() {
				
			})
		}
	},
	exportCSV : function(){
		q = new Object();
		q.Method = "Locker.export";
		if(Session.check())
			q.Session = Cookie.read("Session");
			
		q = $.toJSON(q);
		
		$("#export_q").val(q);
	},
	setCombination : function(id){
		combo = $('#Combination').val().split("-");
		p1 = Format.padZero(combo[0]);
		p2 = Format.padZero(combo[1]);
		p3 = Format.padZero(combo[2]);
		combo = p1+p2+p3;

		q = {Method : "Locker.setCombination", Id : id, Combination : combo};
		API.query(q, function(json){
			$("#error_Combination").html("Combination successfully updated")
		});
		
	},
	getCombination : function(id){
		q = {Method : "Locker.getCombination", Id : id};
		API.query(q, function(json){
			if(json.Combination === null)
				alert("Combination is null, this means that there is no combination");
			else
				$("#Combination").val(Format.combo(json.Combination));
		})
	},
	setFree : function(id){
		
		q = {Method : "Locker.setFree", Id : id};
		API.query(q, function(json){
			//alert(dump(json))
		});
		
	},
	setReserved : function(id){

		q = {Method : "Locker.setReserved", Id : id};
		API.query(q, function(json){
			//alert(dump(json))
		});
		
	},
	release : function(callback){
		q = {Method : "Locker.release"};
		Timer.stop();
		API.query(q, callback);
		UI.showStep(2);
	},
	unregister : function(id){
		q = {Method : "Locker.unregister", Id : id};
		API.query(q, function(){
			history.go(-1);
		});
	}
}

