/* ------------------------- */
/*  XML Loading              */
/* ------------------------- */
// global flag
var isIE = false;
// global request and XML document objects
var req;
var xmlhttp;

function loadXML(url) 
{
    xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) 
	{
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null) 
	{
		xmlhttp.onreadystatechange = processXML;
		xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
	} 
	else 
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

// handle onreadystatechange event of req object
function processXML() {
    // only if req shows "loaded"
    if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
            parseXML();
         } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
         }
    }
}



/*****************************/
/* My Files Control          */
/*****************************/

function parseXML() {
    var action = xmlhttp.responseXML.getElementsByTagName("action")[0].firstChild.data;
    var result = xmlhttp.responseXML.getElementsByTagName("result")[0].firstChild.data;
    
    switch(action) 
    {
       case "addpage":
            if(result == "Success") 
            {
                alert("Page added to Data Cart");
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 + 1;
            } 
            else 
            {
                alert(result);
            }
            break;
    
        case "removepage":
            if(result == "Success") 
            {
                alert("Page removed from Data Cart");
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 - 1;
                this.document.aspnetForm.submit();
            }
            else 
            {
                alert(result);
            }
            break;
    
        case "adddownload":
            if(result == "Success") 
            {
                alert("Download added to Data Cart");
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 + 1;
            } 
            else 
            {
                alert(result);
            }
            break;
    
        case "removedownload":
            if(result == "Success") 
            {
                alert("Downloads removed from Data Cart");
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = 0;
                this.document.aspnetForm.submit();
            } 
            else 
            {
                alert(result);
            }
            break;
            
        case "removecompany":
            if(result == "Success") 
            {
                alert("Company removed from Data Cart");
                //document.getElementById("fileCompanies").innerHTML = document.getElementById("fileCompanies").innerHTML*1 - 1;
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 - 1;    
                this.document.aspnetForm.submit();
            } 
            else 
            {
                alert(result);
            }
            break;
          
         case "removeproperty":
            if(result == "Success") 
            {
                alert("Property removed from Data Cart");
                //Remove 1 property from the data cart total
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 - 1;    
                this.document.aspnetForm.submit();
                
            } 
            else 
            {
                alert(result);
            }
            
         case "removereport":
            if(result == "Success") 
            {  
                alert("Report removed from Data Cart");
                //Remove 1 property from the data cart total
                document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML = document.getElementById("secondarySidebarDatacartItemsSaved").innerHTML*1 - 1;    
                this.document.aspnetForm.submit();
                
            } 
            else 
            {
                alert(result);
            }
            
            break;
    }
}


//var imgFileIt = "/images/AddToReport.png";
var imgFileIt = "/images/AddToReport2a.jpg";
var imgFileItDownload = "/images/Plus.jpg";
//var imgRemove = "/images/Remove.png";
var imgRemove = "/images/remove2a.jpg";
var imgRemoveDownload = "/images/Minus.jpg";

function toggleFileItMF(img) 
{
    if(img != null) 
    {
        if(img.src.indexOf(imgFileIt) > -1) 
        {
            img.src = imgRemove;
            img.title = "Click to remove this Page from your stored files.";
        } 
        else if(img.src.indexOf(imgFileItDownload) > -1) 
        {
            img.src = imgRemoveDownload;
            img.title = "Click to remove this Report from your stored files.";
        } 
        else if(img.src.indexOf(imgRemove) > -1) 
        {
            //img.src = imgFileIt;
            //img.title = "Click to add this Page from your stored files.";
        } 
        else 
        {
            //img.src = imgFileItDownload;
            //img.title = "Click to add this Download from your stored files.";
        }
     
    }
}

function clickFileItMF(img, area, id) 
{
    if(img == null || img.src.indexOf(imgFileIt) > -1 || img.src.indexOf(imgFileItDownload) > -1) 
    {
        loadXML('ajax.aspx?add' + area + '=' + id);
    } 
    else 
    {
        loadXML('ajax.aspx?remove' + area + '=' + escape(id));
    }
    
    toggleFileItMF(img);
    return false;
}


function ReorderUp(area, id) 
{
    switch(area) 
    {
        case "page":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderUpID').value = id;
            break;
        case "download":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderUpDownload').value = id;
            break;
        case "property":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderUpProperties').value = id;
            break;
        case "company":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderUpCompanies').value = id;
            break;
            
    }
    document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_Reorder').click();
}

function ReorderDown(area, id) 
{
    switch(area) 
    {
        case "page":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderDownID').value = id;
            break;
        case "download":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderDownDownload').value = id;
            break;
        case "property":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderDownProperties').value = id;
            break;
         case "company":
            document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_ReorderDownCompanies').value = id;
            break;
    }
    
    document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_Reorder').click();
}




function PrintToPDF() 
{
    //document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_PagesToPrint').value = "";
    //i = 0;
    
    //while(document.getElementById('MyFiles_ShowInPDF' + i) != null) 
    //{
    //    if(document.getElementById('MyFiles_ShowInPDF' + i).checked) 
    //    {
    //        document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_PagesToPrint').value += i + ",";
    //    }
    //    
    //    i++;
    //}
    
    //document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_DownloadsToPrint').value = "";
    //i = 0;
    
    //while(document.getElementById('MyFiles_DownloadInPDF' + i) != null) {
    //
    //    if(document.getElementById('MyFiles_DownloadInPDF' + i).checked) {
    //        document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_DownloadsToPrint').value += i + ",";
    //    }
    //    
    //    i++;
    //}
  
    document.getElementById('ctl00_ContentPlaceHolder1_con_MyFiles_PrintToPDF').click();
   
}
