function validateForm(step){
	var err_count = 0;

	//shipping, billing, confirm

	if(step == "buy_seals"){
		var this_form = document.tampertabsealform;
		
		//VALIDATE QUANTITY
		if(isEmpty(this_form.quantity.value) || !isNumeric(this_form.quantity.value)){
			err_count++;
			hasErrors('tampertabseal_err',true);
		}else{
			hasErrors('tampertabseal_err',false);
		}
		
		//check for errors and alert the user, or submit the form
		if(err_count == 0){
			this_form.submit();
		}else{
			return false;
		}
	
	} else if(step == "buy_locks"){
		var this_form = document.signaturesealform;
		
		//VALIDATE QUANTITY
		if(isEmpty(this_form.quantity.value) || !isNumeric(this_form.quantity.value)){
			err_count++;
			hasErrors('signatureseal_err',true);
		}else{
			hasErrors('signatureseal_err',false);
		}
			
		//check for errors and alert the user, or submit the form
		if(err_count == 0){
			this_form.submit();
		}else{
			return false;
		}
	
	} else if(step == "shipping"){
		var this_form = document.checkout;
		
		//VALIDATE METHOD
		if(isEmpty(this_form.shipmethod.value) || !isNumeric(this_form.shipmethod.value)){
			err_count++;
			hasErrors('shipmethod_err',true);
		}else{
			hasErrors('shipmethod_err',false);
		}

		//VALIDATE FIRSTNAME
		if(isEmpty(this_form.name.value) || !isName(this_form.name.value)){
			err_count++;
			hasErrors('name_err',true);
		}else{
			hasErrors('name_err',false);
		}
		
		//VALIDATE EMAIL
		if(isEmpty(this_form.email.value) || !isEmail(this_form.email.value)){
			err_count++;
			hasErrors('email_err',true);
		}else{
			hasErrors('email_err',false);
		}
		
		//VALIDATE ADDRESS 1
		if(isEmpty(this_form.address_1.value) ){
			err_count++;
			hasErrors('address_1_err',true);
		}else{
			hasErrors('address_1_err',false);
		}
		
		
		//VALIDATE CITY
		if(isEmpty(this_form.city.value) || !isAlphaNumeric(this_form.city.value)){
			err_count++;
			hasErrors('city_err',true);
		}else{
			hasErrors('city_err',false);
		}
	
		//VALIDATE STATE
		if(isEmpty(this_form.state.value)){
			err_count++;
			hasErrors('state_err',true);
		}else{
			hasErrors('state_err',false);
		}
	
		//VALIDATE ZIPCODE
		if(isEmpty(this_form.zipcode.value) || !isZip(this_form.zipcode.value)){
			err_count++;
			hasErrors('zipcode_err',true);
		}else{
			hasErrors('zipcode_err',false);
		}
		
		//VALIDATE PHONE 1
		if(isEmpty(this_form.phone1.value)  ){
			err_count++;
			hasErrors('phone1_err',true);
		}else{
			hasErrors('phone1_err',false);
		}
		
			
		//check for errors and alert the user, or submit the form
		if(err_count == 0){
			this_form.submit();
		}else{
			return false;
		}
	
	}else if(step == "billing"){
		var this_form = document.checkout;

		if(this_form.method[0].checked){
			hasErrors('method_err',false);
				
			//VALIDATE FIRST NAME
			if(isEmpty(this_form.billfirstname.value) || !isName(this_form.billfirstname.value)){
				err_count++;
				hasErrors('first_err',true);
			}else{
				hasErrors('first_err',false);
			}
				
			//VALIDATE LAST NAME
			if(isEmpty(this_form.billlastname.value) || !isName(this_form.billlastname.value)){
				err_count++;
				hasErrors('last_err',true);
			}else{
				hasErrors('last_err',false);
			}
				
			//VALIDATE ADDRESS
			if(isEmpty(this_form.billaddress.value) ){
				err_count++;
				hasErrors('address_err',true);
			}else{
				hasErrors('address_err',false);
			}
		
		
			//VALIDATE CITY
			if(isEmpty(this_form.billcity.value) ){
				err_count++;
				hasErrors('city_err',true);
			}else{
				hasErrors('city_err',false);
			}
		
			//VALIDATE STATE
			if(isEmpty(this_form.billstate.value)){
				err_count++;
				hasErrors('state_err',true);
			}else{
				hasErrors('state_err',false);
			}
			
			//VALIDATE ZIPCODE
			if(isEmpty(this_form.billzipcode.value) || !isZip(this_form.billzipcode.value) || !isNumeric(this_form.billzipcode.value)){
				err_count++;
				hasErrors('zipcode_err',true);
			}else{
				hasErrors('zipcode_err',false);
			}
			
			//VALIDATE CREDIT CARD NUMBER
			if(isEmpty(this_form.creditcardnumber.value) || !isNumeric(this_form.creditcardnumber.value) || !isValidCreditCardNumber(this_form.creditcardnumber.value,this_form.creditcardtype.value)){
				err_count++;
				hasErrors('creditcardnumber_err',true);
			}else{
				hasErrors('creditcardnumber_err',false);
			}
			
			//VALIDATE CVV2 NUMBER
			if(isEmpty(this_form.cvv2number.value) || (this_form.cvv2number.value.length < 3) || !isNumeric(this_form.cvv2number.value)){
				err_count++;
				hasErrors('cvv2number_err',true);
			}else{
				hasErrors('cvv2number_err',false);
			}
		}else if(this_form.method[1].checked){
			hasErrors('method_err',false);

			//nothing to validate yet
		}else{
			err_count++;
			hasErrors('method_err',true);
		}
		
		//check for errors and alert the user, or submit the form
		if(err_count == 0){
			this_form.submit();
		}else{
			return false;
		}
	
	}else if(step == "confirm"){
		var this_form = document.checkout;
		
		//check for errors and alert the user, or submit the form
		if(err_count == 0){
			this_form.submit();
		}else{
			return false;
		}
	
	}else{
		this_form.submit();
	}
}

