var Hahnel = {
	COR : {
		Login : {
			Validate:function() {
				if( $("email").value.blank() ) {
					Hahnel.HighlightAndFocus("email");
					return false;
				}
				if( $("password").value.blank() ) {
					Hahnel.HighlightAndFocus("password");
					return false;
				}
				return true;
			}
		},
		ConfirmForm : {
			Validate:function() {
				$("btnSubmit").disabled = true;
				if( $("orderPO").value.blank() ) {
					alert("Please enter a PO Number of your initials to confirm the order");
					new Effect.Highlight( $("orderPO") );
					$("orderPO").focus();
					$("btnSubmit").disabled = false;
					return false;
				}
				return true;
			}
		},
		Form : {
			validated:true,			
			Validate:function() {
				$("btnSubmit").disabled = true;
				
				Hahnel.COR.Form.validated=true;
				
				var tableTRObjs = $("corGrid").select("TR.row");
				tableTRObjs.each(
					function(name,index) {
						name.removeClassName("error");
						var rowMin = parseInt( name.getAttribute("minQTY") );
						var rowBoxQTY = parseInt( name.getAttribute("corBQTY") );
						var rowId = name.id.split("_")[1];
						var qtyX = parseInt( $("qty_"+rowId).value );						
						if( Hahnel.COR.Form.validated && qtyX!=0 && qtyX%rowBoxQTY!=0) {
							name.addClassName("error");
							alert("You must order quantities in multiples of " + rowBoxQTY);							
							Hahnel.COR.Form.validated=false;													
						}
						if( Hahnel.COR.Form.validated && qtyX!=0 && qtyX<rowMin) {
							name.addClassName("error");
							alert("The minimum order quantity for this product is " + rowMin);							
							Hahnel.COR.Form.validated=false;													
						}
					}
				);
				
				if( !Hahnel.COR.Form.validated ) {
					$("btnSubmit").disabled = false;
					return false;
				}
				
				var minOrder = parseFloat( $("input_minimumOrder").value );
				var orderTotal = parseFloat( $("input_orderValue").value );
				if( orderTotal < minOrder ) {
					alert("There is a minimum order of EUR " + minOrder.toFixed(2) );
					$("btnSubmit").disabled = false;
					return false;
				}
							
				return true;
			}
		},
		Grid : {
			ChangeRow:function(row) {
				var rowObj = $( "row_" + row );
				var qtyObj = $( "qty_" + row );
				var c = 0;
				var e = 0;
				var d = 0;
				var f = 0;
				var g = 0;
				var x = parseInt( qtyObj.value );
				if( !isNaN( x ) ) {
					c = rowObj.getAttribute("corBQTY");
					e = rowObj.getAttribute("corUP");
					d = rowObj.getAttribute("corCBM");
					f = e*x;
					g = (x/c)*d;
					$("row_" + row + "_F").update(f.toFixed(2));
					$("row_" + row + "_G").update(g.toFixed(4));
					$("input_" + row + "_F").value=f.toFixed(2);
					$("input_" + row + "_G").value=g.toFixed(4);
					Hahnel.COR.Grid.UpdateTotals();
				} else {
					qtyObj.value = 0;
					new Effect.Highlight( qtyObj );
				}				
			},
			UpdateTotals:function() {
				var XTotal = 0;
				var FTotal = 0;
				var GTotal = 0;
				var XInputs = $A( $$("input.X") ).map(Element.extend).each( function(name,index){ XTotal+= parseInt( name.value ) });
				var FInputs = $A( $$("input.F") ).map(Element.extend).each( function(name,index){ FTotal+= parseFloat( name.value ) });
				var GInputs = $A( $$("input.G") ).map(Element.extend).each( function(name,index){ GTotal+= parseFloat( name.value ) });
				
				$("orderQTY").update( parseInt( XTotal ) );
				$("orderValue").update( FTotal.toFixed(2) );
				$("orderCBM").update( GTotal.toFixed(4) );
				$("input_orderQTY").value = parseInt( XTotal );
				$("input_orderValue").value = FTotal.toFixed(2);
				$("input_orderCBM").value = GTotal.toFixed(4);
			},
			CheckKey:function(e,id,currentrow) {
				var qtyObj = $( "qty_" + id );
											
				switch(e.keyCode) {
					case 40:
						var xObj = Element.extend( $("corGrid").rows[currentrow+1] );
						if( qtyObj.value.blank() ) {
							qtyObj.value="0";
						}
					break
					case 38:
						var xObj = Element.extend( $("corGrid").rows[currentrow-1] );
						if( qtyObj.value.blank() ) {
							qtyObj.value="0";
						}
					break
					case 13:
						var xObj = Element.extend( $("corGrid").rows[currentrow+1] );
						if( qtyObj.value.blank() ) {
							qtyObj.value="0";
						}
					break
				} 
				try {
					xObj.down(".X").activate();
					if( xObj.down(".X").value=="0" ) {
						xObj.down(".X").value="";							
					}
				} catch(ex) {
								
				}			
				if( ( e.keyCode < 48 || e.keyCode > 57) && ( e.keyCode < 96 || e.keyCode > 105) && e.keyCode != 9 && e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 46 && e.keyCode != 8 && e.keyCode != 39 && e.keyCode != 37 ) {
					e.returnValue=0;
				}				
			}			
		}
	},
	Distributers : {
		Links : {
			DeleteLink:function(id,argType) {
				var confirmation = confirm("Are you sure you want to delete this link?");
				if(confirmation) {
					var url = "index.cfm?action=Distributers_Links_DeleteLink&id="+id+"&type="+argType;
					new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:Hahnel.Distributers.Links.DeleteLink_onSuccess, onFailure:errFunc});
				}
			},
			DeleteLink_onSuccess:function(t) {
				z = parseJSON( t.responseText );
				if( $("Links_" + z.argType) ) {
					$("Links_" + z.argType).update( z.linksHTML );
				}	
				if( $("addLink_" + z.argType) ) {
					$("addLink_" + z.argType).update( z.addLinksHTML );
				}
				$( z.argType + "_partnerLink").activate();					
			},
			AddLink:function(argType) {
				var cId = $( argType + "_masterBatteryId").value;
				var cLink = $( argType + "_partnerLink").value;
				
				if( cLink.blank() ) {
					Hahnel.HighlightAndFocus( argType + "_partnerLink" );
				} else {
					var url = "index.cfm?action=Distributers_Links_AddLink&id="+cId+"&type="+argType + "&cLink=" + escape(cLink);
					new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:Hahnel.Distributers.Links.AddLink_onSuccess, onFailure:errFunc});
				}				
			},
			AddLink_onSuccess:function(t) {
				z = parseJSON( t.responseText );
				if( z.error ) {
					alert( z.error );
					Hahnel.HighlightAndFocus( z.argType + "_partnerLink" );
				} else {
					if( $("Links_" + z.argType) ) {
						$("Links_" + z.argType).update( z.linksHTML );
					}	
					if( $("addLink_" + z.argType) ) {
						$("addLink_" + z.argType).update( z.addLinksHTML );
					}
					$( z.argType + "_partnerLink").activate();
				}					
			}
		},
		Tabs : {
			ToggleTab:function(id) {
				for(i=1;i<=5;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");	
			}
		},
		ValidateLogin:function() {
			if( $("username").value.blank() ) {
				Hahnel.HighlightAndFocus("username");
				return false;
			}
			if( $("password").value.blank() ) {
				Hahnel.HighlightAndFocus("password");
				return false;
			}
			return true;
		}
	},
	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($("town").value.blank()) {
				Hahnel.HighlightAndFocus("town");
				return false;
			}
			if($("country").value.blank()) {
				Hahnel.HighlightAndFocus("country");
				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<=5;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();
}