if(!console) {
	var console = {
		info: function(m) {
			var consoleDiv = document.getElementById('console');
			if(consoleDiv) {
				consoleDiv.innerHTML += '<br>\n' + m;
			}
		}
	};
	console.info('Custom console loaded!');
}

if(window.Ext) {
	//Path to the blank image should point to a valid location on your server
	Ext.BLANK_IMAGE_URL = 'pages/ext-3.1.0/resources/images/default/s.gif';

	Ext.onReady(function(){

/*
		Ext.Ajax.on('beforerequest', this.showSpinner, this);
	    Ext.Ajax.on('requestcomplete', this.hideSpinner, this);
	    Ext.Ajax.on('requestexception', this.hideSpinner, this);	    
*/
		console.info('Ext-JS is loaded');
//	    HelpAssistant.init();
	});
}

function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

// This function has the same purpose than the timeout meta tag above, but
// was added because for some reason clicking any link that has 'javascript' in it
// makes the refresh meta not work (perhaps browsers interpret this as the page changed).
function timeout() {
	setTimeout("location.href='pages/errors/MainTimedout.jsp'", 1770000);  // NOTE: Time is in milliseconds
}

function openLoginPanel() {
    //Determine position and size of the treeParam dialog.
    var w = 800, h = 600;

    if (document.all || document.layers) {
    	w = screen.availWidth;
    	h = screen.availHeight;
    }

    var popW = 500, popH = 275;
    var leftPos = (w-popW)/2, topPos = (h-popH)/2;

    var paramWin = window.open("pages/home/MainLogin.jsp","_self","width=" + popW + ", height=" + popH + ", top=" + topPos + ",left=" + leftPos + ", toolbar=no,scrollbars=no, dependent=yes, resizable=no"); 		
}


function openAMemberRegistrationWindow(amemberBase) {
	//Determine position and size of the treeParam dialog.
	var w = 800, h = 600;

	if (document.all || document.layers) {
   		w = screen.availWidth;
	   	h = screen.availHeight;
	}

	var popW = w, popH = h;

	var leftPos = 0, topPos = 0;

 	var paramWin = window.open(amemberBase + "signup.php","_self","width=" + popW + ", height=" + popH + ", top=" + topPos + ",left=" + leftPos + ", menubar=yes,directories=yes,toolbar=yes,status=yes,scrollbars=yes, dependent=no, resizable=yes"); 		
	
		
}

	
function openNeedSubscriptionWindow() {
	//Determine position and size of the treeParam dialog.
	var w = 800, h = 600;

	if (document.all || document.layers) {
   		w = screen.availWidth;
	   	h = screen.availHeight;
	}

	var popW = 500, popH = 275;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	
 	var paramWin = window.open("navigate.do?nextPage=needSubscription","_self","width=" + popW + ", height=" + popH + ", top=" + topPos + ",left=" + leftPos + ", menubar=no,directories=no,toolbar=no,status=no,scrollbars=no, dependent=yes, resizable=no"); 		
	
}

function popup(url, popW, popH) {
	// Set defaults for variables
	if(!url) {
		alert('url must be set!');
		return;
	}
	popW = popW || 600;
	popH = popH || 375;

	if (document.all || document.layers) {
   		w = screen.availWidth;
	   	h = screen.availHeight;
	} else {
		w = 1024;
		h = 768;
	}

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
 	var paramWin = window.open(url,"popup","width=" + popW + ", height=" + popH + ", top=" + topPos + ",left=" + leftPos + ", menubar=no,directories=no,toolbar=no,status=no,scrollbars=yes, dependent=yes, resizable=yes"); 		
 	paramWin.focus();

}


function openFAQs(anchor) {
	//Determine position and size of the treeParam dialog.
	var w = 800, h = 600;

	if (document.all || document.layers) {
   		w = screen.availWidth;
	   	h = screen.availHeight;
	}

	var popW = 711, popH = h*0.6;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

 	var paramWin = window.open("pages/static/faq/faq.jsp#"+anchor,"FAQs_win","width=" + popW + ", height=" + popH + ", top=" + topPos + ",left=" + leftPos + ", menubar=no,directories=no,toolbar=no,status=yes,scrollbars=yes, dependent=no, resizable=yes"); 		
	
		
}


