
// PCLN-specific AutoComplete
// Modified from Zapatec AutoComplete.


Zapatec.PclnAutoComplete = function(objArgs) {
  // Call constructor of superclass
  Zapatec.PclnAutoComplete.SUPERconstructor.call(this, objArgs);
  this.searchCache = new Object();
  //debugPrint("Zapatec.PclnAutoComplete constructor: this.config.searchOnCharacterCount: " + this.config.searchOnCharacterCount);

};

// Inherit Zapatec.PclnAutoComplete
//alert("Zapatec.PclnAutoComplete" + "\r\n" + Zapatec.PclnAutoComplete);
Zapatec.inherit(Zapatec.PclnAutoComplete, Zapatec.Widget);
//Zapatec.inherit(Zapatec.PclnAutoComplete, Zapatec.AutoComplete);


Zapatec.PclnAutoComplete.prototype.reconfigure = function(objArgs) {
  // Call parent method
  Zapatec.PclnAutoComplete.SUPERclass.reconfigure.call(this, objArgs);
  // Redraw or do something else
  // ...
};


Zapatec.PclnAutoComplete.prototype.callbackSource = function(oArgs) {
	//Updated by CRK
	var strNewFields = "";
	if(oArgs.gpc != null && oArgs.gpc != "") {
	      strNewFields += "&gpc=" + oArgs.gpc;
	}
	if(oArgs.orig_dest != null && oArgs.orig_dest != "") {
	      strNewFields += "&orig_dest=" + oArgs.orig_dest;
	}
	//Updated by MLi
	if(oArgs.offerMethod != null && oArgs.offerMethod != "") {
	      strNewFields += "&offerMethod=" + oArgs.offerMethod;
	}
	var strURL = oArgs.searchBaseURL 
		+ "?pid=" + oArgs.productID
		+ "&ss=" + escape(oArgs.keyword) 
		+ "&type=" + oArgs.searchType
		+  strNewFields
		+ "&visitID=" + oArgs.visitID 
		+ "&fieldDescription=" + escape(oArgs.fieldDescription);

	return {
		source: strURL,
		sourceType: 'json/url'
	};
};




Zapatec.PclnAutoComplete.prototype.configure = function(objArgs) {
  // Overwrite default config options if needed
  // Define new config options
  this.defineConfigOption('fields');
  this.defineConfigOption('dataOnDemand', false);
  this.defineConfigOption('convertTip');
  this.defineConfigOption('width');
  this.defineConfigOption('height');
  this.defineConfigOption('overflow', 'hidden');

  // MG added
  this.defineConfigOption('prepopFirstItemOnBlur');
  this.defineConfigOption('convertTipToTextInput');
  this.defineConfigOption('getTipFilterString');
  
  this.defineConfigOption('productID');
  this.defineConfigOption('searchType');
  //Added by CRK starts
  this.defineConfigOption('orig_dest');
  this.defineConfigOption('gpc');
  //Added by CRK ends
  //added by MLi starts
  this.defineConfigOption('offerMethod');
  //added by MLi ends
  this.defineConfigOption('searchBaseURL');
  
  this.defineConfigOption('searchOnCharacterCount');
  this.defineConfigOption('maxListItems');

  this.defineConfigOption('prepareAutoCompleteStringFunc');
  this.defineConfigOption('cancelAutoCompleteCallFunc');

  if(objArgs["callbackSource"] != null && objArgs["callbackSource"] != "undefined") {
  	// custom
  } else {
  	objArgs["callbackSource"] = this.callbackSource;
  }

  //for (var strOption in objArgs) {
  //	debugPrint(strOption + " -- " + objArgs[strOption]);
  //}
  
  this.config.visitID = "no_vid";
  try{
  	this.config.visitID = csaGetVisitID();
  } catch(e) {
  	//alert("test ERROR");
  }
  
  // Call parent method
  Zapatec.PclnAutoComplete.SUPERclass.configure.call(this, objArgs);
  // Check passed config options and correct them if needed
  // By default dimensions are in pixels
  if (this.config.width &&
   this.config.width == parseInt(this.config.width)) {
    this.config.width += 'px';
  }
  //this.config.height = 200;
  if (this.config.height &&
   this.config.height == parseInt(this.config.height)) {
    this.config.height += 'px';
  }

  this.fieldDescriptions = new Object();

  // Add fields
  if (this.config.fields instanceof Array) {
    for (var iField = 0; iField < this.config.fields.length; iField++) {
      this.addField(this.config.fields[iField]);
    }
  }
  this.field = null;
  this.tip = null;
  this.data = null;
  this.cache = {};


  this.defSelIDX = -1;
  
  this.curSelIDX = this.defSelIDX;


};



