function CatVariation(strColourAbbrev, strColour, iWidth, iHeight, strProductCode, iPrice, strItemID, iStock, iMinStockLevel) {
	this.strColourAbbrev = strColourAbbrev;
	this.strColour = strColour;
	this.iWidth = iWidth;
	this.iHeight = iHeight;
	this.strProductCode = strProductCode;
	this.iPrice = iPrice;
	this.strItemID = strItemID;
	try {this.iStock = parseInt(iStock);}catch(e){this.iStock = 0;}
	try {this.iMinStockLevel = parseInt(iMinStockLevel ? iMinStockLevel : "0");}catch(e){this.iMinStockLevel = 0;}
}


// Set Colour Options
function displayColourOptions(arrVariations,strCode) {
	var i, arrUsed = new Array();

	document.write("<select name='2colour' id='2colour" + strCode + "' onchange='ShowSizes(arrVariations" + strCode + ",\"" +  strCode + "\");'>");

	// options
	document.write("<option value='0' selected='selected'>Select Colour</option>");	
	for(i = 0; i < arrVariations.length; i++) {
		if(!arrUsed[arrVariations[i].strColour] && arrVariations[i].strColour != ""){
			document.write("<option value='" + arrVariations[i].strColourAbbrev + "'>" + arrVariations[i].strColour + "</option>");
			arrUsed[arrVariations[i].strColour] = 1;
		}
	}
	// close select
	document.write("</select>");
}

function displayColourBlocks(arrVariations,strCode) {
	var objDiv = document.getElementById(strCode + "ColourBlocks");
	var i, arrUsed = new Array();
	var strHtml = "";
	
	for(i = 0; i < arrVariations.length; i++) {
		if(!arrUsed[arrVariations[i].strColour] && (arrVariations[i].strColour != "")){
			strHtml += "<div class='colourblock' style='float:left; margin:2px; border:1px solid black; padding:0px;'><a href='#' onclick='setColourFromBlock(\"" + strCode + "\",\"" + arrVariations[i].strColour + "\"); ShowSizes(arrVariations" + strCode + ",\"" +  strCode + "\"); return false;'><img src='client_images/cat_images/" + arrVariations[i].strColourAbbrev.replace(/[\s\/\&]/g, "_") + ".gif' width='18' height='18' border='0' alt='" + arrVariations[i].strColour + "'></a></div>";
			arrUsed[arrVariations[i].strColour] = 1;
		}
	}
	objDiv.innerHTML = strHtml;
}

function displayPrice(arrVariations) {
	var objDiv = document.getElementById("cena");
	var i = 0;
	var strHtml = "";
	var mini = arrVariations[0].iPrice;
	var accSize = arrVariations[0].iWidth;
	
//	document.getElementById("czek").innerHTML = arrVariations[6].iPrice;
	
	for(i = 0; i < arrVariations.length; i++) {

//document.getElementById("czek").innerHTML += "<!-- aaa"+ i + "  "+ arrVariations[i].iPrice  + "-->\n";

if (arrVariations[i].iPrice < mini) {mini = arrVariations[i].iPrice; accSize = arrVariations[i].iWidth;}

	}
	objDiv.innerHTML += "&pound;" + mini.toFixed(2) + "&nbsp;<font size='-2'>(" + accSize + ")</font>";
}

function setColourFromBlock(strBodyPart, strColour) {
	var objColourSelect = document.getElementById("2colour" + strBodyPart);
	var i;
	var fFound = false;
	
	for(i = 0; !fFound && (i < objColourSelect.options.length); i++) {
		if(objColourSelect.options[i].text == strColour) {
			fFound = true;
			objColourSelect.selectedIndex = i;
		}
	}
//	ShowSizes(arrVariations + strBodyPart , strBodyPart );
}

