    function showResponse( originalRequest )
    {
        var i,length;

        root_nodes = originalRequest.responseXML.childNodes;

	for ( i = 0 ; i < root_nodes.length ; i++ )
		if ( root_nodes.item(i).nodeName == "category" )
			category = root_nodes.item(i);

	category_nodes = category.childNodes;

	for ( i = 0 ; i < category_nodes.length ; i++ )
	{
		if ( category_nodes.item(i).nodeName == "subcategories" )
			subcategories = category_nodes.item(i).childNodes;

		if ( category_nodes.item(i).nodeName == "category_name" )
			label = getXmlNodeValue( category_nodes.item(i) );

		if ( category_nodes.item(i).nodeName == "all_option_value" )
			all_option_value = getXmlNodeValue( category_nodes.item(i) );
	}


        for( i = 0 ; i < category.attributes.length ; i++ )
        {
            if( category.attributes.item(i).nodeName == 'select_id' )
                target_select_id = category.attributes.item(i).nodeValue;

            if( category.attributes.item(i).nodeName == 'selected_option_value' )
                selected_option_value = category.attributes.item(i).nodeValue;

            if( category.attributes.item(i).nodeName == 'category_id' )
                category_id = category.attributes.item(i).nodeValue;

        }

        var target_select = document.getElementById( target_select_id );



        length = target_select.length;

        for( i = 0 ; i < length ; i++ )
            target_select.remove( target_select.options[i] );

        if( all_option_value )
        {
            option = document.createElement( "option" );
            optionValue = document.createAttribute( "value" );
            optionValue.value = "0";
            option.setAttributeNode( optionValue );
            optionText = document.createTextNode( all_option_value );
            option.appendChild( optionText );
            target_select.appendChild( option );
        }

        for ( i = 0 ; i < subcategories.length ; i++ )
        {
            if( subcategories.item(i).nodeName == 'label' )
            {
                label = subcategories.item(i).nodeValue;
                //if( $('subCategoriesOf_' + category_id + '_l') != undefined )
                    //$('subCategoriesOf_' + category_id + '_l').innerHTML = label;
            }

            if ( subcategories.item(i).nodeName == "category" )
            {
                category = subcategories.item(i);

                option = document.createElement( "option" );
                optionValue = document.createAttribute( "value" );

                for( j = 0 ; j < category.attributes.length ; j++ )
                {
                    if( category.attributes.item(j).nodeName == 'id' )
                    {
                        optionValue.value = category.attributes.item(j).nodeValue;

                        if ( optionValue.value == selected_option_value )
                        {
                              selected = document.createAttribute( "selected" );
                              option.setAttributeNode( selected );
                              option.selected = true;
                        }

                        option.setAttributeNode( optionValue );
                    }

                    optionText = document.createTextNode( getXmlNodeValue( category ) );
                }

                option.appendChild( optionText );
                target_select.appendChild( option );
            }
        }
        target_select.disabled = false;
    }

    function setSubcategories( category_id , select_id , sov , all_option_value , sync )
    {
        $(select_id).disabled = true;
        var url = '/ajax/subcategories.php' ;
        var pars = 'category_id=' + category_id + '&select_id=' + select_id + '&selected_option_value=' + sov + '&all_option_value=' + all_option_value ;

        if( sync != undefined && sync == 1 )
        {
            var myAjax = new Ajax.Request
            (
                url,
                {
                    method          : 'get',
                    parameters      : pars,
                    onComplete      : showResponse,
                    asynchronous    : 'false'
                }
            );
        }
        else
        {
            var myAjax = new Ajax.Request
            (
                url,
                {
                    method          : 'get',
                    parameters      : pars,
                    onComplete      : showResponse,
                    asynchronous    : 'true'
                }
            );
        }

    }

    function createSubCategories( select , all_option_value , build_select , create_label )
    {
        var category_id = select.value;
        var select_id = select.id;

        var initSelect = document.getElementById( select_id );
        var container = initSelect.parentNode.parentNode;

        initSelect.style.border = "";

        while ( container.lastChild.id != select_id + '_p' )
            container.removeChild( container.lastChild );

        if( category_id == "0" )
            return false;

        var child_select = initSelect.cloneNode(true);
            child_select.id = 'subCategoriesOf_' + category_id;

        p = document.createElement( "p" );
        p.id = 'subCategoriesOf_' + category_id + '_p';

        if ( build_select == 1 )
            p.style.display = "none";

        if( create_label )
        {
            for( i = 0 ; i < initSelect.options.length ; i++ )
                if( initSelect.options[i].selected == true )
                {
                    var category_name = initSelect.options[i].text;
                    break;
                }

            var label = document.createElement( "label" );
                if( category_name == undefined )
                    label.innerHTML = "&nbsp";
                else
                    label.innerHTML = category_name;
                label.id = 'subCategoriesOf_' + category_id + '_l';

            p.appendChild( label );
        }

        p.appendChild( child_select );

        container.appendChild( p );

        var length = child_select.length;
            for( i = 0 ; i < length ; i++ )
                child_select.remove( child_select.options[i] );

        if( build_select == 0 )
            return false;


		var url = '/ajax/subcategories.php' ;
		var pars = 'category_id=' + category_id + '&select_id=' + child_select.id + '&all_option_value=' + all_option_value;
		var myAjax = new Ajax.Request
		(
			url,
			{
				method          : 'get',
				parameters      : pars,
				onComplete      : buildSelect,
				asynchronous    : 'true'
			}
	    );
    }

function buildSelect( originalRequest )
{
    var length,i;
    var root_nodes = originalRequest.responseXML.childNodes;

	for ( i = 0 ; i < root_nodes.length ; i++ )
		if ( root_nodes.item(i).nodeName == "category" )
			category = root_nodes.item(i);

	category_nodes = category.childNodes;

	for ( i = 0 ; i < category_nodes.length ; i++ )
	{
		if ( category_nodes.item(i).nodeName == "subcategories" )
			subcategories = category_nodes.item(i).childNodes;

		if ( category_nodes.item(i).nodeName == "category_name" )
			label = getXmlNodeValue( category_nodes.item(i) );

		if ( category_nodes.item(i).nodeName == "all_option_value" )
			all_option_value = getXmlNodeValue( category_nodes.item(i) );
	}


    for( i = 0 ; i < category.attributes.length ; i++ )
    {
            if( category.attributes.item(i).nodeName == 'category_id' )
                category_id = category.attributes.item(i).nodeValue;
    }

    var child_select_id = 'subCategoriesOf_' + category_id;
    var child_select = $(child_select_id);
    var container = child_select.parentNode.parentNode;

    if( subcategories.length < 1 )
    {
        container.removeChild( $(child_select_id + '_p') ) ;
        return false;
    }

    p.style.display = "inline";
    $(child_select_id).disabled = true;
    //setSubcategories( category_id , child_select_id , 0 , all_option_value );
    showResponse( originalRequest );
}

function getXmlNodeValue(xmlNode)
{
    if( xmlNode.textContent != undefined )
        return xmlNode.textContent;

    if( xmlNode.text != undefined )
        return xmlNode.text;

}