Zapatec.PclnAutoComplete.prototype.addField = function(objField) {

  var strFieldID = "";
  var strFieldDescription = "";
  
  if (typeof objField == 'string') {
  	if(objField.length > 2 && objField.lastIndexOf("~") > -1) {
  		strFieldDescription = objField.substr(objField.lastIndexOf("~")+1);
  		objField = String(objField.substr(0, objField.lastIndexOf("~")));
  	} else {
		strFieldDescription = objField;
  	}
	
	strFieldID = String(objField);
  } else {
  	if(objField != null && objField.id != null && String(objField.id) != "undefined") {
  		strFieldID = String(objField.id);
  	}
  }


  // Get field object
  objField = Zapatec.Widget.getElementById(objField);
  if (!objField || typeof objField.value == 'undefined') {
    return;
  }

  this.fieldDescriptions[strFieldID] = strFieldDescription;


  // Attach
  objField.zpAutoCompleteId = this.id;
  // Turn browser autocomplete feature off
  objField.setAttribute('autocomplete', 'off');
  // Add event listeners
  Zapatec.Utils.addEvent(objField, 'focus', Zapatec.PclnAutoComplete.onFocus);
  Zapatec.Utils.addEvent(objField, 'keydown', Zapatec.PclnAutoComplete.onKeyDown);
  Zapatec.Utils.addEvent(objField, 'keyup', Zapatec.PclnAutoComplete.onKeyUp);
  Zapatec.Utils.addEvent(objField, 'blur', Zapatec.PclnAutoComplete.onBlur);
};



Zapatec.PclnAutoComplete.prototype.removeField = function(objField) {
  // Get field object
  objField = Zapatec.Widget.getElementById(objField);
  if (!objField) {
    return;
  }
  // Detach
  var undef;
  objField.zpAutoCompleteId = undef;
  // Turn browser autocomplete feature on
  objField.removeAttribute('autocomplete');
  // Remove event listeners
  Zapatec.Utils.removeEvent(objField, 'focus', Zapatec.PclnAutoComplete.onFocus);
  Zapatec.Utils.removeEvent(objField, 'keydown',Zapatec.PclnAutoComplete.onKeyDown);
  Zapatec.Utils.removeEvent(objField, 'keyup', Zapatec.PclnAutoComplete.onKeyUp);
  Zapatec.Utils.removeEvent(objField, 'blur', Zapatec.PclnAutoComplete.onBlur);
};



Zapatec.PclnAutoComplete.onFocus = function(objEvent) {
  // Get target element
  var objField = Zapatec.Utils.getTargetElement(objEvent);
  if (objField) {
    // Call method of attached AutoComplete object
    Zapatec.Widget.callMethod(objField.zpAutoCompleteId, 'onFocus', objField);
  }
};



Zapatec.PclnAutoComplete.prototype.onFocus = function(objField) {
  // Unset current field
  this.field = null;
  // Field must be previously attached
  if (!objField || objField.zpAutoCompleteId != this.id) {
    return;
  }
  // Set current field
  this.field = objField;
};




Zapatec.PclnAutoComplete.onBlur = function(objEvent) {
  // Get target element
  var objField = Zapatec.Utils.getTargetElement(objEvent);
  if (objField) {
    // Call method of attached AutoComplete object
    Zapatec.Widget.callMethod(objField.zpAutoCompleteId, 'onBlur');
  }
};





Zapatec.PclnAutoComplete.prototype.onBlur = function() {
  // Do nothing if tip was selected
  if (!this.tip) {
    if(this.config.prepopFirstItemOnBlur == true) {
      this.selectTip(0);
    } else {
      this.selectTip(-1);
    }
  }
};