function ShowSizes (arrVariations,strCode){
	
	var dropColours = document.getElementById('2colour' + strCode);
	var dropSizes = document.getElementById('size' + strCode);
	var i=0;
	var strColour = "";
	var strValue = "";
	var stockValue = 0;
	var IsSock = false;
	var fMens = (window.location.search.indexOf("id2=6:2516") != -1);
	var fBoys = (window.location.search.indexOf("id2=6:2518") != -1);
	var strImageExtension = ".jpg";

	document.getElementById('unitprice'  + strCode).innerHTML = "Select Colour/Size";

	if (document.getElementById('2colour' + strCode).selectedIndex > 0) {// Check Colour was selected for Price
		// Set strColour to selected value
		strColour = document.getElementById('2colour' + strCode).options[document.getElementById('2colour' + strCode).selectedIndex].value;

			/*if(    (window.location.search.indexOf("id2=6:2527") != -1)
			|| (window.location.search.indexOf("id2=6:2530") != -1)
			|| (window.location.search.indexOf("id2=6:2545") != -1)
			|| (window.location.search.indexOf("id2=6:2534") != -1)
			|| (window.location.search.indexOf("id2=6:2537") != -1)
			|| (window.location.search.indexOf("id2=6:2542") != -1)
			|| (window.location.search.indexOf("id2=6:2552") != -1)
			|| (window.location.search.indexOf("id2=6:2548") != -1)) {
			strImageExtension = ".gif";
		}*/
		document.getElementById('garmentImage'+ strCode).src = "client_images/images_08/nc_" + strCode + "_" + strColour +'_main'+ strImageExtension;
	}
	
	if (dropSizes == null) {
	
		} else if (dropColours.selectedIndex > 0) {
		
			// Check Colour was selected for Sizes
			// Set strColour to selected value

		strColour = document.getElementById('2colour' + strCode).options[document.getElementById('2colour' + strCode).selectedIndex].value;

		for(i = dropSizes.options.length - 1; i >= 0; i--) {
				dropSizes.options[i] = null;
				}
		dropSizes.options[dropSizes.options.length]= new Option("Select Size", 0);				

arrVariations.sort(function(el1,el2){

sortarray = new Array();
sortarray['6/7'] = 0;
sortarray['S/B'] = 1;
sortarray['M/B'] =2;
sortarray['L/B']=3;
sortarray['XLB']=4;
sortarray['S']=5;
sortarray['M']=6;
sortarray['L']=7;
sortarray['XL']=8;
sortarray['XXL']=9;
sortarray['3XL']=10;
sortarray['4XL']=11;
							
    if(sortarray[el1.iWidth.replace("  ", "").replace(" ","")] < sortarray[el2.iWidth.replace("  ", "").replace(" ","")]) {return -1; 
	}
    else if (sortarray[el1.iWidth.replace("  ", "").replace(" ","")] > sortarray[el2.iWidth.replace("  ", "").replace(" ","")]){ return 1;
	}
    else return 0;
}
)

		for(i = 0; i < arrVariations.length; i++) {
		strValue = arrVariations[i].iWidth;
		stockValue = arrVariations[i].iStock;
		minStockLevel = arrVariations[i].iMinStockLevel;
		
		IsSock = (window.location.search.indexOf("id2=6:2554") != -1) || (window.location.search.indexOf("id2=6:2544") != -1) || (window.location.search.indexOf("id2=6:2532") != -1) || (window.location.search.indexOf("id2=6:2529") != -1)
		if(arrVariations[i].strColourAbbrev == strColour) {
			
			if(fMens && ((strValue.indexOf("B") != -1) || (strValue.indexOf("6") != -1)) && !IsSock) {
				// skip
			} else if(fBoys && (strValue.indexOf("B") == -1) && (strValue.indexOf("6") == -1) && !IsSock) {
				// skip
			} else {
				if ((stockValue >= minStockLevel) && (stockValue > 0)) {
				
					dropSizes.options[dropSizes.options.length]= new Option(strValue.replace("  ", "").replace(" ",""), strValue, false);
				
				} else {
					
					//if (navigator.appName != "Microsoft Internet Explorer") {
//						dropSizes.options[dropSizes.options.length]= new Option('NS'+strValue,'NS'+strValue, false);
//						dropSizes.options[dropSizes.options.length-1].disabled = true;
//					}
					
				}
				
				
			}		
		}
	}
	
			if (dropSizes.options.length < 2) {
			document.getElementById('unitprice'  + strCode).innerHTML = "<span class='colorsize' style='text-decoration:underline;'>NO SIZES IN THIS SIZE RANGE</span>";
			dropSizes.remove(dropSizes.options[0]);
			dropSizes.options[dropSizes.options.length]= new Option('No sizes', 'No sizes', false);				

			document.getElementById("grdav"+strCode).style.display = "block";
			document.getElementById("av"+strCode).innerHTML = "Hide availibility";					
			}
}

}

