// FOR USE WITH ALL PAGES

// ########################################
// SETUP
// ########################################

// FIRST, REDIRECTS BASED ON URL

	// redirect donation page to https
	if ( document.location.pathname.indexOf('/1/donate.asp') != -1  && document.location.protocol == "http:") {
		document.location.href = "https:" + document.location.host + document.location.pathname;		
	}
	// redirect any https that are not donate page to http
	if ( document.location.pathname.indexOf('/donate.asp') == -1  && document.location.protocol == "https:") {
		document.location.href = "http:" + document.location.host + document.location.pathname;		
	}
	// redirect to www.bamn.com
	if (location.host == "bamn.com") {
		document.location.href = "http://www.bamn.com" + location.pathname;		
	}


// menu-related variables

	var HideMenuMessage = "hide menu";
	var MenuOn = true;
	var MenuIsMoving = false;
	var MenuColPos = 0;
	var AnimSpeed;
	
	var MarginWithoutMenu = 50;	
	var MarginWithMenu = 205; 								// must match stylesheet
	var BkgImagePath = "/gfx/bkgds/bkgd-menu-yellow.gif";	// must match stylesheet
	var RolloverPath = "/gfx/bkgds/bkgd-menu-rollover.jpg";

	var t1 = String(document.URL);
	if (t1.indexOf("http://")==0) t1 = t1.substring(7);
	var SplitURL = t1.split("/"); 							// SplitURL[1] = 1st subdir entry or file in rootdir
	
	// at homepage?
	var IsHomePage;
	if (SplitURL[1].indexOf("index.")==0) IsHomePage = true;
	if (SplitURL[1].length == 0) IsHomePage = true;
	
	// at ca page?
	var IsCaPage;
	if (SplitURL[1] == "california" && SplitURL[2].indexOf ("index.") == 0) IsCaPage = 1;
	if (SplitURL[1] == "california" && SplitURL[2].length == 0) IsCaPage = 1;
	
	// the above doesn't detect some nonsensical things like, e.g., "bamn.com/#?#"

// don't show toggle menu link under these circumstances; 
// otherwise, start the clock to show toggle link
// Ronald: I'm turning off the hide menu message below... doesn't seem to work
	/*	
	if (! (IsCaPage || IsHomePage) )
	{		var TimerShowMenuMsg = setInterval("ShowMenuMsg()", 250);
	}
	*/
// california-specific graphics

	if (IsCaPage)
	{	RolloverPath = "/gfx/bkgds/bkgd-menu-rollover-orange-ca.jpg"; 
	}

// preload rollover image now

	var tempImage = new Image();
	tempImage.src = RolloverPath;


// ########################################
// FUNCTIONS
// ########################################

function ShowMenuMsg()
{
	// fills in toggle menu link text. use of this function ensures no link is shown when js is disabled
	// as a workaround to using "<body onload=...", this function is called immediately
	// and keeps looping via setinterval until the html element has finally appeared

	if (document.getElementById("togglelink") != null) 	
	{
		document.getElementById("togglelink").innerHTML = HideMenuMessage;
		clearInterval(TimerShowMenuMsg);
	}
}


function DoMenu()
{ 
	if (MenuIsMoving) return;

	if (MenuOn)
	{
		// turn off menu instantly
		HideMenu();
		MenuOn = false;
	}
	else	
	{
		// start anim
		MenuColPos = -188;
		document.getElementById("menucolumn").style.left = String(MenuColPos) + "px";			
		document.getElementById("maincolumn").style.marginLeft = String(MarginWithMenu) + "px";
		document.getElementById("menucolumn").style.display = 'block';
		document.getElementById("container").style.backgroundImage = "url(\"" + BkgImagePath + "\")";;
		document.getElementById("togglelink").innerHTML = HideMenuMessage;
	
		AnimSpeed = 50;
		MenuIsMoving = true;
		MenuTimer = setInterval("MenuAnim()", 33);
	}
}

function MenuAnim()
{
	// timer-based loop
	
	MenuColPos += AnimSpeed;
	AnimSpeed *= .74;

	if (MenuColPos > 0)
	{
		// end of enter anim
		MenuColPos = 0;
		clearInterval(MenuTimer);
		MenuOn = true;
		MenuIsMoving = false;
		document.getElementById("menucolumn").style.display = 'block';
		document.getElementById("maincolumn").style.marginLeft = String(MarginWithMenu) + "px";
	}

	document.getElementById("menucolumn").style.left = String(MenuColPos) + "px";	
}

