//
// functions :
//	SetDomainForCookies
//	CreateCookie
//	ReadCookie
//	CreateOptionsCookie
//	ReadOptionsCookie
//	ToggleElementHiddenState
//	NewWindow
//	InitialiseFontSize
//	SelectFontSize
//	gsearch
//	ImageFS
//	MM_swapImgRestore
//	MM_preloadImages
//	MM_findObj
//	MM_swapImage
//
var sDomain; // for cookies
var popup = null;
var aa=screen.availHeight-220;
var oLargeSizeOmnilink;
var oSmallSizeOmnilink;
var oMediumSizeOmnilink;
function SetDomainForCookies(DomainName) {
   sDomain = DomainName
}
function CreateCookie(sName, sValue, iDays) {
	if (iDays) {
		var oDate = new Date();	
		oDate.setTime(oDate.getTime()+(iDays*24*60*60*1000));
		var sExpires = '; expires=' + oDate.toGMTString();		
	}
	else var sExpires = '';	
	document.cookie = sName + '=' + sValue + sExpires + '; path=/; domain=' + sDomain;	
}
function ReadCookie(sName) {
	var sNameEQ = sName + '=';
	var oCookieArray = document.cookie.split(';');
	for (var iIndex=0; iIndex < oCookieArray.length; iIndex++) {
		var sCookieTerm = oCookieArray[iIndex];
		while (sCookieTerm.charAt(0)==' ') sCookieTerm = sCookieTerm.substring(1, sCookieTerm.length);
		if (sCookieTerm.indexOf(sNameEQ) == 0) return sCookieTerm.substring(sNameEQ.length, sCookieTerm.length);
	}
	return null;
}
function CreateOptionsCookie(sName, sValue) {
	var sNameEQ = sName + '=';
	var iTermIndex = -1;
	// Get the whole cookie which contains all the options settings in one.
	var sWholeCookie = ReadCookie('Options');
	// The options are delimited as termx=valx]termy=valy] etc using "]" as the delimiter so split..
	var oWholeCookieArray;
	if (sWholeCookie) {
		oWholeCookieArray = sWholeCookie.split(']');
	} else{
		oWholeCookieArray = new Array();
	}
	// Find out if the option already exists in the whole cookie, loop through it
	for (var iIndex=0; iIndex < oWholeCookieArray.length; iIndex++) {
		// Pick out the term
		var sWholeCookieTerm = oWholeCookieArray[iIndex];
		// split off any leading spaces (might not be required?)
		while (sWholeCookieTerm.charAt(0)==' ') sWholeCookieTerm = sWholeCookieTerms.substring(1, sWholeCookieTerm.length);
		// is this our term? if so note its position in the array
		if (sWholeCookieTerm.indexOf(sNameEQ) == 0) iTermIndex = iIndex;
	}
	// Now we have the index of the term, or -1 if it doesn't exist already
	// If it doesn't exist we will need to add it at the end of the array
	if (iTermIndex==-1) iTermIndex=oWholeCookieArray.length;
	// Now set our value
	oWholeCookieArray[iTermIndex] = sNameEQ + sValue;
	// Now prepare the options cookie for writing out
	var sOptionsCookie = '';
	// Note the length so we don't have to keep requesting it in the loop below
	var iWholeCookieArrayLength = oWholeCookieArray.length;
	for (var iIndex=0; iIndex < iWholeCookieArrayLength; iIndex++) {
		// Pick out the term
		var sOptionsCookieTerm = oWholeCookieArray[iIndex];
		// Add it to the string
		sOptionsCookie = sOptionsCookie + sOptionsCookieTerm;
		// Add the delimiter unless this is the last one
		if (iIndex != (iWholeCookieArrayLength-1)) sOptionsCookie = sOptionsCookie + ']';
	}
	CreateCookie('Options',sOptionsCookie,7);
}
function ReadOptionsCookie(sName) {
	var sNameEQ = sName + '=';
	var sWholeCookie = ReadCookie('Options');
	// The options are delimited as termx=valx]termy=valy] etc using "]" as the delimiter so split..
	var oWholeCookieArray;
	if (sWholeCookie) {
		oWholeCookieArray = sWholeCookie.split(']');
	} else{
		oWholeCookieArray = new Array();
	}
	for (var iIndex=0; iIndex < oWholeCookieArray.length; iIndex++) {
		var sWholeCookieTerm = oWholeCookieArray[iIndex];
		while (sWholeCookieTerm.charAt(0)==' ') sWholeCookieTerm = sWholeCookieTerms.substring(1, sWholeCookieTerm.length);
		if (sWholeCookieTerm.indexOf(sNameEQ) ==0) return sWholeCookieTerm.substring(sNameEQ.length, sWholeCookieTerm.length);
	}
	return null;
}
// Toggles the hidden state of a DOM element specified by its ID
// If you want it to persist in a cookie then give a cookie name and add true
function ToggleElementHiddenState(sElementId, sCookieName, bPersist) {
	var oElementNode=document.getElementById(sElementId);
	if (oElementNode.style.display=='none') {
		oElementNode.style.display='';
		if (bPersist==true) {
			CreateOptionsCookie(sCookieName, 'visible');
		};
	} else {
		oElementNode.style.display='none';
		if (bPersist==true) {
			CreateOptionsCookie(sCookieName, 'hidden');
		};
	};
}
function NewWindow(url)
{
  var bb=BODY1.clientWidth-109; // screen.availWidth*0.8-12 = 807
  var cc=screen.width-bb-10; 
  popup = window.open(url,"small","toolbar=1,top=90,width="+bb+",height="+aa+",scrollbars=1,resizable=1,left="+cc);
  if(popup.opener == null)
  {
  popup.opener = popup;
  popup.focus();
  }
  return false;
}
function InitialiseFontSize() {
    oLargeSizeOmnilink  = document.getElementById('LargeSizeOmnilink');
    oSmallSizeOmnilink  = document.getElementById('SmallSizeOmnilink');
    oMediumSizeOmnilink = document.getElementById('MediumSizeOmnilink');
    
    // Provide the event handlers for the font sizing omnilinks
    if (oLargeSizeOmnilink) {
        oLargeSizeOmnilink.onclick=function() {
            SelectFontSize('Large');
            return false;
        };
    };
    if (oSmallSizeOmnilink) {
        oSmallSizeOmnilink.onclick=function() {
            SelectFontSize('Small');
            return false;
        };
    };
    if (oMediumSizeOmnilink) {
        oMediumSizeOmnilink.onclick=function() {
            SelectFontSize('Medium');
            return false;
        };
    };
    
    // Apply any stored font size value
	var sFontSize = ReadOptionsCookie('FontSize');
	if (sFontSize) {
        SelectFontSize(sFontSize);
	};    

};

