/*
 * Limit text to a certain length specified by limitNum.
 * arguments:
 *  - limitField: the text to limit by the number of characters
 *  - limitCount: the number of characters left for the limitField text
 *  - limitNum:   the maximum number of characters allowed for the limitField text
 */
function limitText(limitField, limitCount, limitNum) {
        if (limitField.value.length > limitNum){
                limitField.value = limitField.value.substring(0, limitNum);
                alert("Must be " + limitNum + " characters or less.");
        }
        else {
                var charsLeft = limitNum - limitField.value.length;
                limitCount.firstChild.nodeValue = charsLeft + " characters left";
        }
}


/*
 * Limit text input when the user press keys on a given
 *  form element preferably a textarea, plus record how many characters left
 *  to input in a given element, e.g. div
 */
function limitTextInTextFields(textarea,counter,maxChars){
       
        var maxChars = maxChars;
        var textarea = document.getElementById(textarea);
        var counter = document.getElementById(counter);

        textarea.onkeydown = function(){
                limitText(textarea,counter,maxChars);  
        }
       
        textarea.onkeyup = textarea.onkeydown;

} 


/**
 * Assign callback function to window.onload without overriding previously assigned callbacks from other scripts 
 * 
 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function popup_image (imageid, popup_width, popup_height) {
	var url = "popup_image.php?id=" + imageid;
	popup_url (url, popup_width, popup_height);
}

function popup_url (url, popup_width, popup_height) {
	var left = (screen.width / 2) - (popup_width / 2);
	var top = (screen.height / 2) - (popup_height / 2);	
	aWindow=window.open(url,"","toolbar=no,width="+popup_width+",height="+popup_height+",status=no,scrollbars=yes,resize=no,menubars=no,left="+left+",top="+top);
}

/* 
 * everytime this function is called it will flip the display property of the selected element. 
 * if selected element is currently block then it will be flipped to none, and vice versa
 */
function showHideItems (itemId, itemButtonId, buttonText) {
	
	//this is the ID of the hidden item
	var selectedId = document.getElementById(itemId);
	var selectedButtonId = document.getElementById(itemButtonId);	

	if (selectedId.style.display == "none") {
        //selectedId are currently hidden, so display them
        selectedId.style.display = "block";	
		selectedButtonId.value = "Hide " + buttonText;	
	} else if (selectedId.style.display == "block") {
        //items are currently displayed, so hide them
        selectedId.style.display = "none";	
		selectedButtonId.value = "Show " + buttonText;				
	} else {
		selectedId.style.display = "none";	
		selectedButtonId.value = "Show " + buttonText;						
	}

}

function deleteConfirm (url) {
	if (confirm("Proceed and delete the selected file?") ) {
		window.open(url, "_self");
	}
}