function CatVariation(strColour, strColourAbbrev, strProductCode, iPrice, strItemID, strVariationParentID, strMinStock, strStock) {
	this.strColour = strColour;
	this.strColourAbbrev = strColourAbbrev;
	this.strProductCode = strProductCode;
	this.iPrice = iPrice;
	this.strItemID = strItemID;
	this.strVariationParentID = strVariationParentID;
	this.strMinStock = strMinStock;
	this.strStock = strStock;	
}

String.prototype.trim = function () {
return this.replace(/^\s*|\s*$/,"");
}

function sortNumber(a, b)
{
sortarray = new Array();
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;

return sortarray[a] - sortarray[b];
}

var arrSelections = new Array();
arrSelections["shirt"] = null;
arrSelections["short"] = null;
arrSelections["sock"] = null;
var arrOldStyles = new Array();
var arrOldStyleIDs = new Array();
arrOldStyles["shirt"] = "";
arrOldStyleIDs["shirt"] = "";
arrOldStyles["short"] = "";
arrOldStyleIDs["short"] = "";
arrOldStyles["sock"] = "";
arrOldStyleIDs["sock"] = "";

// show the correct colours for the given shirt style
//	and change selected colour to the 1st for this style
//	and change the image to that colour for this style
// if no clothing style is yet selected, uses the first style
// if strStartValue is not null, find that value in the options and select it (used when page loads going backwards)

