// Form Guard + EDO self development

// Copyright Xin Yang 2003, 2004
// Web Site: www.yxScripts.com
// EMail: m_yangxin@hotmail.com
// Last Updated: APR-22-2004

// This script is free as long as the copyright notice remains intact.

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\-]+)+$/;
var reIP=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var rePostalCA=/^(\w\d){3}$/;

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+"}\$");
}

function _checkIt(re, field, msg) {
  if (typeof(field.length)!="undefined") {
    if (field.length==0) {
      return true;
    }
    if (typeof(field.options)!="undefined") {
      if (re.test(field.value)) {
        return true;
      }
    }
    else {
      for (var i=0; i<field.length; i++) {
        if (field[i].checked) {
          return true;
        }
      }
    }
  }
  else {
    if (field.type=="checkbox" || field.type=="radio") {
      if (field.checked) {
        return true;
      }
    }
    else if (re.test(field.value)) {
      return true;
    }
  }

  //alert(msg);
  if (typeof(field.select)!="undefined") {
    field.select();
  }
  if (typeof(field.focus)!="undefined") {
    field.focus();
  }

  return false;
}

function goodPhone(pf, field, msg) {
  return _checkIt(rePhone(pf), field, msg);
}

function goodPostalCA(field, msg) {
  return _checkIt(rePostalCA, field, msg);
}

function goodDate(df, field, msg) {
  if (_checkIt(reDate(df), field, msg)) {
    var di=field.value;
    var y4=df.search(/yyyy/i), y=di.substring(y4, y4+4)-0;
    var m2=df.search(/mm/i), m=di.substring(m2, m2+2)-1;
    var d2=df.search(/dd/i), d=di.substring(d2, d2+2)-0;

    var dd=new Date(y, m, d);
    if (y==dd.getFullYear() && m==dd.getMonth() && d==dd.getDate()) {
      return true;
    }
    else {
      alert(msg);

      field.select();
      field.focus();
    }
  }

  return false;
}

function goodIP(field, msg) {
  return _checkIt(reIP, field, msg);
}

function goodChar(field, msg) {
  return _checkIt(reChar, field, msg);
}

function goodEMail(field, msg) {
  return _checkIt(reEMail, field, msg);
}

function goodInt(field, msg) {
  return _checkIt(reInt, field, msg);
}

function goodSignedInt(field, msg) {
  return _checkIt(reSignedInt, field, msg);
}

function goodFloat(field, msg) {
  return _checkIt(reFloat, field, msg);
}

function goodSignedFloat(field, msg) {
  return _checkIt(reSignedFloat, field, msg);
}

function goodIntLen(n, field, msg) {
  return _checkIt(reNumberN(n,0), field, msg);
}

function goodSignedIntLen(n, field, msg) {
  return _checkIt(reNumberN(n,1), field, msg);
}

function goodIntLen2(n, field, msg) {
  return _checkIt(reNumberN2(n,0), field, msg);
}

function goodSignedIntLen2(n, field, msg) {
  return _checkIt(reNumberN2(n,1), field, msg);
}

function goodCharLen(n, m, field, msg) {
  return _checkIt(reCharNM(n,m), field, msg);
}

function goodFloatLen(n, m, field, msg) {
  return _checkIt(reNumberNM(n,m,0), field, msg);
}

function goodSignedFloatLen(n, m, field, msg) {
  return _checkIt(reNumberNM(n,m,1), field, msg);
}

function goodFloatLen2(n, m, field, msg) {
  return _checkIt(reNumberNM2(n,m,0), field, msg);
}

function goodSignedFloatLen2(n, m, field, msg) {
  return _checkIt(reNumberNM2(n,m,1), field, msg);
}

function _rangeIt(field, r1, r2, msg) {
  if (field.value>=r1 && field.value<=r2) {
    return true;
  }
  else {
    alert(msg);

    field.select();
    field.focus();

    return false;
  }
}

