/*
----------------------------------------------------------------------
	BASIC INCLUDE
	Author: Eric Shiarla
	Employed By: Pendergraphics
	
	ABOUT
	Basic functions used throughout the site.
----------------------------------------------------------------------	
*/


function resetHover(el) {
	
	/*  Go through all parent li's and reset their a tags to the default
		style. */
    var cats = document.getElementsByTagName('li');
    for (i = 0; i < cats.length; i++) {
        if (cats[i].className.indexOf("parent") != '-1' && cats[i].className.indexOf("cParent") == '-1') {
			aLink = cats[i].getElementsByTagName('a');
			aLink[0].style.backgroundColor = 'transparent';
			aLink[0].style.color = '#ffffff';			
        }
    }	
}

function hover(el) {
	resetHover(null);
	aLink = el.getElementsByTagName('a');
	aLink[0].style.backgroundColor = '#ffe9d7';
	aLink[0].style.color = '#000000';
}

//
// Dreamweaver jump menu script
//

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

//
// decode()
// Takes an encoded php string and returns the decoded form.
//
function decode(encoded){

  var encodedArray = encoded.split('/');	
  var decoded;
  
  for (var i = 0; i < (encodedArray.length-1); i++) {
  	
	if (i == 0)
		decoded = String.fromCharCode((encodedArray[i]-1));
	else
		decoded += String.fromCharCode((encodedArray[i]-1));
		
  }	

  return document.write(decoded);

}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// Modifications by - http://huddletogether.com/projects/lightbox2/
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}




//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Modifications by - http://huddletogether.com/projects/lightbox2/
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}




//
// validateRequired()
// Cycles through all elements with a class of "reqField", 
// and uses some basic validation.  If the element has an ID of "email"
// or "Email" it will test the value against a regular expression.
//
function validateRequired() {
	
	var fields = document.getElementsByClassName("reqField");
	var agreement = document.getElementById("Yes");
	var error = "The following required fields are incomplete or contain errors.\n\n";
	var returnError = false;
	var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(agreement) {
		if(!agreement.checked) {
			alert("You must agree to the Terms & Conditions before continuing.");
			return false;			
		}
	}
	
	if (fields.length != 0) {
	
		for (var i=0; i<fields.length; i++) {
			
			if (fields[i].value == null || fields[i].value.length == 0) {
				error += " - "+fields[i].id+" is empty\n";
				returnError = true;
			}
			else if (fields[i].id == "email" || fields[i].id == "Email") {
			
				if (!emailFilter.test(fields[i].value)) { 
					error += " - "+fields[i].id+" is invalid\n";
					returnError = true;
				}
			
			}

				
		}		
	
	}
	
	error += "\n\nPlease correct these fields before continuing.";

	if (returnError == true) {
		alert(error);
		return false;
	}
	else {
		return true;
	}
}


//
// clearThis()
// Clears a form element
//
function clearThis(obj) {
	obj.value = "";
}


//
// Generate JavaScript captions from ALT tag
// Foundational code by Dunstan Orchard - 1976design.com
//

function extractImageTitles()
	{
	images = document.getElementsByTagName('img');
	for (var i = 0; i < images.length; i++)
		{
		if (images[i].getAttribute('class') == "caption") {	
			var alt = images[i].getAttribute('alt');
			if ((alt) && (alt != ''))
				{
				if (alt.match('http://', 'i'))
					{
					newlink = document.createElement('a');
					newlink.setAttribute('href', title);
					newlink.setAttribute('alt', ('Go to ' + alt));
					newlink.appendChild(document.createTextNode('Image source'));
					var newdiv = document.createElement('div');
					newdiv.className = 'caption';
					newdiv.appendChild(newlink);
					images[i].parentNode.appendChild(newdiv);
					images[i].removeAttribute('title');
					}
				else
					{
					var newdiv = document.createElement('div');
					newdiv.className = 'caption';
					newdiv.appendChild(document.createTextNode(alt));
					images[i].parentNode.appendChild(newdiv);
					//images[i].removeAttribute('title');
					}
				}
			}
		}
	}

