/*********************************************************************
'***    Program: selectListItem( objList, strItemValue )
'***    Type: Function
'***
'***    Function: Selects item in a list box
'***
'***    Parameters: 
'***		objList - listbox (select) object
'***		strItemValue  - value to look for. the item with this value gets selected
'***
'***    Returns: Void
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/12/99
'*********************************************************************/
function selectListItem( objList, strItemValue ) {
	for (var i=0; i < objList.length; i++) {
		if (objList.options[i].value == strItemValue) {
			// this item needs to get selected
			objList.options[i].selected = true;
			break;
		}
	}
	return;
}


/*********************************************************************
'***    Program: isAlphaAndDigitString(strToCheck) 
'***    Type: Function
'***
'***    Function: Tests a string for presence of alpha and digit chars only
'***
'***    Parameters: 
'***		strToCheck - sting to check for validity
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/12/99
'*********************************************************************/
function isAlphaAndDigitString(strToCheck) {
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  var checkStr = strToCheck;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  return (allValid);
}

/*********************************************************************
'***    Program: isAlphaAndDigitEntry(objText) 
'***    Type: Function
'***
'***    Function: Checks a text box object's value for alpha and digit chars only
'***
'***    Parameters: 
'***		objText - object to check value in
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/12/99
'*********************************************************************/
function isAlphaAndDigitEntry(objText) {
  return (isAlphaAndDigitString(objText.value));
}

/*********************************************************************
'***    Program: isObjectOnForm(objForm, strObjName)
'***    Type: Function
'***
'***    Function: Checks whether an object exists on a given form 
'***
'***    Parameters: 
'***		objForm - form object to use when looking for an element
'***		strObjName - name of an object to look for. Case sensitive!
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/12/99
'*********************************************************************/
function isObjectOnForm(objForm, strObjName) {
	for (intFormIndex = 0; intFormIndex < objForm.elements.length; intFormIndex++) {
		if (objForm.elements[intFormIndex].name == strObjName) {
			return true;
		}
	}
	return false;
}

/*********************************************************************
'***    Program: getSelectedItems( lstWhereToLook )
'***    Type: Function
'***
'***    Function: Enumerates through a list box and returns a comma separated list of selected
'***		items
'***
'***    Parameters: 
'***		lstWhereToLook  - select object to look in
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 9/30/99
'*********************************************************************/
function getSelectedItems( lstWhereToLook ) {
	if (lstWhereToLook == null) {
		return "";
	}

	var strResult = "" ;
	for (intItemIndex = 0; intItemIndex < lstWhereToLook.length; intItemIndex++ ) {
		if (lstWhereToLook[intItemIndex].selected) {
			strResult += "," + lstWhereToLook[intItemIndex].value;
		}
	}
	if (strResult.length > 0) {
		return strResult.substring(1); // all but first comma
	}
	return strResult
}

