//--Xeinet(c) 2006 Custom Ajax Library All rights Reserved
function createXHR() { var xhrObj; if (window.XMLHttpRequest) {try {xhrObj=new XMLHttpRequest();}catch(e){xhrObj = null;}}else if (window.createRequest){try {xhrObj = window.createRequest();}catch(e){xhrObj=null;}}else if(window.ActiveXObject){try{xhrObj=new ActiveXObject("Msxml2.XMLHTTP");} catch(e){try{xhrObj = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){xhrObj = null;}}}return xhrObj;}
//-- Executes Ajax against url, pass in query strings, as params, and include the name of a function 
//-- To Callback E.g ("http://xeinet.com","t=1&w=hello+World",showResult);
//-- Expects a JSON encoded respose - so wil rehyrate a object - if we just want a single value use the function runAjaxNOJSON(url,qs,callback)
function runAjax(url,qs,callback,divObj,isPost) {
      var xhr = createXHR();
      showAjaxActivity(true);
      /* Inner function to bind handler and create closure */
      function bindCallback() { 
            
          if (xhr.readyState == 4) {
              try {
                  if (xhr.status == 200){
                    if(xhr.responseText == "NO_SESSION"){
                     window.location.href = 'logon.aspx';
                    }else{callback( eval('(' +  xhr.responseText + ')'),divObj );showAjaxActivity(false);}
                  }else{
                     showError("Status error:" + xhr.status)
                     callback(null,divObj);
                     showAjaxActivity(false);
                  }
             } catch (e) { showError(e);callback(null);
            }
         }
     }
                
     if (xhr) {
         try {
             xhr.onreadystatechange = bindCallback;
             if(isPost){
                 xhr.open('POST', url + '?aj=1&u=' + (Math.random()*Math.random()), true);
                 xhr.send(qs);
               }else{
                 xhr.open('GET', url + '?aj=1&u=' + (Math.random()*Math.random()) + '&' + qs, true);
                 xhr.send(null);
               }
         }catch (e) {
             showError(e);callback(null,divObj);
        }
     }
 } //eof runAjax
 
 //--Runs ajax returning a literal rather than a json object
 function runAjaxNOJSON(url,qs,callback,divObj,isPost,displayError) {
      var xhr = createXHR();
      /* Inner function to bind handler and create closure */
      function bindCallback() {
          if (xhr.readyState == 4) {
              try {
                  if (xhr.status == 200){
                        if(xhr.responseText == "NO_SESSION"){
                         window.location.href = 'logon.aspx';
                        }else{callback( xhr.responseText,divObj);showAjaxActivity(false);}
                    }
                 else{
                      if(displayError){showError("Status: " + xhr.status);}
                      callback(null,divObj);
                      showAjaxActivity(false);
                     }
             } catch (e) { if(displayError){showError(e);} callback(null,divObj);
            }
         }
     }
                
     if (xhr) {
         try {
             xhr.onreadystatechange = bindCallback;
             if(isPost){
                 xhr.open('POST', url + '?aj=1&u=' + (Math.random()*Math.random()), true);
                 xhr.send(qs);
               }else{
                 xhr.open('GET', url + '?aj=1&u=' + (Math.random()*Math.random()) + '&' + qs, true);
                 xhr.send(null);
               }
         } catch (e) { if(displayError){showError(e);} callback(null,divObj);
        }
     }
 } //eof runAjax

//-- Error handler for Ajax - customize if required
function showError(e){ 
alert('clarete-0245:Error retrieving Ajax data:'+ e); 
showAjaxActivity(false);}

function showAjaxActivity(show){
//TODO: show ajax activity
}

function getPage(p){  runAjax('bordeaux2009.aspx','p=' + p,getPageResult);}
function getPage2010(p){ runAjax('bordeaux2010.aspx','p=' + p,getPageResult);}

function getPageResult(result) {
document.getElementById("contentDiv").innerHTML = result.html;
document.getElementById("pagerDiv").innerHTML = result.pagerhtml;
//alert(result.html);

}
