HJ.HomajoonResource = Class.create();

HJ.HomajoonResource.prototype =Object.extend(new Af.HtmlDialog(), {
      
	initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);        
		 this.makeResTooBoolean = false;
		 this.validate = true;		 
    },
	templateLoaded: function() {
		this.busName = document.getElementById("businessName");		 
		var ec = new Af.ElementCollection(this.element);
		this.hResToSeeingStarPage = document.getElementById("hResToSeeingStarPage");
		this.continueStep = ec.getFirstElementById("continue");
		this.armedButton = this.continueStep;
		this.dontSave =  ec.getFirstElementById("dontSave");
		this.makeMyResourceToo = ec.getFirstElementById("makeMyResourceToo");
		this.continueStep.onclick = this.continueToReview.bind(this);
		this.dontSave.onclick = this.doDontSave.bind(this);         
		this.makeMyResourceToo.onclick = this.doMakeThisResToMyResourceToo.bind(this);
		this.hResToSeeingStarPage.onclick = this.doOpenSeeingStarMainPage.bind(this);
		this.hResToSeeingStarPage.onmouseover = this.doShowPoints.bind(this);
		this.hResToSeeingStarPage.onmouseout = this.doHidePoints.bind(this);

		 
         var ec = new Af.ElementCollection(this.element);
         
         this.editor = new Af.ObjEditor();
         
         this.businessName = this.editor.addTextByEC(ec, "businessName");
         this.editor.addTextByEC(ec, "fName");
         this.editor.addTextByEC(ec, "lName");         
         this.email = this.editor.addTextByEC(ec, "email");
         this.phone = this.editor.addTextByEC(ec, "phone");
         this.website = this.editor.addTextByEC(ec, "website");
         this.address = this.editor.addTextByEC(ec, "address");
         this.city = this.editor.addTextByEC(ec, "city");
         this.state = this.editor.addTextByEC(ec, "state");
         this.zip = this.editor.addTextByEC(ec, "zip");   
		 this.zip.element.onkeypress = this.doValidateZipCode.bindAsEventListener(this);

		this.category = this.editor.addDynamicSelectByEC(ec, "category", new Array(), null, "name", "UUID");       
         
        if (this.obj != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);
        }
		 this.registerPhoneNo();
       
        var req = new Af.DataRequest(svcURL,
		this.requestInfoListCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		req.addService("CommunityResourceService", "getResourceCategoryList");
		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.isCreateMode = mode;
         this.obj = obj;
         if (this.editor != null) {
            this.editor.clearChanges();
            this.editor.setValueFromObj(this.obj);			
         }		 
		 if(this.l != null || this.l != undefined) {
			this.category.setData(this.l);
		 }
		 this.registerPhoneNo();
     },
      
	 registerPhoneNo: function() {
		 if(this.phone != null && this.obj != null) {
			var obj = cloneObject(this.obj);
			app.formatPhone.setListenerOnPhonesId("phone", obj); 
		 }
		 if(this.busName != null) {
			// this.busName.focus();
		 }
	 },

     doDontSave: function() {
         this.hide();
     },
	
	 doValidate: function() {  		
		this.fObject = app.formatPhone.getFormattedObject();
		if(this.fObject["phone"] != undefined && this.fObject["phone"] != null)
			this.obj["phone"] = this.fObject["phone"];
		this.editor.addNV("phone", this.obj["phone"]);

		 var bn = this.businessName.getValue();
		 if(bn == null || trim(bn) == "") {
		 	showMessageDialog("Please enter a business name.", "Request", 300, 100);
            return;
		 }			
		 if(this.obj["phone"] == undefined || this.obj["phone"] == "") {				
				showMessageDialog("Please enter a valid phone number.","Request", 300, 100);
				return;
		 }
		  if(this.category.value == undefined || this.category.value == "") {
		 	showMessageDialog("Please select the best category for this resource.", "Request", 300, 100);
			return;
		 }
		  if(this.address.value != undefined && this.address.value.length > 100 ) {
			 showMessageDialog("The address you have entered too long for us.  Please enter a shorter version.","Request",300,100);
			 return;
		 }

		 this.xmlString = this.editor.getChangeXml(this.isCreateMode);         
         if (this.xmlString == null) {
            showMessageDialog("Any changes you have made have been saved.", "Informing You", 300, 100);
            return;
         }				
		 if(this.email.getValue() != "" && this.email.getValue() != undefined) {
			 if(app.emailValidator.validateEmail(this.email.getValue())==false){
					 showMessageDialog("Please enter a valid email address.","Request",300,100);
					return;
			}
		}
		 this.email.value = app.emailValidator.extractEmailAddress(this.email.value);
		if(this.zip.value != undefined && this.zip.value != ""){ 
			if(app.validator.validateZip(this.zip.value) == false) {				
				showMessageDialog("Please enter a valid zip code.","Request", 300, 100);
				return;	
			}
		 }


		this.bName = this.businessName.value;

		this.rInfo = "";
		var a1 = this.address.getValue();
		if( a1 != "" && a1 != undefined) {
			this.rInfo += a1;
		}
		var a2 = this.city.getValue();
		if(a2 != "" && a2 != undefined) {
			this.rInfo += a2 +'  ';
		}
		var a3 = this.state.getValue();
		if(a3 != "" && a3 != undefined) {
			this.rInfo += a3	+'  ';
		}
		var a4 = this.zip.getValue();
		if(a4 != "" && a4 != undefined) {
			this.rInfo += a4 +'  ';
		}
		var a5 = this.website.getValue();
		if(a5 != "" && a5 != undefined) {
			this.rInfo += '<br/>' +'<a href="'+ a5 + '" >'+ a5 + '</a>';			
		}
		this.validate = false;
		this.continueToReview();
	 },
	doSave: function() {
		 var req = new Af.DataRequest(svcURL, this.saveSuccessful.bind(this), this.saveFailed.bind(this), null,
         this.saveTimedOut.bind(this));
         req.addService("WorkspaceService", "saveResource");
		 req.addParameter("resourceType", "commntyResource");
         if (this.obj.UUID != null) {
            req.addParameter("UUID", this.obj.UUID);
         }          
         req.xmlDoc = this.xmlString; 		 
         ajaxEngine.processRequest(req);		
		 this.hide();
	 },

	saveSuccessful: function(response, msg) {				
        if (this.obj.UUID == null) {			 
            this.obj.UUID = response.responseText;
         }		
       
         this.handler.loadCategoryInfo(); 		 
		 if(this.makeResTooBoolean) {
			 this.makeResTooBoolean = false;
			 this.handler.resourceUUID = response.responseText;
			 this.handler.doAddNow();
		 } else{
			//app.thankYouNote.doOpenThankYouDialog(response.responseText);
		 }
		
		 if(this.rvDialog != null) {		
			if(this.rvDialog.isReviewDone) {				 
				this.rvDialog.doSave();
			}
		}
        
      },
      
      saveFailed: function(dataRequest, msg) {
         showMessageDialog(msg + ".<br/>We are currently unable to save this record.  Please try again.", "Excuse us!", 400, 150);
      },
      
      saveTimedOut: function(dataRequest) {
         showMessageDialog("We are currently unable to save this record. <br/> Please try again.", "Excuse us!", 300, 100);
      },
	doMakeThisResToMyResourceToo: function() {
		  if(this.businessName.value == undefined || this.phone.value == undefined) {
		 	 showMessageDialog("Please fill in everything with * by it.<br/> After you click Submit, this resource will also be entered into your My Resources", "More Information Required", 350, 150);
		 } else {
			showMessageDialog("After you click Submit, this resource will also be entered into your My Resources", "Informing You", 300, 120);
		 }
		  this.makeResTooBoolean = true;
	  },

	continueToReview: function() {		
		if(this.validate) {
		  this.doValidate();
		  return
		}
		this.validate = true;		
		if(this.rvDialog == null) {
		   this.rvDialog = new HJ.WriteReview("Write a Review", this, null);
		   this.rvDialog.width = "554px";		 
		}
		this.rvDialog.isCallFromResource = true;
		var obj = new Object();
		this.rvDialog.showHTMLTemplate(basePageURL + "WriteReview.htm", "pageBottom", "writeReviewContent");			
		this.rvDialog.centerDialog(350, 350);
        this.rvDialog.setObj(this.resourceUUID, obj, this.bName, this.rInfo);       
		return false;
	},
	doOpenSeeingStarMainPage: function() {
		app.seeingStar.loadSeeingStarTemplate();
		return false;
	},
	
	doShowPoints:function(){
		app.seeingStar.doLearnAboutPoints();
	},

	doHidePoints:function(){
		app.seeingStar.hideLearnAboutPoints();
	},
	
	doValidateZipCode: function(evt) {
		return app.validator.doValidateZipCode(evt);
	}

});
		
      

