window.onload = function() {

/*===================================================================================
  AJAX Press Signup Form
===================================================================================*/

function validateEmail(field){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)){
		//alert('test');
		return (true)
	}else{
		return (false)
	}
}

if ($('pressreleases2')) {

	function submit(event){
    	if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
        $('pressreleases2').send({
            onRequest: function(){
                // Show loading div.
                $('q1').setProperty('value','Sending...');
            },
            onSuccess: function(){
                // Hide loading and show success for 3 seconds.
                $('q1').setProperty('value','Thank you for signing up.');

            },
            onFailure: function(){
                // Hide loading and show fail for 3 seconds.
                $('q1').setProperty('value','Please provide a valid email address.');
                return false;
            }
        });
    }

	 $('send').addEvent('click',function(event) { 
        submit(event);
    });
    
    $('q1').addEvent('keydown', function(event){
		var field = $('q1');
		var validateField = validateEmail(field);
		
		if (event.keyCode == 13 && validateField && field != "") { 
			submit(event);
		}
	});
    
}
	
	dropDowns();
	if ($('photo-info')) {$('photo-info').style.display = 'block'; photoInfo();}
	if ($('search-text')){
		$('search-text').addEvent('focus', function() { if (this.getValue() == 'Search') this.removeProperty('value') });
	}
	if ($('toggle')) {readMore();}
	stripeRows();
	if ($('email')) { $('email').addEvents({
		'focus': function(){ if (this.value == 'Your Email Address' ) this.value = ''; }, 
		'blur': function(){  if (!this.value) this.value = 'Your Email Address';  }
	});}
	if ($('drop-shadow-l') && $('drop-shadow-r')) {setTimeout(dropShadows, 10)};
	if (!$('home')) setTimeout(wingsOfFury, 10);
	if ($('tabs')) { tabApp = new neatTabs(); 	setTabs(); }
	if (window.ie6 && $('bottom-boxes-inner')) {
		//Hover state of PNGS for IE6
		var pTags = $$('#bottom-boxes-inner p.linked')
			for (var i=0; i < pTags.length; i++) {
				pTags[i].onmouseover = function() { this.className += ' over'; }
				pTags[i].onmouseout = function() { this.className = 'linked'; }
			}
		}
		//hover for mainnav
		var dropDownLinks = $$('#mainnav a');
		for (var i=0; i < dropDownLinks.length; i++) {
			dropDownLinks[i].addEvent('mouseover', function(){this.style.color = '#999'});
			dropDownLinks[i].addEvent('mouseout', function(){this.style.color = '#fff'});
		};
		
	//fix to make the location inline with the first paragraph	
	if ($$('body').hasClass('pressdetail') == 'true') { 
		if ($$('.press-detail-text p')[0]) $$('.press-detail-text p')[0].style.display = 'inline';
		// if ($$('.press-detail-text p')[1]) $$('.press-detail-text p')[1].style.display = 'inline';
	}
	
	if ($('doc_toggle')) {selectDoc();}
	if ($('decline_nda')) {ndaMsg()};
	
	if($('registration')){
	// if on the extranet page add the 'regForm' id to the form
		var formEl = $(document.body).getElement('form');
		formEl.setProperty('id', 'regForm');
		
		var fieldNames = $$('#regForm input.req').getProperty('name'); // find all the required input fields names
		var formElem  = $$('#regForm input.req'); // find al the required input fields
		var selectNames = $$('#regForm select.req').getProperty('name');
		//var 
		
		$$('#regForm select.req').setProperty('id', selectNames);
		
			
		formElem.each(function(el, index){
			el.setProperty('id', fieldNames[index]);
		});	
	}

	if ($('regForm')) {
			// setTimeout("googleAutoFill()",1000);
			$('regForm').addEvent('submit', function(e) {
			
			new Event(e).stop()
			
			getFields();

			if (checkSelect() && checkEmpty($('firstName')) && checkEmpty($('lastName')) && checkEmpty($('company')) && checkEmpty($('address')) && checkEmpty($('city')) && checkEmpty($('stateProvince')) && checkSpecial($('email')) && checkEmpty($('zip')) && checkSpecial($('phone')) && checkSpecial($('phone'))) {

				$('regForm').submit();   
			} else {
				$('has_errors').style.display = 'block';
				
				getFields();			
			}
		});
	}

	if(window.ie6) {
		$$('#content a').setStyle('position','static');
	}

	//hide all regions
	$$('div.location').setStyle('visibility','visible');
	
	if($("getIt")){
		//distributors location select
		$("getIt").addEvent('change', function(){
			// hides all regions
			$$('.location').setStyle('display','none');
			//finds value of selected item
			var region = '#' + this.getValue();
			//shows region that matches selected item
			$$(region).setStyle('display','block');
		});
	}

	//distributors mootabs
	var regions = $$('.regions');
	for (var i=0; i<regions.length; i++) {
		var current = regions[i].getAttribute("id");
		var name = "myTabs" + i;
		name = new mootabs(current);
	}
	
	if($("reg1")){
		//hide all regions
		$$('div.location').setStyle('display','none');
		if (location.hash == "") {
			$("reg1").setStyle('display','block');
		}
	}
	
	//set page if hash entered
	var page = location.hash.substring(1);
	var locations = $$('.location');
	for (var i=0; i<locations.length; i++) {
		var current = locations[i].getAttribute("id");
		if (page == current) {
			$(page).setStyle("display","block");
			document.getElementById('getIt').value = page;
		} else if (page == '') {
			$('reg1').setStyle("display","block");
			document.getElementById('getIt').value = 'reg1';
		}
	}
}

