function Microsoft()
{
  if (document.all&&document.styleSheets) return true;
  return false;
}

function Netscape()
{
  if (navigator.appName=="Netscape") return true;
  return false;
}

function Mozilla()
{
  if (document.getElementById&&!document.all) return true;
  return false;
}

function Opera()
{
  var userAgent=navigator.userAgent;
  if (userAgent.search("Opera")!=-1) return true;
  return false;
}

function FocusFormElement(formName,elementName)
{
  if (document.forms[formName]) eval("document.forms[formName]."+elementName+".focus()");
  else if (document.GetElementById) eval("document.GetElementById(formName)."+elementName+".focus();");
}

function GetFormObject(formName)
{
  if (document.forms[formName]) return document.forms[formName];
  else if (document.GetElementById) return document.GetElementById(formName);
}

function SubmitForm(name)
{
  if (document.forms[name]) document.forms[name].submit();
  else if (document.GetElementById) document.GetElementById(name).submit();
/*
  if (!Netscape()||Mozilla()) eval('document.forms["'+name+'"].submit();');
  else eval('document.'+name+'.submit();');
*/
}

function SetFormEnabled(formName,elementName,value)
{
  if (!Netscape()||Mozilla()) document.forms[formName].elements[elementName].disabled=value^1;
  else eval('document.'+formName+'.'+elementName+'.disabled='+value^1);
}

function SetFormValue(formName,elementName,value)
{
  if (!Netscape()||Mozilla()) document.forms[formName].elements[elementName].value=value;
  else eval('document.'+formName+'.'+elementName+'.value='+value);
}

function GetCheckboxValue(formName,elementName)
{
//alert(document.forms[formName].elements[elementName].checked);
  if (!Netscape()||Mozilla()) return document.forms[formName].elements[elementName].checked;
  else return eval('document.'+formName+'.'+elementName+'.checked');

}

function AskDelete(dropId,link)
{
  var txt="Sind Sie sicher den Datensatz mit der Id '"+dropId+"' zu entfernen?";
  if (confirm(txt))
  {
    location.href=link;
  }
}

function ChangeLayerContent(name,content)
{
  if (!Netscape())
  {
    document.all[name].innerHTML=content;
  }
  else
  {
    if (!Mozilla())
    {
      document.layers[name].document.open();
      document.layers[name].document.write(content);
      document.layers[name].document.close();
    }
    else document.getElementById(name).innerHTML=content;
  }
}

function Round(wert,stellen)
{
  addstr=Math.pow(10,stellen).toString().substring(1)
  wert=Math.round(wert*Math.pow(10,stellen))/Math.pow(10,stellen)

  if(wert.toString().indexOf(".")==-1)
  wert=wert+"."+addstr;
  else
  {
    if(wert.toString().substring(wert.toString().indexOf(".")+1).length<addstr.length)
    wert=wert.toString()+addstr.substr(0,(addstr.length-wert.toString().substring(wert.toString().indexOf(".")+1).length))
  }
  return wert;
}

var productDetailPicArray;
function ChangeProductDetailPic(dropDownNr)
{
  var formObj;
  if (document.forms["addToBasketForm"]) formObj=document.forms["addToBasketForm"];
  else if (document.GetElementById("addToBasketForm")) formObj=document.GetElementById("addToBasketForm");
  else alert("form not found");

//  var pos=document.forms['addToBasketForm'].elements['varPosArray['+dropDownNr+']'].value;
  var pos=formObj.elements['varPosArray['+dropDownNr+']'].value;

  var htmlStr;
  if (productDetailPicArray[dropDownNr][pos-1]) htmlStr="<img src='"+productDetailPicArray[dropDownNr][pos-1]+"'>";
  else return;
  ChangeLayerContent("productPic",htmlStr);
}

var productDetailGraduatedPiecesArray=Array();
var productDetailGraduatedPriceArray=Array();
function ChangeProduktDetailPrice(obj,maxGraduatedSteps,maxPiecesInFormElement)
{
  if (maxPiecesInFormElement>0)
  {
    if (obj.form.addBasketPieces.value>maxPiecesInFormElement)
    {
      obj.form.addBasketPieces.value=maxPiecesInFormElement;
    }
  }

  if (maxGraduatedSteps<=1) return;
  var value=obj.value;
  var htmlStr;


  for (i=0;i<maxGraduatedSteps;i++)
  {
    if (value<productDetailGraduatedPiecesArray[i])
    {
      if (i==0) htmlStr=productDetailGraduatedPriceArray[0];
      else htmlStr=productDetailGraduatedPriceArray[i-1];
      break;
    }
    else if (i>=maxGraduatedSteps-1)
    {
      htmlStr=productDetailGraduatedPriceArray[i];
    }
  }

  ChangeLayerContent("productPrice",htmlStr);
}

function IsEmailValid(email)
{
  if (email.search(/.+@+./)==-1) return false;
  if (email.search(/.+[.]+./)==-1) return false;
  if (email.length<5) return false;
  return true;
}

var globalMousePosX=0;
var globalMousePosY=0;

function SaveMousePos(e)
{
  if(Netscape()||Mozilla())
  {
    globalMousePosX=e.pageX;
    globalMousePosY=e.pageY;
  }
  else if(Microsoft()||Opera())
  {
    globalMousePosX=document.body.scrollLeft+event.clientX;
    globalMousePosY=document.body.scrollTop+event.clientY;
  }
}

