

// Software design and implementation by Dave Godwin.
// All rights reserved.


// Attempt to print the screen
function PrintPage()
{
	if (window.print)
	{
		window.print();
	}

	else
	{
		alert("Your browser software does not support this feature.");
	}

	return false;
}


// Call up a new window containing the ISO country code table
function CountryWindow(url, fieldName)
{
	// Open a window for the specified URL
	var cWin = window.open(url + "/country.html" + "?field=" + fieldName,
						   "infoWindow", 
						   "height=500,width=400," +
						   "screenX=300,left=300," +
						   "screenY=100,top=100," +
						   "scrollbars"); 
						   
	// Make sure the window has focus
	cWin.focus();
}


// Call up a new window containing the specified URL
function NoteWindow(fileName)
{
	// Open a window for the specified URL
	var nWin = window.open("http://" + window.location.hostname + "/" +
						   fileName, 
						   "infoWindow", 
						   "height=450,width=400," +
						   "screenX=400,left=400," +
						   "screenY=100,top=100," +
						   "scrollbars"); 
						   
	// Make sure the window has focus
	nWin.focus();
}


// Called by checkout form to make sure a form field has some value in it
function ValidateNotEmpty(description, field)
{
	// Internal consistancy check
	if (!field)
	{
		return false;
	}

	// Check the length of the value typed in
	if (field.value.length == 0)
	{
		alert("The " + description + " field must be filled in.");

		// Force the cursor back to this field
		field.focus();

		return false;
	}

	return true;
}

// Called by checkout to validate subscribe radio button 5/17/04

function ValidateSubscribe(field)
{
	var subscribe = document.checkoutForm.Subscribe;
	var i;
	var subscribe_choice = false;

		// Loop from zero to the one minus the number of radio button selections
	for (i = 0; i < subscribe.length; i++)
		{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (subscribe[i].checked)
		subscribe_choice = true; 
		}

		if (!subscribe_choice)
		{
		// If there were no selections made display an alert box 
		alert("Please make a Newsletter Subscribe selection.");

		// Force the mouse back to this field
		subscribe[0].focus();

		return false;
		}

	return true;
}


// Called by checkout form to make sure state value has been filled in
function ValidateState(description, state, country)
{
	// Only check state value for US and Canada
	if (((country.value == 'US') || (country.value == 'CA')) &&
		(state.value.length == 0))
	{
		alert("The " + description + " field must be filled in.");

		// Force the cursor back to this field
		state.focus();

		return false;
	}

	return true;
}


// Called by checkout form to ensure a credit card number was given if needed
function ValidateCardNumber(field)
{
	var payby = document.checkoutForm.Payby;
	var cardno = document.checkoutForm.Cardno;
	var i;

	// Scan the payby setting
	for (i = 0; i < payby.length; i++)
	{
		// Was credit selected but the card number left blank?
		if (payby[i].checked && 
			(payby[i].value == 'CREDIT') &&
			(cardno.value.length == 0))
		{
			alert("The Credit Card Number field must be filled in.");

			// Force the mouse back to this field
			field.focus();

			return false;
		}
	}

	return true;
}


// Turn access on or off for all forms fields used for credit card data entry
function SetCreditFields(active)
{
	// Do we need to disabel the credit card fields?
	disable = (active != 'CREDIT') ? true : false;

	// Set all card field disabled properties
	document.checkoutForm.Cardtype.disabled = disable;
	document.checkoutForm.Cardno.disabled = disable;
	document.checkoutForm.Expmonth.disabled = disable;
	document.checkoutForm.Expyear.disabled = disable;
}


// Decide whether or not the shipping information is the same as
// the billing information
function SetShippingFields(useBilling)
{
	var df = document.checkoutForm;
	var fieldList = new Array('title', 'fname', 'lname', 'company',
							  'street1', 'street2', 'city', 'state',
							  'zip', 'country');
	var field;
	var billingField;
	var shippingField;
	
	// Handle all form fields associated with shipping information
	for (field = 0; field < fieldList.length; field++)
	{
		// Create access variables for the shipping and billing fields
		billingField = eval('df.' + fieldList[field]);
		shippingField = eval('df.ship_' + fieldList[field]);

		// Is the shipping information the same as the billing information?
		if (useBilling)
		{
			// Is this field a select list?
			if (shippingField.type == 'select-one')
			{
				// Copy the billing information
				shippingField.selectedIndex = billingField.selectedIndex;
			}

			// Assume this field has a value property
			else
			{
				// Copy the billing information
				shippingField.value = billingField.value;
			}

			// Make this field uneditable
			shippingField.disabled = true;
		}

		// The shipping and billing information are different
		else
		{
			// Is this field a select list?
			if (shippingField.type == 'select-one')
			{
				// Reset the list selector
				shippingField.selectedIndex = 0;
			}

			// Assume this field has a value property
			else
			{
				// Clear the field value
				shippingField.value = '';
			}

			// Allow this field to be edited
			shippingField.disabled = false;
		}
	}
}


