﻿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 descriptions drop down box
var sizesdd_from='<select name="sizes" id="sizes" onchange="setupdescriptions()"><option value="blank">Select an Option</option></select>';	
document.getElementById('sizes').innerHTML = sizesdd_from;		

// Generate the empty sizes drop down box
var descriptiondd_from='<select name="descriptions" id="descriptions" onchange="setupComplexFeature()"><option value="blank">Select a Delivery Region</option></select>';	
document.getElementById('descriptions').innerHTML = descriptiondd_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.descriptions.selectedIndex = 1;
		// set up the sizes for the chosen description		
		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 descriptions drop down box
function setupsizes(){
	stockcheck();
	clearComplexFeature();		
	var thedescriptions=document.le_form.descriptions;
	var thesizes=document.le_form.sizes;		
	var thecurrentselection=thesizes[thesizes.selectedIndex].value;
	ClearOptions(thesizes);
	AddToOptionList(thesizes, "blank", "Select an Option");
	if (thedescriptions[thedescriptions.selectedIndex].value != "blank"){
		var tmpsizearray = new Array();
		var tmpsizearray_arr_pos=0;		
		for(od=0; od < OptionsData.length; od++) {
			if(OptionsData[od].description == thedescriptions[thedescriptions.selectedIndex].value){
				tmpsizearray[tmpsizearray_arr_pos] = OptionsData[od].size;						
				tmpsizearray_arr_pos++;	
			}		
		}
		tmpsizearray.sort();
		for(i=0; i < tmpsizearray.length; i++){
			AddToOptionList(thesizes, tmpsizearray[i], tmpsizearray[i]);
			if(thecurrentsizeselection == tmpsizearray[i])
				var tmp_setselected=thesizes.options[thesizes.length-1].selected = true;
		}
		setupComplexFeature();
	} else {
		var tmpsizearray=new Array();
		for(od=0; od < OptionsData.length; od++) {
			if (od == 0) {
				tmpsizearray[tmpsizearray.length]=OptionsData[od].size;
			} 
			else {
				var addItem = true;
				for(ca=0; ca < tmpsizearray.length; ca++) {
					if(OptionsData[od].size == tmpsizearray[ca])
						addItem = false;
				}
				if (addItem)
					tmpsizearray[tmpsizearray.length]=OptionsData[od].size;	
			}
		}	
		tmpsizearray.sort();
		for(i=0; i < tmpsizearray.length; i++){
			AddToOptionList(document.le_form.sizes, tmpsizearray[i], tmpsizearray[i]);
		}
	}	
}	
// Populate the descriptions drop down box
function setupdescriptions(){
	clearComplexFeature();
	var thedescriptions=document.le_form.descriptions;
	var thesizes=document.le_form.sizes;		
	var thecurrentdescriptionselection=thedescriptions[thedescriptions.selectedIndex].value;
	ClearOptions(thedescriptions);
	if (thesizes[thesizes.selectedIndex].value != "blank"){
		AddToOptionList(thedescriptions, "blank", "Select a Delivery Region");
		var tmpdescriptionarray_arr_pos=0;	
		tmpdescriptionarray=new Array();
		for(od=0; od < OptionsData.length; od++) {
			if(OptionsData[od].size == thesizes[thesizes.selectedIndex].value){
				tmpdescriptionarray[tmpdescriptionarray_arr_pos]=new Object();
				tmpdescriptionarray[tmpdescriptionarray_arr_pos]["description"]=OptionsData[od].description;
				tmpdescriptionarray[tmpdescriptionarray_arr_pos]["cost"]=OptionsData[od].cost;
				tmpdescriptionarray[tmpdescriptionarray_arr_pos]["stocklevel"]=OptionsData[od].stocklevel;					
				tmpdescriptionarray_arr_pos++;	
			}		
		}			
	myBubbleSort(tmpdescriptionarray,tmpdescriptionarray.length);			
		for(i=0; i < tmpdescriptionarray.length; i++){
			var	price_fixed = parseFloat(tmpdescriptionarray[i].cost);
			if(tmpdescriptionarray[i].stocklevel == 0)
				var Opttext = tmpdescriptionarray[i].description + " £" + price_fixed.toFixed(2) + " Out of stock";				
			else
				var Opttext = tmpdescriptionarray[i].description + " £" + price_fixed.toFixed(2);
			AddToOptionList(thedescriptions, tmpdescriptionarray[i].description, Opttext);
			if(thecurrentdescriptionselection == tmpdescriptionarray[i].description)
				var tmp_setselected=thedescriptions.options[thedescriptions.length-1].selected = true;
		}
		setupComplexFeature();
	} else {
		AddToOptionList(thedescriptions, "blank", "Select a Delivery Region");
		setupdescriptions();
	}	
}		
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.descriptions.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].description == theform.descriptions[sel_col].value && OptionsData[od].stocklevel != 0){
				var selectedOption = " |" + 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);
}
setupsizes();

function checkselectedoptions(obj){
	var theform = document.le_form;
	var sel_col = theform.descriptions.selectedIndex;
	var sel_siz = theform.sizes.selectedIndex;
	// var sel_qty = theform.select_qty.selectedIndex;
	if(theform.descriptions[sel_col].value == "blank"){
		alert("Please select an Option");
		return false;
	}
	if(theform.sizes[sel_siz].value == "blank"){
		alert("Please select a Delivery Region");
		return false;
	}
	if(theform.descriptions[sel_col].value != "blank" && theform.descriptions[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=descriptionarray[id];
	swatchhighlighter(id);
}
function swatchhighlighter(id){
	for(i=0;i<descriptionarray.length-1;i++){
		document.getElementById('swatch_' + i).className="noborder";
	}
	document.getElementById('swatch_' + id).className="border";
	var description=id+1;
	document.le_form.descriptions.options[description].selected=true;		
	setupsizes();
}	
		
	
