//to be able to target this window from "popups"
window.name = "em_Main";

/**
 * BrowserDetect script from http://www.quirksmode.org/js/detect.html
 * USAGE: you can query three properties of the BrowserDetect object:
 * Browser name: BrowserDetect.browser
 * Browser version: BrowserDetect.version
 * OS name: BrowserDetect.OS
 */
if(!BrowserDetect) {
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{	// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 	// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();
}

// 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 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"); 		
	
		
}

/**
 * Bring back to the news page
 * and close the current active window
 * if it's a window different than
 * em_Main window.
 */
function continueAsGuest() {	
	window.open("index.jsp", "em_Main");
	if(window.name != "em_Main") {
		window.close();
	}
}

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 5 seconds of delay.
 */
function helpMsg(e, msg) {
	var id = showHelpTip(e, msg, false);
}


/**
 * 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;
}
