﻿
var ie;
//var prdXmlHttp
//var ProductsprdXmlHttp;  

function testBrowser()
{
ie = document.all?true:false		//determine netscape or IE
}   
   
function GetKeyCode(e)
{
var e = e || window.event;
    var code=0;
    if (e.keyCode)
      {
        code = e.keyCode;
      }
      else if (e.which)
      {
        code = e.which;
      }
return code;
}
   
//------------------------//------------------------//------------------------
//------------------------//------------------------//------------------------
//AutoComplete
//------------------------------//------------------------//------------------------

function SelectList(Control, TargetDiv, ArrayName, BoolName, HiddenFieldID, e)
{

 //===========================================
    var code=GetKeyCode(e);
 //===========================================
     
     
found = false;

        if (HiddenFieldID == undefined)
        {     
            //build the first part of the string considering HiddenFieldID as optional param
            strDiv = " onClick='FillTextBox(\"" + Control.id + "\", this, \"" + TargetDiv + "\", undefined)' onmouseover='" + BoolName + " = false;' onmouseout='" + BoolName + " = true;'>";
            //alert(strDiv);
        }
        else
        {
            strDiv = " onClick='FillTextBox(\"" + Control.id + "\", this, \"" + TargetDiv + "\", \"" + HiddenFieldID.id + "\")' onmouseover='" + BoolName + " = false;' onmouseout='" + BoolName + " = true;'>";
        }
        
        var TheArray = window[ArrayName];

        //test the array to insure that there was no problem filling the list
        //trap the error and kill the function
        try 
        {
           var x = TheArray.length
          }catch(e)
          {
          alert("There was a problem creating the list");
          return false;
        }
        
        ldiv = document.getElementById(TargetDiv)
        val = Control.value.toLowerCase()
        len = val.length
        
        
 divCount = -1;
 var FocusArray = new Array;
 var FocusIndexIndex = 0;
 var arrListItems = new Array(0)
  
//        if (len < 1) 
//        {
//          ldiv.innerHTML = "" 
//          //ldiv.style.display = "none" 
//          ldiv.style.display = "block" 
//                for (i=0;i<TheArray.length;i++) 
//                    { found = true; 
//                      divCount++;  
//                      
//                        pest = TheArray[i];                                                 
//                           var thisid = Control.id;                           
//                            ldiv.innerHTML = ldiv.innerHTML + "<div id='" + i + "' " + strDiv + pest + "</div>";
//                            FocusArray[divCount] = i;
//                    } 
//        }
//        else
//        {        
            ldiv.style.display = "block" 
            ldiv.innerHTML = ""
            for (i=0;i<TheArray.length;i++) 
            {
                pest = TheArray[i]
                ckval = pest.substring(0,len).toLowerCase()
                //alert(ckval + " == " + val)
                if (ckval == val) 
                {
                    found = true; 
                    divCount++;                   
                    var thisid = Control.id;                   
                    //ldiv.innerHTML = ldiv.innerHTML + "<div id='" + i + "' " + strDiv + pest + "</div>";
                    arrListItems.push("<div id='" + i + "' " + strDiv + pest + "</div>");
                    FocusArray[divCount] = i;
                }
            } 
             ldiv.innerHTML = arrListItems.join(''); 
       // } 
        
//======= ARROW SCROLL ========================================
            //===============================================
            if(code != 38 && code != 40 && code != 13)
            {FocusIndex = -1}
            
            //up down
            if(code == 40 && FocusIndex <= divCount)
              {                                                 
                   if (FocusIndex < divCount)
                   {
                   FocusIndex++;
                   }
                 document.getElementById(FocusArray[FocusIndex]).className = "OwnerListHover";
                 BoolName = false;                         
              }
              
            //down up
            if(code == 38 && FocusIndex > -1)
            {      
                if (FocusIndex > 0)
                {FocusIndex -=1;}
                           
                document.getElementById(FocusArray[FocusIndex]).className = "OwnerListHover";
                BoolName = false;
                
            }
            
        //========================================
            
        //========================================
        
            if (code == 13 && FocusIndex > -1)
            {
                BoolName = true;                       
                //AutoCompleteSelected(document.getElementById(FocusArray[FocusIndex]));
               //alert(FocusArray[FocusIndex]);
                if(HiddenFieldID != undefined)
                {
                FillTextBox(Control.id, document.getElementById(FocusArray[FocusIndex]), TargetDiv, HiddenFieldID.id) 
                }else
                {
                FillTextBox(Control.id, document.getElementById(FocusArray[FocusIndex]), TargetDiv, undefined) 
                }
                FocusIndex = -1;
            }
       
//========================================
        
        if (found == false) 
        {
            ldiv.style.display = "none"
        }        
}


