function getPageSize() {   
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function isInteger(e) {
	var k = window.event ? e.keyCode : e.which;
	var s = String.fromCharCode(k);
	var r = /\d/;
	return r.test(s);
}

function isControlChar(e) {
	var k = window.event ? e.keyCode : e.which;
	//  0 == Firefox
	//  8 == delete
	//  9 == tab
	// 10 == \n
	// 13 == \r
	// 37 == left-arrow
	// 39 == right-arrow
	return k == 0 || k == 8 || k == 9 || k == 10 || k == 13 || k == 37 || k == 39;
}

function isCharacter(e, c) {
	var k = window.event ? e.keyCode : e.which;
	var s = String.fromCharCode(k);
	return c == s;
}

function setOption(id, bool) {
	$(id).value = (bool) ? 1 : 0;
}

function showSpecials() {
    var arrayPageSize = getPageSize();
    $('specials-overlay').setStyle({
    	width: arrayPageSize[0] + 'px',
    	height: arrayPageSize[1] + 'px'
    });
	$('specials-overlay').appear({
		duration: 0.2,
		from: 0.0,
		to: 0.8
	});
	$('specials').appear({
		duration: 0.2
	});
}

function hideSpecials() {
	$('specials').fade({
		duration: 0.2
	});
	$('specials-overlay').fade({
		duration: 0.2
	});
}

function setImage(id, url) {
	$(id).src = url;
}

function setPrice(type, displayId, qtyId, n) {
	var qty = $(qtyId).value;
	new Ajax.Updater(displayId, 'ajax/calc.php', {
		method: 'get',
		parameters: { type: type, length: n, qty: qty }
	});
}

function calcCheckout(page_id, source, shipping_price) {
	// Grab address information.
	var country = $(source + '-country').value;
	var zip = $(source + '-zip').value;
	
	// Show spinner.
    $(page_id + '-spinner').appear({
    	duration: 0.2,
		from: 0.0,
		to: 0.8
	});
	
	// Get tax.
	if ($(page_id + '-tax')) {
		new Ajax.Request('ajax/tax.php', {
			method: 'get',
			parameters: { country: country, zip: zip },
			onSuccess: function(transport) {
				// Update sales tax.
				var tax = transport.responseText;
				$(page_id + '-tax').value = tax;
			}
		});
	}
		
	// Get shipping methods and costs.
	new Ajax.Request('ajax/shipping.php', {
		method: 'get',
		parameters: { country: country, zip: zip },
		onSuccess: function(transport) {
			// Clear shipping choices.
			var shipping_select = $(page_id + '-shipping');
			for (i = shipping_select.length; i >= 0; --i) {
				shipping_select[i] = null;
			}
			
			// Update shipping choices.
			var gotShipping = false;
			var shipping = transport.responseText.evalJSON(true);
			for (var prop in shipping) {
				// Grab the method and price.
				var values = shipping[prop].split('-');
				var method = values[0];
				var price = parseFloat(values[1]);
				
				// Remember if we got valid shipping.
				if (price > 0) {
					gotShipping = true;
				}
				
				// Format the price for a nicer option label.
				var nice_price = price.toFixed(2);
				label = method + ' ($' + nice_price + ')';
				
				// Update the options.
				if (shipping_price) {
					shipping_select[shipping_select.length] = new Option(label, price);
				} else {
					shipping_select[shipping_select.length] = new Option(label, method);
				}
			}
			
			// Signal shipping choices have changed.
			if (shipping_select.onchange) {
				shipping_select.onchange();
			}
		},
		onComplete: function(transport) {
			// Hide spinner.
			$(page_id + '-spinner').fade({ duration: 0.2 });
		}
	});
}

function showPaypal() {
    var arrayPageSize = getPageSize();
    $('paypal-overlay').setStyle({
    	width: arrayPageSize[0] + 'px',
    	height: arrayPageSize[1] + 'px'
    });
	$('paypal-overlay').appear({
		duration: 0.2,
		from: 0.0,
		to: 0.8
	});
	$('paypal').appear({
		duration: 0.2
	});
}

function hidePaypal() {
	$('paypal').fade({
		duration: 0.2
	});
	$('paypal-overlay').fade({
		duration: 0.2
	});
}

function validatePaypal() {
	var elements = $('paypal-form').getElements();
	
	// All elements must be filled in, except for *-addr2.
	var complete = true;
	for (var i = 0; i < elements.length; ++i) {
		var id = elements[i].identify();
		if (id.endsWith('-addr2')) {
			continue;
		}
		if (!elements[i].present()) {
			complete = false;
			break;
		}
	}
	
	// If incomplete, display the fill form element. Otherwise hide.
	$('paypal-fill-form').style.visibility = (complete) ? 'hidden' : 'visible';
	return complete;
}


function flipState(id, country_code) {
	if (country_code == 'US') {
		$(id).innerHTML = 'State';
	} else if (country_code == 'CA') {
		$(id).innerHTML = 'Province';
	}
}

function flipZip(id, country_code) {
	if (country_code == 'US') {
		$(id).innerHTML = 'ZIP:';
	} else if (country_code == 'CA') {
		$(id).innerHTML = 'Postal Code:';
	}
}

var idx = 0;
var state_names = []; var state_codes = [];
state_names[idx]	='Alabama';
state_codes[idx++]	='AL';
state_names[idx]	='Alaska';
state_codes[idx++]	='AK';
state_names[idx]	='American Samoa';
state_codes[idx++]	='AS';
state_names[idx]	='Arizona';
state_codes[idx++]	='AZ';
state_names[idx]	='Arkansas';
state_codes[idx++]	='AR';
state_names[idx]	='California';
state_codes[idx++]	='CA';
state_names[idx]	='Colorado';
state_codes[idx++]	='CO';
state_names[idx]	='Connecticut';
state_codes[idx++]	='CT';
state_names[idx]	='Delaware';
state_codes[idx++]	='DE';
state_names[idx]	='District of Columbia';
state_codes[idx++]	='DC';
state_names[idx]	='Federated States of Micronesia';
state_codes[idx++]	='FM';
state_names[idx]	='Florida';
state_codes[idx++]	='FL';
state_names[idx]	='Georgia';
state_codes[idx++]	='GA';
state_names[idx]	='Guam';
state_codes[idx++]	='GU';
state_names[idx]	='Hawaii';
state_codes[idx++]	='HI';
state_names[idx]	='Idaho';
state_codes[idx++]	='ID';
state_names[idx]	='Illinois';
state_codes[idx++]	='IL';
state_names[idx]	='Indiana';
state_codes[idx++]	='IN';
state_names[idx]	='Iowa';
state_codes[idx++]	='IA';
state_names[idx]	='Kansas';
state_codes[idx++]	='KS';
state_names[idx]	='Kentucky';
state_codes[idx++]	='KY';
state_names[idx]	='Louisiana';
state_codes[idx++]	='LA';
state_names[idx]	='Maine';
state_codes[idx++]	='ME';
state_names[idx]	='Marshall Islands';
state_codes[idx++]	='MH';
state_names[idx]	='Maryland';
state_codes[idx++]	='MD';
state_names[idx]	='Massachusetts';
state_codes[idx++]	='MA';
state_names[idx]	='Michigan';
state_codes[idx++]	='MI';
state_names[idx]	='Minnesota';
state_codes[idx++]	='MN';
state_names[idx]	='Mississippi';
state_codes[idx++]	='MS';
state_names[idx]	='Missouri';
state_codes[idx++]	='MO';
state_names[idx]	='Montana';
state_codes[idx++]	='MT';
state_names[idx]	='Nebraska';
state_codes[idx++]	='NE';
state_names[idx]	='Nevada';
state_codes[idx++]	='NV';
state_names[idx]	='New Hampshire';
state_codes[idx++]	='NH';
state_names[idx]	='New Jersey';
state_codes[idx++]	='NJ';
state_names[idx]	='New Mexico';
state_codes[idx++]	='NM';
state_names[idx]	='New York';
state_codes[idx++]	='NY';
state_names[idx]	='North Carolina';
state_codes[idx++]	='NC';
state_names[idx]	='North Dakota';
state_codes[idx++]	='ND';
state_names[idx]	='Northern Mariana Islands';
state_codes[idx++]	='MP';
state_names[idx]	='Ohio';
state_codes[idx++]	='OH';
state_names[idx]	='Oklahoma';
state_codes[idx++]	='OK';
state_names[idx]	='Oregon';
state_codes[idx++]	='OR';
state_names[idx]	='Palau';
state_codes[idx++]	='PW';
state_names[idx]	='Pennsylvania';
state_codes[idx++]	='PA';
state_names[idx]	='Puerto Rico';
state_codes[idx++]	='PR';
state_names[idx]	='Rhode Island';
state_codes[idx++]	='RI';
state_names[idx]	='South Carolina';
state_codes[idx++]	='SC';
state_names[idx]	='South Dakota';
state_codes[idx++]	='SD';
state_names[idx]	='Tennessee';
state_codes[idx++]	='TN';
state_names[idx]	='Texas';
state_codes[idx++]	='TX';
state_names[idx]	='Utah';
state_codes[idx++]	='UT';
state_names[idx]	='Vermont';
state_codes[idx++]	='VT';
state_names[idx]	='Virgin Islands';
state_codes[idx++]	='VI';
state_names[idx]	='Virginia';
state_codes[idx++]	='VA';
state_names[idx]	='Washington';
state_codes[idx++]	='WA';
state_names[idx]	='West Virginia';
state_codes[idx++]	='WV';
state_names[idx]	='Wisconsin';
state_codes[idx++]	='WI';
state_names[idx]	='Wyoming';
state_codes[idx++]	='WY';

idx = 0;
var province_names = []; var province_codes = [];
province_names[idx]	='Alberta';
province_codes[idx++]	='AB';
province_names[idx]	='British Columbia';
province_codes[idx++]	='BC';
province_names[idx]	='Manitoba';
province_codes[idx++]	='MB';
province_names[idx]	='New Brunswick';
province_codes[idx++]	='NB';
province_names[idx]	='Newfoundland and Labrador';
province_codes[idx++]	='NL';
province_names[idx]	='Northwest Territories';
province_codes[idx++]	='NT';
province_names[idx]	='Nova Scotia';
province_codes[idx++]	='NS';
province_names[idx]	='Nunavut';
province_codes[idx++]	='NU';
province_names[idx]	='Ontario';
province_codes[idx++]	='ON';
province_names[idx]	='Prince Edward Island';
province_codes[idx++]	='PE';
province_names[idx]	='Quebec';
province_codes[idx++]	='QC';
province_names[idx]	='Saskatchewan';
province_codes[idx++]	='SK';
province_names[idx]	='Yukon';
province_codes[idx++]	='YT';

function flipStates(id, country_code) {
	var e = $(id);
	
	// Clear existing options.
	for (i = e.length; i >= 0; --i) {
		e[i] = null;
	}
	
	// States?
	if (country_code == 'US') {
		for (i = 0; i < state_names.length; ++i) {
			e[e.length] = new Option(state_names[i], state_codes[i]);
		}
	}
	
	// Provinces?
	if (country_code == 'CA') {
		for (i = 0; i < province_names.length; ++i) {
			e[e.length] = new Option(province_names[i], province_codes[i]);
		}
	}
}

function copyBilling() {
	var copy = $('copy-billing').checked;

	// If true, copy billing information to shipping and disable the
	// fields.
	if (copy) {
		// Remember the previous shipping zip so we only signal change
		// if it has actually changed.
		var old_shipping_zip = $('shipping-zip').value;
	
		$('shipping-first').value = $('billing-first').value;
		$('shipping-last').value = $('billing-last').value;
		$('shipping-addr1').value = $('billing-addr1').value;
		$('shipping-addr2').value = $('billing-addr2').value;
		$('shipping-city').value = $('billing-city').value;
		$('shipping-country').value = $('billing-country').value;
		flipStates('shipping-state', $('shipping-country').value);
		$('shipping-state').value = $('billing-state').value;
		$('shipping-zip').value = $('billing-zip').value;
		$('shipping-phone').value = $('billing-phone').value;
		
		// If the shipping zip has changed, signal.
		if ($('shipping-zip').value != old_shipping_zip &&
			$('shipping-zip').onchange)
		{
			$('shipping-zip').onchange();
		}
		
		$('shipping-first').disable();
		$('shipping-last').disable();
		$('shipping-addr1').disable();
		$('shipping-addr2').disable();
		$('shipping-city').disable();
		$('shipping-country').disable();
		$('shipping-state').disable();
		$('shipping-zip').disable();
		$('shipping-phone').disable();
	}
	
	// Otherwise enable the fields.
	else {
		$('shipping-first').enable();
		$('shipping-last').enable();
		$('shipping-addr1').enable();
		$('shipping-addr2').enable();
		$('shipping-city').enable();
		$('shipping-country').enable();
		$('shipping-state').enable();
		$('shipping-zip').enable();
		$('shipping-phone').enable();
	}
}

function validateCheckout() {
	var elements = $('checkout-form').getElements();
	
	// All elements must be filled in, except for *-addr2 and
	// copy-billing.
	var complete = true;
	for (var i = 0; i < elements.length; ++i) {
		var id = elements[i].identify();
		if (id.endsWith('-addr2') || id == 'copy-billing') {
			continue;
		}
		if (!elements[i].present()) {
			complete = false;
			break;
		}
	}
	
	// The credit card number must be 15-16 digits and the card security
	// code 3-4 digits.
	if ($('card-number').value.length < 15 ||
		$('card-number').value.length > 16 ||
		$('card-csc').value.length < 3 ||
		$('card-csc').value.length > 4)
	{
		complete = false;
	}
	
	// If incomplete, display the fill form text. Otherwise hide.
	$('fill-form').style.visibility = (complete) ? 'hidden' : 'visible';
	return complete;
}
