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 = "#f3f";
    } else {
        this.highlight = highlight;
    }
    this.Add = Add;
    this.AddText = AddText;
    this.AddRadio = AddRadio;
    this.AddCheckbox = AddCheckbox;
    this.AddSelect = AddSelect;
    this.Validate = Validate;
    this.ClearInvalid = ClearInvalidStatus;
}

////////////////////////////////////////////////////////////////////////////////

function Add( elem ) {
    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;
    default:
        alert( elem.type );
    }
}

function AddText( elem ) {
    with( this ) {
        fields.push(elem);
        types.push("T");
    }
}

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 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 "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;
            }
        }
        return valid;
    }
}

function ClearInvalidStatus() {
    with( this ) {
        switch( types[index] ) {
        case "T": // Text 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;
  }
}

////////////////////////////////////////////////////////////////////////////////

