//get the product from the URL
//set the default to FCWeb - this is for use by the search function
var product = window.location.search.slice(1);
if (product == "")
	product = "logform";

//write the name of the product being logged into on the login page

function writeName()
{		document.write("<img src='/login/images/" + product + ".jpg' border='0'>");
}
 
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function FCWebRedirect()
{	if ((product == "logform") || (product == "corpad"))
if (document.referrer.indexOf("thomson.com") == -1)
	{
	window.location = "http://www.thomson.com/financial/financial.jsp";
	}
	else
	{
	window.location = "http://www1.firstcall.com/login/fcweb.shtml";
	}
}

// name      - name of the cookie
// value     - value of the cookie
// [expires] - expiration date of the cookie 
//             (defaults to end of current session)
// [path]    - path for which the cookie is valid 
//             (defaults to path of calling document)
// [domain]  - domain for which the cookie is valid
//             (defaults to domain of calling document)
// [secure]  - Boolean value indicating if the cookie transmission 
//             requires a secure transmission
//
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
//
function setCookie(name, value, expires, path, domain, secure)
{
  // create the cookie
  var curCookie = name + "=" + escape(value) +
                  ((expires) ? "; expires=" + expires.toGMTString() : "") +
                  ((path) ? "; path=" + path : "") +
                  ((domain) ? "; domain=" + domain : "") +
                  ((secure) ? "; secure" : "");

  // set the cookie
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
//
function getCookie(name)
{	var dc = document.cookie;
  	var prefix = name + "=";
  	var begin = dc.indexOf("; " + prefix);

  	if (begin == -1) 
	{	begin = dc.indexOf(prefix);
		if (begin != 0) 
		{	return null;
    	}
  	}
  	else 
	{	begin += 2;
  	}

  	var end = document.cookie.indexOf(";", begin);
	
  	if (end == -1) 
	{	end = dc.length;
  	}

	return unescape(dc.substring(begin + prefix.length, end));
}

// name     - name of the cookie
// [path]   - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to 
//            create cookie)
// * path and domain default if assigned null or omitted if no explicit 
//   argument proceeds
//
function deleteCookieOld(name, path, domain)
{	if (getCookie(name)) 
	{	
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}

function deleteCookie(name, path, domain)
{	if (getCookie(name))
		{	expireDate = new Date;
			expireDate.setDate(expireDate.getDate()-1);
			document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=" + expireDate.toGMTString();
		}
}

function ProdLogin()
{	//This is for FCWeb and Corporate Advantage
	if (product == "logform" || product == "corpad")
	{	document.logform.UNAME.value=document.Login.Username.value
		document.logform.PASSWORD.value=document.Login.Password.value
		document.logform.submit()
	}
	
	//This is for FCWeb Espanol
	if (product == "rde")
	{	document.logform.UNAME.value=document.Login.Username.value
		document.logform.PASSWORD.value=document.Login.Password.value
		document.logform.DIRECTORY.value="FIRSTCALL/APPS/FC622/templates/spa/inet/"
		document.logform.action = "http://www.firstcall.com/cgi-bin/gx.cgi/AppLogic+FIRSTCALL.APPS.FC622.FCLogin.FCLoginInterface"
		document.logform.submit()
	}
	
	//This is for FCWeb Japanese
	if (product == "rdj")
	{	document.logform.UNAME.value=document.Login.Username.value
		document.logform.PASSWORD.value=document.Login.Password.value
		document.logform.DIRECTORY.value="FIRSTCALL/APPS/FC622/templates/jpn/inet/"
		document.logform.action = "http://www.firstcall.com/cgi-bin/gx.cgi/AppLogic+FIRSTCALL.APPS.FC622.FCLogin.FCLoginInterface"
		document.logform.submit()
	}
	
	//This is for FCWeb Korean
    if (product == "rdk")
	{	document.logform.UNAME.value=document.Login.Username.value
		document.logform.PASSWORD.value=document.Login.Password.value
		document.logform.DIRECTORY.value="FIRSTCALL/APPS/FC622/templates/kor/inet/"
		document.logform.action = "http://www.firstcall.com/cgi-bin/gx.cgi/AppLogic+FIRSTCALL.APPS.FC622.FCLogin.FCLoginInterface"
		document.logform.submit()
	}

	//This is for FCWeb Portuguese
	if (product == "rdp")
	{	document.logform.UNAME.value=document.Login.Username.value
		document.logform.PASSWORD.value=document.Login.Password.value
		document.logform.DIRECTORY.value="FIRSTCALL/APPS/FC622/templates/por/inet/"
		document.logform.action = "http://www.firstcall.com/cgi-bin/gx.cgi/AppLogic+FIRSTCALL.APPS.FC622.FCLogin.FCLoginInterface"
		document.logform.submit()
	}
	
	//This is for Estimate Monitor
	if (product == "estmon")
	{	document.estmon.uname.value=document.Login.Username.value
		document.estmon.pword.value=document.Login.Password.value
		document.estmon.submit()
	}
	
	//This is for BondWatch Web
	if (product == "bww")
	{	document.bww_login.username.value=document.Login.Username.value.toUpperCase();
		document.bww_login.password.value=document.Login.Password.value.toUpperCase();
		document.bww_login.submit()
	}
	
	//This is for Insight
	if (product == "insight")
	{	document.insight.username.value=document.Login.Username.value
		document.insight.Password.value=document.Login.Password.value
		document.insight.submit()
	}
	
	if (product == "shwch")
	{	document.shwch.username.value=document.Login.Username.value;
		document.shwch.password.value=document.Login.Password.value;
		document.shwch.submit();
	}
}

function Login()
{	//make sure a username and password were entered
	var errorMessage = "";
	if (document.Login.Username.value == null || document.Login.Username.value == "")
		errorMessage = "You must enter a User Name";
	if (document.Login.Password.value == null || document.Login.Password.value == "")
	{	if (errorMessage == "")
			errorMessage = "You must enter a Password";
		else
			errorMessage = errorMessage + " and Password";
	}
	
	//if either field is blank - alert the user	
	if (errorMessage != "")
	{	alert(errorMessage);
		document.Login.Username.focus();
	}	
	else
	{	//if they checked 'save password' set a cookie
		if (document.Login.saveit.checked)
		{ 	today = new Date()
			today.setTime(today.getTime() + Math.round(86400000*100));
			setCookie("FCLOG", product + "|" + document.Login.Username.value + "|" + document.Login.Password.value, today, "/" , false)
		}
		//then login
		ProdLogin()
	}
}

function checkForCookie()
{	//try to get a cookie
	var cookieInfo = getCookie("FCLOG");
	
	//if there is no cookie - jsut go to the login page
	if (cookieInfo == null)
		return;
		
	//if there is a cookie - see if the user is logging into the same product
	else
	{	var a = new Array();
		a = cookieInfo.split("|");
		var cookieProduct = a[0];
		var cookieName = a[1];
		var cookiePass = a[2];
						
		//if it's the same product as the one they want to log into
		//get the username and password from the cookie, set the document u & p
		if (product == cookieProduct)
		{	document.Login.Username.value = cookieName;
			document.Login.Password.value = cookiePass;
			document.Login.saveit.checked = true;
			showReset();
			// we decided not to log the client in automatically, so they can see any updates ProdLogin();
		}
		//if it's different, then delete the cookie and jump to the normal login process
		//else
		//{	deleteCookie("FCLOG");
		//	return;
		//}
	}
}