function flashSubmit(theForm){
    if(validateLeadForm(theForm)){
        document.getElementById('sbSub').TGotoLabel("/apply_btn", "message");
        theForm.submit();
    }
}
function validateLeadForm(aForm){
    if(!isModernBrowser()) return true;
    var isValid = true;
    var errorLabel = find("errorLabel");
    clearChildren(errorLabel);
    forEach(aForm.elements, function(each){
        each.isValid = true;
        validateRequired(each);
        validateType(each);
        validateOtherControls(each);
        findAndToggleLabelColor(each);        
    });
    if(isValid)
        doValidSubmit(errorLabel);
    else doInvalidSubmit(aForm, errorLabel);
    return isValid;

    //runs when a lead form has been validated and is ready to be captured
    function doValidSubmit(anErrorLabel){
        clearChildren(anErrorLabel);
        var theButton = find("submitButton");
        if(theButton){
            try{
                theButton.disabled='true';
                if(theButton.getAttribute("wait")) 
                    theButton.value = theButton.getAttribute("wait");
            }catch(anError){}
        }
    }
    
    function doInvalidSubmit(aForm, errorLabel){
        setErrorText(aForm, errorLabel);
        if(document.body.scrollTop>200)
            window.scrollTo(0,200);
    }
    
    //support routines
    function setErrorText(aForm, anObject){
        var errIntro = find("errorIntro");
        if(errIntro) errIntro.style.display = "";
        
        var errBox = find("errorBox");
        if(errBox) errBox.style.display = "";
        
        anObject.appendChild(makeTag("br"));
        forEach(aForm.elements, function(each){
            if(each.name == "") return;
            if(aForm.elements[each.name] != each && aForm.elements[each.name][0] != each){ return;} //guard clause for checkbox groups, prevents multiple error messages
            if(aForm.elements[each.name].isValid) return;
            if(aForm.elements[each.name][0] && aForm.elements[each.name][0].isValid) return;
            if(each.getAttribute("errormessage")){
                if(each.getAttribute("errormessage").length>0){
                        anObject.appendChild(makeText(each.getAttribute("errormessage") + " "));
                        anObject.appendChild(makeTag("br"));
                    }
            } else {
                //parent may have an error message
                if(each.getAttribute("isChild") == "true"){
                    var errMsg = syncParentError(each);
                    if(errMsg.length>0 && !(anObject.innerHTML.indexOf(errMsg)>0)){
                        anObject.appendChild(makeText(errMsg + " "));
                        anObject.appendChild(makeTag("br"));
                    }
                }
            }
        });
    }
    function syncParentError(child){
        var aParent = child.parentNode;
        while(aParent){
            if(aParent.getAttribute && aParent.getAttribute("parenterror"))
                return aParent.getAttribute("parenterror");
            aParent=aParent.parentNode;
        }
        return "";
    }
    function syncParentLabel(child, isValid){
        var aParent = child.parentNode;
        while(aParent){
            if(aParent.getAttribute("parentLabel")){
                toggleColor(aParent.firstChild.firstChild,isValid);
                return;
            }
            aParent=aParent.parentNode;
        }
    }
    function validateRequired(each){
        if(each.getAttribute("required") != "true") return;
        each.isValid = each.value.length > 0;
        if (each.type == 'checkbox' || each.type == 'radio') {
            if(each == each.form.elements[each.name]) {
                each.isValid = each.checked;
            } else {
                var oneValid = false;
                forEach(each.form.elements[each.name], function(each){
                    oneValid = oneValid || each.checked;
                });
                each.form.elements[each.name].isValid = oneValid;
                each.isValid = oneValid;
            }
        }
        if(isValid)
            isValid = each.isValid;
        if(each.getAttribute("isChild") == "true")
            syncParentLabel(each, each.isValid);
    }
    function validateType(each){
        if(!(each.isValid && !isValidType(each))) return;
        isValid = false;
        each.isValid = false;
    }     
    function validateOtherControls(each){
        if (each.type == 'textarea'){
            var theMax = each.getAttribute("maxlength") ? parseInt(each.getAttribute("maxlength")) : 4000;
            each.isValid = each.value.length < theMax;
            if (!each.isValid && each.getAttribute("errormessage")=="") 
				each.setAttribute('errormessage', 'The input area allows no more than ' + theMax.toString() + ' characters.');
            if(isValid) isValid = each.isValid;
		}
    }
    function findAndToggleLabelColor(each){
        if(!each.getAttribute("name")) return;
        var aLabel = findLabel(each);
        toggleColor(aLabel, each.isValid);
    }
    function findLabel(each){
		if (each.type=='textarea') return each;
		return find(each.getAttribute("name") + "Label");
    }
    function toggleColor(each, isValid){
		if(!each) return;
		if (each.type=='textarea') {
			if (!isValid) each.className='TextAreaError';
			return;
		}
        each.style.color = isValid ? "" : "#C00";
    }
}
function checkTextLength(aTextArea, anEvent){
    if(anEvent.keyCode == 8) return true;//8==backspace in firefox
    var theMax = aTextArea.getAttribute("maxlength") ? aTextArea.getAttribute("maxlength") : 4000;
    var goodLength = aTextArea.value.length < parseInt(aTextArea.getAttribute("maxlength"));
    aTextArea.className = goodLength ? 'TextArea' : 'TextAreaError';
	if (!goodLength){
		if (!aTextArea.getAttribute("warnVisible")){
			var aTag = makeTag("div");
			aTag.id = aTextArea.getAttribute("name") + 'warning';
			aTag.className="TextAreaWarning";
			aTextArea.setAttribute("warnVisible", 'true');
			aTag.appendChild(makeText("Too Many Characters"));
			aTextArea.parentElement.insertBefore(aTag, aTextArea);
		}
		return cancelEvent(anEvent);
	}
	if (aTextArea.getAttribute("warnVisible")){
		aTextArea.parentElement.removeChild(find(aTextArea.getAttribute("name") + 'warning'));
		aTextArea.removeAttribute("warnVisible")
	}
	return true;
}
function cancelEvent(anEvent){
    if(anEvent.preventDefault){
        anEvent.stopPropagation();
        anEvent.preventDefault();
    }else{
        anEvent.returnValue = false;
        anEvent.cancelBubble = true;
    }
    return false;
}
function isValidType(each){
    var validateTypes = {
        phoneRegex:/^[+]?(\(?[0-9]{3}[\)-\.]?\ ?)?[0-9]{3}[-\.]?[0-9]{4}$/,
        currencyRegex:/^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/,
        numberRegex:/^[-+]?\d*$/,
        decimalRegex:/^[-+]?\d+(\.\d+)?$/,
        emailRegex:/^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/,
        alphaRegex:/^([^0-9]*)$/,
        commaNumberRegex:/^[-+]?[\d,]*$/,
        urlRegex:new RegExp("(((ht|f)tp(s?))\\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\\-\\.]+\\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\\:0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\;\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*")
    };
    if(!each.getAttribute("validate")) return true;
    if(each.value.toString() == "") return true;
    var theMatch = validateTypes[each.getAttribute("validate") + "Regex"] ? validateTypes[each.getAttribute("validate") + "Regex"] : new RegExp(each.getAttribute("validate"));
    return each.value.toString().match(theMatch) != null;
}
function alertObject(anObject){
    alert("called from:" + alertObject.caller);
    for(var each in anObject)
        alert(anObject + ":" + anObject[each]);
}
function forEach(aList,toRun){
    if(!aList.length)return;
    for(var index=0;index<aList.length;index++)
        toRun(aList[index]);
}
function isModernBrowser(){
    return isDefined(document.getElementById)
            && (isDefined(document.attachEvent) 
                || isDefined(document.addEventListener));
}
function find(aName){
    if(!document.getElementById)return null;
    return document.getElementById(aName);
}
function clearChildren(anObject){
    if(!anObject.hasChildNodes || !anObject.removeChild)return;
    while(anObject.hasChildNodes())
        anObject.removeChild(anObject.firstChild);
}
function makeText(aString){
    if(!document.createTextNode) return;
    var aList = aString.split('\\n');
    var aNode = document.createElement("span");
    aNode.appendChild(document.createTextNode(aList[0]));
    for(var each=1;each<aList.length;each++) {
        aNode.appendChild(makeTag("br"));
        aNode.appendChild(document.createTextNode(aList[each]));
    }
    return aNode;
}
function makeTag(aTag){
    if(!document.createElement)return;
    return document.createElement(aTag);
}
function isDefined(anObject){
    return typeof(anObject) != "undefined";
}
    
function checkChildren(aParentCheckbox){
    //todo: child / parent enable and check
}