function loading(cmpnt) {
/*
	document.body.style.cursor='wait';
	cmpnt.style.cursor='wait';
	cmpnt.onclick=new Function('return false');
	cmpnt.onchange=new Function('return false');
	return;
*/	
}

/** Sets cursor to hourglass. Optionally pass
 *	object to change cursor on (text box for
 *	example). 
 */
function cursorWait(cmp) {
	document.body.style.cursor='wait';
	if(cmp) {
		cmp.style.cursor='wait';
	}
}
/** Sets cursor to arrow + hourglass. Optionally pass
 *	object to change cursor on (text box for
 *	example). 
 */
function cursorProgress(cmp) {
	document.body.style.cursor='progress';
	if(cmp) {
		cmp.style.cursor='progress';
	}
}
/** Sets cursor to default (arrow). Optionally pass
 *	object to change cursor on (text box for
 *	example). 
 */
function cursorClear(cmp) {
	document.body.style.cursor = 'default';
	if(cmp) {
		cmp.style.cursor='text';
	}
}

function toggleTrace() {
	if(document.getElementById("stackTrace").style.display == 'block') {
		document.getElementById("stackTrace").style.display  = 'none';
	} else {
		document.getElementById("stackTrace").style.display = 'block';
	}
}

function sendBugReport(optionalInfo) {
	var win = window.open("navigate.do?nextPage=bugReport&cu_optional=" + optionalInfo, "em_Main");
	if(window.name != "em_Main") {
		window.close();
	}
}

/** Switches a given style class for another for a given
 *	html object. Since this object might have many
 *	classes and we want to overwrite only one of these,
 *  then we use string replace instead of just assigning.
 */
function switchStyleClass(obj, src, dest) {
	if(typeof obj == 'string') {
		obj = document.getElementById(obj);
	}
	if(obj) {
		var classes = new String(obj.className);
		if(classes.indexOf(src) >= 0) {
			obj.className = classes.replace(src,dest);
		} else {
			classes += " " + dest;
		}
	}
}



/**
 * Show, hide or toggle the visibility
 * of an html element (typically a DIV).
 *
 * Parameters:
 * id: the id of the html element
 * status: either show / hide / toggle
 */
function showHideElement(id, status) {
    var toggle = false;
    var elmt = document.getElementById(id);
    
    if(elmt) {
	    var tagName = elmt.tagName;
	
	    if(status == 'show') {
			elmt.style.display = '';
	
	    } else if(status == 'hide') {
			elmt.style.display = 'none';
	
	    } else if(status == 'toggle') {
		    var currDisplayStyle = elmt.style.display;
			if(currDisplayStyle == 'none') {
				elmt.style.display = '';
			} else {
				elmt.style.display = 'none';
			}
	
	    } else {
	        alert('Wrong value for show/hide. Please use either "show", "hide" or "toggle" for status...');
	    }
    } else {
    	alert('Unable to find element named "' + id + '"');
    }
	
}

/**
 * Functions to alternate the visibility of up to four
 * html elements. Note that each parameter is optional,
 * so you can call the function with one, two, three or
 * four element ID's to toggle.
 *
 * Use it to create an illusion of expanding / collapsing
 * blocks (div's for example).
 */
function toggleVisibility(id1, id2, id3, id4) {
    if(id1) showHideElement(id1, 'toggle');
    if(id2) showHideElement(id2, 'toggle');
    if(id3) showHideElement(id3, 'toggle');
    if(id4) showHideElement(id4, 'toggle');
}

/**
 * Displays a floating help message that hides itself either on
 * mouse out or after some time delay.
 */
