
function xmlhttpGet(strURL, strResultFunc) 
{
    var xmlHttpReq = false;
        
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
    {
        xmlHttpReq = new XMLHttpRequest();
		if (xmlHttpReq.overrideMimeType)
		{
	        xmlHttpReq.overrideMimeType('text/plain');
		} 
    }
    // IE
    else if (window.ActiveXObject) 
    {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	xmlHttpReq.open('GET', strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() 
    {
        if (xmlHttpReq.readyState == 4) 
        {
            eval(strResultFunc + '(xmlHttpReq);');
        }
    }
    xmlHttpReq.send(null);
	return xmlHttpReq;
}

function getPosition(e)
{
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function ShowDisclaimer()
{	var obj=document.getElementById("wrapper");
	var p=getPosition(obj);
	var dis=document.getElementById("disclaimer");

	dis.style.left=(p.x+300)+"px";
	dis.style.top=(p.y+20)+"px";
	dis.style.display="block";

	return false;
}

function CloseDiv(divName)
{	var div=document.getElementById(divName);

	if (div!=null)
		div.style.display="none";

	return false;
}

function ShowGallery()
{
	xmlhttpGet("gallery.psv","GalleryLoaded");	
	return false;
}

function GalleryLoaded(xmlHttpReq)
{
	var txt=xmlHttpReq.responseText;
	var imagelist=txt.split('\r\n');
	var i,ihtml,galno,col;
	var gal=document.getElementById("content");

	ihtml="<h2>Gallery</h2><table>";
	galno=0;
	col=0;
	for(i=0; i<imagelist.length; i++)
	{
		var cols=imagelist[i].split("|");

		if (cols.length<4) continue;

		if (galno!=parseInt(cols[0]))
		{
			if (galno!=0)
			{
				ihtml+="</div></div></td>";
				col++;
				if (col==2)
				{
					ihtml+="</tr>";
					col=0;
				}
			}
			if (col==0) ihtml+="<tr>";
			ihtml+="<td style='width:50%;'><div class='highslide-gallery'><a id='gallery"+cols[0]+"' href='"+cols[2]+"' class='highslide' onclick='return hs.expand(this, { slideshowGroup: "+cols[0]+" } )'>";
			ihtml+="<img src='"+cols[1]+"' alt='Gallery Image' title='Click to enlarge' /></a>"+cols[3]+"<div class='highslide-caption'>"+cols[3]+"</div><div class='hidden-container'>";
			galno=parseInt(cols[0]);
		}
		else
		{
			ihtml+="<a href='"+cols[2]+"' class='highslide'"; 
			ihtml+=" onclick=\"return hs.expand(this, { thumbnailId: 'gallery"+cols[0]+"', slideshowGroup: "+cols[0]+" })\">";
			ihtml+="<img src='"+cols[1]+"' alt='Gallery Image' title='Click to enlarge' /></a>";
			ihtml+="<div class='highslide-caption'>"+cols[3]+"</div>";
		}
	}

	if (ihtml.length>0)
	{
		gal.innerHTML=ihtml+"</div></div></td></tr></table>";
	}

}

function ShowPage(pageName)
{
	xmlhttpGet(pageName+".htm","PageFetched");	
	return false;
}

function PageFetched(xmlHttpReq)
{	var txt=xmlHttpReq.responseText;
	var cnt=document.getElementById("content");
	
	cnt.innerHTML=txt;
}

function LoadHome()
{
	xmlhttpGet("newsandupdates.htm","NewsFetched");
}

function NewsFetched(xmlHttpReq)
{	var txt=xmlHttpReq.responseText;
	var nws=document.getElementById("left1");

	nws.innerHTML=txt;
	ShowPage('home');
}

function SelectPineYear(obj)
{
	var y=obj.options[obj.selectedIndex].value;

	xmlhttpGet(y+"pine.psv","PineYearFetched");
}

var pineResults=null;

function PineYearFetched(xmlHttpReq)
{	var txt=xmlHttpReq.responseText;
	var pineopt=document.getElementById('pineoption');
	var pinediv=document.getElementById('pineresults');
	
	pineResults=txt.split("\r\n");
	pineopt.selectedIndex=0;
	pineopt.disabled=false;
	pinediv.innerHTML="&nbsp;";

	ShowDenWinners();
}

function SelectPineView(obj)
{	var pinediv=document.getElementById('pineresults');
	if (obj.selectedIndex==0) return;
	var opt=obj.options[obj.selectedIndex].value;

	pinediv.innerHTML=GetPineRace(opt);
}

function GetPineRace(opt)
{	var ihtml;
	var i,j,vals,pos=1;

	if (pineResults==null) return "&nbsp;";

	ihtml=opt+" results:<br><table class=pinetable>";
	vals=pineResults[0].split("|");
	ihtml+="<tr><td class=pinecell>Position</td>";
	for(i=0; i<vals.length; i++)
		ihtml+="<td class=pinecell>"+vals[i]+"</td>";
	ihtml+="</tr>";
	for(j=1; j<pineResults.length; j++)
	{
		vals=pineResults[j].split("|");
		if (vals.length<4) continue;
		if (opt!="Overall Pack")
		{
			if (opt!=vals[2]) continue;
		}
		ihtml+="<tr><td class=pinecell>"+pos+"</td>";
		for(i=0; i<vals.length; i++)
			ihtml+="<td class=pinecell>"+vals[i]+"</td>";
		ihtml+="</tr>";
		pos++;
	}
	ihtml+="</table><br>* = sorted column";

	return ihtml;
}

function ShowDenWinners()
{	var dw=document.getElementById("pinedenwinners");
	var ihtml="<h2>Den Winners</h2>";
	var i,j,vals;
	var dens=["Tiger","Wolf","Bear","Webelos I","Webelos II"];
	ihtml="Den Winners:<br><table class=pinetable><tr><td class=pinecell>Den</td><td class=pinecell>Driver</td><td class=pinecell>Average Time</td></tr>";

	for(i=1; i<pineResults.length; i++)
	{
		vals=pineResults[i].split("|");
		if (vals.length<4) continue;

		for(j=0; j<dens.length; j++)
		{
			if (dens[j]==vals[2])
			{
				dens[j]="xxx";
				ihtml+="<tr><td class=pinecell>"+vals[2]+"</td><td class=pinecell>"+vals[1]+"</td><td class=pinecell>"+vals[4]+"</td></tr>";
				break;
			}
		}
	}
	ihtml+="</table>";
	dw.innerHTML=ihtml;
}
