﻿//TODO: Get rid of copy/paste
var g_strCatalogPrefix;
g_strCatalogPrefix = "/catalog";

var g_strFakeRoot;
g_strFakeRoot = "kamaz";

function getAjax() {
    if (document.all){
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return new XMLHttpRequest();
    }
}

function getRootNode( domDocument){
    var nodes = domDocument.childNodes;
    var nLength = nodes.length;
    
    for(var i = 0; i < nLength; i++) {
        if (nodes[i].tagName == "root") {
            return nodes[i];
        }
    }
}

function getItems(strItemLatinName) {
   var ajax = getAjax();
  ajax.open("GET", g_strCatalogPrefix + "/xml.catalog/" + strItemLatinName, false);
  ajax.send(null);
  var domDocument = ajax.responseXML;   
  return getRootNode(domDocument).childNodes; 
}

function groupChanged(strGroup) {
    conditionChanged(strGroup, "chooseGroup");
    
}

function subGroupChanged(strGroup) {
    conditionChanged(strGroup, "chooseModel");
}

function conditionChanged(strParentCondition, strChildConditionId) {
   var childCondition = document.getElementById(strChildConditionId);
   clearElement(childCondition);
    if (strParentCondition == "") {
        childCondition.disabled = true;
        childCondition.selectedIndex = 0;
        if (childCondition.onchange) {
            childCondition.onchange();
        }
        return;
    }
   
    childCondition.disabled = false;
    
    fillElement(strParentCondition, childCondition); 
}

function clearElement(el) {
    for(var i = 0; i < el.options.length; i++) {
        el.remove(1);
    }   
}

function fillElement(strCondition, el) {
 var nodes = getItems(strCondition);
    
    for(var i = 0; i < nodes.length; i++) {
        if (nodes[i].tagName != "item") {
            continue;
        }
        var opt = document.createElement("option");
        opt.setAttribute("value", nodes[i].getAttribute("latname"));
        opt.appendChild(document.createTextNode(nodes[i].getAttribute("locname")));
        el.appendChild(opt);
    }
}

function filter() {
    var strGroup = getValue("chooseType");
    var strSubGroup = getValue("chooseGroup");
    var strModel = getValue("chooseModel");
    
    if (strModel != "") {
        document.location.href = g_strCatalogPrefix + "/item.catalog/" + strModel;
        return;
    }
    
    if (strSubGroup != "") {
        document.location.href = g_strCatalogPrefix + "/category.catalog/" + strSubGroup;
        return;
    }
    
    if (strGroup != "") {
        document.location.href = g_strCatalogPrefix + "/category.catalog/" + strGroup;
    }
}

function getValue(strSelectId) {
    var select = document.getElementById(strSelectId);
    return select.options[select.selectedIndex].value;
}

function clearFilter() {
    var chooseType = document.getElementById("chooseType");
    if (chooseType == null) {
        return;
    }
    chooseType.selectedIndex = 0;
    
    document.getElementById("chooseGroup").selectedIndex = 0;
    document.getElementById("chooseGroup").disabled = true;
    
    document.getElementById("chooseModel").selectedIndex = 0;
    document.getElementById("chooseModel").disabled = true;
    
    clearElement(chooseType);
    fillElement(g_strFakeRoot, chooseType);
}

function fillMenu() {
    //TODO: Copy/paste
    var ul = document.getElementById("catalogMenu");
    if (ul == null) {
    return;
    }
    var nodes = getItems(g_strFakeRoot);
    for(var i = 0; i < nodes.length; i++) {
        var item = nodes[i];
        if (item.tagName != "item") {
            continue;
        }
        var li = document.createElement("li");
        var a = document.createElement("a");
        a.appendChild(document.createTextNode(item.getAttribute("locname")));
        a.setAttribute("href", g_strCatalogPrefix + "/category.catalog/" + item.getAttribute("latname"));
        li.appendChild(a);
        ul.appendChild(li);
    }
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function trim(str) {
    return str.replace(/(^\s+)|(\s+$)/g, "");
}


function checkOrder() {
    var items = $('.validate');
    var l = items.length;
    for (var i = 0; i < l; i++) {
        if (trim(items[i].value) == "") {
            alert("Не заполнены необходимые поля");
            return false;
        }
    }
    return true;
}


//////////
addLoadEvent(clearFilter);
addLoadEvent(fillMenu);
