
function numkey() {
 var a=",1234567890.";
 var c=window.event.keyCode;
 var b=String.fromCharCode(c);
 c=a.search(b);
 if (c<0) {window.event.returnValue=false;}
}


function donum(fmt,el) {
 var a,b,c,d,e,n,s,dc;
 var fg,nn;
 b=el.value;
 a=b.length;
 c=0;
 d=0;
 e=0;
 fg=0;
 n="";
 dc="";
//Cut value at decimal recording numbers only
 while (e<a) {
  s = b.substr(e, 1);
  d=isNaN(s);
  if (d) {
   if (s==".") {fg=1;}
  }
  else {
   if (fg==0) {n=n+s;}
   else {dc=dc+s;}
  }
  e++;
 } 
//Determine decimal of mask and if comma delimiter
 dc=dc+"000000000";
 a=fmt.length;
 e=0
 c=0
 d=0
 while (e<a) {
  s = fmt.substr(e, 1);
  if (s==".") {d=e;}
  if (s==",") {c=1;}
  e++;
 }
//Format decimals
 d=a - d - 1;
 if (d>0) {dc="." + dc.substr(0,d);}
 else {dc="";}

 nn=n;
//Add commas
 if (c==1) {
  e=n.length; 
  if (e>3) {
   e--;
   fg=0;
   nn="";
   while (e>=0) {
    s = n.substr(e, 1);
    fg++;
    if (fg>3) {
     nn="," + nn;
     fg=1;
    }
    nn=s + nn;
    e--;
   }
  }
 }
 el.value=nn + dc;
}


function dodate(fmt,el) {
//Requires 4 digit year, 2 digit month and day. Separators not needed
 var a,b,c,s,n;
 var nd,sep,m,d;
 sep="";
 b=el.value;
 if (b.length>0) {
  fmt=fmt.toUpperCase();
//First check 8 digits exists
  a=b.length;
  c=0;
  n="";
  while (c<a) {
   s = b.substr(c, 1);
   d=isNaN(s);
   if (d) {}
   else {n=n+s;}
   c++;
  } 
  a=n.length;
  if (a!=8) {
   el.select();
   alert("The date you have entered is not in the required format. Format is " + fmt);   
  }
  else {
//Format delimiter if any
  a=0
  b="DMY"
  while (a<fmt.length) {
   s = fmt.substr(a, 1);
   d=b.search(s);
   if (d<0) {sep=s; a=fmt.length;}
   a++;
  }
//Break the values apart based on the mask
   a=fmt.search("M");
   b=fmt.search("D");
   c=fmt.search("Y");
   if (a>b && a>c) {
    m=n.substr(6,2);
    if (b<c) {
     d=n.substr(0,2);
     nd=d + sep + n.substr(2,4) + sep + m;
    }
    else {
     d=n.substr(4,2);
     nd=n.substr(0,4) + sep + d + sep + m;
    }
   }
   else {
    if (b>a && b>c) {
     d=n.substr(6,2);
     if (a<c) {
      m=n.substr(0,2);
      nd=m + sep + n.substr(2,4) + sep + d;
     }
     else {
     m=n.substr(4,2);
      nd=n.substr(0,4) + sep + m + sep + d;
     }
    }
    else {
     if (a<b) {
      m=n.substr(0,2);
      d=n.substr(2,2);
      nd=m + sep + d + sep + n.substr(4,4);
     }
    else {
      m=n.substr(2,2);
      d=n.substr(0,2);
      nd=d + sep + m + sep + n.substr(4,4);
     }
    }
   }
   a=m.valueOf();
   b=d.valueOf();
   c=0;
   if (a<1 || a>12) {c=1;}
   if (b<1 || b>31) {c=1;}
   if (c==1) {
    el.select();
    alert("The date you have entered is not in the required format. Format is " + fmt);   
   }
   else {
    el.value=nd;
   }
  }
 }
}

