// the basics
try {
document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {}



/*********************************/
/*********************************/


$(function(){
  $('a.new-window').click(function(){
    window.open(this.href);
    return false;
  });
});

function setActiveStyleSheet(title) {
  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;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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=/";
}

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 doSpecialSubmit() {
	var valid = true;
	for (x=0;x<document.inputform.elements.length;x++) {
		if ((document.inputform.elements[x].name != "phone_ext") && (document.inputform.elements[x].name != "addr2")) {
			if (document.inputform.elements[x].value == "") {
				valid = false;
				}
			if (document.inputform.elements[x].type == "radio" && document.inputform.elements[x].checked && document.inputform.elements[x].value == "notagreed") {
				valid = false;
				}
			}
		}
	if (!valid) {
			alert('Please fill out all required fields and agree to the use statement!');
		} else {
			document.inputform.submit();
		}
	}


var validationErrorMessage = new Object();
validationErrorMessage['required'] = 'This field is required';
validationErrorMessage['numeric'] = 'This field requires a number';
validationErrorMessage['postalcode'] = 'This field must hold a zip code';
validationErrorMessage['pattern'] = 'Pattern incorrect';
validationErrorMessage['email'] = 'Incorrect email address';

var validationFunctions = new Object();
validationFunctions["required"] = isRequired;
validationFunctions["pattern"] = isPattern;
validationFunctions["postalcode"] = isPostCode;
validationFunctions["numeric"] = isnumeric;
validationFunctions["email"] = isEmail;

function isRequired(formField) {
	switch (formField.type) {
		case 'text':
		case 'textarea':
		case 'select-one':
			if (formField.value)
				return true;
			return false;
		case 'radio':
			var radios = formField.form[formField.name];
			for (var i=0;i<radios.length;i++) {
				if (radios[i].checked) return true;
			}
			return false;
		case 'checkbox':
			return formField.checked;
	}	
}

function isPattern(formField,pattern) {
	var pattern = pattern || formField.getAttribute('pattern');
	var regExp = new RegExp(pattern,"");
	var correct = regExp.test(formField.value);
	if (!correct && formField.getAttribute('patternDesc'))
		correct = formField.getAttribute('patternDesc');
	return correct;
}

function isPostCode(formField) {
	return isPattern(formField,"^((\\d{5})|(\\d{5}[- ]?\\d{4}))$"); // get +4 working
	// us zip and zip+4 /(^\d{5}$)|(^\d{5}-\d{4}$)/ or /^[\d]{5}(-[\d]{4})?$/  ^[0-9]{5}(-[0-9]{4})?$
	// orig dutch \\d{5}\\s*\\D{2}
}

function isnumeric(formField) {
	return isPattern(formField,"\\d+");
}

function isEmail(formField) {
        // was: return isPattern(formField,"^\\w*@\\w*\.\\w{2,4}$")
        // added for gmail, etc:
        // NEED TO ADD TWO SLASHED TO ESCAPE!
        return isPattern(formField,"^([a-zA-Z0-9\\.\\+\\-\\_])+@([a-zA-Z0-9\\.\\-\\+])+\\.([a-zA-Z0-9]{2,4})+$");
}

function emptyFunction() {
	return true;
}

/*********************************/

var W3CDOM = document.createElement && document.getElementsByTagName;

function validateForms() {
	if (!W3CDOM) return;
	var forms = document.forms;
	for (var i=0;i<forms.length;i++) {
		forms[i].onsubmit = validate;
	}
}

//addEventSimple(window,'load',validateForms);

function validate() {
	var els = this.elements;
	var validForm = true;
	var firstError = null;
	for (var i=0;i<els.length;i++) {
		if (els[i].removeError)
			els[i].removeError();
		var req = els[i].getAttribute('validation');
		if (!req) continue;
		var reqs = req.split(' ');
		if (els[i].getAttribute('pattern'))
			reqs[reqs.length] = 'pattern';
		for (var j=0;j<reqs.length;j++) {
			if (!validationFunctions[reqs[j]])
				validationFunctions[reqs[j]] = emptyFunction;
			var OK = validationFunctions[reqs[j]](els[i]);
			if (OK != true) {
				var errorMessage = OK || validationErrorMessage[reqs[j]];
				writeError(els[i],errorMessage)
				validForm = false;
				if (!firstError)
					firstError = els[i];
				break;
			}
		}
	}

	if (!validForm) {
		alert("Please fill out all required fields ");
		location.hash = '#startOfForm';
	}
	return validForm;
	
}

function writeError(obj,message) {
	obj.className += ' errorMessage';
	obj.onchange = removeError;
	if (obj.errorMessage || obj.parentNode.errorMessage) return;
	var errorMessage = document.createElement('label');
	errorMessage.className = 'errorMessage';
	errorMessage.setAttribute('for',obj.id);
	errorMessage.setAttribute('htmlFor',obj.id);
	errorMessage.appendChild(document.createTextNode(message));
	obj.parentNode.appendChild(errorMessage);
	obj.errorMessage = errorMessage;
	obj.parentNode.errorMessage = errorMessage;
}

function removeError() {
	this.className = this.className.replace(/errorMessage/,'');
	if (this.errorMessage) {
		this.parentNode.removeChild(this.errorMessage);
		this.errorMessage = null;
		this.parentNode.errorMessage = null;
	}
	this.onchange = null;
}

/************************
Community scroller functions
*************************/

function scrollUp(scrollerCtr) {
	var scrollerCtr = document.getElementById('scrollerContainer');
	topEdge = (parseInt(scrollerCtr.style.top));
	if (topEdge <= -100) {
		knockDown(scrollerCtr);
		topEdge += 95;
		}
	scrollerCtr.style.top = (topEdge-1) + 'px';

}

function knockDown(scrollerCtr) {

//	var comments = scrollerCtr.getElementsByClassName('comment');

	if (scrollerCtr.firstChild.nodeType == 3) scrollerCtr.removeChild(scrollerCtr.firstChild);
	scrollerCtr.appendChild(scrollerCtr.firstChild);
}

function initScroller() {
	
	var scroller = document.getElementById('scroller');
	var scrollerBounds = document.getElementById('scrollerbounds');
	var scrollerCtr = document.getElementById('scrollerContainer');
	
	var pauseP = document.createElement('p');
	pauseP.setAttribute('id', 'pause');
	var pauseA = document.createElement('a');
	pauseA.setAttribute('rel','on');
	pauseA.setAttribute('href', '#');

	pauseA.onclick = function() {
		if (this.getAttribute('rel') == 'on') {
			this.setAttribute('rel','off');
			progress = window.clearInterval(progress);
			var buttonChosen = document.getElementById('pause');
			buttonChosen.setAttribute('id', 'play');
		} else {
			this.setAttribute('rel','on');
			progress = self.setInterval('scrollUp()', 100);
			var buttonChosen = document.getElementById('play');
			buttonChosen.setAttribute('id', 'pause');

		}
		return false;
	}

	var pauseTxt = document.createTextNode('Pause');
	pauseA.appendChild(pauseTxt);
	pauseP.appendChild(pauseA);

	scroller.insertBefore(pauseP, scrollerBounds);
	scrollerBounds.style.overflow = 'hidden';
	
	var progress = self.setInterval('scrollUp()', 100);
	scrollerCtr.style.top = '0px';
}
