function addFabricToOrder(key, category, fabric, color){

	var params = new Array();
	params['func'] = 'fabric';
	params['orderAmount'] = document.getElementById('fabricOrderAmount' + key).value;
	params['orderUnit'] = document.getElementById('fabricUnitSelect' + key).value;
	params['category'] = category;
	params['fabric'] = fabric;
	params['color'] = color;

	if(checkFields(params) == true){
		AJAXcall(params);

		if(params['orderUnit'] == 'yard'){
			document.getElementById('fabricUnitSelect' + key).value = 'meter';
			changeUnit(document.getElementById('fabricUnitSelect' + key), key, 'fabric');
		}
		document.getElementById('fabricUnitSelect' + key).selectedIndex = 0;
		document.getElementById('fabricOrderAmount' + key).value = '';
	}

}


function addYardToOrder(type, name, key){

	var params = new Array();
	params['func'] = 'yard';
	params['type'] = type;
	params['orderAmount'] = document.getElementById(type + 'OrderAmount' + key).value;
	params['orderUnit'] = document.getElementById(type + 'UnitSelect' + key).value;
	params['name'] = name;

	if(checkFields(params) == true){
		AJAXcall(params);

//empty fields in form
		document.getElementById(type + 'OrderAmount' + key).value = '';
		if(params['orderUnit'] == 'yard'){
			document.getElementById(type + 'UnitSelect' + key).value = 'meter';
			changeUnit(document.getElementById(type + 'UnitSelect' + key), key, 'fabric');
		}
		document.getElementById(type + 'UnitSelect' + key).selectedIndex = 0;

	}

}


function addCountToOrder(type, name, key){

	var params = new Array();
	params['func'] = 'count';
	params['type'] = type;
	params['orderAmount'] = document.getElementById(type + 'OrderAmount' + key).value;
	params['name'] = name;

	if(checkFields(params) == true){
		AJAXcall(params);

		document.getElementById(type + 'OrderAmount' + key).value = '';
	}

}


function addYardOptionToOrder(type, name, key){

	var params = new Array();
	params['func'] = 'yardOption';
	params['type'] = type;
	params['orderAmount'] = document.getElementById(type + 'OrderAmount' + key).value;
	params['orderUnit'] = document.getElementById(type + 'UnitSelect' + key).value;
	params['option'] = document.getElementById(type + 'Option' + key).value;
	params['name'] = name;


	if(checkFields(params) == true){
		AJAXcall(params);


//empty fields in form
		document.getElementById(type + 'OrderAmount' + key).value = '';
		if(params['orderUnit'] == 'yard'){
			document.getElementById(type + 'UnitSelect' + key).value = 'meter';
			changeUnit(document.getElementById(type + 'UnitSelect' + key), key, 'lining');
		}
		document.getElementById(type + 'Option' + key).selectedIndex = 0;
		document.getElementById(type + 'UnitSelect' + key).selectedIndex = 0;

	}

}


function addCountOptionToOrder(type, name, key){
	var params = new Array();
	params['func'] = 'countOption';
	params['type'] = type;
	params['orderAmount'] = document.getElementById(type + 'OrderAmount' + key).value;
	params['option'] = document.getElementById(type + 'Option' + key).value;
	params['name'] = name;


//empty fields in form
	if(checkFields(params) == true){
		AJAXcall(params);

		document.getElementById(type + 'Option' + key).selectedIndex = 0;
		document.getElementById(type + 'OrderAmount' + key).value = '';
	}

}


function addZipperToOrder(name, key){

	var params = new Array();
	params['func'] = 'zipper';
	params['zipperColor'] = document.getElementById('zipperColor' + key).value;
	params['zipperLength'] = document.getElementById('zipperLength' + key).value;
	params['name'] = name;


	if(checkFields(params, 'zippers') == true){
		AJAXcall(params);

		document.getElementById('zipperLength' + key).value = '';
		document.getElementById('zipperColor' + key).value = '';
	}

}


function removeFromOrder(el, key, category){
	Dom.remove(el.parentNode.parentNode);

	var xmlhttp;
	if (window.XMLHttpRequest){
		var xmlhttp=new XMLHttpRequest();
		}
	else{
		alert('Your browser does not support javascript AJAX implementation. You can not order online - please use our printable order form.');
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.responseText == 'true'){
				window.location.reload()
			}
		}
		else{
			return;
		}
	}

	var url = 'technical/scripts/orderFabric.php?func=remove&entry=' + key + '&category=' + category;
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);

}


