﻿//This JS File got messy
function GetXmlHttpObject()
{
    var ajaxCall=null;
    
    try
    {
        ajaxCall = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            ajaxCall = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            ajaxCall = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    return ajaxCall;
}

function IPAddrRdoClick(value)
{
    document.getElementById('IP_All').checked = false;
    document.getElementById('IP_Remote').checked = false;
    document.getElementById('IP_Campus').checked = false;
    
    if(value == 1)
    {
        document.getElementById('IP_All').checked = true;
    }
    else if(value == 2)
    {
        document.getElementById('IP_Remote').checked = true;
    }
    else
    {
        document.getElementById('IP_Campus').checked = true;
    }
    
    {
        new Ajax.Request
        (
            'AJAX.aspx', 	
            {
                method: 'get',
                parameters:'r=' + Math.random() + '&function=IP_DDL_Switch&mode=' + value,
                onSuccess:ddlSwitch,
                onFailure:errFunc
            }
        );
    }
}

function IPAddrRdoClickConsortia(value)
{
    document.getElementById('IP_All').checked = false;
    document.getElementById('IP_Remote').checked = false;
    document.getElementById('IP_Campus').checked = false;
    
    if(value == 1)
        document.getElementById('IP_All').checked = true;
    else if(value == 2)
        document.getElementById('IP_Remote').checked = true;
    else
        document.getElementById('IP_Campus').checked = true;
        
    {
        new Ajax.Request
        (
            'AJAX.aspx', 	
            {
                method: 'get',
                parameters:'r=' + Math.random() + '&function=IP_DDL_Switch_Consortia&mode=' + value,
                onSuccess:ddlSwitchConsortia,
                onFailure:errFunc
            }
        );
    }
}

function InstitutionClick()
{
    {
        new Ajax.Request
        (
            'AJAX.aspx', 	
            {
                method: 'get',
                parameters:'r=' + Math.random() + '&function=SwitchToAccounts',
                onSuccess:ddlSwitchConsortiaAccounts,
                onFailure:errFunc
            }
        );
    }
}

function ddlSwitch(t)
{
    var resp = t.responseText;
    RepopulateDDL(resp);
}

function ddlSwitchConsortia(t)
{
    var resp = t.responseText;
    RepopulateDDLConsortia(resp);
}

function ddlSwitchConsortiaAccounts(t)
{
    var resp = t.responseText;
    RepopulateDDLConsortiaAccounts(resp);
}

function RepopulateDDL(items)
{
    var res = items.split(',');
    var ddl = document.getElementById('lstChoiceIP');
    
    ClearDropDownList(ddl);
    AddNewListOption(ddl,'All','All');
    
    for(i = 0; i < res.length; i++)
    {
        AddNewListOption(ddl,res[i],res[i]);
    }
    ddl.options[ddl.options.length - 1] = null;
    
    //Refill in stats
    var dateFrom;
    var dateTo
    if(document.getElementById('rboShowAll').checked)
    {
        dateFrom = document.getElementById('ddlMonthFrom').options[0].value;
        dateTo = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').options.length - 1].value;
    }
    else
    {
        dateFrom = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
        dateTo = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    }
    
    GetStatsForMonthRange(dateFrom,dateTo,FindCookieValue('eBooks_InstitutionID'),'','0',GetAllIPs(document.getElementById('lstChoiceIP')),SelectedIPs(document.getElementById('lstChoiceTitle')));
}

//Fills in list box with consortium IPs
function RepopulateDDLConsortia(items)
{
    var res = items.split(',');
    var ddl = document.getElementById('lstChoiceIP');
    
    ClearDropDownList(ddl);
    AddNewListOption(ddl,'All','All');
    
    for(i = 0; i < res.length; i++)
    {
        AddNewListOption(ddl,res[i],res[i]);
    }
    ddl.options[ddl.options.length - 1] = null;
    
    //Refill in stats
    var dateFrom;
    var dateTo
    if(document.getElementById('rboShowAll').checked)
    {
        dateFrom = document.getElementById('ddlMonthFrom').options[0].value;
        dateTo = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').options.length - 1].value;
    }
    else
    {
        dateFrom = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
        dateTo = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    }
}

//Fills in the list box with Consortium Account Names
function RepopulateDDLConsortiaAccounts(items)
{
    var res = items.split('|');
    var ddl = document.getElementById('lstChoiceIP');
    
    ClearDropDownList(ddl);
    AddNewListOption(ddl,'All','All');
    
    var item = '';
    var School = '';
    var SchoolID = '';
    
    for(i = 0; i < res.length; i++)
    {
        item = res[i].split(',');
        School = item[0];
        SchoolID = item[1];
        
        AddNewListOption(ddl,School,SchoolID);
    }
    
    ddl.options[ddl.options.length - 1] = null;
}

function ClearDropDownList(thelist)
{
    for(i = 0; i < thelist.options.length; i++)
    {
        thelist.options.length = 0;
    }
}

function AddNewListOption(itemlist,text,value)
{
    var optn = document.createElement('option');
    optn.text = text;
    optn.value = value;
    itemlist.options.add(optn);
}

function GetStatsForMonthRange(rng1,rng2,InstID,Sort,Direction,IPS,ISBN)
{
    var xmlHttp = GetXmlHttpObject();
    var Range_1 = rng1.split('/'), Range_2 = rng2.split('/');
    var myCookies = document.cookie.split(";");
    var statValue = '0';
    var perferedCookie_Mode;
    
    //Find out cookie value
    for(i = 0; i < myCookies.length; i++)
    {
        if(myCookies[i].indexOf('StatMode') >= 0)
        {
            perferedCookie_Mode = myCookies[i].split("=");
            statValue = perferedCookie_Mode[1];
        }
    }
    
    //If the user has selected all IP values, get all of them
    if(!IPS)
    {
        IPS = GetAllIPs(document.getElementById('lstChoiceIP'));
    }
    
    if(statValue == 'COUNTER')
    {
        statValue = '1';
    }
    else
    {
        statValue = '0';
    }
    //End Cookie Code
    
    if(xmlHttp == null)
    {
        alert('Your Browser Does Not Support AJAX');
        return false;
    }
    
    //If the start date is later than the end date, the selection is invalid
    if(((parseInt(Range_1[1]) * 100) + parseInt(Range_1[0])) > ((parseInt(Range_2[1]) * 100) + parseInt(Range_2[0])))
    {
        alert('invalid range');
        return false;
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById('table_wrapwrap').innerHTML = xmlHttp.responseText;
        }
    }
    
    //xmlHttp.open("GET","AJAX.aspx?function=StatsMonthRng&R1=" + rng1 + "&R2=" + rng2 + "&IID=" + InstID + '&Sort=' + Sort + "&Dir=" + Direction + "&IP=" + escape(IPS) + '&ISBN=' + escape(ISBN)+ '&ICOLC=' + statValue,true);
    //xmlHttp.send(null);
    
    xmlHttp.open('POST','AJAX.aspx',true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send("function=StatsMonthRng&R1=" + rng1 + "&R2=" + rng2 + "&IID=" + InstID + '&Sort=' + Sort + "&Dir=" + Direction + "&IP=" + escape(IPS) + '&ISBN=' + escape(ISBN)+ '&ICOLC=' + statValue);
}

function GetStatsForMonthRange_Switch(rng1,rng2,InstID,Sort,Direction,IPS,ISBN,Mode)
{
    var xmlHttp = GetXmlHttpObject();
    var Range_1 = rng1.split('/'), Range_2 = rng2.split('/');
    var mode_value;
    
    if(xmlHttp == null)
    {
        alert('Your Browser Does Not Support AJAX');
        return false;
    }
    
    if(Mode == 'ICOLC')
    {
        mode_value = '0';
    }
    else
    {
        mode_value = '1';
    }
    
    //If the start date is later than the end date, the selection is invalid
    if(((parseInt(Range_1[1]) * 100) + parseInt(Range_1[0])) > ((parseInt(Range_2[1]) * 100) + parseInt(Range_2[0])))
    {
        alert('invalid range');
        return false;
    }
    
    //If the user has selected all IP values, get all of them
    if(!IPS)
    {
        IPS = GetAllIPs(document.getElementById('lstChoiceIP'));
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById('table_wrapwrap').innerHTML = xmlHttp.responseText;
        }
    }
    
    xmlHttp.open("GET","AJAX.aspx?function=StatsMonthRng&R1=" + rng1 + "&R2=" + rng2 + "&IID=" + InstID + '&Sort=' + Sort + "&Dir=" + Direction + "&IP=" + escape(IPS) + '&ISBN=' + escape(ISBN)+ '&ICOLC=' + mode_value,true);
    xmlHttp.send(null);
}

