var typeNames = new Array('Apex Studded', 'Apex Smooth', 'Apex Square');
var stockLevels = new Array('2', '2', '2', '2', '3',
				'3', '3', '3', '2', '1',
				'2', '3', 'Phone for availability', 'Phone for availability', '5', 
				'2', '1', '2', '3', '2', 
			'Phone for availability', '1', 'Phone for availability', 'Phone for availability', 'Phone for availability',
				'Phone for availability', 'Phone for availability', 'Phone for availability', '1', 'Phone for availability',
				'Phone for availability', '2', 'Phone for availability', 'Phone for availability', '1', 
				'Phone for availability', 'Phone for availability', 'Phone for availability', 'Phone for availability', '3',
			'1', 'Phone for availability', '1', 'Phone for availability', 'Phone for availability',
				'2', 'Phone for availability', 'Phone for availability', 'Phone for availability', 'Phone for availability',
				'Phone for availability', '2', 'Phone for availability', 'Phone for availability', '2', 
				'Phone for availability', 'Phone for availability', 'Phone for availability', 'Phone for availability', 'Phone for availability');	
var stockOrderLevels = new Array('12', '30', '10', '15', '20',
				'10', '19', '12', '13', '10',
				'10', '18', '10', '17', '32', 
				'13', '15', '14', '10', '14', 
			'Phone for availability', '10', '10', '10', 'Phone for availability',
				'Phone for availability', 'Phone for availability', 'Phone for availability', '10', 'Phone for availability',
				'Phone for availability', '10', 'Phone for availability', 'Phone for availability', '10', 
				'10', 'Phone for availability', 'Phone for availability', '2', '3', 
			'17', '10', '3', 'Phone for availability', 'Phone for availability',
				'3', 'Phone for availability', 'Phone for availability', 'Phone for availability', 'Phone for availability',
				'Phone for availability', '10', 'Phone for availability', 'Phone for availability', '7', 
				'Phone for availability', 'Phone for availability', '7', 'Phone for availability', 'Phone for availability');

var colourNames = new Array('aqua', 'black', 'charcoal', 'chocolate', 'coffee',
				'dark grey', 'jade', 'khaki', 'light grey', 'mid blue',
				'mid grey', 'mushroom', 'navy', 'ochre', 'off white', 
				'pillar box', 'racing green', 'rope', 'sage', 'teal');

var prices = new Array('160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '180.00', '180.00', 
				'180.00', '160.00', '160.00', '160.00', '160.00', 
			'160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '180.00', '180.00', 
				'180.00', '160.00', '160.00', '160.00', '160.00', 
			'160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '160.00', '160.00',
				'160.00', '160.00', '160.00', '180.00', '180.00', 
				'180.00', '160.00', '160.00', '160.00', '160.00');

var prices2 = new Array('13.00', '13.00', '13.00', '13.00', '13.00',
				'13.00', '13.00', '13.00', '13.00', '13.00',
				'13.00', '13.00', '13.00', '15.00', '15.00', 
				'15.00', '13.00', '13.00', '13.00', '13.00', 
			'13.00', '13.00', '13.00', '13.00', '13.00',
			'13.00', '13.00', '13.00', '13.00', '13.00',
			'13.00', '13.00', '13.00', '15.00', '15.00', 
			'15.00', '13.00', '13.00', '13.00', '13.00', 
				'13.00', '13.00', '13.00', '13.00', '13.00',
			'13.00', '13.00', '13.00', '13.00', '13.00',
			'13.00', '13.00', '13.00', '15.00', '15.00', 
			'15.00', '13.00', '13.00', '13.00', '13.00');
				
var adhesivePrices = new Array('25.00', '45.00', '75.00', '8.00', '25.00', '48.00', '12.00');

var colors = new Array('#3E848E', '#000000', '#0F3A43', '#364240', '#7F7866',
			'#274C55', '#07675C', '#A5A17E', '#8BA3A7', '#0C5D7B',
			'#678792', '#9CA298', '#03232E', '#D7A256', '#E3DFD4',
			'#B32E31', '#063C2F', '#BBB595', '#98B2A9', '#84ACAE');
			
var profiles = new Array('Studded', 'Smooth', 'Square studded');

function getColor(index)
{
	if (index > 60)
		return '#ffffff';
	return colors[index%20];
}

function getPrice(type, colour)
{
	var index = (type-1)*20 + colour - 1;
	return prices[index];
}

