function lvGet(strID, strSort) {
   if (strSort) {
      if (strSort == document.getElementById(strID + "_sort").value)
         document.getElementById(strID + "_sortType").value = (document.getElementById(strID + "_sortType").value == "asc" ? "desc" : "asc");
      else
         document.getElementById(strID + "_" + document.getElementById(strID + "_sort").value).style.backgroundImage = 'none';
      document.getElementById(strID + "_" + strSort).style.backgroundImage = 'url(/images/img_9_' + document.getElementById(strID + "_sortType").value + '.png)';
      document.getElementById(strID + "_sort").value = strSort;
      document.getElementById(strID + "_start").value = "0";
      document.getElementById(strID + "_moreRows").value = "1";
      document.getElementById(strID + "_count").value = "0";
   }            
   if (document.getElementById(strID + "_moreRows").value == "1")
      document.getElementById(strID + "_iframe").src = '/includes/get' + strID + '.php?start=' + document.getElementById(strID + "_start").value + '&sort=' + document.getElementById(strID + "_sort").value + '&type=' + document.getElementById(strID + "_sortType").value;
}

function lvSearch(strID) {
   if (document.getElementById(strID + "_r1")) {
      Search = document.getElementById(strID + "_search").value.toUpperCase();
      divTop = document.getElementById(strID + "_r1").offsetTop;
      for (i = 1; i < document.getElementById(strID + "_count").value; i++) {
          if (document.getElementById(strID + "_r" + i).innerHTML.toUpperCase().indexOf(Search) != -1) {
             document.getElementById(strID + "_scroll").scrollTop = document.getElementById(strID + "_r" + i).offsetTop - divTop;
             break;
          }
      }
   }
}


// Send HTTP request and evaluate the results
//--------------------------------------------
function SendHTTPRequest(strURL, strPost, strSession) { 
   var http; 
   try { http = new ActiveXObject('Msxml2.XMLHTTP'); }
   catch (e) {
      try { http = new ActiveXObject('Microsoft.XMLHTTP'); }
      catch (e2) {
         try { http = new XMLHttpRequest(); }
         catch (e3) { http = false; }
      }
   }
  
   http.onreadystatechange = function() {
      if (http.readyState == 4) {
         if (http.status == 200) {
            try { eval(http.responseText); }
            catch (e3) { alert(http.responseText); }
         } else if (http.status > 0)
            alert("An error has occured while communicating with the server\nPlease contact 01842 879108 for assistance.");
      }
   }; 

   strMethod = (strPost ? "POST" : "GET");
   http.open(strMethod, strURL,  true); 
   if (strPost) {
      http.setRequestHeader("Host", "cpv5.thebigspider.co.uk");
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Cookie", "PHPSESSID=" + strSession + "; path=/");
      http.setRequestHeader("Content-length", strPost.length);
   }
   http.setRequestHeader("Connection", "close");
   http.send(strPost); 
} 


