var at   = "@";
var hide = "none";
var show = "display";

function Obj(id) {
// defines function Obj()
    return document.getElementById(id);
}
function PopUp (URL,width,height) {
    if(width  == null) { width  = "500" }
    if(height == null) { height = .6*width }
    win = window.open (
	    URL, "PopUp",
	    "scrollbars,menubar,resizable," + 
	    "width=" + width + ",height=" + height );
    WinFocus(win);
}
function Help(content,width,height) {
    if(width  == null) { width  = "500" }
    if(height == null) { height = .6*width }
    iwin = window.open (
            "", "Help",
            "scrollbars,menubar,resizable," +
            "width=" + width + ",height=" + height );
    doc = iwin.document;
    doc.writeln('<html>\n<head>\n<title>Help</title>\n' +
//        '<SCRIPT LANGUAGE="JavaScript" SRC="scripts.js" ></SCRIPT>\n' +
//        '<LINK REL=StyleSheet TYPE="text/css" HREF="styles.css" >\n' +
	'<style>\n' +
	'pp, .pp { font-size:9pt; font-family: arial, sans-serif;\n' +
	'color: #002000; margin-top:6pt; margin-bottom:.0001pt; }\n' +
	'</style>\n' +
	'</head>\n<body bgcolor="#EFE7D0" peachpuff class=pp>\n' + content +
	'\n<hr><form><center>' +
	'<input type=button onclick="window.close()" value="&nbsp;ok&nbsp;">' +
	'</center></form>\n</body>\n</html>');
    doc.close();
    WinFocus(iwin);
}
function WinFocus(win) {
    // assume win.focus works, unless broken old msoft browser
    if ( (navigator.appName.search (/^microsoft/i) == -1) ||
         (navigator.appName == "Microsoft Internet Explorer" &&
          navigator.appVersion.indexOf("Windows", 0) != -1   &&
          navigator.appVersion.substring(0,1) >= 4) ) {
		win.focus()
    }
}
function FinePrint(pixdir) {
    if(pixdir == null) { pixdir = "pix" }
    document.write( 
      '<hr>\n'+
      '<a href="#_top">'+
      '<img src="' + pixdir + '/ptr-up.gif" border=0 width=16 height=9></a>\n'+
      '<br><span style="font-family:arial narrow,helvetica narrow,'+
      'sans-serif;font-size:9pt;color:#889888;'+
      'margin-top:0pt;margin-bottom:.0001pt">\n'
    );
    TimeStamp();
    document.write( 
      '<br>\n'+
      'Copyright &copy; 2006 North Jersey Chapter of the American Society. All rights reserved'+
      '</span>\n'+
      '</td></tr></table></body></html>\n'
    );
    document.close();
}
function TimeStamp(pfx) {
    if(pfx == null) { pfx = "Page last modified: " }
    Date = " " + document.lastModified;
    Date = Date.replace(/([A-Z][a-z]{2})[a-z]*/g,"$1");
    Date = Date.replace(/:\d\d($|[^\d:])/g,"$1");
    Date = Date.replace(/\//g," / ");
    Date = Date.replace(/ 0/g," ");
    Date = Date.replace(/ ([0-9]+:)/," &nbsp;-&nbsp; $1");
    document.write("<nobr>" + pfx + Date + "</nobr>\n");
}

var clickCount = 1;
function PreviousPage1() {
//     alert("clickCount =" + clickCount); // debug
    alert("clickCount = "+clickCount);
    if (clickCount > 1) history.go(-clickCount); else history.back();
    // Usage: <a href="#xxx" onClick="cc()">xxx</a> etc. ...
    //        [ <a href="JavaScript: PreviousPage()">back</a> ]
}
function PreviousPage() {
//     alert("clickCount =" + clickCount); // debug
    if (clickCount > 1) history.go(-clickCount); else history.back();
    // Usage: <a href="#xxx" onClick="cc()">xxx</a> etc. ...
    //        [ <a href="JavaScript: PreviousPage()">back</a> ]
}
function cc() {
    clickCount++;
}
function RadioValue(r) {
    for (i=0;i<r.length;i++){
        if (r[i].checked)
            return r[i].value;
    }
    return "";
}

var holdmailto, listmailto, nchosenmailto, maxallowedmailto;

function clearall(choice) {
    for (i=0;i<choice.length;i++){
        choice[i].checked = false;
    }
    return false;
}
function checkall(choice) {
    for (i=0;i<choice.length;i++){
        choice[i].checked = true;
    }
    return false;
}
function mto(who,where,dom,show,opt,ux) {
    if(opt  == null) opt  = "";
    if(dom  == null) dom  = "";
    if(show == null) show = "";
    if(ux   == null) ux   = "";
    if(dom.match(/^(pbz|bet|rqh|arg|tbi|hf)$/)) {
	dom   = rt(dom);
	who   = rt(who);
	where = rt(where);
	show  = rt(show);
	ux    = rt(ux);
    }
    if(dom != ""   ) dom = "."+dom;
    var em = who+"@"+where+dom;
    if(show == ""  ) show = em;
    if(opt != ""   ) opt = " "+opt;
//    ux = "(ux:"+ux+")"; // DEBUG
    var s= '<'+'a hr'+'ef="mai'+'lto'+':'+em+ux+'"'+opt+'>'+show+'</a>';
    document.write(s);
//    alert(s);
}
var abc = "abcdefghijklm@.- nopqrstuvwxyz#:=,".split("");
var nbc = new Object();
for(var i=0; i<34; i++) nbc[abc[i]] = i;
function rt(s) {
    var y = "";
    for(var i=0; i<s.length; i++) {
        var C = s.substr(i,1);
        var c = C.toLowerCase();
        var x = abc[(nbc[c]+17)%34];
        if(c != C) x = x.toUpperCase();
        if(x != "" && x != null) y += x
        else  y += C;
    }
    return y;
}
function sb(s){
var r=/\[([0-9A-F]{2})\]/g;s=s.replace(r,'%$1');
document.write(unescape(s))}

function TimeStamp(pfx) {    
// or just use:  document.lastModified !
    if(pfx == null) { pfx = "<nobr>Last modified:</nobr>" }
    document.write(pfx+" <nobr>" + document.lastModified + "</nobr>\n");
//    Date = " " + document.lastModified;
//    Date = Date.replace(/([A-Z][a-z]{2})[a-z]*/g,"$1");
//    Date = Date.replace(/:\d\d($|[^\d:])/g,"$1");
//    Date = Date.replace(/\//g,"-");
////    Date = Date.replace(/ 0/g," ");
//    Date = Date.replace(/ ([0-9]+:)/,", $1");
//    document.write(pfx+" <nobr>" + Date + "</nobr>\n");
}
var showing=0 ;
function toggle(id,onlyone,force) {
    var el = Obj(id);
    if(el == null)
	return;
    if(el.style == null)
	return;
    var oldval = el.style.display+"";
    var newval = oldval.match(/none|^$/)? "inline" : "none";
    if(oldval == '') newval="inline";
    if(force == 0) newval = "none";
    if(force == 1) newval = "inline";
//    alert("Toggling "+id+" from \""+oldval+"\" to \""+newval+"\""); // DEBUG
    el.style.display = newval;
//    alert("toggle: id="+id+"name="+el.name+" oldval="+oldval+" newval="+newval); return false;
    if(onlyone==1 && newval=="inline" && el != showing) {
	if(showing.style) {
	  showing.style.display = "none";
	  showing = 0;
	}
	if(newval=="inline")
	  showing = el;    
    }
}
function toggleX(id,onlyone,force) {
    var ob = Obj(id);
    if(ob == null)
	return;
    if(ob.style == null)
	return;
    var oldval = ob.style.display+"";
    var newval = oldval.match(/none|^$/)? "inline" : "none";
    if(oldval == '') newval="inline";
    if(force == 0) newval = "none";
    if(force == 1) newval = "inline";
//    alert("Toggling "+id+" from \""+oldval+"\" to \""+newval+"\""); // DEBUG
    ob.style.display = newval;
//    alert("toggleXX: id="+id+" name="+ob.name+" oldval="+oldval+" newval="+newval); return false;
    if(onlyone==1 && newval=="inline" && ob != showing) {
	if(showing.style) {
	  showing.style.display = "none";
	  showing = 0;
	}
	if(newval=="inline")
	  showing = ob;    
    }
}

function showprops(el) {
    var s = "";
    var n;
    var i=0;
    for(n in el) {
	var v = el[n]+"";
	var nv = n+"="+v;
	if(!v.match(/function|[A-Z][A-Z][A-Z]/)) {
  	  if(1 || !n.match(/[A-Z][A-Z][A-Z]/)) {
	    if(1 || nv.match(/color/i)) {
	      if(v != "") {
		if((++i)%2 == 0)
		  s += "\n"; 
		else
		  s += ", "; 
		s += n+"='"+v+"'";
	      }
	    }
	  }
	}
    }
    alert("Properties: "+s);
}

function MM_findObj(n, d) { //v4.0
    var p,i,x;
    if(!d)
	d=document;
    if( (p=n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p+1)].document;
        n = n.substring(0,p);
    }
    if(!(x=d[n]) && d.all)
	x = d.all[n];
    for (i=0; !x && i<d.forms.length; i++)
	x = d.forms[i][n];
    for(i=0; !x && d.layers && i<d.layers.length; i++)
	x = MM_findObj(n, d.layers[i].document);
    if(!x && document.getElementById)
	x = document.getElementById(n);
    return x;
}

