/*//////////////////////////////////////////////////////////////////////////////
General-purpose Form Validation class
12/2006 - Jerry Knight, Baylor University
03/2011 - Updated to add numeric text field type
 
Example Usage:

(javascript)

        <script type="text/javascript">
        function ValidateMyForm( form ) {
          var v = new Validation();
          v.Add( form.text1 );
          v.Add( form.radio1 );
          v.Add( form.check1 );
          v.Add( form.sel1 );
          if( v.Validate() ) {
            return true;
          } else {
            alert("Some required fields were not completed.");
            return false;
          }
        }
        </script>

(form html)

        <form onsubmit="return ValidateMyForm(this);">
        ...

--------------------------------------------------------------------------------

NOTE: In order to validate a select box, you must have a "default" option 
with a value of -1.  Example:

        <select name="sel1">
          <option value="-1">Select...</option>
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
        </select>

--------------------------------------------------------------------------------

By default, text fields and select boxes that are invalid are colored light 
red (#fcc).  You may change this color:

        var v = new Validation("#cfc");

Or, you may turn this feature off:

        var v = new Validation(false);

--------------------------------------------------------------------------------

Special Types:

var v = new Validation();
v.addEmail( form.email );
v.addPhone( form.phone );
v.addTextNumeric( form.number );

//////////////////////////////////////////////////////////////////////////////*/

var mozilla = document.getElementById && !document.all;
var ie = document.all;

function Validation( highlight ) {
    this.fields = Array();
    this.types = Array();
    this.index = -1;
    if( highlight == null ) {
        this.highlight = "#fcc";
    } else {
        this.highlight = highlight;
    }
    this.Add = Add;
    this.AddText = AddText;
    this.AddTextNumeric = AddTextNumeric;
    this.AddPhone = AddPhone;
    this.AddEmail = AddEmail;
    this.AddRadio = AddRadio;
    this.AddCheckbox = AddCheckbox;
    this.AddSelect = AddSelect;
    this.AddFile = AddFile;
    this.Validate = Validate;
    this.ClearInvalid = ClearInvalidStatus;
}

////////////////////////////////////////////////////////////////////////////////

function Add( elem ) {
    // Workaround for multi radio buttons
    if( elem.length && elem.length > 1 && !elem.type ) {
        elem.type = "radio";
    }
    switch( elem.type ) {
    case "text":
    case "textarea":
        this.AddText( elem );
        break;
    case "radio":
        this.AddRadio( elem );
        break;
    case "checkbox":
        this.AddCheckbox( elem );
        break;
    case "select-one":
        this.AddSelect( elem );
        break;
    case "file":
        this.AddFile( elem );
        break;
    default:
        alert( "Field: <"+elem.nodeName+"> - Unrecognized field type: "+elem.type );
    }
}

function AddText( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("T");
    }
}

function AddTextNumeric( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("N");
    }
}

function AddEmail( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("E");
    }
}

function AddPhone( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("P");
    }
}

function AddRadio( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("R");
    }
}

function AddCheckbox( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("C");
    }
}

function AddSelect( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("S");
    }
}

function AddFile( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("F");
    }
}


