/*
 * loginForm.js
 * http://www.aha.org
 *
 * @projectDescription OAM login form js
 * @author Rey Echevarria rechevarria@aha.org
 * @copyright American Hospital Association
 * @version 1.0
 * @updated 2008-11-18
*/

/* ****************************** *
 * Initialization
 * ------------------------------ */

ADS.addEvent(window, 'load', initDynElements);
ADS.addEvent(window, 'load', initForm);
ADS.addEvent(window, 'load', initCollapsed);
ADS.addEvent(window, 'load', initAddtlInfo);

/* End Initialization 
 * ------------------------------ */
 
/* ****************************** *
 *	Utility Functions 
 * ------------------------------ */

function initDynElements() {
	var sitedomain = getURLParam('domain') || getDomain().toLowerCase();
	logMsg(sitedomain+' links are being set');
	var docroot = sitedomain;
	sitedomain = sitedomain.replace(/-/, '');
	var el = null;
	if (sitedomain == 'osso') {
		sitedomain = 'hospitalconnect';
		docroot = 'hospitalconnect';
	}
	if (sitedomain == 'ashe') {
		docroot = 'oam-'+docroot;
	}
	
	if (sitedomain in PORTAL) {
		el = ADS.$('logoLink');
		if (el) { el.href = 'http://www.'+PORTAL[sitedomain]; }
		
		el = ADS.$('helpPageLink');
		if (el) { el.href = 'http://www.'+PORTAL[sitedomain]+'/'+docroot+'/oam/help.html'; }
		
		el = ADS.$('privacyPageLink');
		if (el) { el.href = 'http://www.'+PORTAL[sitedomain]+'/'+docroot+'/oam/privacy.html'; }
		
		el = ADS.$('contactPageLink');
		if (el) { el.href = 'http://www.'+PORTAL[sitedomain]+'/'+docroot+'/oam/contact_us.html'; }
			
		el = ADS.$('forgotPasswordLink');
		if (el) { el.href = OAMLINK.forgotPassword+'?redirectPage=http://www.'+PORTAL[sitedomain]+'/'+docroot+'/oam/welcome.html'; }
				
		el = ADS.$('registerLink');
		if (el) { el.href = OAMLINK.register+'?redirectPage=http://www.'+PORTAL[sitedomain]+'/'+docroot+'/oam/welcome.html'; }
		
		var el = ADS.$('loginLogoImg');
		if (el) { el.alt = ADS.$('logoLink').href; }
		
		el = ADS.$('errorLogoImg');
		if (el) { 
			el.src = '/oam/images/logos/'+getDomain()+'.gif';	 
			el.alt = ADS.$('logoLink').href;
		}
	}
}

function initForm() {
	var frm = ADS.$('OAMloginform');
	if (frm) {
		frm['userid'].focus();	
		ADS.addEvent(frm['login'], 'click', validateForm);
	}
}

function validateForm() {
	var frm = ADS.$('OAMloginform');
	
	if ((frm['userid'].value == '') || (frm['password'].value == '')) { 
		alert('Please enter a username and password.');
		return false; 
	}
	
	if (frm['remember_me'].value == 'yes') {
		logMsg('initiating cookie set...');
		//setRememberMe();
		submitForm();// comment out this line if setting cookie
	}
	else {
		submitForm();
	}
	return true;
}

function submitForm() {
	var frm = ADS.$('OAMloginform');
	frm.action = '/oam/router.jsp';
	frm.method = 'GET';
	frm.submit();
}

function setRememberMe() {
	ADS.xssRequest(
		'http://www.hospitalconnect.com/oam/oam.dhtml?task=remember&x='+Math.random(),{
			completeListener:function() { 
				if (this.responseJSON.status == 1) {
					// AHA_REM cookie has been set
					logMsg('done setting cookie - status='+this.responseJSON.status);
					submitForm();	
				}
				
			},
			errorListener:function() { 
				logMsg('ERROR: '+this.statusText); 	
			}
		}
	);
	return;
}

/* End Utility Functions
 * ------------------------------ */
 
/* ****************************** *
 *	Sliding DIV Functions 
 * ------------------------------ */

function initCollapsed(){
var a = document.getElementsByTagName('div');
	for(var k=0;k<a.length;k++){
		if(a[k].className == 'collapsible'){
			var n = a[k].firstChild;
			while(n.nodeName.toLowerCase() != 'h1'){n = n.nextSibling;}
			a[k].tween = new Tween(a[k],"height",n.scrollHeight,a[k].scrollHeight,500,'px');
			a[k].style.height = n.scrollHeight+'px';
			n.onclick = function(){
				if(!this.parentNode.tween.playing){
					this.style.backgroundImage = "url("+(this.parentNode.tween.position == 0?"/oam/images/minus.gif":"/oam/images/plus.gif")+")";
					this.parentNode.tween.play();
				}
				
			}
		}
	}	
}

function initAddtlInfo(){
	if(document.getElementById('remember_me')){
		var r = document.getElementById('remember_me');
		var f = document.getElementById('addtlInfo');
		r.tween = new Tween(f,'height',0,f.scrollHeight,300,'px');
		f.style.height = 0;
		r.onclick = function(){
			this.tween.play(); 
		}
	}
}

// Image Popup Script
// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
writeln('width=300-(document.body.clientWidth-document.images[0].width);');
writeln('height=300-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}

/* End Sliding DIV Functions 
 * ------------------------------ */