function updateCart() {
	document.forms[0].submit();
}

function updateFields(val) {
	if (val == "United States") {
		document.forms[0].b_province.disabled = true;
		if (document.forms[0].b_country.options[document.forms[0].b_country.selectedIndex].value == "United States") {
			document.forms[0].b_state.disabled = false;
			//document.forms[0].b_state.selectedIndex = 0;
		}
		document.forms[0].b_city.disabled = false;
		//document.forms[0].b_city.value = '';

		//document.getElementById('billing_city').style.visibility = "visible";
		//document.getElementById('billing_city').style.display = "block";
		//document.getElementById('billing_state').style.visibility = "visible";
		//document.getElementById('billing_state').style.display = "block";
		
		document.forms[0].s_province.disabled = true;
		if (document.forms[0].s_country.options[document.forms[0].s_country.selectedIndex].value == "United States") {
			document.forms[0].s_state.disabled = false;
			//document.forms[0].s_state.selectedIndex = 0;
		}
		document.forms[0].s_city.disabled = false;
		//document.forms[0].s_city.value = '';
		
		//document.getElementById('shipping_city').style.visibility = "visible";
		//document.getElementById('shipping_city').style.display = "block";
		//document.getElementById('shipping_state').style.visibility = "visible";
		//document.getElementById('shipping_state').style.display = "block";

		
		document.forms[0].ship_method_dom.disabled = false;
		document.forms[0].ship_method_intl.disabled = true;
		
		status_view('domestic');
	} else {
		document.forms[0].b_province.disabled = false;
		if (document.forms[0].b_country.options[document.forms[0].b_country.selectedIndex].value != "United States") {
			document.forms[0].b_state.disabled = true;
			document.forms[0].b_state.selectedIndex = 0;
		}
		document.forms[0].b_city.disabled = false;
		//document.forms[0].b_city.value = '';
		
		// SHOW THESE
		//document.getElementById('billing_address2').style.visibility = "visible";
		//document.getElementById('billing_address2').style.display = "block";
		//document.getElementById('billing_province').style.visibility = "visible";
		//document.getElementById('billing_province').style.display = "block";
		// HIDE THE FORM ROWS
		//document.getElementById('billing_city').style.visibility = "hidden";
		//document.getElementById('billing_city').style.display = "none";
		//document.getElementById('billing_state').style.visibility = "hidden";
		//document.getElementById('billing_state').style.display = "none";
		
		document.forms[0].s_province.disabled = false;
		if (document.forms[0].s_country.options[document.forms[0].s_country.selectedIndex].value != "United States") {
			document.forms[0].s_state.disabled = true;
			document.forms[0].s_state.selectedIndex = 0;
		}
		document.forms[0].s_city.disabled = false;
		//document.forms[0].s_city.value = '';
		
		// SHOW THESE
		//document.getElementById('shipping_address2').style.visibility = "visible";
		//document.getElementById('shipping_address2').style.display = "block";
		//document.getElementById('shipping_province').style.visibility = "visible";
		//document.getElementById('shipping_province').style.display = "block";
		// HIDE THE FORM ROWS
		//document.getElementById('shipping_city').style.visibility = "hidden";
		//document.getElementById('shipping_city').style.display = "none";
		//document.getElementById('shipping_state').style.visibility = "hidden";
		//document.getElementById('shipping_state').style.display = "none";
		
		document.forms[0].ship_method_dom.disabled = true;
		document.forms[0].ship_method_intl.disabled = false;
		status_view('international');
	}
}

function showMsg(fld, ct, msg) {
	if (fld.value.length > ct) {
		
			// Try for replacement of Street, Avenue, Road
			newStr = fld.value;
			newStr = newStr.replace(' Avenue', ' Av');
			newStr = newStr.replace(' Ave.', ' Av');
			newStr = newStr.replace(' Boulevard', ' Bl');
			newStr = newStr.replace(' Blvd.', ' Bl');
			newStr = newStr.replace(' Drive', ' Dr');
			newStr = newStr.replace(' Dr.', ' Dr');
			newStr = newStr.replace(' Lane', ' Ln');
			newStr = newStr.replace(' Ln.', ' Ln');
			newStr = newStr.replace(' Place', ' Pl');
			newStr = newStr.replace(' Pl.', ' Pl');
			newStr = newStr.replace(' Road', ' Rd');
			newStr = newStr.replace(' Rd.', ' Rd');
			newStr = newStr.replace(' Street', ' St');
			newStr = newStr.replace(' St.', ' St');
			newStr = newStr.replace(' Terrace', ' Tr');
			newStr = newStr.replace(' Tr.', ' Tr');
			fld.value = newStr;
			
			if (newStr.length > ct) {
				str = fld.value.substr(0, (ct+1));
				fld.value = str;
				err = document.getElementById(fld.name+'_span');
				err.innerHTML = msg;
			}
	} else {
			err = document.getElementById(fld.name+'_span');
			err.innerHTML = '';
	}
}

