// ****************************************************************************************************
// Spend-o-meter code

var budgetBegin = 'July 1, 2008';
var budgetBeginTime = new Date(budgetBegin).getTime();	// change to start of fiscal year
var budgetTotal = 58999770294				// total amount in state budget
var budgetStep = budgetTotal/365/24/60/60/1000;		// how much $$$ per microsecond?
var updateSpeed = 49;					// in microseconds (1000=1 second)

// update DOM element with new amount
function updateSpendometer() {

/*
  var today = new Date().getTime();
  if (today > budgetBeginTime) {
    document.getElementById('spendometer').innerHTML = '<div style="line-height: 1em"><small>Spend-o-meter will be back soon</small></div>'
    return 0;
  }
*/

  // change the text in the "spendometer" span to the new amount (and format as currency)
  amount = ((new Date()).getTime()-budgetBeginTime)*budgetStep;
  if (amount < 0) { 
    amount = "Starting " + budgetBegin;
  } else if (amount > budgetTotal) {
    amount = budgetTotal;
  } else { 
    amount = reformat(amount);
  }
  document.getElementById('spendometer').innerHTML = amount;
  // come back and update this every so often
  setTimeout(updateSpendometer, updateSpeed);
}

// format number as currency string
function reformat(nStr)
{
	nStr += '.00';
	x = nStr.split('.');
	x1 = x[0];
        x[1] = '.' + x[1] + '00';
	x2 = x[1].substr(0,3);
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return '$' + x1 + x2;
}

// ****************************************************************************************************
function podcast() {
  OpenWindow=window.open('http://www.buckeyevoices.org/','podcast','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,width=713,height=576,left=20,top=20');
}

// ****************************************************************************************************
// Multi-purpose functions


function initPage() {
	initRollovers();
	LoadPreferredStyleSheet();
//	WindowLinks();

}


// deletion confirmation box.
// works for forms and for links. If it is a link, you have to do:
//   confirm_delete(1)
//
function confirm_delete($is_link) {
	if (!confirm("Are you sure you want to delete this item?")) {
		return false;
	}
	if ($is_link) {
		return true;
	}
	document.forms[0].dodelete.value = 'delete';
	document.forms[0].submit();
}


// ****************************************************************************************************
// form functions

// Show or hide a field using stylesheets
//
// onClick = showFormField('title',false)
// onChange = if(this.value = 1 showFormField('title',false));
//
// TBI version 1.5 function
//
function showFormField($field,$show) {
	if ($show) {
		document.getElementById('label_' + $field).style.display = 'inline';
		document.getElementById('field_' + $field).style.display = 'inline';
	} else {
		document.getElementById('label_' + $field).style.display = 'none';
		document.getElementById('field_' + $field).style.display = 'none';
	}
}