Zapatec.PclnAutoComplete.onKeyDown = function(objEvent) {
  // Get target element
  var objField = Zapatec.Utils.getTargetElement(objEvent);
  if (objField) {
    // Call method of attached AutoComplete object
    Zapatec.Widget.callMethod(objField.zpAutoCompleteId, 'onKeyDown', objEvent);
  }
};





Zapatec.PclnAutoComplete.prototype.onKeyDown = function(objEvent) {
  // Get event object in IE
  if (!objEvent) {
    objEvent = window.event;
  }

  // Hide tips on Esc
  if (objEvent.keyCode == 27) {
    this.hide();
    // Prevent entered value from erasing
    Zapatec.Utils.stopEvent();
  }

  // Up/Down arrows
  if (objEvent.keyCode == 38 || objEvent.keyCode == 40) {
    //alert("objEvent.keyCode = Up/Down arrows: " + objEvent.keyCode);
    // Prevent entered value from erasing
    Zapatec.Utils.stopEvent();

    var divContainer = this.content;
    //debugPrint("Zapatec.PclnAutoComplete.prototype.onKeyDown"
    //  + "\r\ndivContainer.className = " + divContainer.className 
    //  + "\r\ndivContainer.tagName = " + divContainer.tagName
    //  + "\r\ndivContainer.childNodes = " + divContainer.childNodes
    //  + "\r\ndivContainer.childNodes.length = " + divContainer.childNodes.length
    //  + "\r\ndivContainer.innerHTML = " + divContainer.innerHTML
    //  + "");
      
    var intCurSelOffset = 1;
    if(objEvent.keyCode == 38) {
    	intCurSelOffset = -1;
    }
      
    //Zapatec.Utils.addClass(cal.activeMonth = i, "active");
    
    //Zapatec.PclnAutoComplete.updateListCurrentSelection(intCurSelOffset);
    this.arrowToSelection(intCurSelOffset);
  }
  
    // Enter/return key
    if (objEvent.keyCode == 13 || objEvent.keyCode == 9) {
		//accessibility -- tab/enter behave normally if zp content is hidden
		if (null != this.container && this.container.style.display != 'none') {
      this.selectTip(this.curSelIDX);
      //this.hide();
    
      // Prevent entered value from erasing
      //alert("objEvent.keyCode = Enter: " + objEvent.keyCode);
      if (objEvent.keyCode == 13 || this.curSelIDX >= 0) {
      	Zapatec.Utils.stopEvent(objEvent);
      	objEvent.cancelbubble = true;
      }
      this.hide();
		}
    }
  

  //alert("objEvent.keyCode = " + objEvent.keyCode);

};


// ****
Zapatec.PclnAutoComplete.prototype.arrowToSelection = function(intOffset) {

  //debugPrint("arrowToSelection: this.curSelIDX = " + this.curSelIDX);
  //debugPrint("arrowToSelection: intOffset = " + intOffset);

  var divContainer = this.content;

  var intNewIDX = this.curSelIDX + intOffset;
  var intMaxIDX = divContainer.childNodes.length - 1;
  if(intNewIDX > intMaxIDX) {
    intNewIDX = intMaxIDX;
  }
  if(intNewIDX < 0) {
    intNewIDX = 0;
  }
  
  //debugPrint("arrowToSelection: intNewIDX = " + intNewIDX);
  
  this.updateCurrentSelection(intNewIDX);
  


  return;
};


