// includes/handson_ghbsp.js

var songList = new Array(
  'Lenny Kravitz - "Are You Gonna Go My Way"',
  'Creedence Clearwater Revival - "Up Around the Bend"',
  'Smashing Pumpkins - "Today"',
  'Blondie - "One Way or Another"',
  'Silversun Pickups - "Lazy Eye"',
  'Lynyrd Skynyrd - "Sweet Home Alabama (live)"',
  'Sublime - "Santeria"',
  'No Doubt - "Spiderwebs"',
  'Rise Against - "Re-Education Through Labor"',
  'Oasis - "Some Might Say"',
  'Bob Seger & the Silver Bullet Band - "Hollywood Nights"',
  'Interpol - "Obstacle 1"',
  'Ted Nugent - "Stranglehold"',
  'Black Label Society - "Stillborn"',
  'Motorhead - "Overkill"'
);

var avatarListMale = new Array(
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9e68a00c9/male8b.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9abe200c0/male1a.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9af2e00c1/male2c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9b38c00c2/male3.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9ba1c00c3/male3c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9c24200c4/male4.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9caae00c5/male4c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9d04500c6/male5a.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9dfaa00c7/male6b.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9e23d00c8/male7b.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf966c600b3/biker.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf96c8a00b4/cowboy1.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9790e00b6/elvisish.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9eb6300ca/Ninja1.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9ee4e00cb/rapper.png"
);

var avatarListFemale = new Array(
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf97f1800b7/female1c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf984c300b8/female2b.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf989fd00b9/female3e.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf98fd500ba/female4c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9940000bb/female5a.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf997fe00bc/female6c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf99cba00bd/female7c.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9a10500be/female8a.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf971d200b5/cowgirl.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9a6b800bf/goth.png",
  "http://mediamgrsrv3.devlb.handson.com:25660/mediaserver/1.1/m/wpt2/8a4883be1c56f9df011c6cf9f21b00cc/Snowboarder.png"
);

function loadUm(set) {

  if (!HOWF.currentAvatarStart || !HOWF.currentAvatarSet || HOWF.currentAvatarSet != set) {
    HOWF.currentAvatarStart = 0;
    HOWF.currentAvatarSet = set;
  }

  // restart at front of list
  if (HOWF.currentAvatarStart >= set.length) {
    HOWF.currentAvatarStart = 0;
  }

  // restart at back of list
  if (HOWF.currentAvatarStart < 0) {
    HOWF.currentAvatarStart = (set.length-1);
  }

  for (var i = HOWF.currentAvatarStart, j = 0; j < 5; i++, j++) {

    if (i >= set.length) {
      i = 0;
    }

    document.getElementById('preview' + j).src = set[i];
  }

}

function decrementAvatarSet() {

  HOWF.currentAvatarStart--;
  loadUm(HOWF.currentAvatarSet);

}

function incrementAvatarSet() {

  HOWF.currentAvatarStart++;
  loadUm(HOWF.currentAvatarSet);

}

function loadSongs(set) {

  if (!HOWF.currentSongStart || !HOWF.currentSongSet || HOWF.currentSongSet != set) {
    HOWF.currentSongStart = 0;
    HOWF.currentSongSet = set;
  }

  // restart at front of list
  if (HOWF.currentSongStart >= set.length) {
    HOWF.currentSongStart = 0;
  }

  // restart at back of list
  if (HOWF.currentSongStart < 0) {
    HOWF.currentSongStart = (set.length-1);
  }

  for (var i = HOWF.currentSongStart, j = 0; j < 2; i++, j++) {

    if (i >= set.length) {
      i = 0;
    }

    GH4.Page.stripChildren('preview' + j);

    document.getElementById('preview' + j).appendChild(document.createTextNode(set[i]));

    //document.getElementById('preview' + j).src = set[i];

  }

}

function decrementSongList() {

  HOWF.currentSongStart--;
  loadSongs(HOWF.currentSongSet);

}

function incrementSongList() {

  HOWF.currentSongStart++;
  loadSongs(HOWF.currentSongSet);

}



// This is a silly fix for IE's inability to use document.getElementsByName() the way
// Firefox uses it. Apparently, IE is actually following the HTML 4.01 specs by only
// allowing getElementsByName() to be used on the following elements:
// A, APPLET, BUTTON, FORM, FRAME, IFRAME, IMG, INPUT, OBJECT, MAP, META, PARAM, TEXTAREA, SELECT
function getElementsByName_IE_fix(tag, name) {
  var elem = document.getElementsByTagName(tag);
  var arr = new Array();
  for (i=0, iarr=0; i<elem.length; i++) {
    att = elem[i].getAttribute("name");
    if (att == name) {
      arr[iarr] = elem[i];
      iarr++;
    }
  }
  return arr;
}