// show/hide form elements based on the type of document selected
function typeselected() {
	if(document.composer.type_id[5].selected == true) {
		document.getElementById('link1_text_a').style.display = "block";
		document.getElementById('link1_text_b').style.display = "block";
		document.getElementById('link_title_a').style.display = "none";
		document.getElementById('link_title_b').style.display = "block";
		document.getElementById('link1_text_a').style.display = "none";
		document.getElementById('link1_url_a').style.display = "none";
		document.getElementById('link1_url_b').style.display = "block";
		document.getElementById('link1_url_c').style.display = "block";
		document.getElementById('link1_text_c').style.display = "block";
		document.getElementById('category_a').style.display = "none";
		document.getElementById('category_b').style.display = "none";
		document.getElementById('title_a').style.display = "none";
		document.getElementById('title_b').style.display = "none";
		if (document.getElementById('author_id_a'))      document.getElementById('author_id_a').style.display = "none";
		if (document.getElementById('author_id_b'))      document.getElementById('author_id_b').style.display = "none";
		if (document.getElementById('author_a'))         document.getElementById('author_a').style.display = "block";
		if (document.getElementById('author_b'))         document.getElementById('author_b').style.display = "block";
		if (document.getElementById('author_c'))         document.getElementById('author_c').style.display = "none";
		if (document.getElementById('author_d'))         document.getElementById('author_d').style.display = "block";
		if (document.getElementById('author_tagline_a')) document.getElementById('author_tagline_a').style.display = "none";
		if (document.getElementById('author_tagline_b')) document.getElementById('author_tagline_b').style.display = "none";
		if (document.getElementById('featured_a'))       document.getElementById('featured_a').style.display = "none";
		if (document.getElementById('featured_b'))       document.getElementById('featured_b').style.display = "none";
//		document.getElementById('live_date_a').style.display = "none";
//		document.getElementById('live_date_b').style.display = "none";
		document.getElementById('synopsis_in_body_a').style.display = "none";
		document.getElementById('synopsis_in_body_b').style.display = "none";
		document.getElementById('synopsis_a').style.display = "none";
		document.getElementById('synopsis_b').style.display = "none";
		document.getElementById('body_a').style.display = "none";
		document.getElementById('body_b').style.display = "none";
		document.getElementById('link2_text_a').style.display = "none";
		document.getElementById('link2_text_b').style.display = "none";
		document.getElementById('link2_a').style.display = "none";
		document.getElementById('link2_b').style.display = "none";
		document.getElementById('link3_text_a').style.display = "none";
		document.getElementById('link3_text_b').style.display = "none";
		document.getElementById('link3_a').style.display = "none";
		document.getElementById('link3_b').style.display = "none";
		document.getElementById('pdf_a').style.display = "none";
		document.getElementById('pdf_b').style.display = "none";
	} else {
		document.getElementById('link_title_a').style.display = "block";
		document.getElementById('link_title_b').style.display = "none";
		document.getElementById('link1_text_c').style.display = "none";
		document.getElementById('link1_url_a').style.display = "block";
		document.getElementById('link1_url_b').style.display = "block";
		document.getElementById('link1_url_c').style.display = "none";
		document.getElementById('link1_text_a').style.display = "block";
		document.getElementById('category_a').style.display = "block";
		document.getElementById('category_b').style.display = "block";
		document.getElementById('title_a').style.display = "block";
		document.getElementById('title_b').style.display = "block";
		if (document.getElementById('author_id_a'))      document.getElementById('author_id_a').style.display = "block";
		if (document.getElementById('author_id_b'))      document.getElementById('author_id_b').style.display = "block";
		if (document.getElementById('author_a'))         document.getElementById('author_a').style.display = "block";
		if (document.getElementById('author_b'))         document.getElementById('author_b').style.display = "block";
		if (document.getElementById('author_c'))         document.getElementById('author_c').style.display = "block";
		if (document.getElementById('author_d'))         document.getElementById('author_d').style.display = "none";
		if (document.getElementById('author_tagline_a')) document.getElementById('author_tagline_a').style.display = "block";
		if (document.getElementById('author_tagline_b')) document.getElementById('author_tagline_b').style.display = "block";
		if (document.getElementById('featured_a'))       document.getElementById('featured_a').style.display = "block";
		if (document.getElementById('featured_b'))       document.getElementById('featured_b').style.display = "block";
		document.getElementById('synopsis_in_body_a').style.display = "block";
		document.getElementById('synopsis_in_body_b').style.display = "block";
		document.getElementById('synopsis_a').style.display = "block";
		document.getElementById('synopsis_b').style.display = "block";
		document.getElementById('body_a').style.display = "block";
		document.getElementById('body_b').style.display = "block";
		document.getElementById('link2_text_a').style.display = "block";
		document.getElementById('link2_text_b').style.display = "block";
		document.getElementById('link2_url_a').style.display = "block";
		document.getElementById('link2_url_b').style.display = "block";
		document.getElementById('link3_text_a').style.display = "block";
		document.getElementById('link3_text_b').style.display = "block";
		document.getElementById('link3_url_a').style.display = "block";
		document.getElementById('link3_url_b').style.display = "block";
		document.getElementById('pdf_a').style.display = "block";
		document.getElementById('pdf_b').style.display = "block";
	}
}