function HideMenu()
{
	document.getElementById("menucolumn").style.display= 'none'; 
	document.getElementById("maincolumn").style.marginLeft = String(MarginWithoutMenu) + "px";
	document.getElementById("container").style.backgroundImage = 'none';
	document.getElementById("togglelink").innerHTML = "show menu";	
}

function MenuItemOn(num) 
{
	var idname = "menuitem" + num;
	var x1 = document.getElementById(idname);
	x1.style.backgroundColor = 'white'; 
	x1.style.backgroundImage = "url(\"" + RolloverPath + "\")";
	
}

function MenuItemOff(num) 
{
	/* MUST MATCH ORIGINAL STYLE SHEET */
	var idname = "menuitem" + num;
	var x1 = document.getElementById(idname);
	x1.style.backgroundColor = 'transparent';
	x1.style.backgroundImage = 'none';	
}

function NewWindow(url)
{
	var p = "resizable=0,location=0, directories=0,status=0,menubar=0,scrollbars=0,toolbar=0,width=480,height=440"
	New = window.open(url, '', p);
}


function NewWindow2(url)
{
	vars = "resizable=0,location=0, directories=0,status=0,menubar=0,scrollbars=1,toolbar=0,width=480,height=440"
	New = window.open(url, '', vars);
}



function NewWindowVid(VidCode)
{
	var title = "Video";

	// var pnum = window.location.pathname;

	var p = "resizable=0,location=0, directories=0,status=0,menubar=0,scrollbars=0,toolbar=0,width=660,height=545"
	New = window.open("/video/vidplay.asp?" + VidCode, title, p);
}


 
function ValidateForm(frm, SpecialCase)
{
	// assumes form uses standard form element names, which it better
	// assumes all the fields below exist unless otherwise coded
	// assumes consistent use of maxlength for input elements
	// special cases: contact; boycott
	
	var msg;
	msg = "";
	
	// name must exist
	if (frm.name.value.length == 0)
	{	msg = msg + "Please enter name\n";
	}
	
	// email OR phone# must exist
	if (SpecialCase != "sub")
	{
		if (frm.email.value.length == 0 && frm.phone.value.length == 0)
		{	msg = msg + "Please enter an email address and/or phone number\n";
		}
	}

	// email address format
	if (frm.email.value.length > 0 && !EmailIsValid(frm.email.value))
	{	msg = msg + "Format of email address is not valid\n";
	}	

	// city OR state must exist
	if (SpecialCase != "contact" && SpecialCase != "sub")
	{
		if (frm.city_or_addr.value.length == 0 && frm.state.value.length == 0 && 
				(frm.school == null || (frm.school != null && frm.school.value.length == 0) ) )
		{	msg = msg + "Please enter a location\n";
		}
	}

	// note max length
	if (frm.note != null)
	{
		if (frm.note.value.length > 8000) 
		{	msg = msg + "Please limit message to 8000 characters\n";
		}
	}

	// contact page:  either note or subjectline must exist if no sub
	if (SpecialCase == "contact")
	{
		if (frm.note.value.length == 0 && frm.value2.value.length == 0 && !frm.status.checked)
		{	msg = msg + "Please enter message\n";
		}
	}

	// subscribe page
	if (SpecialCase == "sub")
	{
		if (frm.email.value.length == 0)
		{	msg = msg + "Please enter email address\n";
		}
		
		if (frm.state.value.length == 0)
		{
			msg = msg + "Please make a selection under 'State'";
		}
	}

	// endorsement page
	if (SpecialCase == "endorsement")
	{
		if (frm.org_and_title.value.length == 0)
		{	msg = msg + "Please enter organization\n";
		}
		
		if (!frm.Authorization.checked)
		{	msg = msg + "You must check the authorization box\n";
		}
	}

	if (msg.length == 0) 
		return (true);
	else
	{
		alert (msg);
		return (false);
	}
}

function EmailIsValid(e) 
{
	// not comprehensive...
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(e)) 
		return (true);
	else 
		return (false);
}
