HJ.WriteReview = Class.create();

HJ.WriteReview.prototype = Object.extend(new Af.HtmlDialog(), {
      
	initialize: function(title, handler, content) {
		this._initializeDialog(title, handler, content);
		this.handler = handler;
		this.obj = null; 		
		this.rUUID = null;
		this.infoAndRank = null;
		this.rankPoint = "0";
		this.isReviewDone = false;
		this.isCallFromResource = false;
	},
  
	templateLoaded: function() {		  

		   this.resBusinessName = document.getElementById("resBusinessName");
		   this.resResourceInfo = document.getElementById("resResourceInfo");
		   this.wRvToSeeingStarPage = document.getElementById("wRvToSeeingStarPage");
		   this.wRvToSeeingStarPage.onclick = this.doShowPoints.bind(this);
		  // this.wRvToSeeingStarPage.onmouseover = this.doShowPoints.bind(this);
		   //this.wRvToSeeingStarPage.onmouseout = this.doHidePoints.bind(this);
		   
		   this.rankStatus = document.getElementById("rankStatus");		   

		   this.firstStar = document.getElementById("firstStar");		   
		   this.secondStar = document.getElementById("secondStar");		   
		   this.thirdStar = document.getElementById("thirdStar");		   
		   this.fourthStar = document.getElementById("fourthStar");		   
		   this.fifthStar = document.getElementById("fifthStar");		   

		   this.firstStar.onmouseover = this.doFirstStar.bind(this);
		   this.secondStar.onmouseover = this.doSecondStar.bind(this);
		   this.thirdStar.onmouseover = this.doThirdStar.bind(this);
		   this.fourthStar.onmouseover = this.doFourthStar.bind(this);
		   this.fifthStar.onmouseover = this.doFifthStar.bind(this);
						  
		   this.reviewComments = document.getElementById("reviewComments");
		   
		   this.saveReview = document.getElementById("saveReview");
		   this.armedButton = this.saveReview;
		   this.dontSaveReview = document.getElementById("dontSaveReview");				 	   		 
		   this.saveReview.onclick = this.doSaveReview.bind(this);
		   this.dontSaveReview.onclick = this.doDontSaveReview.bind(this);	

		   this.setInformation();        
	},

	setObj: function(resourceUUID, obj, bName, resInfo) {
			this.obj = obj;			
			this.rUUID = resourceUUID; 				
			this.bName = bName;
			this.resInfo = resInfo;
			this.setInformation();			
	},
	setInformation: function() {
			if(this.resBusinessName != null && this.bName != null) {
				this.resBusinessName.innerHTML = this.bName;
			}
			if(this.resResourceInfo != null && this.resInfo != null) {
				this.resResourceInfo.innerHTML = this.resInfo;
			}
			if(this.reviewComments != null) {
				this.reviewComments.focus();
			}
	},
	
	doSaveReview: function() { 
	/*	var comntText = trim(this.reviewComments.value);				
		 if(comntText== "") {
		     showMessageDialog("Please write comments  ", "Error ! Empty comments", 300, 100);
			 return;
		 }		*/	 
		if(this.rankPoint == "0") {
			showMessageDialog("Please select a star-rating for your resource.  To do this, mouse-over the"+
							" stars to see the ratings for each.<br/>  Click on a star to select the rating.", "Request", 350, 150);
			return;
		}
		this.isReviewDone = true;
		this.hide();
		if(this.isCallFromResource)	{
			this.handler.doSave();
		} else {
			this.doSave();
		}
		return;
	},
	doSave: function() {
		
		this.isReviewDone = false;
	      var s = "<messages>";
		  s += "<comment>" + xmlEncode(trim(this.reviewComments.value)) + "</comment>";		 
		  s += "</messages>";		 		 
						
		var req = new Af.DataRequest(svcURL,this.reqSaveReviewDetailCompleted.bind(this), this.reqFailed.bind(this), null, this.reqTimedOut.bind(this));		   
		req.addService("CommunityResourceService", "saveReviewDetails");		   			
		if(this.rUUID != null && this.rUUID != undefined) {
			req.addParameter("UUID", this.rUUID);
		} else {
			req.addParameter("UUID", this.handler.obj.UUID);
		}						
		req.addParameter("rankPoint", this.rankPoint);		

		req.xmlDoc = s;
		 
		ajaxEngine.processRequest(req);					 		
		this.cleanData();
	},
	doDontSaveReview: function() {
		this.cleanData();
		this.hide();
		this.handler.validate = true;
	},
	reqSaveReviewDetailCompleted: function(response) {			
			if(!this.isCallFromResource)	{
				app.thankYouNote.doOpenThankYouDialog(response.responseText);
				this.handler.doShowReviewDetail();	
				this.handler.loadRecentAdditions();
				this.handler.doResourceSortBy();
			}
			this.isCallFromResource = false;
			app.thankYouNote.doOpenThankYouDialog(response.responseText);
			
	},
	reqFailed: function(dataRequest, msg) {
		    showMessageDialog("We are currently unable to save this review.  Please try again.", "Excuse us!", 300, 100);
	},

	reqTimedOut: function(dataRequest) {
		    showMessageDialog("We are currently unable to save this review.  Please try again." , "Excuse us!", 300, 100);
	},

	doFirstStar: function() {		
		this.firstStar.src = "../hj/img/colorstar.gif";

		this.secondStar.src = "../hj/img/blackstar.gif";
		this.thirdStar.src = "../hj/img/blackstar.gif";
		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Ugh !";
		this.rankPoint = "1";
	},

	doSecondStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
				
		this.thirdStar.src = "../hj/img/blackstar.gif";
		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Poor ";
		this.rankPoint = "2";
	},

	doThirdStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";

		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Average ";
		this.rankPoint = "3";	
	},

	doFourthStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";
		this.fourthStar.src = "../hj/img/colorstar.gif";

		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Very Good ";
		this.rankPoint = "4";	

	},

	doFifthStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";
		this.fourthStar.src = "../hj/img/colorstar.gif";
		this.fifthStar.src = "../hj/img/colorstar.gif";
		this.rankStatus.innerHTML = "Wow ! ";
		this.rankPoint = "5";
	},
	cleanData: function() {	   
	   this.firstStar.src = "../hj/img/blackstar.gif";
	   this.secondStar.src = "../hj/img/blackstar.gif";
	   this.thirdStar.src = "../hj/img/blackstar.gif";
	   this.fourthStar.src = "../hj/img/blackstar.gif";
	   this.fifthStar.src = "../hj/img/blackstar.gif";
	   this.rankStatus.innerHTML = "Ugh !";
	   this.reviewComments.value = "";

	},
	doOpenSeeingStarMainPage: function() {
		app.seeingStar.loadSeeingStarTemplate();
		return false;
	},

	doShowPoints:function(){
		app.seeingStar.doLearnAboutPoints();
	},

	doHidePoints:function(){
		app.seeingStar.hideLearnAboutPoints();
	},
	hideSelectBoxes: function() {
          if (is_ie && !is_ie7 && this.handler != null && this.handler.category != null) {
              this.handler.category.element.style.visibility = "hidden";
		  }
	  },
	  
	  showSelectBoxes: function() {
	      if (is_ie && !is_ie7 && this.handler != null && this.handler.category != null) {
              this.handler.category.element.style.visibility = "visible";
		  }
	  }
});
