/* paypal.js 
 * 
 * Functions to handle form processing on the  Vox  Photographs  website.  This 
 * file will shortly be moved out the HTML file and included. 
 * 
 * James Cradock <jim@yellahoose.com> 
 * 
 * April 12, 2007
 */ 

// Number of decimal places to show for calculated values. 
var DECIMAL_PLACES = 2; 
// State o' Maine sales tax rate. I don't know. It *may* change... 
var MAINE_SALES_TAX = 0.05; 

// Try to submit the form. 
function submitMe(fObj) { 
	// Need form object. 
	if (! fObj) 
		return false; 
	// Are all required data entered? 
	gotRequired(fObj); 

} 

// Remove  whitespace,  e.g.,  spaces,  tabs  stops,  carriage  returns,   from
// beginning and end of string. 
function trim(s) { 
	if (s == "") {
		return s; 
	} else {
		s = s.replace(/^\s/g, ""); 
		s = s.replace(/\s$/g, ""); 
		return s; 
	}
} 
 
// Did the user select "Maine" from the list of the state  select  (drop-down)?
// If they did, they've got to pay tax. 
function gotMaine(fObj) { 
	// Need form object. 
	if (! fObj) 
		return false; 
	// Only really care if the user indicates they're from Vacationland. 
	if (fObj.State[fObj.State.selectedIndex].value == "ME")  
		return true; 
	return false; 
} 

// Determine how many photographs the user is buying. 
function getTotalQuantity(fObj) { 
	// Initialize total for line. 
	var tq = 0; 
	// Need form object. (Repeatingf self.)  
	if (! fObj)  
		return false; 
	// Loop over entered quantities. 
	for (var i = 1; i <= 6; i++) { 
		eval("var q = fObj.Quantity_"+ i +".value"); 
		if (q != "" && ! isNaN(q))  
			tq = parseFloat(tq) + parseFloat(q); 
	} 
	return tq;
} 

