////////////////////////////////////////////////////////////////////////////////////
// Originally from CGI.js
////////////////////////////////////////////////////////////////////////////////////

function getArgs() {
  var args  = new Object();  
  var query = location.search.substring(1);
  var pairs = query.split("&");               // assume params separated by &s not 's
  
  for (var i=0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
      if (pos == -1) continue;
      var argname = pairs[i].substring( 0, pos);
      var value   = pairs[i].substring(pos+1);
      args[argname] = unescape(value);
  }
  
  return args;   
}
 
function getTargetArgs() {
    var realArgs = getArgs();
    var target   = smDecode( realArgs.TARGET );
  
    var args          = new Object();
    var locationParts = target.split("?");
    if ( (locationParts[1] != null) && (locationParts[1] != '') ) {
        var query         = locationParts[1];
        var pairs         = query.split("&");               // assume params separated by &s not 's
      
        for (var i=0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
            if (pos == -1) continue;
            var argname = pairs[i].substring( 0, pos);
            var value   = pairs[i].substring(pos+1);
            args[argname] = unescape(value);
            //		alert('in getTargetArgs(): argname = ' + argname + ', value = ' + value);
        }
    }

    return args;   
} 

////////////////////////////////////////////////////////////////////////////////////
// Originally from chartranslate.js
////////////////////////////////////////////////////////////////////////////////////
function stripAccentChar( aChar ){
    var specialChars = new Array(
                                 String.fromCharCode(192),  // ? 
                                 String.fromCharCode(193),  // ? 
                                 String.fromCharCode(194),  // ? 
                                 String.fromCharCode(195),  // ? 
                                 String.fromCharCode(196),  // ? 
                                 String.fromCharCode(197),  // ? 
                                 String.fromCharCode(199),  // ? 
                                 String.fromCharCode(200),  // ? 
                                 String.fromCharCode(201),  // ? 
                                 String.fromCharCode(202),  // ? 
                                 String.fromCharCode(203),  // ? 
                                 String.fromCharCode(204),  // ? 
                                 String.fromCharCode(205),  // ? 
                                 String.fromCharCode(206),  // ? 
                                 String.fromCharCode(207),  // ? 
                                 String.fromCharCode(209),  // ? 
                                 String.fromCharCode(210),  // ? 
                                 String.fromCharCode(211),  // ? 
                                 String.fromCharCode(212),  // ? 
                                 String.fromCharCode(213),  // ? 
                                 String.fromCharCode(214),  // ? 
                                 String.fromCharCode(217),  // ? 
                                 String.fromCharCode(218),  // ? 
                                 String.fromCharCode(219),  // ? 
                                 String.fromCharCode(220),  // ? 
                                 String.fromCharCode(221),  // ? 
                                             
                                 String.fromCharCode(224),  // ? 
                                 String.fromCharCode(225),  // ? 
                                 String.fromCharCode(226),  // ? 
                                 String.fromCharCode(227),  // ? 
                                 String.fromCharCode(228),  // ? 
                                 String.fromCharCode(229),  // ? 
                                 String.fromCharCode(231),  // ? 
                                 String.fromCharCode(232),  // ? 
                                 String.fromCharCode(233),  // ? 
                                 String.fromCharCode(234),  // ? 
                                 String.fromCharCode(235),  // ? 
                                 String.fromCharCode(236),  // ? 
                                 String.fromCharCode(237),  // ? 
                                 String.fromCharCode(238),  // ? 
                                 String.fromCharCode(239),  // ? 
                                 String.fromCharCode(241),  // ? 
                                 String.fromCharCode(242),  // ? 
                                 String.fromCharCode(243),  // ? 
                                 String.fromCharCode(244),  // ? 
                                 String.fromCharCode(245),  // ? 
                                 String.fromCharCode(246),  // ? 
                                 String.fromCharCode(249),  // ? 
                                 String.fromCharCode(250),  // ? 
                                 String.fromCharCode(251),  // ? 
                                 String.fromCharCode(252),  // ? 
                                 String.fromCharCode(252),  // ? 
                                 String.fromCharCode(255)   // ? 
                                 );
    var normalChars = new Array(
                                'A',
                                'A',
                                'A',
                                'A',
                                'A',
                                'A',
                                'C',
                                'E',
                                'E',
                                'E',
                                'E',
                                'I',
                                'I',
                                'I',
                                'I',
                                'N',
                                'O',
                                'O',
                                'O',
                                'O',
                                'O',
                                'U',
                                'U',
                                'U',
                                'U',
                                'Y',
                
                                'a',
                                'a',
                                'a',
                                'a',
                                'a',
                                'a',
                                'c',
                                'e',
                                'e',
                                'e',
                                'e',
                                'i',
                                'i',
                                'i',
                                'i',
                                'n',
                                'o',
                                'o',
                                'o',
                                'o',
                                'o',
                                'u',
                                'u',
                                'u',
                                'u',
                                'y',
                                'y'  
                                );
   
    var normalizer = new Object;
   
    for (i=0; i<specialChars.length; i++) {
        sc = specialChars[i];
        nc = normalChars[i];
        normalizer[sc] = nc;   
    }
    
    var newChar = normalizer[aChar];
    if ( (newChar == null) || (newChar == '') )
        return aChar;
    else
        return newChar;
}