function authorselected() {

	if(document.composer.author_id[0].selected == true) {
		document.getElementById('author_a').style.display = "block";
		document.getElementById('author_b').style.display = "block";
		document.getElementById('author_tagline_a').style.display = "block";
		document.getElementById('author_tagline_b').style.display = "block";
		document.composer.author.disabled = false;
		document.composer.author_tagline.disabled = false;
	} else {
		document.getElementById('author_a').style.display = "none";
		document.getElementById('author_b').style.display = "none";
		document.getElementById('author_tagline_a').style.display = "none";
		document.getElementById('author_tagline_b').style.display = "none";
		document.composer.author.disabled = true;
		document.composer.author_tagline.disabled = true;
	}
}

function synopsisselected() {

	if(document.composer.synopsis_in_body[1].selected == true) {
		document.getElementById('synopsis_a').style.display = "block";
		document.getElementById('synopsis_b').style.display = "block";
	} else {
		document.getElementById('synopsis_a').style.display = "none";
		document.getElementById('synopsis_b').style.display = "none";
	}
}

// option is the select box option # that should make the hidden field show up.
function publishselected($option) {

	if(document.composer.publish_when[$option].selected == true) {
		document.getElementById('new_publish_date').style.display = "inline";
	} else {
		document.getElementById('new_publish_date').style.display = "none";
	}
}

function expand() {
	for (var i=0; i<expand.arguments.length; i++) {
		var element = document.getElementById(expand.arguments[i]);
		element.style.display = "block";
	}
}

function collapse() {
	for (var i=0; i<collapse.arguments.length; i++) {
		var element = document.getElementById(collapse.arguments[i]);
		element.style.display = "none";
	}
}




/*	Standards Compliant Rollover Script
	specify all rollover images with class="imgover"
	off image: image-name.jpg
	on image: image-name_o.jpg
*/
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));

				// if there's an associated text block to display, show it
				if (document.getElementById("text_" + this.getAttribute('id'))) {
					document.getElementById("text_" + this.getAttribute('id')).style.display='block';
					document.getElementById('text_key_0').style.display='none';
				}
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);

				// if there's an associated text block being displayed, hide it
				if (document.getElementById("text_" + this.getAttribute('id'))) {
					document.getElementById('text_key_0').style.display='block';
					document.getElementById("text_" + this.getAttribute('id')).style.display='none';
				}
			}
		}
	}
}




/*

from http://sitepoint.com/examples/popups/ 

*/

function acpopup(strURL,strType,strHeight,strWidth) { 
  var strOptions=""; 
  if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth; 
  window.open(strURL, '', strOptions); 
} 

/*

from http://sitepoint.com/examples/popups/ 

usage:
	<a href="http://localhost/blah.html" rel="popup|320|240">popup window</a> - link css is .popup
	<a href="http://localhost/blah.html" rel="external">external window that loads in a new window</a> - link css is .external


*/

function windowLinks() {                      // create a new function called windowLink();  
alert ("testing 1");
  if(!document.getElementsByTagName) {        // Only run the function on browsers that 
       return;                                // understand 'getElementsByTagName' - new browsers 
  } 
   
alert ("testing 2");
  var anchors = document.getElementsByTagName("a"); // grab all links and pop them in an array called 'anchors' 
  for (var i = 0; i < anchors.length; i++) {        // start a loop to work our way through  
alert ("testing 3");
   var anchor = anchors[i];                         // grab the next link & copy it to 'anchor' for working 
   var relIndex = anchor.rel;                       // get the value of it's 'REL' attribute 
   if (relIndex) {                                  // does it have a value for REL?... 
alert ("testing 4");
     var relSplit = relIndex.split("|");           
     /* if it does, look for '|' to use to split the value into an 
     array - relSplit[0], relSplit[1], relSplit[2], etc . If it doesn't 
     find any '|', the whole value gets transferred to relSplit[0] */ 
        
  /* XHTML compliant target attribute */ 

     if (relSplit[0] == "external") {       // If the relSplit[0] is 'external'... 
       anchor.target = "_blank";           // then set it's 'target' attribute to '_blank' 
       anchor.className = "external";      // then attach a CSS class to it  
       anchor.title = "Load in new window: "+ anchor.href;   
       // And give it a new title attribute to warn users a new window is launching 
         
  /* Elegantly degrading window code */ 

      } else if (relSplit[0] == "popup") {      // else if relSplit[0] is 'popup'... 
alert ("got popup");
        anchor.className = "popup";               // attach a CSS class to it  
        anchor.title = "Loads in a Popup Window"; // Give it a helpful title attribute 
        anchor.popupWidth = relSplit[1];          // set the popup's width 
        anchor.popupHeight = relSplit[2];          // set the popup's height 
        anchor.onclick = function() {acpopup(this.href,'console',this.popupWidth,this.popupHeight);return false;}; 
        // plug all this information into our original window launch function (acpopup)  
      } 
    } 
  } 
}  

