
// instance variable
var token = false;

// create a popup in center of screen
function pop(loc, pw, ph){
  var ht = screen.height; // get users screen height
  var wd = screen.width; // get users screen width

  var x=(screen.width-pw)/2; // get center of screen minus popup width
  var y=(screen.height-ph)/2; // get center of screen minus popup height

  // set the options for the popup
  var options = "width=" + pw + ",height=" + ph +
    ",scrollbars=yes,titlebar=yes,status=yes,resizable=yes,menubar=no" +
    ",top=" + y + ",screenY=" + y + ",left=" + x + ",screenX=" + x;

  window.open(loc, "login", options);
  return false;
}

// create a popup with specified name
function popname(loc, winName, width, height) {
  var options = "scrollbars=yes,resizable=yes," +
    "width=" + width + ",height=" + height + 
    ",status=yes,location=no,toolbar=no";

  window.open(loc, winName, options);
  return false;
}

// read cookie
function readCookie(name) {
  var cookies = "" + document.cookie;
  var idx = cookies.indexOf(name);
  if(idx < 0 || name == "") return "";
  var idx2 = cookies.indexOf(';', idx);
  if(idx2 < 0) idx2 = cookies.length;
  return unescape(cookies.substring(idx, idx2));
}

// delete a cookie
function delCookie(name, domain, path) {
  if(domain == "" || domain == null) {
    domain = ".wsex.com";
  }
  if(path == "" || path == null) {
    path = "/";
  }
  var expireNow = new Date();
  document.cookie = name + "=" +
    "; expires=Saturday, 01-Jan-2000 00:00:00 GMT" +
    "; domain=" + domain + "; path=" + path + ";"
}

function login(requestParams) {
  alert("This action requires a valid login.");
}

function logout() {
  var calledDomain = document.location.hostname;
  var cookieDomain = calledDomain.replace("www", "");
  delCookie("wseKey", cookieDomain, "/");
  parent.toolbar.token=false;
  parent.nav.token=false;
}

function isLogged() {

  // for all webtv users we disregard
  if(navigator.userAgent.indexOf("WebTV") != -1) {
    return true;
  }

  // for all windows ce users we disregard
  if(navigator.userAgent.indexOf("Windows CE") != -1) {
    return true;
  }

  // for all palm os users we disregard
  if(navigator.userAgent.indexOf("PalmSource") != -1) {
    return true;
  }

  // ensure frames exist
  if(parent.frames.length > 4) {

    // is token set
    if((parent.toolbar != null && !parent.toolbar.token) &&
       (parent.nav != null && !parent.nav.token)) {

      // client is not logged in afaik
      login();
      return false;
    }
  }

  return true;
}

function setTarget(foo) {

  var qs = "";

  // is this a link
  if(foo.href != null) {
    if(foo.pathname != null) {
      if(foo.pathname.substr(0,1) != "/") {
        qs = "/";
      }
      qs += foo.pathname;
    }
    
  } else {

	// look for a form
    var f = document.forms[0];

    if(f != null) {

      qs = f.action + "?";

      // test reading form
      for(i=0; i < f.length; i++) {
        if(f.elements[i].name == "")
          continue;
        if(i!=0) qs += "&";

        qs += f.elements[i].name;
        qs += "=";
        qs += f.elements[i].value;
      }
    }
  }

  qs = escape(qs);

  // finally, add hostname to query string
  qs += "&host=" + document.location.hostname;


  // check if client is logged in
  if(!isLogged()) {
    return false;
  }

  if(window.clk==null){window.clk=1;}else{return false;};
}

// main deposit window
function open_credit_main(str) {
  if(!isLogged()) {
    return false;
  }
  window.open(str,'cashier_credit','scrollbars=yes,resizable=yes,width=620,height=620,status=yes,location=no,toolbar=no');
  return false;
}

// main withdrawal window
function open_debit_main(str) {
  if(!isLogged()) {
    return false;
  }
  window.open(str,'cashier_debit','scrollbars=no,resizable=yes,width=620,height=620,status=yes,location=no,toolbar=no');
  return false;
}

// main casino window
function open_casino_main(str) {
  if(!isLogged()) {
    return false;
  }
  window.open(str,'casino_main', 'resizable=1');
  return false;
}