function dropDowns() {
	var mainnav = $('mainnav');
	for (var i=0; i < mainnav.childNodes.length; i++) {
		node = mainnav.childNodes[i];
		if (node.nodeName == 'LI') {
			node.onmouseover = function() { this.className+="over"; }
			node.onmouseout= function() { this.className=""; }
		}
	}
}


function wingsOfFury(){ 
	$('left-wing').style.bottom = '-1px';
	$('right-wing').style.bottom = '-1px';
	$('left-wing').style.display = 'block';
	$('right-wing').style.display = 'block';
}

function dropShadows(modifier) {
	if (modifier) {distFromBottom = 200};
	var headerDivSize = $('header').getSize().size.y;
    var mainDivSize = $('main').getSize().size.y;
    var distFromBottom = 540;
	if (window.ie6) distFromBottom = 538;
    var shadowHeight = (headerDivSize + mainDivSize) - distFromBottom;
		$('drop-shadow-r').style.height = shadowHeight + 'px';
		$('drop-shadow-l').style.height = shadowHeight + 'px';
		$('drop-shadow-r').style.display = 'block';
		$('drop-shadow-l').style.display = 'block';
}

function photoInfo() {
	var mySlide2 = new Fx.Slide2('photo-info', {mode: 'horizontal'});
	mySlide2.hide();
	$('info-expand').addEvent('click', function(e){
		e = new Event(e);
		mySlide2.toggle();
		e.stop();
	});
}

function stripeRows (){
	var tables = $ES('table');
		for (var i=0; i < tables.length; i++) {
			if (tables[i].hasClass('striped')) {
	   		var tableToStripe = tables[i];
	   		var tableRows = tableToStripe.getElementsByTagName('tr');
	   		for (var e=0; e < tableRows.length; e++) {
				if (e !=0) {
					tableRows[e].className = "alt";
		   			e++;	
				}
	   			
	   		}
		}
	}

}