// Functions for encoding and decoding data to be passed via a HTTP request
//--------------------------------------------------------------------------
var Url = {
   // public method for url encoding
   encode : function (string) {
      return escape(this._utf8_encode(string)).replace(/\+/g, "\%2b");
   },
 
   // public method for url decoding
   decode : function (string) {
      return this._utf8_decode(unescape(string));
   },
 
   // private method for UTF-8 encoding
   _utf8_encode : function (string) {
//      string = string.replace(/\r\n/g,"\n");
      var utftext = "";
      for (var n = 0; n < string.length; n++) {
         var c = string.charCodeAt(n);
         if (c < 128) {
            utftext += String.fromCharCode(c);
         }
         else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
         }
         else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
         }
 
      }
      return utftext;
   },
 
   // private method for UTF-8 decoding
   _utf8_decode : function (utftext) {
      var string = "";
      var i = 0;
      var c = c1 = c2 = 0;
      while ( i < utftext.length ) {
         c = utftext.charCodeAt(i);
         if (c < 128) {
            string += String.fromCharCode(c);
            i++;
         }
         else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
         }
         else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
         }
      }
      return string;
   }
}


   function MessageBox(strTitle, strMessage, strButtons) {
      // Set message box colour
      strType = strMessage.substr(0, 1);
      strClass = (strType == "Y" ? "green" : (strType == "N" ? "red" : (strType == "U" ? "yellow" : 'blue')));
      document.getElementById("message").className = strClass;
      
      // Set the title, message and buttons
      document.getElementById("msgtitle").innerHTML = strTitle;
      document.getElementById("msgmessage").innerHTML = strMessage.substr(1);
      document.getElementById("msgbuttons").innerHTML = strButtons;
   
      // Display the message box
      document.getElementById("overlay").style.display = 'block';
      document.getElementById("message").style.display = 'block';

      // Set message box position in the center
      intLeft = (document.getElementById("overlay").offsetWidth / 2) - (document.getElementById("message").offsetWidth / 2);
      intTop  = (document.getElementById("overlay").offsetHeight / 2) - (document.getElementById("message").offsetHeight / 2);
      document.getElementById("message").style.left = intLeft + "px";
      document.getElementById("message").style.top = intTop + "px";
   }

   function HideMessageBox() {   
      // Hide the message box
      document.getElementById("overlay").style.display = 'none';
      document.getElementById("message").style.display = 'none';
   }

   function checkselect(lvName) {
      if (!lvName) lvName = "";
      intRow = 0;
      while (document.getElementById(lvName + "sel" + intRow)) {
         document.getElementById(lvName + "sel" + intRow).checked = document.getElementById(lvName + "check").checked;
         intRow++;
      }
   }

   
   var tabExecute = false;
   function setTab(id) {
      intX = 1;
      while (document.getElementById("tab_" + intX)) {
         document.getElementById("tab" + intX).className = "tabOff";
         document.getElementById("tab_" + intX).style.display = "none";
         intX++;
      }
      document.getElementById("tab" + id).className = "tabOn";
      document.getElementById("tab_" + id).style.display = "block";
      
      if (tabExecute) executeTab(id);
   }


   //
   // Creates a new overlay and displays a message in the center of the screen   
   function messageOpen(strTitle, strBody, strButtons, strClass) {
      var intMinWidth = 400;
      var intMaxWidth = document.documentElement.clientWidth - 100;
      var intMaxHeight = document.documentElement.clientHeight - 250;      

      // Popup framework
      intID = Math.round(Math.random() * 1000);
      strMessageFramework = '<div id="msgOverlay" class="overlay"></div><table cellpadding="0" cellspacing="0" border="0" id="msgPopup"><tbody><tr><td id="a"></td><td id="b"></td><td id="c"></td></tr><tr><td id="d"></td><td id="e"><h1 id="msgTitle"></h1><div id="msgBody"></div><div style="margin-top: 20px; text-align: center" id="msgButtons"></div></td><td id="f"></td></tr><tr><td id="g"></td><td id="h"></td><td id="i"></td></tr></tbody></table>';
      strMessageFramework = strMessageFramework.replace("msgOverlay", "msg" + intID + "Overlay");
      strMessageFramework = strMessageFramework.replace("msgPopup", "msg" + intID + "Popup");
      strMessageFramework = strMessageFramework.replace("msgTitle", "msg" + intID + "Title");
      strMessageFramework = strMessageFramework.replace("msgBody", "msg" + intID + "Body");
      strMessageFramework = strMessageFramework.replace("msgButtons", "msg" + intID + "Buttons");
      strButtons = strButtons.replace(/\[ID\]/g, intID);
      
      // Insert new framework
      var newFramework = document.createElement('div');
      newFramework.setAttribute('id', 'msg' + intID);
      newFramework.innerHTML = strMessageFramework;
      document.body.appendChild(newFramework);
      
      // Set defaults
      document.getElementById("msg" + intID + "Popup").className = strClass;
      document.getElementById("msg" + intID + "Popup").style.position = 'fixed';
      
      // Set text
      document.getElementById("msg" + intID + "Title").innerHTML = strTitle;
      document.getElementById("msg" + intID + "Body").innerHTML = strBody;
      document.getElementById("msg" + intID + "Buttons").innerHTML = strButtons;

      // Set minimum width
      if (document.getElementById("msg" + intID + "Body").offsetWidth < intMinWidth) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.width = intMinWidth + 'px';
      }

      // Set maximum width
      if (document.getElementById("msg" + intID + "Body").offsetWidth > intMaxWidth) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.width = intMaxWidth + 'px';
      }

      // Set maximum height
      if (document.getElementById("msg" + intID + "Body").offsetHeight > intMaxHeight) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.height = intMaxHeight + 'px';
         document.getElementById("msg" + intID + "Body").style.width = (document.getElementById("msg" + intID + "Body").offsetWidth + 20) + 'px';
      }
      
      // Center popup in the middle of the screen
      document.getElementById("msg" + intID + "Popup").style.left = (document.documentElement.clientWidth / 2) - (document.getElementById("msg" + intID + "Popup").clientWidth / 2) + 'px';
      document.getElementById("msg" + intID + "Popup").style.top = (document.documentElement.clientHeight / 2) - (document.getElementById("msg" + intID + "Popup").clientHeight / 2) + 'px';
   }

   //
   // Hides the popup on a message, leaving the overlay
   function messageHide(intID) {
      // Hide popup
      if (document.getElementById("msg" + intID)) document.getElementById("msg" + intID + "Popup").style.display = 'none';
   }

   //
   // Closes an existing message   
   function messageClose(intID) {
      // Remove popup
      if (document.getElementById("msg" + intID)) document.body.removeChild(document.getElementById("msg" + intID));
   }

   
   
   
