// displays alert if selected combination of size/color is not available.. returns FALSE on alert, TRUE if no alert
function isAvailableWithThisSizeAndColor(product_id,color_id,size_id) {
	
	if (color_id=='') color_id = document.getElementById('colorId_'+product_id).value;
	if (size_id=='')  size_id  = document.getElementById('s_'+product_id).value	
	
	if (size_id=='') {
		alert(document.getElementById('ERROR_NO_CHOOSED_SIZE').value);
		return(false);
	}
	
	try{
		if (!ActiveColorSize[product_id][color_id][size_id]['available']) {
			alert(document.getElementById('ERROR_COLOR_AT_THIS_SIZE_NOT_AVAILABLE').value);
			return(false);
		} else {
			return(true);
		}
	}catch(err){
		alert(document.getElementById('ERROR_COLOR_AT_THIS_SIZE_NOT_AVAILABLE').value);
		return(false);
	}
}

// switch displayed product preview image based on color settings
function showViewImg(product_id,color_id, color_name) { 

	var o;
	var view_id    = document.getElementById('viewId_'+product_id).value;
	var size_id    = document.getElementById('s_'+product_id).value;
	if (isAvailableWithThisSizeAndColor(product_id,color_id,size_id)) {
	
		document.getElementById('colorId_'+product_id).value       = color_id;
		if (document.getElementById('colorName_'+product_id)) {
			document.getElementById('colorName_'+product_id).innerHTML = color_name;
		}
		replaceImg(product_id,view_id);
		return true;
	}
	return false;
}


function changeColor(product_id,color_id, color_name){
	if(showViewImg(product_id,color_id, color_name)){
		var mySelect = document.getElementById("s_"+product_id);
		var selectedSize = mySelect.options[mySelect.selectedIndex].value;
		while (mySelect.options.length) {
			    mySelect.options.remove (0);
		}
		var availableColorSize = ActiveColorSize[product_id][color_id];
		mySelect.options.add(new Option("","",false,false));
		for(var size in availableColorSize){
			var option = new Option(availableColorSize[size]['sizename'],size,false,false);
			if( !availableColorSize[size]['available']){
				option.disabled = true;
			}else if( size == selectedSize){
				option.selected = "1";
			}
			mySelect.options.add(option);
		}
	}
}
// produt image replacer - reload all images based on color/size settings
function replaceImg(product_id,viewId) 
{
	$('#viewId_' + product_id).val(viewId);
	colorId = $('#colorId_' + product_id).val();
	
	// switch main big image
	$('#product_'+product_id+'_image_big').attr('src', imgSrc[product_id+'_'+colorId+'_'+viewId]);
	
	// update zoom image source
	$('#product_' + product_id + '_image_bigZoom').val(imgSrcZoom[product_id+'_'+colorId+'_'+viewId]);
		
	if (imgSrc_mini_viewList[product_id]) 
	{
		for(i=1;i<imgSrc_mini_viewList[product_id].length;i++) 
		{
			thisViewId = imgSrc_mini_viewList[product_id][i];
			$('#product_' + product_id + '_image_mini_' + thisViewId).attr('src',  imgSrc_mini[product_id+'_'+colorId+'_'+thisViewId]);
		}
	}
}

function refreshBasket()  
{ 
	xajax_refreshBasket(); 
}

function onChangeBasket() 
{ 
	xajax_refreshBasket(); 
}

function onChangeProduct(productId,characterId,isSizeTableCall) 
{
	if (typeof(isSizeTableCall) == 'undefined')
	{
		isSizeTableCall = 0;
	}
	
	if (typeof(characterId) == 'undefined')
	{
		characterId = 0;
	}
	
	__productDetailsInitialized = false;
	
	xajax_onChangeProduct(productId,characterId,isSizeTableCall); 
}

function addToBasket(product_id,onlyCloseBasketInfobox,infoBoxOffset)     
{ 
	openerElementId = 'ADD_TO_BASKET_BUTTON_' + product_id;
	
	var q = document.getElementById('q_'+product_id).value;	
	var c = document.getElementById('colorId_'+product_id).value;
	var s = document.getElementById('s_'+product_id).value;
	
	if (s=="") 
	{
		alert(document.getElementById('ERROR_NO_CHOOSED_SIZE').value);
		return;
	}	

	if (typeof(infoBoxOffset)=='undefined') 
	{
		xOffset = 0;
	}
	else 
	{
		xOffset = infoBoxOffset;
	}
	
	var addToBasketInfoboxPOS = findPos(document.getElementById('ADD_TO_BASKET_BUTTON_'+product_id));

	if (isAvailableWithThisSizeAndColor(product_id,c,s)) 
	{
		var sizeName  = getSelectedValueText('s_'+product_id);
		var prodName  = document.getElementById('pn_'+product_id).value;
		
		if (document.getElementById('colorName_'+product_id)) 
		{
			var coloName  = document.getElementById('colorName_'+product_id).innerHTML;
		}
		
		xajax_addToBasket(product_id,q,s,c,prodName,sizeName,coloName,onlyCloseBasketInfobox); 
		
		document.getElementById('ADD_TO_BASKET_INFOBOX').style.position = 'absolute';
		document.getElementById('ADD_TO_BASKET_INFOBOX').style.left     = (addToBasketInfoboxPOS[0] + xOffset) + 'px';
		document.getElementById('ADD_TO_BASKET_INFOBOX').style.top      = addToBasketInfoboxPOS[1] + 'px';
		document.getElementById('ADD_TO_BASKET_INFOBOX').style.display  = 'block';
	}
}

