//STALL SWF FUNCTION------------------------------------------------------------------------------------------------------------------
//Function to stall loading of an swf until a trigger image completes loading - typically the last image on the page
//USAGE: var triggerimg  = new Image(); triggerimg.src  = img12.src;		var waitforimg = setInterval ("stallbanner(triggerimg)", 500);
//USAGE: var triggerimg  = new Image(); triggerimg.src  = "images/pic.jpg";	var waitforimg = setInterval ("stallbanner(triggerimg)", 500);
function stallbanner (img) {
	if (img.complete) {
		var so = new SWFObject("Banner.swf", "Header", "790", "110", "7", "#223355"); so.write("Banner"); clearTimeout (waitforimg);
	}
}



//GALLERY THUMBNAIL GENERATOR FUNCTIONS-----------------------------------------------------------------------------------------------
//Function to generate photo gallery thumbnail set with links - no captions
//USAGE: var total = 18; makethumbs(total);
function makethumbs (a){
	for (var picnum = 1; picnum <= a; picnum++) {
		document.write('<a href="javascript:SwapBkgNoRepCTUrl('+"'"+'PhotoPanel'+"'"+','+"'"+'images/Pic ('+picnum+').jpg'+"'"+');"><img src="images/Pic ('+picnum+').jpg" width="60" height="40" border="0" /></a> ');
	}
}

//Function to generate photo gallery thumbnail set with links and captions
//USAGE: var total = 18; makethumbscap(total);
function makethumbscap (a){
	for (var picnum = 1; picnum <= a; picnum++) {
		document.write('<a href="javascript:SwapBkgNoRepCTUrl('+"'"+'PhotoPanel'+"'"+','+"'"+'images/Pic ('+picnum+').jpg'+"'"+');ShowInfo('+"'"+'Caption'+"'"+','+picnum+','+a+');"><img src="images/Pic ('+picnum+').jpg" width="60" height="40" border="0" /></a> ');
	}
}

//Function to generate photo gallery thumbnail set with mouseover actions and captions - no subdirectory
//USAGE: var total = 18; makerolloverthumbs(total);
function makerolloverthumbs (a){
	for (var picnum = 1; picnum <= a; picnum++) {
		document.write('<img onmouseover="SwapBkgNoRepCTUrl('+"'"+'PhotoPanel'+"'"+','+"'"+'Pic ('+picnum+').jpg'+"'"+');ShowInfo('+"'"+'Caption'+"'"+','+picnum+','+a+');" src="Pic ('+picnum+').jpg" width="60" height="40" border="0" /> ');
	}
}




//COOKIE FUNCTIONS--------------------------------------------------------------------------------------------------------------------
//Function to set a cookie
//USAGE: SetCookie("Style",url,7,"radiancesandiego.org");
function SetCookie (cookie_name,cookie_value,lifespan_in_days,valid_domain){
	// http://www.thesitewizard.com/javascripts/cookies.shtml
	var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
	document.cookie = cookie_name +
		"=" + encodeURIComponent( cookie_value ) +
		"; max-age=" + 60 * 60 * 24 * lifespan_in_days +
		"; path=/" + domain_string ;
}

//Function to retrieve a cookie
//USEAGE: colourscheme = GetCookie("Style");
function GetCookie (cookie_name){
	// http://www.thesitewizard.com/javascripts/cookies.shtml
	var cookie_string = document.cookie ;
	if (cookie_string.length != 0) {
		var cookie_value = cookie_string.match ( '(^|;)[\s]*' + cookie_name + '=([^;]*)' );
		return decodeURIComponent ( cookie_value[2] ) ;
	}
    return '' ;
}

//Function to delete a cookie (deletes at end of session)
//USEAGE: DeleteCookie("Style","radiancesandiego.org");
function DeleteCookie (cookie_name,valid_domain){
    // http://www.thesitewizard.com/javascripts/cookies.shtml
	var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
	document.cookie = cookie_name + "=; max-age=0; path=/" + domain_string ;
}

//Function to append page with new css file: THIS VERSION SETS A COOKIE
//USEAGE: <a href="javascript:load_style('cssjs/site2.css');">Teal</a>
//or:<INPUT TYPE="button" onClick="load_style('cssjs/site2.css');" VALUE="Teal">
function load_style(url) {
	a = document.createElement('link');
	a.setAttribute('rel','stylesheet');
	a.setAttribute('href',url);
	document.getElementsByTagName('head')[0].appendChild(a);
	SetCookie("Style",url,7,"radiancesandiego.org");
}