// increments or decrements the global variable online/handset_guitar_index
// param type: either "online" or "handset"
// param x: either 1 or -1
function alter_guitar_index(type, x) {
  // increment guitar_index
  if (x == 1) {
    // reset it to zero if it's already at the end of the array
    // otherwise, increment
    eval("if ("+type+"_guitar_index == "+type+"_guitars.length - 1) "+type+"_guitar_index = 0; else "+type+"_guitar_index++;");
  } else if (x == -1) { // decrement guitar_index
    // reset it to largest index if it's already at zero
    // otherwise, decrement
    eval("if ("+type+"_guitar_index == 0) "+type+"_guitar_index = "+type+"_guitars.length - 1; else "+type+"_guitar_index--;");
  }
}

// param type: either "online" or "handset"
function redraw_guitar_panel(type) {
  var guitar_divs = new Array(
    document.getElementById(type+"_guitar_1"),
    document.getElementById(type+"_guitar_2"),
    document.getElementById(type+"_guitar_3"),
    document.getElementById(type+"_guitar_4")
  );

  // empty each guitar div
  for (var i=0; i<guitar_divs.length; i++) {
    guitar_divs[i].innerHTML = "";
    guitar_divs[i].onclick = "";
    guitar_divs[i].style.cursor = "default";
  }

  // draw four guitars, starting with guitar_index
  eval("var guitars = "+type+"_guitars");
  eval("var guitar_index = "+type+"_guitar_index");

  for (i=0; i<guitar_panel_size; i++) {
    if (i == guitars.length) {
      break;
    }
    guitar_divs[i].style.cursor = "pointer";
    guitar_divs[i].innerHTML = '<img class="centered" src="'+guitars[guitar_index].thumbnail+'" width="173" height="70"/>';
    guitar_divs[i].onclick = center_stage_guitar_func(guitars[guitar_index]);

    if (guitar_index == guitars.length) {
      guitar_index = 0;
    } else {
      guitar_index++;
    }
  }

  // update the title of online_guitar panel
  document.getElementById("online_guitar_panel_title").innerHTML = "";
  document.getElementById("online_guitar_panel_title").innerHTML = "Web Guitars ("+online_guitars.length+")";

  // update the title of handset_guitar panel
  document.getElementById("handset_guitar_panel_title").innerHTML = "";
  document.getElementById("handset_guitar_panel_title").innerHTML = "Handset Guitars ("+handset_guitars.length+")";
}

// this makes passing a JS object to center_stage_guitar() possible
function center_stage_guitar_func(guitar) {
  return function() {
    center_stage_guitar(guitar);
  }
}

// param guitar: object {id, title, img}
function center_stage_guitar(guitar) {
  // set the stage title
  document.getElementById("transfer_guitar_stage_title").innerHTML = "";
  document.getElementById("transfer_guitar_stage_title").innerHTML = guitar.title;

  // set the stage image
  document.getElementById("transfer_guitar_stage_img").innerHTML = "";
  document.getElementById("transfer_guitar_stage_img").innerHTML = '<img class="centered" src="'+guitar.img+'" width="300" height="84"/><br/><br/>';
  document.getElementById("transfer_guitar_stage_img").innerHTML += '<b>Price:</b> '+guitar.price;

  // tweak the transfer button
  document.getElementById("transfer_guitar_stage_button").onclick = transfer_guitar_func(guitar.id);
}

// this makes calling transfer_guitar() possible
function transfer_guitar_func(guitar_id) {
  return function() {
    transfer_guitar(guitar_id);
  }
}

function transfer_guitar(guitar_id) {
  $.get("account.php?op=transfer_guitar&id="+guitar_id+"&handset_count="+handset_guitars.length, function(dirty_output) {
    var output = dirty_output.trim();

    if (output == "success") {
      window.location.reload();
    } else if (output == "error") {
      alert("Unknown error");
    } else {
      alert(output);
    }
  });
}

function default_contest_search_option() {
  if (contest_search_option == "") {
    checkbox_activate("Search_Group", "ALL", false);
  }
}

function checkbox_activate(group, checkbox_id, large) {
  // grab all the checkboxes
  var checkboxes = document.getElementsByName(group);

  // turn them all off except for the specified one
  for (var i=0; i<checkboxes.length; i++) {
    if (checkboxes[i].id == checkbox_id) {
      checkboxes[i].src = (large) ? "graphics/checkbox_checked_28x29.png" : "graphics/checkbox_small_checked_16x15.png";

      var hidden_param = document.getElementById(group);
      if (hidden_param != null) {
        hidden_param.value = checkbox_id;
      }

      if (group == "Search_Group") {
        contest_search_option = checkbox_id;
      }
    } else {
      checkboxes[i].src = (large) ? "graphics/checkbox_unchecked_28x29.png" : "graphics/checkbox_small_unchecked_16x15.png";
    }
  }
}

