var page;
var state;
var d=3000; //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var ms=1; //Specify marquee scroll speed (larger is faster 1-10)
var p=1; //Pause marquee onMousever (0=no. 1=yes)?
var cs=ms;
var ps=(p==0) ? cs : 0;
var ah='';

// Initialize Marquee //
if (window.addEventListener) { window.addEventListener('load',InitializeMarquee,false); }
	else if (window.attachEvent) { window.attachEvent('onload',InitializeMarquee); }
	else if (document.getElementById) { window.onload=InitializeMarquee; }

function $(i) { return document.getElementById(i); }

function ToggleDisplay(i,l) {
	var o = $(i);
	var d = o.style.display;
	if (d == 'none' || d == '') {
		o.style.display = 'block';
		l.style.display = 'none';
	} else {
		o.style.display = 'none';
		l.style.display = 'block';
	}
	return false;
}

function ToggleGallery() {
	var gs = $('submenu-gallery');
	var d = gs.style.display;
	if (d == 'none' || d == '') {
		gs.style.display = 'block';
		$('menu-gallery').style.backgroundImage = 'url(/layout/images/bullet-down.jpg)';
	} else {
		gs.style.display = 'none';
		$('menu-gallery').style.backgroundImage = 'url(/layout/images/bullet.jpg)';
	}
	return false;
}

function ViewImage(i,w,h) {
	$('cntBlack').onclick = Function('CloseContainer(\'image\',true);');
	$('cntBlack').style.display = 'block';
	$('cntLoading').style.display = 'block';
	var oc = $('cntImage');
	var o = $('imgContent');
	oc.style.width = w+'px';
	oc.style.height = (Number(h)+25)+'px';
	o.innerHTML = '<img id="imgHandle" src="'+i+'" style="width: '+w+'px; height: '+h+'px;" onload="CloseContainer(\'loading\',false);" />';
	oc.style.top = ((Window.getWindowHeight()/2)) +'px';
	oc.style.left = ((Window.getWindowWidth()/2)) +'px';
	oc.style.marginTop = '-'+((Number(h)+25)/2)+'px';
	oc.style.marginLeft = '-'+(w/2)+'px';
	$('cntBlack').style.display = 'block';
	oc.style.display = 'block';
	var theHandle = $('imgHandle');
	var theRoot = $('cntImage');
	Drag.init(theHandle, theRoot);
	return false;
}

function Calendar(y,m) {
	$('Calendar').innerHTML = '<img src="/layout/images/loadingAnimation.gif" style="margin: 60px 0 0 135px; border: 3px solid #847A67;" />';
	var p = 'page=CALENDAR&year='+y+'&month='+m;
	page = 'CALENDAR';
	AJAX('/layout/process.php',p);
}

function ShowDetails(i) {
	$('cntBlack').onclick = Function('CloseContainer(\'details\',true);');
	$('cntBlack').style.display = 'block';
	$('cntLoading').style.display = 'block';
	var p = 'page=EVENTDETAILS&id='+i;
	page = 'EVENTDETAILS';
	//state = 'window.location = \'/shopping-cart/\';';
	AJAX('/layout/process.php',p);
}

function CloseContainer(c,b) {
	if (b) {
		$('cntBlack').style.display = 'none';
		$('cntLoading').style.display = 'none';
	}
	switch (c) {
		case 'pdf':	$('cntPDF').style.display = 'none'; break;
		case 'details':	$('cntDetails').style.display = 'none'; break;
		case 'image':	$('cntImage').style.display = 'none'; break;
		case 'loading':	$('cntLoading').style.display = 'none'; break;
		case 'login':	$('cntLogin').style.display = 'none'; break;
		default:
			$('cntBlack').style.display = 'none';
			$('cntLogin').style.display = 'none';
			break;
	}
}

function CloseStatus() {
	try {
		$('FormError').style.display = 'none';
		$('FormSuccess').style.display = 'none';
	} catch (e) {}
}

function Popup(url,wn) {
	var w = screen.width;
	var h = screen.height;
	var l = (screen.width  - w)/2;
	var t = (screen.height - h)/2;
	var p = 'width='+w+', height='+h;
	p += ', top='+t+', left='+l;
	p += ', directories=yes';
	p += ', location=yes';
	p += ', menubar=yes';
	p += ', resizable=yes';
	p += ', scrollbars=yes';
	p += ', status=yes';
	p += ', toolbar=yes';
	newwin=window.open(url,wn,p);
	if (window.focus) {newwin.focus()}
	return false;
}