function MM_validateFormY(args0) { //v4.0
  var i,p,q,id,nm,nm1,val,rule,def,num,min,fld,max;
  var errors = '';
  var args=MM_validateFormY.arguments;
//  alert("MM_ValidateFormY:  argcount ="+args.length); //DEBUG
  for (i=0; i<(args.length-2); i+=3) {
    id   = args[i];
//    fld  = MM_findObj(args[i]);
    fld  = Obj(args[i]);   // Works much better!!
    def  = args[i+1];
    rule = args[i+2];
//    alert("id=("+id+")  rule=("+rule+")  fld=("+fld+")");
    if (fld) {
      nm  = fld.name+"";
      // Why doesn't the above pick up the name???  It used to work!
      //if(!nm || nm==null || nm==undefined || nm=='' || !nm.match(/[a-z]/i))
	  nm = id.replace(/_[0-9]*$/,"");
//      alert("A. MM_validateFormY: id=("+id+") name=("+nm+")  rule=("+rule+")"); //DEBUG
      nm1 = '\n'+nm.replace(/^[0-9 ]*/,"");
      val = fld.value+"";
      if(val.match(/Select One|No Selection/i))
	  val = "";
      if (val!="") {
	if (rule.indexOf('isEmail')>-1) {
	  p = val.indexOf('@');
	  if (p<1 || p==(val.length-1))
	    errors += nm1+' must contain an e-mail address.\n';
	} else
	if (rule.indexOf('Num')>-1) {
	  if (isNaN(val)) {
	    errors += nm1+' must contain a number.\n';
	  } else
	  if (rule.indexOf('inRange') != -1) {
	    p=rule.indexOf(':');
	    min=rule.substring(8,p); max=rule.substring(p+1);
	    if (val<min || max<val)
	      errors += nm1+' must contain a number between '+min+' and '+max+'.\n';
	  }
	}
      } else
	if (rule.charAt(0) == 'R')
	  errors += nm1+' is required.\n';
    }
  }
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
    return false;
  }
  else {
    for (i=0; i<(args.length-2); i+=3) {
      fld  = MM_findObj(args[i]);
      def  = args[i+1];
      if(fld) {
	if(fld.value+'' == '' && def != null)
	    fld.value = def;
      }
    }
    return true;
  }
}
function MM_validateForm(args0) { //v4.0
  var i,p,q,nm,nm1,val,test,def,num,min,fld,max,errors='';
  var args=MM_validateForm.arguments;
//  alert("MM_ValidateForm:  #args="+args.length); //DEBUG
  for (i=0; i<(args.length-2); i+=3) {
    fld  = MM_findObj(args[i]);
    def  = args[i+1];
    test = args[i+2];
//    alert("DEBUG: Testing fld = '"+fld.name+"  test = "+test); //DEBUG
    if (fld) {
      nm  = fld.name;
      nm1 = '\n'+nm.replace(/^[0-9 ]*/,"");
      val = fld.value;
      if (val!="") {
	if (test.indexOf('isEmail')>-1) {
	  p=val.indexOf('@');
	  if (p<1 || p==(val.length-1))
	    errors += nm1+' must contain an e-mail address.\n';
	} else
	if (test.indexOf('Num')>-1) {
	  if (isNaN(val)) {
	    errors += nm1+' must contain a number.\n';
	  }
	  if (test.indexOf('inRange') != -1) {
	    p=test.indexOf(':');
	    min=test.substring(8,p); max=test.substring(p+1);
	    if (val<min || max<val)
	      errors += nm1+' must contain a number between '+min+' and '+max+'.\n';
	  }
	}
      } else
	if (test.charAt(0) == 'R')
	  errors += nm1+' is required.\n';
    }
  }
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
    return false;
  }
  else {
    for (i=0; i<(args.length-2); i+=3) {
      fld  = MM_findObj(args[i]);
      def  = args[i+1];
      if (fld) {
	if(fld.value == '' && def != null)
	    fld.value = def;
      }
    }
    return true;
  }
}

