// always remove outlines
$("a").each(function(){
	this.onmouseup = this.blur();
});
$(document).ready(function(){
	// open links with the external class in a new window
	$('a.external')
		.attr({
		target: "_blank"
	});
	// open links from other domains in a new window
	var h = window.location.host.toLowerCase();
	$("a[href^='http']:not([href^='http://filterlabs.com']):not([href^='http://www.filterlabs.com']):not([href^='http://manage.filterlabs.com']):not([href^='https://filterlabs.com']):not([href^='https://www.filterlabs.com']):not([href^='https://manage.filterlabs.com']):not([href^='mailto']), a[href$='.pdf']").attr("target", "_blank");
	// date picker
	$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
	$(function($) { $("#startdate").datepicker(); });
	$(function($) { $("#enddate").datepicker(); });
	// text labels
	$('input#s[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}
		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').addClass('focused');
			}
		});
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');
			}
		});
	});
	// image rollovers
	$('.save-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/save_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/save.gif'});
		}
	);
	$('.submit-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/submit_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/submit.gif'});
		}
	);
	$('.change-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/change_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/change.gif'});
		}
	);
	$('.add-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/add_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/add.gif'});
		}
	);
	$('.add-account-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/add-account_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/add-account.gif'});
		}
	);
	$('.add-domain-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/add-domain_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/add-domain.gif'});
		}
	);
	$('.remove-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/remove_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/remove.gif'});
		}
	);
	$('.signup-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/signup_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/signup.gif'});
		}
	);
	$('.purchase-button').hover(
		function(){ // roll over
			$(this).attr({ src : '/images/buttons/purchase_over.gif', alt : '' });
		},
		function(){ // roll off
			$(this).attr({ src : '/images/buttons/purchase.gif'});
		}
	);
	// ajax form submit for adding a user account filter
	$('#newFilter input[type=image]').click(function () {
		$(".filters_msg").show();
		//Get the data from all the fields
		var rulevalue = $('#newFilter input[name=rulevalue]');
		var newrule = $('#newFilter select[name=newrule]');
		var email = $('#newFilter input[name=email]');
		var id = $('#newFilter input[name=id]');
		// form validation
		if (rulevalue.val()=='') {
			//alert('rulevalue');
			return false;
		}
		if (newrule.val()=='') {
			//alert('newrule');
			return false;
		}
		if (email.val()=='') {
			//alert('email');
			return false;
		}
		if (id.val()=='') {
			//alert('id: ' + id.val());
			return false;
		}
		//organize the data properly
		var data = 'page=account&formAction=newFilter&email=' + email.val() + '&newrule=' + newrule.val() + '&rulevalue=' + rulevalue.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
					$('#account-wide-filters').load('/includes/ajax-account-filters.php?email=' + email.val() + '&id=' +id.val());
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for adding a new domain-wide filter
	$('#newGlobalFilter input[type=image]').click(function () {
		//$(".filters_msg").show();
		//Get the data from all the fields
		var rulevalue = $('#newGlobalFilter input[name=rulevalue]');
		var newrule = $('#newGlobalFilter select[name=newrule]');
		var domain = $('#newGlobalFilter input[name=domain]');
		var id = $('#newGlobalFilter input[name=id]');
		// form validation
		if (rulevalue.val()=='') { return false; }
		if (newrule.val()=='') { return false; }
		if (domain.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=settings&formAction=newFilter&domain=' + domain.val() + '&newrule=' + newrule.val() + '&rulevalue=' + rulevalue.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
					//$('.filters_msg').html(html);
					//$('.filters_msg').fadeTo('slow', 1).delay(4000).fadeOut('slow');
					$('#domain-wide-filters').load('/includes/ajax-global-filters.php?id=' + id.val());
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for changing domain password
	$('#newPasswd input[type=image]').click(function () {
		$(".passwd_msg").show();
		//Get the data from all the fields
		var oldpasswd = $('#newPasswd input[name=oldpasswd]');
		var newpasswd = $('#newPasswd input[name=newpasswd]');
		var newpasswd2 = $('#newPasswd input[name=newpasswd2]');
		var id = $('#newPasswd input[name=id]');
		// form validation
		if (oldpasswd.val()=='') { return false; }
		if (newpasswd.val()=='') { return false; }
		if (newpasswd2.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=settings&formAction=newPasswd&id=' + id.val() + '&oldpasswd=' + oldpasswd.val() + '&newpasswd=' + newpasswd.val() + '&newpasswd2=' + newpasswd2.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for changing domain password
	$('#newPasswdByMaster input[type=image]').click(function () {
		$(".passwd_msg").show();
		//Get the data from all the fields
		var newpasswd = $('#newPasswdByMaster input[name=newpasswd]');
		var newpasswd2 = $('#newPasswdByMaster input[name=newpasswd2]');
		var id = $('#newPasswdByMaster input[name=id]');
		// form validation
		if (newpasswd.val()=='') { return false; }
		if (newpasswd2.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=account&formAction=newPasswdByMaster&id=' + id.val() + '&newpasswd=' + newpasswd.val() + '&newpasswd2=' + newpasswd2.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for changing domain smtp route
	$('#newSMTP input[type=image]').click(function () {
		$(".smtp_msg").show();
		//Get the data from all the fields
		var newsmtp = $('#newSMTP input[name=newsmtp]');
		var domain = $('#newSMTP input[name=domain]');
		var id = $('#newSMTP input[name=id]');
		// form validation
		if (newsmtp.val()=='') { return false; }
		if (domain.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=settings&formAction=newSMTP&domain=' + domain.val() + '&id=' + id.val() + '&newsmtp=' + newsmtp.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for changing user filter sensitivity
	$('#filterSensitivity input[type=image]').click(function () {
		$(".sensitivity_msg").show();
		//Get the data from all the fields
		var email = $('#filterSensitivity input[name=email]');
		var required_hits = $('#filterSensitivity input:radio:checked');
		var domain = $('#filterSensitivity input[name=domain]');
		var id = $('#filterSensitivity input[name=id]');
		// form validation
		if (email.val()=='') { return false; }
		if (required_hits.val()=='') { return false; }
		if (domain.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=account&formAction=filterSensitivity&domain=' + domain.val() + '&id=' + id.val() + '&email=' + email.val() + '&required_hits=' + required_hits.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for changing user summary preference
	$('#summaryReport input[type=image]').click(function () {
		$(".summaries_msg").show();
		//Get the data from all the fields
		var summaries = $('#summaryReport select[name=summaries]');
		var id = $('#summaryReport input[name=id]');
		// form validation
		if (summaries.val()=='') { return false; }
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=account&formAction=summaryReport&id=' + id.val() + '&summaries=' + summaries.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
	// ajax form submit for removing an account
	$('#deleteAccount input[type=image]').click(function () {
		//Get the data from all the fields
		var id = $('#deleteAccount input[name=id]');
		// form validation
		if (id.val()=='') { return false; }
		//organize the data properly
		var data = 'page=settings&formAction=newFilter&domain=' + domain.val() + '&newrule=' + newrule.val() + '&rulevalue=' + rulevalue.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/includes/ajax-actions.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if ajax-actions.php returned 1/true
				if (html) {
					// show the results message
					$.jGrowl(html, { life: 6000 });
					//$('.filters_msg').html(html);
					//$('.filters_msg').fadeTo('slow', 1).delay(4000).fadeOut('slow');
					$('#domain-wide-filters').load('/includes/ajax-global-filters.php?id=' + id.val());
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});
});