// Determine if all required data are entered. This  function  should  only  be
// when the user is trying to process their order. 
function gotRequired(fObj) { 
	// Need form object. (Repeatingf self.)  
	if (! fObj)  
		return false; 
	// Check that basic shipping and contact information are entered. 
	if (trim(fObj.Name.value) == "" || trim(fObj.Phone.value) == "" || trim(fObj.Address.value) == "" || trim(fObj.City.value) == "" || trim(fObj.State[fObj.State.selectedIndex].value) == "" || trim(fObj.Zip.value) == "") { 
		alert("Oops! Not all data required to completed your order has been entered! Required data are:\n\n- Name\n- Phone\n- Address\n- City\n- State\n- Zip");
		return; 
	} 
	if (fObj.whichform.value != "Linden") { 
		// Now check that all product information was  entered.  This  means  Item #,
		// Title and Size. 
		for (var i = 1; i < 6; i++) { 
			// Initialize a variable to tally the number of products. 
			var p = 0; 
			eval("var q = fObj.Quantity_"+ i +".value");
			// If there's a quantity that chances aer pretty high that the user is try-
			// ing to purchase something. 
			if (q != "" && ! isNaN(q)) { 
				// Get some variables. Need to check these for data. 
				eval("var ino = fObj.ItemNo_"+ i +".value"); 
				eval("var t = fObj.Title_"+ i +".value"); 
				eval("var s = fObj.Size_"+ i +".value"); 
				eval("var p = fObj.Price_"+ i +".value"); 
				// And then check that data are available. If not, return a nice  message
				// explaining that the user needs to identify what it is  they're  trying
				// to purchase. 
				if (trim(ino) == "" || trim(t) == "" || trim(s) == "" || trim(p) == "") { 
					alert("Oops! Not all data required to complete your order has been entered! Required data are:\n\n- Qty\n- Item #\n- Title\n- Size\n- Price"); 
					return; 
				} 
			} 
		} 
	} 
	if (fObj.whichform.value != "Linden") { 
		// Finally, check that a shipping method was indicated. 
		if (! fObj.ShippingMethod[0].checked && ! fObj.ShippingMethod[1].checked) {
			alert("Oops! Not all data required to complete your order has been entered! Required data are:\n\n- Shipping Method"); 
			return; 
		}
	} 
	// Is the email as entered valid-looking? 
	if (trim(fObj.Email.value) != "" && ! fObj.Email.value.match(/^[A-Za-z0-9_\+-]+(\.[A-Za-z0-9_\+-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.([A-Za-z]{2,4})$/)) { 
		alert("Oops! The email address you are trying to use to complete your order is not valid! Please re-enter the email address in the following format:\n\nyourname@domain.com"); 
		return; 
	} 	
	// Ok. Got this far. Must be ok to try to process. 
	fObj.submit(); 
} 
 
// Calculate line-by-line total. 
function calcTotal(fObj, lineNo) { 
	// Initialize total for line. 
	var t = 0; 
	// Need form object. 
	if (! fObj) { 
		eval("fObj.Total_"+ lineNo +".value = t.toFixed(DECIMAL_PLACES)"); 
		return; 
	} 
	// Get quantity and prices the user's entered. 
	eval("var q = fObj.Quantity_"+ lineNo +".value"); 
	eval("var p = fObj.Price_"+ lineNo +".value"); 
	// Strip unnecessary characters off the value. 
	q = q.replace(/(\$|\,)/g, ''); 
	p = p.replace(/(\$|\,)/g, ''); 
	// Need quantity and price. 
	if (q == "" || p == "" || isNaN(q) || isNaN(p)) { 
		eval("fObj.Total_"+ lineNo +".value = t.toFixed(DECIMAL_PLACES)"); 
		return; 
	} 
	// Calculate the total. 
	t = parseFloat(p) * parseFloat(q); 
// Palladium print check? 
//eval("var pp = fObj.PalladiumPrint_"+ lineNo); 
//if (pp.checked)  
//	t = parseFloat(t) + (parseFloat(pp.value) * parseFloat(q)); 
	// Book mat check? 
	eval("var bm = fObj.BookMat_"+ lineNo); 
	if (bm.checked)  
		t = parseFloat(t) + (parseFloat(bm.value) * parseFloat(q)); 
	// Set the line total. 
	eval("fObj.Total_"+ lineNo +".value = t.toFixed(DECIMAL_PLACES)"); 
	// Set the subtotal. 
	calcSubTotal(fObj); 
	// Set the tax. 
	calcTax(fObj); 
	// Set the shipping. 
	calcShipping(fObj); 
	// Set the grand total. 
	calcGrandTotal(fObj); 
	return; 
} 

// Calculate the subtotal based on all N lines. 
function calcSubTotal(fObj) { 
	// Initialize subtotal. 
	var st = 0; 
	// Loop over lines. 
	for (var i = 1; i <= 6; i++) { 
		eval("var t = fObj.Total_"+ i +".value");
		if (t != "" && ! isNaN(t))  
			st = parseFloat(st) + parseFloat(t); 
	} 
	fObj.SubTotal.value = st.toFixed(DECIMAL_PLACES); 
} 

// Calculate the tax -- for users indicating they're  in  the  great  state  of 
// Maine only. 
function calcTax(fObj) { 
	// Initialize tax. 
	var t = 0; 
	// Need form object. 
	if (! fObj) { 
		fObj.Tax.value = t.toFixed(DECIMAL_PLACES); 
		return; 
	} 
	// Did the user indicate they were in Vacationland? 
	if (gotMaine(fObj)) { 
		if (fObj.SubTotal.value != "" && ! isNaN(fObj.SubTotal.value))  
			t = parseFloat(fObj.SubTotal.value) * parseFloat(MAINE_SALES_TAX); 
	} 
	// Set the tax. 
	fObj.Tax.value = t.toFixed(DECIMAL_PLACES); 
	return; 
} 

// Calculate shipping. To do this, need to determine how many prints  the  user
// is trying to purchase. 
function calcShipping(fObj) { 
	// Initialize shipping. 
	var s = 0; 
	// Need form object. 
	if (! fObj) { 
		fObj.Shipping.value = s.toFixed(DECIMAL_PLACES); 
		return; 
	} 
	// Get total number of prints. 
	var q = getTotalQuantity(fObj); 
	// What shipping method did the user select? 
	if (fObj.ShippingMethod[0].checked) { 
		if (q > 1) 
			s = parseFloat(8) + parseFloat((q - 1) * 2);
		else 
			s = 8; 
	} else if (fObj.ShippingMethod[1].checked) { 
		if (q > 1) 
			s = parseFloat(10) + parseFloat((q - 1) * 2);
		else 
			s = 10; 
	} 
	// Set the shipping. 
	fObj.Shipping.value = s.toFixed(DECIMAL_PLACES); 
} 

// Calculate grand total. 
function calcGrandTotal(fObj) { 
	// Initialize grand total. 
	var gt = 0; 
	// Need form object. 
	if (! fObj) { 
		fObj.GrandTotal.value = s.toFixed(DECIMAL_PLACES); 
		return; 
	} 
	// Calculate the total. 
	(fObj.SubTotal.value!=""&&!isNaN(fObj.SubTotal.value))?gt=parseFloat(gt)+parseFloat(fObj.SubTotal.value):gt=gt+0; 
	(fObj.Tax.value!=""&&!isNaN(fObj.Tax.value))?gt=parseFloat(gt)+parseFloat(fObj.Tax.value):gt=gt+0;
	(fObj.Shipping.value!=""&&!isNaN(fObj.Shipping.value))?gt=parseFloat(gt)+parseFloat(fObj.Shipping.value):gt=gt+0;
	// Set the grand total. 
	fObj.GrandTotal.value = gt.toFixed(DECIMAL_PLACES); 
} 
