﻿document.getElementById('add2cart').style.display="";

// I may wany to add some checking in at some point to ensure all the elements I am using below actually exist

// Generate the empty colours drop down box
var coloursdd_from='<select name="colours" id="colours" onchange="setupsizes()"><option value="blank">Select a colour</option></select>';	
document.getElementById('colours').innerHTML = coloursdd_from;		

// Generate the empty sizes drop down box
var sizesdd_from='<select name="sizes" id="sizes" onchange="setupComplexFeature()"><option value="blank"> ............................ </option></select>';	
document.getElementById('sizes').innerHTML = sizesdd_from;	

// this function is used to pick the single option from the options drop down when a style only has one sku prior to popping up the request stock update notification
function picksingleoption(){
	// if there is only one sku in a style
	if(OptionsData.length == 1){
		// pick it, you know you want it
		document.le_form.colours.selectedIndex = 1;
		// set up the sizes for the chosen colour		
		setupsizes();
		// pick it, you know you want it
		document.le_form.sizes.selectedIndex = 1;
	}	
}

// check stock
function stockcheck(){
		// loop round the options data
		for(od=0; od < OptionsData.length; od++) {
			// if any of the options are out of stock =
			if (OptionsData[od].stocklevel == 0){
				// if there is only one option display just single sku out of stock message
				if(OptionsData.length == 1){
				if(document.getElementById("stockalertssinglesku")){			
					document.getElementById("stockalertssinglesku").style.display="";
				}	
			}	
				else {
					// if there are multiple options display just multi sku out of stock message			
				if(document.getElementById("stockalertsmultisku")){	
					document.getElementById("stockalertsmultisku").style.display="";
				}	
			}	
			break;
		}
	}	
}

// Populate the colours drop down box
function setupcolours(){
	stockcheck();
	clearComplexFeature();		
	var thecolours=document.le_form.colours;
	var thesizes=document.le_form.sizes;		
	var thecurrentcolourselection=thecolours[thecolours.selectedIndex].value;
	ClearOptions(thecolours);
	AddToOptionList(thecolours, "blank", "Select a colour");
	if (thesizes[thesizes.selectedIndex].value != "blank"){
		var tmpcolourarray = new Array();
		var tmpcolourarray_arr_pos=0;		
		for(od=0; od < OptionsData.length; od++) {
			if(OptionsData[od].size == thesizes[thesizes.selectedIndex].value){
				tmpcolourarray[tmpcolourarray_arr_pos] = OptionsData[od].colour;						
				tmpcolourarray_arr_pos++;	
			}		
		}
		tmpcolourarray.sort();
		for(i=0; i < tmpcolourarray.length; i++){
			AddToOptionList(thecolours, tmpcolourarray[i], tmpcolourarray[i]);
			if(thecurrentcolourselection == tmpcolourarray[i])
				var tmp_setselected=thecolours.options[thecolours.length-1].selected = true;
		}
		setupComplexFeature();
	} else {
		var tmpcolourarray=new Array();
		for(od=0; od < OptionsData.length; od++) {
			if (od == 0) {
				tmpcolourarray[tmpcolourarray.length]=OptionsData[od].colour;
			} 
			else {
				var addItem = true;
				for(ca=0; ca < tmpcolourarray.length; ca++) {
					if(OptionsData[od].colour == tmpcolourarray[ca])
						addItem = false;
				}
				if (addItem)
					tmpcolourarray[tmpcolourarray.length]=OptionsData[od].colour;	
			}
		}	
		tmpcolourarray.sort();
		for(i=0; i < tmpcolourarray.length; i++){
			AddToOptionList(document.le_form.colours, tmpcolourarray[i], tmpcolourarray[i]);
		}
	}	
}	
// Populate the sizes drop down box
function setupsizes(){
	clearComplexFeature();
	var thecolours=document.le_form.colours;
	var thesizes=document.le_form.sizes;		
	var thecurrentsizeselection=thesizes[thesizes.selectedIndex].value;
	ClearOptions(thesizes);
	if (thecolours[thecolours.selectedIndex].value != "blank"){
		AddToOptionList(thesizes, "blank", "Select a size");
		var tmpsizearray_arr_pos=0;	
		tmpsizearray=new Array();
		for(od=0; od < OptionsData.length; od++) {
			if(OptionsData[od].colour == thecolours[thecolours.selectedIndex].value){
				tmpsizearray[tmpsizearray_arr_pos]=new Object();
				tmpsizearray[tmpsizearray_arr_pos]["size"]=OptionsData[od].size;
				tmpsizearray[tmpsizearray_arr_pos]["cost"]=OptionsData[od].cost;
				tmpsizearray[tmpsizearray_arr_pos]["stocklevel"]=OptionsData[od].stocklevel;					
				tmpsizearray_arr_pos++;	
			}		
		}			
	myBubbleSort(tmpsizearray,tmpsizearray.length);			
		for(i=0; i < tmpsizearray.length; i++){
			var	price_fixed = parseFloat(tmpsizearray[i].cost);
			if(tmpsizearray[i].stocklevel == 0)
				var Opttext = tmpsizearray[i].size + " £" + price_fixed.toFixed(2) + " Out of stock";				
			else
				var Opttext = tmpsizearray[i].size + " £" + price_fixed.toFixed(2);
			AddToOptionList(thesizes, tmpsizearray[i].size, Opttext);
			if(thecurrentsizeselection == tmpsizearray[i].size)
				var tmp_setselected=thesizes.options[thesizes.length-1].selected = true;
		}
		setupComplexFeature();
	} else {
		AddToOptionList(thesizes, "blank", " ............................ ");
		setupcolours();
	}	
}		
function myBubbleSort(arrayName,length) {
    for (var i=0; i<(length-1); i++) {
        for (var j=i+1; j<length; j++) {
			if ((1 - arrayName[j].size) > (1 - arrayName[i].size)) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            }
		}	
	}
}

