/* The schedule function lets you call a function once a specific object has been loaded
 * @param objectID The ID of the object you're waiting to load
 * @param functionCall The function to call once the object has been loaded
 */
function schedule(objectID, functionCall) {
	if (document.getElementById(objectID)) {
		eval(functionCall);
	} else {
		setTimeout("schedule('" + objectID + "', '" + functionCall + "')", 1);
	}
	return true;
}
/* A browser-neutral helper method that returns an object, 
 * generally an element, based on a specified ID.
 * @param id The ID of the object to be returned
 */
function getObjectById(id) {
	if ( document.layers && document.layers[id] ) {
		return document.layers[id];
	}
	else if ( document.all && document.all[id] ) {
		return document.all[id];
	}
	else if ( document.getElementById && document.getElementById(id) ) {
		return document.getElementById(id);
	} else {
		return null;
	}
}
/* Adds a way to get all of the Elements by their class value
 * @param searchClass  The value to look for in the class attribute
 * @param node         The node of the document to start with. Defaults to document.
 * @tag   tag          Only search for this tag in specified node. Defaults to all.
 */
function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tag == null ) tag = '*';
	var elementList = node.getElementsByTagName(tag);
	var lengthElementList = elementList.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < lengthElementList; i++) {
		if ( pattern.test(elementList[i].className) ) {
			classElements[j] = elementList[i];
			j++;
		}
	}
	return classElements;
}
/* Look over a page and find any occurances of class="print" and replace
 * them with a link that when clicked open's the browser's Print.
 */
function printThisPage() {
	var printList = getElementsByClass('print');
	if (printList) {
		for (i=0; i < printList.length; i++) {
			text = document.createTextNode(printList[i].innerHTML);
			print_action = document.createElement("a");	
			print_action.setAttribute("href", "#");
			print_action.appendChild(text);
			print_action.onclick = function(){
				window.print();
			}
			var parent = printList[i].parentNode;
			var sibling = printList[i].nextSibling;
			parent.insertBefore(print_action, sibling);
			parent.removeChild(printList[i]);
		}
	}
}
/* Checks to see if the supplied element has the specified class name or not
 */
function hasClass(elem, string) {
	if (elem.className != "") {
		var classList = elem.className.split(' ');
		console.log(classList);
		for (i=0; i < classList.length; i++) {
			if (classList[i] == string) {
				return true;
			}
		}
	}
	return false;
}

function changeColorYellow(formField) {
    formField.style.background="#FFFFDD"; 
    formField.style.border="1px solid #FF7200";
    formField.style.padding="1px";
}

function changeColorWhite(formField) {
    formField.style.background="#FFFFFF"; 
    formField.style.border="1px solid #117FB0";
    formField.style.padding="1px";
}

function validateEmptyText(mytext) {
    var re = /^\s{1,}$/g; //match any white space
    if ((mytext.value.length==0) || (mytext.value==null) || ((mytext.value.search(re)) > -1)) { 
        changeColorYellow(mytext);
    }
    else { 
        changeColorWhite(mytext);
    }
}

function validateEmptyWYSIWYG(editor) {
    tinyMCE.triggerSave(true,true);
    var mytext = tinyMCE.getContent(editor);
    var re = /^\s{1,}$/g; //match any white space
    alert(mytext);
    /*
    if ((mytext.length==0) || (mytext==null) || ((mytext.search(re)) > -1)) { 
        changeColorYellow(mytext);
    }
    else { 
        changeColorWhite(mytext);
    }
    */
}

function validateEmptySelect(myselect) {
    var numSelected = 0;
    var i;
    for (i = 0; i < myselect.length; i++) {
        if (myselect.options[i].selected)
            numSelected++;
    }
    if (numSelected < 1) {
        changeColorYellow(myselect);
    }
    else {
        changeColorWhite(myselect);
    }
}

function showHint(element) {
    document.getElementById(element + "_hint").style.display = "inline";
}
function hideHint(element) {
    document.getElementById(element + "_hint").style.display = "none";
}

