HJ.PublishCalender= Class.create();

HJ.PublishCalender.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);
		 this.catList = null;
      },
      
      templateLoaded: function() {
         
         var ec = new Af.ElementCollection(this.element);
         this.submit = ec.getFirstElementById("submit");
         this.armedButton = this.submit;
         this.dontSubmit =  ec.getFirstElementById("dontSubmit");
         this.submit.onclick = this.doSubmit.bind(this);
         this.dontSubmit.onclick = this.doDontSubmit.bind(this);
         this.titleCursor = ec.getFirstElementById("communityTitle");
		 this.titleCursor.focus();

         this.editor = new Af.ObjEditor();
         this.commTitle = this.editor.addTextByEC(ec, "communityTitle");
         this.city = this.editor.addTextByEC(ec, "city");
         this.description = this.editor.addTextByEC(ec, "description");
         this.category = this.editor.addDynamicSelectByEC(ec, "category", new Array(), null, "name", "UUID");
         this.editor.addSelectByEC(ec, "state");
         
         if (this.obj != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);
         }

		 this.cityTextBx = ec.getFirstElementById("city");
		 this.cityTextBx.onfocus = this.focusOnTextBox.bindAsEventListener(this);

         
         var req = new Af.DataRequest(svcURL,
			this.requestInfoListCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		 req.addService("PublicService", "getCalCategoryInfoList");
		 ajaxEngine.processRequest(req);
      },
      
      requestInfoListCompleted: function(response) {
         //debugA(response.responseText);
         var xds = new Af.XMLToDataSet(response.responseXML);
	     var list = xds.data["CategoryInfo"];
	     this.catList = new Array();
	     var o = new Object();
	     o["name"] = "Choose Category";
	     o["UUID"] = "_0";
	     this.catList.push(o);
	     for (var i=0; i<list.length; i++) {
	        var c = list[i];
	        o = new Object();
			if(c["subCategory"]!=null){
				if(c["name"]!="Camps"){
					o["name"] = "---- " + c["name"] + " -----";
				}else{
					o["name"] = "**** " + c["name"].toUpperCase() + " ****";
				}
				
		        o["UUID"] = "_" + (i+1);
		        this.catList.push(o);
			}
	        l2 = c["subCategory"];
	        if (l2 != null) {
	           for (var j=0; j<l2.length; j++) {
	              var sc = l2[j];
	              o = new Object();
				  o["name"] = sc["name"];
				  o["UUID"] = sc["UUID"];
				  this.catList.push(o);
	           }
	        }
	     }
	     
	     this.category.setData(this.catList);
      },
      
      setObj: function(obj, mode) {
         this.isCreateMode = mode;
         this.obj = obj;
         if (this.editor != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);
         }
		 this.resetData();
		 if(this.titleCursor != null) {
			this.titleCursor.focus();
		 }
      },
      
      doDontSubmit: function() {
         this.hide();		 
      },
      
      doSubmit:function(){
		 if(this.commTitle.value == null || this.commTitle.value == "" ) {
			showMessageDialog("Please enter a title for this Community Calendar.", "Request", 350, 100);
			return;
		 }
		 if(this.category.value == null || this.category.value == "" ) {
			showMessageDialog("Please select the best category for this calendar.", "Request", 400, 100);
			return;
		 }
		 if(this.description.value == null || this.description.value == ""){
			showMessageDialog("Please enter your description for this calendar.", "Request", 300, 100);
			return;
		 }
		 if(this.description!= null && this.description.value.length > 500){
			showMessageDialog("Please shorten your description to less than 500 characters.", "Request", 300, 100);
			return;
		}
		 this.obj["published"] = "true";		 
         this.editor.addNV("published", this.obj["published"]);
         var xmlString = this.editor.getChangeXml(this.isCreateMode);
         
         if (xmlString == null) {
            showMessageDialog("Any changes you have made have been saved.", "Informing You", 300, 100);
            return;
         }
         //debugA(xmlString);
         var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.saveFailed.bind(this), null,
         this.saveTimedOut.bind(this));
         req.addService("WorkspaceService", "saveScheduleDetails");
         if(this.handler.sch.UUID!=undefined && this.handler.sch.UUID!=null){
            req.addParameter("UUID", this.handler.sch.UUID);
         }
         req.xmlDoc = xmlString;
         
         ajaxEngine.processRequest(req);
         this.hide();
      },
      
      saveSuccessful:function(){
         this.handler.sch["published"] = "true";
         //this.handler.sharingChanged();
		 this.handler.updateSharingState();
         showMessageDialog("<br/>Your schedule has been successfully published to Community Calendars.  Look there now and you'll find it waiting!", "Success!", 400, 150);
      },
      
      saveFailed:function(){
         showMessageDialog("<br/>We are currently unable to publish this schedule. Please try again.", "Excuse us!", 350, 150);
      },
      
      saveTimedOut:function(){
         showMessageDialog("<br/>We are currently unable to publish this schedule. Please try again.", "Excuse us!", 350, 150);
      },

	resetData: function() {
		 if(this.category != null && this.category != undefined && this.catList != null && this.catList != undefined) {
			this.category.setData(this.catList);
		 }
		 if(this.commTitle != null) {
			 this.commTitle.setValue(""); 
		 }
       /*  if(this.city != null) {
			 this.city.setValue("City"); 
		 }*/
         if(this.description != null) {
			 this.description.setValue(""); 
		 }
		 return;
	},

	focusOnTextBox: function(){		
		if(this.city != null) {
			 this.city.setValue(""); 
		 }
	}
      
});

