var req;
var addrReq;
var addrListReq;
var showReq;
var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

function keyinit()  {    
    if (document.addEventListener)   {
       document.addEventListener("keydown",keydown,false);
    }else if (document.attachEvent){
       document.attachEvent("onkeydown", keydown);
    }else{
       document.onkeydown= keydown;
    }
}
function keydown(e)  {
   if (!e) {
       e = event
   }
   if (e.keyCode == 13) {                
      searchItemsNEW()
      if (e.preventDefault){
         e.preventDefault();
      }
      if (e.stopPropagation){
         e.stopPropagation();
      }
      return false;
   }
}
function callPageNew(pageUrl,divElementID,loadingMessage,pageErrorMessage) {    
    var thisDiv=document.getElementById(divElementID)
    thisDiv.innerHTML="<iframe src=\"" + pageUrl + "\" border=\"0\" height=\"100%\" width=\"100%\"><iframe>"
}    
    
function callPage(pageUrl, divElementId, loadinglMessage, pageErrorMessage) {     
     document.getElementById(divElementId).innerHTML = loadinglMessage;
     req=GetXmlHttpObject();
 
     req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);};
     req.open("GET",pageUrl,true);
     req.send(null);
  }
function callParentPage(pageUrl, divElementId, loadinglMessage, pageErrorMessage) {
     parent.document.getElementById(divElementId).innerHTML = loadinglMessage;
     req=GetXmlHttpObject();
 
     req.onreadystatechange = function() {responseParentfromServer(divElementId, pageErrorMessage);};     
     req.open("GET",pageUrl,true);
     req.send(null);
  }
  
function responsefromServer(divElementId, pageErrorMessage) {
   var output = '';
   if(req.readyState == 4) {
      output = req.responseText;      
      if(req.status == 200) {
         document.getElementById(divElementId).innerHTML = output;
         } else {
         document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
         }
      }
  }
function responseParentfromServer(divElementId, pageErrorMessage) {
   var output = '';
   if(req.readyState == 4) {
      output = req.responseText;      
      if(req.status == 200) {
         
         parent.document.getElementById(divElementId).innerHTML = output;
         } else {
         parent.document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
         }
      }
  }

function callPageWReq(pageURL, divElementID, loadingMessage, pageErrorMessage, theReq) {
    document.getElementById(divElementID).innerHTML=loadingMessage;
    theReq=GetXmlHttpObject();
    theReq.onreadystatechange=function() {responsefromServerWReq(divElementID,pageErrorMessage,theReq)}
    theReq.open("GET",pageURL,true)
    theReq.send(null);
}
function responsefromServerWReq(divElementId, pageErrorMessage, theReq) {
   var output = '';
   if(theReq.readyState == 4) {      
      if(theReq.status == 200) {
         output = theReq.responseText;      
         document.getElementById(divElementId).innerHTML = output;
       } else {
         document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
       }
   }
}

/* this function is for tab panels */
function activeTab(tab,destpage,destdiv)
{   	

	document.getElementById("tab"+tab).className = "active";
	if(destdiv=='content')
	{
		show(false,'tabdiv');
		show(true,'content');
	}else{
		show(true,'tabdiv');
		show(false,'content');
	}	
	callPage(destpage, destdiv, '<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page <img src=\"imgs/error_caution.gif\" />');		
}

function aPostForm(formName,divElementId) {
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    pagename="login1.aspx";
    passData='&dowhat=Login';
    if (null == eval("document."+formName+".UserName.value")) {
        frmuser="";
    }else{
        frmuser=eval("document."+formName+".UserName.value");
    }    
    if (null == eval("document."+formName+".Password.value")) {
        frmpassword="";   
    }else{
        frmpassword=eval("document."+formName+".Password.value");
    }   
    
    passData='dowhat=Login';
    passData=passData+'&Password='+escape(frmpassword);
    passData=passData+'&UserName='+escape(frmuser);

    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}

