/*****************************************************************************/
/**  Static Properties Section.  *********************************************/
/*****************************************************************************/

/** Third Party's kind constants : CENTRAL THIRD. */
var THIRD_KIND_CENTRAL = 15;
/** Third Party's kind constants : STORE THIRD. */
var THIRD_KIND_STORE = 7;
/** Third Party's kind constants : SITE THIRD. */
var THIRD_KIND_SITE = -1;

/** Switches's Array. Used to store all loaded switch
 *  The Array Index Key is '#NAME#THIRD_KIND#THIRD_NUMBER#THIRD_SUB_NUMBER#LANGUAGE#' String
 */
var SWITCH_ARRAY = new Array();

/*****************************************************************************/
/**  Javascript Object Definition Section  ***********************************/
/*****************************************************************************/

/** Switch Object Definition. */
function SwitchElement(_name, _kind, _number, _subNumber, _language, _state) {
	/* Standard Properties */

	/* Switch Name. */
	this.name = _name;

	/* Third Party's Kind. */
	this.kind = _kind;
	/* Third Party's Number. */
	this.number = _number;
	/* Third Party's Sub Number. */
	this.subNumber = _subNumber;

	/* Switch Language. */
	this.language = _language;

	/* Switch State Value. */
	this.state = _state;

	/* toString Method. Used to retrieve a literal value 
		according to switch content.*/
	this.toString = _toStringSwitch;
}

/**
 * This method was used to retrieve the third party literal string.
 * return .: 'Switch(kind - number - sub number) : bookable - online.
 */
function _toStringSwitch() {
	with(this) {
		return 'Switch(' + name + ' - ' + kind + ' - ' + number + ' - '
		 + subNumber + ' - ' + language + ') : ' +  state ;
	}
}

/*****************************************************************************/
/**  Switch Array Static Mangagement Section  ********************************/
/*****************************************************************************/

/**
 * This method was used to retrieve the switch index key.
 * param _oSwitch .: the switch
 * return .........: the switch index key.
 *                   Null if name is null.
 * see ............: getSwitchIndexKeyByKey(_name, _kind, _number, _subNumber, _language) : String
 */
function getSwitchIndexKey(_oSwitch) {
	if (_oSwitch == null) {
		return null;
	}
	return getSwitchIndexKeyByKey(_oSwitch.name, _oSwitch.kind, _oSwitch.number, _oSwitch.subNumber, _oSwitch.language);
}

/**
 * This method was used to retrieve the switch index key.
 * _name ......: the switch name
 * _kind ......: the switch's third party's kind
 * _number ....: the switch's third party's number
 * _subNumber .: the switch's third party's sub number
 * _language ..: the switch's language
 * return .....: the switch index key.
 *               Null if name is null.
 */
function getSwitchIndexKeyByKey(_name, _kind, _number, _subNumber, _language) {
	if (_name == null) {
		return null;
	}
	return '#' + _name + '#' + _kind + '#' + _number + '#' + _subNumber + '#' + _language + '#';
}

/**
 * This method was used to add a switch in switch static array.
 * _oSwitch .: the switch to add
 * return ...: true if added.
 *             false if name is null.
 */
function addSwitch(_oSwitch) {
	var sIndexKey = null;

	/* Retrieve Switch Index Key. */
	sIndexKey = getSwitchIndexKey(_oSwitch);
	
	/* Check key, if it's null : switch or name is null.*/
	if (sIndexKey == null) {
		return false;
	}

	/* Check that child does not allready exists. */
	if(SWITCH_ARRAY[sIndexKey] == null) {
		SWITCH_ARRAY.push(sIndexKey);
	}
	SWITCH_ARRAY[sIndexKey] = _oSwitch;
	return true;
}

/**
 * This method was used to retrieve a switch in switch static array.
 * _name ......: the switch name
 * _kind ......: the switch's third party's kind
 * _number ....: the switch's third party's number
 * _subNumber .: the switch's third party's sub number
 * _language ..: the switch's language
 * return .....: Switch.
 *               Null if name is null.
 *               Null if Switch not found in static array.
 */
function getSwitch(_name, _kind, _number, _subNumber, _language) {
	var sIndexKey = null;

	/* Retrieve Switch Index Key. */
	sIndexKey = getSwitchIndexKeyByKey(_name, _kind, _number, _subNumber, _language);
	/* Check key, if it's null : name is null.*/
	if (sIndexKey == null) {
		return null;
	}

	/* Check Switch exists in Array. */
	if(SWITCH_ARRAY[sIndexKey] == null) {
		return null;
	}
	/* Retrieve & return switch. */
	return SWITCH_ARRAY[sIndexKey];
}

/**
 * This method was used to retrieve the switch list.
 * return .....: Switch List.
 */
function getSwitchList() {
	return SWITCH_ARRAY;
}

/**
 * This method was used to know if switch is enable
 * _name ......: the switch name
 * _kind ......: the switch's third party's kind
 * _number ....: the switch's third party's number
 * _subNumber .: the switch's third party's sub number
 * _language ..: the switch's language
 * return .....: Switch State.
 *               False if Switch not found.
 * see ........: getSwitch(_name, _kind, _number, _subNumber, _language) : SwitchElement
 */
function isEnable(_name, _kind, _number, _subNumber, _language) {
	var oSwitch = getSwitch(_name, _kind, _number, _subNumber, _language);
	if (oSwitch == null) {
		return false;
	}
	return oSwitch.state;
}

/*****************************************************************************/
/**  Dom to Javascript Object Section  ***************************************/
/*****************************************************************************/

