var ProLetterFusion = {
	CreateCampaign : {
		ShowPersonaliseOptions:function() {
			new Effect.BlindDown($("personaliseOptions"),{ duration: 0.4 });
		},
		HidePersonaliseOptions:function() {
			new Effect.BlindUp($("personaliseOptions"),{ duration: 0.4 });
		}
	},
	ListFilter : {
		SelectAll:function() {
			var els = $("filterMoverForm").select(".cb");
			for(i=0;i<els.length;i++) {
				if(els[i].checked) {
					els[i].checked = false;
				} else {
					els[i].checked = true;
				}
			}
			return false;
		}
	},
	Purge : {
		DeleteSubscribers:function() {
			var cCount = 0;
			var els = $("filterMoverForm").select(".cb");
			for(i=0;i<els.length;i++) {
				if(els[i].checked) {
					cCount++;
				}
			}
			if(cCount==0) {
				alert("Please choose at least one deleted subscriber to permanently delete");
			} else {
				var confirmation = confirm("Are you sure you wish to permanently delete these " + cCount + " subscribers?");
				if(confirmation) {
					$("formAction").value="viewListMovePermanentDelete";
					document.filterMoverForm.submit();
				}
			}
		}
	},	
	Campaigns : {
		DeleteCampaign:function(id) {
			var confirmation = confirm("Are you sure you wish to delete this campaign?\n\nDeleting a campaign that has been sent will also delete all reports attached to it.");
			if(confirmation) {
				var url = "index.cfm?action=Ajax_Campaigns_DeleteCampaign&id="+id;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Campaigns.DeleteCampaign_onSuccess, onFailure:errFunc});
			}
		},
		DeleteCampaign_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
		}
	},
	SnapShot : {
		ToggleImageEmbedding:function(cUID) {
			var cEmbed = $("cbei").checked;
			var url = "index.cfm?action=SnapShot_ToggleEmbedImages&cUID="+cUID+"&embed="+cEmbed;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.SnapShot.ToggleImageEmbedding_onSuccess, onFailure:errFunc});
		},
		ToggleImageEmbedding_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.embed==true) {
				$("refExternal").hide();
				$("refInternal").show();
				new Effect.Highlight($("refInternal"));
			} else {
				$("refExternal").show();
				$("refInternal").hide();
				new Effect.Highlight($("refExternal"));
			}	
		}
	},
	Templates : {
		ToggleTemplateAccess:function(userId,templateId) {
			var url = "index.cfm?action=Templates_ToggleUserAccess&tid="+templateId+"&uId="+userId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Templates.ToggleTemplateAccess_onSuccess, onFailure:errFunc});
		},
		ToggleTemplateAccess_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			new Effect.Highlight($("l_"+z.userId+"_"+z.templateId));
		}
	},
	Forms : {
		ShowLightBoxSpinner:function() {	
			if($("btnImport")) {
				$("btnImport").hide();
			}		
			$("LightBoxHolder").update("<img hspace='10' vspace='10' src='images/lightbox/loading.gif' width='32' height='32' alt='' border='0'> Working....<br><br>");	
			MFLightbox.showBoxByID( "LightBoxHolder", 180, 180, false );
			return true;
		}
	},
	Subscribers : {
		DeleteManualSubscriber:function(sId,csId,cId) {
			var confirmation = confirm("Are you sure you want to remove this subscriber from this campaign?");
			if(confirmation) {
				var url = "index.cfm?action=Ajax_Subscribers_DeleteManualSubscriber&sid="+sId+"&csId="+csId+"&cId="+cId;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Subscribers.DeleteManualSubscriber_onSuccess, onFailure:errFunc});			
			}
		},
		DeleteManualSubscriber_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
		}
	},
	Clients : {
		EditClient:function(id) {
			var url = "index.cfm?action=Ajax_Clients_EditClient&id="+id;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Clients.EditClient_onSuccess, onFailure:errFunc});			
		},
		EditClient_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("LightBoxHolder").update(z.formHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 600, 400, false );
			if($("clientName")) {
				$("clientName").focus();
			}
		},
		ValidateEditClient:function() {
			if($("clientName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("clientName");
				return false;
			}
			$("btnEditClient").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Clients.ValidateEditClient_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "editClientForm" ) )});
			return false;
		},
		ValidateEditClient_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
			ProLetterFusion.CloseLightBox();
		},
		AddClient:function() {
			var url = "index.cfm?action=Ajax_Clients_AddClient";
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Clients.AddClient_onSuccess, onFailure:errFunc});			
		},
		AddClient_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("LightBoxHolder").update(z.formHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 600, 400, false );
			if($("clientName")) {
				$("clientName").focus();
			}
		},
		ValidateAddClient:function() {			
			if($("clientName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("clientName");
				return false;
			}
			$("btnCreateClient").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Clients.ValidateAddClient_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "addClientForm" ) )});
			return false;
		},
		ValidateAddClient_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
			ProLetterFusion.CloseLightBox();
		},
		DeleteClient:function(id) {
			var confirmation = confirm("Are you sure you wish to delete this client?");
			if(confirmation) {
				var url = "index.cfm?action=Ajax_Clients_DeleteClient&id="+id;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Clients.DeleteClient_onSuccess, onFailure:errFunc});			
			}
		},
		DeleteClient_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
		}
	},
	Users : {
		ToggleClientAccess:function(userId,clientId) {
			var url = "index.cfm?action=Users_ToggleClientAccess&cid="+clientId+"&uId="+userId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.ToggleClientAccess_onSuccess, onFailure:errFunc});
		},
		ToggleClientAccess_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			new Effect.Highlight($("l_"+z.userId+"_"+z.clientId));
		},
		EditUser:function(id) {
			var url = "index.cfm?action=Ajax_Users_EditUser&id="+id;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.EditUser_onSuccess, onFailure:errFunc});			
		},
		EditUser_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("LightBoxHolder").update(z.formHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 600, 400, false );
			if($("firstName")) {
				$("firstName").focus();
			}
		},
		ValidateEditUser:function() {
			if($("firstName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("firstName");
				return false;
			}
			if($("lastName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("lastName");
				return false;
			}
			if($("loginName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("loginName");
				return false;
			}
			if($("emailAddress").value.blank() || !$("emailAddress").value.isEmail()) {
				ProLetterFusion.HighlightAndFocus("emailAddress");
				return false;
			}
			if($("password").value.blank()) {
				ProLetterFusion.HighlightAndFocus("password");
				return false;
			}
			if($("password2").value.blank()) {
				ProLetterFusion.HighlightAndFocus("password2");
				return false;
			}
			if($("password").value!=$("password2").value) {
				alert("The password does not match the confirmation password");
				return false;
			}
			$("btnEditUser").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.ValidateEditUser_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "editUserForm" ) )});
			return false;
		},
		ValidateEditUser_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
			ProLetterFusion.CloseLightBox();
		},
		AddUser:function() {
			var url = "index.cfm?action=Ajax_Users_AddUser";
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.AddUser_onSuccess, onFailure:errFunc});			
		},
		AddUser_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("LightBoxHolder").update(z.formHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 600, 400, false );
			if($("firstName")) {
				$("firstName").focus();
			}
		},
		ValidateAddUser:function() {			
			if($("firstName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("firstName");
				return false;
			}
			if($("lastName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("lastName");
				return false;
			}
			if($("loginName").value.blank()) {
				ProLetterFusion.HighlightAndFocus("loginName");
				return false;
			}
			if($("emailAddress").value.blank() || !$("emailAddress").value.isEmail()) {
				ProLetterFusion.HighlightAndFocus("emailAddress");
				return false;
			}
			if($("password").value.blank()) {
				ProLetterFusion.HighlightAndFocus("password");
				return false;
			}
			if($("password2").value.blank()) {
				ProLetterFusion.HighlightAndFocus("password2");
				return false;
			}
			if($("password").value!=$("password2").value) {
				alert("The password does not match the confirmation password");
				return false;
			}
			$("btnCreateUser").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.ValidateAddUser_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "addUserForm" ) )});
			return false;
		},
		ValidateAddUser_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
			ProLetterFusion.CloseLightBox();
		},
		DeleteUser:function(id) {
			var confirmation = confirm("Are you sure you wish to delete this user?");
			if(confirmation) {
				var url = "index.cfm?action=Ajax_Users_DeleteUser&id="+id;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ProLetterFusion.Users.DeleteUser_onSuccess, onFailure:errFunc});			
			}
		},
		DeleteUser_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("contentHTML").update(z.contentHTML);
		}
	},
	HighlightAndFocus:function(id) {
		new Effect.Highlight($(id));
		$(id).focus();
	},
	CloseLightBox:function() {
		Element.hide('box');
		Element.hide('overlay');
	}
}

