function setElementText(element, text, display) {
    var elem = document.getElementById(element);
    if (elem != null) {
        elem.innerHTML = text; 
        elem.style.display = display;
    }
}

function crossBrowserLinkClick(element) {
	if (element.click)
		element.click();
	else if (element.onclick)
	    element.onclick();
	
	if (element.href != null) {
	    var href = element.href;
	    //strip off the leading 'javascript:'
	    var func;
    	
	    if (href.indexOf('javascript:') > -1) {
		    func = href.substr(11);
	    } else
		    func = href;
    		
	    //strip the space off (encoded as %20)		
	    if (func.substr(0, 3) == '%20')
		    func = func.substr(3);	

	    //run the function
	    eval(func);
	}	
}

function setLinkTarget(linkID, target) { 
	if (document.getElementById(linkID))
  	document.getElementById(linkID).target = target;
}

function ClearOptions(OptionList) {
	for (x = OptionList.length; x >= 0; x = x - 1) {
		OptionList[x] = null;
	}
}

function AddToOptionList(OptionList, OptionValue, OptionText) {
	OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function populate_days(day, month, year) {			
    var value = day.selectedIndex;

    ClearOptions(day);	

	if (month[month.selectedIndex].value == 1) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 2) {
	    for (x=1; x < 29; x++)
		    AddToOptionList(day, x, x);
	    if ((year[year.selectedIndex].value % 4 == 0) && ((year[year.selectedIndex].value % 100 != 0) || (year[year.selectedIndex].value % 400 == 0)))
		    AddToOptionList(day, 29, 29);
    } else if (month[month.selectedIndex].value == 3) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 4) {
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 5) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 6) {
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 7) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 8) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 9) {
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 10) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 11) {
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 12) {
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    }	

    if (day.length < value || day.length == value)
	    value = (day.length - 1);
	    
    day.selectedIndex = value;
}

function transfer(objFrom, objTo, selected, action) {
	for (var i=0, l=objFrom.options.length; i<l; i++) {
		if (objFrom.options[i].selected) {
			addOption(objTo, objFrom.options[i].text, objFrom.options[i].value);
			
			/*if (action == 'add') {
			    //sVal = ((selected.value == null) ? "" : selected.value) + ',' + objFrom.options[i].value;
			    selected.value += ',' + objFrom.options[i].value;
			}*/
		}
	}
	for (var i=objFrom.options.length-1; i>-1; i--) {
		if (objFrom.options[i].selected) {
			deleteOption(objFrom, i, selected, action);
		}
	}
	var objSelected = (action == 'add') ? objTo : objFrom;
	calculateSelected(objSelected, selected);
}

function calculateSelected(objSelected, selected) {
    selected.value = '';
	for (var i=0; i<objSelected.options.length; i++) {
	    selected.value += ',' + objSelected.options[i].value
	}
}

function deleteOption(object, index, selected, action) {
    /*if (action == 'remove') { 
        var sVal = selected.value.split(',');
        for (x=0; x<sVal.length; x++)
        {
            if (sVal[x] != object.options[index].value)
                sNewVal = ',' + sVal[x]; 
        }
        selected.value = (sNewVal == ',') ? '' : sNewVal;
    }*/
    object.options[index] = null;
}

function addOption(object, text, value) {
	var defaultSelected = true;
	var selected = true;
	var optionName = new Option(text, value, defaultSelected, selected)
	object.options[object.length] = optionName;
}		

function selectAll(objSelect, blnSelected) {
	for (var i=0, l=objSelect.options.length; i<l; i++) {
		objSelect.options[i].selected = blnSelected;
	}			
}

function limitText(limitField, limitCount, limitNum, usedChars) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else if (limitCount != null) {
		limitCount.value = limitNum - limitField.value.length;
	}
	
	if (usedChars != null && $(usedChars) != null)
	    $(usedChars).innerHTML = (limitNum - limitField.value.length) + ' characters left';
}

function textboxDetectEnter(event, buttonID) {
    if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
        crossBrowserLinkClick(document.getElementById(buttonID)); 
        return false;
    } 
    else 
        return true;
}

var bDirty = false;
var dirtyConfirmationMessage = 'You have not saved your changes - are you sure you want to exit without saving?';

function userCheckDirty(message) {
    if (bDirty) {
        var messageToDisplay = message == '' ? dirtyConfirmationMessage : message; 
        if (confirm(messageToDisplay))
            return true;
        else
            return false;
    }     
}

function resetForm() {
    document.forms[0].reset();
    bDirty = false; 
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: -1,
      height: -1
    }
    Object.extend(this.options, options || {});
    var optionsText = this.options.width == -1 ? '' : 'width='+this.options.width+',height='+this.options.height; 
    window.open(this.options.url, '', optionsText);
  }
}

function mceTagWrap(element_id, html, body) {
 html = trim(html);
 /*check that text starts and ends with tags if not wrap it in <p>s.  
   This only happens for single line text messages*/
  if(html.charAt(0) != "<" || (html.charAt(html.length - 1) != ">" || html.charAt(html.length -2) == "/")) {
	html = "<p>"+html+"</p>";	
  }
 return html;
}
function trim(sInString) {
    if (sInString == null || sInString == '')
        return '';
         
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

// ------------------------------------------------------------------
// from http://www.mattkruse.com/javascript/date/source.html
// formatDate (date_object, format)
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function LZ(x) {return(x<0||x>9?"":"0")+x}

function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["NNN"]=MONTH_NAMES[M+11];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["E"]=DAY_NAMES[E+7];
	value["EE"]=DAY_NAMES[E];
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
}

function getArgs(win, querystring)
{
    var args = new Object();
    var query = null;
    if (win != null) 
        query = win.location.search.substring(1);       //Get query string.
    else
        query = querystring;
     
    var pairs = query.split("&");                   //Break at ampersand.

    for(var i = 0; i < pairs.length; i++)
    {
        var pos = pairs[i].indexOf('=');            //Look for  "name=value".
        if (pos == -1) continue;                    //If not found, skip.
        var argname = pairs[i].substring(0,pos);    //Extract the name.
        var value = pairs[i].substring(pos+1);       //Extract the value.
        args[argname] = unescape(value);           //Store as a property
    }
    return args;                                     //Return the object
}