// This file contains intellectual property and artistic creations 
// by Dennis Sandow and is copyright 2000-2010 by the author. 
// For use only on websites maintained by Dennis Sandow, or with
// specific permission.  Do not remove this notice.

// Supports validation of common fields in user forms
// Supports validation of email address in notices pages with mailing list add field
// Supports selection of "MailMe" button in notice pages with mailing lists

stJul = new Date(); var pst = stJul.getTime(), ksOK=0;

function StdWrapup(noSq){
// alert(13+' '+typeof document.StdForm.passFail+' '+ document.StdForm.passFail.value)  
  if ( typeof document.StdForm.passFail != udf ) {
    document.StdForm.passFail.value = 'TESTING';
  };
// alert(typeof document.StdForm.passFail+' '+ document.StdForm.passFail.value)  
  endJul = new Date(); 
  document.StdForm.pageTimer.value = Math.floor( (endJul.getTime()-pst)/1000 );
  if ( isWeb && (
       typeof basURL == udf || 
         ( basURL == 'http://www.longhillnj.info' && 
           noSq != 'nosquelch' &&
           ( typeof document.StdForm.q01MailTo == udf ||
             document.StdForm.q01MailTo.value.indexOf('webmaster@') 
           )
         )
       )
     ){
        alert("This form can not be submitted from this website.  Sorry.  Visit www.longhillnj.us"); 
        return false;
  };
  if (! isWeb) {
    window.alert('This form won\'t be submitted from the local version of the website.'); 
    return false;
  };
  if ( typeof document.StdForm.passFail != udf ) {
    document.StdForm.passFail.value = 'PASS';
  };
  return true;
};    
 
function snitch(ss){ 
  with(document.StdForm){
    if (typeof errors1 == udf ) return true;
    errors1.value = errors1.value + '['+ss+']\n'; 
  };
};

// ckRadio( name1 [,name2[,name3...]] )
// returns false if ANY radio button group nameN does not have an item selected
// else returns name[LAST].value

function ckRadio(){
  retval = false;
  with (document.StdForm) {
    for ( aa=0; aa<=50; aa++) {
      if ( aa > 0 && ! retval ) return false;
      if ( typeof arguments[aa] == udf ) return retval;
      butName = arguments[aa]; retval = false;
      for (x=0;x<=50;x++ ){
        if ( typeof butName[x] == udf ) break;
        if ( butName[x].checked == true ) { retval = butName[x].value; break; }
      };
    };
  };
};

function properSignupEmail() { // for use with distrib-list signup
  if (! properEmailCore(document.signup.emailAddress.value) ){
    alert('Please enter an email address in the format: xxx@xxx.xxx');
    document.signup.emailAddress.focus(); return false;
  };
  return true;
};

function properEmail(s) { // for use in on-line forms 
  if ( ! properEmailCore(s) ){
    alert('Please complete the E-mail field with an address in the format: xxx@xxx.xxx');
    snitch('email >'+s+'<'); return false; 
  };
  return true; 
};

function properEmailCore(s) {
  if (s.length < 10) return false;
  var i, ch, atct=0, dotct=0;
  for (i=0;i< s.length;i++){ 
    ch=s.charAt(i);
    if ( ch==" " ) return false ; 
    if ( ch=="@" ){ atct++; dotct=0; continue;};
    if ( ch=="." ){ dotct++; continue;}; 
  };  
  if ( atct == 1 && dotct >= 1 ) return true;
  return false; 
};

function properAlpha(s,cct,spct) {
  if ( s.length < cct ) {
    if (arguments[3]) alert(arguments[3]); else alert('Please enter your name');
    return false;
  };
  if ( ! spct ) return true;
  var i;
  for ( i=0; i< s.length; i++ ){ 
    if ( s.charAt(i) == " " ) { if ( --spct<=0) return true; };
  };
  if (arguments[3]) alert(arguments[3]); else alert('Please enter your name'); 
  return false; 
};

