// misc functions for NSI composed by www.DanKlarmann.com in June/July 2003

// first, reset overlib defaults
ol_fgcolor = "#669900";
ol_bgcolor = "black";

function popBelow(str){ // used by description spec details
//   overlib('<center>'+str+'</center>',BELOW,OFFSETX,-(str.length/2),FGCOLOR,"#99cc66");
// changed to left justified pop-up 1/13/2004 adk
   overlib('<center>'+str+'</center>',BELOW,FIXX,5,FGCOLOR,"#99cc66");
} // popBelow

function popOnHand(){
   ol1('In-stock quantity indicates what we have on hand. Many parts can be shipped in greater quantities with little or no additional lead time.');
}//popOnHand

function ol1(str){
//   
   overlib("<center>"+str+"</center>");
	
}//ol1

function appPopClick(strShow){ //little "click" pop-up
//   overlib("<center>"+strShow+"</center>",FGCOLOR,"#99cc66",BGCOLOR,"#000000",WIDTH,100);
   overlib("<center>"+strShow+"</center>",WIDTH,100);
}//appPopClick

function reCalcCart(){ // called by onChange in Qty columns of cart
   document.forms["cart"].elements["btnRecalc"].click();
} //reCalcCart

function isImageLoaded(strImgSrc){ //returns image.complete for image matching filename
   var isLoaded = false; //default
   for (var i=0; i<document.images.length; i++){
      if (document.images[i].src.indexOf(strImgSrc) >0){
         isLoaded = document.images[i].complete;
         break; //exit-for
      }
   }
   return isLoaded;
}//isImageLoaded()

// **** Code for pop-up enlargement images
// global vars needed to handle overlapping image pops within the Timeout
var strPopImgFile = ''
var strPopItemCode;
var iOffsetY = -100;
var iPopCount = 2; //0:init, 1:check yPos, reload, 2: done/ignore
function popImage(strImgFile, strItemCode){ // to pop-up larger item images in cart or parts lookup
   // store latest image info, in case new image moved to during timeout from unloaded img
   strPopImgFile = strImgFile;
   strPopItemCode = strItemCode;
   iPopCount = 0; //0:init, 1:check yPos, reload, 2: done/ignore
   popImage2(); // does actual pop-up, and catches timeouts
}// popImage()
function popImage2(){ // do actual pop-up
   if (iPopCount >= 2) {// do nothing
   } else if (isImageLoaded(strPopImgFile)){
      if (iPopCount == 0){ // pop once to load image to get size
         overlib("<img id='pop' src='" + strPopImgFile + "'>",OFFSETY,-1000); // off screen
         iPopCount = 1;
         setTimeout("popImage2()",100);
      } else if (document.images("pop")) { // display image at centered location
         if (Math.abs(iOffsetY + (document.images["pop"].height / 2)) > 10){ //shift iff more than 10 px change
            iOffsetY = - (document.images["pop"].height / 2);
         }
         overlib("<img id='pop' src='" + strPopImgFile + "'>",CAPTION,strPopItemCode,OFFSETY,iOffsetY,BGCOLOR,'#004080');
         iPopCount = 2;
      } else { // still in iPopCount == 1, try again
         overlib("<img id='pop' src='" + strPopImgFile + "'>",OFFSETY,-1000); // off screen
         iPopCount = 1;
         setTimeout("popImage2()",100);
      }
   } else { //image not yet loaded
      ol1(" Image Loading ...");
      setTimeout("popImage2()",333);//half-second delay
   }
}//popImage2()
function ndPop(){ // nd() to kill possible delayed pop-up
   iPopCount = 2; //prevent later pop-up
   nd();
}//ndPop()

function mnuOverlib(strMenu, strTitle){ //for consistent look/feel among various pop-up menus
   overlib(strMenu,FGCOLOR,"white",BGCOLOR,"#115500",STICKY,CAPTION,strTitle,WIDTH,250,OFFSETY,-50);
}//mnuOverLib

// *** pop-up menus for applications in LineSheet *****
//
// Database parts lookup version of LineSheet menu
function mnuAppDb(){ //(Manufacturer,app,app, ...)
   var iNumArgs = arguments.length;
   var strMenu='';

   strMenu += "<center><table border=0 cellpadding=0 cellspacing=1 width='100%'>";
   for (var n=1;n<iNumArgs;n++){ //start w/2nd arg
      strMenu += '<tr><td nowrap class="menu">';
      strMenu += '<a href="parts.asp?Submit=App&App=' + escape(arguments[n]) + "&Manufacturer=" + escape(arguments[0]);
      strMenu += '" class="menu"> ';
      strMenu += arguments[n] + ' </a>';
      strMenu += '</td></tr>';
   }
   strMenu += '</table>';

   overlib(strMenu,FGCOLOR,"white",BGCOLOR,"#115500",STICKY,CAPTION,'Select ' + arguments[0] + ' Application',OFFSETY,-iNumArgs*10, WIDTH, 150);
   //mnuOverlib(strMenu,);
}//mnuAppDb()


