var price = 0.00;
var leatherPrice = 0.00;
var liningPrice = 0.00;
var qty = 1;
var colourChosen = false;


function checkProduct() {
	if (leatherType = document.getElementById("leatherType")) {
		leatherType.options[document.getElementById("leatherType").selectedIndex].value;
		
		if( !leatherType ) {
			allGood = false;
			alert( 'Please select the type of leather you require for this glove.' );
			return( false );
		}
	}
	
	if ( document.getElementById("colours") ) {
		if( !colourChosen ) {
			allGood = false;
			alert( 'Please select the colour you require for this glove.' );
			return( false );
		}
	}
	
	var sizeEntered = false;
	var isSizeOption = false;
	
	// Remove default inputs	
	if (sizeInput = document.getElementById("sizeInput")) {
		if (sizeInput.value == "Size" || sizeInput.value == "") {
			 sizeInput.value = "";
		} else {
			sizeEntered = true;
		}
	}
	
	
	if (lengthInput = document.getElementById("lengthInput")) {
		isSizeOption = true;
		if (document.getElementById("lengthInput").value == "Glove length") {
			document.getElementById("lengthInput").value = "";
		}
	}

	
	// check rest of size options
	if (sizeOptions1 = document.getElementById("sizeOptions1")) {
		isSizeOption = true;
		if (document.getElementById("sizeOptions1").checked) sizeEntered = true;
		if (document.getElementById("sizeOptions2").checked) sizeEntered = true;
		if (document.getElementById("sizeOptions3").checked) sizeEntered = true;
		if (document.getElementById("sizeOptions4").checked) sizeEntered = true;
	}

	if (sizeEntered == false && isSizeOption)	 {
		alert( 'Please enter a size option.' ); 
		return( false );
	}

	return( true );
}

function getLiningPrice(e) {
	if( e != '' ) {
		var url = 'ajax.asp';
		var rand = Math.random(9999);
		var pars = 'action=GETLININGPRICE&optionGroup='+e+'&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: refreshLiningPrice} );
	} else {
		liningPrice = 0.00;
		updatePrices();
	}
}
function refreshLiningPrice(originalRequest) {
	liningPrice = originalRequest.responseText;
	updatePrices();
}

function getColours(e,pid) {
	clearColours();
		
	if( e != '' ) {
		var url = 'ajax.asp';
		var rand = Math.random(9999);
		var pars = 'action=LOADCOLOURS&pid='+pid+'&optionGroup='+e+'&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: refreshColours} );
	} else {
		var html = "";
		for (var i=0; i<25; i++) {
			html = html + '<div class="colorSwatch"></div>';
		}
		document.getElementById('colours').innerHTML = html;
		
		price = 0.00;
		leatherPrice = 0.00;
		updatePrices();
	}
}
function refreshColours(originalRequest) {
	var RequestSplit = originalRequest.responseText.split(":");
	var html = "";
	var slots = 25;
	var curColor = "";
	for (var i=0; i<RequestSplit.length-2; i++) {
		curColor = RequestSplit[i].replace(".jpg", "");
		html = html + '<div class="colorSwatch"><img id="swatch'+(i+1)+'" onclick="javascript:changeGloveRef(\''+RequestSplit[i]+'\')" src="../ProdImages/colours/swatches/'+RequestSplit[i]+'" width="45" height="45" border="0" alt="'+curColor+'" title="'+curColor+'" /></div>';
		slots--;
	}
		
	for (var i=0; i<slots; i++) {
		html = html + '<div class="colorSwatch"></div>';
	}
	document.getElementById('colours').innerHTML = html;
	
	for (var i=0; i<RequestSplit.length-2; i++) {
		new Draggable( 'swatch'+(i+1), {revert:true});
	}
	
	price = 0.00;
	
	leatherPrice = RequestSplit[RequestSplit.length-1];
	updatePrices()
}

function updatePrices() {

	var quantity = document.getElementById('qty').value;
	if( !IsNumeric( quantity ) || !quantity ) {
		quantity = 1;
	}
	
	if( quantity > 1000 ) {
		quantity = 1000;
		alert( "Regrettably, you can only order up to 1,000 of this item. If you wish to place a bigger order please contact us." );
		document.getElementById('qty').value = 1000;
	}
		
	price = parseFloat(leatherPrice);
	price = price + parseFloat(liningPrice);
	price = price * parseFloat(quantity);
	
	var newPrice
	newPrice = "&pound;" + price.toFixed(2);
														 														 
	document.getElementById('orderTotal1').innerHTML = newPrice;
	document.getElementById('orderTotal2').innerHTML = newPrice;
	document.getElementById('orderTotal3').innerHTML = newPrice;
	document.getElementById('orderTotal4').innerHTML = newPrice;
	document.getElementById('orderTotal5').innerHTML = newPrice;
	
	document.getElementById('tp').value = price;
	
}

