
// Address Book request
function GoAddrBook() {
	document.data.AddItem.value = 'AddrBook';
	__utmLinkPost(document.data);
	document.data.submit();
}

// Add To Cart request
function AddCart() {
	document.data.AddItem.value = '1';
	return true;
}

// Check valid zip on submit
function CheckZip(Path) {
	if ((document.data.AddItem) && (document.data.AddItem.value != 'AddrBook') && (Dest != 'INTL')) {
		if (!isValidZipcode(document.data.ZipCode.value) && !isValidPostalcode(document.data.ZipCode.value)) {
			Pop(AbsPath('../../msg/?msgt=9'), 350, 225);
			return false;
		}
		else if (isValidPostalcode(document.data.ZipCode.value) && Dest != 'USCA' && Dest != 'CUSC') {
			Pop(AbsPath('../../msg/?msgt=15'), 350, 225);
			return false;
		}
	}
	return true;
}

// Check that a US zip code is valid
function isValidZipcode(zipcode) { 
   zipcode = removeSpaces(zipcode);
   if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
   if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
   if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
   return true;
}

// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
   if (postalcode.search) {
      postalcode = removeSpaces(postalcode);
      if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
      else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z][ -]\d[a-zA-Z]\d$/) != -1) return true;
      else return false;
   }
   return true;
}

// Remove all spaces from a string
function removeSpaces(string) {
   var newString = '';
   for (var i = 0; i < string.length; i++) {
      if (string.charAt(i) != ' ') newString += string.charAt(i);
   }
   return newString;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
   if (string.search) {
      if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
   }
   return true;
}

// code by Chris Nott (chris[at]dithered[dot]com)