function stripAccentString( aString ){
    if (aString == null)
        return null;
    
    var charArray = aString.split('');
    var newString = '';
    var aChar     = '';
    var newChar   = '';
    for (var i=0; i< charArray.length; i++) {
        aChar        = charArray[i];
        newChar      = stripAccentChar( aChar );
        charArray[i] = newChar;
    }
  
    newString = charArray.join('');
    return newString;
}


function correctGID( aString ){
    var newString = stripAccentString( aString );
    return newString;
}

//
//    Checks to see if a cookie with a given name is set and has a non-empty value
//    (Assumes that the cookie is for te current document.)
// 
function getCookieValue( name ){
    var aCookie = new Cookie( document, name );
    
    if ( aCookie.load() && ( aCookie.$value != "" ) )
        return aCookie.$value;
    else
        return null;        
}

//
// Set a cookie to store the destination for the user after logging in.
//
function mapQueryToCookie ( argName, cookieName ){
    var args = getArgs();                                     // Store query string parameters into an object.
    if ( ( args[ argName ] != null) && (args[argName] != '') ) {
        setCookie(cookieName, args[argName]);
    }
}
   
//
// Set a cookie to store the destination for the user after logging in.
// Similar to mapQueryToCookie, but looks specifically in Query variable named TARGET and
// decodes it's value (from Siteminder's encoding) to pick out any variables
//
//
// Set the value of a cookie
//
function setCookie( cookieName, cookieValue ){
    var aCookie = new Cookie( document, cookieName );
    aCookie.$isSingleValue = true;
    aCookie.$domain = '.' + _HLDN();
    aCookie.$value  = smDecode(cookieValue);    
    aCookie.$value  = cookieValue;    
    aCookie.$path   = '/';    



    aCookie.store();        
}

//
// get turrent "High level" domain name. "i.e.: bell.ca
//
function _HLDN( cookieName, cookieValue ){ 

    var myLocation = location.host;        
    var machines   = myLocation.split('.'); 
    var ext        = machines[machines.length-1];
    var host       = machines[machines.length-2];
     
    // Only take the final part of the domain up to any port that might have been defined.  
    var index     = ext.indexOf(':'); 
    if (index > -1)                   
        ext  = ext.substr(0,index); 
  
    var hldn       = host + '.' + ext; 
               
    return hldn; 
    
} 


//////////////////////////////////////////////////////////////////////////////////////
//                                   Cookie Class                                   //
// Based on the Cookie class in  "JavaScript: The Definitive Reference" by O'Reilly //
//////////////////////////////////////////////////////////////////////////////////////


// The constructor function: creates a cookie object for the specified
// document, with a specified name and optional attributes.
// Arguments:
//   document: The Document object that the cookie is stored for. Required.
//   name:     A string that specifies a name for the cookie. Required.
//   hours:    An optional number that specifies the number of hours from now
//             that the cookie should expire.
//   path:     An optional string that specifies the cookie path attribute.
//   domain:   An optional string that specifies the cookie domain attribute.
//   secure:   An optional Boolean value that, if true, requests a secure cookie.
//
function Cookie(document, name, hours, path, domain, secure)
{
    // All the predefined properties of this object begin with '$'
    // to distinguish them from other properties which are the values to
    // be stored in the cookie.
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}