function checkbox_deactivate(group, large) {
  // grab all the checkboxes
  var checkboxes = document.getElementsByName(group);

  // turn them all off
  for (var i=0; i<checkboxes.length; i++) {
    checkboxes[i].src = (large) ? "graphics/checkbox_unchecked_28x29.png" : "graphics/checkbox_small_unchecked_16x15.png";
  }
}

function show_contest_div(group, div_id) {
  // grab all the divs with name==group (IE hack)
  var divs = getElementsByName_IE_fix("div", group);

  // hide all these divs
  for (i=0; i<divs.length; i++) {
    divs[i].style.display = "none";
  }

  // show the specified div
  document.getElementById(div_id).style.display = "block";
}

function hide_contest_divs() {
  // grab all the dynamic contest divs
  var divs = getElementsByName_IE_fix("div", "create_contest_form_div");

  // hide them all
  for (var i=0; i<divs.length; i++) {
    divs[i].style.display = "none";
  }
}

// displays a specific page of search results
function goto_dynamic_page(page_number) {
  var i;

  curr_dynamic_page_num = page_number;

  // hide all the pages
  for (i=1; i<=num_dynamic_pages; i++) {
    var page_div = document.getElementById("dynamic_page_"+i);
    page_div.style.display = "none";
  }

  // show specified page
  document.getElementById("dynamic_page_"+page_number).style.display = "block";

  // update color of dynamic page links

  for (i=1; i<=num_dynamic_pages; i++) {
    var a_tag = document.getElementById("dynamic_page_link_"+i);
    a_tag.style.color = (i==page_number) ? "red" : "black";

/*** IE no likie
    var class_name = (i==page_number) ? "selected" : "";
    a_tag.setAttribute("class", class_name);
    a_tag.className = class_name;
*/
  }
}

function decrement_dynamic_page() {
  if (curr_dynamic_page_num > 1) {
    goto_dynamic_page(curr_dynamic_page_num - 1);
  }
}

function increment_dynamic_page() {
  if (curr_dynamic_page_num < num_dynamic_pages) {
    goto_dynamic_page(curr_dynamic_page_num + 1);
  }
}

// user has submitted this stage of the contest creation process
function create_contest_step(step_num) {
//  if (step_num > 1) {
//    for (var i=1; i<step_num; i++) {
//      // if any previous contest steps are incomplete, alert the user
//      if (!contest_steps[i]) {
//        alert("Contest step #"+i+" must be completed first.");
//        return;
//      }
//    }
//  }

/*
  // collapse all container divs
  var container_divs = document.getElementsByName("create_contest_nav_container");
  for (var i=0; i<container_divs.length; i++) {
    container_divs[i].className = "step_collapsed";
  }

  // hide all content divs
  var content_divs = document.getElementsByName("create_contest_nav_content");
  for (i=0; i<content_divs.length; i++) {
    content_divs[i].style.display = "none";
  }
*/

  // deactivate the checkboxes for this step
  if (step_num < 5) {
    checkbox_deactivate("step"+step_num+"_checkbox", true);
  }

  // expand specified container div
  var contest_nav_div = document.getElementById("create_contest_nav"+step_num);
  if (contest_nav_div) {
    if (contest_nav_div.getAttribute("class") == "step_collapsed" || contest_nav_div.className == "step_collapsed") {
      contest_nav_div.className = "step_expanded";
      contest_nav_div.setAttribute("class", "step_expanded");

      // show specified content div
      document.getElementById("nav_content"+step_num).style.display = "block";
    }
  }

  // hide all message divs on the upper right
  var message_divs = getElementsByName_IE_fix("div", "create_contest_msg");
  for (i=0; i<message_divs.length; i++) {
    message_divs[i].style.display = "none";
  }

  // hide all form divs on the bottom right
  var form_divs = getElementsByName_IE_fix("div", "create_contest_form_div");
  for (i=0; i<form_divs.length; i++) {
    form_divs[i].style.display = "none";
  }

  // show the specified message div on the right
  document.getElementById("create_contest_msg"+step_num).style.display = "block";

  // show dates div for step four
  if (step_num == 4) {
    document.getElementById("dates_step4_div").style.display = "block";
  } else if (step_num == 5) {
    // show the name/desc text fields
    document.getElementById("name_step5_div").style.display = "block";
  }
}