function GetStatsForMonthRange_MultiMonthPage(rng1,rng2,InstID,Sort,Direction,IPS,ISBN)
{
    var xmlHttp = GetXmlHttpObject();
    var Range_1 = rng1.split('/'), Range_2 = rng2.split('/');
    
    if(xmlHttp == null)
    {
        alert('Your Browser Does Not Support AJAX');
        return false;
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById('table_wrap').innerHTML = xmlHttp.responseText;
        }
    }
    
    //If the user has selected all IP values, get all of them
    if(!IPS)
    {
        IPS = GetAllIPs(document.getElementById('lstChoiceIP'));
    }
    
    xmlHttp.open("GET","AJAX.aspx?function=StatsMonthRngMultiMonth&R1=" + rng1 + "&R2=" + rng2 + "&IID=" + InstID + '&Sort=' + Sort + "&Dir=" + Direction + "&IP=" + escape(IPS) + '&ISBN=' + escape(ISBN),true);
    xmlHttp.send(null);
}

function uncheckRadioButton(ClientID)
{
    document.getElementById(ClientID).checked = false;
}

function MonthFromChange(RangeRadio,MonthFromDDL,MonthToDDL,InstID,IPS,ISBN,ICOLC)
{
    if(document.getElementById(RangeRadio).checked)
    {
        var dateFrom = document.getElementById(MonthFromDDL).options[document.getElementById(MonthFromDDL).selectedIndex].value;
        var dateTo = document.getElementById(MonthToDDL).options[document.getElementById(MonthToDDL).selectedIndex].value;
        GetStatsForMonthRange(dateFrom,dateTo,InstID,'','0',IPS,ISBN);
    }
    else
    {
        return false;
    }
}