// ****
Zapatec.PclnAutoComplete.prototype.updateCurrentSelection = function(intIDX) {

  //var strTest = "";

  //debugPrint("updateCurrentSelection: this.curSelIDX = " + this.curSelIDX);
  //debugPrint("updateCurrentSelection: intIDX = " + intIDX);



  var divContainer = this.content;
  for (var i=0; i < divContainer.childNodes.length; i++)    {
    var objElement = divContainer.childNodes[i];
    //alert(i);
    //strTest += ("----------" + (intIDX==i?"XXXX":"") + "<br>" + objElement.innerHTML + "<br>==========<br>");

    if (objElement.className.indexOf("currentlySelected") > -1) {
    	if(intIDX != i) {
    		Zapatec.Utils.removeClass(objElement, "currentlySelected");
    	}
    } else {
        if(intIDX == i) {
    		Zapatec.Utils.addClass(objElement, "currentlySelected");
    	}	
    }
  }
  


  //debugPrint(strTest);

  //debugPrint("Zapatec.PclnAutoComplete.getSelectedItemIndex"
  //    + "<br>divContainer.className = " + divContainer.className 
  //    + "<br>divContainer.tagName = " + divContainer.tagName
  //    + "<br>divContainer.childNodes = " + divContainer.childNodes
  //    + "<br>divContainer.childNodes.length = " + divContainer.childNodes.length
  //    //+ "\r\ndivContainer.innerHTML = " + divContainer.innerHTML
  //    + "");

  
  this.curSelIDX = intIDX;

  return;
};







Zapatec.PclnAutoComplete.onKeyUp = function(objEvent) {
  // Get target element
  var objField = Zapatec.Utils.getTargetElement(objEvent);
  if (objField) {
    // Call method of attached AutoComplete object
    Zapatec.Widget.callMethod(objField.zpAutoCompleteId, 'onKeyUp', objEvent);
  }
};



Zapatec.PclnAutoComplete.prototype.onKeyUp = function(objEvent) {
  // Get event object in IE
  if (!objEvent) {
    objEvent = window.event;
  }
  // Hide tips on Esc
  if (objEvent.keyCode == 27) {
    return;
  }
  // Field must be attached
  if (!this.field) {
    return;
  }
  
  
  var strSearchKey = String(this.field.value);
  if (typeof this.config.prepareAutoCompleteStringFunc == 'function') {
  	strSearchKey = this.config.prepareAutoCompleteStringFunc(strSearchKey, this.field);
  }
  
  if (strSearchKey.length >= this.config.searchOnCharacterCount) {
    //debugPrint("Zapatec.PclnAutoComplete.prototype.onKeyUp() -- strSearchKey.length > 2");
    if(objEvent.keyCode != 38 && objEvent.keyCode != 40 && objEvent.keyCode != 13 && objEvent.keyCode != 9) {
    	// Load data from server if key was not up/down arrow or enter
    	this.loadData();
    }
  } else if (!strSearchKey.length) {
    // Hide tips
    this.hide();
  }
};




Zapatec.PclnAutoComplete.prototype.loadData = function(objArgs) {
  
  if (this.config.dataOnDemand) {
    if (typeof objArgs != 'object') {
      objArgs = {};
    }
    objArgs.keyword = String(this.field.value);
  }
  
  if (typeof this.config.cancelAutoCompleteCallFunc == 'function') {
  	var blnCancel = this.config.cancelAutoCompleteCallFunc();
  	if(blnCancel == true) {
  		return;
  	}
  }
  
  
  if (typeof this.config.prepareAutoCompleteStringFunc == 'function') {
  	objArgs.keyword = this.config.prepareAutoCompleteStringFunc(String(objArgs.keyword), this.field);
  }
  //debugPrint("Zapatec.PclnAutoComplete.prototype.loadData: objArgs.keyword = " + objArgs.keyword);
  
  objArgs.productID = this.config.productID;
  objArgs.searchType = this.config.searchType;
  objArgs.visitID = this.config.visitID;
  objArgs.searchBaseURL = this.config.searchBaseURL;
  objArgs.fieldDescription = this.fieldDescriptions[this.field.id];
  //Added by CRK starts
  objArgs.orig_dest= this.config.orig_dest;
  objArgs.gpc= this.config.gpc;
  //Added by CRK ends
  //Added by MLi starts
  objArgs.offerMethod= this.config.offerMethod;
  //Added by MLi ends
  
  var strCacheKey = String(objArgs.keyword).toLowerCase().substr(0,this.config.searchOnCharacterCount);
  var objCachedSearch = this.searchCache[strCacheKey];
  //if(objCachedSearch == "WAITING_FOR_RESPONSE") {
  //	return;
  //}
  
  // Only search by number of chars specified by config
  var strMatchKey = String(objArgs.keyword).toLowerCase();
  objArgs.keyword = strCacheKey;
  
  //debugPrint("Zapatec.PclnAutoComplete.prototype.loadData: enumObject(this.searchCache):");
  //debugEnumObject(this.searchCache);
  
  //debugPrint("Zapatec.PclnAutoComplete.prototype.loadData: this.config.searchOnCharacterCount: " + this.config.searchOnCharacterCount);
  //debugEnumObject(this);
  //debugEnumObject(this.config);



  if (objCachedSearch instanceof Object) {
  	// If cached, call loadDataJson directly with cached data
  	//debugPrint("Zapatec.PclnAutoComplete.prototype.loadData: use cached data for " + objArgs.keyword);

  	this.loadDataJson(objCachedSearch);
  	
  } else {
	// If not cached, call parent method
	
	//this.searchCache[strCacheKey] = "WAITING_FOR_RESPONSE";
	
  	//debugPrint("Zapatec.PclnAutoComplete.prototype.loadData: send request for data for " + objArgs.keyword);
  	Zapatec.PclnAutoComplete.SUPERclass.loadData.call(this, objArgs);
  }
  
};