function aQuerystringForm(formName,divElementId){
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();
    pagename="survey.aspx";  
    frmmember=eval("document."+formName+".member.value");
    frmsurveyid=eval("document."+formName+".surveyid.value");
    for (var i=0; i < document.frmSurvey.answer.length; i++)
    {
    if (document.frmSurvey.answer[i].checked)
      {
      var rad_val = document.frmSurvey.answer[i].value;
      }
    }
    frmanswer = rad_val;
    passData='member='+escape(frmmember);
    passData=passData+'&answer='+escape(frmanswer);
    passData=passData+'&surveyid='+escape(frmsurveyid);
    passData=passData+'&dowhat=save';
    
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}
function singleCall(destpage,destdiv) {
    callPage(destpage,destdiv,'<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page');
}

function nothingfunction(){
}

function GetXmlHttpObject() {
   var xmlHttp=null;
   try  { // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
   }
   catch (e) {  // Internet Explorer
      try {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return xmlHttp;
}


function showme(divname){
	showdis=document.getElementById(divname).innerHTML
	document.getElementById('showmetext').value=showdis
}
function calPickDay(theday,themonth,theyear) {
    destpage="quickCalendar.aspx?monum=" + themonth + "&daynum=" + theday + "&yearnum=" + theyear
    callPage(destpage, 'calspan', '<img src=\"imgs/loading.gif\" /> content is loading, please wait...', 'error in loading page <img src=\"imgs/error_caution.gif\" />');		

}
function popupImg(someImage) {
    var newwin=window.open("zoomer.aspx?thepic=" + someImage,"zoom","width=625,height=625,menubar=no,toolbar=no,location=no,status=no,resizable=no,scrollbars=no")
}
 function EditLine(formName,divElementId,pagename,editID) {
    var pageErrorMessage='Error in Loading page';
    req=GetXmlHttpObject();    
    passData='dowhat=Edit';
    passData=passData+'&editID='+editID;
    req.open("POST",pagename,true); 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);}; 
    req.send(passData);
}

function gotoPage(pagename)  {
    parent.location = pagename
}

