function gwAlert(m) {
}


String.prototype.right = function (n) {
  if (n < this.length) {
    return this.substring (this.length - n, this.length);
  } else {
    return this;
  }
}


function valNumber(f) {
  var num=f.value;
  if (/[^\d]/.test(num)) {
    alert("You must enter a number in this field.");
    f.value="";
    f.focus();
  }
}


function valSSN(fld) {
  if (!/^(\d{3})-?(\d{2})-?(\d{4})/.test(fld.value)) {
    alert("You must enter a valid SSN.");
    fld.value="";
    fld.focus();
    return false;
  }
  else {
    fld.value=RegExp.$1+"-"+RegExp.$2+"-"+RegExp.$3;
  }
}


function valEmail (fld) {
  if (!/^(\w+\.)*\w+@(\w+\.)*\w+\.\w{2,5}$/.test(fld.value)) {
    alert("You must enter a valid Email address.");
    fld.value="";
    fld.focus();
    return false;
  }
  return true;
}


function valZip (fld) {
  if (!/$\d{5}^/.test(fld.value) && !/$\d{9}^/.test(fld.value)) {
    alert("Invalid Zip Code.");
    fld.value="";
    fld.focus();
    return false;
  }
  return true;
}


function formatSSN(f) {
  var ssn=f.value;
  if (/(\d{3})[-\s]?(\d{2})[-\s]?(\d{4})/.test(ssn)) {
    f.value=RegExp.$1+"-"+RegExp.$2+"-"+RegExp.$3;
  }
  else {
    alert("You must enter a valid SSN or Tax ID.");
    f.value="";
    f.focus();
  }
}


function formatPhone(f) {
  var phone=f.value;
  if (/^\((\d{3})\)\s?(\d{3})[-\.\s]?(\d{4})((\s|\s?[xX])(\d+))?$/.test(phone) ||
      /^(\d{3})[-\s.]?(\d{3})[-\.\s]?(\d{4})((\s|\s?[xX])(\d+))?$/.test(phone)) {
    var fPhone="("+RegExp.$1+") "+RegExp.$2+"-"+RegExp.$3;
    if (RegExp.$6.length > 0) fPhone += " x"+RegExp.$6;
    f.value=fPhone;
  }
  else {
    alert("You must enter a valid phone number.");
    f.value="";
    f.focus();
  }
}


function formatMoney(f) {
  var amount=f.value;
  if (/(\$\s?)?\d{1,2}(,?\d{3})*(\.\d{1,2})?/.test(amount)) {
    amount=amount.replace(/\s/g,"");
    amount=amount.replace(/\$/,"");
    amount=amount.replace(/,/,"");
    f.value="$"+amount;
  }
  else {
    alert("You must enter a valid dollar amount.");
    f.value="";
    f.focus();
  }
}


function valDate(f, desc) {
  var errorDesc = "";
  if (/^(\d{2})([\/\.-]?)(\d{2})(\2)(\d{2}|\d{4})$/.test(f.value) ||
      /^(\d{1,2})([\/\.-])(\d{1,2})(\2)(\d{2}|\d{4})$/.test(f.value)) {
    var M = RegExp.$1*1;
    var D = RegExp.$3*1;
    var Y = RegExp.$5*1;
    if (Y < 100)
       Y = (Y <= 50) ? Y + 2000 : Y + 1900;
    if (M < 1 || M > 12) {
       errorDesc = "Invalid entry.  Please try again.";
    } else {
       var ld = (Y % 400==0 || Y % 100 != 0 && Y % 4 == 0) ? "9" : "8";
       var maxdays = "312"+ld+"31303130313130313031";
       if (D < 1 || D > maxdays.substr((M-1)*2, 2))
          errorDesc = "Invalid entry.  Please try again.";
    }
    if (errorDesc.length == 0) {
    // Date is valid, so format it.
      f.value = ("0"+M).right(2) + "/" + ("0"+D).right(2) + "/" + Y;
    }
  } else {
    errorDesc = "Invalid entry.  Please try again.";
  }
  if (errorDesc.length > 0) {
    alert(desc + ": " + errorDesc);
    f.focus();
    f.value = "";
    return false;
  }
}


function getRadio(n) {
  var f = document.forms[0];
  var v = "";
  for (var i = 0; i < f.elements[n].length; i++) {
    if (f.elements[n][i].checked) {
      v = f.elements[n][i].value;
    }
  }
  return v;
}


IE = (document.all);
function validate() {
  var fldList="";
  var f=document.forms[0];
  for (var i=0; i < f.elements.length; ++i) {
    if (f.elements[i].onfocus) {
      s=f.elements[i].onfocus.toString();
      if (s.indexOf("required = true") > -1) {
if (IE) f.elements[i].onblur=resetColor;
        var pattern=/fldDesc\s*=\s*(["'])(.*)\1/;
        pattern.test(s);
        var desc=(RegExp.$2.length > 0) ? RegExp.$2 : f.elements[i].name;
        if (f.elements[i].type=="text") {
          if (f.elements[i].value.length==0) {
            fldList+="  "+desc+"\n";
            if (IE) f.elements[i].style.backgroundColor="#ffff00";
          }
          else if (IE) f.elements[i].style.backgroundColor="#ffffff";
        }
        else if (f.elements[i].type.substr(0, 6)=="select") {
          if (f.elements[i].selectedIndex==0) {
            fldList+="  "+desc+"\n";
            if (IE) f.elements[i].style.backgroundColor="#ffff00";
          }
          else if (IE) f.elements[i].style.backgroundColor="#ffffff";
        }
        else if (f.elements[i].type=="radio") {
          if (!getRadio(f.elements[i].name)) {
            fldList += "  " + desc + "\n";
          }
        }
      }
    }
  }
  if (fldList.length > 0) {
    alert("Before submitting this form, please fill in the following:\n\n"+fldList);
    return false;
  }
  else {
    var str = "left=0,screenX=0,top=0,screenY=0";
    if (window.screen) {
      var ah = screen.availHeight - 30;
      var aw = screen.availWidth - 10;
      str += ",height=" + ah;
      str += ",innerHeight=" + ah;
      str += ",width=" + aw;
      str += ",innerWidth=" + aw;
      str += ",scrollbars=yes";
    } else {
      str += ",resizable"; // so the user can resize the window manually
    }
//    conf = open("enroll_confirmation.html", "conf", str);
    f.submit();
  }
}


function resetColor() {
  this.style.backgroundColor="#ffffff";
}