function removeElement(id){
	return Element.remove( id );
}
function showElement(id){
	return Element.show( id );
}

function createErrorMessage(argText) {
	if($("error")==null) {
		var err = document.createElement("P");
		err.innerHTML = argText;
		err.id = "error";
		$("messageHolder").appendChild(err);
	} else {
		$("error").innerHTML = argText;
	}
}

function previewCampaignHTML(campaignUID) {
	var width=700;
	var height=650;
    var sLeft=(screen.width-width)/2;
	var sTop=(screen.height-height)/2;
	
	var params=eval("'height="+height+", width="+width+", top="+sTop+", left="+sLeft+", scrollbars=yes'");

	var previewWindow = window.open("pages/popup/popUp_viewCampaignPreview.cfm?id="+escape(campaignUID), 'previewWindow', params);
}

function previewCampaignText(campaignUID) {
	var width=700;
	var height=650;
    var sLeft=(screen.width-width)/2;
	var sTop=(screen.height-height)/2;
	
	var params=eval("'height="+height+", width="+width+", top="+sTop+", left="+sLeft+", scrollbars=yes'");

	var previewWindow = window.open("pages/popup/popUp_viewCampaignPreviewText.cfm?id="+escape(campaignUID), 'previewWindow', params);
}

function hm() {
	new Effect.SwitchOff($("message"));
}

function he() {
	new Effect.Fade($("error"));
}

var gridTable = {
	mon:function(obj){
		obj.className = "high";
	},
	mout:function(obj,cClass) {
		obj.className = cClass;
	}
}

var filterTable = {
	mon:function(obj){
		obj.className = "high";
	},
	mout:function(obj,cClass) {
		obj.className = cClass;
	}
}

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;'>ProLetterFusion Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}
