function errorInputText(id,str){
	if(!id.hasClass('error-display')){ //prevent multiple error displays on same element
		id.addClass('error-display'); //prevent displaying error twice
		//id.parent().append('<span class="error"><span>'+ str +'</span></span>');
		id.parent().prepend('<span class="error"><span>'+ str +'</span></span>');
	}
	if(!window.focused){ id.focus(); window.focused = true; } //focused on first error
	return true;
}

/*VALIDATE FORM*/
function validateForm(nonempty,matches,select_nonempty,checkbox,valid_email){
	$('.error').remove();
	$('input,select,textarea').removeClass('error-display'); //prevent multiple error displays on same element
	 window.focused = false; //remove true
	var errors = false;
	//validate nonempty fields
	if(nonempty !=''){
		var nonempty_array = nonempty.split( "," );
		for( var i=0; i < nonempty_array.length; i++ ){
			var nonempty_set = nonempty_array[i].split( ":" );
			if( $('#'+nonempty_set[0]).val().length < nonempty_set[1] ){
				errors = errorInputText($('#'+nonempty_set[0]),$('#'+nonempty_set[0]).attr('title') +' must be at least '+ nonempty_set[1] +' characters');
			}
		}
	}
	
	//validate matching fields
	if(matches !=''){
		var matches_array = matches.split( "," );
		for( var j=0; j < matches_array.length; j++ ){
			var matches_pair = matches_array[j].split( ":" );
			if($('#'+matches_pair[0]).val() != $('#'+matches_pair[1]).val()){
				errors = errorInputText($('#'+matches_pair[1]),$('#'+matches_pair[1]).attr('title') +' did not match '+ $('#'+matches_pair[0]).attr('title'));
			}		
		}
	}
	
	//validate select field is chosen
	if(select_nonempty !=''){
		var select_nonempty_array = select_nonempty.split( "," );
		for( var k=0; k < select_nonempty_array.length; k++ ){
			if( $('#'+select_nonempty_array[k]).val() ==''){
				errors = errorInputText($('#'+select_nonempty_array[k]),$('#'+select_nonempty_array[k]).attr('title') +' must be selected</span>');
			}
		}
	}
	//validate checkbox is chosen
	if(checkbox !=''){
		var checkbox_array = checkbox.split( "," );
		for( var l=0; l < checkbox_array.length; l++ ){
			if( $('#'+checkbox_array[l]).is(":not(:checked)")){
				errors = errorInputText($('#'+checkbox_array[l]),$('#'+checkbox_array[l]).attr('title') +' must be checked to continue');
			}
		}		
	}
	//validate email address
	if(valid_email !=''){
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);  
		if(!pattern.test( $('#'+valid_email).val() )){
			errors = errorInputText($('#'+valid_email),$('#'+valid_email).attr('title') +' is not valid');
		}  
	}
	if(errors){	return false; } 
	else {return true; }
}
/*VALIDATE FORM*/

function validateFormShipping(nonempty,matches,select_nonempty,checkbox,valid_email) {
  var errors = false;
  var noerrors = false;
	noerrors = validateForm(nonempty,matches,select_nonempty,checkbox,valid_email);
	
	if(noerrors) { //first validation cycle came back positive;
		var errorStartPos = 0;
		var intlCountry = 0;
		
		var countrySelected = $("#CountryID").val();			
		if (countrySelected != 'US' && countrySelected != 'CA' && countrySelected != 'AU') {
			intlCountry = 1;
		}
		
		// validate state, select menu as well as intl state text box
		if (($("#StatePU").val().length < 1) && ($("#StateTX").val().length < 1)) {

			if (intlCountry == 1) {
				errorInputText($('#StateTX'),'Please enter your State');
				errors = true;
			}
			else {						
				errorInputText($('#StatePU'),'Please select your State');
				errors = true;
				if (countrySelected == 'US') {
					$("#stShipUS").attr('selected',true);
					$("#StateTX").val('');
				}
				else if (countrySelected == 'CA') {
					$("#stShipCA").attr('selected',true);
					$("#StateTX").val('');
				}
				else if (countrySelected == 'AU') {
					$("#stShipAU").attr('selected',true);
					$("#StateTX").val('');
				}
			}
		}
		
		//validate if us state selected, country is United States
		if ( ($("#StatePU").val().length == 2) && (countrySelected != 'US' && countrySelected != 'CA') ) {
			if ($("#StateTX").val().length < 1) {
				errorInputText($('#StateTX'),'You have selected a North American State but an International Country');
				errors = true;
				$("#stShipIntl").attr('selected',true);
			}
		}
		// validate 2 char state is selected from state select menu for Country US
		if (countrySelected == 'US' && $("#StatePU").val().length != 2) {
			errorInputText($('#StatePU'),'Please select a valid US State');
			errors = true;
			$("#stShipUS").attr('selected',true);
			$("#StateTX").val('');
		}
		else if (countrySelected == 'CA' && $("#StatePU").val().length != 2) {
			errorInputText($('#StatePU'),'Please select a valid Canadian State');
			errors = true;
			$("#stShipCA").attr('selected',true);
			$("#StateTX").val('');
		}
}
else{errors = true}; //errors returned from validateForm() check

if (errors) {	return false;	}
else { return true; }

}
function validateFormBilling(nonempty,matches,select_nonempty,checkbox,valid_email) {
  	var errors = false;
  	var noerrors = false;
	noerrors = validateForm(nonempty,matches,select_nonempty,checkbox,valid_email);
	
	if(noerrors) { //first validation cycle came back positive;
		var errorStartPos = 0;

	  // check for a shipping selection if appropriate
	  /*
	  var shipSelectionArray = getElementById("rShipSelection");
	  if (shipSelectionArray && getElementById("rShipSelection").length >= 1) {
		  for (i=0;i<document.fBilling.rShipSelection.length;i++) {
				if (document.fBilling.rShipSelection[i].checked) {
					shippingSelection = document.fBilling.rShipSelection[i].value;
					if (!shippingSelection) {
						errorAlert += "- Please make a shipping selection before continuing\n";
					}
				}
		  }
	  }
	  */

		if($('#CreditCardNumber').length >0 && !($('#CreditCardNumber').val().match(/\*/g))){			
			
			if (checkCreditCard( $('#CreditCardNumber').val(),$('#PaymentTypeID option:selected').text() )) {}
			else {
				errorInputText($('#CreditCardNumber'),'Invalid Card Number');
				errors = true;
			}
		}
	}
	else{errors = true}; //errors returned from validateForm() check

	if (errors) {	return false;	}
	else { return true; }
}