// mcw.js

var arg = new Object;
var players = new Array();
var players_by_type = new Object;
var services = new Array();
var services_by_type = new Object;

function Player (key, type, model, pid, url, bluetooth, services) {
  this.key = key;
  this.type = type;  // a.k.a. make
  this.model = model;
  this.name = type + " " + model;
  this.pid = pid;
  this.url = url;
  this.bluetooth = bluetooth;
  this.all_services = stringSplit(services, ",");  // a.k.a. phones
  this.services = new Object; //instantiated in Service class

  if (typeof(players_by_type[type]) == "undefined") {
    players_by_type[type] = new Array(this.key.toString());
  }
  else {
    players_by_type[type][players_by_type[type].length] = this.key;
  }
  if (typeof(players_by_type["All"]) == "undefined") {
    players_by_type["All"] = new Array(this.key.toString());
  }
  else {
    players_by_type["All"][players_by_type["All"].length] = this.key;
  }
}

function P (str) {
  var args = stringSplit(str,"|");
  var key = players.length++;
  players[key] = new Player (key, args[0], args[1], args[2], args[3], args[4], args[5]);
}

function Service (key, type, model, pid, url) {
  this.key = key;
  this.type = type;  // a.k.a. make
  this.model = model;
  this.name = type + " " + model;
  this.pid = pid;
  this.url = url;

  this.players = new Object;
  for (var x = 0; x < players.length; x++) {
    for (var y = 0; y < players[x].all_services.length; y++) {
      if (this.name == players[x].all_services[y]) {
        if (typeof(this.players["All"]) == "undefined") {
          this.players["All"] = new Array();
          this.players["All"][0] = players[x].key;
        }
        else {
          this.players["All"][this.players["All"].length] = players[x].key;
        }
        if (typeof(this.players[players[x].type]) == "undefined") {
          this.players[players[x].type] = new Array();
          this.players[players[x].type][0] = players[x].key;
        }
        else {
          this.players[players[x].type][this.players[players[x].type].length] = players[x].key;
        }
		if (typeof(players[x].services["All"]) == "undefined") {
          players[x].services["All"] = new Array();
          players[x].services["All"][0] = key;
        }
        else {
          players[x].services["All"][players[x].services["All"].length] = key;
        }
		if (typeof(players[x].services[this.type]) == "undefined") {
          players[x].services[type] = new Array();
          players[x].services[type][0] = key;
        }
        else {
          players[x].services[type][players[x].services[type].length] = key;
        }
        break;
      }
    }
  }
  if (typeof(services_by_type[type]) == "undefined") {
    services_by_type[type] = new Array(this.key.toString());
  }
  else {
    services_by_type[type][services_by_type[type].length] = this.key;
  }
  if (typeof(services_by_type["All"]) == "undefined") {
    services_by_type["All"] = new Array(this.key.toString());
  }
  else {
    services_by_type["All"][services_by_type["All"].length] = this.key;
  }
}

function S (str) {
  var args = stringSplit(str,"|");
  var key = services.length++;
  services[key] = new Service (key, args[0], args[1], args[2], args[3]);
}

// TAB1: STEP1: Show the list of players for a selected type
function showPlayersInType(ptype) {
  var fs = "";  // frame source
  var sp = "";  // selected player
  var lpk = players_by_type[ptype];  // list of player keys
  lpk.sort(sortByPlayerName);
  for (var x = 0; x < lpk.length; x++) {
    fs += "<a name=\"" + players[lpk[x]].name + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
    if (arg.o1 != "" && arg.o1 == lpk[x]) {
      fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
      sp = players[lpk[x]].name;
    }
    else {
      fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
    }
	fs += " onclick=\"parent.document.pform.o1.value='" + lpk[x] + "'; parent.document.pform.s2.value='All'; parent.document.pform.o2.value=''; parent.document.pform.submit();\">" + players[lpk[x]].name + " (" + players[lpk[x]].services["All"].length + ")</div>\n";
  }
  var fw = parent.frames.f1;
  fw.document.open();
  fw.document.write(addHTMLShell(sp,fs));
  fw.document.close();
}

// TAB1: STEP2: Show available services for a specific player
function showServicesForPlayer(pkey) {
  var fs = "";  // frame source
  var ss = "";  // selected service
  var lst = new Array();  // list of service types
  for (stype in players[pkey].services) {
    lst[lst.length] = stype;
  }
  lst.sort(sortAlpha);
  for (var i = 0; i < lst.length; i++) {
    if (lst[i] != "All") {
      fs += "<a name=\"" + lst[i] + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
      if (isDefined(arg.s2) && lst[i] == arg.s2) {
        fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
        ss = lst[i];
      }
      else {
        fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
      }
      fs += " onclick=\"parent.document.pform.s2.value='" + lst[i] + "'; parent.document.pform.submit();\">" + lst[i] + " (" +  players[arg.o1].services[lst[i]].length + ")</div>\n";
    }
  }
  var fw = parent.frames.f2;
  fw.document.open();
  fw.document.write(addHTMLShell(ss,fs));
  fw.document.close();
}