function editInCreator(product_id) {
	
	if (typeof(product_id) == 'undefined' || product_id == 0)
	{
		return;
	}
	
	var creatorURL       = document.getElementById('creatorLink').value;
	
	var mod_product_id   = document.getElementById('modProductId_'+product_id).value;
	var color_id         = document.getElementById('colorId_'+product_id).value;
	var pSize            = document.getElementById('s_'+product_id).value;
	
	if (mod_product_id)
		creatorURL       	+= '&userdesign='+mod_product_id;
	else 	
		creatorURL       	+= '&partProdID='+product_id;
		
	creatorURL        	    += '&colorID='+color_id;

	var NA = false;

	if (pSize == '' || color_id == '' || typeof(ActiveColorSize[product_id][color_id][pSize]) == 'undefined' || typeof(ActiveColorSize[product_id][color_id][pSize]['available']) == 'undefined') 
	{
		alert(document.getElementById('ERROR_COLOR_AT_THIS_SIZE_NOT_AVAILABLE').value);
	} 
	else 
	{
		self.location.href = creatorURL;
	}
}

function toggleBasketDivEmptyNoempty(numOfProductsInBasket,noemptyId,emptyId) {
	
	if (numOfProductsInBasket==0) {
		
			if (document.getElementById(noemptyId)) document.getElementById(noemptyId).style.display = 'none';
			if (document.getElementById(emptyId))   document.getElementById(emptyId).style.display   = 'block';
			
	} else {
		
			if (document.getElementById(noemptyId)) document.getElementById(noemptyId).style.display = 'block';
			if (document.getElementById(emptyId))   document.getElementById(emptyId).style.display   = 'none';
			
	}
	
}


function getSelectedValueText(inputBoxId) {
	var tmp    = document.getElementById(inputBoxId);
	var retVal = '';
	for (i=0;i<tmp.length;i++) {
		if (tmp[i].value==tmp.value) retVal = tmp[i].text; 
	}
	return(retVal);
}

function closeBasketInfobox() {
		document.getElementById('ADD_TO_BASKET_INFOBOX').style.display = 'block';
		document.getElementById('ADD_TO_BASKET_INFOBOX').innerHTML     = '';
}

// scripts used in products view
function switchIt(tab_new) {
	var tab_old = document.getElementById('tab_old').value;
	
	document.getElementById('tab'+tab_old+'_switch').className  = '';
	document.getElementById('tab'+tab_old+'_content').className = 'inactive';
	
	document.getElementById('tab_old').value = tab_new;
	
	document.getElementById('tab'+tab_new+'_switch').className  = 'active';
	document.getElementById('tab'+tab_new+'_content').className = '';
}

// positioning library
var exclude=1;
var agt=navigator.userAgent.toLowerCase();
var win=0;var mac=0;var lin=1;
if(agt.indexOf('win')!=-1){win=1;lin=0;}
if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
var lnx=0;if(lin){lnx=1;}
var ice=0;
var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
var op5=0;var op6=0;var op7=0;
var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
	var thisKDE=agt;
	var splitKDE=thisKDE.split("konqueror/");
	var aKDE=splitKDE[1].split("; ");
	var KDEn=parseFloat(aKDE[0]);
	if(KDEn>=2.2){
		kde=1;
		ns6=1;
		exclude=0;
		}
	}
else if(agt.indexOf('webtv')!=-1){exclude=1;}
else if(typeof window.opera!="undefined"){
	exclude=0;
	if(/opera[\/ ][5]/.test(agt)){op5=1;}
	if(/opera[\/ ][6]/.test(agt)){op6=1;}
	if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
	}
else if(typeof document.all!="undefined"&&!kde){
	exclude=0;
	ie=1;
	if(typeof document.getElementById!="undefined"){
		ie5=1;
		if(agt.indexOf("msie 6")!=-1){
			ie6=1;
			dcm=document.compatMode;
			if(dcm!="BackCompat"){com=1;}
			}
		}
	else{ie4=1;}
	}
else if(typeof document.getElementById!="undefined"){
	exclude=0;
	if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
	else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
	else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
	if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
	}
else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
	exclude=0;
	ns4=1;
	if(typeof navigator.mimeTypes['*']=="undefined"){
		exclude=1;
		ns4=0;
		}
	}
if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

