function ajax(url, idForChange)
{
  self.idForChange = idForChange;

	if (window.XMLHttpRequest)
	  self.xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
	  self.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

 	self.xmlhttp.onreadystatechange=function ()
 	{
 	  if (self.xmlhttp.readyState == 4)
 	    self.changeContent();
 	    
 	}
  self.xmlhttp.open("GET",url,true);
  self.xmlhttp.send(null);


  changeContent = function ()
  {
    elm = document.getElementById(self.idForChange);
    if (!elm) return false;
    elm.innerHTML = self.xmlhttp.responseText; 
  }
}




function pickVariant(iGoodsId, iVariantGroupId)
{
  displayBackgroundOpacity();
  elm = openVariantBox();
  
  ajaxObj = new ajax('/popupVariant.php?goods_id='+iGoodsId+'&variant_type_id='+iVariantGroupId,'dial');
  
}


function variantSelected(iVariantGroupId, iVariantValueId, iVariantPrice, sVariantImgHref, sCatalogNumber)
{
  
  
  inputVariantId = document.getElementById('input_variant_group_'+iVariantGroupId+'_value');
  inputVariantPrice = document.getElementById('input_variant_group_'+iVariantGroupId+'_price');
  inputVariantCatalogNumber = document.getElementById('input_variant_group_'+iVariantGroupId+'_catalog_number');
  inputImage = document.getElementById('input_variant_group_'+iVariantGroupId+'_image');
  
  
  if (inputVariantId)
  {
    inputVariantId.value = iVariantValueId;
    inputVariantPrice.value = iVariantPrice;
    inputVariantCatalogNumber.value = sCatalogNumber;
    inputImage.src = sVariantImagePath + sVariantImgHref;
  }
  
  // zrusit text "nutno vybrat"
  elm = document.getElementById('variant_require_text_'+iVariantGroupId);
  if (elm) elm.style.display="none";
  
  closeVariantBox();
  recountPrice();
}



function recountPrice()
{
    catalog_number = sGoodsCatalogNumber + '.';
    
    iTotalAddPriceVat = 0;
    re = /^input_variant_group_([0-9]*)_price$/i;
    inputs = document.getElementsByTagName('input');
    for (inp in inputs)
    {
      tmpInp = inputs[inp];
      if ((tmpInp.id) && (tmpInp.id.match(re)))
      {
        s = tmpInp.id.match(re);
        elm = document.getElementById('addPrice-JS-'+s[1]);
        if (elm)
        {
          if (tmpInp.value == "")
          {
            elm.innerHTML = '???';
          }
          else
          {
            price = parseFloat(tmpInp.value);
            elm.innerHTML = Math.round(price)+' Kč';
            
            iTotalAddPriceVat +=  (parseFloat(tmpInp.value) + 0);
            //alert(parseFloat(tmpInp.value)+0);
            // catalog number
            elm1 = document.getElementById('input_variant_group_'+s[1]+'_catalog_number');
            if ((elm1) && elm1.value != "")
              catalog_number += elm1.value + '.';
          }
        }
      }
    }
    
    catalog_number = catalog_number.substr(0, (catalog_number.length)-1);
    elm = document.getElementById('JScode');
    elm.innerHTML = catalog_number;
    
    //alert(iTotalAddPriceVat +'+'+iGoodsPriceVat);
    iTotalPriceVat = Math.round(iTotalAddPriceVat + iGoodsPriceVat);
    
    iTotalPrice = Math.round(iTotalPriceVat / ((iGoodsVat/100)+1));
    
    elm = document.getElementById('mainPrice-JS');
    elm.innerHTML = iTotalPrice; 
    elm = document.getElementById('mainPriceVat-JS');
    elm.innerHTML = iTotalPriceVat; 
}




function checkForVariantSelected()
{
  tempDuty = aDutyVariantGroup;
  
  for(id in aDutyVariantGroup)
  {
    elm = document.getElementById('input_variant_group_'+aDutyVariantGroup[id]+'_value');
    if (!elm) 
    {
      alert('Chyba skriptu. DOM element input_variant_group_'+id+'_value nebyl nalezen. Zkuste zopakovat akci znovu, nebo kontaktujte správce webu');
      return false;
    }
    if (elm.value == "")
    {
      alert('Pro objednání je nutno vybrat povinné varianty výrobku');
      return false;
    }
  }
  
  return true;
  
  
}





function openVariantBox(width, height)
{
  if (!width) width=360;
  if (!height) height=300;
  
  div = document.createElement('div');
  div.setAttribute('id','dial');
  
  img1 = document.createElement('img');
  img1.src='/images/spinner.gif';
  
  img1.style.position='relative';
  img1.style.top='150px';
  div.style.textAlign="center";
  
  div.appendChild(img1);
  
  document.body.appendChild(div);
  return div;
}


function displayBackgroundOpacity()
{
  div = document.createElement('div');
  div.setAttribute('id','background-opacity');
  div.onclick=function() {closeVariantBox()};
  
  height = document.body.clientHeight;
  height2 = document.documentElement.clientHeight;
  
  if (height<height2) height = height2;
  
  ver = navigator.appVersion;
  
  re = /(MSIE 6.0)/i;
  
  if (!ver.match(re)) div.style.height = height+'px';
  
  document.body.appendChild(div);
  
  opacity('background-opacity',0, 60, 500);
}


function hideBackgroundOpacity()
{
  div = document.getElementById('background-opacity');
  document.body.removeChild(div);
}


function closeVariantBox()
{
  hideBackgroundOpacity();
  dial1=document.getElementById('dial');
  document.body.removeChild(dial1);
  
}


function opacity(id, opacStart, opacEnd, millisec) 
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}


function changeOpac(opacity, id) 
{
    //return true;
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 