// Set up complex feature value
function setupComplexFeature() {
	var theform = document.le_form;
	var sel_col = theform.colours.selectedIndex;
	var sel_siz = theform.sizes.selectedIndex;	
	if(theform.sizes[sel_siz].value == "blank"){
		theform.ComplexFeatures.value = "";
	} else {
		var compset=false;
		for(od=0; od < OptionsData.length; od++){
			if(OptionsData[od].size == theform.sizes[sel_siz].value && 	OptionsData[od].colour == theform.colours[sel_col].value && OptionsData[od].stocklevel != 0){
				var selectedOption = OptionsData[od].colour + " |" + OptionsData[od].size + " |" + OptionsData[od].description + " |" + OptionsData[od].cost;
				theform.ComplexFeatures.value = selectedOption;
				var	normalprice_fixed = parseFloat(OptionsData[od].price);
				var	specprice_fixed = parseFloat(OptionsData[od].specialprice);
				if(document.getElementById('normalprice')){
					if (specprice_fixed != 0 && specprice_fixed < normalprice_fixed){
						document.getElementById('normalprice').innerHTML = "WAS £" + normalprice_fixed.toFixed(2);	
						document.getElementById('specialprice').innerHTML = "NOW £" + specprice_fixed.toFixed(2);
					}	
					else {
						document.getElementById('normalprice').innerHTML = "";					
						document.getElementById('specialprice').innerHTML = "Price £" + normalprice_fixed.toFixed(2);					
					}
				}	
				compset=true;					
			}
			if(!compset)
				clearComplexFeature();
		}			
	}	
}
function clearComplexFeature() {
	document.le_form.ComplexFeatures.value = "";
}		
function ClearOptions(OptionList) {
	// Always clear an option list from the last entry to the first
	for (x = OptionList.length; x >= 0; x--) {
		OptionList[x] = null;
	}
}
function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
   var optnew = OptionText.replace("&#189;",String.fromCharCode(189));
   optnew = optnew.replace("&#189;",String.fromCharCode(189));
   optnew = optnew.replace("&#189;",String.fromCharCode(189));   
   OptionList[OptionList.length] = new Option(optnew, OptionValue);
}
setupcolours();

function checkselectedoptions(obj){
	var theform = document.le_form;
	var sel_col = theform.colours.selectedIndex;
	var sel_siz = theform.sizes.selectedIndex;
	var sel_qty = theform.select_qty.selectedIndex;
	if(theform.colours[sel_col].value == "blank"){
		alert("Please select a colour");
		return false;
	}
	if(theform.sizes[sel_siz].value == "blank"){
		alert("Please select a size");
		return false;
	}
	if(theform.colours[sel_col].value != "blank" && theform.colours[sel_col].value != "blank" && theform.ComplexFeatures.value == ""){
		warnProductOutOfStock();		
		return false;			
	}
	obj.disabled = true;
	theform.submit();	
}
//if (document.le_form){
//	if (document.le_form.addtocart){
//		document.le_form.addtocart.onclick = ;
//	}
//}	

// Make the javascript version of the product image popup appear	
if (document.getElementById('productimagelink'))	
	document.getElementById('productimagelink').style.display = "block";
// Make the javascript version of the product swatch object appear
//if (document.getElementById('swatch_objectjs'))	
//	document.getElementById('swatch_objectjs').style.display = "block";	

function changeimg(id){
	document.getElementById('imgMainImageZoom').src=colourarray[id];
	swatchhighlighter(id);
}
function swatchhighlighter(id){
	for(i=0;i<colourarray.length-1;i++){
		document.getElementById('swatch_' + i).className="noborder";
	}
	document.getElementById('swatch_' + id).className="border";
	var colour=id+1;
	document.le_form.colours.options[colour].selected=true;		
	setupsizes();
}	
		
	