function SelectedIPs(ListBox)
{
    var count = ListBox.length;
    var i = 0;
    var ips = '';
    
    if(ListBox[0].selected)
        return false;
        
    for(i = 0; i < count; i++)
    {
        if(ListBox[i].selected)
            ips = ips + ListBox[i].value + ',';
    }
    
    return ips;
}

function GetAllIPs(ListBox)
{
    var count = ListBox.length;
    var ips = '';
    
    for(i = 1; i < count; i++)
    {
        ips = ips + ListBox[i].value + ',';
    }
    
    return ips;
}

function NewIpSelected(rng1,rng2,InstID,Sort,Direction,IPS,ISBN,ICOLC)
{
    if(IPS == 'false')
        return false;
    
    GetStatsForMonthRange(rng1,rng2,InstID,Sort,Direction,IPS,ISBN);
}

function OpenPrintPage(r1,r2,ips,isbn,ICOLC)
{
    //StatMode
    var mode = FindCookieValue('StatMode');
    var statValue;
    
    if(mode == 'COUNTER')
    {
        statValue = '1';
    }
    else
    {
        statValue = '0';
    }
    
    window.open('PrintPage.aspx?type=ebooks&Rng1=' + escape(r1) + '&Rng2=' + escape(r2) + '&I=' + escape(ips) + '&ISBN=' + escape(isbn) + "&mode=" + statValue);
}

