var init = true;
function debug (debugValue) {
	//alert(debugValue);
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

var ProductImageSet = function(productViews, currentViewKey) {
	this.productViews = [];
	this.currentViewKey = currentViewKey;
}

var ProductView = function(name, altText, smallImageURL, mediumImageURL, largeImageURL) {
	this.name = name;
	this.altText = altText;
	this.smallImageURL = smallImageURL;
	this.mediumImageURL = mediumImageURL;
	this.largeImageURL = largeImageURL;
	this.selected = false;
}

var currentProductImageSet;

function Price (nowPrice, wasPrice, savingPercentage, savingAmount, showWasPricePercentage, showWasPriceAmount ) {
	this.nowPrice = nowPrice;
	this.wasPrice = wasPrice;
	this.savingAmount = savingAmount;
	this.savingPercentage = savingPercentage;
	this.showWasPricePercentage = showWasPricePercentage;
	this.showWasPriceAmount = showWasPriceAmount;
}

Price.prototype.getNowPrice = function() {return this.nowPrice;};
Price.prototype.getWasPrice = function() {return this.wasPrice;};
Price.prototype.getSavingAmount = function() {return this.savingAmount;};
Price.prototype.getSavingPercentage = function() {return this.savingPercentage;};
Price.prototype.getShowWasPricePercentage = function() {return this.showWasPricePercentage;};
Price.prototype.getShowWasPriceAmount = function() {return this.showWasPriceAmount;};

function SkuSettings (price) {
	this.price = price;
}

SkuSettings.prototype.getPrice = function() {return this.price;};

// KeyValue - Defines the keys for our hashmap
function KeyValue( key, value) {
    this.key = key;
    this.value = value;
}

// Map - defines the hash map array
function Map() {
    this.array = new Array();
}

Map.prototype.put = function( key, value ) {
    if( ( typeof key != "undefined" ) && ( typeof value != "undefined" ) ) {
        this.array[this.array.length] = new KeyValue( key, value);
    }
}

Map.prototype.get = function( key ) {

	debug("map.get('" + key + "'");
    for( var k = 0 ; k < this.array.length ; k++ ) {
    	debug("checking specified key[" + key + "] against stored key[" + this.array[k].key + "]");
        if( this.array[k].key == key ) {
	    	debug("found specified key[" + key + "]");
            return this.array[k].value;
        }
    }
    return "";
}

Map.prototype.length = function() {
    return this.array.length;
}

// On initial load we remove the options from the sub-options until user has selected previous option
function initOptions(multipleProducts) {
	
	// If this is a page with multiple products then we need to ensure we set all possible dropdowns
	var maxNoOfProducts = 1;
	var maxNoOfOptions = 10;
	var displayPriceLabel  = true;
	if (multipleProducts) {
		maxNoOfProducts = 10;
		displayPriceLabel  = false;
	}

	// Iterate through each product on the page (Assumes max of 10 for say a bundle/quick shop page)
	for (prodIdx = 0; prodIdx < maxNoOfProducts; prodIdx++) {
		var productPrefix = "";
		if (maxNoOfProducts > 1) {
			productPrefix = "p" + prodIdx;
		}

		var lastSelectedId  = "";	
		var lastSelectedElement  = "";	
		var lastSelectedOptionNo = 0;
		
		var previousSelected = false;

		var keyPrefix = productPrefix;
		var stockCheckOption = null;
		
		// Iterate through each drop down box and check if it has a selected option
		for (initIdx = 1; initIdx <= maxNoOfOptions; initIdx++) {
			var elementId = productPrefix + "attrValue" + (initIdx);		
			
			if (document.getElementById(elementId)) {
				var definingOption = document.getElementById(elementId);
				if (definingOption != "undefined") {
				
					
					
					
					
					
					//showSoldOut(definingOption, keyPrefix);
				
				
					// If the dropdown is selected then we need to ensure it is sorted
					var selIndex = definingOption.selectedIndex;
					var selValue = definingOption.options[selIndex].value;

						// current one has a selection, but is there another one.
			var testIdx = initIdx + 1;
			var testId = productPrefix + "attrValue" + (testIdx);		
			
			if (document.getElementById(testId) && document.getElementById(testId) != "undefined") {
				// Theres another option, so append the current option to the option string
				keyPrefix += selValue + "_";
			} else {
				// Last one so remember which options to stock check for.
				stockCheckOption = definingOption;
			}


					//alert("init opts selValue=" + selValue);
					if (selIndex > 0) {
					
					
					
						//sortSelect(definingOption, true);
						previousSelected = true;						
						lastSelectedId = productPrefix;
						lastSelectedElement = definingOption;
						lastSelectedOptionNo = initIdx;
						
					} else {
						if (initIdx==1 || previousSelected) {
							// Lets sort the current drop down 
							//sortSelect(definingOption, true);
						} else {
							definingOption.length = 1; // We always leave the default option around
							definingOption.disabled="true";				
						}
						previousSelected = false; // This option has not been selected yet so preven tsubsequent options from being enabled
					}
				}
			} else {
				//debug ("Reached end of attributes");
				initIdx = maxNoOfOptions + 1;
			}		

		}
		
		if (stockCheckOption != null) {
			showSoldOut(definingOption, keyPrefix);
		}

		// Save the default price details for use later
		var priceElementId = 'priceelement' + productPrefix;	
		if (document.getElementById(priceElementId)) {
			defaultPriceDetails.put(priceElementId.toUpperCase(), document.getElementById(priceElementId).innerHTML);
		}

		// Now that we have reselected all required options we need to ensure the correct price is shown using the last selected options
		// but only if a option selected
		if (previousSelected) {
		    switchSkuSettings(document.OrderItemAddForm, lastSelectedElement, lastSelectedId, lastSelectedOptionNo, true);
		}
		

	}
	
	init = false;
}

function showSoldOut(definingOption, prefix) {

	if (!definingOption.disabled) {
		var keyValue = "";

		for (optCounter=1; optCounter < definingOption.options.length; optCounter++) {
		
			var currOptText = definingOption.options[optCounter].value;
		
			// See if we have a stock figure for this option - if so, this is the last drop-down, so display out of stock message if zero stock
			var stockMessage = " - (Sold out)";
		
			var theKey = prefix + currOptText;
//			if (initIdx === 1) {
	//			theKey = prefix + currOptText
		//	}
			//else {
				//theKey = keyValue + "_" + currOptText
			//}
		
			var stockLevel = skuStockMap.get(theKey);
			if (stockLevel === 0) {
				definingOption.options[optCounter].text += stockMessage;
			}
		}
	
//		var selIndex = definingOption.selectedIndex;
	//	var selValue = definingOption.options[selIndex].value;
	
		// Add the currently selected value to the key
		//if (selIndex > 0) {
			//var selValue = definingOption.options[selIndex].value;
			//keyValue += selValue + "_"
		//}
	}
}


function validateSelectedOptions (aForm, multipleProducts) {

	var maxNoOfProducts = 1;
	var maxNoOfOptions = 10;
	if (multipleProducts) {
		maxNoOfProducts = 10;
	}
	
	for (prodIdx = 1; prodIdx <= maxNoOfProducts; prodIdx++) {
		var productPrefix = "";
		if (maxNoOfProducts > 1) {
			productPrefix = "p" + prodIdx;
		}

		// Only validate if the add checkbox is present and set or noty present at all 
		var checkBoxId = productPrefix + "_selected";
		var bValidate=true;
		if (document.getElementById(checkBoxId)) {
			var includeProduct = document.getElementById(checkBoxId);
			if (!includeProduct.checked) {
				bValidate=false;
			}
		}
		
		if (bValidate) {
		for (i = 1; i <= maxNoOfOptions; i++) {
				var elementId = productPrefix + "attrValue" + (i);
				if (document.getElementById(elementId)) {
					var definingOption = document.getElementById(elementId);
					if (definingOption !== "undefined") {
						var selIndex = definingOption.selectedIndex;
						var errorText = definingOption.options[0].text + " " + definingOption.options[0].value;
						if (selIndex <= 0) {
							alert(errorText);
							return false;
						}
					}
				} else {
					i = maxNoOfOptions + 1;
				}		
			}		
		}
	}

	return true;
}

function handleSpecialChars(selValue){
	//handle encoded ampersand followed by eol
	var ampIdx=selValue.search(/\&amp;$/g);
	while(ampIdx>=0){
		selValue=insertString(selValue, ampIdx+5, "amp;");
		ampIdx=selValue.search(/\&amp;$/g);
	}
	//handle encoded ampersand followed by whitespace
	ampIdx=selValue.search(/\&amp;[\s\&]/g);
	while(ampIdx>=0){
		selValue=insertString(selValue, ampIdx+5, "amp;");
		ampIdx=selValue.search(/\&amp;[\s\&]/g);
	}						
	//handle non-ampersand special characters
	ampIdx=selValue.search(/\&[^(amp;)].*/g);
	while(ampIdx>=0){
		selValue=insertString(selValue, ampIdx+1, "amp;");
		ampIdx=selValue.search(/\&[^(amp;)]/g);
	}	
	return selValue;
}
	
function insertString(origString, insertPos, insertStr) {
	var firstPart=origString.substring(0, insertPos);
	firstPart=firstPart+insertStr;
	var secondPart=origString.substring(insertPos);
	return firstPart+secondPart;
}

// This function changes the prices and stock indicator
// based upon the selected options
function switchSkuSettings( aForm, aField, keyPrefix, optionNumber, displayPriceLabel) {
	//debug ("In switchSkuSettings....");
	
	// We build up a key value based upon all the definining attributes selected
	// TODO We assume max of 10 defining attributes at this stage, need to make this more generic
	var keyValue = "";
	var allOptionsSelected = true;
	var previousOptionSelected = true;
	var firstOptionValue = "";

	// For the current product on the page (support Product Display/ Bundle and Quick Shop so may be more thasn 1 product)
	// Check if all the options are selected and change the price and stock indicator for the selected sku as necessary
	// We locate each select field named attrValue for the current product on the page and check if they are all selected
	for (i = 0; i <= 10; i++) {
		var elementId = keyPrefix + "attrValue" + (i+1);
		//debug("checking for element:" + elementId);
		
		if (document.getElementById(elementId)) {
			var definingOption = document.getElementById(elementId);
			if (definingOption !== "undefined") {
				//debug ("found attribute " + elementId);
		
				if (eval((i+1) > optionNumber)) {
					// We have gone past the options that was selected so we need to reset this option
					definingOption.selectedIndex = 0;
				} 
				var selIndex = definingOption.selectedIndex;
				var selValue = definingOption.options[selIndex].value;

				//alert("selectedIndex:" + selIndex + " selValue=" + selValue);			
				if (selIndex <= 0) {
					allOptionsSelected = false;
				} else {
					var selValue = definingOption.options[selIndex].value;
					
					// Since the browser will render the values in the drop down by decoded we have to reencode the & character before we look it up
					// We dont reencode if this is called from the initOptions method as the valules will at this point be correct
					if (!init) {
						//alert("init=" + init + " selValue:" + selValue);
						//ampersands need special handling. pad &amp; with another amp;
						selValue=handleSpecialChars(selValue);
						//selValue = selValue.replace(/&/gi,'&amp;');
					}
				
					//alert("selValue=" + selValue);
					//selValue = selValue.replace(/\"/g,'&#034;').replace(/&quot;/gi,'&amp;quot;');
					//selValue = selValue.replace(/\"/g,'&#034;');
					//selValue = selValue.replace(/&amp;/gi,'&amp;amp;');
					//selValue = selValue.replace(/&quot;/gi,'&amp;quot;');
					if (i === 0) {
						firstOptionValue = selValue.rtrim();
					}
					
					if (selValue == "") {
						allOptionsSelected = false;
					} else {
						if (keyValue != "") {
							keyValue = keyValue + "_";
						} else {
							keyValue = keyPrefix;
						}
						keyValue = keyValue + selValue.rtrim();					
						//alert("keyValue=:" + keyValue);
					}
				}

				// We change the options for the next set of dropdowns if this is the one that has changed
				//debug("About to check " + (i+1) + " > " + optionNumber);
				if (eval((i+1) > optionNumber)) {
					//debug("Lets update the sub options for keyValue:" + keyValue);
					if (previousOptionSelected) {
						var subOptions = optionValueMap.get(keyValue);	
						if (subOptions !== "undefined" && subOptions !== null && subOptions != "") {
							//debug("Found sub options");
							definingOption.disabled="";
							//debug("Settings length to " + (subOptions.length+1));
							definingOption.length = subOptions.length+1;
							for (var subIdx = 0; subIdx < subOptions.length ; subIdx++ ) {
							
								currOptText = subOptions[subIdx];
								
								// See if we have a stock figure for this option - if so, this is the last drop-down, so display out of stock message if zero stock
								var stockMessage = "";
								var stockLevel = skuStockMap.get(keyValue + "_" + currOptText);
								if (stockLevel === 0) {stockMessage = " - (Sold out)";}
								
								definingOption.options[subIdx+1].text = currOptText + stockMessage;
								definingOption.options[subIdx+1].value = subOptions[subIdx];
							}							
							//sortSelect(definingOption, true);
						} else {
							definingOption.length = 1;
							definingOption.disabled="true";
						}
					} else {
						definingOption.length = 1;
						definingOption.disabled="true";
					}
					
					// We are resetting this suboption so we cannot have selected all options
					allOptionsSelected = false;
				}
				
				//debug("current selected index =" + selIndex);
				if (selIndex <= 0) {
					previousOptionSelected = false;
				}
			}
		} else {
			//debug ("Reached end of attributes");
			break;
		}		
	}
	
	var priceElementId = 'priceelement' + keyPrefix;
	
	//alert("switchSkuSettings: looking for priceelement:" + priceElementId + " keyPrefix:" + keyPrefix);
	if (allOptionsSelected == true) {
		//alert("tony: switchSkuSettings: Lets find if the images or prices need to be changed using keyValue:" + keyValue);
		var skuSettings = map.get(keyValue);

        if (skuSettings === "undefined" || skuSettings === null || skuSettings === "") {
        		// The key values need to be double escaped here such that & -> &amp;amp; and " -> &amp;quot;
        		// NB. there is no functionality for &apos; yet - this may need to be added later.
                //keyValue = keyValue.replace(/&/g,'&amp;');
                //keyValue = keyValue.replace(/&amp;#034;/gi,'&quot;');
                //keyValue = keyValue.replace(/&/g,'&amp;');

				//alert("switchSkuSettings: Lets find if the images or prices need to be changed using converted keyValue:" + keyValue);
                skuSettings = map.get(keyValue);
        }
        
        //alert("skuSettings:" + skuSettings);
		if (skuSettings !== "undefined" && skuSettings !== null && skuSettings !== "") {
			// default to true if not passed
			if(displayPriceLabel == null) {	
				displayPriceLabel = true; 
			}
			
			// We found the details so let change the price information
			var priceString = "";
			
			var nowLabel = "";
			if (skuSettings.getPrice().getShowWasPricePercentage() === "true" || skuSettings.getPrice().getShowWasPriceAmount() === "true") {
				nowLabel = "Now&nbsp;";
			}
			
			priceString += "<ul><li>";
			// add price label if required
			if(displayPriceLabel) {
				priceString += "<span class='label'>Price&nbsp;" + nowLabel + "</span>";
			} else {
				priceString += "<span class='nowlabel'>" + nowLabel + "</span>";
			}
			
			priceString += "<span class='amount'>" + skuSettings.getPrice().getNowPrice() + "</span>";
	
			if (skuSettings.getPrice().getShowWasPricePercentage() === "true" || skuSettings.getPrice().getShowWasPriceAmount() === "true") {
				priceString += "<span class='price'><span class='waspricelabel'>Was </span><span class='wasprice'>" + skuSettings.getPrice().getWasPrice() + "</span></span>";
				if (skuSettings.getPrice().getShowWasPricePercentage() === "true") {
					priceString += "<span class='price'><span class='saveuptopercentlabel'>Save </span><span class='saveuptopercent'>" + skuSettings.getPrice().getSavingPercentage() + "%</span></span>";
				}
				if (skuSettings.getPrice().getShowWasPriceAmount() === "true") {
					priceString += "<span class='price'><span class='saveuptoamountlabel'>Save </span><span class='saveuptoamount'>" + skuSettings.getPrice().getSavingAmount() + "</span></span>";
				}
			}
			priceString += "</li></ul>";
			
			//alert("About to set priceElementId:" + priceElementId + " to " + priceString);
			if (document.getElementById(priceElementId) !== null && document.getElementById(priceElementId) !== "undefined") {
				document.getElementById(priceElementId).innerHTML=priceString;
			}
		} else {
			if (document.getElementById(priceElementId) !== null && document.getElementById(priceElementId) !== "undefined") {
				document.getElementById(priceElementId).innerHTML="No price available.";
			}
		}
	} else {
		if (document.getElementById(priceElementId) !== null && document.getElementById(priceElementId) !== "undefined") {
			document.getElementById(priceElementId).innerHTML=defaultPriceDetails.get(priceElementId.toUpperCase());
		}
	}
	
	debug("All options selected = " + allOptionsSelected);
}

// Define page level variables
var skuAlternativeImages = new Map();
var productImageSetMap = new Map();
var map = new Map();
var optionValueMap = new Map();		
var defaultPriceDetails = new Map();
var defaultMainImageSource = "";
var defaultMainImageAltText = "";
var defaultHugeImageSource = "";

// Some generic sort functions for select fields
// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
	if (value1 > value2) return(1);
    if (value1 < value2) return(-1);
    return(0);
}

