// Usage: <SCRIPT LANGUAGE="JavaScript" SRC="/shared/scripts.js" ></SCRIPT>

var at = "@";
function Scripture (passage) {
    passage = "http://bible.gospelcom.net/bible?" + passage;
    passage = passage.replace(/ /g,"+");
    passage = passage.replace(/:[+]*/g,":");
    win = window.open (
	    passage, "Scripture",
	    "bgcolor=#EFE7D0,scrollbars,menubar,resizable,width=550,height=425");
    WinFocus(win);
}
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; 2001 Christ Church, Summit NJ.  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 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 "";
}
function SelectValue(s) {
    for (i=0;i<s.length;i++){
        if (s[i].selected)
            return s[i].value;
    }
    return "";
}

var holdmailto, listmailto, nchosenmailto, maxallowedmailto;

function mkmailtolist(choice,mailto) {
    if(holdmailto == null) holdmailto  = mailto.href;
//  alert("mkmailtolist: holdmailto = "+holdmailto+"\nchoice.length = "+choice.length);
    listmailto  = "mailto:";
    delim = "";
    nchosenmailto = 0;
    maxallowedmailto = 25;
    for (i=0;i<choice.length;i++){
        if (choice[i].checked) {
	    nchosenmailto++;
	    var s = choice[i].value;
	    s = s.replace(/#/g,"@");
	    s = s.replace(/!/g,".");
	    if(nchosenmailto <= maxallowedmailto)
		listmailto = listmailto+delim+s;
	    delim = ", ";
	}
    }
    if(nchosenmailto == 0)
	listmailto = "mailto:(nobody checked)";
    if(nchosenmailto > maxallowedmailto)
	listmailto = "mailto:(too many checked -- max is "+maxallowedmailto+")";
    if(nchosenmailto == choice.length)
	listmailto = holdmailto;
    return listmailto;
}

function mkmailto(choice,mailto) {
    if(holdmailto == null) holdmailto  = mailto.href;
//  alert("holdmailto = "+holdmailto+"\nchoice.length = "+choice.length);
    mkmailtolist(choice,mailto);
    if(nchosenmailto == 0) {
	alert("Please check one or more email addresses first.");
	return false;
    }
    if(nchosenmailto == choice.length) {
	mailto.href = holdmailto;
	return true;
    }
    if(nchosenmailto > maxallowedmailto) {
	alert("Sorry. You have checked "+nchosenmailto+" boxes, which\nexceeds "+
	      "the maximum allowed of "+maxallowedmailto)
	return false;
    }
    mailto.href = listmailto;
    return true;
}

function clearall(choice) {
    for (var i=0;i<choice.length;i++){
        choice[i].checked = false;
    }
    return false;
}
function checkall(choice) {
    for (var i=0;i<choice.length;i++){
        choice[i].checked = true;
    }
    return false;
}
function countchecks(choice) {
    var n = 0;
    for (var i=0;i<choice.length;i++){
        if(choice[i].checked)
	  n++;
    }
    return n;
}
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+' title=" Send email to '+show+' ">'+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;
    document.write(unescape(s.replace(r,'%$1')));
}
