/***************************
 Replay javascript functions
 ***************************/

function popup(boundName) {
  replayWindow = window.open("/sc/php/popup.php?name=" + boundName, "repWindow", "location=no,width=500,height=300");
}

function popupTop(boundName,wid,hei) {
  replayWindow = window.open("popup.php?name=" + boundName, "repWindow", "location=no,width=wid,height=hei");
}

function expandrep(n) {
  var rep = document.getElementById(n);
//  var img = document.getElementById("img" + n);

  if(rep.className == 'hidden') {
    rep.className = 'visible';
//    img.src = imgunexpand.src;
//    img.title = imgunexpand.title;
  } else {
    rep.className = 'hidden';
//    img.src = imgexpand.src;
//    img.title = imgexpand.title;
  }
}

// Current Page Reference
// copyright Stephen Chapman, 1st Jan 2005
// you may copy this function but please keep the copyright notice with it
function getURL(uri) {
  uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
  uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
  uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
  uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
  pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
  pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
  uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
  uri.file = uri.page;
  if (uri.ext != '') uri.file += '.' + uri.ext;
  if (uri.file == '') uri.page = 'index';
  uri.args = location.search.substr(1).split("?");
  if (uri.args[0] != '') { // separate each arg by name/value
    uri.argsplit = uri.args[0].split("&"); uri.argname = []; uri.argval = [];
    for (var i=0; i<uri.argsplit.length; i++) {
      uri.argname[i] = uri.argsplit[i].split("=")[0];
      uri.argval[i]  = uri.argsplit[i].split("=")[1];
    }
  }
  return uri;
}

function init() {
  document.frmsearch.terms.focus();
  var uri = new Object();
  getURL(uri);


  // replay db stuff
  if (uri.file == "rep_db.php" && uri.args[0] != '') {
    for(var i=0; i<uri.argname.length; i++) {
      if (uri.argname[i] == "expand") {
        expandrep(uri.argval[i]); // can expand multiple reps
      }
    }
    //alert(uri.args[0].split("&")[1]);
    //alert(uri.argname[1] + " = " + uri.argval[1]);
  }

  // bound archive stuff
  if (uri.file == "bounds.php") {
    // sadface :(
  }
}

// used only when Checking Out a replay
// Check in is done on another page, like how admin does it.
function reviewrep(id,status,reviewer) {
	var element = document.getElementById(id);	// status box
	element.innerHTML = 'Locking replay for review...';
	xmlhttp.open("POST", '../reviewrep.php',true);      // true = asynchronous?
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");    // Tell server to expect form data!
	xmlhttp.send(
		"&p0omzxx1=" + id +
		"&j3dcnq8u=" + status +
		"&cmvow3nf=" + reviewer); // wierd param names for security? user can call this script from ANY html page!!

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // script ran w/ success
			var check = xmlhttp.responseText;
			element.innerHTML = xmlhttp.responseText;

			// An error occured. Do not disable/enable any fields
			if (check.charAt(0) != "-") {

				// User has checked out this rep. Change color & give download link
				if (status == "checkout") {
					element.style.backgroundColor = "#FFFFD2";
					element.style.border = "2px solid #FF0000";
				}
			}
		}
	}
}
