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();
	
	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) {
    var text; 
    if (window.gecko || window.webkit || window.webkit419 || window.webkit420)
        text = limitField.value.replace(/\n/g,'\r\n');
    else
        text = limitField.value;
    
	if (text.length > limitNum) {
	    text = text.substring(0, limitNum);
		limitField.value = text;
	} else if (limitCount != null) {
		limitCount.value = limitNum - text.length;
	}
	
	if (usedChars != null && $(usedChars) != null)
	    $(usedChars).innerHTML = (limitNum - text.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+',scrollbars=1'; 
    window.open(this.options.url, '', optionsText);
  }
}

function log(message) {
    if (window.gecko && typeof(window["console"]) != 'undefined' && debug)
        console.log(message);
    else if (window.ie && debug)
        alert(message);  
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'

    if (str == null)
        return '';
    
    var ret = str;
    ret = ret.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
    return ret;
}

function changepic(imgTagName,imgFileName) {
	if (document.images) {
		if (document.images[imgTagName]) {
			document.images[imgTagName].src = imgFileName;
		}
	}
}