var Hahnel = {
	HighlightAndFocus:function(id) {
		new Effect.Highlight(id);
		$(id).focus();
	},
	ContactForm : {
		Validate:function() {
			if($("contactName").value.blank()) {
				Hahnel.HighlightAndFocus("contactName");
				return false;
			}
			if($("contactEmail").value.blank() || !$("contactEmail").value.isEmail()) {
				Hahnel.HighlightAndFocus("contactEmail");
				return false;
			}
			if($("contactEmail2").value.blank() || !$("contactEmail2").value.isEmail()) {
				Hahnel.HighlightAndFocus("contactEmail2");
				return false;
			}
			if($("contactEmail").value!=$("contactEmail2").value) {
				Hahnel.HighlightAndFocus("contactEmail");
				return false;
			}
			if($("contactCity").value.blank()) {
				Hahnel.HighlightAndFocus("contactCity");
				return false;
			}
			if($("contactCountry").value.blank()) {
				Hahnel.HighlightAndFocus("contactCountry");
				return false;
			}
			if($("contactMessage").value.blank()) {
				Hahnel.HighlightAndFocus("contactMessage");
				return false;
			}
			return true;
		}
	},
	Products : {
		ValidateEnquiryForm:function() {
			if($("name").value.blank()) {
				Hahnel.HighlightAndFocus("name");
				return false;
			}
			if($("email").value.blank() || !$("email").value.isEmail()) {
				Hahnel.HighlightAndFocus("email");
				return false;
			}
			if($("email2").value.blank() || !$("email2").value.isEmail()) {
				Hahnel.HighlightAndFocus("email2");
				return false;
			}
			if($("enquiry").value.blank()) {
				Hahnel.HighlightAndFocus("enquiry");
				return false;
			}
			if($("email").value!=$("email2").value) {
				Hahnel.HighlightAndFocus("email");
				return false;
			}
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:Hahnel.Products.ValidateEnquiryForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "batteryContactForm" ) )});
			return false;
		},
		ValidateEnquiryForm_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("formHolder").update(z.formHTML);
		}
	},
	Applications : {
		DisplayLaptopBatteryFinder:function(url) {
			var ltbf = window.open (url+"applications/laptopBatteryFinder/index.cfm","ltbf","menubar=0,resizable=1,scrollbars=yes,width=600,height=600"); 
		},
		DisplayDigitalCameraBatteryFinder:function(url) {
			var dcbf = window.open (url+"applications/digitalCameraBatteryFinder/index.cfm","dcbf","menubar=0,resizable=1,scrollbars=yes,width=600,height=600"); 
		},
		DisplayCamcorderBatteryFinder:function(url) {
			var ccbf = window.open (url+"applications/camcorderBatteryFinder/index.cfm","ccbf","menubar=0,resizable=1,scrollbars=yes,width=600,height=600"); 
		}
	},
	Tabs : {
		ToggleTab:function(id) {
			for(i=1;i<=3;i++) {
				if($("tab"+i)) {
					$("tab"+i).removeClassName("sel");
					$("tab"+i+"Content").removeClassName("productTabOn");
					$("tab"+i+"Content").addClassName("productTabOff");
				}
			}
			$("tab"+id).addClassName("sel");	
			$("tab"+id+"Content").removeClassName("productTabOff");
			$("tab"+id+"Content").addClassName("productTabOn");	
		}
	},
	FeaturedProductsWidget: {
		CurrentRow:1,
		FadeTime:5,
		ResumeFading:25,
		PE:null,
		Show:function(id,index) {
			if(Hahnel.FeaturedProductsWidget.PE!=null) {
				Hahnel.FeaturedProductsWidget.PE.stop();
			}
			Hahnel.FeaturedProductsWidget.CurrentRow=index;
			Hahnel.FeaturedProductsWidget.ResumeAfterPeriod();
			Hahnel.FeaturedProductsWidget.HideAndDisplay(id);
			return false;
		},
		HideAndDisplay:function(id) {
			Hahnel.FeaturedProductsWidget.HighlightLink(id);
		},
		HighlightLink:function(id) {
			var selector = "li.sel";
			var objs = $A($("fpul").select(selector));
			objs.each(Hahnel.FeaturedProductsWidget.HideElements);
			$("li_"+id).addClassName("sel");
			Hahnel.FeaturedProductsWidget.ShowProduct(id);
		},
		HideElements:function(name,index) {
			name.removeClassName("sel");
		},
		ShowProduct:function(id) {
			var selector = "div.currentProduct";
			var objs = $A($("productNewsWidget").select(selector));
			objs.each(function(name,index){name.hide()});
			new Effect.Appear($("currentProduct_"+id));
		},
		ResumeAfterPeriod:function() {
			new PeriodicalExecuter(Hahnel.FeaturedProductsWidget.ResumeAfterPeriodStart,Hahnel.FeaturedProductsWidget.ResumeFading);
		},
		ResumeAfterPeriodStart:function(pe) {
			pe.stop();
			Hahnel.FeaturedProductsWidget.Start();
		},
		Start:function() {
			new PeriodicalExecuter(Hahnel.FeaturedProductsWidget.Next,Hahnel.FeaturedProductsWidget.FadeTime);			
		},
		Next:function(pe) {
			Hahnel.FeaturedProductsWidget.PE = pe;
			if(Hahnel.FeaturedProductsWidget.CurrentRow==null) {
				var id = $("fpul").down().id.split("_")[1];
				Hahnel.FeaturedProductsWidget.CurrentRow=0;
			}	else {
				var id = $("fpul").down().next("li",Hahnel.FeaturedProductsWidget.CurrentRow-1).id.split("_")[1];								
			}			
			Hahnel.FeaturedProductsWidget.CurrentRow++;
			if(Hahnel.FeaturedProductsWidget.CurrentRow==$("productNewsWidget").select("li").length) {
				Hahnel.FeaturedProductsWidget.CurrentRow = null;
			}
			Hahnel.FeaturedProductsWidget.HighlightLink(id);
		}
	}
}

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Teamwork Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}