Registrant = {
	create : function(){
		$("input.text.error").removeClass("error");
		$("label.error").hide();
		q = {Method : "Registrant.create"}
		q.Name = $("#Name").val();
		q.Student_Id = $("#Student_Id").val();
		q.Email = $("#Email").val();
		q.Phone = $("#Phone").val();
		
		q.Subscribe = $("#Subscribe:checked").val();
		
		if(q.Subscribe == "on")
			q.Subscribe = "yes";
		else
			q.Subscribe = "no";

		API.query(q, Registrant.set);
	},
	update : function(){
		$("input.text.error").removeClass("error");
		q = {Method : "Registrant.update"}
		q.Name = $("#Name").val();
		q.Student_Id = $("#Student_Id").val();
		q.Email = $("#Email").val();
		q.Phone = $("#Phone").val();
		q.Id = $("#Registrant_Id").val();
		
		q.Subscribe = $("#Subscribe:checked").val();
		
		if(q.Subscribe == "on")
			q.Subscribe = "yes";
		else
			q.Subscribe = "no";

		API.query(q, function(json){
			history.go(-1);
		});
	},
	display : function(){
		txt = "<h3>Locker Information</h3>";
		
		if(Building.Current != undefined && Building.Current.Id == Locker.Current.Building_Id)
			txt += "<strong>Building: </strong>"+Building.Current.Name+"<br />";
		
		txt += "<strong>Locker Number: </strong>";
		
		if(Locker.Current.Batch != undefined)
			txt += Locker.Current.Batch;
			
		if(Locker.Current.Number != undefined)
			txt += Locker.Current.Number;
		
		txt += "<h3>Registrant Info</h3>";
		txt += "<strong>Name: </strong>"+Registrant.Current.Name+"<br />";
		txt += "<strong>Student ID: </strong>"+Registrant.Current.Student_Id+"<br />";
		txt += "<strong>Email: </strong>"+Registrant.Current.Email+"<br />";
		
		Registrant.Current.Phone = Registrant.Current.Phone;
		txt += "<strong>Phone: </strong>"+Registrant.Current.Phone;
		
		
		$("#RegistrantInfo").html(txt);
	},
	displayErrors : function(field){
		$("#"+field).addClass("error").focus();
		$("#"+field+"_error").show();
	},
	displayResults : function(json){
		html = "<thead><tr>";
		html += "<th>Name</th><th>Student ID</th><th>Email</th><th>Phone</th><th>Subscribe</th><th width=\"170\">Edit</th>";
		html += "</tr></thead>"
		
		if(json.Registrants.length > 0) {

			for(i=0;i<json.Registrants.length;i++){
				R = json.Registrants[i];
				html += "<tr Registrant_Id=\""+R.Id+"\">";
				html += "<td>" + R.Name + "</td>";
				html += "<td>" + R.Student_Id + "</td>";
				html += "<td>" + R.Email + "</td>";
				html += "<td>" + R.Phone + "</td>";
				html += "<td>" + R.Subscribe + "</td>";
				html += "<td><a href=\"#\" onclick=\"Locker.get("+R.Locker_Id+");return false;\">edit locker</a> | <a href=\"#\" onclick=\"Registrant.get("+R.Id+");return false;\">edit registrant</a></td>";
				html += "</tr>";
			}
			
		} else {
			html += "<tr>";
			html += "<td colspan=\"6\">No Results</td>";
			html += "</tr>";
		}
		
		$("#search_data").html(html);

		
	},
	get : function(id, callback){
		q = {Method : "Registrant.get", Id : id};
		API.query(q, function(json){
			Tab.show("#EditRegistrant");
			R = json.Registrant;
			html = "<a class=\"ui_btn previous\" href=\"#\" onclick=\"history.go(-1);return false;\">&larr; Back to search results</a>";

			R.Phone = Format.phone(R.Phone);
			
			html += '<form class="center" onsubmit="Registrant.update();return false;"><div class="field"><label for="Name">Name</label><input class="text" type="text" name="Name" id="Name" value="'+R.Name+'" /></div><div class="field"><label for="Student_Id">Student ID</label><input class="text" type="text" name="Student_Id" id="Student_Id" value="'+R.Student_Id+'" /></div><div class="field"><label for="Email">Email</label><input class="text" type="text" name="Email" id="Email" value="'+R.Email+'" /></div><div class="field"><label for="Phone">Phone (306-555-5555)</label><input class="text" type="text" name="Phone" id="Phone" value="'+R.Phone+'" /></div><div class="field"><label for="Subscribe" class="disallow_highlight">Add me to the URSU email update list<input class="checkbox" type="checkbox" name="Subscribe" id="Subscribe"';
						
				if(R.Subscribe == "yes")	
					html += ' checked="checked"';
				
				html += ' /></label></div><div class="buttons clearfix"><input type="hidden" id="Registrant_Id" name="Registrant_Id" value="'+R.Id+'" /><a onclick="history.go(-1);return false;" class="cancel_btn textcenter left">Cancel</a><a onclick="Registrant.update();" class="wide_btn textcenter right">Update</a></div></form>';

			$("#RegistrantDetails").html(html);
			Forms.addEvents();
		});
	},
	set : function(json){
		Registrant.Current = json.Registrant;
		UI.showStep(4);
	}
}
