
// Top Menu (Drop Down).

var menuItems = new Array();
var dropDivs = new Array();
//var dropItems = new Array();

var availWidth = 0;

function actLink(linkID, linkClass) {
    document.getElementById(linkID).className = linkClass + ' active';
}

function norLink(linkID, linkClass) {
    document.getElementById(linkID).className = linkClass;
}

function showDiv(divID) {
    document.getElementById(divID).style.visibility = 'visible';
    document.getElementById(divID).style.display = 'block';
}

function hideDiv(divID) {
    document.getElementById(divID).style.visibility = 'hidden';
    document.getElementById(divID).style.display = 'none';
}

function getOffset(what, offsettype) {
    var totaloffset = (offsettype == 'left') ? what.offsetLeft : what.offsetTop;
    var parentEl = what.offsetParent;
    while(parentEl != null) {
        totaloffset = (offsettype == 'left') ? totaloffset + parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl = parentEl.offsetParent;
    }
    return totaloffset;
}

function initMenu(menu) {

    var objContainer = document.getElementById('container');
    var objMenu = document.getElementById(menu);
    
    if(objContainer && objMenu) {
    
        availWidth = document.getElementById('container').offsetWidth + getOffset(document.getElementById('container'), 'left');
        menuItems = document.getElementById(menu).getElementsByTagName("a");
        for(var i = 0; i < menuItems.length; i++) {
            if(menuItems[i].getAttribute("rel")) {
                menuItems[i].divID = menuItems[i].getAttribute("rel");
                menuItems[i].linkID = menuItems[i].getAttribute("id");
                menuItems[i].saveClass = menuItems[i].className;
                menuItems[i].onmouseover = function() { showDiv(this.divID); };
                menuItems[i].onmouseout = function() { hideDiv(this.divID); };
                dropDivs[i] = document.getElementById(menuItems[i].getAttribute("rel"));
                if(dropDivs[i]) {
                    dropDivs[i].divID = menuItems[i].getAttribute("rel");
                    dropDivs[i].linkID = menuItems[i].linkID;
                    dropDivs[i].saveClass = menuItems[i].saveClass;
                    dropDivs[i].onmouseover = function() { showDiv(this.divID); actLink(this.linkID, this.saveClass); };
                    dropDivs[i].onmouseout = function() { hideDiv(this.divID); norLink(this.linkID, this.saveClass); };
                    dropDivs[i].style.left = getOffset(menuItems[i], 'left') + 'px';
                    dropDivs[i].style.top = getOffset(menuItems[i], 'top') + menuItems[i].offsetHeight + 'px';
                    if(dropDivs[i].offsetWidth < menuItems[i].offsetWidth) dropDivs[i].style.width = menuItems[i].offsetWidth + 'px';
                    if(getOffset(dropDivs[i], 'left') + dropDivs[i].offsetWidth > availWidth) dropDivs[i].style.left = (availWidth - dropDivs[i].offsetWidth - 13) + 'px';
                    /*
                    dropItems[i] = document.getElementById(menuItems[i].getAttribute("rel")).getElementsByTagName("a");
                    for(var j = 0; j < dropItems[i].length; j++) {
                        if(dropItems[i][j] && dropItems[i][j].getAttribute("rel")) {
                            dropItems[i][j].divID = dropItems[i][j].getAttribute("rel");
                            dropItems[i][j].linkID = dropItems[i][j].getAttribute("id");
                            dropItems[i][j].saveClass = dropItems[i][j].className;
                            dropItems[i][j].linkPID = menuItems[i].linkID;
                            dropItems[i][j].savePClass = menuItems[i].saveClass;
                            dropItems[i][j].onmouseover = function() { showDiv(this.divID); actLink(this.linkPID, this.savePClass); };
                            dropItems[i][j].onmouseout = function() { hideDiv(this.divID); norLink(this.linkPID, this.savePClass); };
                            dropDivs[i][j] = document.getElementById(dropItems[i][j].getAttribute("rel"));
                            if(dropDivs[i][j]) {
                                dropDivs[i][j].divID = dropItems[i][j].getAttribute("rel");
                                dropDivs[i][j].divPID = menuItems[i].getAttribute("rel");
                                dropDivs[i][j].linkPPID = menuItems[i].linkID;
                                dropDivs[i][j].savePPClass = menuItems[i].saveClass;
                                dropDivs[i][j].linkPID = dropItems[i][j].linkID;
                                dropDivs[i][j].savePClass = dropItems[i][j].saveClass;
                                dropDivs[i][j].onmouseover = function() { showDiv(this.divID); showDiv(this.divPID); actLink(this.linkPID, this.savePClass); actLink(this.linkPPID, this.savePPClass); };
                                dropDivs[i][j].onmouseout = function() { hideDiv(this.divID); hideDiv(this.divPID); norLink(this.linkPID, this.savePClass); norLink(this.linkPPID, this.savePPClass); };
                                dropDivs[i][j].style.left = getOffset(dropItems[i][j], 'left') + dropItems[i][j].offsetWidth + 'px';
                                dropDivs[i][j].style.top = getOffset(dropItems[i][j], 'top') + 'px';
                                if(getOffset(dropDivs[i][j], 'left') + dropDivs[i][j].offsetWidth > availWidth) dropDivs[i][j].style.left = (getOffset(dropDivs[i], 'left') - dropDivs[i][j].offsetWidth + 6) + 'px';
                                dropDivs[i][j].style.display = 'none';
                            }
                        }
                    }
                    */
                    dropDivs[i].style.display = 'none';
                }
            }
        }
        
    }
}








function Trim(str){
str=str.replace(/(^[\s\u3000]*)|([\s\u3000]*$)/g, "");
return str;
}