//Functions to manage page reload with new css file:
function LoadCookie()	{if (GetCookie('Style') != '') {load_style(GetCookie('Style'));}}
function LoadVCookie()	{if (GetCookie('Style') != '') {load_style('../../../'+GetCookie('Style'));}}
function RefreshStyle()	{if (GetCookie('Style') != '') {DeleteCookie("Style","radiancesandiego.org"); history.go(0);}}



//MISC FUNCTIONS-----------------------------------------------------------------------------------
//Function to append page with new css file: THIS VERSION DOES NOT SET A COOKIE
//USEAGE: <a href="javascript:LoadStyle('cssjs/site2.css');">Teal</a>
//or:<INPUT TYPE="button" onClick="LoadStyle('cssjs/site2.css');" VALUE="Teal">
function LoadStyle(url) {
	a = document.createElement('link');
	a.setAttribute('rel','stylesheet');
	a.setAttribute('href',url);
	document.getElementsByTagName('head')[0].appendChild(a);
}

//Function to reload pngFix (.png background & image swaps need this reloaded to display properly)
function ReloadPNGFix () {$(document).ready(function(){$(document).pngFix();});}

//Function to Setup MP3 Player
function SetupTunes1() {
	var so = new SWFObject("Music/ep_player.swf", "ep_player", "269", "226", "9", "#88bbcc");
	so.addVariable("skin", "Music/skins/nobius/skin.xml");
	so.addVariable("autoplay", "false");
	so.addVariable("shuffle", "false");
	so.addVariable("repeat", "false");
	so.addVariable("buffertime", "1");
	so.addParam("allowscriptaccess", "always");
}



//POPUP WINDOW FUNCTIONS--------------------------------------------------------------------------------------------------------------
//Function to launch Fixed Size Popup Window
//USEAGE: <a href="javascript:PopupWindowFixed('RadRec2.html', 'RadRec', 600, 390)">Click here for pop-out.</a>
function PopupWindowFixed(Source, Name, Width, Height) { 
	Options = "toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=" + Width + ",height=" + Height;
	window.open(Source, Name, Options); 
}

//Function to launch Adjustable Size Popup Window
//USEAGE: <a href="javascript:PopupWindowFixed('RadRec2.html', 'RadRec', 600, 390)">Click here for pop-out.</a>
function PopupWindow(Source, Name, Width, Height) { 
	Options = "toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=1,width=" + Width + ",height=" + Height;
	window.open(Source, Name, Options); 
}



//PROPERTY SWAPPING FUNCTIONS---------------------------------------------------------------------------------------------------------
//Functions to Show/Hide Div Tags:
//USAGE: <a href="javascript:ShowDiv('TargetID');">ShowDiv</a>
function ShowDiv(a) {document.getElementById(a).style.display = "block";}

//USAGE: <a href="javascript:HideDiv('TargetID');">HideDiv</a>
function HideDiv(a) {document.getElementById(a).style.display = "none";}

//USAGE: <a href="javascript:ShowHide('TargetID');">ShowHide</a>
//NOTE: Make sure target's display is actually set to block or none first.
function ShowHide(a) {
	if(document.getElementById(a).style.display == "none") {document.getElementById(a).style.display = "block";}
	else {document.getElementById(a).style.display = "none";}
}


//Functions that swap element backgrounds:
//USAGE: <a href="javascript:ClearBkg('TargetID');">ClearBkg</a>
function ClearBkg(a) {document.getElementById(a).style.background = "url()";}

//USAGE: <a href="javascript:SwapBkg('TargetID','#424 no-repeat top center url(images/test.gif)');">SwapBkg</a>
function SwapBkg(a,b) {document.getElementById(a).style.background = b;}

//USAGE: <a href="javascript:SwapBkgUrl('TargetID', 'images/test.gif');">SwapBkgUrl</a>
function SwapBkgUrl(a,b) {document.getElementById(a).style.background = "url('"+b+"')";}

//USAGE: <a href="javascript:SwapBkgNoRepUrl('TargetID', 'images/test.gif');">SwapBkgNoRepUrl</a>
function SwapBkgNoRepUrl(a,b) {document.getElementById(a).style.background = "no-repeat url('"+b+"')";}

//USAGE: <a href="javascript:SwapBkgNoRepCTUrl('TargetID', 'images/test.gif');">SwapBkgNoRepCTUrl</a>
function SwapBkgNoRepCTUrl(a,b) {document.getElementById(a).style.background = "no-repeat center top url('"+b+"')";}


