// Cross browser get window dimensions
function getSize(which) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return (which=='height' ? myHeight : myWidth);
};

// Stop page going off the top of the screen when resized
function topBlocker(){
	var h = getSize('height');
	var wr = document.getElementById('wrapper');
	if(h <= 550){
		wr.className = 'halt';
	} else {
		wr.className = '';
	}
};

// Remove title and alt attributes
function removeAttributes(node) {
  if(node.title){node.title = "";}
  if(node.alt){node.alt = "";}

  var children = node.childNodes;
  for(var i=0; i < children.length; i++) {
    removeAttributes(children[i]);
  };
}

// Open external links in new window
function externalLinks(){
    var c=document.getElementById('container');
    if(c){
        var ls=c.getElementsByTagName('a');
        for(var i=0;i<ls.length;i++){
						var lnk = ls[i].getAttribute('rel');
            if(lnk == 'external'){
	            ls[i].title+=' (external link opens in a new window)';
	            ls[i].onclick = function(){
								window.open(this.href);
								return false;
							};
            }
        };
    }
};

function brochureForm(){
	if(document.getElementById('page_contact_us_request_brochure')){
		var form = document.getElementById('request_brochure');
		
		form.onsubmit = function(){
			resetForm();
			var elems = this.elements;						
			for (var i=0; i < elems.length; i++) {
				if(i == 0){
					var notice = document.createElement('p');
					notice.id = 'error_notice';
					notice.innerHTML = 'Please fill in the highlighted fields';
					document.getElementById('request_brochure').insertBefore(notice,document.getElementById('fields'));
				}
				var e = document.getElementById(elems[i].id);
				if(e.className.search(/required/) != -1 && e.value == ''){
					var errors = true;
					e.parentNode.className = 'error';
					e.parentNode.parentNode.parentNode.className = "errors";	
				}
			};						
			if(errors==true){
				return false;	
			}			
		};
		
		function resetForm(){
			var form = document.getElementById('request_brochure');
						
			var d = form.getElementsByTagName('div');
			for (var i=0; i < d.length; i++) {
				d[i].className = '';
			};

			var p = document.getElementById('error_notice');
			if(p){
				form.removeChild(p);	
			}			
		};
		
	}
};

// Window & Loading Events
window.onresize = function(){
	topBlocker();
};

// DOM Loaded
function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if (_timer) clearInterval(_timer);
	
	// List functions to run on DOM Load
	removeAttributes(document.body);
	externalLinks();
	brochureForm();
	topBlocker();
	// end
};

// DOM Loaded do not edit
/* for Mozilla/Opera9 and Safari */
if (document.addEventListener) {
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)){
		var _timer = setInterval(function(){if(/loaded|complete/.test(document.readyState)){ init(); }}, 0);
		window.onload(init);
	} else {
		document.addEventListener("DOMContentLoaded", init, false);
	}
} else {
	document.write("<script id=__ie_onload defer src=//:><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			this.onreadystatechange = null;
			init(); // call the onload handler
		}
	};
}