// TAB1: STEP3: Show details of selected service
function showServiceDetails(pkey) {
  var fs = "";  // frame source 
  var lsk = players[pkey].services[arg.s2];  
  lsk.sort(sortByServiceName);
  fs += "<style type=\"text/css\">DIV A {color:#000 !important; text-decoration:none !important;}\nDIV A:hover {color:#FFF !important; text-decoration:none !important;}</style>\n";
  for (x = 0; x < lsk.length; x++) {
    if (isDefined(services[lsk[x]].url) && services[lsk[x]].url != "n/a") {
      fs += "<a name=\"" + services[lsk[x]].name + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
      if (isDefined(arg.o2) && arg.o2 == lsk[x]) {
        fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:0 1px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
        ss = services[lsk[x]].name;
      }
      else {
        fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:0 1px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
      }
      fs += "><a href=\"" + services[lsk[x]].url + "\" target=\"_parent\" style=\"display:block; padding:2px 4px;\">" + services[lsk[x]].name + "</a></div>\n";
    }
    else {
      fs += "<a name=\"" + services[lsk[x]].name + "\"></a><div onmouseover=\"this.style.background='#FFF';this.style.color='#000';\"";
      if (isDefined(arg.o2) && arg.o2 == lsk[x]) {
        ss = services[lsk[x]].name;
      }
      fs += " onmouseout=\"this.style.background='#FFF';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:auto; font:10pt arial,helvetica; background-color:#FFF; color:#000;\" target=\"_parent\">" + services[lsk[x]].name + "</div>\n";
    }
  }
  var fw = parent.frames.f3;
  fw.document.open();
  fw.document.write(addHTMLShell("",fs));
  fw.document.close();
}

// TAB2: STEP1: Show the list of services for a selected type
function showServicesInType(stype) {
  var fs = "";  // frame source
  var ss = "";  // selected service
  var lsk = services_by_type[stype];  // list of service keys
  lsk.sort(sortByServiceName);
  for (var x = 0; x < lsk.length; x++) {
    fs += "<a name=\"" + services[lsk[x]].name + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
    if (arg.o1 != "" && arg.o1 == lsk[x]) {
      fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
      ss = services[lsk[x]].name;
    }
    else {
      fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
    }
	fs += " onclick=\"parent.document.sform.o1.value='" + lsk[x] + "'; parent.document.sform.s2.value='All'; parent.document.sform.o2.value=''; parent.document.sform.submit();\">" + services[lsk[x]].name + " (";
    if (isDefined(services[lsk[x]].players["All"])) {
      fs += services[lsk[x]].players["All"].length;
    }
    else {
      fs += "0";
    }
    fs += ")</div>\n";
  }
  var fw = parent.frames.f4;
  fw.document.open();
  fw.document.write(addHTMLShell(ss,fs));
  fw.document.close();
}

// TAB2: STEP2: Show compatible players for a selected service
function showPlayersForService(skey) {
  var fs = "";  // frame source
  var sp = "";  // selected player
  var lpt = new Array();
  for (ptype in services[skey].players) {
    lpt[lpt.length] = ptype;
  }
  lpt.sort(sortAlpha);
  for (var i = 0; i < lpt.length; i++) {
    if (lpt[i] != "All") {
      fs += "<a name=\"" + lpt[i] + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
      if (isDefined(arg.s2) && lpt[i] == arg.s2) {
        fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
        ss = lpt[i];
      }
      else {
        fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
      }
      fs += " onclick=\"parent.document.sform.s2.value='" + lpt[i] + "'; parent.document.sform.submit();\">" + lpt[i] + " (" +  services[arg.o1].players[lpt[i]].length + ")</div>\n";
    }
  }
  var fw = parent.frames.f5;
  fw.document.open();
  fw.document.write(addHTMLShell(sp,fs));
  fw.document.close();
}