// Some generic sort functions for select fields
// sort function - ascending (case-insensitive)
function sortFuncNumericAsc(record1, record2) {
    return(record1.optText - record2.optText);
}

// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(-1);
    if (value1 < value2) return(1);
    return(0);
}

function sortSelect(selectToSort, ascendingOrder) {
    if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort

    // copy options into an array
    // NB We skip first row as this is always 'Please select...'
    var allNumeric = true;
    var myOptions = [];
    for (var loop=1; loop<selectToSort.options.length; loop++) {
        myOptions[loop-1] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
        if (allNumeric) {
        	allNumeric = (!isNaN(myOptions[loop-1].optText));
        }
    }

    // sort array
    if (ascendingOrder) {
    	if (allNumeric) {
            myOptions.sort(sortFuncNumericAsc);
    	} else {
	        myOptions.sort(sortFuncAsc);
    	}
    } else {
    	if (allNumeric) {
	        myOptions.sort();
       	} else {
	        myOptions.sort(sortFuncDesc);
       	}
    }

    // copy sorted options from array back to select box
    selectToSort.options.length = 1;
    
//  SCO01/04/000014
//  Unencode "double escaped" HTML character entities in the Option texts such that &amp;quot; -> &quot; 
//  and &amp;amp; -> &amp and place them in the Select object.
    for (var loop=0; loop<myOptions.length; loop++) {
        var optObj = document.createElement('option');
        optObj.value = myOptions[loop].optValue;
        
        var beforeConvert = myOptions[loop].optText;
        var afterConvert = beforeConvert.replace(/&amp;/gi,"&");   	

        optObj.innerHTML = afterConvert;
        selectToSort.appendChild(optObj);	
    }
}



