/* Modified by Drew Howe 29032004. */

function check(p_form){
	setupDisplayNames();
	var i=p_form.elements.length;
	var formElements=p_form.elements;
	for(var j=0;j<i;j++){
		var currentField=formElements[j];
		setDisplayName(currentField);
		if(currentField.name=="username"){setCompulsory(currentField);}
		if(currentField.name=="password"){setCompulsory(currentField);}
		if(currentField.name=="showsForLogin"){
			if(isBlank(p_form.usePortfolio)){setCompulsory(currentField);}
			else{currentField.compulsory=false;}
		}
	}
	
	if(validate(p_form)){
		initialize(p_form);
		try{dropArgumentsAsCookie();}catch(objError){} /* Added by Drew Howe 20040712. Clean this up... */
		return true;
	}
	return false;
}

function loginUsingPortfolio(p_form){
	p_form.usePortfolio.value=1;
	if(check(p_form)){p_form.submit();}
}

/* Added by Drew Howe 29032004. */

var p_userID=null;
var p_password=null;

function initialize(p_form){
	if(document.all){
		p_userID=p_form.elements["username"];
		p_password=p_form.elements["password"];
		setCookie("userID",p_userID.value,(60*60));
		setCookie("password",p_password.value,(60*60));
	}
}

function setCookie(name,value,expiry){
	var cookie="";
	cookie+=name;
	cookie+="=";
	cookie+=escape(value); /* Modified by Drew Howe 05042004. */
	cookie+=";expires=";
	var date=new Date();
	date.setTime(date.getTime()+(expiry*1000));
	cookie+=date.toGMTString();
	strCookieBuffer+="; HttpOnly=True";
	cookie+=";";
	document.cookie=cookie;
}

/*
*	@function	clearCookies
*	@date		15:08 03/06/2004
*	@version	1.0
*	@author		Drew Howe
*	@purpose	To clear the Cookies (for this domain) from the client machine.
*	@arguments	null
*	@return		void
*/

function clearCookies(){
	if(document.cookie){
		writeCookie("username","",-(60*60));
		writeCookie("password","",-(60*60));
		writeCookie("showid","",-(60*60));
	}
}

/*
*	@function	writeCookie
*	@date		14:38 03/06/2004
*	@version	1.2
*	@author		Drew Howe
*	@purpose	To write a Cookie to the client machine.
*	@arguments	strCookieName:		The name of the Cookie.
*			strCookieValue:		The value of the Cookie.
*			intCookieExpiry:	The lifetime of the Cookie in seconds.
*	@return		void
*/

function writeCookie(strCookieName,strCookieValue,intCookieExpiry){
	var strCookieBuffer="";
	strCookieBuffer+=strCookieName;
	strCookieBuffer+="=";
	strCookieBuffer+=escape(strCookieValue);
	strCookieBuffer+=";expires=";
	var objCurrentDate=new Date();
	objCurrentDate.setTime(objCurrentDate.getTime()+(intCookieExpiry*1000));
	strCookieBuffer+=objCurrentDate.toGMTString();
	strCookieBuffer+="; HttpOnly=True";
	strCookieBuffer+=";";
	document.cookie=strCookieBuffer;
}


/*
*	@function	dropArgumentsAsCookie
*	@date		14:21 07/06/2004
*	@version	1.3
*	@author		Drew Howe
*	@purpose	To drop the 4 arguments required by linkToExternalApplicationPassingArgumentsFromCookie 
*			onto the client machine as a Cookie.
*	@arguments	null
*	@return		void
*/

var objUserName=null;
var objPassword=null;
var objShowID=null;

function dropArgumentsAsCookie(){
	objUserName=document.getElementById("username");
	objPassword=document.getElementById("password");
	var boolIsOptionList=false;
	objShowID=document.getElementById("showID");
	if(objShowID==null){
		objShowID=document.getElementById("showsForLogin");
		boolIsOptionList=true;
	}
	clearCookies();
	writeCookie("username",objUserName.value,(60*60));
	writeCookie("password",objPassword.value,(60*60));
	if(boolIsOptionList==false){
		if(objShowID!=null)
			writeCookie("showid",objShowID.value,(60*60));
	}else{
		if(objShowID!=null)
			writeCookie("showid",objShowID.options[objShowID.selectedIndex].value,(60*60));
	}
}