function setClothingStyle(strBodyPart, strStartValue, strStartColour) {
	var objForm = document.forms[strBodyPart + "Form"];
	var objStyleSelect = objForm.elements["style"];
	var strStyle = "", strStyleID = "";
	var arrVars = null;
	var fFound = false;
	
	if(arrOldStyles[strBodyPart] != "") {
//		document.getElementById(strBodyPart + arrOldStyles[strBodyPart]).style.display = "none"; // hide prev name + description
//		document.getElementById(strBodyPart + "BPC" + arrOldStyleIDs[strBodyPart]).style.display = "none"; // hide prev name + description
	}
	if(strStartValue) {
		fFound = false;
		for(var i = 0; !fFound && (i < objStyleSelect.options.length); i++) {
			fFound = (objStyleSelect.options[i].value == strStartValue);
		}
		if(fFound) {
			objStyleSelect.selectedIndex = i - 1;
		}
	}
	// get new style name
	if(objStyleSelect.selectedIndex >= 0) {
		strStyle = objStyleSelect.options[objStyleSelect.selectedIndex].text;
		strStyleID = objStyleSelect.options[objStyleSelect.selectedIndex].value;

	} else if(objStyleSelect.options.length > 0) {
		strStyle = objStyleSelect.options[0].text;
		strStyleID = objStyleSelect.options[0].value;
	} else {
		return;
	}
	
//	alert (strBodyPart + "   " + strStyleID + "   " + strStyle );
	
	// convert to style code
	strStyle = arrStyles[strBodyPart + strStyle];
	
//	alert (strStyle );
	
	strStyle = strStyle.replace(/[\s\/\&]/g, "");
	arrVars = eval("arrVariations" + strStyle);
	displayColourBlocks(strBodyPart, arrVars, strStyle);
	displayColourOptions(strBodyPart, arrVars, strStartColour);
//	document.getElementById(strBodyPart + strStyle).style.display = "block"; // show new name + description
//	document.getElementById(strBodyPart + "BPC" + strStyleID).style.display = "block"; // show new name + description

	setCurrentImage(strBodyPart);
	// hold onto this style so things can be hidden if style is changed
	arrOldStyles[strBodyPart] = strStyle;
	arrOldStyleIDs[strBodyPart]= strStyleID;
}
// Set Colour Blocks
function displayColourBlocks(strBodyPart, arrVariations, baseProductCode) {
	var objDiv = document.getElementById(strBodyPart + "ColourBlocks");
	var i, arrUsed = new Array();
	var strHtml = "";
	
	for(i = 0; i < arrVariations.length; i++) {
		if(!arrUsed[arrVariations[i].strColour] && arrVariations[i].strColour != "" && arrVariations[i].strStock >= arrVariations[i].strMinStock){
			strHtml += "<div class='colourblock' style='float:left; margin:2px; border:1px solid black; padding:0px;'><a href='#' onclick='setColourFromBlock(\"" + strBodyPart + "\",\"" + arrVariations[i].strColour + "\");return false;'><img src='client_images/cat_images/" + arrVariations[i].strColourAbbrev.replace(/[\s\/\&]/g, "_") + ".gif' width='18' height='18' border='0' style='border: 1px solid #ffffff;' alt='" + arrVariations[i].strColour + "'></a></div>";
			arrUsed[arrVariations[i].strColour] = 1;
		}
	}
	objDiv.innerHTML = strHtml;
}
// Set Colour Options
function displayColourOptions(strBodyPart, arrVariations, strStartColour) {
	var objForm = document.forms[strBodyPart + "Form"];
	var objColourSelect = objForm.elements["colour"];
	var i, arrUsed = new Array();
	var fFirst = true;
	
	// remove existing options
	for(i = objColourSelect.options.length - 1; i >= 0; i--) {
		objColourSelect.options[i] = null;
	}
	// add new options
	for(i = 0; i < arrVariations.length; i++) {
		if(!arrUsed[arrVariations[i].strColour] && arrVariations[i].strColour != "" && arrVariations[i].strStock >= arrVariations[i].strMinStock){
			objOption = new Option(arrVariations[i].strColour, arrVariations[i].strColourAbbrev);
			if(strStartColour) {
				if(objOption.value == strStartColour) {
					objOption.selected = true;
				}
			} else if(fFirst) {
				objOption.selected = true;
				fFirst = false;
			}
			objColourSelect.options[objColourSelect.options.length] = objOption;
			arrUsed[arrVariations[i].strColour] = 1;
		}
	}
}
function setColourFromBlock(strBodyPart, strColour) {
	var objForm = document.forms[strBodyPart + "Form"];
	var objColourSelect = objForm.elements["colour"];
	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;
		}
	}
	setCurrentImage(strBodyPart);
}
// returns the URL of the current image
function getCurrentImageUrl(strBodyPart, strProductCode, strColour) {
	var strUrl = "";
	var strImageExtension = ".jpg";
	
	strProductCode = strProductCode.replace(/[\s\/\&]/g, "");
//	if (strBodyPart == "shirt" || strBodyPart == "kshirt" || strBodyPart == "kshort"){
//		strImageExtension = ".gif";
//	} else {
		strImageExtension = ".png";
//	}
	if(window.location.href.indexOf("training") == -1) {
		strUrl = "client_images/images_10/" + strProductCode + "_" + strColour.replace(/[\s\/\&]/g, "_") + "_kit" + strImageExtension;
	} else {
		strUrl = "client_images/images_10/" + strProductCode + "_" + strColour.replace(/[\s\/\&]/g, "_") + "_kit" + strImageExtension;
	}
	return strUrl;
}
// show the correct image for the selected style+colour of the given body part
//	and show the correct price for the selected style+colour
function setCurrentImage(strBodyPart) {
	var objForm = document.forms[strBodyPart + "Form"];
	var objStyleSelect = objForm.elements["style"];
	var strStyle = objStyleSelect.options[objStyleSelect.selectedIndex].text;
	var objColourSelect = objForm.elements["colour"];
	var strColour = "";
	var objDiv = document.getElementById(strBodyPart + "Image");
	var objImage = null;
	var objPrice = document.getElementById(strBodyPart + "Price");
	var objProductCode = document.getElementById(strBodyPart + "Code");
	var arrVars = null;
	var fFound = false;
	var iTotal = 0, iChildPrice = 999999, iAdultPrice = 0;
	var strImageExtension = ".jpg";
	var sizesArray = new Array();
	var sFound = false;
	
	if(objColourSelect.selectedIndex < 0) return;
	
	strColour = objColourSelect.options[objColourSelect.selectedIndex].value;
	strProductCode = arrStyles[strBodyPart + strStyle]; // convert name to product code
	strProductCode = strProductCode.replace(/[\s\/\&]/g, "");
	if(document.getElementById("tracksuitImage")) {
		if(strStyle.toLowerCase().indexOf("tracksuit (women") != -1) {
			objDiv = document.getElementById("tracksuitImage");
			document.getElementById("tracksuitImage").style.display = "block";
			document.getElementById("shirtImage").style.display = "none";
		} else {
			document.getElementById("tracksuitImage").style.display = "none";
			document.getElementById("shirtImage").style.display = "block";
		}
	}
	objImage = objDiv.firstChild.firstChild;
	objImage.src = getCurrentImageUrl(strBodyPart, strProductCode, strColour);
	arrVars = eval("arrVariations" + strProductCode);
	// show price of adult and child (if there are two prices)
//			for(i = 0; i < arrVars.length; i++) {
//				if(arrVars[i].strColourAbbrev == strColour) {
//					iChildPrice = Math.min(iChildPrice, arrVars[i].iPrice);
//					iAdultPrice = Math.max(iAdultPrice, arrVars[i].iPrice);
//				}
//			}



//	document.getElementById("msgDiv").innerHTML += "\n<!--" + "Teraz mam " + i +arrVars[i].strProductCode + "-->";									

		for(i = 0; !fFound && (i < arrVars.length); i++) {
				
			if(arrVars[i].strColourAbbrev == strColour) {
			
			if ((arrVars[i].strMinStock <= arrVars[i].strStock) && (arrVars[i].strStock >0)) {	
			
				sFound = false;
				dana = arrVars[i].strProductCode.substr (arrVars[i].strProductCode.lastIndexOf("-")+1,3);

					for (j = 0; !sFound && (i < sizesArray.length); j++) {

						if (sizesArray[j] == dana.trim()) {
						sFound = true;
						}
					}
					
					if (!sFound) {
						sizesArray.push (dana.trim());
						}
				
			}
					//if(iAdultPrice != iChildPrice) {
					//	objPrice.innerHTML = "Adult:" + iAdultPrice + " Junior:" + iChildPrice;
					//} else {
											//	objPrice.innerHTML = iAdultPrice;
					//}
											//	objProductCode.innerHTML = "Product Code: " + strProductCode;
				// ensure 2dp
					//						if(objPrice.innerHTML.indexOf(".") == -1) {
					//						objPrice.innerHTML += ".00";
					//						} else if(objPrice.innerHTML.indexOf(".") == objPrice.innerHTML.length - 2) {
					//							objPrice.innerHTML += "0";
					//						}
					// hold onto this object (used when passing selection info to next page)
			arrSelections[strBodyPart] = arrVars[i];		
				}
		}

	whichdiv = strBodyPart + "AvailableSizes";
	sizesArray.sort(sortNumber);
	document.getElementById(whichdiv).innerHTML = "Available sizes:<br>" + sizesArray.join(", ");
	

// This loop was here originally, to find proces from provided variaitons
//			for(i = 0; !fFound && (i < arrVars.length); i++) {
//			if(arrVars[i].strColourAbbrev == strColour) {
//					fFound = true; 

					//if(iAdultPrice != iChildPrice) {
					//	objPrice.innerHTML = "Adult:" + iAdultPrice + " Junior:" + iChildPrice;
					//} else {
											//	objPrice.innerHTML = iAdultPrice;
					//}
											//	objProductCode.innerHTML = "Product Code: " + strProductCode;
				// ensure 2dp
					//						if(objPrice.innerHTML.indexOf(".") == -1) {
					//						objPrice.innerHTML += ".00";
					//						} else if(objPrice.innerHTML.indexOf(".") == objPrice.innerHTML.length - 2) {
					//							objPrice.innerHTML += "0";
					//						}
					// hold onto this object (used when passing selection info to next page)


//					arrSelections[strBodyPart] = arrVars[i];
//				}
//			}



// calc total
//				objPrice = document.getElementById("shirtPrice");
//				iTotal += parseFloat(objPrice.innerHTML);
//				objPrice = document.getElementById("shortPrice");
//				iTotal += parseFloat(objPrice.innerHTML);
//				objPrice = document.getElementById("sockPrice");
//				iTotal += parseFloat(objPrice.innerHTML);
//				objPrice = document.getElementById("totalPrice");
//				objPrice.innerHTML = Math.floor(iTotal*100)/100;
//				// ensure 2dp
//				if(objPrice.innerHTML.indexOf(".") == -1) {
//					objPrice.innerHTML += ".00";
//				} else if(objPrice.innerHTML.indexOf(".") == objPrice.innerHTML.length - 2) {
//					objPrice.innerHTML += "0";
//				}
}
function printManikin(strStage) {
	var strUrl = "print_manikin_1.aspx?";
	var strTmp = "";
	
	strUrl += "stage=" + strStage;
	
	strTmp = document.getElementById("shirtImage").firstChild.firstChild.src;
	
	strTmp = strTmp.substring(strTmp.lastIndexOf("/") + 1);
	
	strUrl += "&shirt=" + strTmp;
	
	strTmp = document.getElementById("shortImage").firstChild.firstChild.src;
	
	strTmp = strTmp.substring(strTmp.lastIndexOf("/") + 1);
	
	strUrl += "&short=" + strTmp;
	
	strTmp = document.getElementById("sockImage").firstChild.firstChild.src;
	
	strTmp = strTmp.substring(strTmp.lastIndexOf("/") + 1);
		
	strUrl += "&sock=" + strTmp;
	
	if(strStage != "training" && strStage != "tracksuit") {
	
	strUrl += "&shirtn=" + arrSelections["shirt"].strVariationParentID + "&shirtcol=" + arrSelections["shirt"].strColour;
	// add short style + colour
	strUrl += "&shortn=" + arrSelections["short"].strVariationParentID + "&shortcol=" + arrSelections["short"].strColour;
	// add sock style + colour
	strUrl += "&sockn=" + arrSelections["sock"].strVariationParentID + "&sockcol=" + arrSelections["sock"].strColour;
	}
		
	if(strStage == "tracksuit") {
		strTmp = document.getElementById("tracksuitImage").firstChild.firstChild.src;
		strTmp = strTmp.substring(strTmp.lastIndexOf("/") + 1);
		strUrl += "&tracksuit=" + strTmp;
	}
	window.open(strUrl, "printMan", "width=567,height=600,resizable=yes,scrollbars=yes");
}

