window.addEvent('domready', function() {

	if ($('prodTabs_content_wrap')) {
		var myScrollbars = new Scrollbar($('prodTabs_content_wrap'), {
		    mode:'vertical', // vertical by default
		    buttons: true // true by default


		});
	}


	if ($('prodTabs')) {

		$$('ul#prodTabs li a').each(function(item) {

			//alert(item.get('href'));
			//item.set('href', 'javascript:;');

			item.addEvent('click', function() {


				// get the original HREF
				var link = item.get('href');

				// wipe .htm off the end...
				var arrLink = link.split('.htm')

				// add -js.htm on the end..
				var url = arrLink[0] + '-js.htm';



				// start the loader thinggu here?
				showLoad('prodTabs_content');




				// send the request.
				var htmlReq = new Request.HTML({
					onSuccess: function(responseTree, responseElements, responseHTML) {

						// Turn off any selected tabs.
						$$('ul#prodTabs li a').each(function(item) {


							if (item.getParent('li').hasClass('selected')) {
								//alert('we gots OEN');
								item.getParent('li').removeClass('selected');
							}

						});

						item.getParent('li').set('class', 'selected');


						$('prodTabs_content').empty();


						$('prodTabs_content').set('html', responseHTML);
						myScrollbars.refresh();
					},
					onFailure: function() {
						// the request failed.. lets just forward them on so they are under the
						// impression that nothing was wrong!!
						window.location.href = link;

					}
				}).get(url);





				return false;
			});
		});

	}


	if ($('prodEnquire')) {
		$('prodEnquire').addEvent('click', function() {
			doEnquiry();
			return false;
		});
	}

	var hasSendEvent=false;
	function doEnquiry() {
		if ($('enqDiv')) {

			var popDiv = $('enqDiv');


			// this bit TRICKS ie into working? (I dont know how.. or why)
			try {
				var popFrm = $('enqFrm');
			} catch(err) {
				// do nothing - i like errors.
			}



			var popFrm = $('enqFrm');


			$('popCloseE').addEvent('click', function() {
				hasSendEvent=false;
				hideOverlay(popDiv);
				return false;
			});

			if ($('msgDivE')) {
				$('msgDivE').parentNode.removeChild($('msgDivE'));

				$$('form#enqFrm input').each(function(item) {
					if (item.getPrevious('label')) {
						item.getPrevious('label').setStyle('color', '');
					}
				});
			}



			// Figure out which row we'r in..
			//var row = el.getParent('tr');
			//var tds = row.getChildren('td');






			showOverlay(popDiv);


			$('btnEnq').addEvent('click', function(e) {
				if(hasSendEvent==true) {
					return false;
				} else {

					e = new Event(e);
					var passVal = 1;
					$$('form#enqFrm input').each(function(item) {

						if (item.id == 'firstname' || item.id == 'surname' || item.id == 'email' || item.id == 'organisation' || item.id == 'department' || item.id == 'position') {
							if (item.value.length <= 2) {
								item.getParent('div').getPrevious('label').setStyle('color', 'red');
								passVal = 0;
							} else {
								item.getParent('div').getPrevious('label').setStyle('color', '');
							}
						}

					});


					var title = $('title');
					if (title.value == -1) {
						title.getPrevious('label').setStyle('color', 'red');
						passVal = 0;
					} else {
						title.getPrevious('label').setStyle('color', '');
					}



					if (passVal == 1) {
						hasSendEvent=true;
						var url		= '/process/support-enquiry-js';
						var myForm 	= $('enqFrm');


						var req = new Request({
							url: url,
							method:'get',
							onSuccess: function(responseTree) {
								if (responseTree == 1) {
									// this is good news.. it means the email actually sent..
									showMsg('Your enquiry has been sent successfully', $('enqFrm'), 'top', 'done');
									var elP = new Element('p', {
										'href': 'javascript:;',
										'id':'pConfirmMsg'
									});
									elP.setStyles({
										'display':'block',
										'color':'#FFFFFF',
										'background-color':'#00A77E',
										'padding':'5px',
										'margin-top':'10px',
										'border-bottom':'2px solid #333333'
									});

									elP.set('text', 'Thank you, your enquiry has been sent. ');

									var elA = new Element('a', {
										'href': 'javascript:;'
									});
									elA.setStyles({
										'text-decoration':'underline',
										'color':'#FFFFFF',
										'cursor':'pointer',
										'font-weight':'normal'
									});
									elA.set('text', 'Click here to close the form.');

									elA.addEvent('click', function() {
										hasSendEvent=false;
										hideOverlay(popDiv);
										return false;
									});
									elA.inject(elP, 'bottom');
									elP.inject($('btnEnq'), 'after');


									// clear down some of the values to stop spam sends.
									$('stf_fname').value = '';
									$('stf_femail').value = '';

								} else {
									// failed
									showMsg('There was a problem with one or more fields. Please check that all fields contain valid data and that email addresses have been entered in the correct format.', $('enqFrm'), 'top', 'err');
								}
							},
							onFailure: function() {
								// the request failed.. lets tell them..
								showMsg('There was an error whilst trying to process your request. Please try again and if the problem persists contact the site administrator for assistance.', $('enqFrm'), 'top', 'err');

							}
						}).send($('enqFrm').toQueryString());


						/*
						// This bit taken out because it tripped up IE - replaced with above.
						myForm.set('send', {
							url: url,
							method: 'get',
							onSuccess: function(responseTree) {
								if (responseTree == 1) {
									// this is good news.. it means the email actually sent..
									showMsg('Your enquiry has been sent successfully', $('enqFrm'), 'top', 'done');


									// clear down some of the values to stop spam sends.
									$('stf_fname').value = '';
									$('stf_femail').value = '';

								} else {
									// failed
									showMsg('There was a problem with one or more fields. Please check that all fields contain valid data and that email addresses have been entered in the correct format.', $('enqFrm'), 'top', 'err');
								}
							},
							onFailure: function() {
								// the request failed.. lets tell them..
								showMsg('There was an error whilst trying to process your request. Please try again and if the problem persists contact the site administrator for assistance.', $('enqFrm'), 'top', 'err');

							}
						});
						myForm.send(); //Sends the form.
						*/



					} else {
						showMsg('There was a problem with one or more fields. Please check that all fields contain valid data and that email addresses have been entered in the correct format.', $('enqFrm'), 'top', 'err');
					}

					e.stop();
					return false;
				}
			});
		}

	}



	function showMsg(msg, targetDiv, injectTo, classStr) {

		if ($('msgDivE')) {
			$('msgDivE').parentNode.removeChild($('msgDivE'));
		}

		var errDiv = new Element('div', {
			'html':msg,
			'id':'msgDivE',
			'class':classStr
		});

		errDiv.inject(targetDiv, injectTo);

	}


	function showLoad(wrapEl) {

		// moo it..
		wrapEl = $(wrapEl);

		var wrapDim = wrapEl.getSize();
		var lDiv = new Element('div', {
		    'class': 'loading',
		    'styles': {
			'display': 'block',
			'position': 'absolute',
			'height': wrapDim.y,
			'width': wrapDim.x,
			'top': '0px'
		    },
		    'id': 'loadingDiv'
		});



		lDiv.inject(wrapEl, 'inside');


	}


	function hideLoad(wrapEl) {
		$('loadingDiv').remove()
	}



});