function UpdateDldStatus(labelid,D,M,Y)
{
    document.getElementById(labelid).innerHTML = 'Downloaded (' + M + '/' + D + '/' + Y + ')';
}

function errFunc(t)
{
    return false;
}

function ValidateForm1()
{
    alert('im here');
}

function ValidateForm2()
{
    var newUN = document.getElementById('txtNewUName').value;
    var newP1 = document.getElementById('txtNPword1').value;
    var newP2 = document.getElementById('txtNPword2').value;
    var addBar = document.getElementById('txtAddBarcodes').value;
    
    var addIP = document.getElementById('txtAddIP').value;
    var removeIP = document.getElementById('txtRemoveIP').value;
    var addURL = document.getElementById('txtAddURL').value;
    
    if(newP1 == newP2)
    {
        {
            new Ajax.Request
		    (
			    'AJAX.aspx', 	
    			{
	    			method: 'get',
		    		parameters:'r=' + Math.random() + '&function=AccountSettingForm2&NewPassWord=' + newP1 + '&NewUserName=' + newUN + '&addBar=' + escape(addBar) + '&addIP=' + addIP + '&remIP=' + removeIP + '&addURL=' + addURL,
				    onSuccess:ValidateForm2_Success,
			    	onFailure:errFunc
		    	}
	    	);
    	}
	}
	else
	{
	    alert('Passwords Do Not Match');
	}
}

function ValidateForm3()
{
    var pageNumDDL = document.getElementById('ddlResultsNum');
    var sortByDDL = document.getElementById('ddlSortBy');
    var listingDDL = document.getElementById('ddlView');
    var searchDDL = document.getElementById('ddlSearch');
    
    var admin_user = document.getElementById('txtAdminNewUserName').value;
    var admin_pw1 = document.getElementById('txtAdminNewPassword1').value;
    var admin_pw2 = document.getElementById('txtAdminNewPassword2').value;

    if(admin_user.length <= 2 && admin_user != '')
    {
        admin_user = '';
        alert('Username has too few characters');
    }
    
    if(admin_pw1.length <= 2 && admin_pw1 != '')
    {
        admin_pw1 = '';
        admin_pw2 = '';
        alert('Password(s) has too few characters');
    }
    
    if(admin_pw1 == admin_pw2)
    {
        {
            new Ajax.Request
		    (
                'AJAX.aspx',
                {
	        		method: 'get',
		        	parameters:'r=' + Math.random() + '&function=AccountSettingForm3&NumResults=' + pageNumDDL[pageNumDDL.selectedIndex].value + '&SortBy=' + sortByDDL[sortByDDL.selectedIndex].value + '&NewAU=' + admin_user + '&NewAP=' + admin_pw1 + '&List=' + listingDDL[listingDDL.selectedIndex].value + '&Search=' + searchDDL[searchDDL.selectedIndex].value,
			    	onSuccess:ValidateForm3_Success,
                    onFailure:errFunc
		        }
            );
        }
    }
    else
    {
        alert('Passwords Do Not Match');
    }
   
}

function ValidateForm2_Success(t)
{
    var x = t.responseText;
    alert('A Mail Has Been Sent To Request The Change(s)');
}

function ValidateForm3_Success(t)
{
    var x = t.responseText;
    
    if(x == 'false')
    {
        alert('Could not Update Username or Password Information');
    }
    else
    {
        var variables = x.split(';');
        var uName, pWord;
        
        uName = variables[0].split('=');
        pWord = variables[1].split('=');
        
        if(uName.length > 1)
        {
            if(uName[1] != '')
                document.getElementById('lblAdminUsername').innerHTML = '<strong>' + uName[1] + '</strong>';
        }
        
        if(pWord.length > 1)
        {
            if(pWord[1] != '')
                document.getElementById('lblAdminPassword').innerHTML = '<strong>' + pWord[1] + '</strong>';
        }
        
       // alert('Changes Have Been Made');
    }    
}

function Form2Enter(e)
{
    var KeyCode;
    
    if(window.event)
        KeyCode = window.event.keyCode;     //IE
    else
        KeyCode = e.which;     //firefox
        
    if(KeyCode == 13)
    {
        ValidateForm2();
    }
}