function TextCounter(f,c,ml) {
	var l = $(f).value.length;
	if (l > ml) { $(f).value = $(f).value.substring(0, ml); }
		else { $(c).innerHTML = ml-l; }
}

function SubmitForm(frm) {
	var f = $('frm'+frm);
	var b = '';
	switch (frm) {
		case 'ContactUs':
			var i = f.info.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var n = f.name.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var e = f.email.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var ve = ValidateEmail(e);
			var s = f.subject.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var c = f.category.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var m = f.message.value.replace(/^\s*/, "").replace(/\s*$/, "");
			if (n == '') { b += '<li>Please Provide A Name!</li>'; }
			if (e == '') { b += '<li>Please Enter An Email Address!</li>'; }
				else if (ve == false) { b += '<li>Invalid Email Address!</li>'; }
			if (s == '') { b += '<li>Please Provide A Subject!</li>'; }
			if (m == '') { b += '<li>Please Create Message Content!</li>'; }
				else if (m.length > 500) { b += '<li>Message Content Exceeds The 500 Character Limit!</li>'; }
			break;
		case 'RegSoftball':
			var l = f.league.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var n = f.name.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var e = f.email.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var ev = ValidateEmail(e);
			var a = f.address.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var c = f.city.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var s = f.state.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var z = f.zipcode.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var p = f.phone.value.replace(/^\s*/, "").replace(/\s*$/, "");
			if (n=='') { b += '<li>Please Provide Your Full Name!</li>'; }
			if (e=='') { b += '<li>Please Enter An Email Address!</li>'; }
				else if (ev==false) { b += '<li>Invalid Email Address!</li>'; }
			if (a=='' || c=='' || s=='' || z=='') { b += '<li>Please Enter Your Mailing Address!</li>'; }
			if (p=='') { b += '<li>Please Provide Your Phone Number!</li>'; }
			break;
		case 'impact':
			var classes = getCheckedValue(path.classes);
			var name = path.name.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var email = path.email.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var e_verify = emailValidation(email);
			var phone = path.phone.value.replace(/^\s*/, "").replace(/\s*$/, "");
			if (classes == '') { buffer += '<li>Please Select A Class!</li>'; }
			if (name == '') { buffer += '<li>Please Provide Your Full Name!</li>'; }
			if (email != '' && e_verify == false) { buffer += '<li>Invalid Email Address!</li>'; }
			if (phone == '') { buffer += '<li>Please Provide Your Phone Number!</li>'; }
			break;
		case 'RegBanquet':
			var n = f.name.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var e = f.email.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var ev = ValidateEmail(e);
			var a = f.address.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var c = f.city.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var s = f.state.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var z = f.zipcode.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var p = f.phone.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var hc = f.church.value.replace(/^\s*/, "").replace(/\s*$/, "");
			var x = 0;
			if (n=='') { b += '<li>Please Provide Your Full Name!</li>'; }
			if (e=='') { b += '<li>Please Enter An Email Address!</li>'; }
				else if (ev==false) { b += '<li>Invalid Email Address!</li>'; }
			if (a=='' || c=='' || s=='' || z=='') { b += '<li>Please Enter Your Mailing Address!</li>'; }
			if (p=='') { b += '<li>Please Provide Your Phone Number!</li>'; }
			if (hc=='') { b += '<li>Please Provide Your Home Church!</li>'; }
			if (f.session_1.checked){x++;}
			if (f.session_2.checked){x++;}
			if (f.session_3.checked){x++;}
			if (f.session_4.checked){x++;}
			if (x>2) { b += '<li>Please Check Only 2 Sessions!</li>'; }
				else if (x<2) { b += '<li>Please Check 2 Sessions!</li>'; }
			break;
		default:
			break;
	}
	if (b == '') {
		return true;
	} else {
		$('FormErrorList').innerHTML = b;
		$('FormError').style.display = 'block';
		return false;
	}
}

function ValidateEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) { return false; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return false; }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return false; }
	if (str.indexOf(at,(lat+1))!=-1) { return false; }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false; }
	if (str.indexOf(dot,(lat+2))==-1){ return false; }
	if (str.indexOf(" ")!=-1){ return false; }
	return true;				
}

function ValidateSession(f,c,t,m){
	if (c.checked) {
		z=0;
		for (i=1;i<=t;i++) {
			if (f.elements['session_'+i].checked) { z++; }
			if (z>m) { return false; }
		}
	}
	return true;
}

