﻿// DynamicDropDownItem class
///var DynamicDropDownItem = new _DynamicDropDownItem(null, null, null);
/**
 * The DynamicDropDownItem class holds contact information.
 * @author <a href="mailto:eric@evite.com">Eric Berry</a>
 */
function DynamicComboBoxItem(pDataTextField) {
	var DataTextField_Array = pDataTextField.split(", ");
	this.DataTextField = DataTextField_Array[0];
	if(DataTextField_Array.length>1){
	    this.DataTextField2 = DataTextField_Array[1];
	}else{
	    this.DataTextField2 = "";
	}
	
	this.matches = new Array(4);
	this.clearMatches = _ComboBoxclearMatches;
	this.getDataTextField = _ComboBoxgetDataTextField;
	this.getDataTextField2 = _ComboBoxgetDataTextField2;
	this.getDataValueField = _ComboBoxgetDataValueField;
	this.getDisplayText = _ComboBoxgetDisplayText;
	this.getMatch = _ComboBoxgetMatch;
	this.getDataTextDisplayField = _ComboBoxgetDataTextDisplayField;
	this.getHighlightedText = _ComboBoxgetHighlightedText;
	this.setDataTextField = _ComboBoxsetDataTextField;
	
	this.setMatch = _ComboBoxsetMatch;
	this.compareTo = _ComboBoxcompareTo;
	this.compareToItem = _ComboBoxcompareToItem;
	this.getInstance = _ComboBoxgetInstance;
	this.SEARCH_DATATEXTFIELD = 0;
	this.SEARCH_DATATEXTFIELD2  = 1;
	this.SEARCH_DATAVALUEFIELD = 2;
	this.SEARCH_EMPTY = 3;
	
	// initialize matches;
	this.clearMatches();
}
function _ComboBoxgetInstance(DataTextField) {
	return new DynamicDropDownItem(DataTextField);
}

/**
 * @return this.emailAddress
 */
function _ComboBoxgetDataTextDisplayField() {
    if(this.DataTextField2.length>0){
        var returnThis  = this.DataTextField + ", "+this.DataTextField2;
        return returnThis;
    }else{
	    return this.DataTextField;
	}
}
function _ComboBoxgetDataTextField() {
	return this.DataTextField;
}
function _ComboBoxgetDataTextField2() {
	return this.DataTextField2;
}
/**
 * @return this.DATATEXTFIELD
 */
function _ComboBoxgetDataValueField() {
	return this.DataTextField;
}
/**
 * Checks to see if a match has been found for the given field on this Contact.
 * @param field The field to check for a match.
 * @return true if a match has been found.
 */
function _ComboBoxgetMatch(field) {
	return this.matches[field];
}
/**
 * @param DataTextField
 */
function _ComboBoxsetDataTextField(DataTextField) {
	this.DataTextField = DataTextField;
}
function _ComboBoxsetDataTextField2(DataTextField2) {
	this.DataTextField2 = DataTextField2;
}
/**
 * @param DataValueField
 */
/**
 * Lets the contact know that Match has been found for the given field.
 * @param field
 * @param found
 */