/* commented out 3-29-07 -- not needed
// open a sign-in window
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=320,height=200,left = 416,top = 332');");
}
*/

// open a new pop-up window
function openWindow(loc) { 
	newWindow = window.open(loc, 'picker', 'toolbar=no, location=no, directories=no, scrollbars=yes, status=yes, width=600, height=400') 
}


// set the active stylesheet
function setActiveStyleSheet(title, reset) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (reset == 1) {
	  createCookie("style", title, 365);
  }
}


// load the stylesheet defined by the "style" cookie
function LoadPreferredStyleSheet () {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}


// set the style cookie, if it needs to be
function setStyle() {
	var style = readCookie("style");
	if (style != null) {
		setActiveStyleSheet(style, 0);
	}
}

// learn the user's previously preferred style sheet
function getPreferredStyleSheet() {
	linksFound = document.getElementsByTagName("link");
	for (i=0; i<linksFound.length; i++) {
		thisLink = linksFound[i];
		if (thisLink.getAttribute("rel").indexOf("style") > -1 && thisLink.getAttribute("rel").indexOf("alt") == -1 && thisLink.getAttribute("title")) {
		 	return thisLink.getAttribute("title");
		}
	}
}


// create a cookie
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// read a cookie
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


function validateSignup() {
	var emailID=document.getElementById('form-signup').email
	var zipcode=document.getElementById('form-signup').zipcode

	if ((emailID.value==null)||(emailID.value=="")||(emailID.value==" Your e-mail address")){
		alert("Please Enter your email address in the box.")
		emailID.focus()
		return false
	}
	if (checkValidEmail(emailID.value)==false){
		emailID.focus()
		return false
	}
	if (	(zipcode.value==null)||
		(zipcode.value=="")||
		(zipcode.value==" Zip Code")||
		(zipcode.value=="12345")||
		(zipcode.value=="54321")){
		alert("Please Enter your zip code in the box.")
		zipcode.focus()
		return false
	}
	if (checkValidZip(zipcode)==false){
		zipcode.focus()
		return false
	}
	return true
}

function checkValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var msg="The email address you've provided is not valid. Please enter a valid email address."
	if (str.indexOf(at)==-1){
	  alert(msg)
	  return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	  alert(msg)
	  return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	  alert(msg)
	  return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
	  alert(msg)
	  return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	  alert(msg)
	  return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
	  alert(msg)
	  return false
	}
		
	if (str.indexOf(" ")!=-1){
	  alert(msg)
	  return false
	}

	return true					
}

function checkValidZip(zip) {
	var vzip1 = /^\D*(\d{5})$/
	var vzip2 = /^\D*(\d{5})\D*(\d{4})\D*$/
	var vpostcode = /^\W*([a-z]{1}\d{1}[a-z]{1})\W*(\d{1}[a-z]{1}\d{1})\W*$/i
	if(vpostcode.test(zip.value)){
		// valid canadian postal code
		zip.value = zip.value.toUpperCase();
		zip.value = zip.value.replace(vpostcode,'$1 $2');
	} else if(vzip1.test(zip.value)) {
			// valid 5-digit zip code
	} else {
		if(vzip2.test(zip.value)){
			// valid 10-digit zip code
			zip.value=zip.value.replace(vzip2,'$1-$2');
		} else {
			// invalid zip code or candian postal code
			alert("There is a problem with the zip/postal code you entered. Please enter your correct 5 digit or 5+4 digit Zip Code or Canadian Postal Code.");
			return false
		}
	}
	return true
}
function showOnlyTableRow(rows,id) {
	for (counter = 0; counter < rows.length; counter++){
		document.getElementById("row" + rows[counter]).style.display = "none";
	}
	document.getElementById("row" + id).style.display = "";
	return false;
}