
var img_minus = new Image(); img_minus.src = "/conferences/images/minus.gif";
var img_plus = new Image(); img_plus.src = "/conferences/images/plus.gif";

function submitForm(form) {
  document.forms[form].submit();
  return false;
}

function singleSelect(form, count, item) {
  var i;
  for(i=0;i<count;i++)
    if(i!=item) eval("form.item_" + i + ".checked = false;");
    else eval("form.item_" + i + ".checked = true;");
  return true;
}

function returnSelected(form, count) {
  var i;
  for(i=0;i<count;i++)
    if(eval("form.item_" + i + ".checked")) 
      return eval("form.item_" + i + ".value");
  return -1;
}

var current_cell;
var currenth_cell;


function FindIndex(login) {
  for(i=1;i<login_list.length;i++)
    if(login_list[i] == login) return i;
return -1;
}

function UpdateAssignCounts(index1, index2) {
  if(index1 != -1)
    assign_count[index1]--;
  if(index2 != -1)
    assign_count[index2]++;
}

function UpdateAssignText(selectbox, index1, index2) {
  if(index1 != -1)
    selectbox.options[index1].text = author_list[index1] + ' (' + assign_count[index1] + ')';
  if(index2 != -1)
    selectbox.options[index2].text = author_list[index2] + ' (' + assign_count[index2] + ')';
}

function UpdateAssignText2(selectbox, index1, index2, paper) {
  if(pref_list)
    var prefs = pref_list[paper];
  if(index1 != -1)
    if(prefs)
      selectbox.options[index1].text = pref_text[prefs[index1-1]] + author_list[index1] + ' (' + assign_count[index1] + ')';
    else
      selectbox.options[index1].text = author_list[index1] + ' (' + assign_count[index1] + ')';
  if(index2 != -1)
    if(prefs)
      selectbox.options[index2].text = pref_text[prefs[index2-1]] + author_list[index2] + ' (' + assign_count[index2] + ')';
    else
      selectbox.options[index2].text = author_list[index2] + ' (' + assign_count[index2] + ')';
}

function ComputeAssignCounts(selectbox) {
  for(i=1;i<selectbox.options.length;i++) 
    selectbox.options[i].text = author_list[i] + ' (' + assign_count[i] + ')';
}


function AddAuthor(selectbox) {
  index1 = FindIndex(currenth_cell.value);
  if(selectbox.selectedIndex == 0) {
    current_cell.value = '';
    currenth_cell.value = '';
  } else {
    current_cell.value = author_list[selectbox.selectedIndex];
    currenth_cell.value = login_list[selectbox.selectedIndex];
  }
  index2 = FindIndex(currenth_cell.value);
  UpdateAssignCounts(index1,index2);
  for(j=0;j<10;j++)
    UpdateAssignText(eval("document.forms['Assignment'].SelectBox_" + j), index1,index2);
}


function AddAuthor2(selectbox) {
  index1 = FindIndex(currenth_cell.value);
  if(selectbox.selectedIndex == 0) {
    current_cell.value = '';
    currenth_cell.value = '';
  } else {
    current_cell.value = author_list[selectbox.selectedIndex];
    currenth_cell.value = login_list[selectbox.selectedIndex];
  }
  index2 = FindIndex(currenth_cell.value);
  UpdateAssignCounts(index1,index2);
  UpdateAssignText(selectbox, index1,index2);
}

function AddOneAuthor(selectbox,rname, login) { 
  index1 = FindIndex(login.value);
  if(selectbox.selectedIndex == 0) {
    login.value = '';
    rname.value = '';
  } else {
    rname.value = author_list[selectbox.selectedIndex];
    login.value = login_list[selectbox.selectedIndex];
  }
  index2 = FindIndex(login.value);
  UpdateAssignCounts(index1,index2);
  UpdateAssignText(selectbox, index1,index2);
}


function AddOneAuthor2(selectbox,rname, login, paper) { 
  var index1 = FindIndex(login.value);
  if(selectbox.selectedIndex == 0) {
    login.value = '';
    rname.value = '';
  } else {
    rname.value = author_list[selectbox.selectedIndex];
    login.value = login_list[selectbox.selectedIndex];
  }
  var index2 = FindIndex(login.value);
  UpdateAssignCounts(index1,index2);
  UpdateAssignText2(selectbox, index1,index2, paper);
}




function AddAuthor3(selectbox,paper,reviewer,frm,topic) {
  var papers = topic_list[topic];
  var len = papers.length;
  alltopic = frm.AllTopic.checked;
  if(alltopic) {
    for(cc=0;cc<len;cc++) {
      rname = eval("frm.Name_" + papers[cc] + "_" + reviewer);
      login = eval("frm.Login_" + papers[cc] + "_" + reviewer);
      AddOneAuthor(selectbox, rname, login);
    }
  } else {
    rname = eval("frm.Name_" + paper + "_" + reviewer);
    login = eval("frm.Login_" + paper + "_" + reviewer);
    AddOneAuthor(selectbox, rname, login);
  }
}

function AddAuthor4(selectbox,paper,reviewer,frm,topic) {
  var papers = topic_list[topic];
  var len = papers.length;
  var alltopic = frm.AllTopic.checked;
  if(alltopic) {
    for(cc=0;cc<len;cc++) {
      var rname = eval("frm.Name_" + papers[cc] + "_" + reviewer);
      var login = eval("frm.Login_" + papers[cc] + "_" + reviewer);
      AddOneAuthor2(selectbox, rname, login, paper);
    }
  } else {
    var rname = eval("frm.Name_" + paper + "_" + reviewer);
    var login = eval("frm.Login_" + paper + "_" + reviewer);
    AddOneAuthor2(selectbox, rname, login, paper);
  }
}