function changeUnit(quantityUnitSelect, key, type){

	while(quantityUnitSelect.nextSibling){
		Dom.remove(quantityUnitSelect.nextSibling);
	}


	if(quantityUnitSelect.value == 'meter'){
		var elSub = document.createElement('span');
		elSub.setAttribute('class', 'quantityAmount');
		elSub.setAttribute('className', 'quantityAmount'); //added for ie
		elSub.innerHTML = '&nbsp<input class="metersAmount" type="text" name="totalAmount" id="' + type + 'OrderAmount' + key +'" />';
		Dom.add(elSub, quantityUnitSelect.parentNode);


		Event.add(quantityUnitSelect.nextSibling, 'change', function(){
			if(isNaN(quantityUnitSelect.nextSibling.lastChild.value)){
				alert('Fabric amount in meters must be specified as a number.');
				quantityUnitSelect.nextSibling.lastChild.value = '';
			}
		});
	}
	else if(quantityUnitSelect.value == 'yard'){
		var elSub = document.createElement('span');
		elSub.setAttribute('class', 'quantityAmount');
		elSub.setAttribute('className', 'quantityAmount'); //added for ie
		elSub.innerHTML = '&nbsp<input class="yardsWhole" type="text" id="yardsWhole" /> <select id="yardsFraction"><option value="0">0/0</option><option value="0.125">1/8</option><option value="0.250">1/4</option><option value="0.375">3/8</option><option value="0.5">1/2</option><option value="0.625">5/8</option><option value="0.750">3/4</option><option value="0.825">7/8</option></select>';
		Dom.add(elSub, quantityUnitSelect.parentNode);


		var elSub = document.createElement('input');
		elSub.setAttribute('type', 'text');
		elSub.setAttribute('name', 'totalAmount');
		elSub.setAttribute('id', type + 'OrderAmount' + key);
		elSub.setAttribute('class', 'hideMe');
		elSub.setAttribute('className', 'hideMe'); //added for ie
		Dom.add(elSub, quantityUnitSelect.nextSibling);


		Event.add(quantityUnitSelect.nextSibling.firstChild.nextSibling, 'change', function(){
			if(isNaN(quantityUnitSelect.nextSibling.firstChild.nextSibling.value)){
				alert('Fabric amount in yards must be specified as an integer and a fraction.');
				quantityUnitSelect.nextSibling.firstChild.nextSibling.value = '';
			}
			else{
				var wholeYards = quantityUnitSelect.nextSibling.firstChild.nextSibling.value * 1;
				var fractionYards = quantityUnitSelect.nextSibling.lastChild.previousSibling.value * 1;
				quantityUnitSelect.nextSibling.lastChild.value = wholeYards + fractionYards;
			}

		});
		Event.add(quantityUnitSelect.nextSibling.lastChild.previousSibling, 'change', function(){
			var wholeYards = quantityUnitSelect.nextSibling.firstChild.nextSibling.value * 1;
			var fractionYards = quantityUnitSelect.nextSibling.lastChild.previousSibling.value * 1;
			quantityUnitSelect.nextSibling.lastChild.value = wholeYards + fractionYards;
		});

	}
}



var Dom = {
	get: function(el){
		if (typeof el === 'string') {
			return document.getElementById(el);
		}
		else {
			return el;
		}
	},
	add: function(el, dest){
		var el = this.get(el);
		var dest = this.get(dest);
		dest.appendChild(el);
	},
	remove: function(el){
		var el = this.get(el);
		el.parentNode.removeChild(el);
	}
};

var Event = {
	add: function() {
		if (window.addEventListener) {
			return function(el, type, fn) {
				Dom.get(el).addEventListener(type, fn, false);
			};
		}
		else if (window.attachEvent) {
			return function(el, type, fn) {
				var f = function() {
					fn.call(Dom.get(el), window.event);
				};
				Dom.get(el).attachEvent('on' + type, f);
			};
		}
	}()
 };

function AJAXcall(params){
	var xmlhttp;
	if (window.XMLHttpRequest){
		var xmlhttp=new XMLHttpRequest();
		}
	else{
		alert('Your browser does not support javascript AJAX implementation. You can not order online - please use our printable order form.');
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.responseText == 'true'){
				alert('The specified products were added ot your order. To view your order click "My order" link in top right corner of the page.');
			}
			else{
				alert(xmlhttp.responseText);
			}
		}
		else{
			return;
		}
	}

	var url = 'technical/scripts/orderFabric.php?';
	for(var x in params){
		var url = url + x + '=' + params[x] + '&';
	}

	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
}

function returnYards(orderAmount){

	var fraction = orderAmount * 1000;
	fraction = fraction % 1000;
	fraction = fraction / 1000;
	var whole = orderAmount - fraction;

	if(fraction == 0){
		fraction = '';
	}
	else if(fraction == 0.125){
		fraction = '1/8';
	}
	else if(fraction == 0.250){
		fraction = '1/4';
	}
	else if(fraction == 0.375){
		fraction = '3/8';
	}
	else if(fraction == 0.5){
		fraction = '1/2';
	}
	else if(fraction == 0.625){
		fraction = '5/8';
	}
	else if(fraction == 0.750){
		fraction = '3/4';
	}
	else if(fraction == 0.875){
		fraction = '7/8';
	}
	var yardDisplayValues = new Array();
	yardDisplayValues['whole'] = whole;
	yardDisplayValues['fraction'] = fraction;
	return (yardDisplayValues);

}

function checkFields(params, category){
	if(category == 'zippers'){
		if(params['zipperLength'] == null){
			alert('You must specify length of the zipper.')
		}
		if(params['zipperColor'] == null){
			alert('You must specify colour of the zipper.')
		}
		else{
			return(true);
		}
	}
	else{
		if (params['orderUnit'] == ''){
			alert('You must select a unit in which to measure the amount of fabric you wish to purchase');
		}
		else if(params['orderAmount'] == '' || params['orderAmount'] == 0){
			alert('You must enter the quantity of product you wish to purchase.');
		}
		else{
			return(true);
		}
	}
}
