function setCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + days*24*60*60*1000);
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var cookies = document.cookie.split(';');
	for (var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0) == ' ') cookie = cookie.substring(1);
		if (cookie.indexOf(name+'=') == 0)		
			return cookie.substring(name.length+1);
	}
	return null;
}

function deleteCookie(name) {
	setCookie(name, '', -1);
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

var hexChars = "0123456789ABCDEF";
function dec2hex(num) {
	var remainder = num % 16;
	return '' + hexChars.charAt((num - remainder)/16) + hexChars.charAt(remainder);
}

function addHover(target) {
	$$(target).each(function(element) {
		element.onmouseover = function() { this.addClassName('hover'); }
		element.onmouseout = function() { this.removeClassName('hover'); }
	});
}

function monetizeDownload(url, title) {
	var now = new Date();
	var orderID = "" + now.getFullYear() + (now.getMonth() + 1) + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds() + Math.floor(Math.random()*1000);

	pageTracker._addTrans(
		orderID, // order number (required, unique)
		"", // affiliation or store name
		"1", // total value (required)
		"", // tax
		"", // shipping
		"", // city
		"", // state or province
		"" // country
	);

	pageTracker._addItem(
		orderID, // order number (required, unique)
		url, // SKU (stock keeping unit)
		title, // product name
		"resource download", // category
		"1", // unit price (required)
		"1" // quantity (required)
	);
	
	pageTracker._trackTrans();
}

var FormUtils = {
	value: function(field) {
		switch (this.fieldType(field)) {
			case 'radio' :
				for (var ndx = 0; ndx < field.length; ndx++)
					if (field[ndx].checked) return field[ndx].value;
				return false;
			case 'checkbox': return field.checked;
			case 'select-one':
				if (field.selectedIndex == -1) return false;
				return field[field.selectedIndex].value;
			default: return field.value;			
		}
	},

	fieldType: function(element) {
		if (element[0] && element[0].type == 'radio') return 'radio';
		else if (element.type == 'checkbox') return 'checkbox';
		else if (element.type) return element.type;
		else return false;
	},
	
	setSelect: function(element, value) {
		for (var i = 0; i < element.options.length; i++) {
			if (element.options[i].value == value) {
				element.selectedIndex = i;
				break;
			}
		}
	},

	labelFor: function(element) {
		if (!$(element) || !element.id) return false;
		var result = false;
		$A(document.getElementsByTagName('label')).each(function(label) {
			if (label.readAttribute('for') == element.id) result = label;
		});
		return result;
	},

	fixFormLabels: function() {
		// enable label clicks for IE and Safari
		if( document.all || navigator.userAgent.indexOf("Safari") > 0){ 
			var labels = document.getElementsByTagName("label");
			$A(labels).each ( function(label){
				Event.observe(label, "click", function(){
					var target = $(this.getAttribute('for'));
					if(target.type == 'checkbox' || target.type == 'radio')
						target.checked = target.checked == false ? true : false;
					else target.focus();
				});
			});
		}
	},
	
	formatPhone: function(field) {		
		var phone = field.value.replace(/\D+/g,'');
		var p1 = phone.slice(0,3);
		var p2 = phone.slice(3,6);
		var p3 = phone.slice(6,10);

		field.value = "(" + p1 + ") " + p2 + "-" + p3;
		
		return true;
	}
}

/* http://xserve.local/projects/LoudDog/wiki/FormValidator */

FormValidator = Class.create();
FormValidator.prototype = {
	tests: false,
	onSuccess: false,
	onFailure: false,
	header: '',
	listType: 'unordered',
	errors: new Array(),
	serverErrors: new Array(),
	submit: true,
	block: false,
	errorClass: 'invalid',
	scroll: true,
	
	initialize: function(options) {
		if (!FormUtils) alert('ERROR - FormValidator[initialize]\nformUtils.js is required');
		else Event.observe(window, 'load', function() {
			this.form = document.forms[options.form];
			if (this.form) {
				this.form.onsubmit = function() {
					if (this.checkForm())
						this.form.originalSubmit();
					return false;
				}.bind(this);

				this.form.originalSubmit = this.form.submit;
				this.form.submit = this.form.onsubmit;

				this.form.onreset = function() { this.reset(); }

				if (options.output) this.output = $(options.output);
				if (options.tests) this.tests = options.tests;
				if (options.onSuccess) this.onSuccess = options.onSuccess;
				if (options.onFailure) this.onFailure = options.onFailure;
				if (options.submit != null) this.submit = options.submit;
				if (options.errorClass) this.errorClass = options.errorClass;
				if (options.header) this.header = options.header;
				if (options.listType) this.listType = options.listType;
				if (options.scroll != null) this.scroll = options.scroll;

				if (options.serverErrors) {
					var i = 0;
					while (i < options.serverErrors.length) {
						if (options.serverErrors[i] == '') options.serverErrors.splice(i, 1);
						else i++;
					}
					
					this.serverErrors = options.serverErrors;
				}

				if (this.output) {
					if (this.serverErrors && this.serverErrors.length) this.displayErrors();
					else this.output.hide();
				}

				FormUtils.fixFormLabels();
			} else alert('ERROR - FormValidator[initialize]\nUnknown form: ' + options.form);	
		}.bind(this));
	},
	
	displayErrors: function() {
		var response = '';
	
		response += this.header;
		if (this.listType == 'ordered') response += '<ol>';
		else response += '<ul>';
		
		for (var i = 0; i < this.serverErrors.length; i++) if (this.serverErrors[i]) response += '<li>' + this.serverErrors[i] + '</li>';		
		for (var i = 0; i < this.errors.length; i++) response += '<li>' + this.errors[i] + '</li>';
		
		if (this.listType == 'ordered') response += '</ol>';
		else response += '</ul>';
		
		this.output.update(response);
		this.output.style.display = "block";
		if (this.scroll) this.output.scrollTo();		

		this.block = false;
	},
	
	checkForm: function() {
		if (this.block) return false;
		this.block = true;
		
		this.reset();
		
		this.serverErrors = new Array();
	
		if (this.tests)  {
			try { this.tests();	}
			catch(error) { alert('ERROR - FormValidator[tests]\n' + error); }
		}
	
		if (this.errors.length) {
			if (this.output) {
				this.displayErrors();
			} else {
				var response = '';
				for (var i = 0; i < this.errors.length; i++)
					response += this.errors[i] + '\n';
				alert(response);
			}
		
			if (this.onFailure) {
				try { this.onFailure();	}
				catch(error) { alert("ERROR - FormValidator[onFailure]\n" + error);	}
			}
		} else if (this.onSuccess) {
			try { this.onSuccess(); }
			catch(error) { alert("ERROR - FormValidator[onSuccess]\n" + error);	}
			
			if (this.serverErrors.length) this.displayErrors();
		}
		
		return this.submit && this.errors.length == 0;
	},
	
	reset: function() {
		this.errors = new Array();
		
		document.getElementsByClassName(''); // This somehow allows IE7 to use gEBCN on this.form
		var elements = this.form.getElementsByClassName(this.errorClass);
		for (var i = 0; i < elements.length; i++)
			elements[i].removeClassName(this.errorClass);
			
		if (this.output) {
			this.output.style.display = 'none';
			this.output.update('');
		}
	},
	
	runTest: function(errorMessage, test) {
		if (!test) {
			var args = this.runTest.arguments;
			
			if (args.length == 2) this.errors.push(errorMessage);
			else {	
				var invalidated = false;
				for (var i = 2; i < args.length; i++) {
					if (!$(args[i])) alert('ERROR - FormValidator[runTest]\nInvalid field name during test,\n"'+errorMessage+'"'); 
					else if (this.invalidate(args[i])) invalidated = true;
				}
		
				if (invalidated) this.errors.push(errorMessage);
			}
		}
		
		return test;
	},
	
	required: function() {
		var args = this.required.arguments;
		
		if (args.length % 2 == 0) {
			for (var i = 0; i < args.length; i += 2) {
				if (!$(args[i+1])) alert('ERROR - FormValidator[required]\nInvalid field name during test,\n' + args[i] + ' is required');
				else this.runTest(
					args[i] + " is required",
					FormUtils.value(args[i+1]) && FormUtils.value(args[i+1]) != '',
					args[i+1]
				);
			}
		} else alert("ERROR - FormValidator[required]\nRequired takes an even number of parameters.");		
	},
	
	requireOne: function() {
		var args = this.requireOne.arguments;
	
		if (args.length % 2 == 0) {
			var message = 'At least one of the following is required:\n';
			var oneFound = false;
			var elements = Array();

			for (var i = 0; i < args.length; i += 2) {
				if (!$(args[i+1])) alert('ERROR - FormValidator[required]\nInvalid field name during test,\n' + args[i]);
				else {
					message += args[i];
					if (i+2 < args.length) message += ', ';
					if (FormUtils.value(args[i+1]) && FormUtils.value(args[i+1]) != '') oneFound = true;
					elements.push(args[i+1]);
				}
			}
		
			this.runTest(message, oneFound);
		} else alert("ERROR - FormValidator[required]\nRequired takes an even number of parameters.");	
	},
	
	equals: function(msg, field, value) { return this.runTest(msg, FormUtils.value(field) == value, field); },
	validEmail: function(msg, field) { return this.runTest(msg, this.regExp(/^[\w_\.-]+@[\w_-]+(\.[\w_-]+)+$/, FormUtils.value(field)), field); },
	validPassword: function(msg, field) { return this.runTest(msg, this.regExp(/^\S+$/, FormUtils.value(field)), field); },
	validPhone: function(msg, field) { return this.runTest(msg, this.regExp(/^\d{10}$/, FormUtils.value(field).replace(/\D+/g,'')), field); },
	validZipCode: function(msg, field) { return this.runTest(msg, this.regExp(/^\d{5}([\ -]\d{4})?$/, FormUtils.value(field)), field); },
	numeric: function(msg, field) { return this.runTest(msg, this.regExp(/^\-?\d+(\.\d+)?$/, FormUtils.value(field)), field); },
	
	regExp: function(regexp, value) { return regexp.test(value.replace(/^\s+/, '').replace(/\s+$/, '')); },
		
	invalidate: function(element) {
		var elements = new Array();
		
		switch (FormUtils.fieldType(element)) {
			case 'radio' :
				for(var i = 0; i < element.length; i++)
					elements.push(FormUtils.labelFor(element[i]));
				break;
			default:
				elements.push(FormUtils.labelFor(element));
				elements.push(element);
				break;
		}
		
		var invalidated = false;
		for (var i = 0; i < elements.length; i++) {
			if (!elements[i]) invalidated = true;
			else if (!elements[i].hasClassName(this.errorClass)) {
				elements[i].addClassName(this.errorClass);
				invalidated = true;
			} else invalidated = false;
		}
		
		return invalidated;
	}
}

function adWordsTracker() {
	var img = document.getElementById('googleAdWordsTracker');
	if (img) img.src = 'http://www.googleadservices.com/pagead/conversion/1070793317/?label=default&script=0';
}

/***********************************************
* Ultimate Fade-In Slideshow (v1.51): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var fadebgcolor="white";

var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(targetElement, theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder){
this.pausecheck=pause
this.mouseovercheck=0
this.delay=delay
this.degree=10 //initial opacity degree (10%)
this.curimageindex=0
this.nextimageindex=1
fadearray[fadearray.length]=this
this.slideshowid=fadearray.length-1
this.canvasbase="canvas"+this.slideshowid
this.curcanvas=this.canvasbase+"_0"
if (typeof displayorder!="undefined")
theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
this.theimages=theimages
this.imageborder=parseInt(borderwidth)
this.postimages=new Array() //preload images
for (p=0;p<theimages.length;p++){
this.postimages[p]=new Image()
this.postimages[p].src=theimages[p][0]
}
 
var fadewidth=fadewidth+this.imageborder*2
var fadeheight=fadeheight+this.imageborder*2
var html = '';
if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
	html += '<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div></div>';
else
	html += '<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>';
document.getElementById(targetElement).innerHTML = html;

if (iebrowser&&dom||dom) //if IE5+ or modern browsers such as Firefox
this.startit()
else{
this.curimageindex++
setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
}
}

function fadepic(obj){
if (obj.degree<100){
obj.degree+=10
if (obj.tempobj.filters&&obj.tempobj.filters[0]){
if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
obj.tempobj.filters[0].opacity=obj.degree
else //else if IE5.5-
obj.tempobj.style.filter="alpha(opacity="+obj.degree+")"
}
else if (obj.tempobj.style.MozOpacity)
obj.tempobj.style.MozOpacity=obj.degree/101
else if (obj.tempobj.style.KhtmlOpacity)
obj.tempobj.style.KhtmlOpacity=obj.degree/100
else if (obj.tempobj.style.opacity&&!obj.tempobj.filters)
obj.tempobj.style.opacity=obj.degree/101
}
else{
clearInterval(fadeclear[obj.slideshowid])
obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
obj.populateslide(obj.tempobj, obj.nextimageindex)
obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
var slideHTML=""
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML+='</a>'
picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(){
if (this.pausecheck==1) //if pause onMouseover enabled, cache object
var cacheobj=this
if (this.mouseovercheck==1)
setTimeout(function(){cacheobj.rotateimage()}, 100)
else if (iebrowser&&dom||dom){
this.resetit()
var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
crossobj.style.zIndex++
fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
}
else{
var ns4imgobj=document.images['defaultslide'+this.slideshowid]
ns4imgobj.src=this.postimages[this.curimageindex].src
}
this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
}
 
fadeshow.prototype.resetit=function(){
this.degree=10
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
if (crossobj.filters&&crossobj.filters[0]){
if (typeof crossobj.filters[0].opacity=="number") //if IE6+
crossobj.filters(0).opacity=this.degree
else //else if IE5.5-
crossobj.style.filter="alpha(opacity="+this.degree+")"
}
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=this.degree/101
else if (crossobj.style.KhtmlOpacity)
crossobj.style.KhtmlOpacity=this.degree/100
else if (crossobj.style.opacity&&!crossobj.filters)
crossobj.style.opacity=this.degree/101
}
 
 
fadeshow.prototype.startit=function(){
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
this.populateslide(crossobj, this.curimageindex)
if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
var cacheobj=this
var crossobjcontainer=iebrowser? iebrowser["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
}
this.rotateimage()
}
