function getElementsByAttribute(attr,val,container) 
{ 
    container = container || document;
    var all = container.all || container.getElementsByTagName('*');
    var arr = [];
    for(var k=0;k<all.length;k++) {
      //if (all[k].tagName=='DIV' && all[k].className != null && all[k].className != '')
      //  alert('class: ' + all[k].className + '\nattr: ' + attr + '\nval: ' + val + '\nresult: ' + all[k].attributes[attr]); 
        
      if(all[k].attributes[attr] != null && all[k].attributes[attr].nodeValue == val) 
        arr[arr.length] = all[k];
    }
    
    return arr;
}

function setElementText(element, text, display) {
    var elem = document.getElementById(element);
    if (elem != null) {
        elem.innerHTML = text; 
        elem.style.display = display;
    }
}

function displayElementsByClass(className, display) {
    var elems = getElementsByAttribute('class', className);
    for (var x=0; x<elems.length; x++)
        elems[x].style.display = display;
}

function crossBrowserLinkClick(element) {
	if (element.click) {
		element.click();
	} else {
	    if (element.onclick != null) {
	        var onclick = element.onclick.toString();
	        if (onclick.indexOf('{') > -1)
	            onclick = onclick.substr(onclick.indexOf('{')+1, onclick.indexOf('}') - (onclick.indexOf('{')+1) - 1);
	            
	        if (onclick.indexOf('\n') > -1)
	            onclick = onclick.replace(/\n/, '');
	        
	        onclick = onclick.replace(/\s*/, '');
	        eval(onclick);
	    } else {
		    var href = element.href;
		    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) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 2) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    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) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 4) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 5) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 6) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 7) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 8) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 9) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 10) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 11) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 31; x++)
		    AddToOptionList(day, x, x);
    } else if (month[month.selectedIndex].value == 12) {
	    if (month[0].value == 0)
	        AddToOptionList(day, 0, '-');
	    for (x=1; x < 32; x++)
		    AddToOptionList(day, x, x);
    }else if (month[month.selectedIndex].value == 0) { //Dash has been added to dropdowns
        AddToOptionList(day, 0, '-');
	    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);
		}
	}
	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) {
    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) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else if (limitCount != null) {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function textboxDetectEnter(event, buttonID) {
    if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
        crossBrowserLinkClick(document.getElementById(buttonID)); 
        return false;
    } 
    else 
        return true;
}

function swapText(dir, textbox, text) {
    if (dir == 'in' && textbox.value == text)
        textbox.value = '';
    else if (dir == 'out' && textbox.value == '')
        textbox.value = text;         
}

var bDirty = false;
var dirtyConfirmationMessage = 'You will lose the changes you have made to the data.';
var localConfirmationMesssage = '';
//var onSetDirty = new YAHOO.util.CustomEvent('setDirty');

function setDirty(value) {
    onSetDirty.fire();
    bDirty = value;
}

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; 
}

/* A pretty little hack to make uploads not hang in Safari. Just call this
 * immediately before the upload is submitted. This does an Ajax call to
 * the server, which returns an empty document with the "Connection: close"
 * header, telling Safari to close the active connection. A hack, but
 * effective. From projectpath.com */
function closeKeepAlive() {
 if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
 new Ajax.Request("/ping/close", { asynchronous:false });
 }
}