﻿// AJAX JScript for File Zip Archiver

// Sample Use
// <a href='http://webdev1/bgs/bulletin\bulletin801.pdf'>
// <span class='bldem'>Bulletin </span><span class='bld'>Volume 01, dated 30/01/2008</span> <span class='smlnum'>(1.13 MB)</span> 
//  <span class='xsml' id='file27'>
//   <a href='#' onclick="Archiver('../ajax/archiver.aspx?func=add','E:|public_html|BGS|bulletin|bulletin801.pdf=1125706','file27'); return false;" />   <img src='../images/zip.png' alt='Add to zip' class='img'/></a>
//  </span>
// </a>

var xmlHttp;
var eleID;
var iNum = 0;

function Archiver(incomingURL, fileInfo, ID) {

    eleID = ID;

    document.getElementById(eleID).innerHTML = "<span class='Blue'>wait...</span>";

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return false;
    }

    incomingURL = incomingURL + "&file=" + fileInfo;

    //alert(fileInfo);
    // Either has: func=add or func=remove
    if (incomingURL.indexOf("func=add") != -1) iNum++;
    if (iNum > 0) document.getElementById("files").innerHTML = "<a href='../source/zipArchiver.aspx' title='You have file(s) ready to download in your zip archive' /><img src='../images/zip.png' alt='You have file(s) ready to download in your zip archive' class='nobdr' /></a>";

    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", incomingURL, true);
    xmlHttp.send(null);

}

function stateChanged() {
    if (xmlHttp.readyState == 4) {
        document.getElementById(eleID).innerHTML = xmlHttp.responseText;
    }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