function Validate() {
    with( this ) {
        var valid = true;
        for( var i=0; valid && i<fields.length; i++ ) {
            switch( types[i] ) {

            case "T":  // Text Field
                if( !isNotEmpty(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Click or type to clear highlighting
                        fields[i].onkeypress = function() { ClearInvalid(); };
                        fields[i].onclick = function() { ClearInvalid(); };
                    }
                }
                break;

            case "N":  // Numeric Text Field
                if( !isNumeric(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Click or type to clear highlighting
                        fields[i].onkeypress = function() { ClearInvalid(); };
                        fields[i].onclick = function() { ClearInvalid(); };
                    }
                }
                break;

            case "P": // Phone field
                if( !validPhone(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Click or type to clear highlighting
                        fields[i].onkeypress = function() { ClearInvalid(); };
                        fields[i].onclick = function() { ClearInvalid(); };
                    }
                }
                break;

            case "E": // Email field
                if( !validEmail(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Click or type to clear highlighting
                        fields[i].onkeypress = function() { ClearInvalid(); };
                        fields[i].onclick = function() { ClearInvalid(); };
                    }
                }
                break;

            case "R":  // Radio Button
                if( !isValidRadio(fields[i]) ) {
                    valid = false;
                    var elem = fields[i];
                    // Move focus to the first button
                    elem[0].focus();
                    if( highlight ) {
                        index = i;
                        // Highlight all of the radio buttons (in IE)
                        for( var j=0; j<elem.length; j++ ) {
                            elem[j].style.background = highlight;
                            // Click to clear highlighting
                            elem[j].onblur = function() { ClearInvalid() };
                        }
                    }
                }
                break;

            case "C":  // Checkbox
                if( !isChecked(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        index = i;
                        fields[i].style.background = highlight;
                        // Click to clear highlighting
                        fields[i].onblur = function() { ClearInvalid(); };
                    }
                }
                break;

            case "S":  // Select Box
                if( !isSelected(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Choose an option to clear highlighting
                        fields[i].onchange = function() { ClearInvalid(); };
                    }
                }
                break;
            case "F":  // File Upload Box
                if( !isNotEmpty(fields[i]) ) {
                    valid = false;
                    fields[i].focus();
                    if( highlight ) {
                        fields[i].style.background = highlight;
                        index = i;
                        // Click or type to clear highlighting
                        fields[i].onkeypress = function() { ClearInvalid(); };
                        fields[i].onclick = function() { ClearInvalid(); };
                    }
                }
                break;
            }
        }
        return valid;
    }
}

function ClearInvalidStatus() {
    with( this ) {
        switch( types[index] ) {
        case "T": // Text Field
        case "E": // Email Field
        case "N": // Numeric Field
        case "P": // Phone field
           fields[index].style.background = "";
            fields[index].onkeypress = null;
            fields[index].onclick = null;
            break;
        case "R": // Radio Buttons
            var elem = fields[index];
            for( var j=0; j<elem.length; j++ ) {
                elem[j].style.background = "";
                elem[j].onblur = null;
            }
            break;
        case "C": // Checkbox
            fields[index].style.background = "";
            fields[index].onclick = null;
            break;
        case "S": // Select Box
            fields[index].style.background = "";
            fields[index].onchange = null;
            break;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// Field Validation Functions

function isNotEmpty(elem)
{
  if( !elem.value ) {
    return false;
  }
  var str = elem.value;
  if (str == null || str.length == 0) {
    return false;
  } else {
    return true;
  }
}

function isValidRadio(elem) {
  var valid = false;
  for( var i = 0; i < elem.length; i++ ){
    if( elem[i].checked ){
      return true;
    }
  }
  return false;
}

function isChecked(elem) {
    if( elem.checked ) {
        return true;
    } 
    return false;
}

function isSelected(elem) { 
  var i = elem.selectedIndex;
  if( i >= 0 && elem.options[i].value != "-1" ) {
    return true;
  } else {
    return false;
  }
}

function validEmail(elem) {
    var str = elem.value;
    var lat=str.indexOf("@");
    var lstr=str.length;
    var at = str.indexOf("@");
    var p = str.indexOf(".");
    if (at==-1){
        return false;
    }
    if (at==-1 || at==0 || at==lstr){
        return false;
    }
    if (p==-1 || p==0 || p==lstr){
        return false;
    }
    if (str.indexOf("@",(at+1))!=-1){
        return false;
    }
    if (str.substring(at-1,at)=="." || str.substring(at+1,at+2)=="."){
        return false;
    }
    if (str.indexOf(".",(at+2))==-1){
        return false;
    }
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}

function validPhone( elem ) {
    var str = elem.value;
    var num = str.replace(/[^0-9]*/,"");
    if( num.length >= 7 ) {
        return true;
    }
    return false;
}

function isNumeric( elem ) {
    var str = elem.value;
    return !isNaN(parseFloat(str)) && isFinite(str);
}

////////////////////////////////////////////////////////////////////////////////

