// Form Guard

// Copyright Xin Yang 2003, 2004
// Web Site: www.yxScripts.com
// EMail: m_yangxin@hotmail.com
// Last Updated: Sep-01-2004

// This script is free as long as the copyright notice remains intact.

// to consolidate all error messages
var totalAlert="";

// form submit counter
var submitCounter=0;

// regular expressions used by checking functions
var reNonBlank=/[\S]/;
var reHexColor=/^#[0-9a-fA-F]{6}$/;
var reInt=/^\d+$/;
var reSignedInt=/^(\+|-)?\d+$/;
var reFloat=/^\d+(\.\d+)?$/;
var reSignedFloat=/^(\+|-)?\d+(\.\d+)?$/;
var reChar=/^[\w\-]+$/;
var reEMail=/^\w[\w\-\.]+\@\w[\w\-]+(\.\w[\w\-]+)+$/;
var reIP=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var rePostalCA=/^(\w\d){3}$/;
var reURL=/^http(s)?\:\/\/\w[\w\-]+(\.\w[\w\-]+)+([\/\%\?\&\+\#\.\w\-]+)*$/;

function rpChar(f) {
  var df=f;

  df=df.replace(/\\/g, '\\\\');
  df=df.replace(/\//g, '\\\/');
  df=df.replace(/\[/g, '\\\[');
  df=df.replace(/\]/g, '\\\]');
  df=df.replace(/\(/g, '\\\(');
  df=df.replace(/\)/g, '\\\)');
  df=df.replace(/\{/g, '\\\{');
  df=df.replace(/\}/g, '\\\}');
  df=df.replace(/\</g, '\\\<');
  df=df.replace(/\>/g, '\\\>');
  df=df.replace(/\|/g, '\\\|');
  df=df.replace(/\*/g, '\\\*');
  df=df.replace(/\?/g, '\\\?');
  df=df.replace(/\+/g, '\\\+');
  df=df.replace(/\^/g, '\\\^');
  df=df.replace(/\$/g, '\\\$');

  return df;
}

function rePhone(f) {
  var df=rpChar(f);

  df=df.replace(/d/gi, '\\d');
  df=df.replace(/w/gi, '(\\w|\\d)');

  return new RegExp('^'+df+'$');
}

function reDate(f) {
  var df=rpChar(f);

  df=df.replace(/dd/gi, '\\d\\d');
  df=df.replace(/mm/gi, '\\d\\d');
  df=df.replace(/yyyy/gi, '\\d\\d\\d\\d');

  return new RegExp('^'+df+'$');
}

function reCharNM(n,m) {
  return new RegExp("\^[\\w\\-]{"+n+","+m+"}\$");
}

function reNumberN(n,mode) {
  return new RegExp("\^"+(mode!=0?"(\\+\|-)?":"")+"\\d{1,"+n+"}\$");
}

function reNumberN2(n,mode) {
  return new RegExp("\^"+(mode!=0?"(\\+\|-)?":"")+"\\d{"+n+"}\$");
}

function reNumberNM(n,m,mode) {
  return new RegExp("\^"+(mode!=0?"(\\+\|-)?":"")+"\\d{1,"+n+"}(\\.\\d{1,"+m+"})?\$");
}

function reNumberNM2(n,m,mode) {
  return new RegExp("\^"+(mode!=0?"(\\+\|-)?":"")+"\\d{"+n+"}\\.\\d{"+m+"}\$");
}

// wrapper functions
function _alertIt(msg, mode) {
  if (mode) {
    totalAlert+=msg+"\n";
  }
  else {
    totalAlert="";
    alert(msg);
  }
}
function noBadWords(field, strict, words, msg, mode) {
  var lw=[], nwb=strict?'':'\\b';
  for (var i=0; i<words.length; i++) {
    lw[i]=nwb+words[i].toLowerCase()+nwb;
  }

  var re=new RegExp(lw.join("|"), "i");
  if (re.test(field.value)) {
    _alertIt(msg, mode);
    return (mode && mode==1)?true:false;
  }
  else {
    return true;
  }
}

function _checkIt(re, field, msg, mode) {
  if (!re.test(field.value)) {
    _alertIt(msg, mode);

    if (field.select) {
      field.select();
    }
    if (field.focus) {
      field.focus();
    }

    return (mode && mode==1)?true:false;
  }

  return true;
}

function noErrors() {
  if (totalAlert=="") {
    return true;
  }
  else {
    alert(totalAlert);
    totalAlert="";
    return false;
  }
}

// the checking functions
function mygoodIntLen2(n, m, field, msg1, msg2)
{
	var reInt=/^\d+$/;
	var str=new String;
	str=field.value;
	
	if(!reInt.test(field.value))
	{
		alert(msg1);
		if (field.focus) {
      		field.focus();
    	}
		return false;
	}
	else if(str.length<n && str.length>m)
	{
		alert(msg2);
		if (field.focus) {
      		field.focus();
    	}
		return false;
	}
}
function onlyChar(field, msg)
{
	var reChar=/^[a-zA-z]+$/;
	if(!reChar.test(field.value))
	{	
		alert(msg);
		if (field.focus) {
      		field.focus();
    	}
		return false;
	}	
	return true;
}
function onlyCharspace(field, msg)
{
	var reChar=/^[a-z\SA-Z]+$/;
	if(!reChar.test(field.value))
	{	
		alert(msg);
		if (field.focus) {
      		field.focus();
    	}
		return false;
	}	
	return true;
}

function validateTA(field, msg)
{
	
	if(field.value=='')
	{
		alert(msg);
		return false;
	}
	return true;
}
		
function goodPasswords(field1, field2, msg1, msg2, mode) {
  if (nonBlank(field1, msg1, mode?2:0) && nonBlank(field2, msg1, mode?2:0)) {
    if (field1.value == field2.value) {
      return true;
    }
    else {
      _alertIt(msg2, mode);
    }
  }
}

function goodPasswordsLen(field1, field2, n, m, msg1, msg2, msg3, mode) {
  if (nonBlank(field1, msg1, mode?2:0) && nonBlank(field2, msg1, mode?2:0)) {
    if (field1.value == field2.value) {
      if (goodCharLen(n, m, field1, msg3, mode?2:0)) {
        return true;
      }
    }
    else {
      _alertIt(msg2, mode);
    }
  }

  return (mode && mode==1)?true:false;
}

function goodEMail(field, msg, mode) {
  return _checkIt(reEMail, field, msg, mode);
}

function nonBlank(field, msg, mode) {
  if (field.type) {
    if (/file|select|text|password/.test(field.type)) {
      return _checkIt(reNonBlank, field, msg, mode);
    }
    else if (/radio|checkbox/.test(field.type)) {
      if (field.checked) {
        return true;
      }
      else {
        _alertIt(msg, mode);
        field.focus();
        return (mode && mode==1)?true:false;
      }
    }
    else {
      _alertIt("Invalid field for nonBlank() checking", mode);
      return (mode && mode==1)?true:false;
    }
  }
  else if (field.length && field[0].type && /radio|checkbox/.test(field[0].type)) {
    for (var i=0; i<field.length; i++) {
      if (field[i].checked) { return true; }
    }

    _alertIt(msg, mode);
    field[0].focus();
    return (mode && mode==1)?true:false;
  }
  else {
    _alertIt("Invalid field for nonBlank() checking", mode);
    return (mode && mode==1)?true:false;
  }
}

function goodCharLen(n, m, field, msg, mode) {
  return _checkIt(reCharNM(n,m), field, msg, mode);
}

function goodIntLen2(n, field, msg, mode) {
  return _checkIt(reNumberN2(n,0), field, msg, mode);
}

function goodChar(field, msg, mode) {
  return _checkIt(reChar, field, msg, mode);
}

function goodInt(field, msg, mode) {
  return _checkIt(reInt, field, msg, mode);
}

function rangeInt(field, r1, r2, msg, mode) {
  if (goodInt(field, msg, mode?2:0)) {
    return _rangeIt(field, r1, r2, msg, mode);
  }

  return (mode && mode==1)?true:false;
}

function _rangeIt(field, r1, r2, msg, mode) {
  if (field.value>=r1 && field.value<=r2) {
    return true;
  }
  else {
    _alertIt(msg, mode);

    field.select();
    field.focus();

    return (mode && mode==1)?true:false;
  }
}

function goodFloat(field, msg, mode) {
  return _checkIt(reFloat, field, msg, mode);
}

