HJ.OrganizeShifts = Class.create();

HJ.OrganizeShifts.prototype = Object.extend(new Af.StepsMaker(), {
	
	initialize: function(listObj, step1TargetContainerId) {
		this.doInitialize(listObj, step1TargetContainerId);
		this.url = {
			"step4":"/OrganizeShiftsRecipient.htm",
			"step6":"/RSVPConfirm.htm"
		};		

		this.targetContainerId = {
			"step4":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
			"step6":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId			
		};
		
		//  Row Divs as not to rely on tables for the front end.  
		//  name should match the 

this.rowDivHelper = "<div class=\"genericSignupItems\"><input type=\"text\" name=\"description\" value=\"\" class=\"dark\"/></div>" +
		"<div class=\"signUpMiniCal\"><input name=\"time\" type=\"dueDate\" value=\"\" /><img src=\"image/icon_calendar.gif\" alt=\"Calendar\" /></div>" +
        "<div class=\"signUpVolunteer\" id=\"helper\" ><input type=\"text\" value=\"Volunteer Name Will Go Here\" class=\"\"/></div>" +
        "<div class=\"helperId\" id=\"helperId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
        "<div class=\"helperEventId\" id=\"helperEventId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
		"<div class=\"volunteerNotes\" id=\"volunteerNotes\" ><input type=\"text\" value=\"\" class=\"\"/></div>" +
        "<div class=\"driveDelete\"><a href=\"#\"><span class=\"closeBtn\" title=\"Delete\"></span></a></div>";

this.rowDiv2 = "<span name=\"description\" class=\"genericSignupItems\"></span>" +
			"<span name=\"fromTime\" class=\"driveTime\"></span>" +
            "<span name=\"toTime\" class=\"driveTime\"></span>" +
            "<span class=\"signUpVolunteer\" name=\"helper\"><input disabled=\"disabled\" type=\"text\" value=\"\" class=\"dark\"/></span>" +
            "<span name=\"removeHelper\" ><a href=\"#\" style=\"display:none;\">Remove</a></span>" +
        "<div class=\"helperId\" id=\"helperId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
        "<div class=\"helperEventId\" id=\"helperEventId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" + 
        "<div class=\"listItemUUID\" id=\"listItemUUID\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
		"<span class=\"volunteerNotes\" name=\"notes\"><input type=\"text\" value=\"\" class=\"dark\"/></span>";
		
		this.listFields = new Array('notes', 'lastEmailString',
                                    'lastEmailSubject', 'startDT', 'lastEmailNote',
                                    'collectPayments', 'suggestedAmount', 'helperCanModifyAmount', 'showFeesToHelper', 'nextStep');
		this.listItemFields = new Array('description', 'dueDate', 'time', 'toTime',
                                        'helper', 'helperId', 'helperEventId', 'notes');
		this.templateType = HJ.TEMPLATE_TYPE_ORGANIZE_SHIFTS;
	},
	
	// XXX: Copied from GenericSignup.js.  OrganizeShitfs used to 
    // extend OrganizeShifts
	doInitialize: function(listObj, step1TargetContainerId) {
		this.url = {
			"step1":"/GenericSignup.htm",
			"step2":"/GenericSignup.htm",
			"stepPaymentRequest":"/PaymentRequestStep.htm",
			"stepPaymentRequestThroughAmazon":"/AmazonPaymentRequestStep.htm",
			"step3":"/StepsEmails.htm",
			"step4":"/GenericSignupRecipient.htm",
			"stepPaymentAttempt":"/PaymentAttemptStep.htm",
			"stepPaymentAttemptThroughAmazon":"/AmazonPaymentAttemptStep.htm",
			"step5":"/SenderConfirm.htm",
			"step6":"/RSVPConfirm.htm"
		};		

		this.targetContainerId = {
			"step1":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
			"step2":"bodyContainer",
			"stepPaymentRequest":step1TargetContainerId == null ?
					"bodyContainer" : step1TargetContainerId,		
			"stepPaymentRequestThroughAmazon":step1TargetContainerId == null ? "bodyContainer" : step1TargetContainerId,		
			"step3":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
			"step4":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
        	"stepPaymentAttempt":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
        	"stepPaymentAttemptThroughAmazon":step1TargetContainerId == null ? "bodyContainer" : step1TargetContainerId,		
			"step5":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId,
			"step6":step1TargetContainerId == null ?
                    "bodyContainer" : step1TargetContainerId			
		};
		
		this.listObj = listObj;

		// Must declare templateType per template		
		this.data = new Array;
		this.data['item'] = new Array;
		this.data['item'][0] = new Object();
			
		this.tl = null;
		//  Row Divs as not to rely on tables for the front end.  
		//  name should match the 
		this.rowDiv = "<div class=\"genericSignupItems\"><input type=\"text\" name=\"description\" value=\"\" class=\"dark\"/></div>" +
				"<div class=\"signUpMiniCal\"><input name=\"time\" type=\"dueDate\" value=\"\" /><img src=\"image/icon_calendar.gif\" alt=\"Calendar\" /></div>" +
                "<div class=\"signUpVolunteerHide\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
                "<div class=\"helperIdHide\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
                "<div class=\"helperEventIdHide\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
                "<div class=\"UUID\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
				"<div class=\"volunteerNotesHide\"><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
        		"<div class=\"driveDelete\"><a href=\"#\"><span class=\"closeBtn\" title=\"Delete\"></span></a></div>" +
        		"<div class=\"AddItem\" valign=\"middle;\"><a href=\"#\"><img src=\"image/icon_add.gif\" border=\"0\" class=\"addIcon\">Add item</a></div>";

		this.rowDivHelper = "<div class=\"genericSignupItems\"><input type=\"text\" name=\"description\" value=\"\" class=\"dark\"/></div>" +
				"<div class=\"signUpMiniCal\"><input name=\"time\" type=\"dueDate\" value=\"\" /><img src=\"image/icon_calendar.gif\" alt=\"Calendar\" /></div>" +
                "<div class=\"signUpVolunteer\" ><input type=\"text\" value=\"Volunteer Name Will Go Here\" class=\"\"/></div>" +
                "<div class=\"helperId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
                "<div class=\"helperEventId\" id=\"helperEventId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
                "<div class=\"listItemUUID\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
        		"<div class=\"volunteerNotes\"  ><input type=\"text\" value=\"\" class=\"\"/></div>";
		
		this.rowDiv2 = "<span name=\"description\" class=\"genericSignupItems\"></span>" +
                    "<span class=\"signUpMiniCal\" name=\"dueDate\"></span>" +
                    "<span class=\"signUpVolunteer\" name=\"helper\"><input disabled=\"disabled\" type=\"text\" value=\"\" class=\"dark\"/></span>" +
                    "<span name=\"removeHelper\" ><a href=\"#\" style=\"display:none;\">Remove</a></span>" +
	                "<div class=\"helperId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
	                "<div class=\"helperEventId\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" + 
	                "<div class=\"listItemUUID\" ><input type=\"hidden\" value=\"\" class=\"\"/></div>" +
        			"<span class=\"volunteerNotes\" name=\"notes\"><input type=\"text\" value=\"\" class=\"dark\"/></span>";

		this.listFields = new Array('notes', 'lastEmailString',
                                    'lastEmailSubject', 'lastEmailNote',
                                    'collectPayments', 'suggestedAmount', 'helperCanModifyAmount', 'showFeesToHelper', 'nextStep');
		this.listItemFields = new Array('description', 'dueDate',
                                        'helper', 'helperId', 'helperEventId', 'UUID', 'notes');
		this.templateType = HJ.TEMPLATE_TYPE_GENERIC_SIGNUP;
		this.initializeSelectedStepFromQueryString();		
		this.itemCount = 0;
		this.setDataObject();
	},
	
	setDataObject: function() {
		if (this.listObj!=null)	{	
			this.data = this.listObj;
			this.doInitialRoute();
			
			var milliStart = null;
			if (this.data['startDT'] != null && this.data['startDT'] != "") {
				milliStart = new Date(parseInt(this.data['startDT']));
				this.data['startDT'] = milliStart.getMonth() + 1 + "/" + milliStart.getDate()
	            + "/" + milliStart.getFullYear();
			}
		}
	},	
	
	//  Checks selected step and loads the appropriate template
	loadTemplate: function () {
		if (this.listObj != null && this.listObj.listDeleted != null && this.listObj.listDeleted == "true") {
			var errorText = document.getElementById("errorText");
			if (errorText != null && errorText.style != null && errorText.style.display != null && errorText.innerHTML != null) {
				errorText.style.display = "block";
				errorText.innerHTML = "The requester has canceled this Sign-Up";
			} else {
				window.alert("This Sign-Up is cancelled");
			}
			return;
		}

		if (this.selectedStep == 4 || this.selectedStep == 6) {
			this.doLoadTemplate();
		} else {
			window.location = basePageURL + "OrganizeShifts.htm?step="+(this.selectedStep-1) +
								"&uuid="+this.listObj.UUID;
		}
	},

	doLoadStep4:function() {		
		if (this.data['name']!=null || this.data['name']!=undefined)
			document.getElementById('name').innerHTML = this.data['name'];

		if (this.data['startDT']!=null || this.data['startDT']!=undefined)
			document.getElementById('startDT').innerHTML = this.data['startDT'];

		if (this.data['notes']!=null || this.data['notes']!=undefined) {
			document.getElementById('notes').innerHTML = textDisplay(this.data['notes']);
		}
		
		this.listContainer = document.getElementById('listItems');
		
		var listItems = this.data['item'];
	
		if (listItems!=null) {
			for (var i=0; i<listItems.length; i++) {
				var signupDiv = document.createElement('div');
				signupDiv.innerHTML = this.rowDiv2;
				signupDiv.className = "driveFormCont";
							
				signupDiv.id = listItems[i].divName = 'signupDiv_'+i;
				var dataFields = signupDiv.getElementsByTagName('span');

                var description = listItems[i].description;
				if (description==null || description=='undefined') {
					listItems[i].description = '';
                    description = '';
				}
				dataFields[0].innerHTML = description;

                var fromTime = listItems[i].time;
				if (fromTime==null || fromTime=='undefined') {
					listItems[i].time = '';
                    fromTime = '';
				}
				dataFields[1].innerHTML = fromTime;
				
                var toTime = listItems[i].toTime;
				if (toTime==null || toTime=='undefined') {
					listItems[i].toTime = '';
					toTime = '';
				}
				dataFields[2].innerHTML = toTime;

				var helperInputField = signupDiv.getElementsByTagName('input');

                var helper = listItems[i].helper;
				if (helper == null || helper == 'undefined' || helper == 'Volunteer Name Will Go Here') {
					listItems[i].helper = helper = '';
				}
				helperInputField[0].value = helper;	
				
                var helperId = listItems[i].helperId;
				if (helperId == null || helperId == 'undefined' || helperId == '') {
					listItems[i].helperId = helperId = '';
					listItems[i].helper = helper = '';
				}
				helperInputField[1].value = helperId;	

                var helperEventId = listItems[i].helperEventId;
				if (helperEventId == null || helperEventId == 'undefined') {
					listItems[i].helperEventId = helperEventId = '';
				}
				helperInputField[2].value = helperEventId;	
				
                var listItemUUID = listItems[i].UUID;
				if (listItemUUID == null || listItemUUID == 'undefined') {
					listItems[i].UUID = listItemUUID = '';
				}
				helperInputField[3].value = listItemUUID;	

				// notes field
                var notes = listItems[i].notes;
				if (notes == null || notes == 'undefined' || notes == '') {
					listItems[i].notes = notes = '';
				}
				helperInputField[4].value = notes;
				helperInputField[4].id = "notes_"+i;

				if (helperId!=null && helperId != "" && login==null) {
					if(helper != null && helper != ''  && helper != 'undefined') {
						helperInputField[0].disabled = 'disabled';
						helperInputField[4].disabled = 'disabled';
					}
					else { 
						helperInputField[0].disabled = false;							
						helperInputField[4].disabled = false;
					}
				} else {
					helperInputField[0].disabled = false;
					helperInputField[4].disabled = false;
					helperInputField[0].onclick = this.selectHelper.bindAsEventListener(this);
					helperInputField[0].onfocus = this.selectHelper.bindAsEventListener(this);
				}
				
				this.listContainer.appendChild(signupDiv);
			}		
		}
	},


	loadStep4:function() {
		this.doLoadStep4();
	}
});