function getExVAT(price)
{
	var priceNum = parseFloat(price);
	var exVAT = ((price*100)/117.5).toFixed(2);
	return exVAT;
}

function writeBasketText()
{
	var subtotal = getBasketSubtotal();
	text = '<a href="http://www.rubberflooringonline.co.uk/basket.htm" onmouseover="MM_swapImage(\'Image2\',\'\',\'images/shopping_basket2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += '£' + getBasketTotal(subtotal, getPostage(subtotal)).toFixed(2);
	text += '</a>';
	document.write(text);
}

function writeBasketImage()
{
	text = '<a href="http://www.rubberflooringonline.co.uk/basket.htm" onmouseover="MM_swapImage(\'Image2\',\'\',\'images/shopping_basket2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += '<img src="images/shopping_basket.jpg" alt="Shopping Basket" name="Image2" width="107" height="46" border="0" id="Image2" /></a>';
	document.write(text);
}

function writeBasketImageWithPrefix(prefix)
{
	text = '<a href="http://www.rubberflooringonline.co.uk/basket.htm" onmouseover="MM_swapImage(\'Image2\',\'\',\''+prefix+'images/shopping_basket2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += '<img src="'+prefix+'images/shopping_basket.jpg" alt="Shopping Basket" name="Image2" width="107" height="46" border="0" id="Image2" /></a>';
	document.write(text);
}

function writeSampleRequestWithPrefix(prefix)
{
	text = '<a href="http://www.rubberflooringonline.co.uk/request2.htm" onmouseover="MM_swapImage(\'Image1\',\'\',\''+prefix+'images/sample_requests2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += '<img src="'+prefix+'images/sample_requests.jpg" alt="Sample Requests" name="Image1" width="107" height="46" border="0" id="Image1" /></a>';
	document.write(text);
}

function writeSampleRequest()
{
	text = '<a href="http://www.rubberflooringonline.co.uk/request2.htm" onmouseover="MM_swapImage(\'Image1\',\'\',\'images/sample_requests2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += '<img src="images/sample_requests.jpg" alt="Sample Requests" name="Image1" width="107" height="46" border="0" id="Image1" /></a>';
	document.write(text);
}

function writeRequestText()
{
	var samples = checkSamples();
	text = '<a href="http://www.rubberflooringonline.co.uk/request2.htm" onmouseover="MM_swapImage(\'Image1\',\'\',\'images/sample_requests2.jpg\',1)" onmouseout="MM_swapImgRestore()">';
	text += 'Samples: ' + samples;
	text += '</a>';
	document.write(text);
}

function writeEmail()
{
	var text = '<a href="mailto:administrator@rubberflooringonline.co.uk">administrator@rubberflooringonline.co.uk</a>';
	document.write(text);
}

function getBasketString(type, colour, price, quantity)
{
	var basketstring = '##'+type+'#'+colour+'#'+price+'#'+quantity+'#';
	return basketstring;
}

function addToRequest(type, colour)
{
	var items = checkSamples();
	if (items > 2)
	{
		alert('You have already chosen 3 samples');
		return;
	}
	var cookiestring = getCookie("samples");
	var basketstring = '##'+type+'#'+colour+'#';
	var searchstring = type+'#'+colour;
	if (cookiestring == null || cookiestring == '' || cookiestring.indexOf(searchstring) < 0)
	{
		cookiestring += basketstring;
		setCookie("samples", cookiestring);
		writeRequest();
	}
	else
	{
		alert('This sample is already in your basket');
	}	
}

function checkSamples()
{
	var cookiestring = getCookie("samples");
	var numitems = 0;
	if (cookiestring != null)
	{
		while (cookiestring.indexOf('##') != -1)
		{
			cookiestring = cookiestring.substring(cookiestring.indexOf('##')+2);
			numitems += 1;
		}
	}
	return numitems;
}

function checkStock(type, colour, quantity)
{
	var typeNum = getTypeNum(type);
	var colourNum = getColourNum(colour);
	var index3 = 0;
	if (typeNum != -1 && colourNum != -1)
	{
		index3 = typeNum*20 + colourNum;
		var stock = parseInt(stockLevels[index3]);
		if ( isNaN(stock) )
			stock = 0;
		var stock2 = parseInt(stockOrderLevels[index3]);
		if ( isNaN(stock2) )
			stock2 = 0;
		var qty = parseInt(quantity);
		
		if (qty > stock && qty <= stock2)
			return !confirm('The item you have selected is only available on 7 working day delivery. If you wish to continue, please click OK. If not, please click Cancel to go back and amend your order. Thank you!');
		else if (qty > stock2)
			return !confirm('The item you have selected is out of stock with an 6-8 week lead time. If you wish to continue, please click OK. If not, please click Cancel to go back and amend your order. Thank you!');
		return false;
	}
	return false;
}

