// BASKET CONTROL FUNCTIONS
// ------------------------
// This scripts the form fields in the gallery pages,
// talks to the internal PHP basket using AJAX,
// and runs the DHTML interactivity
//
// -----------------------------------------------------------------------------

// Define the prices for each size
// Format : prices[] = (size, standard price, card-mounted price, canvas-mounted price)
// -1 indicates unavailable mounting option
var prices = new Array();
prices[prices.length] = [   '6 x 4',  8, -1,  -1];
prices[prices.length] = [   '7 x 5', 10, -1,  -1];
prices[prices.length] = [   '9 x 6', 11, -1,  -1];
prices[prices.length] = [  '10 x 7', 12, 35,  50];
prices[prices.length] = [  '10 x 8', 15, 35,  50];
prices[prices.length] = [  '12 x 8', 15, 40,  60];
prices[prices.length] = [ '12 x 10', 25, 40,  70];
prices[prices.length] = [ '15 x 12', 37, 53,  90];
prices[prices.length] = [ '20 x 16', 48, 70, 110];

// The simpleviewer Flash object
var svObj = navigator.appName.indexOf("Microsoft") != -1 ? sv : document.sv;

// -----------------------------------------------------------------------------

function sv_DoFSCommand(command, pic) {
	// Set the hidden form field that contains the name of the picture that has focus in SimpleViewer
	document.basket_control_form.picture.value = pic;
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub sv_FSCommand(ByVal command, ByVal args)\n');
 	document.write(' call sv_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
} 

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function build_size_menu() {
	// Populate the size drop down from the sizes in the prices array
	var n = prices.length;
	for (i=0; i<n; i++) {
		document.basket_control_form.size[i] = new Option(prices[i][0], prices[i][0]);
	}
	display_options_and_prices(0);
}

// -----------------------------------------------------------------------------

function build_price_table() {
	// Build the price guide
	var tbody = document.getElementById('price_guide_tbody');
	var tr, td;
	var n = prices.length;
	for (i=0; i<n; i++) {
		tr = tbody.insertRow(tbody.rows.length);
		td = tr.insertCell(0);
		td.innerHTML = prices[i][0];
		td = tr.insertCell(1);
		td.setAttribute('align', 'right');
		td.innerHTML = prices[i][1] == -1 ? 'N/A' : '&pound;' + prices[i][1];
		td = tr.insertCell(2);
		td.setAttribute('align', 'right');
		td.innerHTML = prices[i][2] == -1 ? 'N/A' : '&pound;' + prices[i][2];
		td = tr.insertCell(3);
		td.setAttribute('align', 'right');
		td.innerHTML = prices[i][3] == -1 ? 'N/A' : '&pound;' + prices[i][3];
	}
}

// -----------------------------------------------------------------------------

function display_options_and_prices(size_index) {
	// Display the available mounting options and prices for the selected size
	
	// Get the DIVS to manipulate
	if (typeof pr1 == 'undefined') {
		cb1 = document.getElementById('c1'); 
		cb2 = document.getElementById('c2'); 
		cb3 = document.getElementById('c3'); 
		pr1 = document.getElementById('p1'); 
		pr2 = document.getElementById('p2'); 
		pr3 = document.getElementById('p3'); 
	}
	// Which options are available?
	
	var standard = prices[size_index][1] != -1;
	var card     = prices[size_index][2] != -1;
	var canvas   = prices[size_index][3] != -1;
	
	// Disable unavailable mounting options
	document.basket_control_form.mount[0].disabled = !standard;
	document.basket_control_form.mount[1].disabled = !card;
	document.basket_control_form.mount[2].disabled = !canvas;
	
	if (document.basket_control_form.mount[2].checked && !canvas || document.basket_control_form.mount[1].checked && !card) document.basket_control_form.mount[0].checked = 1;
	
	// Display pprices and grey out any unavilable mounting options
	cb1.className = pr1.className = standard ? '' : 'disabled';
	cb2.className = pr2.className = card     ? '' : 'disabled';
	cb3.className = pr3.className = canvas   ? '' : 'disabled';
	pr1.innerHTML = standard ? '&pound;' + prices[size_index][1] : 'Not available';
	pr2.innerHTML = card     ? '&pound;' + prices[size_index][2] : 'Not available';
	pr3.innerHTML = canvas   ? '&pound;' + prices[size_index][3] : 'Not available';
	return;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function basket_add() {
	// Add a picture to the RomanCart basket, using the selected size and mount settings
	var size = document.basket_control_form.size.value;
	var item = document.basket_control_form.picture.value;
	var mount = '';
	var p_index = 1;
	if (document.basket_control_form.mount[1].checked) {
		mount = ' on card';
		p_index = 2;
	} else if (document.basket_control_form.mount[2].checked) {
		mount = ' on canvas';
		p_index = 3;
	}
	var price = prices[document.basket_control_form.size.selectedIndex][p_index];

	ajax = new sack();
	ajax.setVar('e_class','basket');
	ajax.setVar('e_action', 'add');
	ajax.setVar('e_ajax', 1);
	ajax.setVar('path', site_path);
	ajax.setVar('size', size);
	ajax.setVar('item', item);
	ajax.setVar('mount', mount);
	ajax.setVar('price', price);
	ajax.setVar('quantity', 1);
	ajax.requestFile = engine_run_url;
	ajax.method = 'POST';
	ajax.onLoading    = basket_add_when_loading;
	ajax.onCompletion = basket_add_when_completed;
	ajax.runAJAX();
}

// -----------------------------------------------------------------------------

function basket_add_when_loading() {
	// Disable 'add to basket' button
	document.forms.basket_control_form.add.className = 'button_active';
	document.forms.basket_control_form.add.disabled = 1;
	return;
}

// -----------------------------------------------------------------------------

function basket_add_when_completed() {
	ajax.runResponse();
	document.forms.basket_control_form.add.className = 'button';
	document.forms.basket_control_form.add.disabled = 0;
	var i = document.getElementById('basket_items'); 
	var t = document.getElementById('basket_total'); 
	if (ajax.responseStatus) {
		i.innerHTML = num_items;
		t.innerHTML = total;
	} else {
		alert ("NO RESPONSE\n" + ajax.URLString);
	}
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function basket_view() {
	// View the internal shopping basket
	ajax = new sack();
	ajax.setVar('e_class','basket');
	ajax.setVar('e_action', 'view');
	ajax.setVar('e_ajax', 1);
	ajax.setVar('path', site_path);
	ajax.requestFile = engine_run_url;
	ajax.method = 'POST';
	ajax.onLoading    = basket_view_when_loading;
	ajax.onCompletion = basket_view_when_completed;
	ajax.runAJAX();
}

// -----------------------------------------------------------------------------

function basket_view_when_loading() {
	var g = document.getElementById('gallery_content'); 
	var s = document.getElementById('simpleviewer_div'); 
	var b = document.getElementById('basket_view_area'); 
	g.style.visibility= 'hidden';
	s.style.visibility= 'hidden';
	b.style.visibility= 'visible';
}

// -----------------------------------------------------------------------------

function basket_view_when_completed() {
	// The basket is shown after basket_view() or basket_update() complete
	var g = document.getElementById('gallery_content'); // Repetition for Safari - doesn't support ajax.onLoading yet
	var s = document.getElementById('simpleviewer_div'); 
	var b = document.getElementById('basket_view_area'); 
	g.style.visibility= 'hidden';
	s.style.visibility= 'hidden';
	b.style.visibility= 'visible';
	if (ajax.responseStatus) {
		var d = document.getElementById('basket_data_area'); 
		d.innerHTML = ajax.response;
	} else {
		alert ("NO RESPONSE\n" + ajax.URLString);
	}
}

// -----------------------------------------------------------------------------

function basket_update() {
	// Update quantities in the internal basket
	ajax = new sack();
	ajax.setVar('e_class','basket');
	ajax.setVar('e_action', 'update');
	ajax.setVar('e_ajax', 1);
	ajax.setVar('path', site_path);
	var f = document.basket;
	var c = f.counter.value;
	for (var i=0; i<c; i++) {
		if (typeof f['q'+i] != 'undefined') ajax.setVar('q['+i+']', parseInt(f['q'+i].value));
	}
	ajax.requestFile = engine_run_url;
	ajax.method = 'POST';
	ajax.onLoading    = basket_update_when_loading;
	ajax.onCompletion = basket_update_when_completed;
	ajax.runAJAX();
}

// -----------------------------------------------------------------------------

function basket_update_when_loading() {
	// Disable 'update' button
	document.forms.basket.update.className = 'button_active';
	document.forms.basket.update.disabled = 1;
}

// -----------------------------------------------------------------------------

function basket_update_when_completed() {
	basket_view_when_completed();
	document.forms.basket.update.className = 'button';
	document.forms.basket.update.disabled = 0;
	var i = document.getElementById('basket_items'); 
	var t = document.getElementById('basket_total'); 
	i.innerHTML = document.basket.num_items.value;
	t.innerHTML = document.basket.total.value;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function basket_hide() {
	// Hide the onsite basket and redisplay simpleviewer; 
	var b = document.getElementById('basket_view_area'); 
	var g = document.getElementById('gallery_content'); 
	var s = document.getElementById('simpleviewer_div'); 
	b.style.visibility= 'hidden';
	g.style.visibility= 'visible';
	s.style.visibility= 'visible';
}

// -----------------------------------------------------------------------------

function basket_transfer_message() {
	// Show message about transfering to RomanCart
	var m = document.getElementById('transfer_message'); 
	m.style.visibility= 'visible';
	set_opacity('basket_view_area', 40);
}

// -----------------------------------------------------------------------------

function basket_transfer_control(action) {
	// Transfer order to RomanCart
	if (action == 'proceed') {
		document.forms.transfer_control_form.ok.className = 'button_active';
		document.forms.transfer_control_form.ok.disabled = 1;
		document.basket.submit();
	} else {
		var m = document.getElementById('transfer_message'); 
		m.style.visibility= 'hidden';
		set_opacity('basket_view_area',100);
	}
}

// -----------------------------------------------------------------------------

function price_guide_control(action) {
	// Show or hide the size guide and dim the background
	var s = document.getElementById('simpleviewer_div'); 
	var p = document.getElementById('price_guide'); 
	if (action == 'show') {
		s.style.visibility = 'hidden';
		p.style.display = 'block';
		set_opacity('gallery_intro', 40);
		set_opacity('gallery_basket_controls', 40);
	} else {
		p.style.display = 'none';
		s.style.visibility = 'visible';
		set_opacity('gallery_intro', 100);
		set_opacity('gallery_basket_controls', 100);
	}
}

// -----------------------------------------------------------------------------

function set_opacity(id, value) {
	// Set opacity of object, where opacity is 0 - 100
	var obj = document.getElementById(id); 
	obj.style.opacity = value/100;
	obj.style.filter = 'alpha(opacity=' + value*100 + ')';
}
