var BayRun = {
		ContactForm : {
			Validate:function() {
				if($("contactName").value.blank()) {
					BayRun.HighlightAndFocus("contactName");
					return false;
				}
				if($("contactEmail").value.blank() || !$("contactEmail").value.isEmail()) {
					BayRun.HighlightAndFocus("contactEmail");
					return false;
				}
				if($("contactPhone").value.blank()) {
					BayRun.HighlightAndFocus("contactPhone");
					return false;
				}
				if($("yourEnquiry").value.blank()) {
					BayRun.HighlightAndFocus("yourEnquiry");
					return false;
				}
				return true;
			}
		},
		Registration : {
			Validate : function() {
				if($("registerFirstName").value.blank()) {
					BayRun.HighlightAndFocus("registerFirstName");
					return false;
				}
				if($("registerLastName").value.blank()) {
					BayRun.HighlightAndFocus("registerLastName");
					return false;
				}
				if($("registerEmail").value.blank() || !$("registerEmail").value.isEmail()) {
					BayRun.HighlightAndFocus("registerEmail");
					return false;
				}
				if($("registerPhone").value.blank()) {
					BayRun.HighlightAndFocus("registerPhone");
					return false;
				}
				if($("registerAddress1").value.blank()) {
					BayRun.HighlightAndFocus("registerPhone");
					return false;
				}
				if($("registerTown").value.blank()) {
					BayRun.HighlightAndFocus("registerTown");
					return false;
				}
				if($("registerCity").value.blank()) {
					BayRun.HighlightAndFocus("registerCity");
					return false;
				}
				return true;
			}
		},
		DateChooser : {
			ChangeDate:function(d,m,y) {
				var cDay = $(d).value;
				var cMonth = $(m).value;
				var cYear = $(y).value;
				var url = "index.cfm?action=DateChooser_ChangeDate&d="+cDay+"&m="+cMonth+"&y="+cYear;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BayRun.DateChooser.ChangeDate_onSuccess, onFailure:errFunc});
			},
			ChangeDate_onSuccess:function(t) {
				z = parseJSON(t.responseText);
				$("dobDay").replace(z.newSelectHTML);
			}
		},
		HighlightAndFocus:function(id) {
			new Effect.Highlight(id);
			$(id).focus();
		},		
		InitialiseLightboxHolder:function() {
			if(!$("LightBoxHolder")) {
				$$("body")[0].insert("<div id='LightBoxHolder' style='display:none'>Dan</div>");
			}
		},
		CloseLightBox:function() {
			Element.hide('box');
			Element.hide('overlay');		
		}
}

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;'>Digital Crew Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}