/*********************************************************************
'***    Program: getCleanKeywords
'***    Type: Function
'***
'***    Function: Analized incoming string for signs of full text search syntax
'***		and attempts to fix syntax issues by adding "AND" operator if
'***		users do not specify full text search syntax.
'***
'***    Parameters: 
'***		strKeywords - string to analyze
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 8/7/2001
'*********************************************************************/
function getCleanKeywords( strDirtyKeywords ) {

	var sValue = strDirtyKeywords;
        sValue = " " + strDirtyKeywords;

        // remove leading and trailing spaces
	sValue = sValue.replace(/(^ *)|( *$)/g, "");

        // replace any number of spaces with just one space
	sValue = sValue.replace(/( +)/g, " ");

        if ((sValue.toLowerCase().indexOf(" and ") == -1) && (sValue.toLowerCase().indexOf(" or ") == -1) && (sValue.toLowerCase().indexOf(" near ") == -1)) {

		// no "OR"s and "AND"s
		if (sValue.indexOf('"') == -1)  {
	  
			// replace spaces with " and "
			sValue = sValue.replace(/ /g, " and ");
		}
		else {
			// replace expression in quotes+space with itself following "and "
			re = /(".*" )/g;
			sValue = sValue.replace(re, "$1and ");
		}
	}
	// replace double and single quotes with "&quot;"
	sValue = sValue.replace(/('|")/g, "&quot;");
	return sValue;
}


/*********************************************************************
'***    Program: getCleanCCNo( strDirtyCCNo )
'***    Type: Function
'***
'***    Function: removes spaces and dashes from given string
'***		
'***
'***    Parameters: 
'***		strDirtyCCNo - string (cc no) to clean
'***
'***    Returns: String
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 5/18/2001
'*********************************************************************/
function getCleanCCNo( strDirtyCCNo ) {
	var strNewString = new String(strDirtyCCNo);
	return strNewString.replace(/[ -]/g, "");
}

/*********************************************************************
'***    Program: unselectListItems( objList )
'***    Type: Function
'***
'***    Function: unselects all item in a list box
'***
'***    Parameters: 
'***		objList - listbox (select) object
'***
'***    Returns: Void
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 5/24/2001
'*********************************************************************/
function unselectListItems( objList ) {
	for (var i=0; i < objList.length; i++) {
		// this item needs to get selected
		objList.options[i].selected = false;
	}
	return 0;
}

/*********************************************************************
'***    Program: isListItemSelected( objList, strItemValue )
'***    Type: Function
'***
'***    Function: unselects all item in a list box
'***
'***    Parameters: 
'***		objList - listbox (select) object
'***		strItemValue - value of item to test
'***
'***    Returns: Boolean
'***    Remarks: none
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 9/13/2001
'*********************************************************************/
function isListItemSelected( objList, strItemValue ) {
	for (var i=0; i < objList.length; i++) {
		// this item needs to get selected
		if (objList.options[i].value == strItemValue) {
			return objList.options[i].selected == 1;
		}
	}
	return false;
}

/*********************************************************************
'***    Program:  isnertFirstElementIntoListObject
'***    Type: Function
'***
'***    Function: Inserts a first element into list object passed into this function as 
'***		parameter.
'***
'***    Parameters: 
'***		objList - object reference. must be a select object
'***		strElementText  - text to use when inserting element
'***		strElementValue - value of new element
'***
'***    Returns: String
'***    Remarks: First element will be selected
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 9/19/2001
'*********************************************************************/
function isnertFirstElementIntoListObject( objList, strElementText, strElementValue ) {
	arrNewLabels = new Array( objList.length );
	var intOption = 0;

	// save all options
	for (intOption = 0; intOption < objList.length; intOption++) {
		arrNewLabels[intOption] = new Option(  objList.options[intOption].text, objList.options[intOption].value );
	}

	// remove all options
	for (objList.length; intOption >= 0; intOption--) {
		objList.options[intOption] = null;
	}


	// add new first option
	objList.options[0] = new Option(strElementText, strElementValue, true, true );

	// add saved options
	for (intOption = 0; intOption < arrNewLabels.length; intOption++) {
		objList.options[intOption+1] = arrNewLabels[intOption];
	}
 
	return true;
}

/*********************************************************************
'***    Program:  validateCreditCardExpirationDate( strDate )
'***    Type: Function
'***
'***    Function: validates a date entered as credit card expiration
'***		required format: mm/yy.
'***		This function just checks the format, not the actual date
'***
'***    Parameters: 
'***		strDate - date to validate
'***
'***    Returns: Boolean
'***    Remarks: 
'***
'***    Created by: Dimab
'***    Changed by: Dimab
'***    Last change: 9/13/2002
'*********************************************************************/
function validateCreditCardExpirationDate( strDate ) {
	var strDateToCheck = new String(strDate);

	if (strDateToCheck.length != 5) {
		return false;
	}
	if (strDateToCheck.indexOf("/") != 2 ) {
		return false;
	}
	
	// validate month
	if ( ! isDigitString(strDateToCheck.substring(0,2))) {
		return false;
	}

	// validate year
	if ( ! isDigitString(strDateToCheck.substr(3,2))) {
		return false;
	}
	return true;
}

/*********************************************************************
'***    Program: isDigitString(strToCheck) 
'***    Type: Function
'***
'***    Function: Tests a string for presence of digit chars only
'***
'***    Parameters: 
'***		strToCheck - sting to check for validity
'***
'***    Returns: True/False
'***    Remarks: none
'***
'***    Created by: jeffl
'***    Changed by: jeffl
'***    Last change: 04/01/02
'*********************************************************************/
function isDigitString(strToCheck) {
  var checkOK = "0123456789.";
  var checkStr = strToCheck;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  return (allValid);
}