function SelectFontSize(sFontSize) {
    /*SetActiveStylesheet(sFontSize, null);*/
    document.getElementById('bodydiv').className=sFontSize + 'FontSize';
    CreateOptionsCookie('FontSize',sFontSize);
};
function gsearch(searchtext) {
    document.url='http://www.google.com/search?q=' + searchtext + '+site:hertfordtownchurch.org';
    document.getElementById('gsearch').href=document.url;
};
function ImageFS(pic,w,h) {
	mw = 712;
	mw = w+48;
	mh = 540;
	mh = h+80;
	slaveW = window.open(pic,'swin2',"width="+mw+",height="+mh+",resizable=1");
	if (slaveW==null) alert('your popup stopper prevented the picture from appearing')
	else {
	slaveW.resizeTo(mw,mh);
	slaveW.focus();
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.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); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function unhide(sElid) {
 document.getElementById(sElid).style.display='';
}
// this preload is a waste of time
MM_preloadImages('home_r4_c2.gif','Hertford.gif','header_r2_c1.gif','header_r2_c7.gif','but_about.gif','but_meetings.gif','but_kids.gif','but_members.gif','but_contact.gif');

// stop IE6 flicker - nb should really avoid background images on <a>
// nb still flickers when IE settings check pages 'every visit to the page'
try {
	document.execCommand("BackgroundImageCache", false, fix);
} catch(err) { }