function contest_add_task(step_num, task) {
  var nav_content;

  if (step_num == 1) {
    // grab the step 1 nav content div
    nav_content = document.getElementById("nav_content1");

    if (task == "earn_money") {
      // contest is our global contest object
      contest["Goal_Money"] = document.getElementById("earn_money_amount").value;

      // add a task to nav_content1, but first
      // see if there's already an earn_money_task div
      var earn_money_task = document.getElementById("earn_money_task");
      if (earn_money_task == null) {
        earn_money_task = document.createElement("div");
        earn_money_task.setAttribute("id", "earn_money_task");
        earn_money_task.setAttribute("class", "contest_task");
        earn_money_task.className = "contest_task";
        nav_content.appendChild(earn_money_task);
      }
      earn_money_task.innerHTML = "";
      earn_money_task.innerHTML = "Earn Money Goal: $"+contest["Goal_Money"]
        +'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'earn_money_task\', \'Goal_Money\')" />';
      earn_money_task.style.display = "block";
    } else if (task == "increase_points") {
      if (goal_points_type == "") {
        alert("Please select the type of points to be increased.");
        return;
      }

      // goal_points_type: global variable == "Goal_Fame", "Goal_Buzz", or "Goal_Credibility"
      contest[goal_points_type] = document.getElementById("increase_points_amount").value;

      // add a task to nav_content1, but first
      // see if there's already an increase_points div
      var increase_points_task = document.getElementById("increase_points_"+goal_points_type+"_task");
      if (increase_points_task == null) {
        increase_points_task = document.createElement("div");
        increase_points_task.setAttribute("id", "increase_points_"+goal_points_type+"_task");
        increase_points_task.setAttribute("class", "contest_task");
        increase_points_task.className = "contest_task";
        nav_content.appendChild(increase_points_task);
      }
      increase_points_task.innerHTML = "";
      var title = goal_points_type.substring(5); // remove "Goal_"
      increase_points_task.innerHTML = title+" Points Goal: "+contest[goal_points_type]
        +'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'increase_points_'+goal_points_type+'_task\', \''+goal_points_type+'\')" />';
      increase_points_task.style.display = "block";
    }
  } else if (step_num == 2) {
    // grab the step 2 nav content div
    nav_content = document.getElementById("nav_content2");

    if (task == "money_step2") {
      contest["Wager_Money"] = document.getElementById("money_step2_amount").value;

      // add a task to nav_content2, but first
      // see if there's already a money_step2_task div
      var money_step2_task = document.getElementById("money_step2_task");
      if (money_step2_task == null) {
        money_step2_task = document.createElement("div");
        money_step2_task.setAttribute("id", "money_step2_task");
        money_step2_task.setAttribute("class", "contest_task");
        money_step2_task.className = "contest_task";
        nav_content.appendChild(money_step2_task);
      }
      money_step2_task.innerHTML = "";
      money_step2_task.innerHTML = "Money Wager: $"+contest["Wager_Money"]
        +'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'money_step2_task\', \'Wager_Money\')" />';
      money_step2_task.style.display = "block";
    } else if (task == "wager_points") {
      if (wager_points_type == "") {
        alert("Please select the type of points to be wagered.");
        return;
      }

      // wager_points_type: global variable == "Wager_Fame", "Wager_Buzz", or "Wager_Credibility"
      contest[wager_points_type] = document.getElementById("wager_points_amount").value;

      // add a task to nav_content2, but first
      // see if there's already a wager_points div
      var wager_points_task = document.getElementById("wager_points_"+wager_points_type+"_task");
      if (wager_points_task == null) {
        wager_points_task = document.createElement("div");
        wager_points_task.setAttribute("id", "wager_points_"+wager_points_type+"_task");
        wager_points_task.setAttribute("class", "contest_task");
        wager_points_task.className = "contest_task";
        nav_content.appendChild(wager_points_task);
      }
      wager_points_task.innerHTML = "";
      var title = wager_points_type.substring(6); // remove "Wager_"
      wager_points_task.innerHTML = title+" Points Wager: "+contest[wager_points_type]
        +'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'wager_points_'+wager_points_type+'_task\', \''+wager_points_type+'\')" />';
      wager_points_task.style.display = "block";
    }
  } else if (step_num == 3) {
    // grab the step 3 nav content div
    nav_content = document.getElementById("nav_content3");

    if (task == "max_num_entrants") {
      contest["By_Invitation_Only"] = "false";
      contest["Max_No_Of_Participants"] = document.getElementById("max_num_entrants").value;

      // add a task to nav_content3, but first
      // see if there's already a max_num_entrants_task div
      var max_num_entrants_task = document.getElementById("max_num_entrants_task");
      if (max_num_entrants_task == null) {
        max_num_entrants_task = document.createElement("div");
        max_num_entrants_task.setAttribute("id", "max_num_entrants_task");
        max_num_entrants_task.setAttribute("class", "contest_task");
        max_num_entrants_task.className = "contest_task";
        nav_content.appendChild(max_num_entrants_task);
      }
      max_num_entrants_task.innerHTML = "";
      max_num_entrants_task.innerHTML = "Max. No. Of Participants: "+contest["Max_No_Of_Participants"]
        +'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'max_num_entrants_task\', \'Max_No_Of_Participants\')" />';
      max_num_entrants_task.style.display = "block";
    } else if (task == "private_invitees") {
      var max_invitees = 3;

      contest["Invitees"] = document.getElementById("private_invitees").value;

      // make sure there aren't more than max_invitees
      if (contest["Invitees"].split(",").length > max_invitees) {
        alert("Error: The maximum number of private invitees is "+max_invitees+".");
        return;
      }

      // set contest to "private" rather than "public"
      contest["By_Invitation_Only"] = "true";

      // add a task to nav_content3, but first
      // see if there's already a private_invitees div
      var private_invitees_task = document.getElementById("private_invitees_task");
      if (private_invitees_task == null) {
        private_invitees_task = document.createElement("div");
        private_invitees_task.setAttribute("id", "private_invitees_task");
        private_invitees_task.setAttribute("class", "contest_task");
        private_invitees_task.className = "contest_task";
        nav_content.appendChild(private_invitees_task);
      }
      private_invitees_task.innerHTML = "";
      private_invitees_task.innerHTML = 'Invitees: <span style="font-weight:normal">'+contest["Invitees"]
        +'</span><img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'private_invitees_task\', \'Invitees\')" />';
      private_invitees_task.style.display = "block";
    }
  } else if (step_num == 4) {
    nav_content = document.getElementById("nav_content4");

    // add a task to nav_content4, but first
    // see if there's already a wager_type_task div
    var wager_type_task = document.getElementById("wager_type_task");
    if (wager_type_task == null) {
      wager_type_task = document.createElement("div");
      wager_type_task.setAttribute("id", "wager_type_task");
      wager_type_task.setAttribute("class", "contest_task");
      wager_type_task.className = "contest_task";
      nav_content.appendChild(wager_type_task);
    }
    wager_type_task.innerHTML = "";
    wager_type_task.innerHTML = "Wager Type: "+task+'<img class="link delete" src="graphics/contest_button_delete_48x16.jpg" width="48" height="16" onclick="contest_delete_task(\'wager_type_task\', \'Wager_Type\')" />';
    wager_type_task.style.display = "block";
  }

  // add the "completed" check mark if applicable
  if (nav_content.childNodes.length >= nav_content.getAttribute("min")) {
    nav_content.parentNode.className = "step_checked";
    nav_content.parentNode.setAttribute("class", "step_checked");

    // mark this step as complete
    contest_steps[step_num] = true;
  }


  if (step_num != 4) {
    // uncheck the graphical checkboxes associated with this step
    checkbox_deactivate("step"+step_num+"_checkbox", true);

    // hide all dynamic contest divs
    hide_contest_divs();
  }
}