// app image files version of Linesheet menus
function mnuApp(){ //(Link,desc,Link,desc ...)
   var iNumArgs = arguments.length;
   var strMenu='';

   for (var n=0;n<iNumArgs;){ //2 increments happen in loop body
      if (n>0){
        strMenu += "<br> ";
      }
      strMenu += '<a href="applications/' + arguments[n++] + '.asp';
      strMenu += '" class="app"> ';
      strMenu += arguments[n++] + ' </a>';
   }
   mnuOverlib(strMenu,'Select Application');
}//mnuApp() application image files version
// menu for Application exploded drawings (unused as of July 2003)
function mnuAppItems(){ //(NSI,desc,NSI,desc,...)
   var iNumArgs = arguments.length;
   var strMenu='';

   for (var n=0;n<iNumArgs;){ //2 increments happen in loop body
      if (n>0){
        strMenu += "<br> ";
      }
      strMenu += '<a href="../cart.asp?submit=Buy&amp;Item=' + arguments[n++];
      strMenu += '" class="app"> ';
      strMenu += arguments[n++] + ' </a>';
   }
   mnuOverlib(strMenu,'Add To Cart');
}//mnuAppItems()

// *** end Linesheet/application functions

// *** multi-pane slideshow functions *******
//

// assume original panes created and loaded by html <img> tags
// Pane <img>s are named <strPaneNames>1, <strPaneNames>2,...
// image files are named <strImgName>.jpg

function presetPanes(strSetImgNames, numPanes, numImages, strSetPaneNames, iSetPaneDelay){
   strImgName = strSetImgNames;
   iMaxPanes = numPanes;
   iMaxImages = numImages;
   iPaneImgNum = numPanes + 1; //start with image numPanes+1, assume imgPane(1..numPanes) already loaded
   if (strSetPaneNames) { // not null means specified
      strPaneNames = strSetPaneNames
   }
   if (iSetPaneDelay){ // not null means specified
      iPaneDelay = iSetPaneDelay;
   }
   preLoadPanes(); // preset must be called from inside <body>, preferably near bottom
   setTimeout("rotatePane();",iPaneDelay/iMaxPanes);//start the animation
}//presetPanes

function nextPane(strObjName){ // called by presetPanes() and self via setTimeout()
//alert("T:" + this.name);
   if (this.iLoadedImages < this.iMaxImages) {
      this.iLoadedImages = 0 //reset and count how many are loaded
      for (var i=1; i <= this.iMaxImages; i++){
         if (document.images[this.strImgName + i].complete){
            this.iLoadedImages++;
         }
      }//endfor
   }
   if (this.iLoadedImages > this.iMaxPanes)  { // rotate iff enough loaded images
      // if image not complete, find next one that is
      while (! document.images[this.strImgName + this.iPaneImgNum].complete){
         this.iPaneImgNum = (this.iPaneImgNum == this.iMaxImages) ? 1 : this.iPaneImgNum + 1;
      } //endwhile
      document.images(this.strPaneNames + this.iPaneNum).src = "images/" + this.strImgName + this.iPaneImgNum + ".jpg";
      this.iPaneNum++;
      if (this.iPaneNum > this.iMaxPanes) {
         this.iPaneNum = 1;
      }
      this.iPaneImgNum = (this.iPaneImgNum == this.iMaxImages) ? 1 : this.iPaneImgNum + 1;
   } //endif completed rotation

   if (this.iPaneDelay > 100){ // this if clause for protection of uninitialized object
      setTimeout(strObjName +'.nextPane("'+strObjName + '")' ,this.iPaneDelay/this.iMaxPanes);
   }

}//rotatePane

function preLoadImages(){ // called by presetPanes()
   document.writeln ('<div style="display:none;">');
   for (var i=1; i<= this.iMaxImages; i++){
      document.writeln ("<img src='" + "images/" + this.strImgName + i + ".jpg' id='" + this.strImgName + i + "' width=1 />")
//      document.writeln ("<img src='" + "images/" + this.strImgName + i + ".jpg' id='" + this.strImgName + i + "' />")
   } //endfor
   document.writeln ('</div>');
}//preLoadImages
// object definition for a slideshow

function SlideShow(strSetImgNames, numPanes, numImages, strSetPaneNames, iSetPaneDelay){
   this.iPaneNum= 1;
   this.iPaneImgNum = 1;
   this.iLoadedImages = 0; //number or images completely loaded

   this.strImgName = strSetImgNames;
   this.iMaxPanes = numPanes;
   this.iMaxImages = numImages;
   this.strPaneNames = strSetPaneNames;
   this.iPaneDelay = iSetPaneDelay; // millisecs for all panes (indiv pane delay = iPaneDelay/iNumPanes)

   this.preLoadImages = preLoadImages;
   this.nextPane =   nextPane;
} //SlideShow() object definition

// ****  end multi-pane rotating images slideshow code ****

function validEmail(frm){ // returns true if field named "email" in frm is valid
  var strEmail = frm.email.value;
  if (strEmail.search( /\w/) < 0) { // will accept empty or whitespace email address
    return true;
  } else if (strEmail.search( /^.+\@[a-zA-Z0-9\-\_\.]+\.([a-zA-Z]{2,4})$/i ) < 0 ){
    alert('Invalid or incomplete email address: ' + strEmail +"\nEither delete the field or correct the email address");
    frm.email.focus();
    return false;
  } else {
    return true;
  }
}// validEmail

