function switchSystem(target){

    switch(target){

	case 'owa':
	    document.getElementById('owa-div').className = "display";
	    document.getElementById('legacy-div').className = "hidden";
	    break;

	case 'legacy':
	    document.getElementById('legacy-div').className = "display";
	    document.getElementById('owa-div').className = "hidden";
	    
	    document.getElementById('imapuser').focus();
	    break;
	
	default:
	    document.getElementById('legacy-div').className = "hidden";
	    document.getElementById('owa-div').className = "hidden";
	    break;
    }

}


function submit_login()
{
    if (document.implogin.imapuser.value == "") {
        alert('Please provide your username and password');
        document.implogin.imapuser.focus();
        return false;
    } else if (document.implogin.pass.value == "") {
        alert('Please provide your username and password');
        document.implogin.pass.focus();
        return false;
    } else {
        return true;
    }
}

function enter_key_trap(e)
{
    var keyPressed;

    if (document.layers) {
        keyPressed = String.fromCharCode(e.which);
    } else if (document.all) {
        keyPressed = String.fromCharCode(window.event.keyCode);
    } else if (document.getElementById) {
        keyPressed = String.fromCharCode(e.keyCode);
    }

    if ((keyPressed == "\r" || keyPressed == "\n") && (submit_login())) {
        document.implogin.submit();
    }
}

function updatePort()
{
}

/*
// Setup the enter keytrap code
if (window.document.captureEvents != null) {
    window.document.captureEvents(Event.KEYPRESS);
    window.document.onkeypress = enter_key_trap;
}
*/

//Caps Lock Check
function checkCapsLock( e ) {
	var myKeyCode=0;
	var myShiftKey=false;
	var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

	// Internet Explorer 4+
	if ( document.all ) {
		myKeyCode=e.keyCode;
		myShiftKey=e.shiftKey;

	// Netscape 4
	} else if ( document.layers ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	// Netscape 6
	} else if ( document.getElementById ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	}

	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
		alert( myMsg );

	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
		alert( myMsg );

	}
}

//onKeyPress="checkCapsLock( event )

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name )
{	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain )
{
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Cookie_Check(Element_ID)
{
	// remember, these are the possible parameters for Set_Cookie:
	// name, value, expires, path, domain, secure
	Set_Cookie( 'test', 'none', '', '/', '', '' );
	// if Get_Cookie succeeds, cookies are enabled, since 
	//the cookie was successfully created.
	if ( Get_Cookie( 'test' ) )
	{
		document.getElementById(Element_ID).innerHTML = 'Enabled';
		cookie_set = true;
		// and these are the parameters for Delete_Cookie:
		// name, path, domain
		// make sure you use the same parameters in Set and Delete Cookie.
		Delete_Cookie('test', '/', '');
	}
	// if the Get_Cookie test fails, cookies 
	//are not enabled for this session.
	else
	{
		document.getElementById(Element_ID).innerHTML = 'Disabled';
		cookie_set = false;
	}
}

function queryString(parameter)
{ 

  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false;
  }
}



function init()
{
	//Check JS
        document.getElementById('jsNotification').innerHTML = "";
	document.getElementById("JsCheck").innerHTML = 'Enabled';

	// remember, these are the possible parameters for Set_Cookie:
	// name, value, expires, path, domain, secure
	Set_Cookie( 'test', 'none', '', '/', '', '' );
	// if Get_Cookie succeeds, cookies are enabled, since 
	//the cookie was successfully created.
	if ( Get_Cookie( 'test' ) )
	{
		document.getElementById("CookieCheck").innerHTML = 'Enabled';
		cookie_set = true;
		// and these are the parameters for Delete_Cookie:
		// name, path, domain
		// make sure you use the same parameters in Set and Delete Cookie.
		Delete_Cookie('test', '/', '');
	}
	// if the Get_Cookie test fails, cookies 
	//are not enabled for this session.
	else
	{
		document.getElementById("CookieCheck").innerHTML = 'Disabled';
		cookie_set = false;
	}
        
        //Check if login failed paremeter is in URL
        var logout_reason = queryString('logout_reason');
        if ( logout_reason != false)
        {
	    document.getElementById('legacy-div').className = "display";
	    document.getElementById('owa-div').className = "hidden";
	    
            switch(logout_reason)
            {
                case 'failed':
                    document.getElementById('legacy-div').innerHTML = "<span class='error'>Login attempt failed.</span><br />" + document.getElementById('legacy-div').innerHTML;
                    break;
                case 'logout':
                    document.getElementById('legacy-div').innerHTML = "<span class='success'>You have been logged out.</span><br />" + document.getElementById('legacy-div').innerHTML;
                    break;
                default:
                    //nothing
                    break;
                
            }
            
	    document.getElementById('imapuser').focus();            
            
        }
	switchSystem('legacy');	
}

window.onload = init;