function _ComboBoxsetMatch(field, found) {
	this.matches[field] = found;
}
/**
 * Gets the DisplayText for the given field.
 * @param searchTermLength The length of the SearchTerm
 * @param field The field to get the display text for.
 * @return The DisplayText for the given field.
 */
 function _ComboBoxgetHighlightedText(searchTermLength, matchDATATEXTFIELD, matchDATATEXTFIELD2, matchDATAVALUEFIELD) {
    displayText = '';    
    fieldValue  = this.getDataTextField();
    
	if(matchDATATEXTFIELD==true){
	    if(fieldValue.length > searchTermLength){
	        displayText = "<b>"+fieldValue.substring(0,searchTermLength)+"</b>"+fieldValue.substring(searchTermLength,fieldValue.length);;
	    }else{
	        displayText = "<b>"+fieldValue+"</b>"
	    }
	}else{
	    displayText = fieldValue
	}
	
	fieldValue  = this.getDataTextField2();
	if(matchDATATEXTFIELD2==true){
	    if(fieldValue.length > searchTermLength){
	        displayText += ", <b>"+fieldValue.substring(0,searchTermLength)+"</b>"+fieldValue.substring(searchTermLength,fieldValue.length);;
	    }else if(fieldValue.length>0){
	        displayText += ", <b>"+fieldValue+"</b>"
	    }
	}else{
	    if(fieldValue.length>0){
	        displayText += ", "+fieldValue;
	    }
	}
	
	return displayText;
}
function _ComboBoxgetDisplayText(searchTermLength, field) {
	displayText = '';
	fieldValue = '';
	switch(field) {
		case DynamicDropDownItem.SEARCH_DATATEXTFIELD:
			fieldValue = this.getDataTextField();
			break;
		default: // SEARCH_TEXT
			fieldValue = this.getDataTextField();
			break;
	}
	
	if(fieldValue) {
		if(this.getMatch(field)) {
			fieldValueMatch = '';
			fieldValueEnd = '';
			if(fieldValue.length > searchTermLength) {
				fieldValueMatch = fieldValue.substring(0, searchTermLength);
				fieldValueEnd = fieldValue.substring(searchTermLength, fieldValue.length);
			} else {
				fieldValueMatch = fieldValue;
			}	
			displayText += '<b>' + fieldValueMatch + '</b>' + fieldValueEnd;
		} else {
			displayText += fieldValue;
		}
	}
	return displayText;
}
/**
 * Search sort of works like the java.util.Comparable interface. If the given
 * field starts with the given search term 0 is returned thus returning they are
 * equal. If the field is greater than the searchTerm a 1 is returned and if
 * the field is less than the searchTerm a -1 is returned.
 */
function _ComboBoxcompareTo(searchTerm, field) {
	searchTermLength = searchTerm.length;
	searchFieldValue = '';
	searchFieldValueSubstring = '';
	searchResult = 0;
	switch(field) {
		case this.SEARCH_DATATEXTFIELD:
			searchFieldValue = this.DataTextField.toLowerCase();
			break;
	    case this.SEARCH_DATATEXTFIELD2:
			searchFieldValue = this.DataTextField2.toLowerCase();
			break;
		default: // SEARCH_DATATEXTFIELD
			searchFieldValue = this.DataTextField.toLowerCase();
			alert("-"+searchFieldValue);
			break;
	}
	searchFieldValueSubstring = searchFieldValue;
	if(searchFieldValue.length > searchTermLength) {
		searchFieldValueSubstring = searchFieldValue.substring(0, searchTermLength);
	}
	//alert('comparing: ' + searchFieldValue + ' against: ' + searchTerm);
	if(searchFieldValueSubstring == searchTerm) { // searchFieldValue starts with searchTerm
		//this.setMatch(field, true);
		searchResult = 0;
	} else if(searchFieldValueSubstring > searchTerm) {
		//this.setMatch(field, false);
		searchResult = 1; // this contacts field is greater than the searchTerm
	} else {
		//this.setMatch(field, false);
		searchResult = -1; // this contacts field is less than the searchTerm
	}
	return searchResult;
}
/**
 * Clears the matches found on this DynamicDropDownItem.
 */
function _ComboBoxclearMatches() {
	this.setMatch(this.SEARCH_DATATEXTFIELD, false);
}

function _ComboBoxcompareToItem(contact) {
	compareValue = -1;
	if(this.getDataTextField().toLowerCase() > contact.getDataTextField().toLowerCase()) {
		compareValue = 1;
	} else if (this.getDataTextField().toLowerCase() == contact.getDataTextField().toLowerCase()) {
	    if(this.getDataTextField().toLowerCase() > contact.getDataTextField().toLowerCase()) {
	    	compareValue = 1;
	    } else if (this.getDataTextField().toLowerCase() == contact.getDataTextField().toLowerCase()) {
	        compareValue    = 0;
	    }
	}
	return compareValue;
}
