
if( document.all && !document.getElementById ) {
    document.getElementById = function( id ) {
         return document.all[ id ];
    }
}

function isNotEmpty( field ){
	return document.getElementById( field ).value.length > 0
}

function isValidEmail( field ){
	var value = document.getElementById( field ).value;

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(value))) {
	       var error = "Please enter a valid email address.\n";
	}

	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (value.match(illegalChars)) {
	   var error = "The email address contains illegal characters.\n";
	}

	if( error ){
		alert( error );
		return false;
	}

	return true;

}


function isValidPhone( field ){
	var value = document.getElementById( field ).value;

	var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters


	if (isNaN(parseInt(stripped))) {
	   var error = "The phone number contains illegal characters.";
	}

	if (!(stripped.length == 10)) {
		var error = "The phone number is the wrong length. Make sure you included an area code.";
	}

	if( error ){
		alert( error );
		return false;
	}

	return true;
}

function isValidPhoneOptional( field ){
	var value = document.getElementById( field ).value;

	if( ! value ) return true;

	return isValidPhone( field );

}



function isValidAddress( field ){
	var value = document.getElementById( field ).value;

	var emailFilter=/^.*PO ?Box/i;
	if ( emailFilter.test(value) ) {
	       var error = "We cannot ship to a PO Box. (UPS cannot deliver to a PO box.)\n";
	}

	if( error ){
		alert( error );
		return false;
	}

	return true;
}

function isValidZip( field ){
	var value = document.getElementById( field ).value;


	var stripped = value.replace(/[\-\ ]/g, '');
	//strip out acceptable non-numeric characters

	if (isNaN(parseInt(stripped))) {
	   var error = "The zip code contains illegal characters.";
	}

	if ( ! stripped.length == 9 && ! stripped.length == 5 ) {
		var error = "The zip code is the wrong length.";
	}


	if( error ){
		alert( error );
		return false;
	}

	return true;
}




function validate(){

	var good = true;

	good = good && isNotEmpty( 'FirstName' );
	good = good && isNotEmpty( 'LastName' );
	good = good && isNotEmpty( 'Email' );
	good = good && isNotEmpty( 'PhoneMain' );
	good = good && isNotEmpty( 'Address1' );
	good = good && isNotEmpty( 'City' );
	good = good && isNotEmpty( 'State' );
	good = good && isNotEmpty( 'Zip' );

	if( ! good ){
		alert( "A required field is missing." );
		return false;
	}

	if( ! isValidAddress( 'Address1' ) ) return false;
	if( ! isValidAddress( 'Address2' ) ) return false;
	if( ! isValidEmail( 'Email' ) ) return false;
	if( ! isValidPhone( 'PhoneMain' ) ) return false;
	if( ! isValidPhone( 'PhoneMain' ) ) return false;
	if( ! isValidPhoneOptional( 'PhoneAlt' ) ) return false;
	if( ! isValidZip( 'Zip' ) ) return false;

}


function submitAddToCart(){
	document.getElementById("addToCart").submit();
}




