//  this file includes the generic javascript functions on the herman street
//  snarthtml pages


//
//  this function builds the mini cart visible from all smarthtml pages
//
function DisplayMiniCart(name,style) {
  var cookies=document.cookie;  //read in all cookies
  var start = cookies.indexOf(name + "=");  //set start to beginning of ss_cart cookie
  var cartvalues = "";
  var linecount = 0;
  var start1;
  var end1;
  var tmp;



  if (start != -1)  //Cart cookie exists
  {
    // Mini-Cart header
    document.write("<div class=\"minicart\">"); 
    document.write("<a href=\"http://www.hermanstreet.com/ss-bin/sc/order.cgi?storeid=*18e69050bb8f759c5b63ac3b4090&function=show\">");
    document.write("<img src=\"http://www.hermanstreet.com/store/media/i/minicart.gif\" align=\"left\" border=\"0\" >");
    document.write("<\/a>"); 
    document.write("<div class=\"carttext\">");

    start = cookies.indexOf("=", start) +1;  
    var end = cookies.indexOf(";", start);  
    if (end == -1)
    {
      end = cookies.length;
    }
    cartvalues = unescape(cookies.substring(start,end)); //read in just the cookie data

    start = 0;
    while ((start = cartvalues.indexOf("|", start)) != -1)
    {
      start++;
      end = cartvalues.indexOf("|", start);
      if (end != -1)
      {
        linecount++;
        if (linecount == 2)  // Total Quantity of Items
        {
          tmp = cartvalues.substring(start,end);
          colon = tmp.indexOf(":", 0);
          document.write("<div class=\"miniinfo\">");
          document.write("Contains ");
          document.write(tmp.substring(colon+1,end - start));
          if ((tmp.substring(colon+1,end - start)) == 1 )
          {
            document.write(" Item");
          }
          else
          {
            document.write(" Items");
          }
            document.write("<\/div>");
            document.write("<div class=\"miniinfo\">");
            document.write("Subtotal: ");
        }
        if (linecount == 3)  // Product Subtotal
        {
            tmp = cartvalues.substring(start,end);
            colon = tmp.indexOf(":", 0);
            document.write(tmp.substring(colon+1,end - start));
            document.write("<\/div>");
        }
        start = end;
      }
      else
        break;
    } // end while loop
    
    //close minicart HTML
    document.write("<\/div>");
    document.write("<\/div>");
  }
}

  //
  //  begin bubble popup functions
  //  

  function goPopup(thePage,w,h){
      window.open(thePage,'screen','width='+w+',height='+h+',scrollbars=yes,resizable=yes');
  }

  //this function makes it when you click to enlarge it displays the larger product img div.
  function popLargeImg(n){
	eval('oDivEl=document.getElementById("' + n + '");');




	if(oDivEl.style.visibility=="visible"){
		oDivEl.style.visibility="hidden";
	}else{
		currentindex++;
		oDivEl.style.top = (posY() + 50) + "px";
  //		oDivEl.style.z-index=currentindex;
		oDivEl.style.visibility="visible";
	}
  }
  
  function onSelectBox(ids){
      for(var i = 0; i < ids.length; i++){
        var element = document.getElementById(ids[i]);

        element.checked = false;
        element.disabled = true;
        
      }
  }

  /*
     name: offSelectBox
     author: brett donohoo
     parameter(id): array of ids being turned on
     parameter(priceArr): priceArr[0] - id of price container
                          priceArr[1] - price
     
  */
  
  function offSelectBox(ids){
    
      for(var i = 0; i < ids.length; i++){
        var element = document.getElementById(ids[i]);

        element.disabled = false;

      }
      
  }
  
  function updateElement(updArr){
    for(var i = 0; i < updArr.length; i++){
        
        var updInfo = updArr[i].split(':');
        
        document.getElementById(updInfo[0]).innerHTML = updInfo[1];
      }
  }

  function fadeBox(divName, endOpac){
      var grayBox = document.getElementById(divName);
      var i = parseFloat(grayBox.style.opacity);
      var step = 0.1;

      if(endOpac){
          if(i < endOpac){
              i += step;
	      i = parseFloat(i.toPrecision(2));
              document.getElementById(divName).style.filter = "alpha(opacity=" + i*100 + ")";
              document.getElementById(divName).style.opacity = i;
          }else window.gbinterval = clearInterval(window.gbinterval);
      }else{
          if(i > endOpac){
              i -= step;
	      i = parseFloat(i.toPrecision(2));
              document.getElementById(divName).style.filter = "alpha(opacity=" + (i*100) + ")";
              document.getElementById(divName).style.opacity = i;
          }else {
              window.gbinterval = clearInterval(window.gbinterval);
              showhidediv(divName,2);
          }
      }
  }          
    
  function showhidediv(divname,type) {
      box=document.getElementById(divname);
      box.style.display=(type==1) ? "block":"none";
  }          

  function popgbox(type/*, loadurl*/) {

      if(type == 1) {
          // default width and height
          width=330;
          height=230;
          // hide scroll bars
          windowscroll=document.getElementsByTagName('html')[0];
          windowscroll.style.overflow="hidden";

          // make the div visible
          box=document.getElementById('faderbox');
          box.style.opacity=0;
          box.style.filter="alpha(opacity=0)";
          showhidediv('faderbox',1);
          // fade in
          window.gbinterval=setInterval("fadeBox('faderbox',0.6)",25);
        

          // set the top and left based on browser window position
          // get position of pop window

          leftscroll=(document.documentElement.scrollLeft) ?  document.documentElement.scrollLeft : document.body.scrollLeft;
          topscroll=(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
          // make IE and opera play nice together

          /*alert(window.innerHeight);
          alert(window.innerWidth);
          
          alert(document.body.clientHeight);
          alert(document.body.clientWidth);*/

          browseheight=(window.innerHeight) ? window.innerHeight:(document.documentElement.clientHeight!=0?document.documentElement.clientHeight:document.body.clientHeight);
          browsewidth=(window.innerWidth) ? window.innerWidth:(document.documentElement.clientWidth!=0?document.documentElement.clientWidth:document.body.clientWidth);

          // if the browser is smaller than the window will be..
          height=(browseheight < height) ? browseheight:height;
          width=(browsewidth < width) ? browsewidth:width;

          browseheight=(browseheight < height) ? 0:browseheight;
          boxtop=(browseheight/10) + topscroll;
          boxtop=boxtop-(height/10);
          boxtop=(boxtop <=0) ? 0:boxtop;

          browsewidth=(browsewidth < width) ? 0:browsewidth;
          boxleft=(browsewidth/3) + leftscroll;
          boxleft=boxleft-(width/3);
          boxleft=(boxleft <= 0) ? 0:boxleft;

           // show the content area with the iframe
          /*ifm=document.getElementById('popfrm');
          ifm.src=loadurl;
          ifm.style.height=(height)+ "px";
          ifm.style.width=(width)+ "px";*/
          contbox=document.getElementById('popcontent');
          contbox2=document.getElementById('popcontent2');

          //position
          contbox.style.top=boxtop + "px";
          contbox.style.left=boxleft + "px";
          contbox2.style.top=(boxtop + 50) + "px";
          contbox2.style.left=boxleft + "px";

          /*document.getElementById('pophead').contentWindow.document.body.innerHTML='<div style="width:45px; margin-top:5px; float:right;"><a style="font-size:12px; color:#000000; text-decoration:none;" href="javascript:top.popgbox(2);"><img src="http://10.207.58.160/sandboxes/Jon/ttrlogin/greybox/w_close.gif" border="0" alt="close box"/> Close</a></div>';
          document.getElementById('pophead').contentWindow.document.body.style.padding="0";
          document.getElementById('pophead').contentWindow.document.body.style.margin="0";
          document.getElementById('pophead').contentWindow.document.body.style.background="#ABABC2";
          document.getElementById('pophead').contentWindow.document.body.style.border="0";
          document.getElementById('pophead').contentWindow.document.getElementsByTagName('html')[0].style.overflow="hidden";
          document.getElementById('pophead').style.width=(width - 2) + "px";*/

          // slide the content div out?

          // for now just set the width and height to what was passed in
          contbox.style.height=height + "px";
          contbox.style.width=width + "px";
          contbox.style.height=height + "px";
          contbox.style.width=width + "px";
          
          
          contbox2.style.height=(height - 50) + "px";
          contbox2.style.width=width + "px";
          contbox.style.display="block";
          contbox2.style.display="block";
          // call some function to slide it

      }
      else {
          // hide the content div
          showhidediv('popcontent',2);
          showhidediv('popcontent2',2);
          // fade it out
          window.gbinterval=setInterval("fadeBox('faderbox',0)",25);

          // put back the scrolls
          windowscroll=document.getElementsByTagName('html')[0];
          windowscroll.style.overflow="auto";
          // and for safari and chrome
          document.body.scrollTop += 1;
          document.body.scrollTop -= 1;
      }
  }
  
  //
  //  end bubble pop up functions
  //

//Small business form validation function
function ValidateForm(frm){
  if(frm==1){
  
	  var phone=document.orderReq.phone
	  var name=document.orderReq.name
	  var email=document.orderReq.email
	  var company=document.orderReq.company
  }else{
	  var phone=document.orderReq2.phone
	  var name=document.orderReq2.name
	  var email=document.orderReq2.email
	  var company=document.orderReq2.company            
  }



  if ((name.value==null)||(name.value=="")){
		  alert("Please enter your name.")
		  return false;
  }
  if ((company.value==null)||(company.value=="")){
		  alert("Please enter your company's name.")
		  return false;
  }
  if ((email.value==null)||(email.value=="")){
		  alert("Please enter a valid email address.")
		  return false;
  }       
  if ((phone.value==null)||(phone.value=="")){
		  alert("Please enter phone number.")
		  return false;
  }


  return true;
}
/****************This Function lets you change your our price and reg price on the pages*********************/
function SetBackupCDPrice(checkbox, backupCD){
    if(checkbox.checked){
        var regprice = document.getElementById('regprice').innerHTML;
        var newRegprice = Number(regprice.substring(1)) + Number(backupCD);
        document.getElementById('regprice').innerHTML = '$' + newRegprice.toFixed(2);
        var ourprice = document.getElementById('ourprice').innerHTML;
        var newOurprice = Number(ourprice.substring(1)) + Number(backupCD);
        document.getElementById('ourprice').innerHTML = '$' + newOurprice.toFixed(2);
    }else{
        var regprice = document.getElementById('regprice').innerHTML;
        var newRegprice = Number(regprice.substring(1)) - Number(backupCD);
        document.getElementById('regprice').innerHTML = '$' + newRegprice.toFixed(2);
        var ourprice = document.getElementById('ourprice').innerHTML;
        var newOurprice = Number(ourprice.substring(1)) - Number(backupCD);
        document.getElementById('ourprice').innerHTML = '$' + newOurprice.toFixed(2);
    }
}
/***********************************************************************************************************/


