HJ.HomajoonQA = Class.create();

HJ.HomajoonQA.prototype =Object.extend(new Af.HtmlDialog(), {

	initialize: function(title, handler, content) {
		 this._initializeDialog(title, handler, content);
		 this.userObj =null;
		 this.userScrName = null;
	},
	templateLoaded: function() {
		
		var ec = new Af.ElementCollection(this.element);
		 this.memberScrName = ec.getFirstElementById("memberScreenName");
		this.save = ec.getFirstElementById("save");
		this.dontSave =  ec.getFirstElementById("dontSave");
		this.save.onclick = this.doSave.bind(this);
		this.armedButton = this.save;
		this.dontSave.onclick = this.doDontSave.bind(this);
		this.seeingStarLink = ec.getFirstElementById("seeingStarLink");
		this.seeingStarLink.onclick = this.doShowPoints.bind(this);
		//this.seeingStarLink.onmouseover = this.doShowPoints.bind(this);
		//this.seeingStarLink.onmouseout = this.doHidePoints.bind(this);

		this.QsnCursor = ec.getFirstElementById("question");
		this.QsnCursor.focus();
		this.editor = new Af.ObjEditor();

		this.editor.addTextByEC(ec, "question");
		this.editor.addTextByEC(ec, "description");

		this.emailAnswer = ec.getFirstElementById("emailAnswer");

		this.category = this.editor.addDynamicSelectByEC(ec, "category", new Array(), null, "name", "UUID");

		this.getScreenName();
		if (this.obj != null) {
				this.editor.clearChanges();
				this.editor.setValueFromObj(this.obj);
		}

		var req = new Af.DataRequest(svcURL,
		this.requestInfoListCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		req.addService("CommunityQAService", "getQACategoryList");
		ajaxEngine.processRequest(req);
	},
	
	 requestInfoListCompleted: function(response) {
		//debugA(response.responseText);
		var xds = new Af.XMLToDataSet(response.responseXML);
		var list = xds.data["CategoryInfo"];
		this.l = new Array();
		var o = new Object();
		o["name"] = "Choose Category";
		o["UUID"] = "";
		this.l.push(o);
		var campsCatList = new Array();
		var planeCatList = new Array();
		for (var i=0; i<list.length; i++) {
			if(list[i]["owner"] !=null && list[i]["owner"]=="Camps"){
				  campsCatList.push(list[i]);
			  }else{
  				  planeCatList.push(list[i]);
			}				
		}
		   o = new Object();
		   o["name"] = "---- " + "GENERAL" + " -----";
		   o["UUID"] = "_"+"1";
		   this.l.push(o);
			l2 = planeCatList;
		    if (l2 != null) {
			   for (var k=0; k<l2.length; k++) {
				  var sc = l2[k];
				  o = new Object();
				  var str = (sc["name"]).toString();
				  o["name"] = " "+str.replace("&amp;","&")+" ";
				  o["UUID"] = sc["UUID"];
				  this.l.push(o);
			   }
		    }
		   o = new Object();
		   o["name"] = "**** " + "CAMPS" + " ****";
		   o["UUID"] = "_"+"2";
		   this.l.push(o);
		for(var j=0;j<campsCatList.length;j++){
			var c = campsCatList[j];
	        o = new Object();
			var str = (c["name"]).toString();
			o["name"] = " "+str.replace("&amp;","&")+" ";
	        o["UUID"] = c["UUID"];
	        this.l.push(o);
		}
	     
		this.category.setData(this.l);
	},
	
	setObj: function(obj, mode) {
		this.getScreenName();
		this.isCreateMode = mode;
		this.obj = obj;
		this.obj["emailAnswer"] = "false";
		if (this.editor != null) {
				this.editor.clearChanges();
				this.editor.setValueFromObj(this.obj);
		}
		if(this.l!=null || this.l!=undefined){
				this.category.setData(this.l);
		}
		if(this.QsnCursor != null) {
			this.QsnCursor.focus();
		}
	},
      
	doDontSave: function() {
		this.doHidePoints();
		this.hide();
	},
	
	 doSave: function() {
		if(this.emailAnswer!=null && this.emailAnswer.checked){
			this.obj["emailAnswer"] = "true";
		}
		this.editor.addNV("emailAnswer",  this.obj["emailAnswer"]);

		var ec = new Af.ElementCollection(this.element);
		var Question = trim(ec.getFirstElementById("question").value);
		var description = trim(ec.getFirstElementById("description").value);
		var xmlString = this.editor.getChangeXml(this.isCreateMode);
		
		if (xmlString == null) {
				showMessageDialog("Any changes you have made have been saved.", "Informing You", 300, 100);
		return;
		}
		
		if(Question==""){
				showMessageDialog("Please enter a question.", "Request", 300, 100);
				return;
		}
		
		if(Question.length>150){
				showMessageDialog("Please enter a concise question (less than 150 characters). <br/>If you need more room to explain, please use the Details field.", "Request", 400, 100);
				return;
		}

		 if(description.length>250){
				showMessageDialog("Please shorten your details to less than 250 characters.", "Request", 300, 100);
				return;
		}

		if(this.category.value == undefined || this.category.value=="") {
				showMessageDialog("Please select a category that best fits your question.", "Request", 300, 100);
				return;
		}
		
		var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.saveFailed.bind(this), null,
		this.saveTimedOut.bind(this));
		req.addService("WorkspaceService", "saveQuestion");
		
		if (this.obj.UUID != null) {
				req.addParameter("UUID", this.obj.UUID);
		}
		req.xmlDoc = xmlString;
		ajaxEngine.processRequest(req);
		this.hide();
	 },

	saveSuccessful: function(response, msg) {
		this.emailAnswer.checked = false;
		if (this.obj.UUID == null) {
				var strArray = response.responseText.split(",");
				this.obj.UUID = strArray[0];
				this.earnedPoints = strArray[1];
				app.thankYouNote.doOpenThankYouDialog(this.earnedPoints);
		}
		this.editor.clearChanges();
		this.handler.loadCategoryInfo();
},

		saveFailed: function(dataRequest, msg) {
				showMessageDialog("We are currently unable to save this question.  Please try again.", "Excuse us!", 350, 100);
		},

		saveTimedOut: function(dataRequest) {
				showMessageDialog("We are currently unable to save this question.  Please try again.", "Excuse us!", 350, 100);
		},

	getScreenName: function() {
	   var req = new Af.DataRequest(svcURL,this.screenNameLoadCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
	   req.addService("AccessControlSvc", "getMyProfile");
	   ajaxEngine.processRequest(req);
	 },
	 
	 screenNameLoadCompleted: function(response) {
	   var xmlDataSource = new Af.XMLToDataSet(response.responseXML);
	   this.userObj = xmlDataSource.data;
		 this.userScrName = this.userObj.userId;
		 this.memberScrName.innerHTML = this.userScrName;
	},

	doOpenSeeingStarMainPage: function() {
		app.seeingStar.loadSeeingStarTemplate();
		return false;
	},

	doShowPoints:function(){
		app.seeingStar.doLearnAboutPoints();
	},

	doHidePoints:function(){
		app.seeingStar.hideLearnAboutPoints();
	}
});

