function debug( msg ) {
	if( !$('debug') && $('header') )
		new Insertion.Before($('header'), "<div id='debug'></div>");
	if( $('debug') )
		$('debug').innerHTML += "<br/>" + msg;
}

function confirmdelete(url,msg){
	window.status = 'Delete';
	var del= confirm (msg);

	if(del){
		window.location.href = url;
	}
}

function PopWin(url, wname, w, h, pl, pt) {
	var winX = pl;
	var winY = pt;
	// Nav 4 gives true screen pos %, while default assumes % on 640X480
	if (parseInt(navigator.appVersion) >= 4) {
		winX = (screen.availWidth - w)*pl*.01;
		winY = (screen.availHeight - h)*pt*.01;
	}

	pWin = window.open(url, wname,'scrollbars=yes,status=no,dependent,resizable=yes,width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
	if (pWin.focus) {
		pWin.focus();
	}
}

/************** Custom Functions **************/
// trim facility for sting
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function format(num) {
	if(num.length >= 2)	return num;
	return (num < 10) ? '0' + num : num;
}

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(ckName)
{
	var search = ckName + "=";
	var ckValue = "";
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			ckValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return ckValue;
}

function clearCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "; path=/") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function reportError(req) {
	alert("Sorry. There was an error.");
}

// Given  : key is a string of the 26 letters in arbitrary order,
//          message is the string to be encoded using the key
// Returns: the coded version of message using the substitution key
function Encode(message, key) {
	var alphabet, coded, i, ch, index;

	alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
	if( !key )
		key = "DXBqMdtfaxkFW7u5cpuq2JPyGyKcmqTT5XvfxqkSQCwxsdxD2MJjqye76NUb9rB";

	coded = "";
	for (i = 0; i < message.length; i++) {        // for as many letters as there are
		ch = message.charAt(i);                   //   access the letter in the message
		index = alphabet.indexOf(ch);             //   find its position in alphabet
		if (index == -1) {                        //   if it's not a letter,
			coded = coded + ch;                   //     then leave it as is & add
		}                                         //   otherwise,
		else {                                    //     find the corresponding
			coded = coded + key.charAt(index);    //     letter in the key & add
		}
	}

	return coded;
}

// Bookmark the page ( only works in IE & firefox )
function bookmark(url, title){
	if( !title )
		title = document.title;
	if( !url )
		url = document.location.href;

	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

// Hightlight listtable rows on mouseover & click.. Hightlight form elements on mouseover & focus
function hightlight_effects( ) {
	var tables = $A(document.getElementsByTagName("table"));
	if( tables && tables.length > 0 ) {
		tables.findAll( function(tobj) { return ( tobj && tobj.className == 'listtable' ); } ).each( function(tableobj) {
			$A(tableobj.getElementsByTagName("tr")).each( function(elm) {
				Event.observe(elm, 'mouseover', function(ev) {elm.addClassName('hover');})
				Event.observe(elm, 'mouseout', function(ev) {elm.removeClassName('hover');})
				Event.observe(elm, 'click', function(ev) { elm.hasClassName('focus') ? elm.removeClassName('focus') : elm.addClassName('focus');})
			} );
		});
	}

	var frms = $A(document.forms);
	if( frms && frms.length > 0 ) {
		frms.findAll( function(fobj) { return ( fobj && $(fobj).hasClassName('form') ); } ).each( function(frmobj) {
			var hcls = "";
			var fcls = "";
			Form.getElements(frmobj).collect( function(elm) {
				if( elm.type == 'textarea' ) {
					hcls = "textarea-hover";
					fcls = "textarea-focus";
				}
				else if( elm.type == 'submit' || elm.type == 'reset' || elm.type == 'button' ) {
					hcls = "";
					fcls = "";
				}
				else {
					hcls = "input-hover";
					fcls = "input-focus";
				}
	
				if( hcls && fcls ) {
					Event.observe(elm, 'mouseover', function(ev) {elm.addClassName(hcls);})
					Event.observe(elm, 'mouseout', function(ev) {elm.removeClassName(hcls);})
					Event.observe(elm, 'focus', function(ev) {elm.addClassName(fcls);})
					Event.observe(elm, 'blur', function(ev) {elm.removeClassName(fcls);})
				}
			} );
		});
	}
}


function returnEmails() {	
	var tmp_pccef = '';
	if( $('pccef_val').checked ) {
		var tmp_pccef = '&pccef='+ $('pccef_val').value;
	}
	var url = 'massmail_ajax.php';
	var pars = 'state='+ $('state').value +'&country='+ $('country').value +'&utid='+ $('utid').value + tmp_pccef;
	var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: showMassmailResponse} );		
}

function showMassmailResponse(originalRequest) {
	$('ajax_email_list').innerHTML = originalRequest.responseText;	
}

/***** Load & Unload calls *****/
Event.observe(window,'load',function(){
},false);

Event.observe(window,'unload',function(){
},false);