// Filename: Common.js
// Description: Common JavaScript functions used throughout the MailHarbor website
// Author: Ryan Peters

function Get (id)
{
	return document.getElementById(id);
}

function AlertTrialExpired()
{
    alert("Your Free Trial has expired and you may no longer send campaigns.\n\nPlease renew your account to continue.");
}

function ConfirmDelete (text)
{   
    return confirm(text ? text : "MailHarbor Alert:\n\nAre you sure you want to delete this? Once deleted, it is NOT recoverable.\n ");
}

function ConfirmSubmit()
{
    return confirm("MailHarbor Alert:\n\nAre you sure? This action cannot be undone.\n ");	
}

function ConfirmCancel ()
{
	return confirm("MailHarbor Alert:\n\nAre you sure you want to cancel this?\n ");	
}

function ConfirmNewWindow ()
{
	return confirm("This area will open in a new window. Proceed?");	
}

function ConfirmNavigateAway ()
{
	return confirm("Navigate away from this page?\n\nProceed?\n");	
}

function ConfirmImport ()
{
	return confirm("Your are about to import these contacts. This process may take several minutes for large lists.\n\nProceed?\n");
}

function FadeInNotification(id)
{
    $("#" + id).stop().slideDown("slow").fadeTo("fast", 0.5).fadeTo("slow", 1).fadeTo(9000, 1).slideUp("slow");
}

function FadeOutNotification(id)
{
    $("#" + id).stop().slideUp("slow");
}

function ProperName (obj)
{
	var aryName = obj.value.split(" ");
	var strNewName = "";

	for (i=0; i<aryName.length; i++, strNewName += i == aryName.length ? "" : " ")
		for (j=0; j<aryName[i].length; j++)
			strNewName += j == 0 ? aryName[i].substring(j,j+1).toUpperCase() : aryName[i].substring(j,j+1);

	obj.value = strNewName;				
}

function InsertAtCursor (id, value) 
{
	if (value.length > 0)
	{		
		obj = document.getElementById(id);
		
		if (document.selection) 
		{
			obj.focus();
			sel = document.selection.createRange();
			sel.text = value;
		}
		else if (obj.selectionStart || obj.selectionStart == '0') 
		{
			var startPos = obj.selectionStart;
			var endPos = obj.selectionEnd;

			obj.value = obj.value.substring(0, startPos) + value + obj.value.substring(endPos, obj.value.length);
		}
		else 
			obj.value += value;
	}
}

function Redirect (url)
{
	document.location.href=url;
}

function ClearBox (id)
{
	var obj = document.getElementById(id);
	obj.value = "";
	obj.focus();
	obj.blur();
}

// Ryan's fix for background PNG's
function BackgroundPngFix ( obj )
{
	if (navigator.appName == "Microsoft Internet Explorer" && obj)
	{
		// strip out the url
		var tmp_image 	= obj.style.backgroundImage;
		var curr_image 	= tmp_image.substring(4, tmp_image.length - 1);
	
		obj.style.backgroundImage 	= "url(/style/transparent.gif)";
		obj.runtimeStyle.filter 	= "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + curr_image + "',sizingMethod='scale')";
	}
}

function SelectBox (id)
{
	var obj = document.getElementById(id);
	obj.focus();
	obj.select();	
}

function ToggleLoadBar (id)
{
	var load_bar = document.getElementById(id);
	
	load_bar.style.display = load_bar.style.display == "none" ? "" : "none";
}

function SetCookie (name, value, expires, path, domain, secure) 
{
	var curCookie = name + "=" + escape(value) +
					((expires) ? "; expires=" + expires.toGMTString() : "") +
					((path) ? "; path=" + path : "") +
					((domain) ? "; domain=" + domain : "") +
					((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function GetCookie (name) 
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) 
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} 
	else
		begin += 2;
	
	var end = document.cookie.indexOf(";", begin);
	
	if (end == -1)
		end = dc.length;
		
	return unescape(dc.substring(begin + prefix.length, end));
}