function Form3Enter(e)
{
    var KeyCode;
    
    if(window.event)
        KeyCode = window.event.keyCode;     //IE
    else
        KeyCode = e.which;     //firefox
        
    if(KeyCode == 13)
    {
        ValidateForm3();
    }
}

function SwitchMode(Mode,RngMin,RngMax)
{
    {
        new Ajax.Request
		(
            'AJAX.aspx',
            {
	    		method: 'get',
		    	parameters:'r=' + Math.random() + '&function=SwitchMode&Mode=' + Mode,
				onSuccess:errFunc,
                onFailure:errFunc
		    }
        );
    }
    
    var ip = SelectedIPs(document.getElementById('lstChoiceIP'));
    var title = SelectedIPs(document.getElementById('lstChoiceTitle'));
    var from = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
    var to = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    var IID = FindCookieValue('eBooks_InstitutionID');
    
    if(document.getElementById('rboShowAll').checked)
    {
        from = RngMin;
        to = RngMax;
    }
    
    GetStatsForMonthRange_Switch(from,to,IID,'','',ip,title,Mode)
}

function SwitchMode_Consortia(Mode)
{
    {
        new Ajax.Request
		(
            'AJAX.aspx',
            {
	    		method: 'get',
		    	parameters:'r=' + Math.random() + '&function=SwitchMode&Mode=' + Mode,
				onSuccess:errFunc,
                onFailure:errFunc
		    }
        );
    }
    
    ReadConsortiaStatistics();
}

function SwitchBox(mode)
{
    var ip = document.getElementById('aIP');
    var inst = document.getElementById('aInst');
    
    if(mode == 'IP')
    {
        ip.className = 'tabon';
        inst.className = '';
        iprdo.style.display = 'block';
        IPAddrRdoClickConsortia(1);
    }
    else
    {
        ip.className = '';
        inst.className = 'tabon';
        iprdo.style.display = 'none';
        InstitutionClick();
    }
}

function FindCookieValue(CookieName)
{
    var myCookies = document.cookie.split(";");
    var cookieValue = '';
    var perferedCookie;
    
    //Find out cookie value
    for(i = 0; i < myCookies.length; i++)
    {
        if(myCookies[i].indexOf(CookieName) >= 0)
        {
            perferedCookie = myCookies[i].split("=");
            cookieValue = perferedCookie[1];
        }
    }
    //End Cookie Code
    
    return cookieValue;
}

function ExportToExcel()
{
    var ip = SelectedIPs(document.getElementById('lstChoiceIP'));
    var title = SelectedIPs(document.getElementById('lstChoiceTitle'));
    var from = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
    var to = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    var check;
    
    if(document.getElementById('rboShowAll').checked)
    {
        check = 'y';
    }
    else
    {
        check = 'n';
    }
    
    window.open('PrintPage.aspx?type=excel&IPS=' + ip + '&Titles=' + title + '&showall=' + check + '&from=' + from + '&to=' + to);
}

//Login Page Function
function Login(e,Username,Password)
{
    var KeyCode;
    
    if(window.event)
        KeyCode = window.event.keyCode; //IE
    else
        KeyCode = e.which;              //FireFox
        
    if(KeyCode == 13)
    {
        {
            new Ajax.Request
            (
                'AJAX.aspx',
                {
                    method: 'get',
                    parameters:'r=' + Math.random() + '&function=Login&U=' + Username + '&P=' + Password,
                    onSuccess:CheckLogin,
                    onFailure:errFunc
                }
            );
        }
    }
}

function CheckLogin(t)
{
    if(t.responseText == '1')
    {
        if(document.getElementById('rdoEbooks_Standard').checked)
            //window.location = 'BrowseStats.aspx?mode=ICOLC';
            window.location = 'Dashboard.aspx';
        else
            window.location = 'ConsortiaStats.aspx?mode=ICOLC';
    }
}