//Functions that swap other element properties:
//USAGE: <a href="javascript:SwapH('TargetID', '400px');">SwapH</a><br />
function SwapH(a,b) {document.getElementById(a).style.height = b;}

//USAGE: <a href="javascript:SwapW('TargetID', '300px');">SwapW</a><br />
function SwapW(a,b) {document.getElementById(a).style.width = b;}

//USAGE: <a href="javascript:SwapColor('TargetID', '#0ff');">SwapColor</a><br />
function SwapColor(a,b) {document.getElementById(a).style.color = b;}

//USAGE: <a href="javascript:SwapProp('TargetID','height','300px');">SwapProp</a><br />
//NOTE: This function will obviously only support the included properties.
function SwapProp(a,b,c) {
	if(b == 'height') {document.getElementById(a).style.height = c;}
	if(b == 'width') {document.getElementById(a).style.width = c;}
	if(b == 'color') {document.getElementById(a).style.color = c;}
	if(b == 'background') {document.getElementById(a).style.background = c;}
	if(b == 'display') {document.getElementById(a).style.display = c;}
	if(b == 'float') {document.getElementById(a).style.float = c;}
	if(b == 'src') {document.getElementById(a).src = c;}
	if(b == 'class') {document.getElementById(a).className = c;}
}



//CLASS AND OBJECT SWAPPING FUNCTIONS-------------------------------------------------------------------------------------------------
//Function to swap css class:
//USEAGE: <a href="javascript:SwapClass('test','code red');">SwapClass</a>
function SwapClass(a,b) {document.getElementById(a).className = b;}

//Function to swap SWF source (Requires presence of swfobject.js):
//USEAGE: <a href="javascript:SwapSWF('TargetID','NewSource','NewTitle','Width#','Height#','Version#','Bkg#');">Swap SWF</a>
function SwapSWF(a,b,c,d,e,f,g)	{var so = new SWFObject(b,c,d,e,f,g); so.write(a);}

//Function to swap the source of an Iframe:
//('TargetID', 'NewSourceURL')
function SwapIframe(a,b) {document.getElementById(a).src = b;}

//Function to swap the source of an image:
//('TargetID', 'Path of image to swap (relative to document)')
function SwapImg(a,b) {document.getElementById(a).src = b;}



//BULLET SWAPPING FUNCTIONS-----------------------------------------------------------------------------------------------------------
//Function that selects the current Bullet and displays its content:
//('BulletId', #selected, #total)
function BulSelect(a,b,c){
	for (var i = 1; i <= c; i++) {
		if (i == b)	{
			document.getElementById(a+i).className				= "BulletOn MainCurrent";
			document.getElementById(a+"Box"+i).style.display	= "block";
		}
		else		{
			document.getElementById(a+i).className				= "BulletOff TabOffColor";
			document.getElementById(a+"Box"+i).style.display	= "none";
		}
	}	
}



//TAB SWAPPING FUNCTIONS--------------------------------------------------------------------------------------------------------------
//Function that selects the current Tab and displays its content:
//NOTE: HTML and Classes must follow the template
//USAGE: <a id="TabBoxD1" class="TabBoxDOn " style="width:238px;" onmouseover="TabUp('TabBoxD',1,3);">HTML</a>
function TabUp(a,b,c){
	for (var i = 1; i <= c; i++) {
		if (i == b)	{
			document.getElementById(a+i).className				= a+"On";
			document.getElementById(a+"Box"+i).style.display	= "block";
		}
		else		{
			document.getElementById(a+i).className				= a+"Off";
			document.getElementById(a+"Box"+i).style.display	= "none";
		}
	}	
}

//Alternate Function that selects the current Tab and displays its content:
//('TabId', #selected, #total)
function TabSelect(a,b,c){
	for (var i = 1; i <= c; i++) {
		if (i == b)	{
			document.getElementById(a+i).className				= "TabOn MainBkgColor MainCurrent";
			document.getElementById(a+"Box"+i).style.display	= "block";
		}
		else		{
			document.getElementById(a+i).className				= "TabOff TabBkgColor TabOffColor";
			document.getElementById(a+"Box"+i).style.display	= "none";
		}
	}	
}

