function hasReferererWithExtendedChars(r)
{
  r = r ? decodeURI(r) : '';
  var use_ga = false;
  for (var i=0; i < r.length; i ++)
  {
    if (r.charCodeAt(i) > 31 && r.charCodeAt(i) < 127)
    {
      use_ga = true;
      break;
    }
  }
  return use_ga;
}

function submitWebSiteSearch()
{
 if (trim(document.search.q.value) == 'Type Website Search Text')
 {
     document.search.q.value = "";
 }
 document.search.submit();
}
/* Open a new window */
function NewWindow(mypage, myname, w, h, pos, infocus)
{
  var win = null;
  if (pos == "random") {
      myleft = (screen.width) ? Math.floor(Math.random() * (screen.width-w)):100;
      mytop = (screen.height) ? Math.floor(Math.random() * ((screen.height-h)-75)):100;
  }
  if (pos == "center") {
      myleft = (screen.width) ? (screen.width-w) / 2:100;
      mytop = (screen.height) ? (screen.height-h) / 2:100;
  } else if ((pos !='center' && pos != "random") || pos == null) {
      myleft = 0;mytop = 20
  }
  settings = "width=" + w + ", height=" + h + ",top=" + mytop + ",left=" + myleft + ", scrollbars=yes, location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=yes";
  win = window.open(mypage, myname, settings);
  win.focus();
}
function NewWindowOptions(mypage, myname, w, h, pos, infocus, scroll, resize)
{
  var win = null;
  if (pos == "random") {
      myleft = (screen.width) ? Math.floor(Math.random() * (screen.width-w)):100;
      mytop = (screen.height) ? Math.floor(Math.random() * ((screen.height-h)-75)):100;
  }
  if (pos == "center") {
      myleft = (screen.width) ? (screen.width-w) / 2:100;
      mytop = (screen.height) ? (screen.height-h) / 2:100;
  } else if ((pos !='center' && pos != "random") || pos == null) {
      myleft = 0;mytop = 20
  }
  settings = "width=" + w + ", height=" + h + ",top=" + mytop + ",left=" + myleft + ", scrollbars= "+scroll+", location=no, directories=no, status=no, menubar=no, toolbar=no, resizable="+resize;
  win = window.open(mypage, myname, settings);
  win.focus();
}
function openNewWindow(theURL,winName,features) //v2.0
{
  window.open(theURL,winName,features);
}
function getPrintPDFURL()
{
  return 'http://206.106.137.34/MktServices/servlet/CreatePdf?f=';
}
function getPrintPDFLink(uri, date)
{
  var hostname = window.location.hostname;
  window.location.href = getPrintPDFURL()+uri+'&lm='+date+'&domain='+ hostname;
}
function trim(string)
{
  return(string.replace(/^\s+|\s+$/g,''));
}
function validName(name)
{
  ok = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM' ";
  for(i=0;i < name.length; i++)
  {
    if(ok.indexOf(name.charAt(i)) < 0)
    {
     return false;
    }
  }
  return true;
}
function validEmail(email)
{
  if(email.length < 7)
  {
   return false;
  }
  else
  {
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    for(i=0;i < email.length; i++)
    {
      if(ok.indexOf(email.charAt(i)) < 0)
      {
       return false;
      }
    }
  }
  return true;
}
function validURL(url)
{
  if(url.length < 9)
  {
    return false;
  }
  else
  {
    ok = "1234567890qwertyuiopasdfghjklzxcvbnm.-_QWERTYUIOPASDFGHJKLZXCVBNM/~";
    for(i=0;i < url.length; i++)
    {
      if(ok.indexOf(url.charAt(i)) < 0)
        return false;
    }
  }
  return true;
}
function stringContainsDigits(str, min)
{
  var ok = "1234567890";
  var c = 0;
  for(i=0;i < str.length; i++)
  {
    if(ok.indexOf(str.charAt(i)) != -1)
    {
      c ++;
    }
  }
  if (c > min)
  {
    return true;
  }
  return false;
}
function isRadioSelected(r)
{
   for(i=0; i<r.length; i ++)
   {
       if (r[i].checked)
       {
           return true;
       }
   }
   return false;
}
function eMailCheck(str) 
{
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  if (str.indexOf(at)==-1){
   return false;
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   return false;
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
   return false;
  }
  if (str.indexOf(at,(lat+1))!=-1){
   return false;
  }
  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
   return false;
  }
  if (str.indexOf(dot,(lat+2))==-1){
   return false;
  }
  if (str.indexOf(" ")!=-1){
   return false;
  }
  return true;				
}
function cookieIsEnabled(name, value, days)
{
  setCookie(name, value, days);
  if (document.cookie.indexOf(name) != -1)
  {
    setCookie(name, value, 0);
    return true;
  }
  return false;
}
function setCookie(name, value, days, domain)
{
  var v = name + "=" + value;
  if (days){
    var exp = new Date();
    exp.setTime(exp.getTime() + (3600000*24*days));
    v += ";expires="+exp.toGMTString();
  }
  v += ";path=/";
  if (domain){v += ";domain="+domain;}
  document.cookie = v;
}
function hasCookie(name, value)
{
  if (value == getCookie(name))
  {
   return true;
  }
  return false;
}
function deleteCookie(name)
{
  setCookie(name,"1",0);
}
function getCookie(name) 
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
     begin = dc.indexOf(prefix);
     if (begin != 0) return null;
  } else {
     begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
      end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end)); 
}
function addSiteLocationCookie(name, value, days)
{
  if (hasCookie(name, value))
  {
    alert("You have already set this site as your default IB site.");
  }
  else {
    if (cookieIsEnabled('t',1,1))
    {
      setCookie(name, '', -10, 'institutions.interactivebrokers.com');
      setCookie(name, '', -10, 'investors.interactivebrokers.com');
      setCookie(name, '', -10, 'individuals.interactivebrokers.com');
      setCookie(name, '', -10, 'www.interactivebrokers.com');
      setCookie(name, value, days, '.interactivebrokers.com');
      if (value == '' && days < 0) {
        alert("Default IB site cleared.");
        window.location.reload();
      }
      else if (document.cookie.indexOf(name) != -1)
      {
        alert("You have set this site as your default IB site.");
        window.location.reload();
      }
     }
     else {
        alert("Please enable cookies to set your default IB site.");
     }
   }
}
function getEventXYCoordinates(e)
{
  if (isie){e = event;}
  if (navigator.appName == "Microsoft Internet Explorer") {
      y = (e.clientY + document.body.scrollTop);
      x = e.clientX;
  }
  else {
      y = e.pageY;
      x = e.pageX;
  }
  return new Array(parseInt(x), parseInt(y));
}
function showExpandContract(div)
{
  var c = div.childNodes;
  if (c.item(1).firstChild.nodeValue == ' Expand')
  {
    c.item(1).firstChild.nodeValue = ' Contract';
    c.item(0).src = '/images/common/minus_symbol.gif';
    c.item(2).style.display = 'block';
  }
  else {
    c.item(1).firstChild.nodeValue = ' Expand';
    c.item(0).src = '/images/common/plus_symbol.gif';
    c.item(2).style.display = 'none';
  }
}
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}
var keyStr = "ABCDEFGHIJKLMNOP" +
"QRSTUVWXYZabcdef" +
"ghijklmnopqrstuv" +
"wxyz0123456789+/" +
"=";
function encode64(input) {
    input = escape(input);
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output = output +
        keyStr.charAt(enc1) +
        keyStr.charAt(enc2) +
        keyStr.charAt(enc3) +
        keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);
    return output;
}
function decode64(input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);
    return unescape(output);
}
//Changes <a> to formWelcome to landing page for google stats
function changeA2LP() {
    if (hasCookie('googlermkt', '1')) {
        return;
    }
    //Iterate through all <a> and change to landing page
    var a = document.getElementsByTagName("a");
    for (var i = 0; i < a.length; i++) {
        if (a[i].href.indexOf('/Universal/servlet/formWelcome') != -1) {
            a[i].href = '/mkt/reg_lp.php?href=' + encode64(a[i].href);
        }
    }
    a = document.getElementsByTagName("area");
    for (var i = 0; i < a.length; i++) {
        if (a[i].href.indexOf('/Universal/servlet/formWelcome') != -1) {
            a[i].href = '/mkt/reg_lp.php?href=' + encode64(a[i].href);
        }
    }
}
/* Until links to new Guide are changed */
function newGuideLinksSwap()
{
  var a = document.getElementsByTagName("a");
  for (var i = 0; i < a.length; i++) 
  {
    if (a[i].href.indexOf('/twsguide.htm#usersguidebook/') != -1)
    {
       a[i].href = a[i].href.replace(/\/twsguide.htm#usersguidebook\//, '/twsguide_CSH.htm#usersguidebook/');
    }
    else if (a[i].href.indexOf('/webtrader2/servlet/login?DEMOMODE=true') != -1 && a[i].href.indexOf('redirect=off') == -1)
    {
       a[i].href = a[i].href.replace('DEMOMODE=true','DEMOMODE=true&redirect=off');
    }
    else if (a[i].href.indexOf('interactivebrokers.co.uk/contract_info/') != -1)
    {
       a[i].href = 'http://www1.interactivebrokers.ch/contract_info/';
    }
  }
}
function fortifyInput(name)
{
  ok = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890/.%?&-+=_@:;#";
  out = "";
  for(i=0;i < name.length; i++)
  {
    var ok_index = ok.indexOf(name.charAt(i));
    if(ok_index >= 0)
    {
     out += ok.charAt(ok_index);
    }
  }
  return out;
}
/* Dropdown for Open account - replaces dropdown.js */
sfHover = function() {
	if (document.getElementById("navigation"))
        {
        var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" over";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");
		}
	}
        }
}
addOnloadEvent(sfHover);
addOnloadEvent(changeA2LP);
addOnloadEvent(newGuideLinksSwap);
