﻿// JScript File
function makeRequest(url, category, producer, product, el_id, link_id, root) {
        
        if (category == 0) category = '';
        if (producer == 0) producer = '';
        if (product == 0)  product = '';
        
		var ind = category.search("/");
		var c_id = category.substring(0, ind);
		var c_unique = category.substring(ind+1, category.length);
		
		ind = producer.search("/");
		var pr_id = producer.substring(0, ind);
		var pr_unique = producer.substring(ind+1, producer.length);
		
		ind = product.search("/");
		var pd_id = product.substring(0, ind);
		var pd_unique = product.substring(ind+1, product.length);
		
        var http_request = false;
        
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml; charset=utf8');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
		
        if (!http_request) {
            alert('Не вышло :( Невозможно создать экземпляр класса XMLHTTP ');
            return false;
        }
        
		http_request.open('POST', url, true);
		//http_request.setRequestHeader('Content-Type', 'application/octet-stream');
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf8');
		http_request.onreadystatechange = function() { myf(http_request, el_id, link_id, c_unique, pr_unique, pd_unique, root); };
		
        var q = 'categoryId='+c_id+'&producerId='+pr_id;
		http_request.send(q);
    }

    function myf(http_request, el_id, link_id, c_unique, pr_unique, pd_unique, root) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				var old_div = document.getElementById(el_id);
				old_div.innerHTML = http_request.responseText;
				
				createLink(link_id, c_unique, pr_unique, pd_unique, root);
				
            } else {
                alert('С запросом возникла проблема.');
            }
        }

    }
    
    function clearSelect(elementId)
    {
        document.getElementById(elementId).innerHTML = "<select class='select_style'></select>";
    }
    function createLink(link_id, st_1, st_2, st_3, root)
    {
    	//var ind = st_1.search("/");
    	//if (ind > 0)
    	//{
		//    st_1 = st_1.substring(ind+1);
		//}
        
        var link_string = root;
        if (st_3 > "") 
		    link_string += st_3;
		else if (st_2 > "") 
		    link_string = link_string + st_1 + "/" + st_2;
		else if (st_1 > "")
		    link_string += st_1;
		
	    document.getElementById(link_id).href = link_string;
    }