// 10th September 2009   
   
   
   //
   // Creates a new HTTP request
   function HTTPRequest(strHost, strURI, strPost) { 
      var http = false;
      
      try {
         http = new ActiveXObject('Msxml2.XMLHTTP'); 
      } catch (e) {
         try {
            http = new ActiveXObject('Microsoft.XMLHTTP');
         } catch (e2) {
            try {
               http = new XMLHttpRequest();
            } catch (e3) {
               http = false;
            }
         }
      }
     
     if (http) {
         http.onreadystatechange = function() {
            if (http.readyState == 4) {
               if (http.status == 200) {
                  try { 
                     eval(http.responseText);
                  } catch (e3) {
                     alert(http.responseText); 
                     eval(http.responseText);
                  }
               } else if (http.status > 0) {
                  alert("An error has occured while communicating with the server.\nPlease contact 01842 879108 for assistance.");
               }
            }
         }; 

         strMethod = (strPost != "" ? "POST" : "GET");
         http.open(strMethod, strURI,  true); 
         if (strPost) {
            http.setRequestHeader("Host", strHost);
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            if (strSession != "") http.setRequestHeader("Cookie", "PHPSESSID=" + strSession + "; path=/");
            http.setRequestHeader("Content-length", strPost.length);
         }
         http.setRequestHeader("Connection", "close");
         http.send(strPost);
      }
   } 


   // Creates a new overlay and a message framework
   function msgOpen(strTitle, strBody, strButtons, strClass) {
      // Popup framework
      intID = Math.round(Math.random() * 10000);
      strMessageFramework = '<div id="msgOverlay" class="overlay"><img style="padding: 10px; background-color: #fff; border: 1px solid #999" src="/images/wait.gif" width="25" height="25" border="0"></div><form id="msgForm" class="tbsPopup"><div id="msgTitle" class="tbsPopupTitle"></div><div id="msgBody" class="tbsPopupBody"></div><div class="tbsPopupButtons"><div id="msgButtons"></div></div></form>';
      strMessageFramework = strMessageFramework.replace("msgOverlay", "msg" + intID + "Overlay");
      strMessageFramework = strMessageFramework.replace("msgForm", "msg" + intID + "Form");
      strMessageFramework = strMessageFramework.replace("msgTitle", "msg" + intID + "Title");
      strMessageFramework = strMessageFramework.replace("msgBody", "msg" + intID + "Body");
      strMessageFramework = strMessageFramework.replace("msgButtons", "msg" + intID + "Buttons");
      strButtons = strButtons.replace(/\[ID\]/g, intID);
      
      // Insert new framework
      var newFramework = document.createElement('div');
      newFramework.setAttribute('id', 'msg' + intID);
      newFramework.innerHTML = strMessageFramework;
      document.body.appendChild(newFramework);
      
      // Set text
      document.getElementById("msg" + intID + "Title").innerHTML = strTitle;
      document.getElementById("msg" + intID + "Body").innerHTML = strBody;
      document.getElementById("msg" + intID + "Buttons").innerHTML = strButtons;

      return intID;
   }


   //
   // Makes an existing message visible and places it in the center of the screen
   function msgShow(intID) {
      var intMinWidth = 200;
      var intMaxWidth = document.documentElement.clientWidth - 100;
      var intMaxHeight = document.documentElement.clientHeight - 150;      

      // Show message
      document.getElementById("msg" + intID + "Form").style.display = 'table';

      // Set minimum width
      if (document.getElementById("msg" + intID + "Body").offsetWidth < intMinWidth) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.width = intMinWidth + 'px';
      }

      // Set maximum width
      if (document.getElementById("msg" + intID + "Body").offsetWidth > intMaxWidth) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.width = intMaxWidth + 'px';
      }

      // Set maximum height
      if (document.getElementById("msg" + intID + "Body").offsetHeight > intMaxHeight) {
         document.getElementById("msg" + intID + "Body").style.display = 'block';
         document.getElementById("msg" + intID + "Body").style.overflow = 'auto';
         document.getElementById("msg" + intID + "Body").style.height = intMaxHeight + 'px';
         document.getElementById("msg" + intID + "Body").style.width = (document.getElementById("msg" + intID + "Body").offsetWidth) + 'px';
      }

      // Center message in the middle of the screen
      document.getElementById("msg" + intID + "Form").style.left = '50%';
      document.getElementById("msg" + intID + "Form").style.marginLeft = '-' + (document.getElementById("msg" + intID + "Form").offsetWidth / 2) + 'px';
      document.getElementById("msg" + intID + "Form").style.top = '50%';
      document.getElementById("msg" + intID + "Form").style.marginTop = '-' + (document.getElementById("msg" + intID + "Form").offsetHeight / 2) + 'px';
   }

      
   //
   // Hides an existing message, leaving the overlay
   function msgHide(intID) {
      if (document.getElementById("msg" + intID)) document.getElementById("msg" + intID + "Form").style.display = 'none';
   }


   //
   // Closes an existing message
   function msgClose(intID) {
      if (document.getElementById("msg" + intID)) document.body.removeChild(document.getElementById("msg" + intID));
   }   
   
   
   //
   // Finds the absolute position of an element
   function findPos(obj) {
      var curleft = curtop = 0;
      if (obj.offsetParent)
         do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
         } while (obj = obj.offsetParent);
      return [curleft,curtop];
   }

   
   function round(dblNumber, intDecimals) {
      return Math.round(dblNumber * parseFloat("100000000".substr(0, intDecimals + 1))) / parseFloat("100000000".substr(0, intDecimals + 1));
   }
   
   // Trim whitespace
   function trim(str) {
      str = str.replace(/^\s+/, '');
      for (var i = str.length - 1; i >= 0; i--) {
         if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
         }
      }
      return str;
   }
