var myTimer = (new Date()).getTime();

//ar mai putea fi folosit in admin la filtrele dupa litere tapate in inputboxuri
function setTrigger(reset, target, what, stat) {

    //reset request
    if (reset == 1) {
        myTimer = (new Date()).getTime();
        setTimeout('setTrigger(0, \'' + target + '\', \'' + what + '\', \'' + stat +'\')', 1000);
    }

    //getting current time...
    var right_now = (new Date()).getTime();

    if (right_now - myTimer >= 1000) {
        //trigger
        if (what == 'showBrandsNo') {
        	makeRequest('showBrandsNo', target, 'brands_layer');
        }
		if (what == 'showCompsNo') {
			makeRequest('showCompsNo', target, 'comps_layer', stat);
		}
		if (what == 'showProdsNo') {
			makeRequest('showProdsNo', target, 'prods_layer', stat);
		}
		if (what == 'showDictionaryNo') {
			makeRequest('showDictionaryNo', target, 'dictionary_layer', stat);
		}
		if (what == 'showUserNo') {
			makeRequest('showUserNo', target, 'user_layer', stat);
		}
    }
}


function isArray(obj) {

    return (typeof(obj)=="object" && (obj.length) &&(typeof(obj)!="string"));
}

function array_dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0; j<level+1 ;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "[" + item + "] {\n";
				dumped_text += array_dump(value,level+1);
				dumped_text += level_padding + " }\n";
			} else {
				dumped_text += level_padding + "[" + item + "] => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


//cele mai multe din cerererile makeRequest sunt preluate prin Ajaxa...
function makeRequest(what, target, location, extra) {

    if (what == 'showPropForm') {
        url = 'data.php?what=cat_prop&cat=' + target;
        //document.getElementById(location).innerHTML = 'Loading properties...';
    }
    if (what == 'showPath') {
        url = 'data.php?what=cat_path&cat=' + target;
        //document.getElementById(location).innerHTML = 'Loading path...';
    }

    if (what == 'showBrandsNo') { url = 'data.php?what=brands_no&filter=' + target; }

	if (what == 'showCompsNo') {
		url = 'data.php?what=comps_no&filter=' + target + '&stat=' + extra;
		//document.getElementById(location).innerHTML = 'Loading properties...';
	}

	if(what == 'getBrands' || what == 'getStores') {
		url = 'data.php?what='+what;
	}

	if (what == 'showProdsNo') {
		url = 'data.php?what=prods_no&filter=' + target + '&stat=' + extra;

		//document.getElementById(location).innerHTML = 'Loading properties...';
	}

	if (what == 'showDictionaryNo') {
		url = 'data.php?what=dictionary_no&filter=' + target + '&show=' + extra;
		//document.getElementById(location).innerHTML = 'Loading properties...';
	}

	if (what == 'showUserNo') {
		url = 'data.php?what=user_no&filter=' + target + '&show=' + extra;

		//document.getElementById(location).innerHTML = 'Loading properties...';
	}

    if (what == 'showSubCats') {
        url = 'data.php?what=sub_cats&cat=' + target;
        //document.getElementById(location).innerHTML = 'Loading...';
    }

    if (what == 'MerchantProducts') {
    	url = 'data_merchant.php?what=merchant_products' + target;
    }

    if (what == 'CheckAddProduct') {
    	url = 'data_merchant.php?what=find_add_product' + target;
    }

    if (what == 'loadSearchExp') {
    	url = 'data.php?what='+what+'&limit='+target;
    }
    if (what == 'srch_suggest') {
    	url = 'data.php?what='+what+'&filt='+target+'&limit=10&boolTmp=yes';
    }

    if (what == 'loadBuyNow') {
    	url = 'data.php?what='+what+'&limit='+target;
    	//alert(url);
    }

    if (what == 'logshop' || what == 'logviewdetails' || what == 'regClick') {
    	var strQry = '';
    	for (var k in target) {
    		strQry +='&' + k + '='+encodeURIComponent(target[k]);
    	}
    	url = 'data.php?what='+what+strQry;
    	//alert(url);
    }

    if(what == 'putinListBox' || what == 'putinCompareBox' || what == 'putinHistoryBox') {
    	url = 'data.php?what='+what+'&ProdID='+target+'&ProdName='+encodeURIComponent(extra)+'&PhotoSrc='+encodeURIComponent(location);
    	//alert(url);
    }

    if(what == 'loadListBox' || what == 'loadCompareBox' || what == 'loadHistoryBox') {
    	url = 'data.php?what='+what;
    	//alert(url);
    }


    var http_request = null;


    try{
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e){
        try{
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(sc){
            http_request = null;
        }
    }
    if(!http_request && typeof XMLHttpRequest != "undefined"){
        http_request = new XMLHttpRequest();
    }


    if(http_request){
        http_request.open("GET", url, true);
        http_request.onreadystatechange = function(){
            if(http_request.readyState == 4) {

                if (http_request.responseText){
                    if (what == 'showPropForm') { showPropertyTable (Json.evaluate(http_request.responseText), location); }
                    if (what == 'showPath') { document.getElementById(location).innerHTML = ''; document.getElementById(location).innerHTML = http_request.responseText; }
                    if (what == 'showBrandsNo') {document.getElementById(location).innerHTML = http_request.responseText; }
					if (what == 'showCompsNo') {document.getElementById(location).innerHTML = http_request.responseText; }
					if (what == 'showDictionaryNo') {document.getElementById(location).innerHTML = http_request.responseText; }
					if (what == 'showUserNo') {document.getElementById(location).innerHTML = http_request.responseText; }
					if (what == 'showProdsNo') {document.getElementById(location).innerHTML = http_request.responseText; }
                    if (what == 'showSubCats') { document.getElementById(location).innerHTML = ''; showCategories(http_request.responseText, location); }
                    if (what == 'MerchantProducts') {ShowProductList(Json.evaluate(http_request.responseText), location);}
                    if (what == 'CheckAddProduct') {SetFoundProduct(Json.evaluate(http_request.responseText));}
                    if (what == 'loadSearchExp')	{SetSearchExpArr(Json.evaluate(http_request.responseText));}
                    if (what == 'srch_suggest')	{AddSearchSuggest(Json.evaluate(http_request.responseText),location);}
                    if (what == 'logshop' || what == 'logviewdetails' || what == 'regClick') {;}
                    if (what == 'loadBuyNow')	{SetBuyNow(Json.evaluate(http_request.responseText));}
                    if (what == 'putinListBox' || what == 'putinCompareBox' || what == 'putinHistoryBox') {putProductInBoxes(what, Json.evaluate(http_request.responseText));}
                    if (what == 'loadListBox' || what == 'loadCompareBox' || what == 'loadHistoryBox') {loadXBoxes(what, Json.evaluate(http_request.responseText));}
                }
            }
        }
        http_request.send(null);

    } else {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
}

//obsolete - inlocuita cu Ajax in admin_categories.tpl
function initPage(page, page_target) {
    if (page == 'a_p') {
        makeRequest('showPath', page_target, 'prod_path');
        makeRequest('showSubCats', 0, 'cat_tree_root');
    }
}