// BROWSER & OS DETECTION
function envDetect() {
  agt = navigator.userAgent.toLowerCase();
  is_mac = (agt.indexOf("mac") != -1);
  is_major = parseInt(navigator.appVersion);
  msie_vers_start = agt.indexOf("msie") +5;
  msie_real_vers = parseFloat(agt.substring(msie_vers_start, msie_vers_start +3));
  is_linux = (agt.indexOf("inux") != -1);
  is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && (agt.indexOf('webtv') == -1));
  is_nav6 = (is_nav && (agt.indexOf('netscape6') != -1));
  is_nav4up = (is_nav && (is_major >= 4));
  is_mozilla = (is_nav && (agt.indexOf('netscape') == -1) && (agt.indexOf('gecko') != -1));
  is_ie = (agt.indexOf("msie") != -1);
  is_ie3 = (is_ie && (is_major < 4));
  is_ie4 = (is_ie && (is_major == 4) && (msie_real_vers < 5));
  is_ie4up = (is_ie  && (is_major >= 4));
  is_ie5up = (is_ie  && !is_ie3 && !is_ie4);
  
  browserName = is_nav6 ? "ns6" : is_linux ? "linux" : (is_nav4up && !is_mac) ? "ns4pc" : is_mozilla ? "moz" : (is_nav4up && is_mac) ? "ns4mac" : (is_ie5up && is_mac) ? "ie5mac" : (is_ie4up && is_mac) ? "ie4mac" : "generic";
  }

// GENERIC CUSTOM POP-UP WINDOW
function newWindow(winURL, winName, width, height, msg) {
	options = 'height=' + height + ',width=' + width + ',scrollbars=no,resizable=no';
    newWin = window.open(winURL, winName, options);
	newWin.document.write("<HTML><HEAD><TITLE>H R K C &nbsp; o n l i n e</TITLE></HEAD><BODY BGCOLOR='#7B0000' LINK='#FFFFCC' ALINK='#FFFFCC' VLINK='#FFFFCC' TOPMARGIN='0' LEFTMARGIN='0' MARGINWIDTH='0' MARGINHEIGHT='0'><SPAN STYLE='COLOR: #FFFFFF; FONT-FAMILY: Verdana, Arial, Helvetica, Sans-Serif; LINE-HEIGHT: 10pt; FONT-SIZE: 7.5pt'>");
    newWin.document.write(msg);
	newWin.document.close();
    newWin.focus();
}

// GENERIC FORM VALIDATION
function validForm(theForm) {
  for(var intCtr = 0; intCtr < (theForm.elements.length); ++intCtr) {
 	var temp = theForm.elements[intCtr];
	if(temp.type == "text" && temp.value == "") {
//	  temp.focus();
	  return false;
	}
	if(temp.tagName == "TEXTAREA" && temp.value == "") {
	  return false;
	}
    if(temp.type == "checkbox" && temp.name == "agree") {
      if (temp.checked == true) {
        return true;
      } else {
      return false;
      }
    }
  }
  return true;
}

// GENERIC EMAIL VALIDATION
function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// VALIDATES AND PROCESSES CONTACT US FORM
function sendMail(theForm) {
  data = theForm.from.value;

  if (!validForm(theForm) || !isEmail(data) || theForm.body.value == "" || theForm.inquiry.value == "none") {
    alert("Make sure all the relevant fields are filled in.\n\nYou must select a specific inquiry type and provide a valid email address.");
    return false;
  } else {
    return true;
  }
}

// VALIDATES SHORYUHAI FORM
function registerMe(theForm) {
  if (!validForm(theForm)) {
    alert("Make sure all the relevant fields are filled in!");
    return false;
  } else {
    return true;
  }
}

// VALIDATES AND PROCESSES EMAIL SUBSCRIPTION FORM
function subscribeMe(theForm) {
  data = theForm.email.value;

  // ARE THEY .HARVARD.EDU ?
  if (!isEmail(data) || data.indexOf("harvard.edu") == -1) {
    alert("Sorry, but HRKC membership - including that on email lists - is restricted\nto Harvard University affilliates.\n\nIf you are a Harvard affilliate but wish to subscribe an email address\nother than your harvard.edu address, please email us directly\nat kendo@hcs.harvard.edu");
    return false;
  } else {
    alert("Thank you for subscribing to the HRKC email list.\n\nYour subscription has been sent to our web administrator\nand is awaiting approval.\n\nOnce authorized, you will be able to receive list-wide emails.");
    return true;
  }
}