function copyData(fld) {
	frm = document.forms[0];
	
	if (fld.checked) {
		frm.s_first_name.value = frm.b_first_name.value;
		frm.s_last_name.value = frm.b_last_name.value;
		//frm.s_company.value = frm.b_company.value;
		frm.s_address1.value = frm.b_address1.value;
		frm.s_address2.value = frm.b_address2.value;
		frm.s_country.selectedIndex = frm.b_country.selectedIndex;
		updateFields(frm.s_country.options[frm.s_country.selectedIndex].value)
		frm.s_city.value = frm.b_city.value;
		frm.s_state.selectedIndex = frm.b_state.selectedIndex;
		frm.s_province.value = frm.b_province.value;
		frm.s_postal_code.value = frm.b_postal_code.value;
		
		
	} else {
		frm.s_first_name.value = '';
		frm.s_last_name.value = '';
		//frm.s_company.value = '';
		frm.s_address1.value = '';
		frm.s_address2.value = '';
		frm.s_country.selectedIndex = frm.b_country.selectedIndex;
		updateFields(frm.s_country.options[frm.s_country.selectedIndex].value)
		frm.s_city.value = '';
		frm.s_state.selectedIndex = 0;
		frm.s_province.value = '';
		frm.s_postal_code.value = '';
	}
}