//Tabs
function neatTabs() {
	correctTitle = document.title;
	this.navToNew = function(newVal) {
		newVal = newVal.hash.replace('#','');
		unFocus.History.addHistory(newVal);
	};
	// This is the method that will recieve notifications from the History Keeper,
	// which will then update the state of the app.
	// :NOTE: This will be called when a new entry is added.

	this.historyListener = function(historyHash) {
		// update the stateVar
		stateVar = historyHash;
		// update display content
		show(historyHash);
		selectedState('n-' + historyHash);
		setTimeout(dropShadows, 10)
		// document.title = "unFocus History Keeper - Current History: " + historyHash;
		// document.title = "unFocus History Keeper - Current History: " + historyHash;
		if (window.ie) {document.title = correctTitle;}
		
	};
	// subscribe to unFocus.History
	unFocus.History.addEventListener('historyChange', this.historyListener);

	// Check for an initial value (deep link).
	// In this demo app, the historyListener can handle the task.
	this.historyListener(unFocus.History.getCurrent());

};

var tabApp;

tabDivs = new Array();
tabs = $ES('a', 'tabs');

function setTabs(){
	
	if (window.location.hash == false || window.location.hash == "#") {window.location.hash = "overview"};
	if (window.ie) {document.title = correctTitle;}
	
	hideAll();
	show(unFocus.History.getCurrent());
	var tabs = $ES('a', 'tabs');
	for (var i=0; i < tabs.length; i++) {
		tabs[i].addEvent('click', function(){ hideAll(); tabApp.navToNew(this); show(unFocus.History.getCurrent()); })
	}
}

function hideAll() {
	var tabs = $ES('a', 'tabs');
	for (var i=0; i < tabs.length; i++) {
		tabDivs.push(($(tabs[i]).hash.replace('#','')));
		if (tabDivs[i]) {
		$('t-' + tabDivs[i]).style.display = "none";
		}
	}
}

function show(div) {
	hideAll();
	if (!window.location.hash) {window.location.hash = "overview"};
	if (window.ie) {document.title = correctTitle;}
	if ($('t-' + div)) {
		$('t-' + div).style.display = 'block';
		
		sIFR.replace(thesis, {
		  selector: 'h1',
		  css: {
		    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'font-size': '22px' }
		  },
		  wmode: 'transparent',
		  tuneHeight: '',
		  forceClear: 'true',
		  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]

		});

		sIFR.replace(thesis, {
		  selector: 'h4',
		  css: {
		    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'display': 'block', 
				'font-size': '15px' }
		  },
		  wmode: 'transparent',
		  tuneHeight: '-5',
		  forceClear: 'true',
		  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
		});


		sIFR.replace(thesis, {
		  selector: 'h2',
		  css: {
		    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'display': 'block', 
				'font-size': '20px' }
		  },
		  wmode: 'transparent',
		  tuneHeight: '',
		  forceClear: 'true',
		  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
		});

		sIFR.replace(thesis, {
		  selector: 'h3#video-h3',
		  css: {
		    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'display': 'block', 
				'font-size': '16px' }
		  },
		  wmode: 'transparent',
		  tuneHeight: '-3',
		  forceClear: 'true',
		  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
		});

		sIFR.replace(thesis, {
		  selector: 'h3',
		  css: {
		    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'display': 'block', 
				'font-size': '16px' }
		  },
		  wmode: 'transparent',
		  tuneHeight: '-5',
		  forceClear: 'true',
		  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
		});
	};
	
}

function selectedState(el) {
	var tabsLi = $ES('li', 'tabs');
	for (var i=0; i < tabsLi.length; i++) {	
		tabsLi[i].className = "";
	};
	if ($(el)) {$(el).className = 'selected';};
	
}

//select document forms
function selectDoc() {
	$('doc_toggle').onchange = function() {
		if ($('doc_toggle').value == 'yes' || $('doc_toggle').value == 'true' ) {
			$('doc-request').style.display = 'block';
		}
		
		if ($('doc_toggle').value == 'no' || $('doc_toggle').value == 'false') {
			$('doc-request').style.display = 'none';
		};
		dropShadows();
	}
}

