function valve_win () {
  window.open('https://images.westpacsupply.com/ValveSpecs1.htm','ValveSpecs','top=0,left=150,width=800,height=800,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
}

function toggle_checks (formobj) {
/**********************************************************
 * reverse the checkedness of all checkboxes in this form *
 **********************************************************/
 var i;

 for (i = 0; i < formobj.elements.length; i++) {
     if (formobj.elements[i].type == "checkbox") {
         if (formobj.elements[i].checked) {
             formobj.elements[i].checked = false;
         } else {
             formobj.elements[i].checked = true;
         }
     }
 }
}

function opt_smartinteger (t) {
  // assist user in entering a valid integer
  // but a blank is allowed. This is the optional part!
  var result;
  var pattern = /^\d+$/;
  var efield = t.value;
 
  result = efield.match(pattern);
  if (result != null) {
      return true;
  } else {
      t.value = '';
      alert("Please enter a number");
      return true;
  }

}
function opt_smartemail (t) {
  // assist user in entering a valid email address
  // but a blank is allowed. This is the optional part!
  var result;
  var pattern = /^[\w\-\.]+@[\w\-\.]+$/;
  var efield = t.value;
 
  result = efield.match(pattern);
  if (result != null) {
      return true;
  } else {
      t.value = '';
      alert("Please enter a valid email address");
      return true;
  }

}

function opt_smartstring (t) {
  // assist user in entering words, numbers, dashes, underscores and a space
  // but a blank is allowed. This is the optional part!
  var result;
  var pattern = /^[\w\-\s\.]+$/;
  var efield = t.value;
 
  result = efield.match(pattern);
  if (result != null) {
      return true;
  } else {
      t.value = '';
      if (efield != '') {
          alert("Only letters, numbers, spaces, underscores, dots and dashes are allowed");
      }
      return true;
  }
}

function opt_smartstring2 (t) {
  // assist user in entering words, numbers, dashes, underscores
  // no blanks. This is the optional part!
  var result;
  var pattern = /^[\w\-\.]+$/;
  var efield = t.value;
 
  result = efield.match(pattern);
  if (result != null) {
      return true;
  } else {
      t.value = '';
      alert("Only letters, numbers, underscores, dots and dashes are allowed");
      return true;
  }
}

function opt_smartphone(t) {
// Check for correct phone number
  var result;
  var pattern = /^\d{10}$/;
  var efield = t.value;
  efield = strip_non_numeric(efield);
 
  result = efield.match(pattern);
  if (result != null) {
      return true;
  } else {
      t.value = '';
      alert("Please enter a valid phone number");
      return true;
  }
}

function strip_non_numeric(text) {
/*********************************************
 * formats string for only digits characters *
 *********************************************/
  var newtext = '';
  for(i=0; i<text.length; i++) {
      letter = text.charAt(i);
      if (!letter.match(/\s/)) {
          if (letter.match(/\d/)) newtext = newtext + letter;
      }
  }
  return newtext;
}

function opt_smartdate (t) {
  // assist user in entering a valid date (mm-dd-yyyy)
  // but a blank is allowed. This is the optional part!
  // It converts 4 digit years to 2 digit years. It looks for
  // leap year correctness and defaults to today's date.
  var result;
  var pattern_1d = /^\d$/;
  var pattern_2d = /^\d\d$/;
  var pattern_3ds = /^(\d)(\d\d)$/; // assume m-dd
  var pattern_4d = /^\d\d\d\d$/;
  var pattern_4ds = /^(\d\d)(\d\d)$/; // assume mm-dd
  var pattern_6d = /^(\d\d)(\d\d)(\d\d)$/;
  var pattern_8d = /^(\d\d)(\d\d)(\d\d\d\d)$/;
  var pattern_garbage = /^([a-zA-Z]+)$/;
  var datefield = t.value;
  var ok = 0; // assume garbage
                                                                                                        
  // convert dashes and dots to dashes to slashes
  if (datefield != null) {
      datefield = (datefield.split("-")).join("/");
      datefield = (datefield.split(".")).join("/");
  } else {
     t.value = '';
     return true;
  }
                                                                                                        
  result = datefield.match(pattern_garbage);
  if (result != null) {
      t.value = '';
      return true;
  }

  result = datefield.match(pattern_8d);
  if (result != null) {
      datefield = result[1] + '/' + result[2] + '/' + result[3];
      ok = 1;
  }
  result = datefield.match(pattern_6d);
  if (result != null) {
      datefield = result[1] + '/' + result[2] + '/' + result[3];
      ok = 1;
  }
  result = datefield.match(pattern_3ds);
  if (result != null) {
      datefield = '0' + result[1] + '/' + result[2] + '/' + get_year();
      ok = 1;
  }
  result = datefield.match(pattern_4ds);
  if (result != null) {
      datefield = result[1] + '/' + result[2] + '/' + get_year();
      ok = 1;
  }
  if (ok != 1) {
      t.value = '';
      return true;
  }
                                                                                                        
  // Assume now its properly formatted with dashes. Split into 3 fields: mm dd yyyy
  var a = datefield.split("/");
  if ((a[0] == "") || (a[0] == null)) a[0] = get_chaser_month();
  result = a[0].match(pattern_1d); // one digit month test
  if (result != null) a[0] = "0" + a[0]; // matched one digit month
  result = a[0].match(pattern_2d); // 2 digit month test
  if (result == null) a[0] = get_chaser_month(); // not 2 digit month, use today
  if ((a[0] > 12) || (a[0] < 1)) a[0] = get_chaser_month();
                                                                                                        
  if ((a[1] == "") || (a[1] == null)) a[1] = get_chaser_day();
  result = a[1].match(pattern_1d); // 1 digit day test
  if (result != null) a[1] = "0" + a[1]; // matched 1 digit day
  result = a[1].match(pattern_2d); // 2 digit day test
  if (result == null) a[1] = get_chaser_day(); // not 2 digit day, use today
  if (a[1] < 1) a[1] = get_chaser_day();
                                                                                                        
  if ((a[2] == "") || (a[2] == null)) a[2] = get_year();
  result = a[2].match(pattern_2d); // 2 digit year test
  if (result == null) {
      result = a[2].match(pattern_4d); // 4 digit year test
      if (result == null) {
          // not 2 or 4 digit year, use this year
          a[2] = get_year();
      }
  } else {
      // 2 digit year, convert to 4 digit year
      a[3] = get_year();
      a[3] = a[3].substr(0,2); // get century
      a[2] = a[3] + a[2]; // convert to 4 digits
  }
                                                                                                        
  var lastdayofmonth = get_last_day(a[0],a[2]);
  if (a[1] > lastdayofmonth) a[1] = lastdayofmonth;
                                                                                                        
  t.value = a[0] + "/" + a[1] + "/" + a[2];
  return true;
}

function validateCreditCard(s) {
  var efield = s.value;
  efield = strip_non_numeric(efield);
  s.value = efield;
  return;

  // validate number
  j = w.length / 2;
  if (j < 6.5 || j > 8 || j == 7) return false;
  k = Math.floor(j);
  m = Math.ceil(j) - k;
  c = 0;
  for (i=0; i<k; i++) {
    a = w.charAt(i*2+m) * 2;
    c += a > 9 ? Math.floor(a/10 + a%10) : a;
  }
  for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
  return (c%10 == 0);
}

function qcount(q, orig, min, max, pack) {
  // also tests for non whole numbers
  // pack means multiple of, 2 means only even numbers are allowed
                                                                                                    
  var retvalue;
  var quantity = q.value; // current value of input box
  var testval = Math.round(quantity);
                                                                                                    
  if (testval != quantity) {
      alert("Only whole numbers are allowed");
      q.value = orig;
      return false;
  }
                                                                                                    
  testval = quantity % pack; // modulo function
  if (testval != 0) {
      // not multiple of pack size
      alert("Quantities must be a multiple of " + pack);
      q.value = orig;
      return false;
  }
                                                                                                    
  // Up And Down Changes allowed
  if (max != -1) {
      if (quantity > max) {
          alert("Quantities above " + max + " are not allowed");
          q.value = orig;
          return false;
      }
  }
  if (min != -1) {
      if (quantity < min) {
          alert("Quantities below " + min + " are not allowed");
          q.value = orig;
          return false;
      }
  }
  if (quantity != orig) {
      haveChange = true; // if user fails to hit save button
  }
  return true;
}

function digits_only (field, event) {
/************************************************
 * allow enter key, tab and digits only         *
 * usage: onkeypress="digits_only(this,event);" *
 # 48-57 = digits, enter, tab, del, backsp      *
 ************************************************/
  var keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  //var keychar = String.fromCharCode(keycode);
                                                                                                    
  if (keycode == 46) {
      //return false; // period
      return true; // period
  }
  if (keycode >= 48 && keycode <= 57) {
      return true; // digits
  }
  if (keycode == 13) {
      return true; // enter
  }
  if (keycode == 8) {
      return true; // tab
  }
  if (keycode == 9) {
      return true; // del
  }
  if (keycode == 46) {
      return true; //backspace
  }
                                                                                                    
  return false;
}

function confirmChange(field) {
  var $ok;
  $ok = confirm('You currently have products in your shopping cart.\nChanging to a different location will clear your cart.\nDo you wish to continue?');
  if ($ok) field.form.submit();
  return $ok;
}

function rememberLogin(cbox) {
// user requests to remember login with cookie for 90 days
  if (cbox.checked) {
      // set
      Set_Cookie('KeyDataUser', $('username4').value, '90', '/', '', '');
  } else {
      // clear
      Delete_Cookie('KeyDataUser', '/', '');
  }
}

function Set_Cookie(name, value, expires, path, domain, secure) {
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime(today.getTime());

  if (expires) {
      expires = expires * 1000 * 60 * 60 * 24; // in days
  }
  var expires_date = new Date(today.getTime() + (expires));

  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie(check_name) {
  // note: document.cookie only returns name=value, not the other components
  // note: that in cases where cookie is initialized but no value, null is returned
  var a_all_cookies = document.cookie.split( ';' ); // name and value pairs array
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ ) {
      // now we'll split apart each name=value pair
      a_temp_cookie = a_all_cookies[i].split( '=' );

      // and trim left/right whitespace while we're at it
      cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

      // if the extracted name matches passed check_name
      if (cookie_name == check_name) {
          cookie_found = true;
          // we need to handle case where cookie has no value but exists (no = sign, that is):
          if (a_temp_cookie.length > 1 ) {
              cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
          }
          return cookie_value;
          break;
      }
      a_temp_cookie = null;
      cookie_name = '';
  }
  if (!cookie_found) {
      return null; // cookie missing
  }
}

function Delete_Cookie(name, path, domain) {
// expire a cookie
  if (Get_Cookie(name)) document.cookie = name + "=" +
  ( ( path ) ? ";path=" + path : "") +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

