var preLoad = new Array();
var preLoadName = new Array();
var preLoadIndex = 0;

function $(name)
{
	return document.getElementById(name);
}
function ajaxRequest()
{
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false;
}
function ajaxLoad(uri, accept)
{
	enable(false);
	mygetrequest = ajaxRequest();
	mygetrequest.onreadystatechange=accept;
	mygetrequest.open("GET", uri, true)
	mygetrequest.send(null)
}
function ajaxSubmit(uri, param, accept)
{
	enable(false);
	mygetrequest = ajaxRequest();
	mygetrequest.onreadystatechange=accept;
	mygetrequest.open("post", uri, true);
	mygetrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mygetrequest.setRequestHeader("Content-length", param.length);
	mygetrequest.setRequestHeader("Connection", "close");
	mygetrequest.send(param);
}
	function ajaxStatusCheck(name)
{
	if (mygetrequest.readyState==4)
	{
		if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1)
		  	return 1;
		else{
			alert('An error (status='+mygetrequest.status+': '+mygetrequest.statusText+') has occured making the '+name+' request');
			enable(true);
			return -1;
		}
	}
	return 0;
}
function ajaxDonePost()
{
	var status = ajaxStatusCheck('submit');
	if (status != 1) return null;
	var jsondata;
	try
	{
		jsondata = eval("("+mygetrequest.responseText+")");
	}
	catch (er)
	{
		alert('error parsing '+name+' json response"'
			+mygetrequest.responseText);
		enable(true);
		status = 0;
	}
	if (status != 1) {
		enable(true);
		return null;
	}
	enable(true);
	return jsondata;
}
function enable(yn)
{
	var inputs = document.getElementsByTagName("input");
	var i;
	for (i = 0; i < inputs.length; i++) {
		inputs[i].disabled = !yn;
	}
	inputs = document.getElementsByTagName("button");
	for (i = 0; i < inputs.length; i++) {
		inputs[i].disabled = !yn;
	}
}