function rangeInt(field, r1, r2, msg) {
  if (goodInt(field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedInt(field, r1, r2, msg) {
  if (goodSignedInt(field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeFloat(field, r1, r2, msg) {
  if (goodFloat(field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedFloat(field, r1, r2, msg) {
  if (goodSignedFloat(field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeIntLen(n, field, r1, r2, msg) {
  if (goodIntLen(n, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedIntLen(n, field, r1, r2, msg) {
  if (goodSignedIntLen(n, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeIntLen2(n, field, r1, r2, msg) {
  if (goodIntLen2(n, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedIntLen2(n, field, r1, r2, msg) {
  if (goodSignedIntLen2(n, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeFloatLen(n, m, field, r1, r2, msg) {
  if (goodFloatLen(n, m, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedFloatLen(n, m, field, r1, r2, msg) {
  if (goodSignedFloatLen(n, m, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeFloatLen2(n, m, field, r1, r2, msg) {
  if (goodFloatLen2(n, m, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function rangeSignedFloatLen2(n, m, field, r1, r2, msg) {
  if (goodSignedFloatLen2(n, m, field, msg)) {
    return _rangeIt(field, r1, r2, msg);
  }

  return false;
}

function _dd(n) {
  return (n<10)?"0"+n:""+n;
}

function _getOffset(n) {
  var d=new Date();
  if (n!=0) {
    d.setTime(d.getTime()+n*86400000);
  }
  return d.getFullYear()+""+_dd(d.getMonth()+1)+""+_dd(d.getDate())+"";
}

function _stringIt(df, d) {
  var y4=df.search(/yyyy/i), m2=df.search(/mm/i), d2=df.search(/dd/i);
  return d.substring(y4, y4+4)+d.substring(m2, m2+2)+d.substring(d2, d2+2);
}

function rangeDate(df, field, r1, r2, msg) {
  if (goodDate(df, field, msg)) {
    var d=_stringIt(df, field.value);

    var r1x="", r2x="";
    if (r1.search(/^\d+$/)!=-1) {
      r1x=_getOffset(r1-0);
    }
    else {
      r1x=_stringIt(df, r1);
    }
    if (r2.search(/^\d+$/)!=-1) {
      r2x=_getOffset(r2-0);
    }
    else {
      r2x=_stringIt(df, r2);
    }

    if (d<r1x || d>r2x) {
      alert(msg);

      field.select();
      field.focus();
    }
    else {
      return true;
    }
  }

  return false;
}

function goodDateRange(df, field1, field2, msg) {
  if (goodDate(df, field1, msg) && goodDate(df, field2, msg)) {
    if (_stringIt(df, field1.value)>_stringIt(df, field2.value)) {
      alert(msg);
      field1.focus();
    }
    else {
      return true;
    }
  }

  return false;
}

function goodDateRange2(df, field1, field2, msg) {
  if (goodDate(df, field1, msg) && goodDate(df, field2, msg)) {
    if (_stringIt(df, field1.value)>=_stringIt(df, field2.value)) {
      alert(msg);
      field1.focus();
    }
    else {
      return true;
    }
  }

  return false;
}

function goodHexColor(field, msg) {
  return _checkIt(reHexColor, field, msg);
}

function nonBlank(field, msg) {
  return _checkIt(reNonBlank, field, msg);
}

function goodRadioedFields(form, fn, re, msgs, msg) {
  for (var i=0; i<form[fn].length; i++) {
    if (form[fn][i].checked) {
      return _checkIt(re, form[form[fn][i].value], msgs[i]);
    }
  }

  alert(msg);
  return false;
}

function goodRadioedFields2(form, fn, re, msgs, msg) {
  for (var i=0; i<form[fn].length; i++) {
    if (form[fn][i].checked) {
      return _checkIt(re[i], form[form[fn][i].value], msgs[i]);
    }
  }

  alert(msg);
  return false;
}

function noBadWords(field, strict, words, msg) {
  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)) {
    alert(msg);
    return false;
  }
  else {
    return true;
  }
}


function isHKID(hkid){

	var hkid_array = new Array();
	var total=0;
	var remainder, checksum;
	
	for (i=0;i<8;i++){
		hkid_array[i] = hkid.charAt(i);		
	}


	if (hkid_array[0].toUpperCase()=='A' || hkid_array[0].toUpperCase()=='L' || hkid_array[0].toUpperCase()=='W'){
		hkid_array[0]=1;
	}else if (hkid_array[0].toUpperCase()=='B' || hkid_array[0].toUpperCase()=='M' || hkid_array[0].toUpperCase()=='X'){
		hkid_array[0]=2;
	}else if (hkid_array[0].toUpperCase()=='C' || hkid_array[0].toUpperCase()=='N' || hkid_array[0].toUpperCase()=='Y'){
		hkid_array[0]=3;
	}else if (hkid_array[0].toUpperCase()=='D' || hkid_array[0].toUpperCase()=='O' || hkid_array[0].toUpperCase()=='Z'){
		hkid_array[0]=4;
	}else if (hkid_array[0].toUpperCase()=='E' || hkid_array[0].toUpperCase()=='P'){
		hkid_array[0]=5;
	}else if (hkid_array[0].toUpperCase()=='F' || hkid_array[0].toUpperCase()=='Q'){
		hkid_array[0]=6;
	}else if (hkid_array[0].toUpperCase()=='G' || hkid_array[0].toUpperCase()=='R'){
		hkid_array[0]=7;
	}else if (hkid_array[0].toUpperCase()=='H' || hkid_array[0].toUpperCase()=='S'){
		hkid_array[0]=8;
	}else if (hkid_array[0].toUpperCase()=='I' || hkid_array[0].toUpperCase()=='T'){
		hkid_array[0]=9;
	}else if (hkid_array[0].toUpperCase()=='J' || hkid_array[0].toUpperCase()=='U'){
		hkid_array[0]=10;
	}else if (hkid_array[0].toUpperCase()=='K' || hkid_array[0].toUpperCase()=='V'){
		hkid_array[0]=11;
	}

	for (j=8,i=0;j>=2,i<7;j--,i++){
		total = total + (j* hkid_array[i]);
	}

	remainder = total%11;
	checksum = 11-remainder;
	
	if (hkid_array[7].toUpperCase()=='A'){
		hkid_array[7]=10;
	}
	
	if (hkid_array[7]!=checksum){
		return false;
	}
	
	return true;

}


function validate_allrdo() {
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 


///////////////////////////////////////////////////////////////////////////////////////
// function to check if the words in textarea exceed the limit
// parameters: this_field - form textarea component name
//			   limited_word - restricted words number
//			   mode - mode 1: word count, mode 2: character count
// return: true - valid / false -invalid
///////////////////////////////////////////////////////////////////////////////////////

function CountWords (this_field, limited_word, mode) {	

var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (mode==1 && word_count > limited_word) return false;
if (mode==2 && char_count > limited_word) return false;

return true;
}

//
//check is numeric
//
//
//

function isNumeric(num){
invalid=false;
for (i=0;i<num.length;i++){
	if (isNaN(parseInt(num.substr(i,1),10))){
		invalid=true;
	}
}

return !(invalid);
}


//
//check is positive number or float
//
//
//

function isNumber(num){
invalid=false;
if (isNaN(parseInt(num,10))){
	invalid=true;
}

return !(invalid);
}
