/******************************************************************************
  * Common Functions
  * Written 10/15/2000
  *
  * Usage (replace &gt; & &lt; appropriately): 
  *   place the following line in the HEAD or BODY tags.  (HEAD is preferable...;)
  *   &lt;SCRIPT Language="Javascript" SRC="/include/CommonFunctions.js"&gt;&lt;/SCRIPT&gt;
  * 
  *****************************************************************************/
var isNN4up = (document.layers)? true : false;
var isIE4up = (document.all)? true : false;
var isNN = isNetscape(0);

/* does the browser know how to swap images? */

function isBrowserMouseOverEnabled() 
{
	return ( isNetscape(4) || isIE(4) )
}


/* returns true if browser is Netscape, version is equal to or higher than verOrHigher and if it is not a mac (Mac's are notorious for not supporting JavaScript)*/
function isNetscape(verOrHigher)
{
	var bwr = navigator.appName;
	var ver = parseInt(navigator.appVersion);
	var plt = navigator.platform;
	if ( (bwr == "Netscape")  &&  ( ver >= verOrHigher )  &&  (plt.indexOf('Mac') == -1) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

/* returns true if browser is IE, version is equal to or higher than verOrHigher and if it is not a mac (Mac's are notorious for not supporting JavaScript)*/
function isIE(verOrHigher)
{
	var bwr = navigator.appName;
	var ver = parseInt(navigator.appVersion);
	var plt = navigator.platform;
	if ( (bwr == "Microsoft Internet Explorer")  &&  ( ver >= verOrHigher )  &&  (plt.indexOf('Mac') == -1) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

/******************************************************************************
 * getKeypressed()
 * This function is bessed used in a text form field in the 
 * onKeyPressed=  Attribute.
 * Sample usage:  <INPUT TYPE="TEXT" NAME="T1" onKeyPressed="JavaScript:getKeyPressed()">
 */
function getKeypressed() 
{
	var theKey;
	var itsEvent;
	if ( isNetscape(4) ) 
	{
		itsEvent = getKeypressed.arguments[0];
		theKey = itsEvent.which;
	}
	else if ( isIE(4) ) 
	{
		theKey = window.event.keyCode;
	}
	else
	{
		; /* sorry don't know the event you created */
	}
		
	/*
	if (theKey == 13)
	{
		document.CheckAvailability_Frm.submit();
	}	*/
		
	return theKey
} 

/******************************************************************************
 * isAscii(text [, min [, max]])
 * Are the characters in text between 0 and 128?
 * PARAMETERS:
 *		text:         The text to be tested.
 *		MIN_CHAR_NUM: (*Optional) - If present, then use this number as the min
 *                     character number allowed (instead of the default of 0)
 *		MAX_CHAR_NUM: (*Optional) - If present, then use this number as the max
 *                     character number allowed (instead of the default of 128)
 * RETURN VALUES:
 *      true   - if all the characters are between 0 and 128?
 *      false  - if any of the character codes is below min_char_num or 
 *               greater than max_char_num.
 * USAGE:
 *      isAscii("my String");
 *      isAscii("my String", 32, 128);
 */
function isAscii(text) 
{
	var MIN_CHAR_NUM = 0;
	var MAX_CHAR_NUM = 128;
	var i = 0;
	var itsCharCode = null;
	
	if (isAscii.arguments[1] >= 0) /* did they pass in the optional parameter MIN_CHAR_NUM? */
	{
		MIN_CHAR_NUM = isAscii.arguments[1];
	}
	if (isAscii.arguments[2] >= 0) /* did they pass in the optional parameter MAX_CHAR_NUM? */
	{
		MAX_CHAR_NUM = isAscii.arguments[2];
	}
		
	for (i = 0; i < text.length; i++)
	{
		itsCharCode = text.charCodeAt(i);
		if ( (itsCharCode < MIN_CHAR_NUM) || (itsCharCode > MAX_CHAR_NUM) )
		{
			return false
		}
	}
	return true;
}

// selects/deselects all checkboxes in a form
function checkAll(form,box) {
	var thisForm = eval("document." + form);
	var box = eval("document." + form + "." + box);
		
	if (box.checked == true) {
		for (i=0; i<thisForm.elements.length; i++) {
		
			if (thisForm.elements[i].type=="checkbox" && i!=0) {
				thisForm.elements[i].checked = true;
			}
		
		}
	
	}
	else {
		
		for (i=0; i<thisForm.elements.length; i++) {
		
			if (thisForm.elements[i].type == "checkbox" && i!=0) {
				thisForm.elements[i].checked = false;
			}
		
		}		
		
	}

}// end checkAll()



// form validation code

function isEmpty(str) {
	if (str.length==0){
		return true
	}else{
		return false
	}
}


function checkspaces(str) {
	var y = str.length;
	y = y - 1;


	if (str.charAt(0) == " "){
		return true;
	}else if(str.charAt(y) == " "){
		return true;
	} else {
		return false;
	}
}


function checkdoublespaces(str) {

	if (str.indexOf("  ") != -1){
		return true;
	} else {
		return false;
	}
}

function swapImage(imageName, fileLocation)
{
	img = eval("document.images['" + imageName + "']");
	img.src = fileLocation;
}

function preloadImages()
{
	if (document.images) {
    if (typeof(document.WM) == 'undefined'){
      document.WM = new Object();
    }
    document.WM.loadedImages = new Array();
    // Loop through all the arguments.
    var argLength = preloadImages.arguments.length;
    for(arg=0;arg<argLength;arg++) {
      // For each arg, create a new image.
      document.WM.loadedImages[arg] = new Image();
      // Then set the source of that image to the current argument.
      document.WM.loadedImages[arg].src = preloadImages.arguments[arg];
    }
  }

}

// sizes and sets a standard popup window
function popupWindow(filename, type) {
		
	var w = "670";
	var h = "600";
	
	switch(type)
	{
		case "flyer":
			h = "700";
			break;
	}
	
    popup_window = window.open( filename, "popup_window", "left=20,top=20,width=" + w + ",height=" + h + ",resizable=yes,scrollbars=1,toolbar=0,status=0,location=0,directories=0,menubar=0");
	popup_window.focus();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function CheckAll(CheckboxArray, Checked)
{
    for(i=0; i<CheckboxArray.length; i++)
        CheckboxArray[i].checked = Checked;
}

/**************************
* Automatically sets focus to the next form element when the current 
* form element's maxlength has been reached. 
*/
function autoTab(input,len, e) {
  var key = is_ie ? e.keyCode : e.which;  // is_ie is set in sniffer.js
  var filter = [0,8,9,16,17,18,35,36,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,key)) {
    //alert("key: " + key);
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function isNumeric(event) {
  var key = is_ie ? event.keyCode : event.which;  // is_ie is set in sniffer.js
  
  if (key >= 48 && key <= 57) return true;  // 0-9
  if (key >= 96 && key <= 105) return true; // 0-9 keypad
  if (key >= 35 && key <= 40) return true;  // end, home, arrow keys
  if (key == 46 || key == 8 || key == 9 || key == 116) return true;  // delete, backspace, tab, F5
  return false;
}

