// JavaScript Document
var counter = 1;
var fadeinopacityval = 0;
var fadeIncrement = 0.1;
var numberOfEnglishFacts = 24;
var numberOfFrenchFacts = 23;

function fadeoutText() {
	var opacityval = YAHOO.util.Dom.getStyle('fact' + counter, 'opacity');
	if (opacityval <= 0) {
		YAHOO.util.Dom.setStyle('fact' + counter, 'display', 'none');
		clearTimeout(fadeoutTimer);
		if( document.getElementById( 'fact' + counter ).parentNode.id == "factBoxContent" ){
			if (counter == numberOfEnglishFacts) {
				counter = 1;
			} else counter++;
		} else {
			if (counter == numberOfFrenchFacts) {
				counter = 1;
			} else counter++;
		}
		YAHOO.util.Dom.setStyle('fact' + counter, 'display', 'block');
		YAHOO.util.Dom.setStyle('fact' + counter, 'opacity', 0);
		fadeinText();
	} else {
		YAHOO.util.Dom.setStyle('fact' + counter, 'opacity', opacityval - fadeIncrement);
		fadeoutTimer = setTimeout('fadeoutText();', 100);
	}
}

function fadeinText() {
	if (fadeinopacityval >= 1) {
		clearTimeout(fadeinTimer);
		fadeinopacityval = 0;
		fadeoutTimer = setTimeout('fadeoutText();', 10000);
	} else {
		fadeinopacityval += fadeIncrement;
		YAHOO.util.Dom.setStyle('fact' + counter, 'opacity', fadeinopacityval);
		fadeinTimer = setTimeout('fadeinText();', 100);
	}
}

function prepareTextSizeIncreaser(){
  if( document.getElementById ){
    if( document.getElementById( 'increasetextsize' ) ){
		var gallery = document.getElementById( 'increasetextsize' );
		gallery.onclick = function(){
		  if( document.getElementById( 'introSectionText' ) ) document.getElementById('introSectionText').className = 'intro1Large';
		  if( document.getElementById( 'primaryCol' ) ) document.getElementById('primaryCol').className = 'body1Large';
		  if( document.getElementById( 'secondaryCol' ) ) document.getElementById('secondaryCol').className = 'body2Large';
		};
    }
  }
}

function prepareTextSizeDecreaser(){
  if( document.getElementById ){
    if( document.getElementById( 'decreasetextsize' ) ){
		var gallery = document.getElementById( 'decreasetextsize' );
		gallery.onclick = function(){
		  if( document.getElementById( 'introSectionText' ) ) document.getElementById('introSectionText').className = 'intro1Small';
		  if( document.getElementById( 'primaryCol' ) ) document.getElementById('primaryCol').className = 'body1Small';
		  if( document.getElementById( 'secondaryCol' ) ) document.getElementById('secondaryCol').className = 'body2Small';
		};
    }
  }
}

function prepareTextSizeResetter(){
  if( document.getElementById ){
    if( document.getElementById( 'resettextsize' ) ){
		var gallery = document.getElementById( 'resettextsize' );
		gallery.onclick = function(){
		  if( document.getElementById( 'introSectionText' ) ) document.getElementById('introSectionText').className = 'intro1';
		  if( document.getElementById( 'primaryCol' ) ) document.getElementById('primaryCol').className = 'body1';
		  if( document.getElementById( 'secondaryCol' ) ) document.getElementById('secondaryCol').className = 'body2';
		};
    }
  }
}

function prepareLinkedImages(){
  if( document.getElementById &&
      document.getElementsByTagName ){
    if( document.getElementById( 'colB' ) ){
      var gallery = document.getElementById( 'colB' );
      var links = gallery.getElementsByTagName( 'a' );
      for( var i=0; i < links.length; i++ ){
           if (links[i].firstChild) {
		  if (links[i].firstChild.nodeName == 'IMG') {
			  links[i].className = 'linkedimage';
		  }
	}
      }
    }
  }
}

function prepareGallery(){
  if( document.getElementById &&
      document.getElementsByTagName ){
    if( document.getElementById( 'imagegallery' ) ){
      var gallery = document.getElementById( 'imagegallery' );
      var links = gallery.getElementsByTagName( 'a' );
      for( var i=0; i < links.length; i++ ){
        links[i].onclick = function(){
          return showPic(this);
        };
      }
    }
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function restoreCPGIDefaults() {
	var cpgi_answers = window.document.forms.namedItem("cpgi").elements;
	document.getElementById("q1box").className = "";
	document.getElementById("q2box").className = "blueback";
	document.getElementById("q3box").className = "";
	document.getElementById("q4box").className = "blueback";
	document.getElementById("q5box").className = "";
	document.getElementById("q6box").className = "blueback";
	document.getElementById("q7box").className = "";
	document.getElementById("q8box").className = "blueback";
	document.getElementById("q9box").className = "";
}

function checkCPGIValues() {
	restoreCPGIDefaults();
	var cpgi_answers = window.document.forms.namedItem("cpgi").elements;
	var error_string = "";
	
	if (cpgi_answers.namedItem("q1-default").checked) {
		error_string += "\nQuestion 1";
		document.getElementById("q1box").className = "error";
	}
	if (cpgi_answers.namedItem("q2-default").checked) {
		error_string += "\nQuestion 2";
		document.getElementById("q2box").className = "error";
	}
	if (cpgi_answers.namedItem("q3-default").checked) {
		error_string += "\nQuestion 3";
		document.getElementById("q3box").className = "error";
	}
	if (cpgi_answers.namedItem("q4-default").checked) {
		error_string += "\nQuestion 4";
		document.getElementById("q4box").className = "error";
	}
	if (cpgi_answers.namedItem("q5-default").checked) {
		error_string += "\nQuestion 5";
		document.getElementById("q5box").className = "error";
	}
	if (cpgi_answers.namedItem("q6-default").checked) {
		error_string += "\nQuestion 6";
		document.getElementById("q6box").className = "error";
	}
	if (cpgi_answers.namedItem("q7-default").checked) {
		error_string += "\nQuestion 7";
		document.getElementById("q7box").className = "error";
	}
	if (cpgi_answers.namedItem("q8-default").checked) {
		error_string += "\nQuestion 8";
		document.getElementById("q8box").className = "error";
	}
	if (cpgi_answers.namedItem("q9-default").checked) {
		error_string += "\nQuestion 9";
		document.getElementById("q9box").className = "error";
	}
	
	if (error_string.length == "") return true;
	else {
			error_string = "All questions must be answered before you submit.\nThe following questions have not been answered:\n" + error_string;
			alert(error_string);
			return false;
	}
}

function checkEmailAQuestionValues() {
	var emailaq_answers = window.document.forms.namedItem("emailaquestion").elements;
	var error_string = "";
	
	if (emailaq_answers.namedItem("fullname").value == '') {
		error_string += "\nWhat is your name?";
	}
	if (emailaq_answers.namedItem("emailaddress").value == '') {
		error_string += "\nWhat is your email address?";
	}
	if (emailaq_answers.namedItem("question").value == '') {
		error_string += "\nWhat is your question?";
	}
	
	if (error_string.length == "") return true;
	else {
			error_string = "All questions must be answered before you submit.\nThe following questions have not been answered:\n" + error_string;
			alert(error_string);
			return false;
	}
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

addLoadEvent(prepareTextSizeIncreaser);
addLoadEvent(prepareTextSizeDecreaser);
addLoadEvent(prepareTextSizeResetter);
addLoadEvent(prepareLinkedImages);