//
// This function is the store() method of the Cookie object.
//
function _Cookie_store()
{
    // First, loop through the properties of the Cookie object and
    // put together the value of the cookie. Since cookies use the
    // equals sign and semicolons as separators, we'll use colons
    // and ampersands for the individual state variables we store 
    // within a single cookie value. Note that we escape the value
    // of each state variable, in case it contains punctuation or other
    // illegal characters.
    var cookieval = "";    
    
    if ( this.$isSingleValue ) {
        cookieval = this.$value;     	
    }
    else {
        for(var prop in this) {
            // Ignore properties with names that begin with '$' and also methods.
            if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
                continue;
            if (cookieval != "") cookieval += '&';
            cookieval += prop + ':' + escape(this[prop]);
        }
    }
    
    // Now that we have the value of the cookie, put together the 
    // complete cookie string, which includes the name and the various
    // attributes specified when the Cookie object was created.
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();

    if (this.$path) cookie += '; path=' + this.$path;

    if (this.$domain) cookie += '; domain=' + this.$domain;

    //    alert('domain: ' + this.$domain);
    if (this.$secure) cookie += '; secure';

    //    alert('cookie to be written: ' + cookie);
    // Now store the cookie by setting the magic Document.cookie property.
    this.$document.cookie = cookie;
}

//
// This function is the load() method of the Cookie object.
//
function _Cookie_load()
{
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property.
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    this.$value = cookieval;   // Set the complete string to a variable that can be used later.


    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable 
    // names and values. The name/value pairs are separated from each
    // other by ampersands, and the individual names and values are
    // separated from each other by colons. We use the split method
    // to parse everything.
    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    for(var i=0; i < a.length; i++)  // Break each pair into an array.
        a[i] = a[i].split(':');

    // Now that we've parsed the cookie value, set all the names and values
    // of the state variables in this Cookie object. Note that we unescape()
    // the property value, because we called escape() when we stored it.
    for(var j = 0; j < a.length; j++) {
        this[a[j][0]] = unescape(a[j][1]);
    }

    // We're done, so return the success code.
    return true;
}

//
// This function is the remove() method of the Cookie object.
//
function _Cookie_remove()
{
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

// Create a dummy Cookie object, so we can use the prototype object to make
// the functions above into methods.
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;



////////////////////////////////////////////////////////////////////////////////////
// Originally from loginbox.js
//////////////////////////////////////////////////////////////////////////////////////////////

//
// Set a Cookie to store the Target (aka destination after login) for the user.
//
function setTarget(){
    mapQueryToCookie( "TARGET", "GES_TARGET");     // Take the target from the Query Sting variable "Target".           
}



//
// Set a Cookie to store the number of attepts the user has made.
//
function setCounter(){
    var counter = getCookieValue('GES_TRYNO');
    counter = parseInt(counter);
    if (isNaN(counter))
        counter = 0;
    counter++;
    var sCounter = "" + counter + "";
    setCookie('GES_TRYNO', sCounter);   
}

//
//  Decode a string from SiteMinder's encoding process.
//
function smDecode(url)
{
    if ( (url != null) && (url.indexOf("$SM$") == 0) ){
        n = "";
     
        for (i=4; i < url.length; i++) {
            a = url.charAt(i);
          

            if (a == '$') {
                i++; a = url.charAt(i);
            }
            else {
                if (a == '%') {
                    b = url.substr (i,3);
                    a = unescape (b);
                    i += 2;
                }
            }
            n = n + a;
        }
        url = n;
    }

    return url;
}   


////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

// Original Author: Phil Beauchamp (Philippe.Beauchamp@emergis.com)  
// Description:
//
//     - Provide a mechanism for redirection to avoid problems with Netscape >= 4.5, which stops
//       a window from redirecting content in a frame when the window's content and frame content are in
//       different domains.
//
Redirector = function(targetLocation)
{
    this.target = targetLocation;
}
     
Redirector.prototype.setTarget = function(targetLocation)
{
    this.target = targetLocation;
}
       
Redirector.prototype.go = function(targetLocation)
{
    self.location =  targetLocation;
}
       
var REDIRECTOR = new Redirector();


