document.write("<script type=\"text/javascript\" src=\"assets/scripts/fullkit.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"assets/scripts/EdeptiveAjax.js\"></script>");

// globals
var gstrDomain = window.location.protocol + "//" + window.location.host + "";
var goAjaxVariations = null;
var gsBodyPart = "";

function getVariations(iParentID, fInclAssets, sFields) {
	
	//alert ("Polecono mi znalezc vars dla parenta  " +iParentID + " z parametren=" +  fInclAssets + "\n bede pobieral pola" + sFields);
	
	var sUrl = gstrDomain + "/webservices/io_json.asmx/getCatalogueVariationsMatchJson2";
	var sParams = "";
	var sHtml = "";

	sParams += (sParams == "" ? "" : "&") + "ParentID=" + iParentID;
	sParams += (sParams == "" ? "" : "&") + "IncludeAssets=" + fInclAssets;
	sParams += (sParams == "" ? "" : "&") + "FieldList=" + sFields;
	sParams += (sParams == "" ? "" : "&") +	"CategoryID=" + getUrlParam("type");
	sParams += (sParams == "" ? "" : "&") + "CategoryTypeID=" + 6;
	//sUrl = "load_xml.aspx?url=" + escape(sUrl) + "&params=" + escape(sParams);
	try {
		goAjaxVariations = new EdeptiveAjax();
		goAjaxVariations.postRequest(sUrl, sParams, onVariationsResponse);
	} catch(e) {
		displayError(e.description);
	}
	// show loading animation
	document.getElementById("msgDiv").innerHTML = "<div id='variationsLoading'><img src='assets/images_10/loading.gif' alt='Loading...' />&nbsp;&nbsp;&nbsp;Loading...</div>";
}

function onVariationsResponse() {
	var sStatus = goAjaxVariations.checkReadyState("devnull", null, null, null);
	
	if(sStatus == "OK") {
		var response = goAjaxVariations.request.responseText;
		var oItem = null;
		var i = 0;
		var aResponseVariations = null, aVariations = null;

if(response != "") {
			response = response.substring(response.indexOf("["));
			response = response.substring(0, response.lastIndexOf("]") + 1);

//alert ("response przetworzone" +  response );

if(response != "") {
				aResponseVariations = eval(response);
								
				for(i = 0; i < aResponseVariations.length; i++) {
				
					oItem = aResponseVariations[i];

					oCat = new CatVariation(oItem.colour, oItem.colour_abbrev, oItem.supplier_product_code, oItem.price2, oItem.id, oItem.variation_parent_id, oItem.min_stock_level,  oItem.stock);

					aVariations = eval("arrVariations" + oCat.strProductCode.substring(0, oCat.strProductCode.indexOf("-")));
					aVariations.push(oCat);
					aVariations = eval("arrVarByName" + oCat.strProductCode.substring(0, oCat.strProductCode.indexOf("-")));
					aVariations[oCat.strColourAbbrev + oCat.iWidth] = oCat;
				}
			}
			document.getElementById("msgDiv").innerHTML = "&nbsp;";
//			alert ("!!!");
			setClothingStyle(gsBodyPart, getUrlParam(gsBodyPart), getUrlParam(gsBodyPart + "col"));
			gsBodyPart = "";
		} else {
			document.getElementById("msgDiv").innerHTML = "Sorry no variations loaded...";
		}
	} else if(sStatus != null) {
		document.getElementById("msgDiv").innerHTML = "Sorry an error occurred: " + sStatus;
	}
}

function setClothingStyle2(strBodyPart){

	var objForm = document.forms[strBodyPart + "Form"];
	var objStyleSelect = objForm.elements["style"];
	var strStyle = "", strStyleID = "";
	var arrVars = null;

	// 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;
	}
	
	// convert to style code
	strStyle = arrStyles[strBodyPart + strStyle];
	strStyle = strStyle.replace(/[\s\/\&]/g, "");
	arrVars = eval("arrVariations" + strStyle);

//	alert ("strSTyle  " +strStyle + "a tablica=" + arrVars.length );

if(arrVars.length == 0) {
		gsBodyPart = strBodyPart;
		getVariations(strStyleID,false,'colour,colour_abbrev,supplier_product_code,price2,id,variation_parent_id,min_stock_level,stock');
	} else {
		setClothingStyle(strBodyPart);
	}

}
var gaQueue = new Array();
function setClothingStyleQueue(aQueue) {
	gaQueue = aQueue;
	processClothingQueue();
}
function processClothingQueue() {
	if(gsBodyPart == "") {
		if(gaQueue.length > 0) {
			setClothingStyle2(gaQueue.pop());
			setTimeout("processClothingQueue()", 1000);
		}
	} else {
		setTimeout("processClothingQueue()", 1000);
	}
}