function doPreload() {
	for (i = 0; i < preLoadIndex; i++) {
		preLoad[i] = new Image();
		preLoad[i].src = preLoadName[i];
	}
}
function page_turn(old, new1) {
	document.getElementById(old).style.display = 'none';
	document.getElementById(new1).style.display = 'inline';
	window.location.hash = 'Bdy';
	mb_y1k_resize();

}
var shipping_set = 0;
function verify(i) {
	if (things[i].question != '') {
		if (document.getElementById('os0_'+things[i].item_number).value == 'Select a '+things[i].question) {
			document.getElementById('os0_'+things[i].item_number).style.color = 'red';
			document.getElementById('oe0_'+things[i].item_number).style.display = 'block';
			return false;
		}
	}
	if (things[i].question2 != '') {
		if (document.getElementById('os1_'+things[i].item_number).value == 'Select a '+things[i].question2) {
			document.getElementById('os1_'+things[i].item_number).style.color = 'red';
			document.getElementById('oe1_'+things[i].item_number).style.display = 'block';
			return false;
		}
	}
	if (shipping_set) return true;
	document.getElementById('ship_to').style.color = 'red';
	document.getElementById('ship_error').style.display = 'block';
	document.getElementById('canada').focus();
	return false;
}
function do_option_thing(i,j) {
	unerror(i,j);
	display_stock_level(i);
}
function unerror(i, j) {
	document.getElementById('os'+j+'_'+things[i].item_number).style.color = 'black';
	document.getElementById('oe'+j+'_'+things[i].item_number).style.display = 'none';
}
function display_stock_level(i) {
	if (things[i].question == "") {
		display_stock(i, things.stock);
		return;
	}
	if (things[i].question2 == '') {
		for (j = 0; j < things[i].answers.length; j++) {
			if (document.getElementById('os0_'+things[i].item_number).value == things[i].answers[j]) {
				display_stock(i, things[i].stock[j]);
				return;
			}
		}
		document.getElementById('stock_'+things[i].item_number).style.display = 'none';
		return;
	}
	for (j = 0; j < things[i].question.length; j++) {
		if (document.getElementById('os0_'+things[i].item_number).value == things[i].answers[j]) {
			for (k = 0; k < things[i].answers2.length; k++) {
				if (document.getElementById('os1_'+things[i].item_number).value == things[i].answers2[k]) {
					display_stock(i, things[i].stock[j][k]);
					return;
				}
			}
		}
	}
	document.getElementById('stock_'+things[i].item_number).style.display = 'none';
}
function display_stock(i, s)
{
	var stock = "Out of stock";
	if (s < 4) document.getElementById('stock_'+things[i].item_number).style.color = 'red';
	else document.getElementById('stock_'+things[i].item_number).style.color = 'black';
	if (s > 9) stock = 'In stock';
	else if (s > 1) stock = 'There are '+s+' in stock';
	else if (s == 1) stock = 'There is 1 in stock';
	else if (s == -1) stock = 'Coming Soon';
	document.getElementById('stock_'+things[i].item_number).innerHTML = stock;
	document.getElementById('stock_'+things[i].item_number).style.display = 'inline';
}
function shippingf(j) {
	var ships = document.getElementsByName('shipping');
	var noships = document.getElementsByName('no_shipping');
	var i;
	var ns;
	createCookie('shippin_loc', j, 1);
	if (j == 3) ns = 1;
	else ns = 2;
	shipping_set = 1;
	for (i = 0; i < things.length; i++) {
		if (j == 3 || things[i].shipping[j] == 0)
			document.getElementById('item_'+i+'_ship1').innerHTML = '';
		else
			document.getElementById('item_'+i+'_ship1').innerHTML = '&nbsp;plus $'+things[i].shipping[j]+' shipping';
		document.getElementById('item_'+i+'_ship2').setAttribute('value', things[i].shipping[j]);
	}
	for (i = 0; i < noships.length; i++) {
		noships[i].setAttribute('value', ns);
	}
	document.getElementById('ship_to').style.color = 'black';
	document.getElementById('ship_error').style.display = 'none';
}
function Thing(item_name, item_number, amount, shipping, question, answers, question2, answers2, stock, desc, photos, thumbs)
{
	this.item_name = item_name;
	this.item_number = item_number;
	this.amount = amount;
	this.shipping = shipping;
	this.question = question;
	this.answers = answers;
	this.question2 = question2;
	this.answers2 = answers2;
	this.stock = stock;
	this.desc = desc;
	this.photo = photos;
	this.thumb = thumbs;
}
function catalog() {
	var txt;
	var ipp = readCookie('ipp');
	if (ipp != null) items_per_page = parseInt(ipp);

	document.getElementById('catalog').innerHTML = '<p class="m"><b><i>Loading</i></b></p>';
	ajaxLoad('catalog.json', catalog2);
}
function catalog2() {
	var txt;
	var i;
	things = ajaxDonePost();
	
	if (things == null) return;
	catalog3();
}
function catalog3() {
	var shipping = readCookie('shippin_loc');

	var shipsel = ['', '', '', ''];
	if (shipping != null) {
		shipping = parseInt(shipping, 10)
		if (!isNaN(shipping) && shipping >= 0 && shipping <= 3) shipsel[shipping] = ' checked="checked"';
	}
		

	txt = '<h1 align="left"><br>Yukon 1000 Merchandise</h1>'
			+ '	<table><tr id="ship_to">'
			+ '		<td class="m">Ship to: </td>'
			+ '	  <TD class="m">'
			+ '    <INPUT NAME="shipping" id="canada" TYPE="radio" VALUE="canada"'+shipsel[0]+' onclick="shippingf(0)">'
			+ '	<label for="canada">Canada</label></TD>'
			+ '	  <TD class="m">'
			+ '    <INPUT NAME="shipping" id="US" TYPE="radio" VALUE="US"'+shipsel[1]+' onclick="shippingf(1)">'
			+ '	<label for="US">USA</label></TD>'
			+ '	  <TD class="m">'
			+ '    <INPUT NAME="shipping" id="other" TYPE="radio" VALUE="other"'+shipsel[2]+' onclick="shippingf(2)">'
			+ '	<label for="other">Other</label></TD>'
			+ '	  <TD class="m">'
			+ '    <INPUT NAME="shipping" id="whitehorse" TYPE="radio" VALUE="whitehorse"'+shipsel[3]+' onclick="shippingf(3)">'
			+ '	<label for="whitehorse">Pickup in Whitehorse</label></TD>'
			+ '	<td id="ship_error" style="display:none" class="mi">You must specify a shipping region</td>'
			+ '<td class=m><select name="npag" id="npag" style="padding-right:0px" onchange="set_page_length()">';
			for (i = 1; i <= things.length; i++) {
				txt += '<option value="'+i+'"';
				if (i == items_per_page) txt += ' selected="selected"';
				txt += '>'+i+'</option>';
			}
			txt += '</select> items per page</td>'
			+ '</tr></table>';
	var npages = things.length + items_per_page - 1;
	var npages = (npages - (npages % items_per_page)) / items_per_page;
	for (page = 1; page <= npages; page++)
	{
		txt = txt + display_page(page, npages);
	}
	document.getElementById('catalog').innerHTML = txt;
	var str=window.location.href;
	var i = str.lastIndexOf('#');
	if (i >= 0) {
		str = str.substr(i+1);
		i = parseInt(str, 10);
		if (isNaN(i) || i < 0 || i > things.length)
			window.location.hash = "Bdy";
		else {
			page_turn('page'+1, 'page'+i);
		}
	}	
	setTimeout("doPreload()", 5000);
	mb_y1k_resize();

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function set_page_length() {
	items_per_page = parseInt(document.getElementById('npag').value);
	createCookie('ipp', items_per_page, 300);
	catalog3();
}

function display_page(page, npages) {
	var txt;
	var pageturn = display_page_ctrl(page, npages);
	if (page == 1) disp = 'inline';
	else disp = 'none';
	txt = '<div id="page'+page+'" style="position:relative;display:'+disp+'">';
	txt = txt + pageturn;
	fop = (page-1) * items_per_page;
	lop = fop + items_per_page - 1;
	if (lop >= things.length) lop = things.length - 1;
	for (i = fop; i <= lop; i++) txt = txt + display_item(i);
	txt = txt + '<br>'+ pageturn+'</div>';
	return txt;
}
function display_page_ctrl(page, npages) {
	var next = page+1;
	var prev = page-1;
	var view_cart = '<div style="float:right"><form name="_xclick" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" '
				  + ' method="post">'
				  + '<input type="hidden" name="cmd" value="_cart">'
				  + '<input type="hidden" name="business" value="info@yukon1000.com">'
				  + '<input type="hidden" name="cpp_header_image" id="cpp_header_image" '
				  + 'value="http://yukon1000.com/images/Y1K_banner-602x90-text.jpg">'
				  + '<input type="image" style="float:right;position:relative" '
				  + ' src="https://www.paypal.com/en_US/i/btn/view_cart_03.gif" border="0" '
				  + ' name="submit" alt="View Cart"><input type="hidden" name="display" value="1"></form></div>';
	var txt = '<div style="position:relative;float:left;clear:both;width:100%" class="mi">'+view_cart+'Page&nbsp;'+page+'&nbsp;of&nbsp;'+npages+'&nbsp;';
	if (npages == 1) return '';
	if (npages == 2 && page == 1) 
		return txt+'<button onclick="page_turn(\'page1\', \'page2\')">next</button></div>';;
	if (npages == 2 && page == 2) 
		return txt+'<button onclick="page_turn(\'page2\', \'page1\')">prev</button></div>';;
	if (page == 1)
		return txt + '<button disabled="disabled">first</button>'
			+ '<button disabled="disabled">prev</button>'
			+ '<button onclick="page_turn(\'page1\', \'page2\')">next</button>'
			+  '<button onclick="page_turn(\'page1\', \'page'+npages+'\')">last</button></div>';;
	if (page == npages ) {
		return txt+'<button onclick="page_turn(\'page'+page+'\', \'page1\')">first</button>'
			+  '<button onclick="page_turn(\'page'+page+'\', \'page'+prev+'\')">prev</button>'
			+ '<button disabled="disabled">next</button>'
			+ '<button disabled="disabled">last</button></div>';;
		}
	return txt+'<button onclick="page_turn(\'page'+page+'\', \'page1\')">first</button>'
		+  '<button onclick="page_turn(\'page'+page+'\', \'page'+prev+'\')">prev</button>'
		+  '<button onclick="page_turn(\'page'+page+'\', \'page'+next+'\')">next</button>'
		+  '<button onclick="page_turn(\'page'+page+'\', \'page'+npages+'\')">last</button></div>';;
}
function display_item(i) {
	var height = things[i].thumb.length+4;
	height = (height - (height % 5)) / 5;
	height = 411 + 64 * height;
	var txt = '<br clear="all"><div style="float:left;width:100%"><div style="width:395px;float:left;position:relative">'
		+     '<img src="' + things[i].photo[0] + '" style="margin:10px 10px 0px 10px;position:relative" '
		+	  'id="'+things[i].item_number+'" alt="'+things[i].item_name+'">';
	
	if (things[i].photo.length > 1) {
		for (j = 0; j <things[i].photo.length; j++) {
			txt = txt + '<img src="'+things[i].thumb[j]+'" style="margin:2px 0px 10px 10px;position:relative" '
					  + 'alt="'+things[i].item_name+'" '
					  + 'onclick="document.getElementById(\''+things[i].item_number+'\').src=\''+things[i].photo[j]+'\'">'
			preLoadName[preLoadIndex] = things[i].photo[j];
			preLoadIndex++;
			}
		}
	txt = txt + '</div>';
	txt = txt + things[i].desc;
	txt = txt + '<p class=m>$' + things[i].amount + ' CAD each<span id="item_'+i+'_ship1">';
	if (things[i].shipping[2] != 0) txt += ' plus shipping';
	txt += '</span></p>';
	txt = txt + '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" '
			  + 'onsubmit="return verify(\''+ i + '\')">'
			  + '<input type="hidden" name="add" value="1">'
			  + '<input type="hidden" name="cmd" value="_cart">'
			  + '<input type="hidden" name="business" value="info@yukon1000.com">'
			  + '<input type="hidden" name="cpp_header_image" id="cpp_header_image" '
			  + 'value="http://yukon1000.com/images/Y1K_banner-602x90-text.jpg">'
			  + '<input type="hidden" name="item_name" value="'+ things[i].item_name + '">'
			  + '<input type="hidden" name="item_number" value="'+ things[i].item_number + '">'
			  + '<input type="hidden" name="amount" value="'+ things[i].amount + '">'
			  + '<input type="hidden" id="item_'+i+'_ship2" name="shipping" value="2">'
			  + '<input type="hidden" id="no_shipping" name="no_shipping" value="2">'
			  + '<input type="hidden" name="return" value="http://www.yukon1000.com">'
			  + '<input type="hidden" name="currency_code" value="CAD">'
			  + '<input type="hidden" name="cancel_return" value="http://www.yukon1000.com">'
			  + '<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_SM.gif" '
			  + 'align="right" name="submit" alt="Add to Cart" style="position:relative">';
	if (things[i].question != '') {
		txt = txt + '<input type="hidden" name="on0" value="'+things[i].question+'">'
				  + '<select name="os0" id="os0_'+ things[i].item_number + '" onChange="do_option_thing(\''+ i +'\', \'0\')">'
				  + '<option value="Select a '+things[i].question+'">Select a '+things[i].question+'</option>';
		for (j = 0; j < things[i].answers.length; j++) {
			txt = txt + '<option value="'+things[i].answers[j]+'">'+things[i].answers[j]+'</option>';
		}
		txt = txt + '</select>';
		txt = txt + '<p class="mi" id="oe0_'+ things[i].item_number + '" style="color:red;display:none">'
				  + 'You must specify a '+things[i].question+'</p>';
	}
	if (things[i].question2 != '') {
		txt = txt + '<input type="hidden" name="on1" value="'+things[i].question2+'">'
				  + '<select name="os1" id="os1_'+ things[i].item_number + '" onChange="do_option_thing(\''+ i +'\', \'1\')">'
				  + '<option value="Select a '+things[i].question2+'">Select a '+things[i].question2+'</option>';
		for (j = 0; j < things[i].answers2.length; j++) {
			txt = txt + '<option value="'+things[i].answers2[j]+'">'+things[i].answers2[j]+'</option>';
		}
		txt = txt + '</select>'
		txt = txt + '<p class="mi" id="oe1_'+ things[i].item_number + '" style="color:red;display:none">'
				  + 'You must specify a '+things[i].question2+'</p>'

	}
	if (things[i].question == '') {
		txt = txt + '<br><p class="m" id="stock_'+ things[i].item_number + '" style="display:inline';
		if (things[i].stock < 4) txt+=';color:red';
		txt += '">';
		if (things[i].stock == -1) txt += 'Coming Soon</p>';
		else if (things[i].stock < 1) txt += 'Out of stock</p>';
		else if (things[i].stock > 9) txt += 'In stock</p>';
		else {
			txt += 'There ';
			if (things[i].stock ==1) txt += 'is 1 in stock</p>';
			else txt += 'are '+things[i].stock+' in stock</p>';
		}

	} else 	txt = txt + '<br><p class="m" id="stock_'+ things[i].item_number + '" style="display:none">&nbsp;</p>';
	txt = txt + '</form></div>'
	return txt;
}