// deletes the task from the left nav area and deletes the contest[] element
function contest_delete_task(div_id, contest_element) {
  var task = document.getElementById(div_id);
  if (task != null) {

    var parent = task.parentNode;
    var grandParent = parent.parentNode;
    parent.removeChild(task);

    // hide check mark
    if (parent.childNodes.length < parent.getAttribute("min")) {
      grandParent.className = "step_expanded";
      grandParent.setAttribute("class", "step_expanded");

      // mark this step as incomplete
      contest_steps[parent.getAttribute("step_num")] = false;
    }
  }

  contest[contest_element] = "";
}

function contest_save() {

  contest["Contest_Name"] = document.getElementById("contest_name").value;
  contest["Contest_Description"] = document.getElementById("contest_desc").value;

  var alphanumeric = /^[a-zA-Z _0-9]*$/;

  if (contest["Contest_Name"] == "" || contest["Contest_Description"] == "") {

    alert("You must enter values for \"Contest Name\" and \"Contest Description\".");
    return false;

  }

  if (!alphanumeric.test(contest["Contest_Name"]) || !alphanumeric.test(contest["Contest_Description"])) {

    alert("Please enter only letters or numbers.");
    return false;

  }

  //alert("user_gmt_offset: "+user_gmt_offset);

  $.post("account.php?op=create_contest1&user_gmt_offset="+user_gmt_offset, contest, function(dirty_output) {
    var output = dirty_output.trim();

    if (output.match(/^Error/)) {
      //alert("Unknown error");
      alert(output); return;
    } else {
      var contest_id = parseInt(output);
      tb_show(null, "account.php?op=create_contest2&c="+contest_id, null);
    }
  });
}