function status_view(thediv) {
	var allcontent = Array('domestic','international','empty');
	for (i=0; i<allcontent.length; i++) {
		var div_name = allcontent[i] + "_div";
		
		if (allcontent[i] == thediv) {
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}
}

function account_view(thediv) {
	var allcontent = Array('option1','option2','option3');
	for (i=0; i<allcontent.length; i++) {
		var div_name = allcontent[i] + "_div";
		
		if (allcontent[i] == thediv) {
			document.forms[0].action.value = allcontent[i];
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}
}


























/* ------------------ validation --------------------- */

function isEmail(fld, name) {
	if (fld.value.length) {
		var isEmail = ((fld.value.indexOf("@") != -1) && (fld.value.indexOf(".") != -1));
		if (!isEmail) {
			alert(name+' does not appear to be a valid format.  E-mail addresses must by in the xxx@xxx.com format.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isValidExpDate(fld_month, fld_year, svr_month, svr_year) {
		//if ((fld_month.disabled==false) && (fld_year.disabled==false)) return true;
		if (fld_month.options[fld_month.selectedIndex].value < svr_month) {
			if (fld_year.options[fld_year.selectedIndex].value == svr_year) {
				alert('Expiration Date does not appear to be valid');
				fld_month.focus();
				return false;
			}
		}
		
		return true;
}

function isNumeric(fld, name) {
	if (fld.value.length) {
		if (isNaN(fld.value)) { 
			alert(name+' must be a number.  Please do not include commas or non-numeric characters in your submission.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isMoney(fld, name, xmatch) {
	// strip all the currency stuff
	money = _removeCurrency(fld.value);
	if (fld.value.length) {
		if (isNaN(money) || !money.length) {
			alert(name+' appears to be an invalid money format.  Please do not include commas or dollar signs in your submission.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = money;
		}

	}
	
	return true;
}

function isRequired(fld, name) {
	if (fld.disabled) return true;
	if (!fld.value.length) {
		alert(name+' is a required field.');
		fld.focus();
		if (!fld.options) {
			fld.select();
		}
		return false;
	}
	
	return true;
}

function isChecked(fld, name) {
	if (fld.disabled) return true;
	if (!fld.checked == true) {
		alert(name+' must be checked.');
		fld.focus();
		if (!fld.options) {
			fld.select();
		}
		return false;
	}
	
	return true;
}

function isMinimum(fld, name, minimum) {
	if (fld.disabled) return true;
	if (fld.value.length < minimum) {
		alert(name+' requires a minimum character count of '+minimum);
		fld.focus();
		fld.select();
		return false;
	}
	return true;
}

function isRequiredSelect(fld, name) {
	if (fld.disabled == true) return true;
	if (fld.options[fld.selectedIndex].value == '') {
		alert(name+ ' is a required field.');
		fld.focus();
		return false;
	} else {
		//alert(fld.options[fld.selectedIndex].value);
	}

	return true;

}

function isDate(fld, name) {
	if (fld.value.length) {
	
		date = _validateUSDate(fld.value);
		
		if (!date.length) {
			alert(name+' appears to be invalid.  Please use the mm/dd/yyyy format.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = date;
		}

	}
	
	return true;
}

function isIdentical(fld1, fld2, label1, label2) {

	if (fld1.value !== fld2.value) {
		alert(label1+' does not match '+label2+'.');
		fld2.focus();
		fld2.select();
		return false;
	} else {
		if (!isRequired(fld1, label1)) return false;
	}
	
	return true;
}

function isReserved(fld, name, value) {

	if (fld.value.length) {
		reserved = Array('guest','admin');
	
		val = fld.value.toLowerCase();
		val = escape(val);
		
		for (i=0;i<reserved.length;i++) {
			if (value != reserved[i]) {
				if (val == reserved[i]) {
					if (val != value) {
						alert(name+' is a reserved word and cannot be used!');
						fld.focus();
						fld.select();
						return false;
					}
				}
			} else {
				fld.value = value;
			}
		}
	
		if (val.indexOf('%20', 0) >= 0) {
			alert(name+' must not contain spaces.');
			fld.focus();
			fld.select();
			return false;
		}

	}
	
	// if (!isRequired(fld, name)) return false;
	
	return true;
}





function _removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]|[a-zA-Z]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function _validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2,4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return ''; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) //find date separator
		if (!isNaN(strSeparator)) {
			var strSeparator = strValue.substring(1,2) //find date separator
		}
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = arrayDate[1]; // if (intDay < 10) { intDay = '0' + intDay; }
    var intYear = arrayDate[2]; 
    var intMonth = arrayDate[0]; // if (intMonth < 10) { intMonth = '0' + intMonth; }

    if (intYear == '00') { intYear = '2000'; } else if (intYear < 50) { intYear = parseInt(intYear) + 2000; } else if (intYear > 50 && intYear < 100) { intYear = parseInt(intYear) + 1900; }
    
    //check if month value and day value agree
    if(arrayLookup[intMonth] != null) {
      if(intDay <= arrayLookup[intMonth] && intDay != 0)
        return intMonth+'/'+intDay+'/'+intYear; //found in lookup table, good date
    }

    //check for February
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return intMonth+'/'+intDay+'/'+intYear; //Feb. had valid number of days
  }
  return ''; //any other values, bad date
}


/**
 * Checks whether a given credit card number has a valid Luhn checksum.
 * This allows you to spot most randomly made-up or garbled credit card numbers immediately.
 * Reference: http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html
 */
function isLuhn(fld, name) {
		
		//fld.value.replace(new RegExp('/^0-9/'));
		
		if (isLuhnNum(fld.value)) {
				var no_digit = fld.value.length;
				var oddoeven = no_digit & 1;
				var sum = 0;
				for (var count = 0; count < no_digit; count++) {
						var digit = parseInt(fld.value.charAt(count));
						if (!((count & 1) ^ oddoeven)) {
								digit *= 2;
								if (digit > 9) digit -= 9;
						};
						sum += digit;
				};
				if (sum == 0) {
					alert(name+' does not appear to be a valid format. Check that the correct amount of digits are in place. We will remove all non-numeric characters automtically.');
					fld.focus();
					fld.select();
					return false;
				}
				if (sum % 10 == 0) return true;
		};
		alert(name+' does not appear to be a valid format. Check that the correct amount of digits are in place. We will remove all non-numeric characters automtically.');
		fld.focus();
		fld.select();
		return false;
}

function isLuhnNum(argvalue) {
		argvalue = argvalue.toString();
		if (argvalue.length == 0) {
				return false;
		}
		for (var n = 0; n < argvalue.length; n++) {
				if ((argvalue.substring(n, n+1) < "0") ||
						(argvalue.substring(n,n+1) > "9")) {
						return false;
				}
		}
		return true;
}

