var ie4          = false;
var ie5          = false;
var ns4          = false;
var ns6          = false;
var mac          = false;
var running      = false;
var timerId      = null;
var delay        = 500;
var currentMenu  = null;
var previousMenu = null;
var currentSub   = null;
var previousSub  = null;
var pageId       = null;
var loaded       = false;
var homeloaded   = false;
var testing      = false;
var allopened    = false;
var dir_images   = 'images/';

if(document.layers){origWidth=innerWidth;origHeight=innerHeight;onresize=function(){if(innerWidth!=origWidth||innerHeight!=origHeight)location.reload()}}else{onresize=function(){window.location.reload();}}
function SetFocus(fieldname,formname){if (formname == ''){ formname = 'form'; }if (eval("document."+formname+".elements[fieldname].value == ''")){ eval("document."+formname+".elements[fieldname].focus();"); }}
function Popup(title,filename,w,h,scroll,resizable){if (title == ''){ title = 'popup'; }if (scroll == 'scroll'){ scroll = 'yes'; }else{ scroll = 'no'; }if (resizable == 'lock'){ resizable = 'no'; }else{ resizable = 'yes'; }winOptions = eval("'width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");window.open(filename,title,winOptions);return;}
function safemail(name,domain,display,this_class) {displayed = (typeof(display) == "undefined") ? name+"@"+domain : display;displayed_class = (typeof(this_class) == "undefined") ? '' : ' class="'+this_class+'"';document.write('<a href=mailto:'+name+'@'+domain+displayed_class+'>'+displayed+'</a>');}
function StartTimer(event){running = true; timerId = setTimeout(event,delay);}
function StopTimer(){running = false; clearTimeout(timerId);}
function SetElementPosition(id,top,left){
  if (testing) { alert("SetElementPosition\nid = "+id+"\ntop = "+top+"\nleft = "+left); }
  if (ns4) {
    eval('document.'+id).top = top;
    eval('document.'+id).left = left;
  } else if (ns6) {
    document.getElementById(id).style.top = top+'px';
    document.getElementById(id).style.left = left+'px';
  } else {
    eval(id).style.top  = top;
    eval(id).style.left = left;
  }
}
function SetVisibility(id,flag) {
  if (ns4) {
    var str = (flag) ? 'show' : 'hide';
    eval("document."+id).visibility = str;
  } else if (ns6) {
    var str = (flag) ? 'visible' : 'hidden';
    document.getElementById(id).style.visibility = str;
  } else {
    var str = (flag) ? 'visible' : 'hidden';
    eval("document.all."+id).style.visibility = str;
  }
}
function getImageTop(imgID) {
  if (ns4) return eval(imgID).y;
  else return getRealTop(imgID);
}
function getImageLeft(imgID) {
  if (ns4) return eval(imgID).x
  else return getRealLeft(imgID);
}
function getRealTop(imgElem) {
  yPos = eval(imgElem).offsetTop;
  tempEl = eval(imgElem).offsetParent;
  while (tempEl != null) {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
  return yPos;
}
function getRealLeft(imgObj) {
	xPos = eval(imgObj).offsetLeft;
	tempObj = eval(imgObj).offsetParent;
  while (tempObj != null){
    xPos   += tempObj.offsetLeft;
    tempObj = tempObj.offsetParent;
  }
	return xPos;
}
function getImageWidth(id) {
  if (ie4){
    return eval('document.all.'+id).style.pixelWidth;
  } else if (ns4) {
    return eval('document.'+id).width;
  } else {
    return document.getElementById(id).offsetWidth;
  }
}
function getElementWidth(id) {
  if      (ie4){ return eval('document.all.'+id).offsetWidth; }
  else if (ns4){ return eval('document.'+id).clip.width; }
  else         { return document.getElementById(id).offsetWidth; }
}
function getElementHeight(id) {
  if      (ie4){ return eval('document.all.'+id).offsetHeight; }
  else if (ns4){ return eval('document.'+id).clip.height; }
  else         { return document.getElementById(id).offsetHeight; }
}
function layerWrite(id,text) {
  if (ns4) {
    var lyr = document.layers[id].document;
    lyr.open();
    lyr.write(text);
    lyr.close();
  } else if (ns6) {
    rng = document.createRange();
    el = document.getElementById(id);
    rng.setStartBefore(el);
    htmlFrag = rng.createContextualFragment(text);
    while (el.hasChildNodes()) el.removeChild(el.lastChild);
    el.appendChild(htmlFrag);
  } else if (ie4 || ie5) {
    document.all[id].innerHTML = text;
  }
}
//////////////////////////////////////////
//////////////////////////////////////////
function Init(id,load_shadow) {
  SetBrowser();
  if (id == 'home') { HomeLoadImages(); }
  LoadImages();
  if (ie5 || ns6) { LoadMenus(); }
  pageId = id;
  loaded = true;
}
function SetBrowser() {
  agent = navigator.userAgent.toLowerCase();
  mac   = (agent.indexOf("mac")!=-1);
  ie4   = (document.all && !document.getElementById) ? true : false;
  ie5   = (document.all && document.getElementById)  ? true : false;
  ns4   = (document.layers)                          ? true : false;
  ns6   = (document.getElementById && !document.all) ? true : false;
  if (testing){ alert("agent: "+agent+"\nmac: "+mac+"\nie4: "+ie4+"\nie5: "+ie5+"\nns4: "+ns4+"\nns6: "+ns6+"\n"); }
}
//custom function for k2
function HomeLoadImages() {
  //preload menu bgs
  SupplierOff     = new Image();
  SupplierOff.src = dir_images+"hm_img_supplier.jpg";
  SupplierOn      = new Image();
  SupplierOn.src  = dir_images+"hm_img_supplier_b.jpg";
  
  BuyerOff     = new Image();
  BuyerOff.src = dir_images+"hm_img_buyer.jpg";
  BuyerOn      = new Image();
  BuyerOn.src  = dir_images+"hm_img_buyer_b.jpg";
  homeloaded = true;
}
function LoadImages() {
  //preload menu bgs
  MenuBgOff     = new Image();
  MenuBgOff.src = dir_images+"subnav_off.gif";
  MenuBgOn      = new Image();
  MenuBgOn.src  = dir_images+"subnav_on.gif";
  
	ImagePlus      = new Image();
  ImagePlus.src  = dir_images+"icon_plus.gif";
  ImageMinus     = new Image();
  ImageMinus.src = dir_images+"icon_minus.gif";
}
function LoadMenus() {
  //menus
  for (var i=0; i<7; i++) {
    imgId    = 'menu'+i;
    menuId   = 'menu'+i+'Div';
    shadowId = 'menubg'+i+'Div';
    
    //get anchor image
    if (ns6){ anchorObj = eval('document.getElementById("'+imgId+'")'); }
    else    { anchorObj = eval('document.images["'+imgId+'"]'); }
    
    //set position
    t = getImageTop(anchorObj);
    l = getImageLeft(anchorObj);
    w = getImageWidth(imgId);
    menuTop  = t + 7;
    menuLeft = (i == 5) ? l - 11 : l;
    SetElementPosition(menuId,menuTop,menuLeft);
    
    //shadow
    if (ie5 || ns6) {
      menuWidth  = getElementWidth(menuId);
      menuHeight = getElementHeight(menuId);
      if (menuWidth > 1 && menuHeight > 1) {
        var tempstring,writestring;
        tempstring = '<img src="'+dir_images+'shadow.gif" width="'+menuWidth+'" height="'+menuHeight+'" alt="" border="0">';
        writestring =  (!(mac && (ie4 || ie5))) ? '<table cellpadding="0" cellspacing="0" border="0"><tr><td class="transparent">'+tempstring+'</td></tr></table>' : tempstring;
        layerWrite(shadowId,writestring);
        shadowTop  = menuTop + 4;
        shadowLeft = menuLeft + 4; 
        SetElementPosition(shadowId,shadowTop,shadowLeft);
      }
    }
  }
}
//custom function for k2
//imgId - id of image that marks where the sidenav starts
//menuId - id of div for sidenav menu for width/height
//direction - indicates direction of shadow, also references one of two shadow divs
function LoadShadow(imgId,menuId,direction) {
  //shadow
  if (ie5 || ns6) {
    //set shadowId based on type of shadow
    shadowId = 'sidemenu'+direction+'bgDiv';
    
    //get anchor image
    if (ns6){ anchorObj = eval('document.getElementById("'+imgId+'")'); }
    else    { anchorObj = eval('document.images["'+imgId+'"]'); }
    
    //set position
    t = getImageTop(anchorObj);
    l = getImageLeft(anchorObj);  
  
    menuWidth  = getElementWidth(menuId);
    menuHeight = getElementHeight(menuId);
    if (menuWidth > 1 && menuHeight > 1) {
      var tempstring,writestring;
      //shadow should only be 4 wide for vertical, 4 high for horizontal
      thisWidth   = (direction == 'vertical') ? 4 : menuWidth;
      thisHeight  = (direction == 'horizontal') ? 4 : menuHeight - 4;
      
      tempstring = '<img src="'+dir_images+'shadow.gif" width="'+thisWidth+'" height="'+thisHeight+'" alt="" border="0">';
      writestring =  (!(mac && (ie4 || ie5))) ? '<table cellpadding="0" cellspacing="0" border="0"><tr><td class="transparent">'+tempstring+'</td></tr></table>' : tempstring;
      layerWrite(shadowId,writestring);
      
      //shadow positioned just around menu
      shadowTop  = t + ((direction == 'horizontal') ? menuHeight : 4);      
      shadowLeft = l + ((direction == 'vertical') ? menuWidth : 4); 
      
      SetElementPosition(shadowId,shadowTop,shadowLeft);
      document.getElementById(shadowId).style.visibility = "visible";
    }
  }
}
function ShowMenu(id) {
  if (running) { StopTimer(); }
  currentMenu = id;
  if (currentMenu != previousMenu && previousMenu != null) { HideMenu(previousMenu); }
  if (previousSub != null) { HideSubMenu(previousSub); }
  if (ie4) {
    //do nothing
  } else if (ns4) {
    //do nothing
  } else {
    eval('document.getElementById("menu'+id+'Div").style.visibility = "visible"');
    eval('document.getElementById("menubg'+id+'Div").style.visibility = "visible"');
  }
  previousMenu = currentMenu;
}
function HideMenu(id) {
  if (id == null) { return; }
  if (ie4) {
    //do nothing
  } else if (ns4) {
    //do nothing
  } else {
    eval('document.getElementById("menu'+id+'Div").style.visibility = "hidden"');
    eval('document.getElementById("menubg'+id+'Div").style.visibility = "hidden"');
    if (previousSub != null) { HideSubMenu(previousSub); }
  }
}
function HomeShow(id,graphic) {
  if (ie4) {
    eval('document.images["home'+id+'"].src = '+graphic+'On.src');
  } else if (ns4) {
    eval('document.images["home'+id+'"].src = '+graphic+'On.src');
  } else {
    eval('document.getElementById("home'+id+'").src = '+graphic+'On.src');
  }
}
function HomeHide(id,graphic) {
  if (id == null) { return; }
  if (ie4) {
    if (id != pageId){ eval('document.images["home'+id+'"].src = '+graphic+'Off.src'); }
  } else if (ns4) {
    if (id != pageId){ eval('document.images["home'+id+'"].src = '+graphic+'Off.src'); }
  } else {
    if (id != pageId){ eval('document.getElementById("home'+id+'").src = '+graphic+'Off.src'); }
  }
}
function ShowAnswer(id) {
  var answer = document.getElementById("a"+id);
  if (answer.style.display == "none") {
    answer.style.display = "block";
    document.getElementById("plusminus"+id).src = ImageMinus.src;
    //document.getElementById("countview").src = "faq_countview.php?faq_id="+id;
  } else {
    answer.style.display = "none";
    document.getElementById("plusminus"+id).src = ImagePlus.src;
  }
}
function OpenAll(id) {
  //open all answers
  divColl = document.getElementsByTagName("DIV");
  for (i=0; i<divColl.length; i++) {
    if (divColl[i].className == "answer") {
      divColl[i].style.display = (allopened) ? "none" : "block";
    }
  }
  
  //change "plus" to "minus"
  regex = new RegExp("plusminus(\\d){1,2}");
  imgColl = document.getElementsByTagName("IMG");
  for (i=0; i<imgColl.length; i++) {
    if (regex.test(imgColl[i].id)) {
      imgColl[i].src = (allopened) ? ImagePlus.src : ImageMinus.src;
    }
  }
  
  //set flag
  allopened = !allopened;
}

