/* PLEASE READ

IF YOU ARE SEEING THIS, you are probably using an old (3.x or older) browser.
To solve this problem and see the correct page, you can either upgrade to a
newer browser or disable javascript in your old browser.

Alternately, you have loaded this file manually in order to inspect our
fine javascript source code.  Enjoy.

script.js $Revision: #1 $
*/

if (window.location != window.parent.location) {
  window.parent.location = window.location;
}

function autoWarn() {
  var form = document.log;
  var select = form.subaction;
  if (select.selectedIndex == 1) {
    if (!confirmAutoReport()) {
      select.selectedIndex=0;
    }
  }
}

function sure() {
  return confirm("Are you sure?");
}

function submitConf(what) {
  var form = document.log;
  var i, n, check, cnt;
  n = form.elements[what].length;
  cnt = 0;
  if (!n) {
    if (form.elements[what].checked == true) {
      cnt = 1;
    }
  } else {
    for (i=0; i < n; i++) {
      check = form.elements[what][i];
      if (check.checked == true) {
	cnt++;
      }
    }
  }
  if (cnt == 1) {
    return confirm("This action will delete 1 email.");
  } else if (cnt > 0) {
    return confirm("This action will delete " + cnt + " emails.");
  } else {
    return true;
  }
}       

function checksure(cb) {
  if (cb.checked == true) {
    return sure();
  } else {
    return true;
  }
}

function checkAll(what) {
  var form = document.log;
  var i, n, check;
  n = form.elements[what].length;
  if (!n) {
    form.elements[what].checked=true;
  } else {
    for (i=0; i < n; i++) {
      check = form.elements[what][i];
      check.checked = true;
    }
  }
}

function showDate (dtx) {
  var dte;
  dte = new Date(dtx * 1000);
  if (dte.getYear() >= 2038) {
    dte = new Date(dtx);
  }
  var tz = (dte.getTimezoneOffset()/60)*100;
  var tzs = new String(Math.abs(tz));
  while ((tzs.length) < 4) {
    tzs = "0" + tzs;
  }
  if (tz < 0) {
    tzs = "+" + tzs;
  } else {
    tzs = "-" + tzs;
  }
  if (tz == 0) {
    return dte.toGMTString();
  } else {
    return dte.toLocaleString() + " " + tzs;
  }
}

