﻿// JScript File
var xpathCardBase = '//cardlist/card';

var xml; 
var xmlFile = "../../cardlist.xml";
var element;
var currentType;

var arrAttributes = new Array('travel','cash','rewards','lowapr','introapr','transferapr','noannual')
var arrSubAttributesCounts = new Array();
var subTotCount;

function loadXMLDoc(filename) {
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject) {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument) {
        xmlDoc=document.implementation.createDocument("","",null);
    }
    else {
        alert('Your browser cannot handle this script');
    }
    xmlDoc.async=false;
    xmlDoc.load(filename);
    return xmlDoc;
}

function initCards(){
    xml=loadXMLDoc(xmlFile);
    setNodeCount(getNodeCount(xpathCardBase));

    if( document.getElementById ) // this is the way the standards work
        element = document.getElementById("total");
    else if( document.all ) // this is the way old msie versions work
        element = document.all["total"];
    else if( document.layers ) // this is the way nn4 works
        element = document.layers["total"];
    else
        alert('NOT SUPPORTED');
}

function setActiveCardType(selected){
    //alert('Active Type: ' + selected);
    currentType = '[@type=\'' + selected + '\']';
    var xpathCardType = xpathCardBase + currentType;
    count = getNodeCount(xpathCardType);
    subTotCount = count;
    setNodeCount(count);
    showSubTypes();
}

function showSubTypes() {
    for (var i = 0; i < arrAttributes.length; i++) {
        nextSubType = '[@subtype=\'' + arrAttributes[i] + '\']';
        xpathCardType = xpathCardBase + currentType + nextSubType
        totsubcount = getNodeCount(xpathCardType)
        arrSubAttributesCounts[i] = 0; //CLEAR CURRENT COUNT
        arrSubAttributesCounts[i] = totsubcount //SET NEW COUNT
        obj = document.getElementById(arrAttributes[i])
        obj.checked = false; //CLEARS THE PREVIOUS CHECKED BOXS
        desc = document.getElementById(arrAttributes[i] + '_row')
        if (totsubcount > 0){
            obj.disabled = false;
            desc.className='enable';
        } 
        else {
            obj.disabled = true;
            desc.className='disable';
        }
    }
}

function updateSubType(){
    //JUST GO AHEAD AND SEE WHAT IS CHECKED
    var subTypeTot = 0;
    for (var i = 0; i < arrAttributes.length; i++) {
        obj = document.getElementById(arrAttributes[i])
        
        if (obj.checked){
            //alert(arrAttributes[i] + ' checked = True')
            subTypeTot = subTypeTot + arrSubAttributesCounts[i]
            //alert(subTypeTot)
        }
    }
    if (subTypeTot > 0) {
        setNodeCount(subTypeTot)
    }
    else {
        setNodeCount(subTotCount)
    }
}

function updateCount(currentType){
    var xpathCardType = xpathCardBase + '[@type=\'' + type + '\']'
    //alert(xpathCardType)
    setNodeCount(getNodeCount(xpathCardType));
}

function getNodeCount(xpath){

    var nodeCount = 0;
    //var count = document.evaluate("count(//cardlist/card[@type='Consumer'])", xml, null, XPathResult.ANY_TYPE,null);
    //alert('COUNT:'+document.evaluate("count(//cardlist/card[@type='Consumer'])", xml, null, XPathResult.ANY_TYPE,null))
    //document.getElementById('selectiontotal').innerHTML=count;
    try{
        if (window.ActiveXObject) {
        //alert('window.ActiveXObject');
            var nodes=xml.selectNodes(xpath);

            nodeCount=nodes.length;
            //alert('IE NODE COUNT: '+nodeCount)
        }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument) {
            //alert('OTHER');
            //alert("count("+xpath+")");
            nodeCount=xml.evaluate("count("+xpath+")", xml, null, XPathResult.ANY_TYPE, null).numberValue; 
            //alert('FF NODE COUNT: '+nodeCount)
        }
        else {
            alert('NOT SUPPORTED')
        }
    }
    catch(err){
        alert('Node Error: ' + err.description )
    }
    return nodeCount;
}

function setNodeCount(nodeCount){ document.getElementById("total").innerHTML = nodeCount; }