//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 reload page with new css file:
//usage: <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);}}



//Function to Setup MP3 Player-----------------------------------------------------------------------------------------------
function SetupTunes1() { //v3.0
	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");
}



//Function to launch Fixed Size Popup Window---------------------------------------------------------------------------------
//usage: <a href="javascript:PopupWindowFixed('RadRec2.html', 600, 390, 'RadRec')">Click here for pop-out.</a>
function PopupWindowFixed(Source, Width, Height, Name) { 
	Options = "toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=" + Width + ",height=" + Height;
	window.open(Source, Name, Options); 
}



//Function to swap the source of an Iframe-----------------------------------------------------------------------------------
//('TargetID', 'NewSourceURL')
function SwapIframe(a,b)	{document.getElementById(a).src=b;}



//Functions that Swap Layer Backgrounds--------------------------------------------------------------------------------------
//('LayerID', 'Path of image to swap (relative to document)')
function SwapImg(c,d)		{document.getElementById(c).src = d;}

//('LayerID', 'Path of image to swap (relative to document)')
function SwapBkg(c,d)		{document.getElementById(c).style.background = "no-repeat center top url('"+d+"')";}

//('LayerID', 'Path of image to swap (relative to document)')
function SwapBkgGrad(c,d)	{document.getElementById(c).style.background = "url('"+d+"')";}

//('LayerID')
function ClearBkg(c)		{document.getElementById(c).style.background = "no-repeat center top url(' ')";}



//TAB SWAPPING FUNCTIONS-----------------------------------------------------------------------------------------------------
//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 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";
		}
	}	
}

//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-------------------------------------------------------------------------------------------------------
//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;}

//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,'off')" onblur="clearField(this,'on')" />
function clearField(thefield,onOff) {
	if(onOff == 'off') {
		if	(thefield.defaultValue==thefield.value) {thefield.value = ""; thefield.style.color = '#000000';}
	}
	else {
		if	(thefield.value=="") {thefield.value = thefield.defaultValue; thefield.style.color = '#AAAAAA';}
	}
}

//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 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;
}

//Functions that handle 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];}}
}