function Trim(s) {
    s = s+"";
    return s.replace(/  +/g," ").replace(/^ | $/g,"");
}
function fixzip(zip) {
    zip += "";
    return zip.replace(/[^0-9]/g,'').replace(/^([0-9]{5})([0-9]{1,4}).*/,'$1-$2');
}
function fixnum(num) {
    num += "";
    return num.replace(/[^0-9]/g,'');
}
function fixphone(phone) {
    phone += "";
    if(phone.match(/^[0-9]{3}[\-.][A-Z]{3}[\-.][A-Z]{4}$/i))
	return phone;
    return phone.
	replace(/ext(ension\.)?/i,"x").
	replace(/[^0-9x]/ig,"").
	replace(/^([0-9]{3})/,"$1-").
	replace(/^([0-9]{3}-[0-9]{3})/,"$1-").
	replace(/^([0-9]{3}-[0-9]{3}-[0-9]{4})([0-9])/,"$1x$2").
	replace(/[^0-9]*$/,"");
}
function tc(str) {
    str += "";
//    if(str.match(/[a-z]/) && str.match(/[A-Z]/)) return str;
    str = str.replace(/^ +| +$/g,"");
    str = tc0(str," ");
    str = tc0(str,"'");
    str = tc0(str,'"');
    str = tc0(str,'-');
    str = str.replace(/Sanofi/gi,"sanofi");
    str = str.replace(/Aventis/gi,"aventis");
    return str;
}
function tc0(str,sep) {
    var w = str.split(sep);
    // this probably isn't a complete list of words that shouldn't be capitalized
    var little_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'for', 'with',
				 'at', 'an', 'from', 'by', 'if', 'of');
    for (i=0;i<w.length;i++) {
	var ww = w[i]+"";
	if (i == 0) {
	    //always capitalize the first word
	    w[i] = (ww.substring(0,1)).toUpperCase() + ww.replace(/./,"");
	}
	else {
	    var hit=0;
	    for(j=0;j<=little_words.length;j++) {
		if(little_words[j] == ww) hit++;
	    }
	    if(!hit)
		w[i] = (ww.substring(0,1)).toUpperCase() + ww.replace(/./,"");
	}
    }
    return w.join(sep);
}
