var browser = navigator.appName;
//<!-- This example is from JavaScript: The Definitive Guide, 3rd Edition.   -->
//<!-- That b ok and this example were Written by David Flanagan.            -->
//<!-- They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.        -->
//<!-- This example is provided WITHOUT WARRANTY either expressed or implied.-->
//<!-- You may study, use, modify, and distribute it for any purpose,        -->
//<!-- as long as this notice is retained.                                   -->


// 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 OpenWindow(urladdr, intwidth, intheight, scrollbars)
{

   var hWnd = window.open (urladdr,"NewWindow","width="+ intwidth +",height="+ intheight +",toolbar=no,resizable=no,scrollbars=" + scrollbars + ",screenX=50,ScreenY=50,left=100,top=100");
}


function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 = "";
    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;
    if (this.$secure) cookie += '; secure';

    // 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);

    // 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 i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][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;
}

// This function returns true if the cookie does not contain any property values.
function _Cookie_isEmpty()
{
    var cookieval = "";
    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]);
    }

}

// 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;
Cookie.prototype.isEmpty = _Cookie_isEmpty;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

<!-- super duper simple rollover scripts -->
function rollOff(which)    {
		document.images[which].src = "/en/images/" + which + "_off.gif";    }	
function rollOver(which)    {
		document.images[which].src = "/en/images/nav/b_" + which + "_over.gif";    } 
function rollOn(which)    {
		document.images[which].src = "/en/images/nav/b_" + which + "_on.gif";    }
		
//handle browser resizing problem for navigator 4
if(!window.orig_width) {
  window.onresize = reset_layers;
  window.orig_width = window.innerWidth;
  window.orig_height = window.innerHeight;
}

function reset_layers() {
    if (window.innerWidth != orig_width || window.innerHeight != orig_height) {
      location.reload();
    }
}

// used by the country drop down to change the windows location to the correct
// country page
function pageChanger(country, whichForm) {
if ((browser == 'Netscape') && (document.country == null)) {
    document.mainPage.document.country.elements[whichForm].options[0].selected=true;
  } else {
    document.country.elements[whichForm].options[0].selected=true;
  }
  location=(country);
}

function pop(url, wide, tall, xtra) {
	if (xtra == '' || xtra == null) xtra='scrollbars=yes,resizable=yes';
	window.open(url, 'palf', 'width='+wide+',height='+tall+','+xtra);
}
function getCookie(name) { 
    cname = name + "="; 
    allcookies = document.cookie; 
    if (allcookies.length > 0) { 
        begin = allcookies.indexOf(cname);
        if (begin != -1) { 
            begin += cname.length; 
            end = allcookies.indexOf(";", begin); 
            if (end == -1) end = allcookies.length; 
        return unescape(allcookies.substring(begin, end)); 
       } 
    return null; 
    } 
}

function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function writeCookieValue (cookie_name){
var output = getCookie(cookie_name);
document.write(output);
}




//used by the test_virtual page to ensure javascript is proxy-ing correctly
function showAlert() {
  window.alert("JavaScript Is Working!");
}

//tzm
function refreshOnExpired(cookieName)
{

    //alert("cookie is: " + this.document.cookie);
    //alert("looking for: " + cookieName);

    var homepageExpired = new Cookie(document,  cookieName);
    
    if (homepageExpired.load())
    {
        //alert("found it!");     
        homepageExpired.$path="/";   
        homepageExpired.remove();    
        document.location.reload();
        return;
    }
    else
    {
         //alert("didn't find it");        
         ;
    }

    var authTokenCookie = new Cookie(document, "PS_TOKEN");
    if (!authTokenCookie.load())
    {
         document.location.reload();
         return;
    }

}