Zapatec.PclnAutoComplete.prototype.loadDataJson = function(objData) {
  // Get data
  if (!(objData instanceof Object)) {
    objData = {};
  }
  if (!(objData.tips instanceof Array)) {
    objData.tips = [];
  }
  this.data = objData;


  var strSearchKey = String(this.field.value);
  if (typeof this.config.prepareAutoCompleteStringFunc == 'function') {
  	strSearchKey = this.config.prepareAutoCompleteStringFunc(strSearchKey, this.field);
  }
  
  
  // Filter to show only items with desired substring
  var strMatchKey = String(strSearchKey).toLowerCase();
  var objFiltered = new Object();
  	
  	
  if(strMatchKey.length == this.config.searchOnCharacterCount) {
  
  	// Set original ranks
	if( (objData.tips != null) && (objData.tips[0] != null) ) { 
	    if( (objData.tips[0].ornk == null) || (String(objData.tips[0].ornk) == "undefined") ) {
			//alert("test 2");
	
		for(var i = 0; i < objData.tips.length; i++) {
			objData.tips[i].ornk = i;
		}		
		

	  }
	}

	objFiltered = objData;
  	


  } else {
	
		
	objFiltered.tips = new Array();
	
	var arrTips = objData.tips;
		
	//var strTest = "";
	for(var i = 0; i < arrTips.length; i++) {
		//strTest += ",,,,," + i + " -- " + arrTips[i].nm.toLowerCase() + " -- " + strMatchKey;
		
		var strComparisonText = arrTips[i].fdn;

		if(arrTips[i].als != null && arrTips[i].als != "undefined") {
			strComparisonText += "|" + arrTips[i].als;
		}
		strComparisonText = strComparisonText.toLowerCase();

		
		// If filter text function defined, use it instead
		//if (typeof this.config.getTipFilterString == 'function') {
		//	//debugPrint("Zapatec.PclnAutoComplete.prototype.loadDataJson: (typeof this.config.getTipFilterString == 'function') was true");
		//	//debugPrint("*** " + strComparisonText + " -- " + this.config.getTipFilterString(arrTips[i]).toLowerCase());
		//	strComparisonText = this.config.getTipFilterString(arrTips[i]).toLowerCase();
		//}
    
		//if(arrTips[i].nm.toLowerCase().indexOf(strMatchKey) > -1) {
		if(strComparisonText.indexOf(strMatchKey) > -1) {
			objFiltered.tips[objFiltered.tips.length] = arrTips[i];
		}
	}
	
  }
  this.data = objFiltered;

  this.show();
  

  var strCacheKey = String(strSearchKey).toLowerCase().substr(0,this.config.searchOnCharacterCount);
  var objCachedSearch = this.searchCache[strCacheKey];
  
  this.searchCache[strCacheKey] = objData;

};






