﻿/* gets the window size for dynamic div positioning */
function getWindowSize(p) {  // true for parent
   var myWidth = 0, myHeight = 0;
   var mySize=new Array;// = 0, myHeight = 0;
   if (p) { // called from child page (uploading form)
      if( typeof( parent.window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = parent.window.innerWidth;
        myHeight = parent.window.innerHeight;
      } else if( parent.document.documentElement && ( parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = parent.document.documentElement.clientWidth;
        myHeight = parent.document.documentElement.clientHeight;
      } else if( parent.document.body && ( parent.document.body.clientWidth || parent.document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = parent.document.body.clientWidth;
        myHeight = parent.document.body.clientHeight;
      }
   } else { // called from parent page (calling page)
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
   }
   mySize.width=myWidth;
   mySize.height=myHeight;
  
   return mySize;
}
    
/* dynamically create div layer pop-ups (upload form and wait animation) */
function createDiv() {
   /* upload form pop-up */
   var h = "550px";
   var w = "1000px";
   dv = document.createElement("div");
   dv.setAttribute("id","searchDiv");
   dv.style.position="absolute";  
   dv.style.width=w;
   dv.style.height=h;
   dv.style.zIndex="1";
   dv.style.backgroundColor="#F0F0F0";
   dv.style.borderStyle="double"
   dv.style.visibility="hidden";
   /* need to pass appropriate parameters */
   document.getElementById("body1").appendChild(dv);
   
   /* loading animation */
   /*
   var myTable = document.createElement("table");
   myTable.width = "100%";
   var myTBody = document.createElement("tbody");
   var myRow = document.createElement("tr");
   myRow.height="22";
   myRow.vAlign="bottom";
   var myRow2 = document.createElement("tr");
   var myCell = document.createElement("td");
   var myCell2 = document.createElement("td");
   myCell.align = "center";
   myCell2.align = "center";
   myCell2.style.fontFamily = "Arial";
   myCell2.style.fontWeight = "900";
   myCell2.style.fontSize = 12;
   myCell.innerHTML = "<img src='images/loadingAnimation.gif'/>";
   myCell2.innerHTML = "Processing: please wait...";
   myTable.appendChild(myTBody);
   myTBody.appendChild(myRow);
   myTBody.appendChild(myRow2);
   myRow.appendChild(myCell);
   myRow2.appendChild(myCell2);
   
   var h2 = "50px";
   var w2 = "250px";
   dvWait = document.createElement('div');
   dvWait.setAttribute('id',"waitDiv");
   dvWait.style.position="absolute";  
   dvWait.style.width=w2;
   dvWait.style.height=h2;
   dvWait.style.zIndex=2;
   dvWait.style.backgroundColor="#7AA9DD";
   dvWait.style.borderStyle="double"
   dvWait.style.visibility="hidden";
   dvWait.appendChild(myTable);
   document.getElementById("body1").appendChild(dvWait);
   */
}
      
/* shows and positions the upload form div */
function showSearch() {
   if (document.getElementById("searchDiv")==null) {
       createDiv();
   }
   var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
   var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
   var dsoctop=document.all? iebody.scrollTop : pageYOffset;
   var theSize=getWindowSize(false); // // called from main form
   
   var upl = document.getElementById("searchDiv");
   //upl.style.pixelLeft=((theSize.width/2)-(parseInt(upl.style.width.replace("px",""))/2))+dsocleft;
   //upl.style.pixelTop=((theSize.height/2)-(parseInt(upl.style.height.replace("px",""))/2))+dsoctop;
   upl.style.left=(((theSize.width/2)-(parseInt(upl.style.width.replace("px",""))/2))+dsocleft).toString() + "px";
   upl.style.top=(((theSize.height/2)-(parseInt(upl.style.height.replace("px",""))/2))+dsoctop).toString() + "px";
 //  var param = "?cdtid="+document.getElementById("hdnDistrict").value+"&cltype="+document.getElementById("hdnLibType").value+"&caddusr="+document.getElementById("hdnAddedBy").value;
 //  var param = "?cdtid=2&cltype=Forms&caddusr=72";
   upl.innerHTML="<iframe id='searchframe' src='Search.aspx' width='100%' height='100%'/>";
   upl.style.visibility="visible";
}
  
var t; // global variable for timer
/* checks the iframe for error from server (page can not be displayed) */
function checkForError () {
   t = setTimeout(check,500);
}

/* checks to see if upload had an unhandled server error */  
function check() {
   try {
      var v = window.frames["searchframe"].document.getElementById("DDListStates");
   } catch(err) {
      v==null;
   }
   if (v==null) { // if error then reset
      clear();
      document.getElementById("waitDiv").style.visibility="hidden";
      /* have to reset parameters */
    //  var param = "?cdtid="+document.getElementById("hdnDistrict").value+"&cltype="+document.getElementById("hdnLibType").value+"&caddusr="+document.getElementById("hdnAddedBy").value;
    //  var param = "?cdtid=2&cltype=Forms&caddusr=72";
      document.getElementById("searchframe").src = "Search.aspx";
      alert("An error occurred during search.");
   }
}

/* clears the timer that we use to check for server errors */
function clear() {
   clearTimeout(t);
}

/* clears values in search form prior to closing form */
function closeDiv() {
   
   var iframeEl = document.getElementById('searchframe');
   
   if ( iframeEl.contentDocument ) { // DOM
        iframeEl.contentDocument.getElementById("DDListStates").selectedIndex=0;
        iframeEl.contentDocument.getElementById("DDListCounties").selectedIndex=0;
        iframeEl.contentDocument.getElementById("DDListCats").selectedIndex=0;
        iframeEl.contentDocument.getElementById("DDListSubCats").selectedIndex=0;
        iframeEl.contentDocument.getElementById("Train").innerText = "";
        iframeEl.contentDocument.getElementById("listing").src = "Search.htm";
   } else if ( iframeEl.contentWindow ) { // IE win
        iframeEl.contentWindow.document.getElementById("DDListStates").selectedIndex=0;
        iframeEl.contentWindow.document.getElementById("DDListCounties").selectedIndex=0;
        iframeEl.contentWindow.document.getElementById("DDListCats").selectedIndex=0;
        iframeEl.contentWindow.document.getElementById("DDListSubCats").selectedIndex=0;
        iframeEl.contentWindow.document.getElementById("Train").innerText = "";
        iframeEl.contentWindow.document.getElementById("listing").src = "Search.htm";
   }

   document.getElementById("searchDiv").style.visibility="hidden";
   location.reload();
}

/* dynamically called method from child form backing code class CloseWait.cs - clears timer and hides loading animation */
function cwParent() {
   clear();
   document.getElementById("waitDiv").style.visibility="hidden";
}

/* shows the loading animation while file is being processed and launches check error timer for server errors */
function showWait() {
   /* gets any scrolling offsets so we can keep pop ups in center of display area */
   var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
   var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
   var dsoctop=document.all? iebody.scrollTop : pageYOffset;
   
   var theSize=getWindowSize(true); // called from child - gets parents window dimensions
   var wait = document.getElementById("waitDiv");
   wait.style.pixelLeft=((theSize.width/2)-(parseInt(wait.style.width.replace("px",""))/2))+dsocleft;
   wait.style.pixelTop=((theSize.height/2)-(parseInt(wait.style.height.replace("px",""))/2))+dsoctop;
   wait.style.visibility="visible";
   checkForError();
} 

/* validates fields on loading form prior to processing data - if passes validation launches loading animation */   
function validateFields() {
   if (window.frames["uplframe"].document.getElementById("FileUpload1").value==null ||
      window.frames["uplframe"].document.getElementById("FileUpload1").value=="" ||
      window.frames["uplframe"].document.getElementById("descTxt")==null ||
      window.frames["uplframe"].document.getElementById("descTxt").value=="" ||
      window.frames["uplframe"].document.getElementById("DropDownList1").selectedIndex==0
      ) {
      alert("Please fill all three fields of data appropriately.");   
      return false;
   }
   showWait();
   return true;
}
