 /*
2
3example:
4
5smartButton.buttons["loginButton"] = { // loginButton is the button's id attribute
6 required: "#userName, #password", // input elements which must recieve input
7 disabledSrc: "images/buttons/btn_login_dis.gif", // grayed out image
8 disabledTitle: "you must fill all the fields", // alt text for the button
9 testState: function(){return (document.getElementById("userName").value!="Ryan")} // this example will not let Ryan log in.
10}
11
12Notes:
13requires jQuery.
14testState is optional.
15
16*/

 smartButton = {
 buttons: {},
 init: function(){

 if(typeof(jQuery(".smartButton")) == 'undefined' || jQuery(".smartButton") == null) {alert("I am not finding init");return}
 smartButtonElements = jQuery(".smartButton").get();
 
  for(var i = 0;i<smartButtonElements.length;i++){
  
  var thisButton = smartButtonElements[i]
  
 if(!thisButton.id){alert("not finding id")}

 if(!smartButton.buttons){alert("not samrt button")}
 if(!smartButton.buttons[thisButton.id]){alert("not in id")}

thisButton.smartButton = smartButton.buttons[thisButton.id];
thisButton.requiredInputs = jQuery(thisButton.smartButton.required).get();
var disabledButton = document.createElement("img");
var myStyleNode = thisButton.getAttributeNode("style");
if(!!myStyleNode){disabledButton.setAttributeNode(myStyleNode.cloneNode(false))}

 disabledButton.src = thisButton.smartButton.disabledSrc;
 disabledButton.title = thisButton.smartButton.disabledTitle;
 disabledButton.className = thisButton.className;
 disabledButton.style.display = "none";

 thisButton.parentNode.insertBefore(disabledButton, thisButton);
 thisButton.disabledState = disabledButton;

 for(var i = 0;i<thisButton.requiredInputs.length;i++){
 var thisInput = thisButton.requiredInputs[i];
 thisInput.isValid = false;
 if(!thisInput.smartButtons){thisInput.smartButtons = []}
 thisInput.smartButtons.push(thisButton)
 $(thisInput).bind("click keyup blur",smartButton.validateInput);
 smartButton.validateInput.apply(thisInput);
 }
 smartButton.validateButton(thisButton);
 }
 },
 validateButton: function(thisButton){
 var isEnabled = true;
 if(!thisButton.requiredInputs){return}
 for(var i = 0;i<thisButton.requiredInputs.length;i++){
 var thisInput = thisButton.requiredInputs[i];
 isEnabled = isEnabled && thisInput.isValid;
 };
 if(thisButton.smartButton.testState){isEnabled = isEnabled && (thisButton.smartButton.testState() != false)}
 if(isEnabled){
 thisButton.style.display = "";
 thisButton.disabledState.style.display = "none";
 }else{
 thisButton.style.display = "none";
 thisButton.disabledState.style.display = "";
 }
 },
 validateInput: function(){
 switch(this.type.toLowerCase()){
 case "checkbox":{
 this.isValid = this.checked;
 break;
 }
 case "radio":{
 if(!this.name){break}
 this.isValid = false;
 var relatedRadios = document.getElementsByName(this.name);
 for(var i = 0; i<relatedRadios.length; i++){this.isValid = this.isValid || relatedRadios[i].checked}
 for(var i = 0; i<relatedRadios.length; i++){relatedRadios[i].isValid = this.isValid}
 break;
 }
 default:{
 this.isValid = !!(this.value != "");
 break;
 }
 }
 for(var i = 0;i<this.smartButtons.length;i++){
 var thisButton = this.smartButtons[i];
 smartButton.validateButton(thisButton);
 }
 }
}

 if(typeof(jQuery) != "undefined"){
 jQuery(function(){smartButton.init()});
}

