var message, fieldtofocus, passed, problemMsg, creditCardMsg;
var skipField = false;  // can not set focus on these fields
// Master function that checks all the pieces 
function isOrderReady() {
	passed = true;  
	fieldtofocus = "";
	message ="Please review the following required fields: \n";
	problemMsg = "";
	creditCardMsg = "";
	
// Checks to see that something has been ordered
			
	if (document.ordering.subtotal_b.value == "" || document.ordering.subtotal_b.value <= 0)  {
		if (document.ordering.subtotal_j.value == "" || document.ordering.subtotal_j.value <=  0)  {
			passed = false;
			message += "- - Select at least one Book or Journal \n";
			skipField = true;
		}
	}

// Collects the billing information
		if (document.ordering.customer_b.value == "") {
    		message += "- - Billing Name \n"; 
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.customer_b};
		}
					
    	if (document.ordering.address_b.value == "") {
    		message += "- - Billing Address \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.address_b};
    	}
	
    	if (document.ordering.city_b.value == "") {
    		message += "- - Billing City \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.city_b};
		}
	
		if (document.ordering.state_b.value == "") {
    		message += "- - Billing State \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.state_b};
		}
		
		if (document.ordering.zip_b.value == "") {
    		message += "- - Billing Zip \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.zip_b};
		}
		
		if (document.ordering.country_b.value == "") {
    		message += "- - Billing Country \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.country_b};
		}
		
		if (document.ordering.phone_b.value == "") {
    		message += "- - Billing Phone \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.phone_b};
		}

		var element = document.ordering.email_b;
		var result = isEmail(element);
		if (result)  {
			passed = false;
		if (fieldtofocus == "")  {fieldtofocus = document.ordering.email_b};
	}
		
		// Do some validation on the Email side of things
function isEmail(element)  {
	var inputStr = element.value;
	if (inputStr.length < 8)  {
		message += "- - Billing Email: should be at least 8 characters \n";
		return true;
	}	
	var charA = inputStr.indexOf("@");
	if (charA == -1)  {
		message += "- - Billing Email: did not find the @ character \n";
		return true;
	}
	if (charA < 2 )  {
		message += "- - Billing Email: expected at least 2 characters before the @ character \n";
		return true;
	}
	var charP = inputStr.lastIndexOf(".");
	if (charP == -1)  {
		message += "- - Billing Email: expected to find the . character \n";
		return true;
	}
	if (charP != inputStr.length - 3 && charP != inputStr.length - 4)  {
		message += "- - Billing Email: ending . character not in correct position \n";
		return true;
	}
	if (charP < charA + 3)  {
		message += "- - Billing Email: should have at least 2 characters between the @ and . characters \n";
		return true;
	}
	return false;
}
		
		
// Collects the ship to information
		if (document.ordering.shipto_billing.checked)  {  }
		else  {
		if (document.ordering.customer_s.value == "") {
    		message += "- - Ship to Name \n"; 
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.customer_s};
		}
					
    	if (document.ordering.address_s.value == "") {
    		message += "- - Ship to Address \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.address_s};
    	}
	
    	if (document.ordering.city_s.value == "") {
    		message += "- - Ship to City \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.city_s};
		}
	
		if (document.ordering.state_s.value == "") {
    		message += "- - Ship to State \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.state_s};
		}
		
		if (document.ordering.zip_s.value == "") {
    		message += "- - Ship to Zip \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.zip_s};
		}
		
		if (document.ordering.country_s.value == "") {
    		message += "- - Ship to Country \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.country_s};
		}
		
		if (document.ordering.phone_s.value == "") {
    		message += "- - Ship to Phone \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.phone_s};
		}
		}
		
	
		if (document.ordering.creditcard[0].checked == false && document.ordering.creditcard[1].checked == false && document.ordering.creditcard[2].checked == false) {
    		message += "- - Payment Credit Card Type \n";
			passed = false;
			skipField = true;
		}
		
		if (document.ordering.cardnumb.value == "") {
    		message += "- - Payment Credit Card Number \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.cardnumb};
		}
		
		if (document.ordering.expiredate.value == "") {
    		message += "- - Payment CC Expiration Date \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.ordering.expiredate};
		}
	
	if (passed == true)  {
		document.ordering.submit();
	}
	if (passed == false)  {
		fixFieldInfo();
	}
}

function fixFieldInfo() {
	alert(message + creditCardMsg + problemMsg);
	if (skipField == false)  {
		fieldtofocus.focus();
	}
}