function changeGloveRef(e) {
	document.getElementById('gloveRef').innerHTML = '<img src="../ProdImages/colours/'+e+'" />';
}

function getOptionID( e, c ) {
	var url = 'ajax.asp';
	var rand = Math.random(9999);
	var pars = 'action=GETOPTIONGROUPID&optionGroup='+e+'&color='+c+'&rand=' + rand;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: completeOptionAug} );
}
function completeOptionAug(originalRequest) {
	var RequestSplit = originalRequest.responseText.split(":");
	document.getElementById('OPTidOption'+RequestSplit[0]).value = RequestSplit[1];
	colourChosen = true;
}

function colour1(element, dropon, event) {
	var elementSrc = element.src;
	document.getElementById('colour1').innerHTML = '<img src="'+elementSrc+'" />';
	elementSrc = elementSrc.replace('Buffed-Hogskin','Buffed');
	var srcSplit = elementSrc.split("/");
	var colorSplit = srcSplit[srcSplit.length-1].split("-");
	var color = '';
	for (var i=1; i<colorSplit.length; i++) {
		color = color + colorSplit[i];
		if( i<colorSplit.length ) { color = color + " "; }
	}
	color = color.replace(".jpg", "");
	document.getElementById('label1').innerHTML = 'Glove Colour<br />'+color;

			//alert(document.getElementById('leatherType').value);
  //var leatherType = document.forms[0].leatherType.options[document.forms[0].leatherType.selectedIndex].value;
  var leatherType = document.getElementById('leatherType').value;
getOptionID( leatherType, color );
	//document.getElementById('PalmColour').value = color;
}

function optionalColours(element,dropon,event) {
	var elementSrc = element.src;
	document.getElementById(dropon.id).innerHTML = '<img src="'+elementSrc+'" width="45" height="35" />';
	elementSrc = elementSrc.replace('Buffed-Hogskin','Buffed');
	var srcSplit = elementSrc.split("/");
	var colorSplit = srcSplit[srcSplit.length-1].split("-");
	var color = '';
	for (var i=1; i<colorSplit.length; i++) {
		color = color + colorSplit[i];
		if( i<colorSplit.length ) { color = color + " "; }
	}
	color = color.replace(".jpg", "");
	
	var eSplit = dropon.id.split("optional");
	ci = "fOptional"+eSplit[1];
	document.getElementById(ci).value = color;
}

function colour2(element, dropon, event) {
	var elementSrc = element.src;
	document.getElementById('colour2').innerHTML = '<img src="'+elementSrc+'" />';
	var srcSplit = elementSrc.split("/");
	var colorSplit = srcSplit[srcSplit.length-1].split("-");
	var color = '';
	for (var i=1; i<colorSplit.length; i++) {
		color = color + colorSplit[i];
		if( i<colorSplit.length ) { color = color + " "; }
	}
	color = color.replace(".jpg", "");
	document.getElementById('label2').innerHTML = 'Choice 2<br />'+color;
	document.getElementById('OptionalColour2').value = color;
}
function colour3(element, dropon, event) {
	var elementSrc = element.src;
	document.getElementById('colour3').innerHTML = '<img src="'+elementSrc+'" />';
	var srcSplit = elementSrc.split("/");
	var colorSplit = srcSplit[srcSplit.length-1].split("-");
	var color = '';
	for (var i=1; i<colorSplit.length; i++) {
		color = color + colorSplit[i];
		if( i<colorSplit.length ) { color = color + " "; }
	}
	color = color.replace(".jpg", "");
	document.getElementById('label3').innerHTML = 'Choice 3<br />'+color;
	document.getElementById('OptionalColour3').value = color;
}

function clearColours() {
	document.getElementById('colour1').innerHTML = '';
	//document.getElementById('colour2').innerHTML = '';
	//document.getElementById('colour3').innerHTML = '';
	document.getElementById('gloveRef').innerHTML = '<img src="../media/prod-view/placeholder-glove.jpg" />';
	
	if( document.getElementById('OPTidOption8') != null ) { document.getElementById('OPTidOption8').value = ''; }
	if( document.getElementById('OPTidOption9') != null ) { document.getElementById('OPTidOption9').value = ''; }
	if( document.getElementById('OPTidOption10') != null ) { document.getElementById('OPTidOption10').value = ''; }
	
	colourChosen = false;
}