/* Basic functions used in the UI or elsewhere */
function confirm_delete(url, label)
{
	if(label == null || label == ''){ label = 'this item'; }
	var response = confirm('Are you sure you want to permanently delete ' + label + '?');
	if(response)
	{
		window.location = url;
	}
}
function confirm_cancel(url, label)
{
	if(label == null || label == ''){ label = 'this item'; }
	var response = confirm('Are you sure you want to cancel ' + label + '?');
	if(response)
	{
		window.location = url;
	}
}

function change_css_class(id, style)
{
	element = document.getElementById(id);
	element.setAttribute("class", style);
	element.setAttribute("className", style);
}
function change_css_visibility(id, mode)
{
	element = document.getElementById(id);
	element.style.visibility = mode;
}

function have_group(fh)
{
	got_group = false;
	for(i=0; i < fh.elements.length; i++) 
	{
		if(fh.elements[i].type == 'checkbox')
		{
			if(fh.elements[i].name.indexOf('group[') == 0)
			{
				if(fh.elements[i].checked)
				{
					got_group = true;
				}
			}
		}
	}
	return got_group;
}

function chooseallcategories(obj, nom)
{
	if(obj.form.elements[nom])
	{
		chk = false;
		label_text = ' Toggle All';
		if (obj.checked){ chk = true; label_text = ' Toggle All'; }
		for(i=0; i < obj.form.elements[nom].length; i++)
		{
			obj.form.elements[nom][i].checked = chk;
		}
		document.getElementById('chooseallcategories').innerHTML = label_text;
	}
	return true;
}
function check_from(fh)
{
	var do_alert = false;
	var matches = fh.from.value.match(/@/g);
	if(fh.from.value != '' && (fh.from.value.indexOf(',') >= 0 || (matches && matches.length >= 2))){ do_alert = true; }
	if(do_alert)
	{
		fh.from.focus();
		fh.from.select();
		alert('It appears you have more than one email address in the From field. Email can only be From one email address. Please adjust this.');
		return false;
	}
	if(fh.from.value.toLowerCase() == fh.to.value.toLowerCase())
	{
		fh.from.focus();
		fh.from.select();
		alert('It appears your From and To addresses are identical. This is typical of spam messages, so please adjust this.');
		return false;
	}
	return true;
}

function change_bgrnd(this_row, checked)
{
	if(checked){ $("#"+this_row).addClass("altnoaccess"); }
	else{ $("#"+this_row).removeClass("altnoaccess");}
}

this.imagePreview = function(){	
		xOffset = 10;
		yOffset = 30;
	$("a.imgpreview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='imgpreview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#imgpreview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#imgpreview").remove();
    });	
	$("a.imgpreview").mousemove(function(e){
		$("#imgpreview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function show_throbber()
{
	$.colorbox({iframe:true, href:'/aw_php/aw/aw_includes/images/ajax-loader.html', width:'50%', height:'50%'});
}

/* COOKIE FUNCTIONS */
function createCookie(name,value,days) 
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0)
		{
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}
