/* This file requires:

/js/utilities.js
/js/ajax/net.js
/js/dhtml/display.js
/js/forms/script_tmt_validator.js

*/

// IE 6.0 doesn't supports the const keyword, so we store constants inside a global object
act2Const = new Object();
act2Const.LAYER_SUFFIX = "Layer";
act2Const.THANK_LAYER_SUFFIX = "LayerThanks";
act2Const.LABEL_CLASS = "act2Label";
act2Const.LABEL_SELECTED_CLASS = "act2LabelSelected";
act2Const.LABEL_STARS_CLASS = "act2Stars";
act2Const.CLOSE_CLASS = "act2CloseLayer";
act2Const.RATE_URL = "/customcf/ajax/act2rating.cfm";
act2Const.RECOMMEND_URL = "/customcf/ajax/act2recommend.cfm";
act2Const.STAR_HTML = '<img src="/images/act2/rate_selected.gif" />';
act2Const.ERROR_MSG = "Unable to fullfill your request";

// This object act as a container for all the tools
function act2Constructor(){
	
	this.tools = new Array();
	
	var rateLabel = document.getElementById("act2Rate");
	if(rateLabel){
		this.tools.push(new act2RateTool(rateLabel, this));
	}
	var reccomendLabel = document.getElementById("act2Reccommend");
	if(reccomendLabel){
		this.tools.push(new act2ReccomendTool(reccomendLabel, this));
	}
	// Close all the tools
	this.closeAll = function(){
		for(var i=0; i<this.tools.length; i++){
			this.tools[i].closeTool();
		}
	}
	
}

// Abstract tool object, implements shared functionalities
function abstractAct2Tool(label, containerObj){
	this.container = containerObj;
	this.labelNode = label;
	this.layerNode = document.getElementById(label.id + act2Const.LAYER_SUFFIX);;
	this.thankLayerNode = document.getElementById(label.id + act2Const.THANK_LAYER_SUFFIX);;
	this.formNode = this.layerNode.getElementsByTagName("form")[0];
	this.submitButton = tmt_getSubmitNodes(this.layerNode)[0];
	// Store the currently active layer
	this.activeLayer = this.layerNode;
	// Get the "close" links
	var layerLinks = this.layerNode.getElementsByTagName("a");
	var thankLinks = this.thankLayerNode.getElementsByTagName("a");
	this.layerClose = getNodesByClass(layerLinks, act2Const.CLOSE_CLASS)[0];
	this.thankClose = getNodesByClass(thankLinks, act2Const.CLOSE_CLASS)[0];

	// Open first layer
	this.openLayer = function(){
		// First close anything that may be open
		this.container.closeAll();
		this.labelNode.className = act2Const.LABEL_SELECTED_CLASS;
		this.layerNode.style.display = "block";
		// Set the currently active layer
		this.activeLayer = this.layerNode;
	}
	// Open thankyou layer
	this.openThankLayer = function(){
		this.layerNode.style.display = "none";
		this.thankLayerNode.style.display = "block";
		this.activeLayer = this.thankLayerNode;		
	}
	// Close the active layer
	// This method would be better called "close", but it's a reserved word :-(
	this.closeTool = function(){
		this.labelNode.className = act2Const.LABEL_CLASS;
		this.activeLayer.style.display = "none";
	}
	
	this.displayError = function(){
		alert(act2Const.ERROR_MSG);	
	}
		
	// Assign references "hook" to "slave" objects
	this.layerClose.tool = this;
	this.thankClose.tool = this;
	this.labelNode.tool = this;
	
	// Assign events to slave objects
	this.layerClose.onclick = function(){
		this.tool.closeTool();
	}
	this.thankClose.onclick = function(){
		this.tool.closeTool();
	}
	this.labelNode.onclick = function(){
		this.tool.openLayer();
	}
}

function act2RateTool(labelNode, containerObj){
	// Extend the abstract object
	var obj = new abstractAct2Tool(labelNode, containerObj);
	obj.stars = 0;
	// The label is contained inside a <div> tag
	obj.labelContainer = obj.labelNode.parentNode;
	
	// Overwrite the base object since we need to display stars and handle soome logic
	obj.closeTool = function(){
		if(obj.activeLayer == obj.thankLayerNode){
			// Done with rating
			obj.labelContainer.className = act2Const.LABEL_STARS_CLASS;
			obj.activeLayer.style.display = "none";
		}
		else{
			// aborting rate operation
			obj.labelNode.className = act2Const.LABEL_CLASS;
			obj.activeLayer.style.display = "none";
		}
	}
	obj.submitButton.onclick = function(){
		var params = obj.collectParams();
		// AJAX's HTTP call
		new net.ContentLoader(act2Const.RATE_URL, obj.thankUser, obj.displayError, "POST", params);
		return false;
	}
	obj.thankUser = function(){
		obj.openThankLayer();
		obj.displayStars();
	}
	obj.collectParams = function(){
		var checkedRadio = getCheckedRadio(obj.formNode.elements["act2Rating"]);
		// Store the rating
		obj.stars = parseInt(checkedRadio.value);
		var str = "";
		str += "page=" + document.getElementById("act2RatingURL").value;
		str += "&"
		str += "comments=" + document.getElementById("act2RatingComments").value;
		str += "&"
		str += "rating=" + checkedRadio.value;
		return str;
	}
	obj.displayStars = function(){
		var starsHTML = "";
		for(var i=0; i<obj.stars; i++){
			starsHTML += act2Const.STAR_HTML;
		}
		// Print the stars and swap CSS
		obj.labelContainer.className = act2Const.LABEL_SELECTED_CLASS + " " + act2Const.LABEL_STARS_CLASS;
		obj.labelContainer.innerHTML = starsHTML;
	}
	return obj;
}

function act2ReccomendTool(labelNode, containerObj){
	// Extend the abstract object
	var obj = new abstractAct2Tool(labelNode, containerObj);
	
	obj.submitButton.onclick = function(){
		// Check if input is correct invoking TMT Validator
		var isValid = tmt_validateForm(obj.formNode);
		if(isValid){
			var params = obj.collectParams();
			// AJAX's HTTP call
			new net.ContentLoader(act2Const.RECOMMEND_URL, obj.thankUser, obj.displayError, "POST", params);
		}
		return false;
	}
	obj.thankUser = function(){
		obj.openThankLayer();
	}
	obj.collectParams = function(){
		var str = "";
		str += "fromFirstName=" + document.getElementById("act2ReccommendFromFirstName").value;
		str += "&"
		str += "fromLastName=" + document.getElementById("act2ReccommendFromLastName").value;
		str += "&"
		str += "fromEmail=" + document.getElementById("act2ReccommendFromEmail").value;
		str += "&"
		str += "toFirstName=" + document.getElementById("act2ReccommendToFirstName").value;
		str += "&"
		str += "toLastName=" + document.getElementById("act2ReccommendToLastName").value;
		str += "&"
		str += "toEmail=" + document.getElementById("act2ReccommendToEmail").value;
		str += "&"		
		str += "page=" + document.getElementById("act2ReccommendURL").value;
		str += "&"
		str += "message=" + document.getElementById("act2ReccommendMessage").value;
		str += "&"
		return str;
	}
	return obj;
}

addEvent(window, "load", act2Constructor);