function dophone(fmt,el) {
 var a,b,c,d,n,s,nc;
 b=el.value;
 a=b.length;
 if (a>0) {
//Count the number of expected numbers
  c=0;
  nc=0;
  while (c<fmt.length) {
   s = fmt.substr(c, 1);
   if (s=="#") { nc++;}
   c++;
  }
//Compare to numbers supplied
  c=0;
  n="";
  while (c<a) {
   s = b.substr(c, 1);
   d=isNaN(s);
   if (d) {}
   else {
    if (s>="0" && s<="9") {
     n=n+s;}
    }
   c++;
  } 
  a=n.length;
  if (a!=nc) {
   el.select();
   alert("The telephone number you have entered is not in the required format " + fmt);   
  }
  else {
//Create format based on all formatting characters supplied
   c=0;
   nc=0;
   a="";
   while (c<fmt.length) {
    s = fmt.substr(c, 1);
    if (s=="#") {
     a=a + n.substr(nc,1);
     nc++;
    }
    else {
     a=a + s;
    }
    c++;
   }
   el.value=a;
  }
 }
}


function dopostal(el) {
 var a,b,c,d,e,i,nc,fg,ps;
 var ns="0123456789";
 fg=0;
 a=el.value;
 if (a.length>0) {
  a=a.toUpperCase();
  b=/\ /g;
  a=a.replace(b,"");
  nc=0;
  ps="";
//Count number of numbers. If not 3, 5 or 9 is invalid
  for (i = 0; i < a.length; i++) {
   c=a.substr(i,1);
   d=isNaN(c);
   if (d) {}
   else {
    nc++;
    ps=ps + c;
   }
  }
  if(nc==3 || nc==5 || nc==9) {
//If 3, assume Canadian
   b="";
   if(nc==3) {
    if (a.length!=6) {
     fg=1;
    }
    else {
     i=0;
     d=0;
     while (i<a.length) {
      c=a.substr(i,1);
      if (d==0) {
       e=c.toLowerCase();
       if (e==c) {fg=1;}
       else {b=b + c;}
       d=1;
      }
      else {
       e=isNaN(c);
//alert(e);
       if (e) {fg=1;}
       else {b=b + c;}
       d=0;
      }
      i++;
     }
     if (fg==0) {b=b.substr(0,3) + " " + b.substr(3,3);}
    }
   }
   else {
//Otherwise assume US
    if (nc==9) {b=ps.substr(0,5) + " " + ps.substr(5,4);}
    else {b=ps;}
   }
  }
  else {
   fg=1;
  }
  if (fg!=0) {
   el.select();
   alert("The postal code you have entered does not appear to be a valid format North American format");   
  }
  else {
   el.value=b;
  }
 }
}


function handleSubmit() {
 var fm=document.forms["rsmart"];
 var a,b,c,i,t;
 var emsg="";
 var cr = String.fromCharCode(13,10);

 if (fm!=null) {

//Check for required fields before submit. 
//for handler
  i=0;
  a=0;
  while (i<fm.elements.length) {
   b=fm.elements[i].tagName;
   b=b.toUpperCase();
   if (b=="INPUT") {
    b=fm.elements[i].type;
    b=b.toUpperCase();
    if (b=="TEXT") {
     b=fm.elements[i].className;
     if (b=="rq") {
      c=fm.elements[i].value;
      if (c.length<1) {
       emsg=emsg + cr + fm.elements[i].title;
       a++;
      }
     }
    }
   }
   i++;
  }
  if (a>0) {
   b=" is";
   if (a>1) {b="s are";}
   b="The following field" + b + " required and must be completed in order to submit this form"
   b=b + emsg;
   alert(b);
  }
  else {
//Add title to actual field to input objects name so can be interpreted. Add id property for select objects
   i=0;
   while (i<fm.elements.length) {
    b=fm.elements[i].tagName;
    b=b.toUpperCase();
    if (b=="INPUT" || b=="TEXTAREA") {
     t=fm.elements[i].name + "-_-" + fm.elements[i].title
     fm.elements[i].name=t;
    }
    else {
     if (b=="SELECT") {
      t=fm.elements[i].name + "-_-" + fm.elements[i].id
      fm.elements[i].name=t;
     }
    }
    i++;
   }
   fm.method="post";
   fm.action="rsmart_handler.asp";
   fm.submit();
  }
 }
}