//Form Validation

function getFields() {
	var fields = $$('#regForm input.req');
	var errors = $$('#regForm li.error');
	$('has_errors').style.display = 'none';
	
	for (var i=0; i < errors.length; i++) {
		errors[i].style.display = 'none';
		
	};
	
	for (var i=0; i < fields.length; i++) {
	
		checkEmpty(fields[i]);
		checkSpecial(fields[i]);
	};
	
	
	checkSelect();

	
}

function checkEmpty(field) {
	
	if (field.value.length == 0){
		var showError = field.name + "_err";
		
		$(showError).style.display = 'block';
		$('has_errors').style.display = 'block';
		passed = false;
		return false;
	}
	passed = true;
	return true;
}

function checkSpecial(field) {

	if (field.id == 'email') {
		//check for valid email format
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value))) {
			var showError = field.id + "_err";
			$(showError).style.display = 'block';
			$('has_errors').style.display = 'block';
			// field.style.borderColor = 'red';
			
			return false;
		} 
		return true;
	} 
	
	// else if (field.id == 'zip') {
	// 	if (!(/(^\d+$)|(^\d+\.\d+$)/).test(field.value) || field.value == "") {
	// 		var showError = field.id + "_err";
	// 		$(showError).style.display = 'block';
	// 		$('has_errors').style.display = 'block';
	// 		// field.style.borderColor = 'red';
	// 		
	// 		return false;
	// 		}
	// 	return true;
	// }
	
	else if (field.id == 'phone') {
		var valid = 1;
		var goodChars = "0123456789()-+ ";
		var phone = $('phone').value;
		for (i =0; i <= phone.length -1; i++) {
			if (goodChars.indexOf(phone.charAt(i)) == -1) {
				var showError = field.id + "_err";
				$(showError).style.display = 'block';
				$('has_errors').style.display = 'block';
				// field.style.borderColor = 'red';
				
				return false;
			}
		return true;
		}
		
	}

	else { return true; }
	
}

function checkSelect() {
	if ($('country')) {
		if ($('country').selectedIndex == 3 || $('country').value.length < 2){
			$('country_err').style.display = 'block';
			$('has_errors').style.display = 'block';
			// $('country').style.borderColor = 'red';
			return false;
		}
	}
	return true;
}

function ndaMsg() {
	$('decline_nda').addEvent('click', function(){
		$('decline_nda_msg').style.display = 'block';
	});
}


function readMore() {
	var mySlide = new Fx.Slide('more');
	mySlide.hide();
	$('more').style.display = 'block';
	sIFR.replace(thesis, {
	  selector: 'h3',
	  css: {
	    '.sIFR-root': { 'background-color': 'transparent', 'color': '#ffffff', 'display': 'block', 
			'font-size': '16px' }
	  },
	  wmode: 'transparent',
	  tuneHeight: '-5',
	  forceClear: 'true',
	  ratios: [9,1.27,14,1.19,21,1.16,28,1.13,38,1.12,53,1.11,60,1.1,61,1.11,94,1.1,96,1.09,101,1.1,102,1.09,103,1.1,108,1.09,110,1.1,1.09]
	});
	
	$('toggle').addEvent('click', function(e){
		
		
		
		
		e = new Event(e);
		mySlide.toggle().chain(function(){
			
			
				$('more').style.height='auto';
				
				if($('more').parentNode.style.height != '0px'){
					
					$('more').parentNode.style.height = 'auto';
				} else {
					$('more').parentNode.style.height = '0px';
				}
			}
		);
		e.stop();
		this.toggleClass('closed');
		if (window.ie6) {
			if ($('left-wing').style.display == 'block') {
				$('left-wing').style.display = 'none';
				$('right-wing').style.display = 'none';
			}
	}
	setTimeout('dropShadows()', 1000);	
	});
}