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( ) {
	$A(document.getElementsByTagName("table")).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');})
		} );
	});

	$A(document.forms).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);})
			}
		} );
	});
}

/***** Load & Unload calls ****
Event.observe(window,'load',function(){
},false);

Event.observe(window,'unload',function(){
},false);
*/

/****** Display Login Popup ******/
function displayLoginPopUp(goto_url) {

	showLogin(goto_url);
	return;

	var ypos = 0;
	var xpos = 0;
	if (window.pageYOffset) {
	  ypos = window.pageYOffset;
	  xpos = window.pageXOffset;
	}
	else {
	  ypos = document.documentElement.scrollTop;
	  xpos = document.documentElement.scrollLeft;
	}

	var frameWidth;
	var frameHeight;
	if (self.innerWidth) {
      frameWidth = self.innerWidth;
      frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
      frameWidth = document.documentElement.clientWidth;
      frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
      frameWidth = document.body.clientWidth;
      frameHeight = document.body.clientHeight;
	}	
		  
	$('login_popup_wrap').style.display = "block";
	$('login_popup_wrap').style.position = "absolute";
	$('login_popup_wrap').style.zIndex = 1000;
	
	var top = ypos + (frameHeight/2) - ($('login_popup_wrap').offsetHeight/2) + 75;
	var left = xpos + (frameWidth/2) - ($('login_popup_wrap').offsetWidth/2);
	$('login_popup_wrap').style.top = top + "px";
	$('login_popup_wrap').style.left = left + "px";
	
	document.loginform.goto.value = goto_url; 
	
	return FALSE;
}

function hideLoginPopUp() {
	$('login_popup_wrap').style.display = "none";
}

function gotojoin(url,args){
	if(url){
		window.location.href = url+args;
	}
}

