
/**
 * Add an event w/o hijacking the handler
 */
function addEvent( target, event, method ) {
	if ( target.addEventListener ) {
		target.addEventListener( event, method, false );
	} else if ( target.attachEvent ) {
		target.attachEvent( 'on' + event, method );
	} 
}


/**
 * Perform the login
 */
function resiteLogin() {
	
	/**
	 * Original parse query string function
	 */
	this.parseQueryString = function(str) {
		str = str ? str : location.search; 
		var query = str.charAt(0) == '?' ? str.substring(1) : str;
		var args = new Object();
		if (query) {
			var fields = query.split('&');
			for (var f = 0; f < fields.length; f++) {
				var field = fields[f].split('=');
				args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
			}
		}
		
		return args;
		
	}
	
	
	/**
	 * Hide an element
	 */
	this.hide = function( id) {
		document.getElementById( id).style.display = 'none';
		document.getElementById( id).style.visibility = 'hidden';
		
	}
	
	/**
	 * Show an element
	 */
	this.show = function( id) {
		document.getElementById( id).style.display = 'block';
		document.getElementById( id).style.visibility = 'visible';
		
	}
	
	
	var args = this.parseQueryString();
	var returned = false;
	
	
	if ( args['cw_e'] !== undefined) {
		// replace messages
		document.getElementById( resiteLogin.MESSAGE_DIV).innerHTML = resiteLogin.messages[args['cw_e']];
		this.show( resiteLogin.MESSAGE_DIV);
		returned = true;
		
	}
	
	
	if ( args['cw_actn'] == 'getLogin') {
		// show forgot passwd form, hide login form
		returned = true;
		this.hide( resiteLogin.LOGIN_DIV);
		this.show( resiteLogin.FORGOT_DIV);
		
	} else {
		// show login box
		this.hide( resiteLogin.FORGOT_DIV);
		this.show( resiteLogin.LOGIN_DIV);
		
	}
	
	
	if ( returned) {
		// we were returned from the server for some reason
		// highlight #login
	}
	
	
}


/* IDs for dhtml goodness */
resiteLogin.LOGIN_DIV = 'loginbox';
resiteLogin.MESSAGE_DIV = 'messages';
resiteLogin.FORGOT_DIV = 'forgot';


/* Constants for messages */
resiteLogin.LOGIN_FAILED = 0;
resiteLogin.ACCOUNT_DEACTIVATED = 2;
resiteLogin.ACCOUNT_NO_ACCESS = 3;
resiteLogin.INVALID_BROWSER = 4;
resiteLogin.NO_JS = 5;
resiteLogin.POPUP_BLOCKER = 6;
resiteLogin.USER_AND_PASS = 7;
resiteLogin.LOGIN_SENT = 8;
resiteLogin.LOGIN_FAILED = 9;
resiteLogin.EMAIL_NOT_FOUND = 10;
resiteLogin.REMIND_EMAIL_NOT_FOUND = 11;
resiteLogin.MULTIPLICITY = 12;

/* Message text */
resiteLogin.messages = new Array();
resiteLogin.messages[resiteLogin.LOGIN_FAILED] = '<p>Login failed.  Please try again.</p>';
resiteLogin.messages[resiteLogin.ACCOUNT_DEACTIVATED] = '<p>Your account has been deactivated. Please contact your supervisor.</p>';
resiteLogin.messages[resiteLogin.ACCOUNT_NO_ACCESS] = '<p>Your account is active but you don\'t have community access. Please contact your supervisor.</p>';
resiteLogin.messages[resiteLogin.INVALID_BROWSER] = '<p>Please use MS Internet Explorer 6+ or Mozilla Firefox</p>';
resiteLogin.messages[resiteLogin.NO_JS] = '<p>Please use a Javacript enabled browser</p>';
resiteLogin.messages[resiteLogin.POPUP_BLOCKER] = '<p>A popup-blocker has prevented CommunityWare<sup>SM</sup> from opening. The popup-blocker must be disabled or configured to allow popups from \'resiteonline.com\'.  You may also try holding the \'Ctrl\' key while logging in to circumvent the popup-blocker.</p>';
resiteLogin.messages[resiteLogin.USER_AND_PASS] = '<p>Please enter both a username and password</p>';
resiteLogin.messages[resiteLogin.LOGIN_SENT] = '<p>Your CommunityWare<sup>SM</sup> login information has been sent</p>';
resiteLogin.messages[resiteLogin.LOGIN_FAILED] = '<p>Sending login failed.<p>Please contact your supervisor or <a href="mailto:support@resiteit.com">Customer Support</a> for assistance.</p>';
resiteLogin.messages[resiteLogin.NO_EMAIL] = '<p>Sorry, a CommunityWare<sup>SM</sup> login was not found matching that email address</p>';
resiteLogin.messages[resiteLogin.NO_REMINDER_EMAIL] = '<p>Please enter a valid email address</p>';
resiteLogin.messages[resiteLogin.MULTIPLICITY] = '<p>Sorry, more than one CommunityWare<sup>SM</sup> login was found. Please contact your supervisor or <a href="mailto:support@resiteit.com">Customer Support</a> for assistance.</p>';


/* Attach event handler */
addEvent( window, 'load', resiteLogin);