// Set Size Options
function displaySizeOptions(arrVariations,strCode) {
						var i, arrUsed = new Array();
						var strValue = "";
						var stockValue = 0;
						var fMens = (window.location.search.indexOf("id2=6:2516") != -1);
						var fBoys = (window.location.search.indexOf("id2=6:2518") != -1);
						
						// open select
						document.write("<select name='size' id='size" + strCode + "' onchange='setPrice(\"" + strCode + "\",arrVarByName" + strCode + ");'>");
						// options
						document.write("<option value='0' selected='selected'>Select Size</option>");		
						
						for(i = 0; i < arrVariations.length; i++) {
							strValue = arrVariations[i].iWidth;
							stockValue = arrVariations[i].iStock;
					
							if(!arrUsed[strValue]) {
								if(fMens && (strValue.indexOf("B") != -1)) {
									// skip
								} else if(fBoys && (strValue.indexOf("B") == -1) && (strValue.indexOf("6") == -1)) {
									// skip
								} else {
									
								//	if (stockValue != 0) {
									document.write("<option value='" + strValue + "'>" + strValue + "</option>");					
								//	} else {
								//	document.write("<option value='" + strValue + "' disabled='disabled'>" + strValue + "</option>");
								//	}
								}
								arrUsed[strValue] = 1;
							}
						}

						// close select
						document.write("</select>");
					}


// Set the product price from selected options
function setPrice(strProductCode, arrVarByName) {
	var strColour = "";
	var strSize = "";
	var isStock = true;
	
	// Check Size was selected for Price
	if (document.getElementById('size' + strProductCode).selectedIndex > 0){
		// Set strSize to selected value
		strSize = document.getElementById('size' + strProductCode).options[document.getElementById('size' + strProductCode).selectedIndex].value;
		// Check to see if colour option exists
		if (document.getElementById('2colour' + strProductCode) == null) {
				var price = arrVarByName[strSize].iPrice;							
				document.getElementById('unitprice'  + strProductCode).innerHTML = "&pound;" + price.toFixed(2);
				// Set the product item
				document.getElementById('item' + strProductCode).value = arrVarByName[strSize].strItemID;
		} else if (document.getElementById('2colour' + strProductCode).selectedIndex > 0) {// Check Colour was selected for Price
			// Set strColour to selected value
			strColour = document.getElementById('2colour' + strProductCode).options[document.getElementById('2colour' + strProductCode).selectedIndex].value;
			// Set Price to display

				if (arrVarByName[strColour + strSize] != null) {
				var price = arrVarByName[strColour + strSize].iPrice;							
				document.getElementById('unitprice'  + strProductCode).innerHTML = "&pound;" + price.toFixed(2);
				// Set the product item
				
			isStock = !document.getElementById('size' + strProductCode).options[document.getElementById('size' + strProductCode).selectedIndex].disabled;

			if (isStock) {
				document.getElementById('item' + strProductCode).value = arrVarByName[strColour + strSize].strItemID;
}
			} else {
				document.getElementById('unitprice'  + strProductCode).innerHTML = "";
				document.getElementById('item' + strProductCode).value = "";
				alert("Sorry, this item is currently out of stock. \nPlease select another option.");
			}
		} else if (document.getElementById('2colour' + strProductCode).selectedIndex = 0) {
			
				document.getElementById('unitprice'  + strProductCode).innerHTML = "Select Colour/Size";
		}
		
		// end Check Colour was selected for Price
	}
	// end Check Size was selected for Price
}

//  check for valid numeric strings
function IsNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var fResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && fResult; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			fResult = false;
		}
	}
	return fResult;
}
// Check form was completed sucessfully
function chkForm(strProductCode,passedID) {
	var qty = document.getElementById('qty' + strProductCode);
	
	// Check Colour exist or if it was selected
	if (document.getElementById('2colour' + strProductCode) != null && document.getElementById('2colour' + strProductCode).selectedIndex == 0){
			alert("Please select a Colour option");
			document.getElementById('2colour' + strProductCode).focus();
	}
	// Check Size was selected
	else if (document.getElementById('size' + strProductCode).selectedIndex == 0){
			alert("Please select a Size option");
			document.getElementById('size' + strProductCode).focus();
	}
	// Check product is available
//	else if (document.getElementById('item' + strProductCode).value == "") {
//		alert("Sorry, this item is currently out of stock. \nPlease select another option.");
//	}
	// Check Quantity was added
	else if (qty.value.length == 0){
			alert("Please enter your Quantity");
			qty.value="";
			qty.focus();
	}
	// Check Quantity is numeric
	else if (IsNumeric(qty.value) == false) {
		alert("Please check your Quantity");
		qty.value="1";
		qty.focus();
	}
	else {
		// Submit item to basket
		
		
	document.getElementById('frmProduct' + strProductCode).submit();

	}
}