function IsIE6_And_SP_Installed()
{
  if (typeof (navigator.appMinorVersion) != "undefined")
  {
    if (navigator.appMinorVersion.substr(1,2)=='SP') return true;
  }
  return false;
}


function ChangeStock(variationGroupsCount,showProductAvailableFrom,availableInProductsTable,tmpAvail,newWareHouseStockArray,variationCnt)
{
  var unlimited="unbegrenzt";
  var posArray=new Array();
  var productAvailableUnlimited=-2147283648;


  for (var i=0;i<variationGroupsCount;i++)
  {
    var tmpSelectionName="varPosArray["+i+"]";
    var varGroupId="varGroupIdArray["+i+"]";
    posArray[document.getElementById(varGroupId).value]=document.getElementById(tmpSelectionName).value;
  }


  for (var i=0;i<variationCnt;i++)
  {
    var variationStr=newWareHouseStockArray[i];

    variationStrArray=variationStr.split(",");

    var cmpStr1=variationStr.replace(variationStrArray[4],"");
    var cmpStr1=cmpStr1.replace("0,","");

    var cmpStr2="";
    for (var j=0;j<variationGroupsCount;j++)
    {
      var tmpSelectionName="varPosArray["+j+"]";
      var varGroupId="varGroupIdArray["+j+"]";
      cmpStr2=cmpStr2+posArray[document.getElementById(varGroupId).value];
      cmpStr2=cmpStr2+",";
    }

    do
    {
      cmpStr2=cmpStr2+"0,";
    } while (cmpStr1.length>cmpStr2.length);

  	//alert(cmpStr1+" ist gleich "+cmpStr2);

    if (cmpStr1==cmpStr2)
    {
      var ergStr=variationStrArray[4];
      ergStr=ergStr.replace("0:","");

      var xPos=ergStr.search(/-.+/);
      var warehouseId=ergStr.slice(0,xPos);
      var stockStr=ReplaceStr("-","",ergStr);
      var tmpStock=ReplaceStr(warehouseId,"",stockStr);
    }
  }

  for (var i=0;i<15;i++)
  {
  	if (typeof(posArray[i+1])=="undefined") posArray[i+1]=0;
  }

  //document.getElementById("warehouseId").value=wareHouseRecIdArray[posArray[5]][posArray[4]][posArray[3]][posArray[2]][posArray[1]];
  document.getElementById("warehouseId").value=warehouseId;

  if (showProductAvailableFrom>1)
  {
  	//stock=warehouseStockArray[posArray[5]][posArray[4]][posArray[3]][posArray[2]][posArray[1]];
  	stock=tmpStock;
  	if (stock==0) stock=tmpAvail;
    if (!stock) stock=tmpAvail;
  }
  else stock=availableInProductsTable;

	if (showProductAvailableFrom==1) stock=availableInProductsTable;
 	else if (showProductAvailableFrom==3)
 	{
 		if (availableInProductsTable==productAvailableUnlimited) stock=productAvailableUnlimited;
 		//else stock+=availableInProductsTable;
 	}

  if (stock=="") stock=0;

  if (isNaN(stock)) stock=0;

  if (stock==productAvailableUnlimited) stock=unlimited;
  document.getElementById("warehouseStock").innerHTML=stock;
  if (stock>0) document.getElementById("warehouseStockPic").innerHTML="<img border=0 src='../templates/default/default/images/icons/available.gif'>";
  else document.getElementById("warehouseStockPic").innerHTML="<img border=0 src='../templates/default/default/images/icons/not_available.gif'>";
}

function ReplaceStr(t, u, s)
{
  /*
  **  Replace a token in a string
  **    t  token to be found and removed
  **    u  token to be inserted
  **    s  string to be processed
  **  returns new String
  */

  i = s.indexOf(t);

  var leftLen=i;
  var middleLen=t.length;
  var rightLen=s.length-i-t.length;

  var leftPart=s.substr(0,leftLen);
  var middlePart=u;
  var rightPart=s.substr(leftLen+middleLen,rightLen);

  r = "";
  if (i == -1) return s;
  r = leftPart + middlePart + rightPart;
  return r;
}

function GetWindowSizeX()
{
 if (window.innerWidth) return window.innerWidth;
 else if (document.body && document.body.offsetWidth) return document.body.offsetWidth;
}

function GetWindowSizeY()
{
 if (window.innerHeight) return window.innerHeight;
 else if (document.body && document.body.offsetHeight) return document.body.offsetHeight;
}

function GetBodySizeX()
{
  return document.body.clientWidth;
}

function GetBodySizeY()
{
  return document.body.clientHeight;
}

function Dec2Hex(v)
{
  v = v.toString(16);
  return v;
//  for(; v.length < 6; v = '0' + v);
//  for(; v.length < 1; v = '0' + v);
//  return v;
}

function chr(v)
{
  return unescape("%"+Dec2Hex(v));
}


function scrollDown(height)
{
  var maxSteps = 4;
  var curStep = 0;

  curStep = curStep + 1;
  if(curStep > maxSteps) curStep = 0;
  step = height * curStep;
  dest = height * curStep;
  start = height * (curStep - 1)
  for(step = start + 25; step <= dest; step++)
  {
    parent.scroll(0, step);
  }
}

function scrollUp(height)
{
  var maxSteps = 4;
  var curStep = 0;

  curStep = curStep -1;
  if(curStep < 0) curStep = maxSteps;
  step = height * curStep;
  dest = height * curStep;
  start = height * (curStep + 1)
  for(step = start; step >= dest; step--)
  {
    parent.scroll(0, step);
  }
}