function send_message() {

  var alphanumeric = /^[a-zA-Z _0-9]*$/;

  var send_message_obj = {
    "op" : "do_send_message",
    "send_msg_to" : document.getElementById("send_msg_to").value,
    "send_msg_subject" : document.getElementById("send_msg_subject").value,
    "send_msg_body" : document.getElementById("send_msg_body").value
  }

  for (var x in send_message_obj) {

    if (x == "op") {
      continue;
    }

    if ((x == "send_msg_to" || x == "send_msg_body") && send_message_obj[x] == "") {

      alert("You must enter a value for \"To\" and \"Message\".");
      return false;

    }

    if (!alphanumeric.test(send_message_obj[x])) {

      alert("Please enter only letters or numbers.");
      return false;

    }

  }

  $.post("account.php", send_message_obj, function(dirty_output) {
    var output = dirty_output.trim();
    var thick_box = document.getElementById("send_msg_lightbox");
    thick_box.innerHTML = "";
    thick_box.innerHTML = output;
  });
}

// does away with the "unread_message" class name
function mark_as_read(message_id) {
  var msg = document.getElementById("read_msg_"+message_id);
  msg.className = "";
  msg.setAttribute("class", "");
}

function delete_message(message_id) {
  $.get("account.php?op=delete_message&m="+message_id, function(success) {
    if (success) {
      window.location.reload();
    } else {
      alert("Error: failed to delete this message");
    }
  });
}

function show_username_lookup() {
  // hide the send message div
  var send_msg_container = document.getElementById("send_msg_container");
  send_msg_container.style.display = "none";

  // show the username lookup div
  var username_lookup_container = document.getElementById("username_lookup_container");
  username_lookup_container.style.display = "block";
}

function hide_username_lookup() {
  // hide the username lookup div
  var username_lookup_container = document.getElementById("username_lookup_container");
  username_lookup_container.style.display = "none";

  // show the send message div
  var send_msg_container = document.getElementById("send_msg_container");
  send_msg_container.style.display = "block";
}

function do_username_lookup() {
  var username_lookup_obj = {
    "op" : "username_lookup",
    "lookup_username" : document.getElementById("lookup_username").value,
    "lookup_city_state" : document.getElementById("lookup_city_state").value
  }

  $.post("account.php", username_lookup, function(dirty_output) {
    var output = dirty_output.trim();
    var lookup_results = document.getElementById("lookup_results");

    // remove previous search results
    while (lookup_results.length > 0) {
      lookup_results.remove(0);
    }

    // parse the AJAX output

    // populate the lookup_results <select> element

  });
}

function select_username() {
  // grab selected username
  var lookup_results = document.getElementById("lookup_results");
  var username = lookup_results.options[lookup_results.selectedIndex].text;

  // insert the username into send_msg_to text field
  document.getElementById("send_msg_to").value = username;

  hide_username_lookup();
}

// no need for:
// "function reg_one()"
// ...that AJAX call is made when the Registration button is clicked

// process registration form -- step one
function do_reg_one() {
  var reg_username = encodeURIComponent(document.getElementById("reg_username").value);

  $.get("account.php?op=do_reg_one&reg_username="+reg_username, function(dirty_output) {
    var output = dirty_output.trim();
    var thick_box = document.getElementById("reg_lightbox");
    thick_box.innerHTML = "";
    thick_box.innerHTML = output;
  });
}

// show registration form -- step two
function reg_two() {
  $.get("account.php?op=reg_two", function(dirty_output) {
    var output = dirty_output.trim();
    var thick_box = document.getElementById("reg_lightbox");
    thick_box.innerHTML = "";
    thick_box.innerHTML = output;
  });
}

// process registration form -- step two
function do_reg_two() {
  var reg_username = encodeURIComponent(document.getElementById("reg_username").value);
  var reg_pin = encodeURIComponent(document.getElementById("reg_pin").value);
  var reg_password = encodeURIComponent(document.getElementById("reg_password").value);
  var reg_confirm_password = encodeURIComponent(document.getElementById("reg_confirm_password").value);
  $.get("account.php?op=do_reg_two&reg_username="+reg_username+"&reg_pin="+reg_pin+"&reg_mobile_number="+555-555-5555+"&reg_password="+reg_password+"&reg_confirm_password="+reg_confirm_password, function(dirty_output) {
    var output = dirty_output.trim();
    var thick_box = document.getElementById("reg_lightbox");
    thick_box.innerHTML = "";
    thick_box.innerHTML = output;
  });
}