function setRefreshHomepage(cookieName)
{
//alert("In refresh homepage");
    var homepageExpired = new Cookie(document, cookieName, 3, "/");
    homepageExpired.value="true";
    homepageExpired.store();
}
function removeSubstring(str, strRemove)
{
    if (str == null)
        return "";
            
    var     nStart = -1,
            nEnd   = -1;
    var     strReturn = str;
    var     bFound = false;
    
    nStart = str.indexOf(strRemove);
    if (nStart != -1)
    {
    do
    {
            strReturn = str.substring(0, nStart);
            
            nEnd = strRemove.length + nStart;
            if (nEnd >= str.length)
            {
                // strReturn already has everything we need, 'cause
                // we're chopping off the last segment.
                bFound = true;
                break;
            }
            if ((str.charAt(nEnd) != '|' && str.charAt(nEnd) != '%'))
            {
                // we matched a list member that's an initial substring of 
                // what we're looking for - ignore it!!
                continue;
            }

            // found a match in the middle of the list...
            bFound = true;
            strReturn += str.substring(nEnd+1);
            break;
            
    }while ((nStart = str.indexOf(nStart+nEnd+1, strRemove)) != -1)
    }
    if (!bFound)
        strReturn = str;
    return strReturn;
}        

function removeFromExpiredList(cookie, qs, domain)
{
    var list = cookie.list;
    if (list == null)
        return;
    else
        list = list.toLowerCase();
        
    list = removeSubstring(list, qs)
    
    cookie.list = list;
    //alert("new cookie list: " + cookie.list);
    
    
    // get rid of the old cookie...
    cookie.$path="/";
    cookie.$domain=domain;
    cookie.remove();
    
    // restore the new cookie...
    cookie.store();
}



function isInExpiredList(cookie, qs)
{
    var bReturn = false;
    var list = cookie.list;
    if (list != null)
    {
        list = list.toLowerCase();        
        var nStart = list.indexOf(qs);
        
        if (nStart != -1)
        {
            var nEnd = nStart + qs.length;
            if (list.length == nEnd || list.charAt(nEnd) == '|')
            {
                bReturn = true;
            }
        }
        if (list.indexOf("refresh_all_tabs") != -1)
            bReturn = true;
    }
    return bReturn;
}

function addToExpiredList(cookie, qs, domain)
{
    var list = cookie.list;
    if (list != null)
        list = list.toLowerCase();

    if (!isInExpiredList(cookie, qs))
    {
        if (list == null)
            list = qs.replace(",","");
        else
            list = list + "|" + qs.replace(",","");
            
        cookie.list = list.replace(",","");

        // get rid of the old cookie...
        cookie.$path="/";
        cookie.$domain=domain;
        cookie.remove();

        // restore cookie...
        cookie.store();
    }
}


function removeFromCookie(cookie, qs, domain)
{
    removeFromExpiredList(cookie, qs, domain)
    var list = cookie.list;
    if (list == null || list == "")
    {
        cookie.$domain=domain;
        cookie.$path="/";   
        cookie.remove();
    }
}

function refreshOnExpired(cookieName, qs, domain)
{

// delay refresh for IE5.0 when using https to eliminate security warning
if (navigator.appVersion.indexOf("MSIE 5.0")>0) {
     var hRefPrefix = document.location.protocol.toLowerCase();
     if (hRefPrefix == "https:") {
          string="refreshOnExpired2('"+cookieName+"','"+qs+"','"+domain+"');";
          setTimeout(string,1);
          }
     else
          refreshOnExpired2(cookieName, qs, domain);
     }
else
     refreshOnExpired2(cookieName, qs, domain);
}

function refreshOnExpired2(cookieName, qs, domain)
{
    //alert("cookie is: " + this.document.cookie);
    //alert("looking for: " + cookieName);

    cookieName = cookieName.toLowerCase();
    qs = qs.toLowerCase();
    
    var pageExpired = new Cookie(document,  cookieName);

    if (pageExpired.load())
    {
        //alert("found cookie");
        if (isInExpiredList(pageExpired, qs))
        {
            //alert("found cookie, reloading page");
            removeFromCookie(pageExpired, qs, domain);            
            document.location.reload();
            return;
        }
        
    }
    //alert("didn't find it");        


    var authTokenCookie = new Cookie(document, "PS_TOKEN");
    if (!authTokenCookie.load())
    {
         document.location.reload();
         return;
    }

}


function setRefreshPage(cookieName, qs, domain)
{

   cookieName = cookieName.toLowerCase();

   var pageExpired = new Cookie(document, cookieName);
    if (!pageExpired.load())
    {
        pageExpired = new Cookie(document, cookieName,120,"/",domain);
    }

    qs = qs.toLowerCase();
    
    if (qs == null)
        qs ="?tab=DEFAULT";
    addToExpiredList(pageExpired, qs, domain);
}