var DidOne=0;
function blurOnce(ObjCopy){
  if (DidOne) return;
  ObjCopy.testNo.value=''; DidOne=1;
};

function startPhoneLookup(ObjCopy,uP,sameWin){
  if (typeof ObjCopy == udf ) return false;
  TN = perfectPhone(ObjCopy.testNo.value,1);
  ObjCopy.testNo.value=TN;
  if( ! properPhone('Phone',TN) ) return false;
  if (sameWin) location.href='phonfind.htm?'+TN ; else NewWd((uP==2?'../':'')+(uP>=1?'../':'')+'phonfind.htm?'+TN);
  return;
};

function perfectPhone(s,amplify){
  s2 = s.replace(/[^0-9a-zA-Z]/g,'');
  if( amplify && s2.length == 4 ) s2=647+s2;
  if( amplify && s2.length == 7 ) s2=908+s2;
  if( s2.length == 10 ) s2=s2.replace(/[ABCabc]/g,'2').replace(/[DEFdef]/g,'3').replace(/[GHIghi]/g,'4').replace(/[JKLjkl]/g,'5').replace(/[MNOmno]/g,'6').replace(/[PRSprs]/g,'7').replace(/[TUYtuv]/g,'8').replace(/[WXYwxy]/g,'9')
  s2 = s2.replace(/[^0-9]/g,'');
  return s2.replace(/(\d{3})(\d{3})(\d{4})/,"$1-$2-$3");
};

function properPhone(word, sIn, d7ok) {
  var i, ch, digct=0, cct=0,spct=0;
  for (i=0;i< sIn.length;i++){
    ch=sIn.charAt(i);
    if ( ch>="0" && ch<="9" ){ digct++; continue; };
    if ( ch==" " || ch=='-' ){ spct++; continue;}; cct++;
  };  
  if ( ( digct==10 || ( d7ok && digct==7 ) ) && spct<=2 && cct==0){ 
    return true;
  } else {  
    alert('Please be sure the '+ word +' Number is '+(d7ok?'7 or ':'')+'10 digits, no parentheses, with no more than 2 dashes or spaces.');
    return false; 
  };
};

function perfectTown(s){
  return s.replace('_My','Meyersville' ).replace('_M','Millington').replace( '_S','Stirling').replace('_G','Gillette').replace(/[^ ]Millington.*/, '\nMillington, NJ 07946').replace(/[^ ]Stirling.*/,'\nStirling, NJ 07980').replace( /[^ ]Gillette.*/,'\nGillette, NJ 07933').replace(/[^ ]Meyersville.*/, '\nMeyersville, NJ 07933');
} ;

function isDigits(s) {
  if ( s.length == 0 ) return false;
  var i, ch;
  for (i=0;i< s.length;i++){ 
    ch=s.charAt(i);
    if ( (i==0 && ch=="$") || (ch>="0" && ch<= "9") || ch == "-" || ch=="." || ch==",") continue;
    return false;
  };
  return true; 
};

function properZip(s) {
  var i, ch, zct=0;
  for (i=0;i< s.length;i++){ 
    ch=s.charAt(i);
    if ( ch>="0" && ch<= "9" ) {zct++; continue;};
    if ( ch == "-" ) continue;
    break;
  };
  if (zct != 5 && zct != 9) {
    alert('Please enter a 5 or 9 digit zip code. Dash is optional.');
    return false;
  };
  return true; 
};

// news.cgi does ls -tr |tail -1 to find most recently updated news item
// then it writes a blind call to setMailMe() and preloads the name of the latest file
// after loading, this fn executes and presets the corresponding button.
// 9/8/6 no action if only one item in the group ??

function setMailMe( match ) {
  with (document.selMail) {
    for ( aa=0; aa<=50; aa++) {
      if ( typeof selBut[aa] != udf &&
           typeof selBut[aa].value != udf && 
           selBut[aa].value == match ){ selBut[aa].checked = true; break; };
    };
  };
};