//Consortia Stat Functions
//Primary and almighty guru of consortia interface reading
function ReadConsortiaStatistics()
{
    //Read the interface first
    var ip = document.getElementById('aIP');
    var insts = document.getElementById('aInst');
    
    var ipMode = (ip.className == 'tabon');
    var instsMode = (insts.className == 'tabon');
    
    //Selected Dates Drop Down
    var from = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
    var to = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    var allDates;
    
    if(document.getElementById('rboShowAll').checked)
        allDates = '1';
    else
        allDates = '0';
    
    var TitleBox = document.getElementById('lstChoiceTitle');
    var IPBox = document.getElementById('lstChoiceIP');
    
    var SelectedTitles = SelectedIPs(TitleBox);
    var SelectedIP = SelectedIPs(IPBox);
    
    if(!SelectedTitles)
        SelectedTitles = '';
    
    if(!SelectedIP)
        SelectedIP = '';
        
    var StatMode = FindCookieValue('StatMode');
    
    if(StatMode == 'ICOLC')
        StatMode = 1;
    else
        StatMode = 0;
        
    if(StatMode == 0)
        document.getElementById('table_wrapwrap').innerHTML = '<iframe src=\'loader.htm\' width=\'748px\' height=\'600px\' frameborder=\'0\' scrolling=\'auto\'></iframe>';
        
    //Time for some AJAX magic
    var xmlHttp = GetXmlHttpObject();
    
    if(xmlHttp == null)
    {
        alert('Your Browser Does Not Support AJAX');
        return false;
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            if(StatMode == 0)
            {
                //If we are getting the COUTNER report, we need to have the table HTML saved so we can reference it inside the iframe
                document.getElementById('consortia').innerHTML = xmlHttp.responseText;
                document.getElementById('table_wrapwrap').innerHTML = '<iframe id=\'CounterIframe\' src=\'ConsortiaCounterIframe.aspx\' width=\'748px\' height=\'600px\' frameborder=\'0\' scrolling=\'auto\'></iframe>';
            }
            else
            {
                document.getElementById('table_wrapwrap').innerHTML = xmlHttp.responseText;
            }
        }
    }
    
    xmlHttp.open('POST','AJAX.aspx',true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send('function=ConsortiaStats&mode=' + StatMode + '&instsMode=' + instsMode + '&allDates=' + allDates + '&DateFrom=' + from + '&DateTo=' + to + '&SelectedTitles=' + SelectedTitles + '&SelectedIP=' + SelectedIP);
}