function IsPositiveFloat(_str){
var tmp_str = Trim(_str);
var pattern = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/; 
return pattern.test(tmp_str); 
}

function isFloat(val) {
    if(!val || (typeof val != "string" || val.constructor != String)) {
      return(false);
    }
    var isNumber = !isNaN(new Number(val));
    if(isNumber) {
      if(val.indexOf('.') != -1) {
        return(true);
      } else {
        return(false);
      }
    } else {
      return(false);
    }
  }
  function isPositiveInteger(val){
      if(val==null){return false;}
      if (val.length==0){return false;}
      for (var i = 0; i < val.length; i++) {
            var ch = val.charAt(i)
            if (ch < "0" || ch > "9") {
            return false
            }
      }
      return true;
}

function FIND(item) {
  if (document.all) return(document.all[item]);
  if (document.getElementById) return(document.getElementById(item));
  return(false);
}

function showselect (id) {
  FIND(id).className='flagsbox';
} 
function hideselect (id) {
  FIND(id).className='';
}
function BarterShowHide(id) {
  if(FIND(id).style.display=='block') {
    FIND(id).style.display='none';
  }
  else {
    FIND(id).style.display='block';
  }
}

var st1;
var st2;
var thetop=-1;
var flagdivvisible=0;
var oMyDiv;
var boxMyDiv;

function writeContentDoc(Doc, TargetId) {
   if(!oMyDiv) oMyDiv=document.createElement("div");
   oMyDiv.innerHTML=Doc;
   var oInsertTarget=document.getElementById(TargetId);
   oInsertTarget.innerHTML=oMyDiv.innerHTML;
}

function writeContentBox(Doc, TargetId) {
   if(!boxMyDiv) boxMyDiv=document.createElement("div");
   boxMyDiv.innerHTML=Doc;
   var oInsertTarget=document.getElementById(TargetId);
   oInsertTarget.innerHTML=boxMyDiv.innerHTML;
}

function writeContent(url, DocTargetId, writeFunc) {
   makeHttpRequest("GET",WebServerScriptUrlSSL+url+'&rand='+Math.floor(Math.random()*1000000),'', false, writeFunc, DocTargetId);
}

function getop(){
  if (navigator.appName == "Netscape") {
    return window.pageYOffset;
  }
  else {
    return (document.body.scrollTop)?document.body.scrollTop:document.documentElement.scrollTop;
  }
}

function refreshlocation(){
    if(flagdivvisible && thetop!=getop()){
      centerDiv();
    }
    if(!flagdivvisible) {
      if(st2) clearTimeout(st2);
      return;
    }
    st2=setTimeout("refreshlocation()",10);
}

function centerDiv(){
  var suffix="";
  if(FIND('divbasketpopup').style.left.indexOf('px')>=0){
    suffix="px";
  }
  FIND('divbasketpopup').style.left=Math.floor((screen.availWidth-280)/2)+suffix;
  FIND('divbasketpopup').style.top=Math.floor(screen.availHeight/2-148)+getop()+suffix;
  thetop=getop();
}

function appendProductURL(formular){
  var urlstr = "";
  var productParams = new Array('ChangeObjectID','ProductID','Quantity');
  for(var i=0;i<productParams.length;i++){
    urlstr += "&"+productParams[i]+"="+eval("formular."+productParams[i]+".value");  
  }
  return urlstr;
}

function divpopup(url,formular){
  url+=appendProductURL(formular);
  writeContent(url,'divbasketpopup','writeContentDoc');  
  if(st1){clearTimeout(st1)};
  var basketstatus = getBasketPopupStatus(); 
  if(!basketstatus || basketstatus == "false"){
    centerDiv();
    FIND('divbasketpopup').style.visibility='visible';
    flagdivvisible=1;
  }
    st1=setTimeout("flagdivvisible=0;FIND('divbasketpopup').style.visibility='hidden'",7000);
    refreshlocation();
}

function closedivpopup(){
  FIND('divbasketpopup').style.visibility='hidden';
  flagdivvisible=0;    
}



function searchByPrices(prices) {  
    var list_price = prices.split("-");
    document.getElementById('PriceFrom').value = list_price[0];
    document.getElementById('PriceTo').value = list_price[1];
    document.getElementById('searchPrice').submit();
}


// red input checkbox

var checkboxHeight = "25";


/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } </style>');

var Custom = {
	init: function() {
	  var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].type == "checkbox" && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight) + "px";
						span[a].style.backgroundPosition = position;
					} 
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} 
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
			} 
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} 
		}
	}	
}

function initAll() {
  if(window.initMenu) initMenu('tabmenu');
  if(window.initQuickBuy) initQuickBuy();
  if(Custom.init) Custom.init();
}

window.onload = initAll;



function dp(discount,price){
  if(typeof(discount)=="undefined") discount = 0;
  var price = (1-discount/100)*parseFloat(price);
  return formatPrice(price);
}

function displayMeldenummer(cga){
  if(!(cga=="KG0006" || cga=="KG0013" || cga=="")){
    return true;
  }
}

function a2s(a){
  var r = "";
  var i = 0;
  while(i < a.length) {
    r +=  String.fromCharCode(a.substring(i,i+2));
    i+=2;
  }
  return r;
}
function s2a(s){
  var r = "";
  var i = 0;
  while (i< s.length) {
    r += s.charCodeAt(i++);
  }
  return r;
}
function avValue(cga,ObjectID){
  return parseFloat(a2s(eval("av_"+ObjectID+"["+s2a(cga)+"]"))); 
}

function priceDisplay(cga,ListPrice,ObjectID){
  if(cga!="" && cga!="KG0006" && avValue(cga,ObjectID)!=0){
    return ListPrice;
  } else {
    return "<strong>"+ListPrice+"</strong>";
  }
}
