// JS GLOBALs

// this is an array that contains all onLoad
var onload_functions = new Array();


DOM = (document.getElementById) ? true : false;
NS4 = (document.layers) ? true : false;
IE = (document.all) ? true : false;
IE4 = IE && !DOM;
isIE = ((document.all) || (IE4 && navigator.appVersion.indexOf("5.")!=-1));

//
// Register an onload function to be called upon BODY.onLoad()
//
// Returns void.
// Accepts a Function Object (  i.e. func_obj = new Function("alert('this is the body of this function');");   )
//
// takes an optional parameter to determine 'when' it is onloaded, 0 going first and higher being later
// defaults to (0) if not specified
// TODO - prefer to have it default to 10 (so things that are specified have a chance to go before it)
// TODO - so use our condense_array function across the array in the onload

function registerOnLoadFunction( func_obj , onload_position) {
	// append the func_obj to onloads global array.
	if ( onload_position == null) { onload_position = 0; }
	if (onload_functions[onload_position] == null) { onload_functions[onload_position] = new Array(); }
	if ( func_obj != null ) {
		onload_functions[onload_position][onload_functions[onload_position].length] = func_obj;
	}

}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (';', offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + '=' + escape (value) +
	((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
	((path == null) ? '' : ('; path=' + path)) +  
	((domain == null) ? '' : ('; domain=' + domain)) +    
	((secure == true) ? '; secure' : '');
}

var pop_win_count = GetCookie('popup_win_count');
var expDays = 1;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

if ( pop_win_count == null ) {
	SetCookie('popup_win_count',0,exp,'/');
	pop_win_count = 0;
}


var call_sync_diagnosis = false;
var layers_doc = false;
var call_sync_selection = false;
function showPopup(p_SelectedObject) {
	if ( p_SelectedObject.options[p_SelectedObject.selectedIndex].value != '' )
		eval ("PopWin('" + p_SelectedObject.options[p_SelectedObject.selectedIndex].value + "',400,400)"); 
}
function changeTabs(p_SelectedObject) {
	if ( p_SelectedObject.options[p_SelectedObject.selectedIndex].value != '' )
		eval ("document.location='" + p_SelectedObject.options[p_SelectedObject.selectedIndex].value + "'"); 
}

function changeback(originalcolor){if ( !isIE )return true;if (window.event.fromElement.contains(window.event.toElement)||source.contains(window.event.toElement)||source.id=="ignore")return;if (window.event.toElement!=source)source.style.backgroundColor=originalcolor;}
function changeto(highlightcolor, layerNum) {if ( ! isIE ) return true;source = document.all( layerNum );if (source.tagName=="TR"||source.tagName=="TABLE")return;while(source.tagName!="TD")source=source.parentElement;if (source.style.backgroundColor!=highlightcolor)source.style.backgroundColor=highlightcolor;}
function changeto1(highlightcolor){if (!isIE) return true;source=window.event.srcElement;if (source.tagName=="TR"||source.tagName=="TABLE")return;while(source.tagName!="TD")source=source.parentElement;if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore")source.style.backgroundColor=highlightcolor;}function changeback(originalcolor){if (window.event.fromElement.contains(window.event.toElement)||source.contains(window.event.toElement)||source.id=="ignore")return;if (window.event.toElement!=source)source.style.backgroundColor=originalcolor;}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	
	if (!d) {
		d = document;
	}
		
	if ( (p=n.indexOf('?')) > 0 && parent.frames.length ) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring( 0, p );
	}

	if ( !(x=d[n]) && d.all ) {
		x = d.all[n];
	}
	
	for ( i=0; !x && i < d.forms.length; i++ ) {
		x = d.forms[i][n];
	}

	for ( i=0; !x && d.layers && i < d.layers.length; i++ ) {
		x = MM_findObj( n, d.layers[i].document );
	}

	if ( !x && d.getElementById ) {
		x = d.getElementById( n );
	}

	return x;
}

function PopWin(p_Where, p_Width, p_Height, p_Name, p_MenuBar, p_ToolBar) {
	if (p_Width == '' || typeof(p_Width) == 'undefined') {
		p_Width = 560;
	}
	if (p_Height == '' || typeof(p_Height) == 'undefined') {
		p_Height = 400;
	}
	if (p_Name == '' || typeof(p_Name) == 'undefined') {
		str = new String();
		p_Name = str.concat('fetch_pop', pop_win_count.toString());
	} else {
		str = new String();
		p_Name = str.concat( p_Name, pop_win_count.toString() );
	}
	pop_win_count++;
	SetCookie('popup_win_count',pop_win_count,exp,'/');
	if (p_MenuBar == '' || typeof(p_MenuBar) == 'undefined') {p_MenuBar = 'yes'}
	if (p_ToolBar == '' || typeof(p_ToolBar) == 'undefined') {p_ToolBar = 'no'}
	var win=open(p_Where, p_Name, 'menubar='+p_MenuBar+',toolbar='+p_ToolBar+',width=' + p_Width + ',height=' + p_Height + ',resizable=1,scrollbars=1');
	win.moveTo(0,0);
	win.focus();
	return win;
}
function PopWinWithoutMenu(p_Where, p_Width, p_Height, p_Name) {
	if (p_Width == '') {p_Width = 560}
	if (p_Height == '') {p_Height = 400}
	if (p_Name == '') {p_Name = 'fetch_pop'}
	var win=open(p_Where, p_Name, 'menubar=no,toolbar=no,width=' + p_Width + ',height=' + p_Height + ',resizable=1,scrollbars=1');
	win.moveTo(0,0);
	win.focus();
	return win;
}
function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++)
		if(ok.indexOf(e.charAt(i))<0)	return (false);

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two))	return (-1);		
	}
}


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
/*    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4
    */

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function



/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes( element, do_check )
{
	var elts = MM_findObj(element); 
	var elts_cnt = (typeof(elts.length) != 'undefined')
			? elts.length
			: 0;

	if (elts_cnt) {
		for (var i = 0; i < elts_cnt; i++) {
			elts[i].checked = do_check;
		} // end for
	} else {
		elts.checked = do_check;
	} // end if... else

	return true;
} // end of the 'setCheckboxes()' function

function maximize_window( ) {
	if (window.screen) {

		self.moveTo(0,0);
		self.resizeTo(screen.availWidth,screen.availHeight);

	}
}

/**
 * Calls all registered on load functions for this page.
 *
 * This function is not a user function. It is called by every
 * page's onLoad event.
 *
 * Returns nothing.
 * Accepts nothing.
 *
 */

function check_for_onload_function() {
	if (window.onloadfunc != undefined) {
		onloadfunc();
	}

	if (onload_functions != undefined) {
	//onload_functions = g_condense_array(onload_functions);
		for (var i =0; i < onload_functions.length; i++ ) {
			//alert("now i'm doing " + i + " with length " + onload_functions[i].length);
			if (onload_functions[i] != undefined) {
				for (var j = 0; j < onload_functions[i].length; j++ ) {
					//alert(i + " " + j + " " + onload_functions[i][j])
					onload_functions[i][j]();
				}
			}
		}
	}
}