function deleteFromRequest(type, colour)
{
	var cookiestring = getCookie("samples");
	cookiestring = cookiestring.replace('##'+type+'#'+colour+'#', '');
	setCookie("samples", cookiestring);
	writeRequest();
	return false;
}

function getRequestText(isbasket)
{
	var basketstring = '';
	var cookiestring = getCookie("samples");
	if (cookiestring == null)
		return '<tr><td colspan=3>No samples selected</td></tr>';
	var type = '';
	var colour = '';
	var numitems = 1;
	while (cookiestring.indexOf('##') != -1)
	{
		cookiestring = cookiestring.substring(cookiestring.indexOf('##')+2);
		var index = cookiestring.indexOf('#');
		type = cookiestring.substring(0, index);
		cookiestring = cookiestring.substring(index+1);
		index = cookiestring.indexOf('#');
		colour = cookiestring.substring(0, index);
		cookiestring = cookiestring.substring(index+1);
		basketstring += '<tr><td>'+type+'</td><td>'+colour+'</td>';
		if (isbasket.indexOf("true") > -1)
			basketstring += '<td><button onclick="return deleteFromRequest(\''+type+'\', \''+colour+'\');">Delete</button></td>';
		else
			basketstring += '<td></td>';
		basketstring += '<input type="hidden" name="item_name_'+numitems+'" value="'+type+' - '+colour+'">';
		basketstring += '</tr>';
		numitems += 1;
	}
	if (numitems == 1)
		basketstring += '<tr><td colspan=3>No samples selected</td></tr>';
	return basketstring;
}

function getBasketSubtotal()
{
	var cookiestring = getCookie("basket");
	if (cookiestring == null)
		return 0;
	var price = 0.00;
	var itemprice = 0.00;
	while (cookiestring.indexOf('##') != -1)
	{
		cookiestring = cookiestring.substring(cookiestring.indexOf('##')+2);
		var index = cookiestring.indexOf('#');
		index = cookiestring.indexOf('#', index+1);
		cookiestring = cookiestring.substring(index+1);
		index = cookiestring.indexOf('#');
		itemprice = parseFloat(cookiestring.substring(0, index));
		cookiestring = cookiestring.substring(index+1);
		index = cookiestring.indexOf('#');
		quantity = parseInt(cookiestring.substring(0, index));
		cookiestring = cookiestring.substring(index+1);
		price += (itemprice * quantity);
	}
	return price;
}

function getPostage(subtotal)
{
	var postCode = "";
	if (document.getElementById('Postcode') != null)
		postCode = removeSpaces(document.getElementById('Postcode').value);		
	var postage = 0;
	if (subtotal > 0 && subtotal < 100)
		postage = 10;
	if (postCode != null && postCode != '')
	{
		if (postCode.indexOf('IV') == 0 || postCode.indexOf('HS') == 0 || postCode.indexOf('ZE') == 0)
			postage += getSurcharge();
		else if (postCode.indexOf('KW') == 0 || postCode.indexOf('BT') == 0 || postCode.indexOf('IM') == 0)
			postage += getSurcharge();
		else if (postCode.length == 7 && postCode.indexOf('PH') == 0)
		{
			var num = parseInt(postCode.substring(2, 4));
			if ((num > 29 && num < 45) || (num > 16 && num < 27) || (num > 48 && num < 51))
				postage += getSurcharge();
		}
		else if (postCode.length == 7 && postCode.indexOf('PA') == 0)
		{
			var num = parseInt(postCode.substring(2, 4));
			if ((num > 19 && num < 50) || (num > 59 && num < 79))
				postage += getSurcharge();
		}
	}
	return postage;
}

function getSurcharge()
{
	var surcharge = 2 * getQuantity();
	return surcharge;
}

