
function getElement(e, f){
	if (document.layers){
		f = (f) ? f : self;
		if (f.document.layers[e]) {
			return f.document.layers[e];
		}
		for (W = 0; W < f.document.layers.length; W++) {
			return (getElement(e, f.document.layers[W]));
		}
	}
	if (document.all) {
		return document.all[e];
	}
	return document.getElementById(e);
}

function hideById(id)
{
	if (el = getElement(id)) {
		el.style.display = 'none';
	}
}

function showById(id)
{
	if (el = getElement(id)) {
		getElement(id).style.display = 'block';
	}
}

function hideAllByPrefix(prefix)
{
	for (i = 0; i < document.all.length; i++) {
		if (document.all[i].id.substring(0, prefix.length) == prefix) {
			hideById(document.all[i].id);
		}
	}
}

function showAllByPrefix(prefix)
{
	for (i = 0; i < document.all.length; i++) {
		if (document.all[i].id.substring(0, prefix.length) == prefix) {
			showById(document.all[i].id);
		}
	}
}

function getElementsByPrefix(prefix)
{
	var elems = new Array();
	
	for (i = 0; i < document.all.length; i++) {
		if (document.all[i].id.substring(0, prefix.length) == prefix) {
			elems.push(document.all[i]);
		}
	}
	return elems;
}

function setStyle(el, value, override)
{
	if (override) {
		el.style = value;
	}
	else {
		alert('not overriding');
	}
}

function setClassName(el, value)
{
	el.className = value;
	alert(el.className);
}

function unsetSelectedLink(el)
{
	alert(el);
}

function ltrim(s)
{
	return s.replace(/\s*$/, '');
}

function rtrim(s)
{
	return s.replace(/^\s*/, '');
}

function trim(s)
{
	return s.replace(/^\s*/, '').replace(/\s*$/, '');
}

function setValue(id, value)
{
	getElement(id).value = value;
}

function setValues(arr)
{
	for (key in arr) {
		setValue(key, arr[key]);
	}
}

function submitForm(formId)
{
	getElement(formId).submit();
}

function formatPhoneNumber(telNo)
{
    // If it's blank, save yourself some trouble by doing nothing.
    if (telNo.value == "") return;

    var phone = new String (telNo.value);
    
    phone = phone.substring(0,14);

    if (phone.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null)
    {
        if (phone.match (".[0-9]{2}.[0-9]{3}-[0-9]{4}|" + ".[0-9].[0-9]{3}-[0-9]{4}|" +
            ".[0-9]{3}.[0-9]{2}-[0-9]{4}|" + ".[0-9]{3}.[0-9]-[0-9]{4}") == null)
        {
            var phoneNumeric = phoneChar = "", i;
            // Loop thru what user has entered.
            for (i=0;i<phone.length;i++)
            {
                phoneChar = phone.substr (i,1);
    
                if (!isNaN (phoneChar) && (phoneChar != " ")) phoneNumeric = phoneNumeric + phoneChar;
            }
    
            phone = "";

            for (i=0;i<phoneNumeric.length;i++)
            {
                // If it's the first digit, throw in "(" before that.
                if (i == 0) phone = phone + "(";
                // If you are on the 4th digit, put ") " before that.
                if (i == 3) phone = phone + ") ";
                // If you are on the 7th digit, insert "-" before that.
                if (i == 6) phone = phone + "-";
                // Add the digit to the phone charatcer string you are building.
                phone = phone + phoneNumeric.substr (i,1)
            }
        }
    }
    else
    { 
        // This means the tel no is in proper format. Make sure by replacing the 0th, 4th and 8th character.
        phone = "(" + phone.substring (1,4) + ") " + phone.substring (5,8) + "-" + phone.substring(9,13); 
    }
    // So far you are working internally. Refresh the screen with the re-formatted value.
    if (phone != telNo.value) telNo.value = phone;
}

function checkPhoneNumber(telNo)
{
    if (telNo.value == "") return;
    if (telNo.value.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null)
    {
        if (telNo.value.match ("[0-9]{10}") != null)
            formatPhoneNumber(telNo)              
    }
}


/**
 * Internet Explorer holds references to objects which are not JavaScript 
 * objects, and which produce errors if they are treated as JavaScript 
 * objects. This is a problem because typeof identifies them as JavaScript 
 * objects. The isAlien() function will return true if a is one of those 
 * alien objects.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isAlien(a) {
	return isObject(a) && typeof a.constructor != 'function';
}

/**
 * isArray() returns true if a is an array, meaning that it was produced by 
 * the Array constructor or by using the [ ] array literal notation.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isArray(a) {
	return isObject(a) && a.constructor == Array;
}

/**
 * isBoolean(a) returns true if a is one of the boolean values, true or false.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isBoolean(a) {
	return typeof a == 'boolean';
}

/**
 * isEmpty(a) returns true if a is an object or array or function containing 
 * no enumerable members.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isEmpty(o) {
	var i, v;
	if (isObject(o)) {
		for (i in o) {
			v = o[i];
			if (isUndefined(v) && isFunction(v)) {
				return false;
			}
		}
	}
	return true;
}

/**
 * isFunction(a) returns true if a is a function. Beware that some native 
 * functions in IE were made to look like objects instead of functions. This 
 * function does not detect that.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isFunction(a) {
	return typeof a == 'function';
}

/**
 * isNull(a) returns true if a is the null value.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isNull(a) {
	return typeof a == 'object' && !a;
}

/**
 * isNumber(a) returns true if a is a finite number. It returns false if a is 
 * NaN or Infinite. It also returns false if a is a string that could be 
 * converted to a number.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isNumber(a) {
	return typeof a == 'number' && isFinite(a);
}

/**
 * isObject(a) returns true if a is an object, and array, or a function. It 
 * returns false if a is a string, a number, a boolean, or null, or undefined.
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isObject(a) {
	return (a && typeof a == 'object') || isFunction(a);
}

/**
 * isString(a) returns true if a is a string.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isString(a) {
	return typeof a == 'string';
}

/**
 * isUndefined(a) returns true if a is the undefined value. You can get the 
 * undefined value from an uninitialized variable or from a missing member of 
 * an object.
 *
 * {@link http://www.crockford.com/javascript/remedial.html}
 */
function isUndefined(a) {
	return typeof a == 'undefined';
}

Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
	var i;
	for (i = 0; i < this.length; i++) {
		// Matches identical (===), not just similar (==).
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