// TAB2: STEP3: Show details of selected player
function showPlayerDetails(skey) {
  var fs = "";  // frame source
  var lpk = services[skey].players[arg.s2];  // list of player keys
  lpk.sort(sortByPlayerName);
  fs += "<style type=\"text/css\">DIV A {color:#000 !important; text-decoration:none !important;}\nDIV A:hover {color:#FFF !important; text-decoration:none !important;}</style>\n";
  for (x = 0; x < lpk.length; x++) {
    if (isDefined(players[lpk[x]].url) && players[lpk[x]].url != "n/a") {
      fs += "<a name=\"" + players[lpk[x]].name + "\"></a><div onmouseover=\"this.style.background='#AAA';this.style.color='#FFF';\"";
      if (isDefined(arg.o2) && arg.o2 == lpk[x]) {
        fs += " onmouseout=\"this.style.background='#090';this.style.color='#FFF';\" style=\"padding:0 1px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#090; color:#FFF;\"";
        ss = players[lpk[x]].name;
      }
      else {
        fs += " onmouseout=\"this.style.background='#DDD';this.style.color='#000';\" style=\"padding:0 1px; margin:0 0 2px 0; cursor:pointer; font:10pt arial,helvetica; background-color:#DDD; color:#000;\"";
      }
      fs += "><a href=\"" + players[lpk[x]].url + "\" target=\"_parent\" style=\"display:block; padding:2px 4px;\">" + players[lpk[x]].name + "</a></div>\n";
    }
    else {
      fs += "<a name=\"" + players[lpk[x]].name + "\"></a><div onmouseover=\"this.style.background='#FFF';this.style.color='#000';\"";
      if (isDefined(arg.o2) && arg.o2 == lpk[x]) {
        ss = players[lpk[x]].name;
      }
      fs += " onmouseout=\"this.style.background='#FFF';this.style.color='#000';\" style=\"padding:2px 5px; margin:0 0 2px 0; cursor:auto; font:10pt arial,helvetica; background-color:#FFF; color:#000;\" target=\"_parent\">" + players[lpk[x]].name + "</div>\n";
    }
  }  
  var fw = parent.frames.f6;
  fw.document.open();
  fw.document.write(addHTMLShell("",fs));
  fw.document.close();
}

function addHTMLShell(a,c) {
  var h = "";
  h += "<html>\n<head>\n<style type=\"text/css\">\n";
  h += "A:link {color:#009; text-decoration:none;}\n";
  h += "A:visited {color:#009; text-decoration:none;}\n";
  h += "A:hover {color:#009; text-decoration:underline !important;}\n";
  h += "B.a2 {font:bold 10pt arial,helvetica;}";
  h += "BODY {padding:0; margin:0; padding:0; font:10pt arial,helvetica;}\n";
  h += ".a2 {font:10pt arial,helvetica;}\n";
  h += ".v1 {font:8pt verdana,geneva;}\n";
  h += "</style>\n<script language=\"Javascript\">\n";
  h += "\n";
  h += "</script>\n</head>\n<body marginheight=\"0\" marginwidth=\"0\"";
  if (document.all && a != "") {
    h += " onload=\"document.location.hash='" + a + "'\"";
  }
  h += ">\n" + c + "\n</body>\n</html>\n";
  return h;
}