//*******************************************************
// Refresh product details page when colour changes
// Only used for clients that do not load colour
// as a defning option
//*******************************************************
function refreshColour (aForm, aField) {

	// Construct url to refresh to
	var selIndex = aField.selectedIndex;
	var url = aField.options[selIndex].value;

	// Now add other field values so that we dont lose selected options
	var maxNoOfOptions = 10;
	for (i = 1; i <= maxNoOfOptions; i++) {
		// Append ant selected options to the url
		var elementId = "attrValue" + (i);		
		if (document.getElementById(elementId)) {
			var definingOption = document.getElementById(elementId);
			if (definingOption != "undefined") {
				var selIndex = definingOption.selectedIndex;
				if (selIndex > 0) {
					var selValue = definingOption.options[selIndex].value;
					var selName = definingOption.name;
					url += "&" + selName + "=" + selValue;
				}
			}
		}
	}
	
	if (document.getElementById("WC_CachedProductOnlyDisplay_FormInput_quantity_In_OrderItemAddForm_1")) {
		var qty = document.getElementById("WC_CachedProductOnlyDisplay_FormInput_quantity_In_OrderItemAddForm_1");
		url += "&" + qty.name + "=" + qty.value;
	}
	
	// TODO Add the current qty and any other selected options to the request
	window.location.href = url;

}