//Sort Consortia ICOLC Statistics
function ReadConsortiaStatistics_Sort(field)
{
    //Figure out what direction we are going
    var Direction;
    
    //School,Title,Sessions,Searches,Page Hits
    //sortDirSchool,sortDirTitle,sortDirSessions,sortDirSearches,sortDirPage
    
    if(field == 'School')
    {
        Direction = document.getElementById('sortDirSchool').value;
        
        if(document.getElementById('sortDirSchool').value == 'ASC')
            document.getElementById('sortDirSchool').value = 'DESC'
        else
            document.getElementById('sortDirSchool').value = 'ASC'
    }
    else if(field == 'Title')
    {
        Direction = document.getElementById('sortDirTitle').value;
        
        if(document.getElementById('sortDirTitle').value == 'ASC')
            document.getElementById('sortDirTitle').value = 'DESC'
        else
            document.getElementById('sortDirTitle').value = 'ASC'
    }
    else if(field == 'Sessions')
    {
        Direction = document.getElementById('sortDirSessions').value;
        
        if(document.getElementById('sortDirSessions').value == 'ASC')
            document.getElementById('sortDirSessions').value = 'DESC'
        else
            document.getElementById('sortDirSessions').value = 'ASC'
    }
    else if(field == 'Searches')
    {
        Direction = document.getElementById('sortDirSearches').value;
        
        if(document.getElementById('sortDirSearches').value == 'ASC')
            document.getElementById('sortDirSearches').value = 'DESC'
        else
            document.getElementById('sortDirSearches').value = 'ASC'
    }
    else
    {
        Direction = document.getElementById('sortDirPage').value;
        
        if(document.getElementById('sortDirPage').value == 'ASC')
            document.getElementById('sortDirPage').value = 'DESC'
        else
            document.getElementById('sortDirPage').value = 'ASC'
    }
    
    //Read the interface
    var ip = document.getElementById('aIP');
    var insts = document.getElementById('aInst');
    
    var ipMode = (ip.className == 'tabon');
    var instsMode = (insts.className == 'tabon');
    
    //Selected Dates Drop Down
    var from = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
    var to = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    var allDates;
    
    if(document.getElementById('rboShowAll').checked)
        allDates = '1';
    else
        allDates = '0';
    
    var TitleBox = document.getElementById('lstChoiceTitle');
    var IPBox = document.getElementById('lstChoiceIP');
    
    var SelectedTitles = SelectedIPs(TitleBox);
    var SelectedIP = SelectedIPs(IPBox);
    
    if(!SelectedTitles)
        SelectedTitles = '';
    
    if(!SelectedIP)
        SelectedIP = '';
        
    var StatMode = FindCookieValue('StatMode');
    
    if(StatMode == 'ICOLC')
        StatMode = 1;
    else
        StatMode = 0;
        
    //Time for some AJAX magic
    var xmlHttp = GetXmlHttpObject();
    
    if(xmlHttp == null)
    {
        alert('Your Browser Does Not Support AJAX');
        return false;
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById('table_wrapwrap').innerHTML = xmlHttp.responseText;
        }
    }
    
    xmlHttp.open('POST','AJAX.aspx',true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send('function=ConsortiaStatsSort&mode=' + StatMode + '&instsMode=' + instsMode + '&allDates=' + allDates + '&DateFrom=' + from + '&DateTo=' + to + '&SelectedTitles=' + SelectedTitles + '&SelectedIP=' + SelectedIP + '&SortField=' + field + '&Dir=' + Direction);
}

function PrintConsortia(method)
{
    //Read the interface first
    var ip = document.getElementById('aIP');
    var insts = document.getElementById('aInst');
    
    var ipMode = (ip.className == 'tabon');
    var instsMode = (insts.className == 'tabon');
    
    //Selected Dates Drop Down
    var from = document.getElementById('ddlMonthFrom').options[document.getElementById('ddlMonthFrom').selectedIndex].value;
    var to = document.getElementById('ddlMonthTo').options[document.getElementById('ddlMonthTo').selectedIndex].value;
    var allDates;
    
    if(document.getElementById('rboShowAll').checked)
        allDates = '1';
    else
        allDates = '0';
    
    var TitleBox = document.getElementById('lstChoiceTitle');
    var IPBox = document.getElementById('lstChoiceIP');
    
    var SelectedTitles = SelectedIPs(TitleBox);
    var SelectedIP = SelectedIPs(IPBox);
    
    if(!SelectedTitles)
        SelectedTitles = '';
    
    if(!SelectedIP)
        SelectedIP = '';
        
    var StatMode = FindCookieValue('StatMode');
    var typeQS = '';
    
    if(StatMode == 'ICOLC')
    {
        if(method == 'Print')
            typeQS = 'ConsortiaICOLC_Print';
        else
            typeQS = 'ConsortiaICOLC_Excel';
    }
    else
    {
        if(method == 'Print')
            typeQS = 'ConsortiaCOUNTER_Print';
        else
            typeQS = 'ConsortiaCOUNTER_Excel';
    }
        
    window.open('PrintPage.aspx?type=' + typeQS + '&instsMode=' + instsMode + '&allDates=' + allDates + '&DateFrom=' + from + '&DateTo=' + to + '&SelectedTitles=' + SelectedTitles + '&SelectedIP=' + SelectedIP);
}

function GetConsortiaTable()
{
    document.getElementById('table_wrapwrap').innerHTML = window.parent.document.getElementById('consortia').innerHTML;
}
