// loginLink.js

function getCookieValue(offset) {
	var endstr = document.cookie.indexOf(';', offset);
	if ( endstr == -1 ) {
		endstr=document.cookie.length;
	}
	var result = unescape(document.cookie.substring(offset,endstr));
	if ( result != null && result.length < 1) {
		result = null;
	}
	return result;
}

function getCookie(name) {
	var arg = name + "=";
	var argLen = arg.length;
	var cookieLen = document.cookie.length;
	var i = 0;
	while ( i < cookieLen ) {
		var j = i + argLen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieValue(j);
		}
		i++;
		if (i + argLen > cookieLen) break;
	}
	return null;	
}
	

function getLoggedInTopText() {
	var loggedInTopText='<img src="/images/help_left.jpg" width="264" height="13" alt=""><a href="javascript:popup(\'/help/viewHelp.do\',\'help\')" onmouseover="mOver(\'help\');return true" onmouseout="mOut(\'help\');return true"><img src="/images/help_off.gif" width="35" name="help" height="13" alt="help" border="0"></a><img src="/images/seperator.gif" width="2" height="13" alt="seperator"><a href="/about_us/sitemap.html" onmouseover="mOver(\'site_map\');return true" onmouseout="mOut(\'site_map\');return true"><img src="/images/site_map_off.gif" name="site_map" width="56" height="13" alt="sitemap" border="0"></a><img src="/images/seperator.gif" width="2" height="13" alt="seperator"><a href="/forums/wcg/logout" onmouseover="mOver(\'sign_out_top\');return true" onmouseout="mOut(\'sign_out_top\');return true"><img src="/images/sign_out_top_off.gif" width="57" name="sign_out_top" height="13" alt="sign out" border="0"></a><img src="/images/grey.gif" width="16" height="13" alt=""><br><img src="/images/tagline.jpg" width="511" height="73" alt="technology solving problems">';
	return loggedInTopText;
}

function checkLogin() {
	var cookieValue = getCookie('member');
    if ( cookieValue != null ) {
		document.getElementById('loggedInTop').innerHTML=getLoggedInTopText();
    }
}

function redirectToForums() {
	var cookieValue = getCookie('member');
    if ( cookieValue != null ) {
    	self.location.href="/forums/wcg";
    } else {
		self.location.href="/forums/wcg/index";
    }
}

function writeForm(name, action, method) {
	var host = window.location.host;
	if ( host.indexOf("127.0.0.1:9080") > -1 ) {
		host = "127.0.0.1:9443";
	} else if ( host.indexOf("127.0.0.1") > -1 ) {
		host = "127.0.0.1";
	} else if ( host.indexOf("wcgdev.net") > -1 ) {
		host = "secure.wcgdev.net";
	} else if (	host == 'worldgrid.chicago.ibm.com' )  {
		host = 'worldgrid.chicago.ibm.com';
	} else if (	host == 'member.chicago.ibm.com' )  {
		host = 'member.chicago.ibm.com';
	} else if (	host == 'beta.worldcommunitygrid.org' )  {
		host = 'beta.worldcommunitygrid.org';
	} else {
		host = 'secure.worldcommunitygrid.org';
	}
	var url = "https://" + host + action;
	var form = "<form name=\""+name+"\" action=\"" + url + "\" method=\"" + method + "\">";
	document.write(form);
}

function writeLink(action) {
	var host = window.location.host;
	if ( host.indexOf("127.0.0.1:9080") > -1 ) {
		host = "127.0.0.1:9443";
	} else if ( host.indexOf("127.0.0.1") > -1 ) {
		host = "127.0.0.1";
	} else if ( host.indexOf("wcgdev.net") > -1 ) {
		host = "secure.wcgdev.net";
	} else if (	host == 'worldgrid.chicago.ibm.com' )  {
		host = 'worldgrid.chicago.ibm.com';
	} else {
		host = 'secure.worldcommunitygrid.org';
	}
	var url = "https://" + host + action;
	var link = "<a href=\""+url+"\" class=\"contentLink\">";
	document.write(link);
}

function submitByEnterKey(event, x)
{
	browserNSFrom = (document.layers) ? true : false;
	
	var code = 0;
	if (browserNSFrom)
		code = event.which;
	else
		code = event.keyCode;
	if (code == 13)
	{
		document.forms[x].submit();
		event.cancelBubble = true;
		return false;
	}
	return true;
}