function formval(maxlength) {
  var textval = document.forms['submitspam'].spam.value;
  var warning = "\n[truncated by SpamCop]\n";
  if (textval.length > maxlength) {
    if (confirm("Message is larger than maximum size, " + maxlength + " bytes. \ Truncate?")) {
      document.forms['submitspam'].spam.value =
      textval.substr(0, (maxlength - warning.length) - 1) +warning;
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
}

function confirmAutoReport() {
  var message="THIS WILL REPORT WITHOUT FURTHER CONFIRMATION!\n" +
     "Reporting spam without confirmation is dangerous both because you\n" +
     "must be absolutely sure what is spam AND because if there is any\n" +
     "error in finding the spam's source, you may report your own\n" +
     "mailserver or ISP without realizing it\n\n" +
     "Are you sure you want to use this option?"
  return confirm(message);
}

function confirmAppeal(checkbox) {
  var message="THIS CHECKBOX IS DISABLED!\n" +
     "This checkbox has probably been disabled because the\n" +
     "account is already closed or this site is not related to the\n" +
     "spam.\n\n" +
     "Are you sure you want challenge this status?  To do so, click\n" +
     "cancel and then enter an explanation in the notes section\n" +
     "indicating:\n" +
     "- Why you believe the site is still active\n" +
     "- Why you are sure it is responsible for the spam\n\n" +
     "Otherwise, CLICK \"OK\" NOW";

  var message2="Are you really sure?  This account has already been\n" +
               "CLOSED or is INNOCENT!  Please click CANCEL to confirm.";

  if ((checkbox.checked == true) &&
      (!confirm(message))) {
    checkbox.checked=true;
  } else {
    checkbox.checked=false;
  }
}

function confirmNomunge(checkbox) {
  var message="REVEALING SENSITIVE INFORMATION!\n" +
     "This checkbox is disabled because this ISP refuses to accept\n" +
     "reports with the recipient information (your email address)\n" +
     "concealed.\n\n" +
     "If you file this report, it will be sent with your email address\n" +
     "revealed.  ONLY do this if you specifically trust this ISP.\n" +
     "If you are unsure, CLICK CANCEL NOW!";

  if ((checkbox.checked == true) && (confirm(message))) {
    checkbox.checked=true;
  } else {
    checkbox.checked=false;
  }
}

function showcomment(name) {
  document.location = "#" + name;
  document.forms['sendreport'].elements[name].focus();
}

/* validSelection function courtessy of Jack from Bellingham */
function validSelection () {
  var max = document.log.elements.length;
  var anyCheck = false;
  var errmsg = ""
  for (var i = 0; i < max; i++) {
    if (document.log.elements[i].type == "checkbox") {
      if (document.log.elements[i].checked == true) {
        var anyCheck = true;
        break;
      }
    }
  }
  if (anyCheck == false) {
    errmsg = errmsg + "You must select at least one message.\n";
  }
  if (document.log.subaction.selectedIndex == 0) {
    errmsg = errmsg + "You must select an action from the list above.";
  }
  if (errmsg.length > 0){
    alert (errmsg);
    return false;
  } else {
    return true;
  }
}

function menuChoice() {
  var form = document.forms['menuform']
  var URL = form.mainmenu[form.mainmenu.selectedIndex].value;
  form.mainmenu.selectedIndex = 0;
  if (URL != '') {
    window.location = URL;
  }
  return true;
}

function fontPlus() {
  var style, newstyle;
  style = readCookie("style");
  if (style == 'Large') {
    newstyle = 'XLarge';
  } else if (style == 'XLarge') {
    newstyle = "XLarge";
  } else if (style == 'Small') {
    newstyle = '';
  } else if (style == '') {
    newstyle = 'Large';
  } else if (style == 'XSmall') {
    newstyle = 'Small';
  } else {
    newstyle = 'Large';
  }
  if (style != newstyle) {
    setStyle(newstyle);
  }
}

function fontMinus() {
  var style, newstyle;
  style = readCookie("style");
  if (style == 'Large') {
    newstyle = '';
  } else if (style == 'XLarge') {
    newstyle = "Large";
  } else if (style == 'Small') {
    newstyle = 'XSmall';
  } else if (style == 'XSmall') {
    newstyle = 'XSmall';
  } else {
    newstyle = 'Small';
  }
  if (style != newstyle) {
    setStyle(newstyle);
  }
}

function setStyle(title) {
  setActiveStyleSheet(title);
  createCookie("style", title, 365);
}

/* copied from  styleswitcher.js http://www.alistapart.com/articles/alternate/
*/
function setActiveStyleSheet(title) {
  if(! document.getElementsByTagName) return;
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function createCookie(name,value,days) {
  var expires;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+((60*60*24*1000)*days));
    expires = "; expires="+date.toGMTString();
  } else {
    expires = "";
  }
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function readFaq(msg, faqurl, continueurl) {
  if (confirm(msg)) {
    document.location=faqurl;
  } else {
    document.location=continueurl;
  }
}

/* Show/hide the contact form in cases where it's hidden by default */
function hideForm() {
  document.getElementById('hideform').style.display='none';
}
function showForm() {
  document.getElementById('hideform').style.display='inline';
}

function visToggle(item) {
    var I = document.getElementById(item + "Icon");
    var D = document.getElementById(item + "Info");

    if (D.style.display == "" || D.style.display == "none") {
        D.style.display = "table-row";
        I.src = "/images/expanded.png";
    } else {
        D.style.display = "none";
        I.src = "/images/collapsed.png";
    }
}

function checkDomain(nname){
    var i;
    var domains, errors = new Array();
    domains = nname.split('\n');        
    var val = true;
    for (i=0; i < domains.length; i++) {
      if ((! domains[i].match (/^([a-z0-9][a-z0-9\-]{0,61}[a-z0-9]{0,61}\.)+[a-z]{2,6}$/i))) {
        val = false;
        errors.push(domains[i]);
      }
    } if ( !val ) {
        alert("Warning! Following name(s): \n"+ errors.join(', ') +"\nis not valid domain name." );   
        return false;
    }
    return true;
}


function checkDomain_new(nname){
    var i;
    var domains, errors  = new Array();
    domains = nname.split(/(?:\,|;|\n|\s)/);

    var val = true;
    for (i=0; i < domains.length; i++) {
      if (!domains[i].match (/\D\W/i)) 
      {
      continue;
      }

      if (! domains[i].match (/^([a-z0-9][a-z0-9\-]{0,61}[a-z0-9]{0,61}\.)+[a-z]{2,6}$/i)) {
        val = false;
        errors.push(domains[i]);
      }
    } 
    if ( !val ) {
        alert("Warning! Following name(s): \n"+ errors.join(', ') +"\nis not valid domain name." );   
        return false;
    }
    return true;
}


function showEditForm(item) {
    var L = document.getElementById(item + "Link");
    var F = document.getElementById(item + "Form");
    if (F.style.display == "" || F.style.display == "none") {
        F.style.display = "inline";
        L.style.display = "none";
    } else {
        F.style.display = "none";
        L.style.display = "inline";
    }
}