function ClearDropdown(TargetDiv, isBlurry)
{

//isBlurry is the name of the global boolean var.
if(window[isBlurry])
    {
    ldiv = document.getElementById(TargetDiv);
    ldiv.innerHTML = "";
    ldiv.style.display = "none"
    window[isBlurry]=true;
   }
}

function FillTextBox(TextBox, SelectedDiv, ListDiv,  hiddenFieldID)
{
//alert("FillPestTextBox - " + TextBox + ", " + SelectedDiv.innerHTML + ", " + ListDiv + ", " +  hiddenFieldID);
    document.getElementById(TextBox).value = SelectedDiv.innerHTML;
    document.getElementById(ListDiv).style.display = "none"; 
    if (hiddenFieldID != undefined)
    {
        document.getElementById(hiddenFieldID).value = "selected" 
    }
}

//value is the value of the dropdown list and the key for the datarow
function FillProductText(id, value, epaid, aiId)
{
  //alert(id + " | " + value + " | " + epaid + " | " + aiId + " | " +  aiArray[value]);
   if(value != "Edit")
   {
        if(value != "")
        {
            document.getElementById(epaid).value = epaArray[value];
            document.getElementById(aiId).value = aiArray[value];
        }else
        {
            document.getElementById(epaid).value = "";
            document.getElementById(aiId).value = "";
        }
   }
 }
 
function ShowEditForm(id, value)
{
    if (value == "Edit")
    {
        document.getElementById(id).style.visibility = "visible";
    }
}

       
function checkForNulls(node)
{
    if(node != null)
    {
        return node.nodeValue;
    }
    else
    {
        return "";
    }
}


//AutoCompleteURL is optional and compensates when page is not in reports directory
function SendRequest(strXML, StateChangedFunction, RequestObject, AutoCompleteURL)
{
    //alert("send request" + AutoCompleteURL);
     
    if (RequestObject==null)
    {
       // alert ("Your browser does not support AJAX! Please continute filling out the form by hand.");
        return;
    }
    //used to stop caching when the list is called from the close button on the edit form
    var date = new Date();
    var timestamp = "&t=" + date.getTime();
    
    var url="../controls/AutoComplete.asmx" + strXML + timestamp;
   
    if(AutoCompleteURL != undefined)
    {
        url = AutoCompleteURL;
    }
    
    RequestObject.onreadystatechange = StateChangedFunction;   
    RequestObject.open("GET",url,true);
    //THE FOLLOWING 2 LINES ARE ADDED FOR A POST OPERATION
    //RequestObject.setRequestHeader("Content-Type", "text/xml");
    //RequestObject.setRequestHeader("Content-Length", "25");
   
    RequestObject.send(null);
  
}
 
function PostRequest(strParams, StateChangedFunction, RequestObject, AutoCompleteURL) {
    //alert("PostRequest" + strParams);
    if (RequestObject == null) {
         //alert ("Your browser does not support AJAX! Please continute filling out the form by hand.");
        return;
    }
    
    RequestObject.onreadystatechange = StateChangedFunction;
    RequestObject.open("POST", AutoCompleteURL, true);
    //THE FOLLOWING 2 LINES ARE ADDED FOR A POST OPERATION

    RequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    RequestObject.setRequestHeader("Content-Length", strParams.length);
    //alert("PostRequest" + strParams);
    RequestObject.send(strParams);

}