function AJAX(url,p) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; }
	xmlHttp.onreadystatechange=StateChanged;
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(p);
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
}

function StateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		switch(page) {
			case 'EVENTDETAILS':
				if (xmlHttp.responseText) {
					$('cntDetails').innerHTML = xmlHttp.responseText;
					$('cntDetails').style.display = 'block';
					CloseContainer('loading',false);
				} break;
			case 'CALENDAR':
				if (xmlHttp.responseText) {
					$('Calendar').innerHTML = xmlHttp.responseText;
				} break;
			case 'SCREMOVE':	eval(state); break;
			default:			break;
		}
	}
}

function ScrollMarquee() {
	if (parseInt(cm.style.top)>(ah*(-1)+8)) { cm.style.top=parseInt(cm.style.top)-cs+'px'; }
		else { cm.style.top=parseInt(mh)+8+'px'; }
}

function InitializeMarquee() {
	cm=$('vmarquee');
	cm.style.top=0;
	mh=$('MarqueeContainer').offsetHeight;
	ah=cm.offsetHeight;
	if (window.opera || navigator.userAgent.indexOf('Netscape/7')!=-1) {
		cm.style.height=mh+'px';
		cm.style.overflow='scroll';
		return;
	}
	setTimeout('lefttime=setInterval("ScrollMarquee()",30)',d);
}

function SetFocus(i) {
	$(i).focus();
}


function toggle(item,act) {
	var status = document.getElementById(item).style.display;
	var temp = item+'_sign';
	if (status == 'none' || status == '') {
		document.getElementById(item).style.display = 'block';
		document.getElementById(temp).innerHTML = '[-]';
	} else {
		document.getElementById(item).style.display = 'none';
		document.getElementById(temp).innerHTML = '[+]';
	}
}


function toggleRadioButton(item,extra) {
	var path1 = document.getElementById(extra+'1');
	var path2 = document.getElementById(extra+'2');
	if (item.checked && item.value == 'FAM') {
		path1.disabled = false;
		path2.disabled = false;
	} else {
		path1.disabled = true;
		path2.disabled = true;
	}
}	

function formFocus(item,txt) {
	if (item.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == txt) {
		item.value = '';
		item.focus();
	}
}

function formBlur(item,txt) {
	if (item.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == '') {
		item.value = txt;
		formKeyUp(document.getElementById('formLogin').username,'formLogin');
	}
}

function formKeyUp(item,obj) {
	if (item.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != '' && item.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != 'Username') {
		document.getElementById(obj).password.disabled = false;
		document.getElementById(obj).submit.disabled = false;
	} else {
		document.getElementById(obj).password.disabled = true;
		document.getElementById(obj).submit.disabled = true;
		document.getElementById(obj).username.focus();
	}
}
	
function formError(code) {
	document.getElementById('formErrorList').innerHTML = code;
	document.getElementById('formError').style.display = "inline-block";
	try { document.getElementById('formSuccess').style.display = "none"; }
		catch (e) {}
}

function formSuccess(code) {
	document.getElementById('formSuccess').innerHTML = code;
	document.getElementById('formSuccess').style.display = "inline-block";
	try { document.getElementById('formError').style.display = "none"; }
		catch (e) {}
}

function embedPDF(page,item,action) {
	document.getElementById('darkContainer').style.display = 'block';
	document.getElementById('contentContainer').style.display = 'block';
	document.getElementById('embedPDFContainer').style.display = 'block';
	var myPDF = new PDFObject({
		url: 'pdf.php?page='+page+'&id='+item,
		pdfOpenParams: { scrollbars: '1', toolbar: '0', statusbar: '0', messages: '0', navpanes: '0' }
	}).embed('embedPDFContainer');
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

var Window = {	
	//Returns an integer representing the width of the browser window (without the scrollbar).
	getWindowWidth : function() {
	return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},
 
	//Returns an integer representing the height of the browser window (without the scrollbar).
	getWindowHeight : function() {	
	return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},	
 
	//Returns an integer representing the scrollWidth of the window. 
	getScrollWidth : function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},
 
	//Returns an integer representing the scrollHeight of the window. 
	getScrollHeight : function(){		
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
	},			
 
	//Returns an integer representing the scrollLeft of the window (the number of pixels the window has scrolled from the left).
	getScrollLeft : function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},
 
	//Returns an integer representing the scrollTop of the window (the number of pixels the window has scrolled from the top).
	getScrollTop : function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	}
};