function CheckoutSubmitCheck()
{
	var df = document.checkoutForm;
	var billing;
	var shipping = true;

	// Validate all billing information
	billing =  ValidateNotEmpty('First Name', df.fname) &&
			   ValidateNotEmpty('Last Name', df.lname) &&
			   ValidateNotEmpty('Street Address', df.street1) &&
			   ValidateNotEmpty('City', df.city) &&
			   ValidateState('State', df.state, df.country) &&
			   ValidateNotEmpty('Zipcode', df.zip) &&
			   ValidateNotEmpty('Email', df.email) &&
			   ValidateCardNumber(df.Cardno) &&
			   ValidateSubscribe(df.Subscribe);

	// If we need to, validate shipping information.
	// Confirm all needed fields exist first.
	if (billing && df.ship_same && df.ship_same.checked && 
		(df.ship_same.checked != true))
	{
		shipping = ValidateNotEmpty('First Name', df.ship_fname) &&
				   ValidateNotEmpty('Last Name', df.ship_lname) &&
				   ValidateNotEmpty('Street Address', df.ship_street1) &&
				   ValidateNotEmpty('City', df.ship_city) &&
				   ValidateState('State', df.ship_state, df.ship_country) &&
				   ValidateNotEmpty('Zipcode', df.ship_zip);
	}

	// Allow form submission if both billing and shipping data are OK
	return billing && shipping;
}


// Called in response to the item form submission event.
function OnAddItems(formName)
{
	var quantityField;
	var quantityMin;
	var quantityMax;
	var quantity;
	var total = 0;
	var limit;
	var name;
	var message;
	var i;

	// Web TV can't deal, so bail
	if (is.webtv)
	{
		// Allow submission.  Server will catch errors.
		return true;
	}

	// Use default form name if no name was specified
	if (typeof(formName) == 'undefined')
	{
		formName = 'itemForm';
	}

	// The first quantity field has no number
	quantityField = eval('document.' + formName + '.QTY');
	quantityMin   = eval('document.' + formName + '.QTY_MIN');
	quantityMax   = eval('document.' + formName + '.QTY_MAX');
	name          = eval('document.' + formName + '.ITEM_NAME');

	// Check every field on the form
	for (i = 1; i <= document.itemForm.length; i++)
	{
		// Does this quantity field exist?
		if (quantityField)
		{
			// Get the number of this item ordered
			quantity = parseInt(quantityField.value);

			// Is there a minimum quantity field?
			if (quantityMin)
			{
				// Get the lower limit value
				limit = parseInt(quantityMin.value);

				// Is the quantity value below the limit ?
				if ((quantity > 0) && (quantity < limit))
				{
					// Construct and display a pop-up message
					message = 'The minimum quantity allowed';
					if (name)
					{
						message += ' for ' + name.value;
					}
					alert(message + ' is ' + limit + '.');

					// Alter the form value and put the cursor there
					quantityField.value = limit;
					quantityField.focus();

					// Abort the form submission
					return false;
				}
			}

			// Is there a maximum quantity field?
			if (quantityMax)
			{
				// Get the upper limit value
				limit = parseInt(quantityMax.value);

				// Is the quantity value above the limit ?
				if (quantity > limit)
				{
					// Construct and display a pop-up message
					message = 'The maximum quantity allowed';
					if (name)
					{
						message += ' for ' + name.value;
					}
					alert(message + ' is ' + limit + '.');

					// Alter the form value and put the cursor there
					quantityField.value = limit;
					quantityField.focus();

					// Abort the form submission
					return false;
				}
			}

			// Total up the number of items ordered
			total += quantity;
		}

		// Generate the names of the next set of item fields
		quantityField = eval('document.' + formName + '.QTY' + i);
		quantityMin   = eval('document.' + formName + '.QTY_MIN' + i);
		quantityMax   = eval('document.' + formName + '.QTY_MAX' + i);
		name          = eval('document.' + formName + '.ITEM_NAME' + i);
	}
	
	// If nothing was ordered, deny the form submission
	if (total == 0)
	{
		alert('You must select at least one item to place in your shopping cart.');

		return false;
	}

	// Allow the form submission
	return true;
}


// Called when the quantity up/down buttons are pressed on the product page.
function OnChangeQuantity(delta)
{
	// Create a reference to the quantity field
	var quantity = document.itemForm.qty;

	// Give a new value to the quantity field
	quantity.value = Math.max(Math.round(quantity.value) + delta, 1);
}