function PostSOAP(strParams, StateChangedFunction, RequestObject, AutoCompleteURL) {
    //alert("PostRequest" + strParams);
    if (RequestObject == null) {
        //alert ("Your browser does not support AJAX! Please continute filling out the form by hand.");
        return;
    }

    RequestObject.onreadystatechange = StateChangedFunction;
    RequestObject.open("POST", AutoCompleteURL, true);
    //THE FOLLOWING 2 LINES ARE ADDED FOR A POST OPERATION

    RequestObject.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    RequestObject.setRequestHeader("Content-Length", strParams.length);
    //alert("PostRequest" + strParams);
    RequestObject.send(strParams);

} 

//this is the browser test function
function GetHttpObject()
    {
        //var prdXmlHttp=null;
            try
              {
              // Firefox, Opera 8.0+, Safari
              prdXmlHttp = new XMLHttpRequest();
                          
              }
            catch (e)
              {        
                  // Internet Explorer
                  try
                    {
                    prdXmlHttp=new ActiveXObject("Msxml2.XmlHttp");
                    }
                  catch (e)
                    {
                    prdXmlHttp=new ActiveXObject("Microsoft.XmlHttp");
                    }
              }
    return prdXmlHttp;
    }  

//get and xml doc from a returned string of XML
function CreateXMLDoc(text, ResultTagName)
{
//alert(text);
    if(ie) 
      {   
          //Internet Explorer
          try{
              xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async=false;
              xmlDoc.loadXML(text); 
             }
              catch(e){alert("IE Browser XML Load error:( httpRequests.js.CreateXMLDoc() ) " + e.message);
             }         
      }
        else
      {
        //Firefox, Mozilla, Opera, etc.
        try{
              parser=new DOMParser();
              xmlDoc=parser.parseFromString(text,"text/xml");
              xmlDoc.normalize();
            }
              catch(e){alert("FireFox Browser XML parser error:( httpRequests.js.CreateXMLDoc() ) " + e.message);
            }
      } 
      //return xmlDoc; 
            
        x = xmlDoc.getElementsByTagName(ResultTagName);        
        //alert(x[0].childNodes[0].nodeValue);
        //var strXML = x[0].childNodes[0].nodeValue
        var strXML;
        try
          {

              strXML = x[0].childNodes[0].nodeValue
              //alert(strXML);
          }
        catch(e) {
            alert("The system encountered an error, you request did not completed. ");
            //alert("httpRequsts.js/CreateXMLDoc/ "  + e.message);
            strXML="<string></string>";
          }
          
        //alert(strXML);
     // document.write(strXML);    
          
        //LOAD THE RETURNED STRING OF XML INTO ANOTHER PARSER AND return;
        if(ie)
        { 
           xmlList = new ActiveXObject("Microsoft.XMLDOM"); 
           xmlList.async=false;      
           xmlList.loadXML(strXML);           
        }        
        else
        {//alert('loading' + strXML);
            parser = new DOMParser();
            xmlList = parser.parseFromString(strXML,"text/xml");            
        } 
              
        return xmlList;
}




//following 4 items determine the bottom of the window and sets the dropdown list to appear 
//either above or below a textbox 
//call onfocus, onkeyup  
function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;}
function pageWidth() {
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null; 
} 
function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} 
function posBottom() {return posTop()+pageHeight();} 
function PositionListDiv(textboxID, ListDivID)
 {      
    var txtTop = document.getElementById(textboxID).style.top;
    var txtHeight = document.getElementById(textboxID).style.height;
    //get the actual length of the div after it's filled with content
    var divLength = parseInt(document.getElementById(ListDivID).offsetHeight);
    
    var divBottom = divLength + parseInt(txtHeight) + parseInt(txtTop) + 10;
    
    txtTop = txtTop.substring(0,txtTop.indexOf("px"));
    txtHeight = txtHeight.substring(0,txtHeight.indexOf("px"));
    //be sure there is something in the height attr.
    if (txtHeight == "")
    {
        txtHeight = 12;
    }
    
    var NewTop = 0;    
    if(divBottom < posBottom())
    {//alert("fits ");   
        NewTop = parseInt(txtTop) + parseInt(txtHeight) + 2;  
    }
    else
    {//alert("no fit");
        NewTop = ( parseInt(txtTop) - divLength );
    }
     document.getElementById(ListDivID).style.top = NewTop + "px";
}
//---- /div positioning  --------------/


testBrowser();
 