function login() {
  if (!checkInput("reg", "webreg")) {
    alert("input error");
    return false;
  }

  var login_username = encodeURIComponent(document.getElementById("login_username").value);
  var login_password = encodeURIComponent(document.getElementById("login_password").value);

  var login_box = document.getElementById("login_box");
  login_box.innerHTML = "";
  login_box.innerHTML = '<div style="relative">';
  login_box.innerHTML += '<img style="position:absolute; top:27px; left:50px; z-index:99" src="graphics/loader.gif"/>';
  login_box.innerHTML += '</div>';

  $.get("account.php?op=login&login_username="+login_username+"&login_password="+login_password, function(dirty_output) {
    var output = dirty_output.trim();

    if (output.match(/^Error/)) {
      alert(output);

      $.get("account.php?op=show_login_form", function(dirty_output) {
        var output = dirty_output.trim();
        login_box.innerHTML = "";
        login_box.innerHTML = output;
      });
    } else {
      window.location.reload();
//      $.get("account.php?op=show_welcome_msg&username="+login_username, function(dirty_output) {
//        var output = dirty_output.trim();
//        login_box.innerHTML = "";
//        login_box.innerHTML = output;
//      });
//
//      // show the subnav bar
//      document.getElementById("subnav").style.display = "block";
    }
  });
}

// subnav active tag toggle woot
function toggle_subnav(id1,id2,id3) {

  img_one = document.getElementById(id1).src;
  img_two = document.getElementById(id2).src;

  if(img_one.search(/_on/) < 0) {
    var onk2 = img_one.replace(/.gif/,'_on.gif');
    document.getElementById(id1).src = onk2;
  }

  if(img_two.search(/_on/) > 0) {
    var onk3 = img_two.replace(/_on.gif/,'.gif');
    document.getElementById(id2).src = onk3;
  }

  if(id3) {

    img_three = document.getElementById(id3).src;

    if(img_three.search(/_on/) > 0) {
      var onk4 = img_three.replace(/_on.gif/,'.gif');
      document.getElementById(id3).src = onk4;
    }

  }

}

function logout() {
  window.location.href = "logout.php";
}

function showhide(id) {
  if (document.getElementById) {
    obj = document.getElementById(id);
    if (obj.style.display == "none") {
      obj.style.display = "";
    } else {
      obj.style.display = "none";
    }
  }
}

//proto for content nav menu
function hideDIV(d) {
  document.getElementById(d).style.display = "none";
}

function displayDIV(d) {
  document.getElementById(d).style.display = "block";
}

function showIMG(d,e) {
  document.getElementById(d).src = document.getElementById(e).src;
}


function bgit(id, id2, id3) {
  if (document.getElementById) {

    obj = document.getElementById(id);
    obj2 = document.getElementById(id2);
    obj3 = document.getElementById(id3);

    objc = document.getElementById(id + "1").src;
    objc2 = document.getElementById(id2 + "1").src;
    objc3 = document.getElementById(id3 + "1").src;

    if(obj.src.search(/cropped/) < 0) {

      obj.src =  "graphics/cropped.gif?" + Math.random();
      obj2.src = "graphics/pixel_clear.gif";
      obj3.src = "graphics/pixel_clear.gif";

      if(objc.search(/_on/) < 0) {
        var onk2 = objc.replace(/.gif/,'_on.gif');
        document.getElementById(id + "1").src = onk2;
      }

      if(objc2.search(/_on/) > 0) {
        var onk3 = objc2.replace(/_on.gif/,'.gif');
        document.getElementById(id2 + "1").src = onk3;
      }

      if(objc3.search(/_on/) > 0) {
        var onk4 = objc3.replace(/_on.gif/,'.gif');
        document.getElementById(id3 + "1").src = onk4;
      }

    }
  }
}

/*
  document.getElementById(id).style.backgroundColor ? "darkgreen":"lightgreen";
  var onk = document.getElementById(id);
  onk.bgColor = "lightgreen";
  style.bgColor = style.bgColor ? "darkgreen":"lightgreen"; */


function hideit(id) {
  var onk = document.getElementById(id);
  onk.style.display = "none";
}

function showit(id) {
  var onk = document.getElementById(id);
  onk.style.display = "block";
}


/**
 * JSON template for storing how we check various page elements.
 */
var inputFields = {
/*
  "formname" : {

    "elementsuffix" : {

      "type" : "text",
      "description" : "Element Description",
      "confirm" : false,
      "empty" : false

    }
*/
};

/**
 * Check input fields.
 */