function toggleVisibility(name) {
  var obj = document.getElementById(name);
  var img = name + "_image";
  if(obj.style.display == 'none') {
    obj.style.display = 'inline';
    document[img].src = img_minus.src;
  } else {
    obj.style.display = 'none';
    document[img].src = img_plus.src;
  }
  return false;
}

function hideMenus() {
  var obj1 = document.getElementById("header_row");
  var obj2 = document.getElementById("footer_row");
  var obj3 = document.getElementById("left_menu");
  var obj4 = document.getElementById("top_menu");
  obj1.style.display = "none";
  obj2.style.display = "none";
  obj3.style.display = "none";
  obj4.style.display = "none";
  window.print();
  alert('Click Ok to show menus after printing completes');
  showMenus();
  return false;
}

function showMenus() {
  var obj1 = document.getElementById("header_row");
  var obj2 = document.getElementById("footer_row");
  var obj3 = document.getElementById("left_menu");
  var obj4 = document.getElementById("top_menu");
  obj1.style.display = "inline";
  obj2.style.display = "inline";
  obj3.style.display = "inline";
  obj4.style.display = "inline";
  return false;
}


function cookie_test(cookie, domain) {
  var x_cookieName = cookie;
  var x_domain  = domain;

  var x_expires = new Date(); x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
  xDeleteCookie(x_cookieName); // don't get false positive

  // add path if you want to. drop expires for session cookie. drop domain for default domain test.
  document.cookie = x_cookieName + "=test; expires=" + x_expires.toGMTString() + "; domain=" + x_domain;

  // now look for it.
  var x_cookieString = document.cookie || "";
  var x_cookies = x_cookieString.split(/\s*;\s*/);
  var x_found = 0;
  for (var i in x_cookies) {
    var cookie_ = x_cookies[i];
    var dough = cookie_.split(/\s*=\s*/);
    if (dough[0] == x_cookieName) { x_found = 1; break; }
  }

  // ensure it's gone
  xDeleteCookie(x_cookieName);

	    // do whatever you want with x_found bool
  if (x_found == 1) {
    return true;
  }
  else {
    return false;
  }
}

function xDeleteCookie(name) {
  var oldDate = new Date(1970, 1, 1);
  document.cookie = name + "=0; expires=" + oldDate.toGMTString();
}

function PaperMenu(session, sabs, spap, paper) {
  document.writeln('<FORM script=ess.pl METHOD=POST><TD>');
  document.writeln("<INPUT TYPE=HIDDEN NAME=paper VALUE=" + paper + ">");
  document.writeln("<INPUT TYPE=HIDDEN NAME=s VALUE=" + session + ">");
  document.writeln('<SELECT NAME=papertask>');
  document.writeln('<OPTION VALUE=view>View</OPTION>');
  if(sabs || spap) document.writeln('<OPTION VALUE=edit>Edit</OPTION>');
  if(spap) document.writeln('<OPTION VALUE=usubmitform>Upload</OPTION>');
  document.writeln('<OPTION VALUE=dsubmit>Download</OPTION>');
  if(sabs) document.writeln('<OPTION VALUE=delete>Delete</OPTION>');
  document.writeln('</SELECT>');
  document.writeln('<INPUT TYPE=SUBMIT VALUE=go></TD></FORM>');
}

function psubmit(task) {
  var form = document.forms["PaperForm"];
  form.papertask.value = task;
  form.submit();
  return false;
}

function PaperReviewerList(reviewerPerPaper, paper) {
  var i;
  document.writeln("<TABLE>");
  for(i=0;i<reviewerPerPaper;i++) {
    if(i % 5 == 0) document.writeln("<TR>");
    document.writeln("<TD>");
    document.writeln("<INPUT TYPE=TEXT NAME=Name_" + paper + "_" + i + " SIZE=15 VALUE='' onFocus=\"ReviewerBox('" + paper + "','" + i + "',0);\">"); 
    document.writeln("<INPUT TYPE=HIDDEN NAME=Login_" + paper + "_" + i + " VALUE=''>");
    if(i % 5 == 4) document.writeln("</TR>");
  }
  document.writeln("</TABLE>");
}

function ReviewerBox (p,r,t) {
  paper = p;
  reviewer = r;
  topic = t;
  frm = document.forms['Assignment'];
  var i;
  var options = "<TABLE><TR><TD><FORM NAME=ReviewerBox><SELECT NAME=SelectBox SIZE=12 onChange='AddAuthor4(this,paper,reviewer,frm,topic);'>";
  for(i=0;i<author_list.length;i++) {
    options = options + "<OPTION>";
    if(i!=0) options = options + pref_text[pref_list[paper][i-1]];
    options = options + author_list[i];
    if(i!=0) options = options + "(" + assign_count[i] + ")";
    options = options + "</OPTION>";
  }
  options = options + "</SELECT></TD></TR></FORM></TABLE>";
  overlib(options,STICKY,CAPTION,'Reviewer List');
  return true;
}


//--------------------------------- MENU DISPLAY FUNCTIONS ---------//

function m_in(obj, color) {
  obj.style.borderStyle='inset';
  obj.style.color = color;
}

function m_out(obj, color) {
  obj.style.borderStyle='solid';
  obj.style.color = color;
}

