// Color safe array (segments)
var Color= new Array();
Color[1] = "ff";
Color[2] = "ee";
Color[3] = "dd";
Color[4] = "cc";
Color[5] = "bb";
Color[6] = "aa";
Color[7] = "99";
// =========================================================================================================================
// Wait to fade an element
// -------------------------------------------------------------------------------------------------------------------------
function waittofade() {
	if (getElem('fade')) {
    setTimeout("fadeIn(7)", 1000);
	 }
}
// =========================================================================================================================
// Fade in an element
// -------------------------------------------------------------------------------------------------------------------------
function fadeIn(what,where) {
		if (isUndefined(what)) what = 'fade';
    if (where >= 1) {
        getElem(what).style.backgroundColor = "#ffff" + Color[where];
		  if (where > 1) {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			} else {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			  getElem('fade').style.backgroundColor = "transparent";
			}
    }
}
// =========================================================================================================================
// Validate if a field is empty adn display error message
// -------------------------------------------------------------------------------------------------------------------------
function validateField(fieldId, alertMessage) {
    if (getElem(fieldId).value == "") {
        alert(alertMessage);
        getElem(fieldId).focus();
        return false;
    } else {
        return true;
    }
}
// =========================================================================================================================
// Popup a window
// -------------------------------------------------------------------------------------------------------------------------
function popup(url,width,height,scrollbars) {
	if (isUndefined(scrollbars)) scrollbars = 'yes';
	options = 'width='+width+',height='+height+',scrollbars='+scrollbars+',resizable=yes,toolbar=no,directories=no,menubar=no,status=no,left=100,top=100';
	window.open(url,'popup',options);
	return false;
}
// =========================================================================================================================
// Switch visibility for one element
// -------------------------------------------------------------------------------------------------------------------------
function toggleElement(name) {
		if (getElem(name).style.display == 'none') {
				getElem(name).style.display = 'block'
		} else {
				getElem(name).style.display = 'none'
		}
}
// =========================================================================================================================
// Display/hide elements
// -------------------------------------------------------------------------------------------------------------------------
function showHideElems(name1,name2) {
	getElem(name1).style.display = 'block'
	getElem(name2).style.display = 'none'
}
// =========================================================================================================================
// Swap visibility between two elements
// -------------------------------------------------------------------------------------------------------------------------
function swapElements(name1,name2) {
		if (getElem(name1).style.display == 'none') {
				getElem(name1).style.display = 'block'
				getElem(name2).style.display = 'none'
		} else {
				getElem(name1).style.display = 'none'
				getElem(name2).style.display = 'block'
		}
}
// =========================================================================================================================
// Get an element by class name
// -------------------------------------------------------------------------------------------------------------------------
function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName), function(elem) { return hasClass(elem, className) });
}
// =========================================================================================================================
// Set the prototype for push
// -------------------------------------------------------------------------------------------------------------------------
if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}
// =========================================================================================================================
// Set the prototype for find
// -------------------------------------------------------------------------------------------------------------------------
Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}
// =========================================================================================================================
// Set the prototype for has
// -------------------------------------------------------------------------------------------------------------------------
Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}
// =========================================================================================================================
// Set the prototype for isUndefined
// -------------------------------------------------------------------------------------------------------------------------
function isUndefined(v) {
    var undef;
    return v===undef;
}
// =========================================================================================================================
// Filter a list
// -------------------------------------------------------------------------------------------------------------------------
function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}
// =========================================================================================================================
// Map function
// -------------------------------------------------------------------------------------------------------------------------
function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}
// =========================================================================================================================
// Get element by id (cross browser)
// -------------------------------------------------------------------------------------------------------------------------
function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}
// =========================================================================================================================
// Checks for a class in an element
// -------------------------------------------------------------------------------------------------------------------------
function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}
// =========================================================================================================================
// indexOf (Sean)
// -------------------------------------------------------------------------------------------------------------------------
function indexOf( _value ) {
  for( i = 0; i < this.length; i++ ) {
     if ( this[ i ].indexOf( ":" + _value ) > 0 ) return( i );
  }
  return( 0 )
}

Array.prototype.indexOf = indexOf;