function show(sw,obj) {
	// show/hide the divisions
	if (sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'visible';
	if (!sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'hidden';
	if (sw && ns4) document.layers[obj].visibility = 'visible';
	if (!sw && ns4) document.layers[obj].visibility = 'hidden';
}
function openeditX(HT,WT,sbars) {
	var winX = (document.all)?window.screenLeft:window.screenX;
	var winY = (document.all)?window.screenTop:window.screenY;
	theX=parseFloat(winX)+parseFloat(100)
	theY=parseFloat(winY)+parseFloat(50)
	var W = "height="+HT+",width="+WT+",scrollbars="+sbars+",left="+theX+",top="+theY
	editwinX=open("blank.html","editwinX",W)
	return true
}
followme=""
function getSubCats(catID,level,text) {
    if (level>2) {
       parts=followme.split(">>")
       followme=""       
       for (x=1;x<=level-2;x++) {
           followme=followme + ">>" + parts[x]
       }
    } else {
       followme=""
    }
    //alert(catID + "-" + level + "-" + text)
    text=text.replace("~39","'")
    followme=followme + ">><a class=follow href=\"javascript:getSubCats(" + catID + "," + level +",'"+ escape(text) +"')\">" + text + "</a>&nbsp;&nbsp;"
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme
    pageURL="bp_subcats.aspx?level=" + level + "&catID=" + catID    
    callPage(pageURL,"submaincontent","Loading","Unable to Locate SubCats")
}

function getitem(itemCode, itemDesc) {
    itemDesc=itemDesc.replace("~39","'")
    followme=followme +">>" + itemCode + "-" + itemDesc
    
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme 
    
    pageURL="item.aspx?itemnum=" + itemCode    
    callPage(pageURL,"mainsec","Loading","Unable to Find Item")
}
function getaltitem(itemCode, itemDesc, oldItem) {
    itemDesc=itemDesc.replace("~39","'")
    // followme=followme +">><span class=title>" + itemCode + "-" + itemDesc + "</span>"
    followme=">><span class=title>" + itemCode + "-" + itemDesc + "</span>"
    
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme 
    
    pageURL="bp_viewproduct.aspx?itemnum=" + itemCode + "&oldItem=" + oldItem    
    //callPage(pageURL,"submaincontent","Loading","Unable to Find Item")
    document.location=pageURL
}
function getANitem() {    
    itemCode=document.getElementById("searchtext").value;
    pageURL="item.aspx?itemnum=" + itemCode    
    callPage(pageURL,"mainsec","Loading","Unable to Find Item")
}
function searchItem() {
    someString=document.getElementById("searchtext").value;
    pageURL="item.aspx?itemnum=" + someString;   
    if (document.getElementById("mainsec")==null) {
         document.location="catlist.aspx?someStr=" +someString
         return
    }     
    callPage(pageURL,"mainsec","Loading","Unable to Find Item");
}    
function searchItems() {
    someString=document.getElementById("searchfrm").value
    // alert(someString);
    pageURL="bp_productlist.aspx?someStr=" + someString    
    if (document.getElementById("maincontent")==null) {
         document.location="buyproducts_catalog.aspx?someStr=" +someString
         return
    }
    // callPage(pageURL,"submaincontent","Loading","Unable to Find Item")
    document.location=pageURL
}    
function searchItemsNEW() {
   //alert('hello?')
    var someString=document.getElementById("searchfld").value
  //alert(someString)    
    window.location="buyproducts_catalog.aspx?thePage=search&someStr=" + someString    
}    
function openItems() {
    pageURL="bp_productlist.aspx"
    //callPage(pageURL,"submaincontent","Loading","Unable to Find Items")
    //setFollowme('Item Search List');
    window.location=pageURL
}
function putincart() {
    itemnum=document.getElementById("part").value
    qty=document.getElementById("qty").value
    um=document.getElementById("um").value       
    window.location="bp_putincart.aspx?itemnum=" + itemnum + "&qty=" + qty +"&um=" + um
}  
function putinIFcart() {
    itemnum=document.getElementById("part").value
    qty=document.getElementById("qty").value
    um=document.getElementById("um").value       

    followme="<span class=title>Your Cart</span>"
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme 

    pageURL="bp_putincart.aspx?itemnum=" + itemnum + "&qty=" + qty +"&um=" + um 
    callPage(pageURL,"submaincontent","Loading","Unable to Find Cart Items")
}    
function putinIFcart2() {
    pageURL="bp_putincart.aspx?item=session"
    callPage(pageURL,"submaincontent","Loading","Unable to Find Cart Items")
    setFollowme("<span class=title>Your Cart</span>")
}
function openAjax(thepage) {
    
    if (thepage=='cart') {
        openCart()
    }
    if (thepage=='login') {
        openLogin()
    }
    if (thepage=='checkout') {
        openCheckout()
    }
    if (thepage=='search') {
        openItems()
    }
    if (thepage=='specials') {
        openSpecials()
    }
    if (thepage=='item') {
        openItembyid()
    }
    if (thepage=='putcart') {
        putinIFcart2()
    }    
}
function updateOtherContent() {        
    
    callParentPage("setOtherContent.aspx?dowhat=loginheader","login","","")
    parent.document.getElementById("login").className="logout"
    
}    
function openItembyid(){
    pageURL="bp_viewproduct.aspx?item=session"
    //callPage(pageURL,"submaincontent","Loading","Unable to Find Item")
    //setFollowme('View Item');
    window.location=pageURL
}
function openSpecials() {
    pageURL="bp_specials.aspx"
    callPage(pageURL,"submaincontent","Loading","Unable to Find Items")
    setFollowme('Specials');
}
function openCart() {
    followme="<span class=title>Your Cart</span>"
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme 
    cartframe="<div><iframe id='IFbuyproducts' width='760px' height='1080px' frameborder='0' src='bp_cart.aspx' ></iframe></div>"
    cartDiv=document.getElementById("submaincontent")
    cartDiv.innerHTML=cartframe
}
function setFollowme(text) {
    followme="<span class=title>" + text + "</span>"
    followDiv=window.parent.document.getElementById("followme")
    followDiv.innerHTML=followme     
}
function setFollowme2(text) {
    followme="<span class=title>" + text + "</span>"
    followDiv=document.getElementById("followme")
    followDiv.innerHTML=followme     
}
function openLogin() {
    loginframe="<div><iframe id='IFbuyproducts' width='760px' height='1080px' frameborder='0' src='bp_login.aspx' ></iframe></div>"
    loginDiv=document.getElementById("submaincontent")
    loginDiv.innerHTML=loginframe
    setFollowme('User Login');
}
function openCheckout() {
    theframe="<div><iframe id='IFbuyproducts' width='760px' height='1080px' frameborder='0' src='bp_payment-terms.aspx' ></iframe></div>"
    theDiv=document.getElementById("submaincontent")
    theDiv.innerHTML=theframe
    setFollowme('Select Terms ');
}
function altputincart(itemnum,qty,um) {
    window.location="bp_putincart.aspx?itemnum=" + itemnum + "&qty=" + qty +"&um=" + um
}    

function deleteline(index) {
    window.location="buyproducts_cart.aspx?dowhat=delete&index=" + index
}

function updatecart(linecnt) {
    qtystr=""
    for (x=0;x<linecnt;x++) {
       qtystr=qtystr + "&qty" + x + "=" + document.getElementById("qty" + x).value
    }
    window.location="bp_cart.aspx?dowhat=update" + qtystr
}
function calcTax(statetax,countytax) {
    ordAmt=document.getElementById("orderamt").innerHTML
    theTaxRate=document.getElementById("taxrate")
    theTaxAmt=document.getElementById("taxamt")
    ordAmt=ordAmt.replace("$","")
    ordAmt=ordAmt.replace(",","")
    shipst=document.getElementById("sstate").value
    shipst=shipst.toUpperCase()    
    if (shipst=="FL") {
        myTaxRate=statetax+countytax
    } else {
        myTaxRate=0
    }
    myTaxAmt=ordAmt*(myTaxRate/100)
    myTaxAmt=Math.round(myTaxAmt*100)
    myTaxAmt=myTaxAmt/100
    theTaxRate.innerHTML=myTaxRate
    theTaxAmt.innerHTML="$" + myTaxAmt
    
    totalinv=document.getElementById("total")
    theTotal=parseFloat(ordAmt)+parseFloat(myTaxAmt)
    totalinv.innerHTML="$" + theTotal
}        
function checkterms(){
    creddiv = document.getElementById("creditdiv")
    termdiv = document.getElementById("termsdiv")
    termstype = document.getElementById("cctype").value
    if (termstype == "11") {
        creddiv.className = "payinvis"
        termdiv.className = "payvis"
    }else if (termstype=="3" || termstype=="4" || termstype=="5" || termstype=="6")  {
        creddiv.className = "payvis"
        termdiv.className = "payinvis"
    }
}
function confirmthis(){
    termschk = document.getElementById("yesterms")    
    //url = "bp_checkoutstep3.aspx"
    if (termschk.checked) {
        document.frmCart.submit()
        //window.location=url
    }else{
        alert("Please read the terms and check before confirming order.")
    }
}
function setPullDowns(CCType,CCMo,CCYr){
//Variable for all teh drop downs in this function
    var DropDown = document.getElementById("cctype") //card Type
    switch (CCType) {
    case 4: DropDown.selectedIndex = 0; break;
    case 5: DropDown.selectedIndex = 1; break;
    case 3: DropDown.selectedIndex = 2; break;
    case 6: DropDown.selectedIndex = 3; break;
    case 11: DropDown.selectedIndex = 4; break;
    default: DropDown.selectedIndex = 0;
    }
    DropDown = document.getElementById("ccmonth") //EXP Month
    DropDown.selectedIndex = CCMo;
    var thisYear = new Date()
    DropDown = document.getElementById("ccyear") //EXP Year
    DropDown.selectedIndex = CCYr - thisYear.getFullYear() + 1;

}
