var AspoIreland = {
	HAL:function(id) {
		new Effect.Highlight( $( id ) );
		$( id ).focus();
	},
	Forms : {
		ValidateContactForm:function() {
			if( $("contactName").value.blank() ) {
				AspoIreland.HAL("contactName");
				return false;
			}
			if( $("contactEmail").value.blank() ) {
				AspoIreland.HAL("contactEmail");
				return false;
			}
			if( $("contactPhone").value.blank() ) {
				AspoIreland.HAL("contactPhone");
				return false;
			}
			if( $("yourEnquiry").value.blank() ) {
				AspoIreland.HAL("yourEnquiry");
				return false;
			}
			return true;
		}
	}
}

Comments = {
	Validate:function() {
		if( $("commentBody").value.blank() ) {
			new Effect.Highlight( $("commentBody") );
			$("commentBody").focus();
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, onSuccess:Comments.Validate_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "commentForm" ) )});
		return false;
	},
	Validate_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		$("commentHolder").update( z.contentHTML );
		if( $("commentBody") ) {
			$("commentBody").focus();
		}
	},
	ValidateLoginForm:function() {
		if( $("memberEmailAddress").value.blank() ) {
			new Effect.Highlight( $("memberEmailAddress") );
			$("memberEmailAddress").focus();
			return false;
		}
		if( $("memberPassword").value.blank() ) {
			new Effect.Highlight( $("memberPassword") );
			$("memberPassword").focus();
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, onSuccess:Comments.ValidateLoginForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "loginForm" ) )});
		return false;
	},
	ValidateLoginForm_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		if( z.result ) {
		
			$("commentHolder").update( z.contentHTML );
			
			LightboxHolder.Close();
			if( $("commentBody") ) {
				$("commentBody").focus();
			}
		} else {
			alert(z.message);
		}
	},
	ShowLogin:function(id) {
		var url = "index.cfm?action=Comments_ShowLogin&id="+id;
		new Ajax.Request( url, {asynchronous:true, onSuccess:Comments.ShowLogin_onSuccess, onFailure:errFunc});
	},
	ShowLogin_onSuccess:function(t) {		
		z = parseJSON( t.responseText );				
		LightboxHolder.DestroyCreate("LightboxHolder");
		$("LightboxHolder").update(z.lightboxHTML);
		Lightbox.showBoxByID( "LightboxHolder", 500, 400, false );
		if( $("memberEmailAddress") ) {
			$("memberEmailAddress").focus();
		}
	},
	ShowCreateAccount:function(id) {
		var url = "index.cfm?action=Comments_ShowCreateAccount&id="+id;
		new Ajax.Request( url, {asynchronous:true, onSuccess:Comments.ShowCreateAccount_onSuccess, onFailure:errFunc});
	},
	ShowCreateAccount_onSuccess:function(t) {
		z = parseJSON( t.responseText );				
		LightboxHolder.DestroyCreate("LightboxHolder");
		$("LightboxHolder").update(z.lightboxHTML);
		Lightbox.showBoxByID( "LightboxHolder", 500, 400, false );
		if( $("memberFirstName") ) {
			$("memberFirstName").focus();
		}
	},
	ValidateCreateAccountForm:function() {
		if( $("memberFirstName").value.blank() ) {
			new Effect.Highlight( $("memberFirstName") );
			$("memberFirstName").focus();
			return false;
		}
		if( $("memberLastName").value.blank() ) {
			new Effect.Highlight( $("memberLastName") );
			$("memberLastName").focus();
			return false;
		}
		if( $("memberEmailAddress").value.blank() ) {
			new Effect.Highlight( $("memberEmailAddress") );
			$("memberEmailAddress").focus();
			return false;
		}
		if( $("memberPassword").value.blank() ) {
			new Effect.Highlight( $("memberPassword") );
			$("memberPassword").focus();
			return false;
		}
		if( $("memberPassword2").value.blank() ) {
			new Effect.Highlight( $("memberPassword2") );
			$("memberPassword2").focus();
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, onSuccess:Comments.ValidateCreateAccountForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "createAccountForm" ) )});
		return false;
	},
	ValidateCreateAccountForm_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		if( z.result ) {			
			LightboxHolder.Close();
			alert(z.message);
		} else {
			alert(z.message);
		}
	}
}

var LightboxHolder = {	
	DestroyCreate:function( id ) {
		if( $('boxContents') ) {
			$('boxContents').update("&nbsp;")
		}
		
		if( $( id ) ) {
			$( id ).remove();			
		}
		if( !$( id ) ) {
			Element.extend( document.getElementsByTagName("body")[0] ).insert("<div id='" + id + "' style='display:none'></div>");
		}
	},
	Close:function() {
		Element.hide('box');
		Element.hide('overlay');
	}
}

var TagLib = {
	AutoComplete : {
		Create : function() {
			
			if( $("query") ) {
				new Autocomplete("query", { 
					serviceUrl	:	"index.cfm?action=suggestTags" ,
					onSelect	:	function(data,value){										
										TagLib.AutoComplete.Load( data );
									}
				});
				$("query").focus();
			}
		},
		Load:function( data ) {
			var tagString = currentTags + escape( "," + data );
			document.location = "index.cfm?page=" + currentViewPage + "&tags=" + tagString;
		}
	}
}


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();
}