function buildMCW() {
  var cp = document.getElementById("choose_player");
  var cs = document.getElementById("choose_service");
  if (isDefined(arg.t) && arg.t == 2) {
    cp.style.display = "none";
    cp.style.visibility = "hidden";
    cs.style.display = "block";
	cs.style.visibility = "visible";
	document.pform.t.value = 2;
  }
  else {
    cp.style.display = "block";
    cp.style.visibility = "visible";
    cs.style.display = "none";
	cs.style.visibility = "hidden";
	document.pform.t.value = 1;
  }
  if (!isDefined(arg.t) || arg.t == "1") {
	// toggle visibility of headers and columns  
    var hs1 = document.getElementById("hdr_step1p");
    var hs2 = document.getElementById("hdr_step2p");
    var hs3 = document.getElementById("hdr_step3p");
	var pc1 = document.getElementById("pcol1");
	var pc2 = document.getElementById("pcol2");
	var pc3 = document.getElementById("pcol3"); 
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2) && arg.s2 != "All") {
      hs1.style.color = "#000";
      hs2.style.color = "#000";
      hs3.style.color = "#FFF";
      pc1.style.display = "block";
      pc1.style.visibility = "visible";
      pc2.style.display = "block";
      pc2.style.visibility = "visible";
      pc3.style.display = "block";
      pc3.style.visibility = "visible";
    }
    else if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2)) {
      hs1.style.color = "#000";
      hs2.style.color = "#FFF";
      hs3.style.color = "#000";
      pc1.style.display = "block";
      pc1.style.visibility = "visible";
      pc2.style.display = "block";
      pc2.style.visibility = "visible";
      pc3.style.display = "none";
      pc3.style.visibility = "hidden";
    }
    else {
      hs1.style.color = "#FFF";
      hs2.style.color = "#000";
      hs3.style.color = "#000";
      pc1.style.display = "block";
      pc1.style.visibility = "visible";
      pc2.style.display = "none";
      pc2.style.visibility = "hidden";
      pc3.style.display = "none";
      pc3.style.visibility = "hidden";
    }
    if (isDefined(arg.s1)) {
      showPlayersInType(arg.s1);
    }
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2)) {
      showServicesForPlayer(arg.o1);
    }
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2) && arg.s2 != "All") {
      showServiceDetails(arg.o1);
    }
  }
  else if (isDefined(arg.t) && arg.t == "2") {
    // toggle visibility of headers and columns  
    var hs1 = document.getElementById("hdr_step1s");
    var hs2 = document.getElementById("hdr_step2s");
    var hs3 = document.getElementById("hdr_step3s");
	var sc1 = document.getElementById("scol1");
	var sc2 = document.getElementById("scol2");
	var sc3 = document.getElementById("scol3");
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2) && arg.s2 != "All") {
      hs1.style.color = "#000";
      hs2.style.color = "#000";
      hs3.style.color = "#FFF";
      sc1.style.display = "block";
      sc1.style.visibility = "visible";
      sc2.style.display = "block";
      sc2.style.visibility = "visible";
      sc3.style.display = "block";
      sc3.style.visibility = "visible";
    }
    else if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2)) {
      hs1.style.color = "#000";
      hs2.style.color = "#FFF";
      hs3.style.color = "#000";
      sc1.style.display = "block";
      sc1.style.visibility = "visible";
      sc2.style.display = "block";
      sc2.style.visibility = "visible";
      sc3.style.display = "none";
      sc3.style.visibility = "hidden";
    }
    else {
      hs1.style.color = "#FFF";
      hs2.style.color = "#000";
      hs3.style.color = "#000";
      sc1.style.display = "block";
      sc1.style.visibility = "visible";
      sc2.style.display = "none";
      sc2.style.visibility = "hidden";
      sc3.style.display = "none";
      sc3.style.visibility = "hidden";
    }
    if (isDefined(arg.s1)) {
      showServicesInType(arg.s1);
    }
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2)) {
      showPlayersForService(arg.o1);
    }
    if (isDefined(arg.s1) && isDefined(arg.o1) && isDefined(arg.s2) && arg.s2 != "All") {
      showPlayerDetails(arg.o1); 
    }
  }
}

function getArgs(a) {
  a = "" + a;
  while(true) {
    var i = a.indexOf('+');
    if (i < 0) { break; }
    a = a.substring(0,i) + "%20" + a.substring(i + 1, a.length);
  }
  a = unescape(a);
  if (a.lastIndexOf("?") == 0) {
    var tmp_a = a.split("?");
	a = tmp_a[1];
  }
  else { return; }
  var pairs = a.split("&");
  for (var i = 0; i < pairs.length; i++) {
    var kv = pairs[i].split("=");
    arg[kv[0]] = kv[1];
  }
}

function initMCW() {
  (!document.location.search) ? getArgs(parent.document.location.search) : getArgs(document.location.search);
  anchorWiz("#mcw_top");
}

function isDefined(x) {
  if (!x || typeof(x) == "undefined" || x == "") { return false; }
  else { return true; }
}

function sortAlpha(a,b) {
  if (a.toString().toLowerCase() < b.toString().toLowerCase()) return -1;
  if (a.toString().toLowerCase() > b.toString().toLowerCase()) return 1;
  return 0;
}

function sortByPlayerName(a,b) {
  if (players[a].name.toString().toLowerCase() < players[b].name.toString().toLowerCase()) return -1;
  if (players[a].name.toString().toLowerCase() > players[b].name.toString().toLowerCase()) return 1;
  return 0;
}

function sortByServiceName(a,b) {
  if (services[a].name.toString().toLowerCase() < services[b].name.toString().toLowerCase()) return -1;
  if (services[a].name.toString().toLowerCase() > services[b].name.toString().toLowerCase()) return 1;
  return 0;
}

function stringSplit(string,delimiter) {
  if (string == null || string == "") {
    return null;
  }
  else if (string.split != null) {
    return string.split(delimiter);
  }
  else {
    var ar = new Array();
    var i = 0;
    var start = 0;
    while (start >= 0 && start < string.length) {
      var end = string.indexOf(delimiter, start);
      if (end >= 0 ) {
        ar[i++] = string.substring(start, end);
        start = end+1 ;
      }
	  else {
        ar[i++] = string.substring(start, string.length);
        start = -1;
      }
    }
    return ar;
  }
}

function anchorWiz(anchor_name) {
  if (location.href.indexOf(anchor_name) == -1) {
    location.href=location.href+anchor_name;
  }
}