window.addEvent('domready',function() {
	if ($('faqs')) toggleList();
	if ($('contactform')) formvalidation();
	if ($('signupform')) signupformvalidation();
});

function toggleList() {
	$('faqs').getChildren('li').each(function(el, index) {
		var div = el.getFirst('div');
		var divh = div.getSize().y;
		div.getChildren('p').each(function(el, index) {
			el.setStyles({
				'font-size':'12px',
				'line-height':'16px',
				'padding':'4px 0px'
			});
		});
		
		div.setStyle('height','0');
		var fx = new Fx.Tween(div);
		el.getFirst('a').onclick = function() {
			var h = (div.getSize().y > 0) ? 0 : divh;
			fx.start('height',h);
			return false;
		}
	});
}


function signupformvalidation() {
	$('submit').onclick = function() {
		var name = $('fullname').value;
		var email = $('e').value;
		var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var errorcount = 0;
		
		if (name == 'Please enter your Full Name') $('fullname').onclick = function() { this.set('value',''); }
		if (email == 'Please enter a valid Email Addresss') $('e').onclick = function() { this.set('value',''); }
		
		if (name == '' || name == ' '|| name == 'Please enter your Full Name') {
			errorcount++;
			var fx = new Fx.Tween($('fullname'));
			fx.start('background-color','#BA0000','#FFFFFF');
			$('fullname').set('value','Please enter your Full Name');
		}
		if (email == '' || email == ' ' || !emailExp.test(email) || email == 'Please enter a valid Email Addresss') {
			errorcount++;
			var fx = new Fx.Tween($('e'));
			fx.start('background-color','#BA0000','#FFFFFF');
			$('e').set('value','Please enter a valid Email Addresss');
		}
				
		if (errorcount <= 0) {
			//send information to email.php in the library (php folder)
			var request = new Request({
				method: 'post',
				url: '/library/ajax/email.php',
				data: {
					'e':email,
					'fullname':name,
					'mailinglist':'true'
				},
				onRequest: function() {
					//alert('requesting');
				},
				onComplete: function(response) {
					if (response == 'Success') {
						window.location = "/thank-you/mailing-list/";
					} else {
						window.location = "/error-message/";
					}
				}
			}).send();
		}
		return false;
	}
}

function formvalidation() {
	$('submit').onclick = function() {
		var name = $('fullname').value;
		var email = $('e').value;
		var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var errorcount = 0;
		
		if (name == 'Please enter your Full Name') $('fullname').onclick = function() { this.set('value',''); }
		if (email == 'Please enter a valid Email Addresss') $('e').onclick = function() { this.set('value',''); }
		
		if (name == '' || name == ' '|| name == 'Please enter your Full Name') {
			errorcount++;
			var fx = new Fx.Tween($('fullname'));
			fx.start('background-color','#BA0000','#FFFFFF');
			$('fullname').set('value','Please enter your Full Name');
		}
		if (email == '' || email == ' ' || !emailExp.test(email) || email == 'Please enter a valid Email Addresss') {
			errorcount++;
			var fx = new Fx.Tween($('e'));
			fx.start('background-color','#BA0000','#FFFFFF');
			$('e').set('value','Please enter a valid Email Addresss');
		}
		
		if ($('mailinglistyes').checked) {
			var join = 'Yes';
		} else {
			var join = 'No';
		}
				
		if (errorcount <= 0) {
			//send information to email.php in the library (php folder)
			var request = new Request({
				method: 'post',
				url: '/library/ajax/email.php',
				data: {
					'e':email,
					'fullname':name,
					'industry':$('industry').value,
					'telephone':$('telephone').value,
					'comments':$('comments').value,
					'join':join
				},
				onRequest: function() {
					//alert('requesting');
				},
				onComplete: function(response) {
					if (response == 'Success') {
						window.location = "/thank-you/contact/";
					} else {
						window.location = "/error-message/";
					}
					
				}
			}).send();
		}
		return false;
	}
}


/*
* * * * * * * * * * * * * * * * * * * * *
IE :hover Fix --- taken from A List Apart
* * * * * * * * * * * * * * * * * * * * *
*/
sfHover = function() {
	var sfEls = document.getElementById("mainnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);