var XML_SWITCH_LIST = 'switches';

var XML_SWITCH = 'switch';

var XML_SWITCH_LANGUAGE = 'language';
var XML_SWITCH_STATE = 'state';
var XML_SWITCH_NAME = 'name';

var XML_THIRD_PARTY = 'third';

var XML_THIRD_PARTY_KIND = 'kind';
var XML_THIRD_PARTY_NUMBER = 'number';
var XML_THIRD_PARTY_SUB_NUMBER = 'sub-number';

/**
 * This method was used to retrieve a switch from Xml Switch Dom Element.
 * param _oXmlSwitch .: the switch Xml Dom Element
 * return ............: The new switch instance
 *                      Null if xml dom element  name is null.
 *                      Null in case of exception.
 */
function getDomSwitch(_oXmlSwitch) {
	var bState = false;
	var oSwitch = null;

	var _kind = null;
	var _number = null;
	var _subNumber = null;

	try {
		if (_oXmlSwitch == null) {
			return null;
		}

		/* Retrieve name. */
		var _name = getAttributeValue(_oXmlSwitch, XML_SWITCH_NAME);		
		/* Retrieve Language. */
		var _language = getChildNodeValue(_oXmlSwitch, XML_SWITCH_LANGUAGE);
		/* Retrieve State. */
		var _state = getChildNodeValue(_oXmlSwitch, XML_SWITCH_STATE);

		/* Manage Child Node - Third */
		var oNodes = _oXmlSwitch.getElementsByTagName(XML_THIRD_PARTY);
		if (oNodes.length > 0) {
			_kind = getAttributeValue(oNodes[0], XML_THIRD_PARTY_KIND);
			_number = getAttributeValue(oNodes[0], XML_THIRD_PARTY_NUMBER);
			_subNumber = getAttributeValue(oNodes[0], XML_THIRD_PARTY_SUB_NUMBER);
		}

		if (_name == null) {
			return null;
		}

		if (_state == null) {
			bState = false;
		} else {
			bState = (_state == 'true');
		} 
		oSwitch = new SwitchElement(_name, _kind, _number, _subNumber, _language, bState);
		return oSwitch;

	} catch(e) {
		return null;
	}
}

/*****************************************************************************/
/**  Service Section  ********************************************************/
/*****************************************************************************/

/** Url Base. */
var SWITCH_BASE_URL = null;

/**
 * This method was used to init switch service.
 * param _baseUrl .: the url base.
 */
function _initSwitchService(_baseUrl) {
	SWITCH_BASE_URL = _baseUrl;
}

/**
 * This method was used to retrieve the switch service url.
 * Third Party 1 is mandatory, Third party 2 is optionnal.
 * param _client .....: the client
 * param _kind1 ......: the first third party's kind
 * param _number1 ....: the first third party's number
 * param _subNumber1 .: the first third party's sub number
 * param _kind2 ......: the second third party's kind
 * param _number2 ....: the second third party's number
 * param _subNumber2 .: the second third party's sub number
 * return ............: Url, null if init url base, client, first third kind, number & sub number is null.
 */
function getSwitchUrl(_client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2) {
	if (SWITCH_BASE_URL == null) {
		return null;
	}
	if (_client == null) {
		return null;
	}
	if (_kind1 == null) {
		return null;
	}
	if (_number1 == null) {
		return null;
	}
	if (_subNumber1 == null) {
		return null;
	}

	return SWITCH_BASE_URL + "/service/getSwitches.do?serviceClient=" + _client
		+ "&thirdKind1=" + _kind1
		+ "&thirdNumber1=" + _number1
		+ "&thirdSubNumber1=" + _subNumber1
		+ "&thirdKind2=" + _kind2
		+ "&thirdNumber2=" + _number2
		+ "&thirdSubNumber2=" + _subNumber2;
}

/**
 * This method was used to load switches for a tuple of third party.
 * Third Party 1 is mandatory, Third party 2 is optionnal.
 * param _client .....: the client
 * param _kind1 ......: the first third party's kind
 * param _number1 ....: the first third party's number
 * param _subNumber1 .: the first third party's sub number
 * param _kind2 ......: the second third party's kind
 * param _number2 ....: the second third party's number
 * param _subNumber2 .: the second third party's sub number
 * param _localFunc ..: the function call after load.
 *                      The function must have this kind of prototype:
 *                      _localFunc(httpStatus, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2)
 */
function _loadSwitches(_client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2, _localFunc) {
	var sUrl = null;
	var i = 0;

	sUrl = getSwitchUrl(_client, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2);
	if (sUrl == null) {
		return false;
	}

	var func = function (xmlHttp, httpStatus) {
		oGlobalXml = xmlHttp.responseXML;
		if(oGlobalXml!=null) {
			var oSwitchesNode = oGlobalXml.getElementsByTagName(XML_SWITCH_LIST);
			if (oSwitchesNode != null && oSwitchesNode.length > 0) {
				var oNodes = oSwitchesNode[0].getElementsByTagName(XML_SWITCH);
				
				for (i = 0; i < oNodes.length; i++) {
					var oSwitch = getDomSwitch(oNodes[i]);
					
					if (oSwitch != null) {
						addSwitch(oSwitch);
					}
				}
			}
		}
		if (_localFunc != null) {
			try {
				_localFunc(httpStatus, _kind1, _number1, _subNumber1, _kind2, _number2, _subNumber2);
			} catch (e) {
				return false;
			}
		}
		return true;
	};
	
	var oProcess = new IAjaxProcess(sUrl, func);
	oProcess.run();
	return true;
}