function checkInput(prefix,form) {

  var errorMsg = "";
  var returnValue = true;

  for (var x in inputFields[form]) {

    var thisElement = document.getElementById(prefix + "_" + x);

    // check to see if we have a confirm field
    if (inputFields[form][x].confirm) {

      var thisElementConfirm = document.getElementById(prefix + "_confirm_" + x);

      if (thisElement.value != thisElementConfirm.value) {

        errorMsg = errorMsg + "The values entered for " + inputFields[form][x].description + " do not match.\n";

        returnValue = false;

      }

    }

    // check to make sure it's not empty
    if (!inputFields[form][x].empty && thisElement.value == "") {

      errorMsg = errorMsg + "You must enter a value for " + inputFields[form][x].description + "\n";

      returnValue = false;

    } else {

      // do any special processing
      switch (inputFields[form][x].type) {

        case "email":

          if (!validEmail(thisElement.value)) {

            returnValue = false;

          }

          break;

        case "password":

          if (thisElement.value.length < 6) {

            errorMsg = errorMsg + "Your password must be at least 6 characters long.\n";

            returnValue = false;

          }

          break;

        default:

          break;

      }

    }

  }

  // display any errors that we've accumulated
  if (errorMsg != "") {

    alert(errorMsg);

  }

  return returnValue;

}

/**
 * Check to see if an email address is valid (stolen code).
 */
function validEmail(eAddr) {

  var result = false;
  var ndxAt = ndxDot =  0;

  var ndxAt = eAddr.indexOf("@");
  var ndxDot = eAddr.indexOf(".");
  var ndxDot2 = eAddr.lastIndexOf(".");

  if ((ndxDot < 0) || (ndxAt < 0)) {

    alert("Your email address lacks a '.' or '@'.\n\nThe valid format is 'you@domain.suffix.'");

  } else if ( (ndxDot2 - 3) <= ndxAt) {

    alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");

  } else {

    result = true;

  }

  return result;

}


function clearvalue() {

  var js_email = document.getElementById("player_search");

  if(js_email.value == "Enter player\'s name") {
    js_email.style.color = "#000";
    js_email.value = "";
  }

}


function refillvalue() {

  var js_email = document.getElementById("player_search");

  if(js_email.value == "") {
    js_email.style.color = "#aaa";
    js_email.value = "Enter player\'s name";
  }

}


function resetvalue() {

  var js_email = document.getElementById("player_search");

  js_email.style.color = "#aaa";
  js_email.value = "Enter player\'s name";

}


function sbswap(d) {

  var js_img = document.getElementById("leader_board_header_swap");

  if(d == "career") {
    js_img.src = "graphics/text_career_score_85x22.gif";
  } else if(d == "battle") {
    js_img.src = "graphics/text_battle_points_85x22.gif";
  } else {
    return false;
  }

}


function extendz(d,smallheight,bigheight) {

  var box_size = document.getElementById(d);

  if(box_size.style.height == bigheight + "px") {

  box_size.style.height = smallheight + "px";

  } else {

  box_size.style.height = bigheight + "px";

  }

}


function toggle_minmax(d) {

  var img = document.getElementById(d).getElementsByTagName("img")[0];
  var imgSrc = img.src;

  if (imgSrc.search(/_on/) > 0) {

    img.src = imgSrc.replace(/_on.gif/,'.gif');

  } else if (imgSrc.search(/_on/) < 0) {

    img.src = imgSrc.replace(/.gif/,'_on.gif');

  }

}


function centeredWindow(url,name,width,height,scrollbars,resizeable) {
  var xPos = (screen.availWidth / 2) - (width / 2);
  var yPos = (screen.availHeight / 2) - (height / 2);

  var options = "width=" + width + ",height=" + height + ",resizeable=" + resizeable +",scrollbars=" + scrollbars + ",left=" + xPos + ",top=" + yPos;

  wind = window.open(url,name,options);
  wind.focus();
}

/**
 * Open the Privacy Policy.
 */
function openPrivacyWindow() {
  centeredWindow("legal.php?action=privacy","legalWindow",850,700,1,1);
}

/**
 * Open the Terms of Use.
 */
function openTermsWindow() {
  centeredWindow("legal.php?action=terms","legalWindow",850,700,1,1);
}
var current = "";

function showInstructions() {

  // if something is showing, hide it
  if (current != "") {
    hideInstructions();
  }

  var chosenId = document.getElementById("selectPage").options[document.getElementById("selectPage").selectedIndex].value;

  var chosenObject = document.getElementById(chosenId);

  if (chosenObject) {

    chosenObject.style.display = "block";
    document.getElementById('popup_background').style.display = "block";

    current = chosenId;

  }

}

function hideInstructions() {

  var currentObject = document.getElementById(current);

  if (currentObject) {

    currentObject.style.display = "none";
    document.getElementById('popup_background').style.display = "none";

    current = "";

  }

}