Zapatec.PclnAutoComplete.getElementPosition = function(objElement ) {

  if( typeof( objElement.offsetParent ) != 'undefined' ) {
    var intX = 0;
    var intY = 0;
    for(  ; objElement; objElement = objElement.offsetParent ) {
    
          var sPos = Zapatec.Utils.getStyleProperty(objElement, 'position');
          
          var strTagName = String(objElement.tagName).toLowerCase();

          if(sPos == null || sPos == "" || sPos == "static") {
                // NOT a positioned element
          } else {
                break;
      }
    
      intX += objElement.offsetLeft;
      intY += objElement.offsetTop;
    }
    return { 
      left:intX, 
      top:intY 
    };
  } else {
    return { 
    	left:objElement.x, 
    	top:objElement.y 
    };
  }
};






Zapatec.PclnAutoComplete.prototype.show = function() {

  
  this.hide();

  if (!this.data || !(this.data.tips instanceof Array) ||
   !this.data.tips.length) {
    return;
  }
  if (!this.field || !this.field.parentNode) {
    return;
  }
  
  this.tip = null;


  var objEL = Zapatec.PclnAutoComplete.getElementPosition(this.field);


  var intFieldHeight = this.field.offsetHeight;
  var intFieldTop = objEL.top;
  var intFieldLeft = objEL.left;
  
  var intListTop = intFieldTop + intFieldHeight + 2;	// +2 to put a little space between them
  var intListLeft = intFieldLeft;


  // Create container and WCH
  if (!this.container) {
    // Create container
    this.container = Zapatec.Utils.createElement('div');
    this.container.style.position = 'absolute';
    this.container.style.display = 'none';
    this.container.style.zIndex = 101;

    // Insert container
    this.field.parentNode.insertBefore(this.container, this.field);

    
    // Correct container position
    this.container.style.top = intListTop + 'px';



    // Create WCH
    this.wch = Zapatec.Utils.createWCH(this.container);

    // Put WCH under container
    if (this.wch) {
      this.wch.style.zIndex = -1;
    }

    this.content = Zapatec.Utils.createElement('div', this.container);
    this.content.className = this.getClassName({prefix: 'zpAutoComplete'});

    if (this.config.width) {
      if (this.config.width == 'auto') {
        this.content.style.width = this.field.offsetWidth + 'px';
      } else {
        this.content.style.width = this.config.width;
      }
    }
    if (this.config.height) {
      this.content.style.height = this.config.height;
    }
    
    this.content.style.overflow = this.config.overflow;
  } else if (this.container.parentNode != this.field.parentNode) {

    // Move container
    this.field.parentNode.insertBefore(this.container, this.field);

    // Correct container position
    this.container.style.top = intListTop + 'px';
  }



  // Add tips
  var intMaxCountInDropdown = 10;
  if(this.config.maxListItems && !isNaN(this.config.maxListItems)) {
  	intMaxCountInDropdown = this.config.maxListItems;
  }
  var intCount = 1;
  
  var strNewHTML = "";
  for (var iTip = 0; iTip < this.data.tips.length; iTip++) {
   if(intCount <= intMaxCountInDropdown) {
    intCount++;
    var objTip = this.data.tips[iTip];
    var arrHtml = [];
   
	arrHtml.push("<div ");
	arrHtml.push(   " onmousedown=\"Zapatec.Widget.callMethod('" + this.id + "','selectTip'," + iTip + ")\"");
	arrHtml.push(   " onmouseover=\"Zapatec.Widget.callMethod('" + this.id + "','updateCurrentSelection'," + iTip + ")\"");
	arrHtml.push(   " onmouseout=\"Zapatec.Widget.callMethod('" + this.id + "','updateCurrentSelection',-1)\"");
	arrHtml.push('>');
    
    if (typeof this.config.convertTip == 'function') {
      arrHtml.push(this.config.convertTip(objTip));
    } else {
      // Convert to string by default
      arrHtml.push(objTip + '');
    }
    arrHtml.push('</div>');
    strNewHTML += arrHtml.join('');
   }
  }

  this.content.innerHTML += strNewHTML;
  
  
  // Show container
  this.container.style.display = '';

  this.content.style.minWidth = "250px";

  if(this.content.offsetHeight > (this.content.clientHeight + 2)) {
  	this.content.style.minHeight = (this.content.clientHeight + 18) + "px";
  }
  
  // Setup WCH
  Zapatec.Utils.setupWCH(this.wch, 0, 0, this.container.offsetWidth,
   this.container.offsetHeight);


  this.curSelIDX = this.defSelIDX;

  this.updateCurrentSelection(this.curSelIDX);

};