var HelpTip = new function() {
	var lastTipElmt = null;
	var lastTip = null;
	
	this.helpMsg = function(e, msg) {
		var target = (e && e.target) || (event && event.srcElement);
//		console.info('helpMsg: ' + target);

		if(!isElmtLastTip(e)) {
//			console.info('helpMsg: new tip to instantiate');

			// target isn't the same as last tip
			if(lastTip != null) {
//				console.info('helpMsg: lastTip != null; destroying previous tip');
				lastTip.destroy();
			}
			
			lastTip = new Ext.QuickTip({
				html: msg,
				anchorToTarget: true,
				target: target
			});
			lastTipElmt = target;
			
		} else {
//			console.info('helpMsg: same as last tip, doing nothing');
		}

		var xy = getMouseXY(e);
		// put a small offset besides the mouse cursor
		xy[0] = xy[0] + 15;
		xy[1] = xy[1] + 18;
		lastTip.showAt(xy); // weird: Ext displays a small triangle near the bottom left corner of the tooltip on first show, but it dissapears afterwards, hence the 2 show functions here... 
		lastTip.hide();
		lastTip.showAt(xy);
	}
	
	this.hideHelpTip = function() {
		if(lastTip != null) {
			lastTip.hide();
		}
	}
	
	/**
	 * Find the correct element which was targetted. This is because
	 * mouse over event targets can be children of the dom node which
	 * captured the mouse over event.
	 */
	var isElmtLastTip = function(e) {
		if(lastTip == null) {
			return false;
		}
		
		var target = (e && e.target) || (event && event.srcElement);
		if(!target) return false;
		
		var found = false;
		var e = target;
		var maxCpt = 0;
		while(!found && e) {
			if(e == lastTipElmt) {
				found = true;
			} else {
				e = e.parentNode;
			}
			maxCpt++;
			if(maxCpt > 10) {
				// Speed optimization, limit the number of iterations just in case...
				break;
			}
		}
		
		return found;		
	}

}


/**
 * Get the mouse coordinates.
 */
var getMouseXY = function(e) {
	var xy = [];
	if (Ext.isIE) { // grab the x-y pos.s if browser is IE
		xy[0] = event.clientX + document.body.scrollLeft
		xy[1] = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		xy[0] = e.pageX
		xy[1] = e.pageY
	}  
	// catch possible negative values in NS4
	if (xy[0] < 0){xy[0] = 0}
	if (xy[1] < 0){xy[1] = 0}  
	return xy
}

function helpMsg(e, msg) {
	HelpTip.helpMsg(e, msg);
}
function showHelpTip(e, msg) {
	HelpTip.helpMsg(e, msg);
}
function hideHelpTip(e) {
	HelpTip.hideHelpTip();
}


/**
 * Dynamically finds the real height of an element
 * once it rendered by the browser (instead of the CSS
 * height attribute which might not be set at all).
 * You must pass an object ID here.
 */
function findElementHeight(id) {
	var d = document.getElementById(id);
	if(d) {
		var divHeight;
		if(d.offsetHeight) {
		     divHeight=d.offsetHeight;
		} else if(d.style.pixelHeight) {
		     divHeight=d.style.pixelHeight;
		} 
		return divHeight;
		
	} else {
		return;
	}
}

/**
 * Looks for an element (e) inside an array (a).
 * Apparently IE doesn't support the function indexOf
 * on arrays so I had to code my own.
 *
 * Like the standard indexOf, returns an index >= 0
 * if the element is found in the array, or -1 if not.
 */
function arrayFindIndexOf(a, e) {
    if(!a || !e) {
    	return;
    }
    
    for(i = 0; i < a.length; i++) {
    	if(a[i] == e) {
    		return i;
    	}
    }
    
    return -1;
}








/**
 * Writes a small html code that displays the assistant image with 
 * attached functionnality.
 */
function getAssistantLink(msgId) {
	HelpAssistant.getAssistantLink(msgId);
}

function getAssistantLinkHtml(msgId, positionStyle) {
	HelpAssistant.getAssistantLinkHtml(msgId, positionStyle);
}

function writeAssistantLinkAtPos(msgId, positionStyle) {
	HelpAssistant.writeAssistantLinkAtPos(msgId, positionStyle);
}
