// Register common
sf = new Image(17,16); 
sf.src="images/sunflower.png";
sf_over = new Image(); 
sf_over.src="images/sunflower-over.png";

function Trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
function GenPass (len) {
    var plen = len;
    var password="";
    var possible = "abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    while (password.length < plen) {
        password = password + possible.substr(Math.floor(Math.random()*possible.length), 1);
    }
    document.account.genpass.value = password;
}
function UsePass() {
    var password=document.getElementById('genpass').value;
    if (password.length > 0) {
      document.account.password1.value = password;
      document.account.password2.value = password;
      document.account.genpass.value = "";
    }
}
var HELP='&nbsp;';
var MSG='&nbsp;';
function DoMsg(msg) {
  var textnode=document.getElementById('message');
  textnode.innerHTML=msg;
  MSG=msg;
  return;
}
function SetMsg(msg) {
    mesg=escape(msg);
    var curCookie = "registerMsg=" + mesg + "; path=/";
    document.cookie = curCookie;
}
function GetMsg() {
  LOADED = true;
  var allcookies = document.cookie;
  var pos = allcookies.indexOf("registerMsg");
  if (pos != -1) {
    var start = pos + 12;
    var end = allcookies.indexOf(';', start);
    if (end == -1) {end = allcookies.length;}
    var msg = allcookies.substring(start,end);
    var regex9 = /[\+|\s]/ig;
    msg = msg.replace(regex9, "&nbsp\;");
    msg = unescape(msg);
    if (msg.substring(0,1) == '@') {
      var msgurl = msg.substring(1);
      msgWindow(msgurl);
    } else {
      DoMsg(msg);
    }
    var curCookie = "registerMsg=" + "+" + "; path=/";
    document.cookie = curCookie;
  }
}
function ClearMsg() {
  var textnode=document.getElementById('message');
  textnode.innerHTML=HELP;
  MSG='&nbsp;';
  return;
}
function DoHelp(help) {
  var textnode=document.getElementById('message');
  textnode.innerHTML=help;
  HELP=help;
  return;
}
function ClearHelp() {
  var textnode=document.getElementById('message');
  textnode.innerHTML=MSG;
  HELP='&nbsp';
  return;
}
function ClearList() {
  var i, box;
  for (i = 0; i <document.list.elements.length; i++) {
    box = document.list.elements[i];
    if (box.type == "radio") {
      box.checked = false;
    }
  }
  ClearMsg();
}
function isValidCreditCard(type, ccnum) {
  if (type == "Visa") {
    // Visa: length 16, prefix 4, dashes optional.
    var re = /^4\d{3}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/;
  } else if (type == "MasterCard") {
    // Mastercard: length 16, prefix 51-55, dashes optional.
    var re = /^5[1-5]\d{2}[- ]?\d{4}-?\d{4}[- ]?\d{4}$/;
  } else if (type == "Discover") {
    // Discover: length 16, prefix 6011, dashes optional.
    var re = /^6011[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/;
  } else if (type == "Amex") {
    // American Express: length 15, prefix 34 or 37.
    var re = /^3[4,7]\d{13}$/;
  }
  if (!re.test(ccnum)) return false;
  // Remove all dashes and spaces
  ccnum = ccnum.split("-").join("");
  ccnum = ccnum.split(" ").join("");
  // Checksum ("Mod 10")
  // Add even digits in even length strings or odd digits in odd length strings.
  var checksum = 0;
  for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
    checksum += parseInt(ccnum.charAt(i-1));
  }
  // Analyze odd digits in even length strings or even digits in odd length strings.
  for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
    var digit = parseInt(ccnum.charAt(i-1)) * 2;
    if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
  }
  if ((checksum % 10) == 0) return true; else return false;
}
function validateTime(fld) {
  var h,m, z, nh;
  var tim = undefined;
  var RegExTime = /^(0?[1-9]|1[0-2]):([0-5]\d)(\s+([ap]m|[AP]M))?$/;
  var timfld=Trim(fld.value);
  if (timfld.match(RegExTime)) {
    h = RegExp.$1;
    m = RegExp.$2;
    z = RegExp.$4.toLowerCase();
    nh = Number(h);
    if (z == 'pm' && nh < 12) {
      nh = nh + 12;
    } else if (z == 'am' && nh == 12) {
      nh = 0;
    } else if (!z && nh < 7) {
      nh = nh + 12;
    }
    if (nh == 24) {nh = 0;}
    h = String(nh);
    if (nh < 10) {h = '0' + h;}
    tim = h + ':' + m + ':00'
    return tim;
  } else {
    return tim;
  }
}
function validateDate(fld) {
  var y,m,d
  var dat = undefined;
  var RegExDate = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
  if (fld.value.match(RegExDate)) {
      RegExDate = /^(\d{1,2})(-|\.|\/)(\d{1,2})(-|\.|\/)(\d{4})$/;
      if (fld.value.match(RegExDate)) {
        y = RegExp.$5;
        m = RegExp.$1;
        d = RegExp.$3;
        if (m.length == 1) {m = '0' + m;}
        if (d.length == 1) {d = '0' + d;}
        dat = y + "-" + m + '-' + d;
        return dat;
      } else {
        return dat;
      }
  } else {
      return dat;
  } 
}
function msgWindow(msg_url) {
  var x = 620;
  var y = 620;
  var msg_attr = "width=" + x + ",height=" + y + ",scrollbars=yes,resizable=yes,status=no,toolbar=no";
  var mw = window.open(msg_url,'msgWin',msg_attr,'true');
//  mw.location.replace(msg_url);
  mw.focus();
}
function New_account() {
  if (!LOADED) {return;}
  var htdoc = SSL + "newacct.php?submit=new";
  window.location.href=htdoc;
}
function New_member(id) {
  if (!LOADED) {return;}
  var htdoc = SSL + "newmemb.php?submit=new&nu_account_id=" + id;
  window.location.href=htdoc;
}
function Edit_account(id) {
  if (!LOADED) {return;}
  var htdoc = SSL + "editacct.php?submit=edit&nu_account_id=" + id;
  window.location.href=htdoc;
}
function Edit_member(aid,mid,rep) {
  if (!LOADED) {return;}
  var htdoc = SSL + "editmemb.php?submit=edit&nu_account_id=" + aid;
  htdoc = htdoc + "&nu_member_id=" + mid;
  if (rep == 'replace')  {
    window.location.replace(htdoc);
  } else {
    window.location.href=htdoc;
  }
}
function Enroll_member(cid,sid,oid,mid,upd) {
  if (!LOADED) {return;}
  if (upd == 'Y') {
    SetMsg('Please update or confirm the T-shirt size for this family member');
    var htdoc = SSL + "enroll.php?submit=getshirt";
    htdoc = htdoc + "&nu_category_id=" + cid;
    htdoc = htdoc + "&rtn_course_id=" + sid;
    htdoc = htdoc + "&nu_offer_id=" + oid;
    htdoc = htdoc + "&nu_member_id=" + mid;
    window.location.href=htdoc;
  } else {
    var htdoc = SSL + "enroll.php?submit=enroll";
    htdoc = htdoc + "&nu_category_id=" + cid;
    htdoc = htdoc + "&rtn_course_id=" + sid;
    htdoc = htdoc + "&nu_offer_id=" + oid;
    htdoc = htdoc + "&nu_member_id=" + mid;
    window.location.href=htdoc;
  }
}
function Help(file) {
  var htdoc = NOSSL + "help/" + file + ".php";
  helpWindow(htdoc);
}
function helpWindow(help_url) {
  var x = 1024;
  var y = 1024;
  var help_attr = "width=" + x + ",height=" + y + ",scrollbars=yes,resizable=yes,status=yes,toolbar=yes";
  var url = help_url;
  var hw = window.open(url,'helpWin',help_attr,'true');
  hw.focus();
}
function Cart(aid) {
  if (!LOADED) {return;}
  var htdoc = SSL + "enroll.php?submit=cart";
  htdoc = htdoc + "&nu_account_id=" + aid;
  window.location.href=htdoc;
}
function Checkout(amt) {
  if (!LOADED) {return;}
  var htdoc = SSL + "checkout.php?submit=getcard";
  htdoc = htdoc + "&nu_amt=" + escape(amt);
  window.location.href=htdoc;
}
function Set_express(amt) {
  if (!LOADED) {return;}
  var htdoc = SSL + "checkout.php?submit=setexpress";
  htdoc = htdoc + "&nu_amt=" + escape(amt);
  window.location.href=htdoc;
}
function List_accounts(order_by,field) {
  if (!LOADED) {return;}
  var htdoc = SSL + "listaccts.php?submit=list";
  htdoc = htdoc + "&order_by=" + order_by;
  htdoc = htdoc + "&nu_field=" + field;
  window.location.href=htdoc;
}
function List_categories() {
  if (!LOADED) {return;}
  var htdoc = NOSSL + "listcats.php?submit=list";
  window.location.href=htdoc;
}
function List_courses() {
  if (!LOADED) {return;}
  var htdoc = NOSSL + "listcourses.php?submit=list";
  window.location.href=htdoc;
}
function List_terms() {
  if (!LOADED) {return;}
  var htdoc = NOSSL + "listterms.php?submit=list";
  window.location.href=htdoc;
}
function Config() {
  if (!LOADED) {return;}
  var htdoc = SSL + "config.php?submit=edit"
  window.location.href=htdoc;
}
function Utils() {
  if (!LOADED) {return;}
  var htdoc = SSL + "utils.php?submit=menu"
  window.location.href=htdoc;
}
function Register_home() {
  var htdoc = NOSSL + "index.php"
  window.location.href=htdoc;
}
function Logout() {
  var htdoc = NOSSL + "login.php?submit=logout"
  window.location.replace(htdoc);
}
