/**
 * 
 * Javascript file for client side javascript swampdonkey functionality
 * @requires cmc common to be defined before this javascript file.
 * @author pmander
 */

//package
cmc.swampdonkey = {};

var SD_FORM = "sdForm";

/**
 * -------------------------------- DoubleOptionsList -------------------------
 */

/**
 * Javascript client for DoubleOptionsListTag
 */
cmc.swampdonkey.DoubleOptionsList = function()
{
	this.DEFAULT_JSONRPC_PATH = "/JSON-RPC";
	
	this.jsonrpc = null;
	this.field = null;
	this.dropdown1 = null;
	this.dropdown2 = null;
	
	this.fieldName = null;
};

/**
 * Initialises the AJAX stuff and sets up events on the options lists
 */
cmc.swampdonkey.DoubleOptionsList.prototype.init = function(fieldName, jsonrpcPath) 
{	
	if(!jsonrpcPath)
	{
		jsonrpcPath = this.DEFAULT_JSONRPC_PATH;
	}
	
	this.jsonrpc = new JSONRpcClient(jsonrpcPath);
	
	this.fieldName = fieldName;
	this.field;
	this.dropdown1 = document.getElementById(fieldName + "_1");
	this.dropdown2 = document.getElementById(fieldName + "_2");
	
	//decide whether second dropdown should be disabled
	if(this.dropdown1.selectedIndex > 1)
	{
		this.dropdown2.removeAttribute(ATT_DISABLED);
	}
	else
	{
		this.dropdown2.setAttribute(ATT_DISABLED, "disabled");
	}
	
	if(this.dropdown2.options.length <= 2)
	{
		this.populateSecondDropdown();
	}
	
	//set up events
	var dropdown = this;
	
	//populate second dropdown when first changes
	document.getElementById(fieldName + "_1").onchange = function() {
		dropdown.setValue();
	    dropdown.populateSecondDropdown();
	};
	//update hidden value when second dropdown changes
	document.getElementById(fieldName + "_2").onchange = function() {
	    dropdown.setValue();
	};
	
};

/**
 * Uses AJAX to populate the second dropdown, based on a linking key
 * @param linkingKey The key used to decide what to populate the second drop down with
 */
cmc.swampdonkey.DoubleOptionsList.prototype.populateSecondDropdown = function(linkingKey) 
{	
	var firstValue = this.dropdown1.value;
	var jsonRpcObject = eval("this.jsonrpc." + this.fieldName);
	var that = this;	
	jsonRpcObject.getLinkedOptions(function(response, exception) {
	
	//callback function	
		
		//b0rk
		if(exception)
		{
			that.setValue(this.dropdown1.value, "error");
			throw new cmc.swampdonkey.SwampDonkeyException(exception.name, exception.code, exception.message);
		}
	
		var optionsMap = response.map;
			
		//clear current options
		while(that.dropdown2.options.length > 2)
		{
			that.dropdown2.remove(doubleDropDown.dropdown2.options.length - 1);
		}
		
		//sort the list. 
		//(this is a hack. we cannot currently maintain the order of lists from the bundle)
		var optionsList = new Array();
		for(var key in optionsMap)
		{
			optionsList.push(optionsMap[key]);
		}
		optionsList.sort();
		
		//add new ones
		for(var i = 0; i < optionsList.length; i++)
		{
			var option = document.createElement(ELEMENT_OPTION);
			option.setAttribute(ATT_VALUE, optionsList[i]);
			option.appendChild(document.createTextNode(optionsList[i]));
			that.dropdown2.appendChild(option);
		}
			
		//decide whether second dropdown should be disabled or not
		if(that.dropdown1.selectedIndex > 1 && that.dropdown2.options.length > 2)
		{
			that.dropdown2.removeAttribute(ATT_DISABLED);
		}
		else
		{
			that.dropdown2.setAttribute(ATT_DISABLED, "disabled");
		}
	}
	, firstValue);

};

/**
 * Sets a hidden value comprised of the values of the first and second dropdowns
 * 
 */
cmc.swampdonkey.DoubleOptionsList.prototype.setValue = function() 
{	
	
	var hiddenField;

	//if the hidden input does not exist, add it
	//the hidden field is generated in the javascript rather than the custom tag,
	//because we cannot guarantee javascript disabled support will work if that we do that.
	if(this.field)
	{
		hiddenField = this.field;
	}
	else
	{
		var sdForm = document.getElementById(SD_FORM);
		
		var hiddenField = document.createElement("input");
	    hiddenField.setAttribute(ATT_TYPE, "hidden");
		hiddenField.setAttribute(ATT_NAME, this.fieldName);
		hiddenField.setAttribute(ATT_ID, this.fieldName);
		sdForm.appendChild(hiddenField);
		this.field = hiddenField;
	}

	var hiddenValue;
	
	if(arguments.length == 0)
	{
		hiddenValue = this.dropdown1.value + "_" + this.dropdown2.value;
		hiddenField.setAttribute(ATT_VALUE, hiddenValue);
	}
	else
	{
		hiddenValue = arguments[0] + "_" + arguments[1];
		hiddenField.setAttribute(ATT_VALUE, hiddenValue);
	}
	
	
};

/**
 * ----------------------------- Swampdonkey Exception ------------------------
 */

/**
 *
 */
cmc.swampdonkey.SwampDonkeyException = function(name, code, message)
{
	alert("an exception occured");
};