Zapatec.PclnAutoComplete.prototype.hide = function() {
  if (this.container) {
    // Hide container
    this.container.style.display = 'none';
    // Remove tips
    this.content.innerHTML = '';
  }
};




Zapatec.PclnAutoComplete.prototype.processCustomerSearchParametersForTracking 
		= function(oField, strFieldOriginalValue, objSelectedTip, strSelectedTipIndex) {
	
	if(oField != null && oField.id != null && oField.id != "" ) {

		var strHiddenDataFieldID = "CSP_" + String(oField.id);

		var objHiddenDataField = document.getElementById(strHiddenDataFieldID);
		
		if(objHiddenDataField == null) {
		
			if(oField.form != null) {
				var oForm = oField.form;

				var objNewField = document.createElement('input');
				objNewField.setAttribute('type', 'hidden');
				objNewField.setAttribute('name', strHiddenDataFieldID);
				objNewField.setAttribute('id', strHiddenDataFieldID);
				objNewField.setAttribute('value', '');

				oForm.appendChild(objNewField);
				
				objHiddenDataField = document.getElementById(strHiddenDataFieldID);

			}
		}
		
		
		if(objHiddenDataField != null && objHiddenDataField.type == "hidden") {
			
			var strProductID = "";
			if(this.config != null && this.config.productID != null) {
				strProductID = String(this.config.productID);
			}
			
			var strSearchKey = strFieldOriginalValue;
			var strSelectedRank = strSelectedTipIndex;
			var strSelectedValue = String(oField.value);
			var strSelectedType = String(objSelectedTip.ty);


			var strCacheKey = strFieldOriginalValue.substr(0, this.config.searchOnCharacterCount);
			var objCachedSearch = this.searchCache[strCacheKey];
			var objCachedSearchTips = new Object();
			if(objCachedSearch != null) {
				objCachedSearchTips = objCachedSearch["tips"];
			}



			var strOriginalRank = String(objSelectedTip.ornk);
			

			var strVisitID = "no_vid";
			try{
				strVisitID = csaGetVisitID();
			} catch(e) {
			}
			
			var strFieldDescription = this.fieldDescriptions[String(oField.id)];
			

			var strData =	strProductID 
				+ "|" + strSearchKey
				+ "|" + strSelectedType
				+ "|" + strSelectedValue
				+ "|" + strSelectedRank
				+ "|" + strOriginalRank
				+ "|" + strFieldDescription
				+ "|" + strVisitID
				+ "";
			
			//alert(strData);
			
			
			objHiddenDataField.value = strData;
		}
		
	}
};






Zapatec.PclnAutoComplete.prototype.selectTip = function(iTip) {
  this.hide();
  if (!this.data || !(this.data.tips instanceof Array) ||
   typeof this.data.tips[iTip] == 'undefined') {
    return;
  }
  var objTip = this.data.tips[iTip];
  this.tip = objTip;



  var strFieldOriginalValue = String(this.field.value);
  var strSelectedTipIndex = String(iTip);
  var objSelectedTip = objTip;


  
  // Update field
  if (typeof this.config.convertTipToTextInput == 'function') {
    // Convert value using callback
    this.field.value = "" + this.config.convertTipToTextInput(objTip, this.field.id);
  } else if (this.field) {
    // Convert to string
    this.field.value = objTip + '';
  }

  this.processCustomerSearchParametersForTracking(this.field, strFieldOriginalValue, objSelectedTip, strSelectedTipIndex);

};







// DEBUG functions


function debugPrint(strFoo) {
	var oDebug = document.getElementById("debugLog");
	if(oDebug) {
		oDebug.innerHTML += strFoo + "<br/>\r\n";
	}
}

function debugClear() {
	var oDebug = document.getElementById("debugLog");
	if(oDebug) {
		oDebug.innerHTML = "";
	}
}
function debugEnumObject(objX) {
	for(var vX in objX) {
		debugPrint("....." + vX + " = " + objX[vX]);
	}
}