function isValidCreditCardNumber(cardNumber, cardType){
	var isValid = false;
	var ccCheckRegExp = /[^\d ]/;
	isValid = !ccCheckRegExp.test(cardNumber);
	
	if (isValid)
	{
		var cardNumbersOnly = cardNumber.replace(/ /g,"");
		var cardNumberLength = cardNumbersOnly.length;
		var lengthIsValid = false;
		var prefixIsValid = false;
		var prefixRegExp;
	
		switch(cardType)
		{
			case "MasterCard":
				lengthIsValid = (cardNumberLength == 16);
				prefixRegExp = /^5[1-6]/;
				break;
	
			case "Visa":
				lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
				prefixRegExp = /^4/;
				break;
	
			case "Amex":
				lengthIsValid = (cardNumberLength == 15);
				prefixRegExp = /^3(4|7)/;
				break;
	
			case "Discover":
				lengthIsValid = (cardNumberLength == 16);
				prefixRegExp = /^6011/;
				break;
	
			default:
				prefixRegExp = /^$/;
				return 0;
		}
	
		prefixIsValid = true;//prefixRegExp.test(cardNumbersOnly);
		isValid = prefixIsValid && lengthIsValid;
	}
	
	if (isValid)
	{
		var numberProduct;
		var numberProductDigitIndex;
		var checkSumTotal = 0;
	
		for (digitCounter = cardNumberLength - 1; digitCounter >= 0; digitCounter--)
		{
			checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
			digitCounter--;
			numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
			for (var productDigitCounter = 0; productDigitCounter < numberProduct.length; productDigitCounter++)
			{
				checkSumTotal += 
				parseInt(numberProduct.charAt(productDigitCounter));
			}
		}
	
		isValid = (checkSumTotal % 10 == 0);
	}
	
	return true;
}

function removeBad(InStr){
    InStr = InStr.replace(/\</g,"");
    InStr = InStr.replace(/\>/g,"");
    InStr = InStr.replace(/\"/g,"");
    InStr = InStr.replace(/\'/g,"");
    InStr = InStr.replace(/\%/g,"");
    InStr = InStr.replace(/\;/g,"");
    InStr = InStr.replace(/\(/g,"");
    InStr = InStr.replace(/\)/g,"");
    InStr = InStr.replace(/\&/g,"");
    InStr = InStr.replace(/\+/g,"");
    return InStr;
}

//age check
function twoDigits(dig){
	var str = dig.toString();
	var digit = (str.length == 2) ? str : '0'+str;
	return digit;
}


function realMonth(mm){
	var realmonth = (mm < 12) ? mm + 1 : mm = 1;
	return realmonth;
}
			
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}
	
// returns true if the string only contains characters A-Z, a-z or 0-9 or . or #
function isAddress(str){
	var re = /[^a-zA-Z0-9\#\.]/g
	if (re.test(str)) return true;
	return false;
}
	
// returns true if the string is 5 digits
function isZip(str){
	var re = /\d{5,}/;
	if(re.test(str)) return true;
	return false;
}
	
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return true;
	return false;
}

// returns true if string follows proper name conventions A-Z or - or ' or space 
function isName(str){
	var re = /^[a-zA-Z-'\s]*$/
	if (re.test(str)) return true;
	return false;
}
	
// returns true if the string only contains characters A-Z or a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9-'\s]/g
	if (re.test(str)) return false;
	return true;
}

function isNumeric(str){
	var re = /^\d+$/
	if (re.test(str)) return true;
	return false;
}

function isEmpty(str){
	if(str.length == 0 || str == null){
		return true;
	}else{
		return false;
	}
}
	
function isEmail(str){
	if(str == '') return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
	
function stripWhitespace(str, replacement){
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}

//function for showing inline error bubble
function hasErrors(fieldID, err){
	if(err){
		document.getElementById(fieldID).style.visibility = 'visible';
	}else{
		document.getElementById(fieldID).style.visibility = 'hidden';
	}
}