function getQuantity()
{
	var cookiestring = getCookie("basket");
	if (cookiestring == null)
		return 0;
	var quantity = 0;
	var itemquantity = 0;
	while (cookiestring.indexOf('##') != -1)
	{
		cookiestring = cookiestring.substring(cookiestring.indexOf('##')+2);
		var index = cookiestring.indexOf('#');
		index = cookiestring.indexOf('#', index+1);
		cookiestring = cookiestring.substring(index+1);
		index = cookiestring.indexOf('#');
		cookiestring = cookiestring.substring(index+1);
		index = cookiestring.indexOf('#');
		itemquantity = parseInt(cookiestring.substring(0, index));
		cookiestring = cookiestring.substring(index+1);
		quantity += itemquantity;
	}
	return quantity;
}

function removeSpaces(string) 
{
	var tstring = "";
	if (string == null)
		return tstring;
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function getBasketTotal(subtotal, postage)
{
	return (subtotal + postage);
}

function writeBasket()
{
	window.location.href= 'http://www.rubberflooringonline.co.uk/basket.htm';
}

function writeRequest()
{
	window.location.href= 'http://www.rubberflooringonline.co.uk/request2.htm';
}

function setCookie(name, value) 
{
  	var curCookie = name + "=" + escape(value);
  	var date = new Date();
	date.setTime(date.getTime()+(7*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	var curCookie = name+"="+escape(value)+expires+"; path=/";
  	document.cookie = curCookie;
}

function addtocookie(name, value)
{
	var curCookie = getCookie(name) + escape(value);
	setCookie(name, curCookie);
}

function getCookie(name) 
{
  	var dc = document.cookie;
  	var prefix = name + "=";
  	var begin = dc.indexOf("; " + prefix);
  	if (begin == -1) 
	{
   		 begin = dc.indexOf(prefix);
    		if (begin != 0) 
			return null;
  	} 
	else
    		begin += 2;
  	var end = document.cookie.indexOf(";", begin);
  	if (end == -1)
    		end = dc.length;
  	return rehash(unescape(dc.substring(begin + prefix.length, end)));
}

function deleteCookie(name) 
{
   if (getCookie(name)) 
   {
     	document.cookie = name + "=";
   }
}

function rehash(cookiestring)
{
	var rep = /%23/g;
	cookiestring = cookiestring.replace(rep, '#');
	return cookiestring;
}

function getTypeNum(type)
{
	for (var i = 0; i < typeNames.length; i++)
		if (type == typeNames[i])
			return i;
	return -1;
}

function getColourNum(colour)
{
	for (var i = 0; i < colourNames.length; i++)
		if (colour == colourNames[i])
			return i;
	return -1;
}

function sniffBrowsers()
{

}

function doMenus()
{

}

function calculateTiles()
{
	clearTileFields();
	var width_m = document.getElementById('width_m').value;
	var width_ft = document.getElementById('width_ft').value;
	var width_m_float = 0;
	var length_m_float = 0;
	if (width_m.length > 0) {
		width_m_float = parseFloat(width_m);
		if ( isNaN(width_m_float) ) {
			alert('Please enter a decimal in the width field.');
			return;
		}
	}
	else {
		var width_ft_float = parseFloat(width_ft);
		if ( isNaN(width_ft_float) ) {
			alert('Please enter a decimal in the width field.');
			return;
		}
		width_m_float = width_ft_float*0.3048;
	}
	var length_m = document.getElementById('length_m').value;
	var length_ft = document.getElementById('length_ft').value;
	if (length_m.length > 0) {
		length_m_float = parseFloat(length_m);
		if ( isNaN(length_m_float) ) {
			alert('Please enter a decimal in the length field.');
			return;
		}
	}
	else {
		var length_ft_float = parseFloat(length_ft);
		if ( isNaN(length_ft_float) ) {
			alert('Please enter a decimal in the length field.');
			return;
		}
		length_m_float = length_ft_float*0.3048;
	}
	var area = width_m_float * length_m_float;
	document.getElementById('area').value = area.toFixed(2);
	var tiles = parseInt(area/(0.602*0.602)) + 1;
	document.getElementById('tiles').value = tiles;
	var boxes = parseInt(tiles/15);
	document.getElementById('boxes').value = boxes;
	var loose = tiles - (boxes*15);
	document.getElementById('loose_tiles').value = loose;			
}

function clearTileFields()
{
	document.getElementById('area').value = 0;
	document.getElementById('tiles').value = 0;
	document.getElementById('boxes').value = 0;
	document.getElementById('loose_tiles').value = 0;
}
