// Marriott Foundation JavaScript Document
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
			window.onload = function() {
				oldonload();
				func();
			}
	} 
}
function dynamicCounter() {
	if (!document.getElementById) return false;
	var formElement = document.getElementById("message");
	var textElement = document.getElementById("character-remainder");
	var maxCharacterLength = 1000;
	var characterLength = formElement.value.length;
	var characterRemainder = textElement;
	if (characterLength > maxCharacterLength) {
		characterRemainder.firstChild.nodeValue = 0;
	}
	else {
		characterRemainder.firstChild.nodeValue = maxCharacterLength - characterLength;
	}
	setTimeout(dynamicCounter, 500);
}
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}
function loadDynamicCounter() {
	if (!document.getElementById) return false;
	if (document.getElementById("character-remainder") && document.getElementById("message")) {
		dynamicCounter();
		document.getElementById("message").onkeyup = dynamicCounter;
	}
}
function resetFields(whichForm){
	for (var i=0; i<whichForm.elements.length; i++){
		var element = whichForm.elements[i];
		if (element.type == "submit" || element.type == "button" && element.name == "clear") continue; 
		element.value="";
	}
}
function resetForm(){
	if (!document.getElementById) return false;
	for (var j=0; j < document.forms.length; j++){
		var thisForm = document.forms[j];
		//for (var i=0; i<thisForm.elements.length; i++){
			//var element = thisForm.elements[i];
			if (document.getElementById("clear")){
				document.getElementById("clear").onclick=function(){
					resetFields(thisForm);
				}
				document.getElementById("clear").onkeypress=function(){
					resetFields(thisForm);
				}
			}
		//}
	}
}

function replaceText(formElement, textElement,defaultLabelText) {
	if (!document.getElementById) return false;
	var slectedCategory = formElement.options[formElement.selectedIndex].value;
	var label = textElement;
	if ((slectedCategory == "SYP" )|| (slectedCategory == "TGC" )){
		label.firstChild.nodeValue ="School/School District";
		}
	else
		{
		label.firstChild.nodeValue =defaultLabelText;
		}

}
function changeLabelName() {
	if (!document.getElementById) return false;
	if (document.getElementById("category") && document.getElementById("companyNameLabel")) {
		var formElement = document.getElementById("category");
		var textElement = document.getElementById("companyNameLabel");
		var defaultLabelText=textElement.firstChild.nodeValue;
		replaceText(formElement, textElement,defaultLabelText)	
		formElement.onchange = function() {
			replaceText(formElement, textElement,defaultLabelText);
		}
		
	}
}

addLoadEvent(doPopups); 
addLoadEvent(resetForm); 
addLoadEvent(loadDynamicCounter);
addLoadEvent(changeLabelName);