function selButtonClick(sizeId,colorId){
	
	var original_sizeId=sizeId;
	var original_colorId=colorId;
	
	//use current values for parameters not passed
	if(sizeId==null) sizeId=document.getElementById("sizeid").value;
	if(colorId==null) colorId=document.getElementById("colorid").value;
	
	var selectedITObject;
	for(var i=0;i<arr_it.length;i++){
		if(arr_it[i].sizeid==sizeId&&arr_it[i].colorid==colorId){
			selectedITObject=arr_it[i];
			break;
		}
	}
	
	//such combination not found
	//use the first available value for the parameter not passed
	if(selectedITObject==null){
		if(original_colorId!=null){
			for(var i=0;i<arr_it.length;i++){
				if(arr_it[i].colorid==colorId){
					selectedITObject=arr_it[i];
					break;
				}
			}
		}

		if(original_sizeId!=null){
			for(var i=0;i<arr_it.length;i++){
				if(arr_it[i].sizeid==sizeId){
					selectedITObject=arr_it[i];
					break;
				}
			}
		}
		
	}
	
	if(selectedITObject!=null){
		useITObject(selectedITObject);
		//mark buttons selected
		var sel_buttons=document.getElementById("td_variants").getElementsByTagName("a");
		for(var i=0;i<sel_buttons.length;i++){
			//show "out of stock" image (it will be hidden later in some buttons - see below)
			document.getElementById(sel_buttons[i].id.replace(/^a_/,"img_")).style.display="";
			if(sel_buttons[i].name=='a_color'){
				sel_buttons[i].className=((sel_buttons[i].id=='a_color'+selectedITObject.colorid)?"ITColorBoxSelected":"ITColorBox");
			}
			if(sel_buttons[i].name=='a_size'){
				sel_buttons[i].className=((sel_buttons[i].id=='a_size'+selectedITObject.sizeid)?"ITSizeBoxSelected":"ITSizeBox");
			}
		}
		//hide "out of stock" image on available items		
		for(var i=0;i<arr_it.length;i++){
			if(arr_colors.length>1&&arr_it[i].sizeid==selectedITObject.sizeid)
				document.getElementById("img_color"+arr_it[i].colorid).style.display="none";
			if(arr_sizes.length>1&&arr_it[i].colorid==selectedITObject.colorid)
				document.getElementById("img_size"+arr_it[i].sizeid).style.display="none";
		}
	}
}

function selRadioClick(selected_itid){
	for(var i=0;i<arr_it.length;i++){
		if(arr_it[i].itid==selected_itid){
			useITObject(arr_it[i]);
			document.getElementById("variants_radio"+arr_it[i].itid).checked=true;
			break;
		}
	}
}

function useITObject(it){
	if(it==null) return;
	document.getElementById("d_it_name").innerHTML=it.itn;
	document.getElementById("d_it_largeimg_link").href=(lngid==1?"ArticleZoomFr.asp":"ArticleZoom.asp")+"?itid="+it.itid;
	document.getElementById("d_it_largeimg").src="showimg.asp?id="+it.itid+"&fieldname=smallpic&tablename=it&fieldid=itid";
	document.getElementById("d_it_largeimg").alt=it.itn+" | "+it.ittn;
	document.getElementById("d_it_listprice").innerHTML=it.listprice;
	document.getElementById("d_it_avln").innerHTML=it.avln;
	document.getElementById("d_it_itid").innerHTML=it.itid;
	document.getElementById("itid").value=it.itid;
	document.getElementById("colorid").value=it.colorid;
	document.getElementById("sizeid").value=it.sizeid;
}

function initializeButtons(){
	if(document.getElementById("td_variants")==null) return;

	var colors_html="";
	if(arr_colors.length>1){
		for(var i=0;i<arr_colors.length;i++){
			colors_html+='<a name="a_color" id="a_color'+arr_colors[i].colorid+'" href="javascript:selButtonClick(null,'+arr_colors[i].colorid+');" title="'+arr_colors[i].colorn+'"><div style="width:20px;height:20px;background-color:'+arr_colors[i].htmlcolor+';">&nbsp;<img id="img_color'+arr_colors[i].colorid+'" src="img/outOfStockX.gif" class="xred" style="display:none;"/></div></a>';
		}
		colors_html='<div style="clear:both;">Select color:'+colors_html+'</div>';
	}
	
	var sizes_html="";
	if(arr_sizes.length>1){
		for(var i=0;i<arr_sizes.length;i++){
			sizes_html+='<a name="a_size" id="a_size'+arr_sizes[i].sizeid+'" href="javascript:selButtonClick('+arr_sizes[i].sizeid+',null);" title="'+arr_sizes[i].sizen+'">'+arr_sizes[i].sizen+'<img id="img_size'+arr_sizes[i].sizeid+'" src="img/outOfStockX.gif" class="xred" style="display:none;"/></a>';
		}
		sizes_html='<div style="clear:both;">Select size:'+sizes_html+'</div>';
	}
	
	document.getElementById("td_variants").innerHTML=colors_html+sizes_html;
	selButtonClick(document.getElementById('sizeid').value,document.getElementById('colorid').value);
}

function initializeRadios(){
	if(document.getElementById("td_variants")==null) return;

	var generated_html="";
	if(arr_it.length>1){
		for(var i=0;i<arr_it.length;i++){
			generated_html+='<div><input type="radio" name="variants_radio" id="variants_radio'+arr_it[i].itid+'" value="'+arr_it[i].itid+'" onclick="selRadioClick(this.value);"/><label for="variants_radio'+arr_it[i].itid+'">'+arr_it[i].itn+' ('+arr_it[i].listprice+'&euro;)</label></div>';
		}
		generated_html='<div style="clear:both;">'+generated_html+'</div>';
	}
	
	document.getElementById("td_variants").innerHTML=generated_html;
	selRadioClick(document.getElementById('itid').value);
}

//-------------
//--inline code
//-------------


for (var i=0;i<arr_it.length;i++){
	var c_color,c_size, element_found;

	element_found=false;
	for (var j=0;j<arr_colors.length;j++){
		if(arr_it[i].colorid==arr_colors[j].colorid){
			element_found=true;
			break;
		}
	}
	if(!element_found){
		c_color=new Object();
		c_color.colorid=arr_it[i].colorid;
		c_color.colorn=arr_it[i].colorn;
		c_color.htmlcolor=arr_it[i].htmlcolor;
		arr_colors.push(c_color);
	}

	element_found=false;
	for (var j=0;j<arr_sizes.length;j++){
		if(arr_it[i].sizeid==arr_sizes[j].sizeid){
			element_found=true;
			break;
		}
	}
	if(!element_found){
		c_size=new Object();
		c_size.sizeid=arr_it[i].sizeid;
		c_size.sizen=arr_it[i].sizen;
		arr_sizes.push(c_size);
	}
}

if(variousPrices)
	initializeRadios();
else
	initializeButtons();
