﻿  // MULTIPLE XMLHttpRequest FrameWork [BEGIN]
  // Author: Cha
  var maxCon = 10; // maximum connection
  var tailCon = 1; // initial tail
  var HTTPCon = new Array; // array of connection

  // init connection array
  for(i=1;i<=maxCon;i++)
  {
   HTTPCon[i]=false;
  }

  // function to find one available connection
  // either find used empty or create new if available
  function fine1Con()
  {
   var i;

   // try to find 1 used empty (completed)
   for(i=1;i<tailCon;i++)
   {
    if(HTTPCon[i]) {
     if(HTTPCon[i].readyState==0 || HTTPCon[i].readyState==4)
      return i ; // return empty one
    }
   }

   // if can not 1 used empty, pick 1 new
   if(tailCon<=maxCon)
   {  

    // crate new object
    if (window.XMLHttpRequest) {
     HTTPCon[tailCon] = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
     HTTPCon[tailCon] = new ActiveXObject("Microsoft.XMLHTTP");
    }   

    // if new object was created successfully
    if(HTTPCon[tailCon])
    {
     i=tailCon; 
     tailCon=tailCon+1;
     return i; // increase the tail of stack and exit
    } else {
     return false; // new object was failed (browser may not be ajax anable)
    }
   } else {
    return false; // maximum connection reached
   }

  }

  function getXMLData(dataSource,divID,HTMLOnly)
  {
    // find me one available Connection
    var gotOne = fine1Con();
    if(gotOne)
    {
     if (HTTPCon[gotOne]) {
      var obj = document.getElementById(divID);
      HTTPCon[gotOne].open("GET", dataSource);
      HTTPCon[gotOne].onreadystatechange = function() {
       if (HTTPCon[gotOne].readyState == 4 && HTTPCon[gotOne].status == 200) {
       var result = HTTPCon[gotOne].responseText;
       var tabnumber = 0
       if(HTMLOnly>0)
       {
        // make sure that this is the correct format   
        t1=result.indexOf('">');
        t2=result.indexOf('</string>');
        if((t1>0)&&(t2>0)&&(t2>t1)){
         result=cleanGTLT(result.substring(t1+2,t2));
         t1=result.indexOf('#A#');
         t2=result.indexOf('#B#');
         if((t1>0)&&(t2>0)&&(t2>t1)){
          tabnumber=result.substring(t1+3,t2);
          result=result.substring(0,t1);
         }
        }
       } 
       if(obj){ obj.innerHTML = result; }  
       if(aftergetXMLData) {
        aftergetXMLData(tabnumber,result); // if there is aftergetXMLData function, call it
       }
       } 
      }
      HTTPCon[gotOne].send(null);
     }
    } else {
      // All Connection full 
     alert('Connection Full');
    }
  }
  
  function postXMLData(dataSource,divID,dataPost,HTMLOnly)
  {
    // find me one available Connection
    var gotOne = fine1Con();
    if(gotOne)
    {
     if (HTTPCon[gotOne]) {
      var obj = document.getElementById(divID);
      HTTPCon[gotOne].open('POST', dataSource);
      // need this content type
      HTTPCon[gotOne].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      HTTPCon[gotOne].onreadystatechange = function() {
       if (HTTPCon[gotOne].readyState == 4 && HTTPCon[gotOne].status == 200) {
        var result = HTTPCon[gotOne].responseText;
        if(HTMLOnly>0)
        {
         // make sure that this is the correct format   
         t1=result.indexOf('">');
         t2=result.indexOf('</string>');
         if((t1>0)&&(t2>0)&&(t2>t1)){
          result=cleanGTLT(result.substring(t1+2,t2));
         }
        }        
        obj.innerHTML = result; 
        if(afterpostXMLData) {
         afterpostXMLData(); // if there is aftergetXMLData function, call it
        }        
        }
      }
      HTTPCon[gotOne].send(dataPost);
     }
    } else {
      // All Connection full 
     alert('Connection Full');
    }
  }
  
  function cleanGTLT(s)
  {
   while (s.indexOf('&gt;')>-1)
   {
     s=s.replace('&gt;','>');
   }
   while (s.indexOf('&lt;')>-1)
   {
     s=s.replace('&lt;','<');
   }
   return s;   
  }

function updateHelpfulness(ReviewID, isHelpful) {

    var data = "{ReviewID:" + ReviewID + ",Helpfulness:" + isHelpful + "}";

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/ws/Ajax/Review.asmx/ReviewHelpfulness",
        data: data,
        dataType: "json",
        async: false,
        success: function (commenthelpful) {
            alert(commenthelpful.d);
        }
    });

}

function loadPage(url, containerid) {
    var loadedobjects = ""
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject) { // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        }
        catch (e) {
            try {
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e) { }
        }
    }
    else
        return false
    page_request.onreadystatechange = function() {
        loadpage(page_request, containerid)
    }
    page_request.open('GET', url, true)
    page_request.send(null)
}

function loadpage(page_request, containerid) {
    if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1))
        document.getElementById(containerid).innerHTML = page_request.responseText
}

function loadobjs() {
    if (!document.getElementById)
        return
    for (i = 0; i < arguments.length; i++) {
        var file = arguments[i]
        var fileref = ""
        if (loadedobjects.indexOf(file) == -1) { //Check to see if this object has not already been added to page before proceeding
            if (file.indexOf(".js") != -1) { //If object is a js file
                fileref = document.createElement('script')
                fileref.setAttribute("type", "text/javascript");
                fileref.setAttribute("src", file);
            }
            else if (file.indexOf(".css") != -1) { //If object is a css file
                fileref = document.createElement("link")
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", file);
            }
        }
        if (fileref != "") {
            document.getElementsByTagName("head").item(0).appendChild(fileref)
            loadedobjects += file + " " //Remember this object as being already added to page
        }
    }
}

function openQV(findControl, qvURL, leftcoord, topcoord) {
    var oWnd = $find(findControl);
    oWnd.setUrl(qvURL);
    oWnd.moveTo(leftcoord, topcoord);
    oWnd.show();
}
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}








