//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2008, 2009 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/**
* @fileOverview This JavaScript file contains functions used by the Gift Registry related pages/features.
*/

//declare the namespace if it does not already exist

if (GRCommonUtilsJS == null || typeof(GRCommonUtilsJS) != "object") {
	var GRCommonUtilsJS = new Object();
}

GRCommonUtilsJS = {

	langId: "-1", /* language of the  store */
	storeId: "", /*numeric unique identifier of the store */
	catalogId: "", /*catalog of the store that is currently in use */

	/**
	 * Sets common parameters used by the services
	 * @param (int) langId The language of the store.
	 * @param (int) storeId The store currently in use.
	 * @param (int) catalogId The catalog of the store currently in use.
	 */
	setCommonParameters:function(langId,storeId,catalogId){
			this.langId = langId;
			this.storeId = storeId;
			this.catalogId = catalogId;
	},
	
	 /**
     * This function is used to highlight the selected item in a drop down box. 
     * @param (Element) dropDown  : The drop down box to be used.
     * @param (String) value  : The value which has to be highlighted.
     */
	selectDropDown : function(dropDown, value) 
	{
		if (value != "") {
			for (var i = 0, len = dropDown.options.length; i < len; i++) {
				if (dropDown.options[i].value == value) {
					dropDown.options.selectedIndex = i;
					break;
				}
			}
		} 
	},
	/**
	 * This function updates the input associative array params with the input key-value pair. 
	 * If the toArray value is true, this function creates an associative array for duplicate entries; otherwise it overwrites the existing array.
	 * The function is used for updating input parameters before passing them to a service call.
	 *
	 * @param {Array} params The associative array to update.
	 * @param {String} key The key to search for in the array.
	 * @param {String} value The new value to update with when the key has been found in the array.
	 * @param {Boolean} toArray If the value is true, then the function creates a new array for duplicate entries. If the value is false, no new array will be created, the existing array will be overwritten.
	 * @param {Integer} index The index in the array in which the value should be updated.
	 *
	 * @returns {Array} params The updated associative array.
	 */
	updateParamObject:function(params, key, value, toArray, index){
		if(params == null){
			params = [];
		}
		
		if(params[key] != null && toArray){
			if(dojo.lang.isArrayLike(params[key])){
				//3rd time onwards
				if(index != null && index != ""){
					//overwrite the old value at specified index
					params[key][index] = value;
				}else{
					params[key].push(value);
				}
			}else{
				//2nd time
				var tmpValue = params[key];
				params[key] = [];
				params[key].push(tmpValue);
				params[key].push(value);
			}
		}else{
			//1st time
			if(index != null && index != "" && index != -1){
				//overwrite the old value at specified index
				params[key+"_"+index] = value;
			}else if(index == -1){
				var i = 1;
				while(params[key + "_" + i] != null){
					i++;
				}
				params[key + "_" + i] = value;
			}else{
				params[key] = value;
			}
		}
		return params;
	}
	
}