//Function to swap a grouping's content and highlight its associated tab/button:
//(I mostly use this only for bulleted lists where each tab has 2 seperate divs for the up state and the down state)
//('TargetID base name', # selected, # total in this group) Assumes Base1Up, Base1Down, Base1Window exist.
function Show(c,d,e) {
	for (var i = 1; i <= e; i++) {
		if (i == d) {
			document.getElementById(c+i+'Up').style.display = "none";
			document.getElementById(c+i+'Down').style.display = "block";
			document.getElementById(c+i+'Window').style.display = "block";
		}
		else {
			document.getElementById(c+i+'Up').style.display = "block";
			document.getElementById(c+i+'Down').style.display = "none";
			document.getElementById(c+i+'Window').style.display = "none";
		}

	}	
}

//Function to highlight a grouping's associated tab/button only (no content is affected):
//('TargetID base name', # selected, # total in this group, 'Path to ButtonBaseName') Assumes ButtonBaseNameUp/Down.gif exist.
function ShowTab(c,d,e,f) {
	for (var i = 1; i <= e; i++) {
		if (i == d)	{document.getElementById(c+i).style.background = "no-repeat url('"+f+"Down.gif')";}
		else		{document.getElementById(c+i).style.background = "no-repeat url('"+f+"Up.gif')";}
	}	
}

//Function to swap a tab grouping's content only (no buttons or tabs are affected):
//('TargetID base name', # selected, # total in this group)
function ShowInfo(c,d,e) {
	for (var i = 1; i <= e; i++) {
		if (i == d)	{document.getElementById(c+i).style.display = "block";}
		else		{document.getElementById(c+i).style.display = "none";}
	}	
}



//FORM INPUT FUNCTIONS----------------------------------------------------------------------------------------------------------------
//Clear And Replace Form Text:
//USAGE: <textarea id="Comments" onfocus="clearText(this);"onblur="replaceText(this);">Default text here.</textarea>
function clearText(thefield)	{if (thefield.defaultValue == thefield.value) { thefield.value = "" } } 
function replaceText(thefield)	{if (thefield.value == "") { thefield.value = thefield.defaultValue } }

// Replace input field with default value on blur if nothing was entered:
// USAGE: <input type="text" value="Default Value" onfocus="ClearField(this)" onblur="ClearField(this)" />
function ClearField(thefield)	{
	if (thefield.value == "") { thefield.value = thefield.defaultValue } else
	if (thefield.defaultValue == thefield.value) { thefield.value = "" }
}

// Replace input field with GREY default value on blur if nothing was entered (css should match grey color):
// USAGE: <input type="text" value="Default Value" onfocus="GreyField(this,'on')" onblur="GreyField(this,'off')" />
function GreyField(thefield,onOff) {
	if (onOff == 'on') {if (thefield.value == thefield.defaultValue) {thefield.value = ""; thefield.style.color = '#9ff';}} else
	if (thefield.value == "") {thefield.value = thefield.defaultValue; thefield.style.color = '#999';}
}

//General Form Validator (arguments are ID names that are "required". ID names should equal Default Values.):
//USAGE: <form action="javascript:alert('Thanks')" onSubmit="return ValidateForm('Name','Phone','Email');">
function ValidateForm() {
	var ra = arguments; var ralen = ra.length;
	for(i = 0; i < ralen; i++){
		if(document.getElementById(ra[i]).value == document.getElementById(ra[i]).defaultValue) {
			alert("Please enter your "+document.getElementById(ra[i]).defaultValue+".");
			document.getElementById(ra[i]).focus();
			return false;
		}
		if(ra[i] == 'email'){
			if(document.getElementById(ra[i]).value.indexOf("@") == -1) {
				alert("Please enter a valid email address.");
				document.getElementById(ra[i]).focus();
				return false;
			}
		}
	}
	return true;
}

//applies the selected value to a text field.
function showSelection(a,b) {
	var sourceID = a; var targetID = b;
	var selection = document.getElementById(sourceID);
	document.getElementById(targetID).value = selection.options[selection.selectedIndex].text;
}

//Swap Content Of Form Text Box:
//('Text to be displayed', 'Target ID')
function showTitle(a,b)	{var textstring = a; var targetID = b; document.getElementById(targetID).value = textstring;}



//MM FUNCTIONS------------------------------------------------------------------------------------------------------------------------
//The frequently used Find Object Function:
function MM_findObj(n, d) { //v4.01
	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);
	if(!x && d.getElementById) x=d.getElementById(n);
	return x;
}

//Function that swaps images:
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 that restores swapped images:
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 that handles image preload:
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];}
	}
}
