//RM25022007: Gets the reference to HTML object
function getElementByID(name){
	object = document.getElementById(name);
	if (object !== null){
		return object;
	}else{
		return false;
	}
}

function disableEnableBuyButton(){
    if (getElementByID('product_select_list').length < 1){
        getElementByID('buy_button').disabled = true;
    }else{
        getElementByID('buy_button').disabled = true
    }
}

function setLoading(mode){
    var lo = getElementByID('lo');
    if (lo === false) return;

    if (mode === 'show'){
        lo.style.display = '';
    }else{
        lo.style.display = 'none';
    }
}

function addProductToChartList(id_product_retail){
    createAjaxRequest('add', id_product_retail, renderProductChartList);
}

function removeProductFromChartList(id_product_retail){
    createAjaxRequest('remove', id_product_retail, renderProductChartList);
}

function setQuantity(quant, id_product_retail){
    if (quant == ''){
        quant = 0;
    }

    if (quant < 1){
        return '';
    }

    var qstr= '?action=refresh&id_item='+id_product_retail+'&quant='+quant;
	var url = '/si/veleprodaja/ajax/default.html' + qstr;

    xmlHttp = _xml();
	xmlHttp.open('get', url, true);
	xmlHttp.onreadystatechange = renderProductChartList;
	xmlHttp.send(null);
	setLoading('show');
}

function buyProducts(){
    createAjaxRequest('buy', null, productsBoughtSuccessfuly);
}

function productsBoughtSuccessfuly(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
	   var response = xmlHttp.responseText;
       getElementByID('home').innerHTML = response;
	   setLoading('hide');
	}
}

function renderProductChartList(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
	   var response = xmlHttp.responseText;
	   getElementByID('product_list').innerHTML = response;
       setLoading('hide');
	}
}

//RM09052007 Specific for retail sales
function createAjaxRequest(action, id_product_retail, function_name){
    var qstr= '?action='+action+'&id_item='+id_product_retail;
	var url = '/si/veleprodaja/ajax/default.html' + qstr;

    xmlHttp = _xml();
	xmlHttp.open('get', url, true);
	xmlHttp.onreadystatechange = function_name;
	xmlHttp.send(null);
    setLoading('show');
}

///////////////////////////////////
/// INPUT SUGGESTION FUNCTIONS ////
///////////////////////////////////
function getSuggestions() {
    var input   = getElementByID("product_name").value;

    //RM23052007 Check if lens attributes are selected
	var adv_div = getElementByID('advanced_search_div');
    if (adv_div.style.display === ''){
    	//RM23052007 Check which attribute is selected
    	var selects = adv_div.getElementsByTagName('select');
    	var qstr    = '?action=search&input='+input;
    	for (i = 0; i < selects.length; i++) {
    		if (selects[i].options[selects[i].selectedIndex].value !== 'null') {
    			qstr += '&'+selects[i].options[selects[i].selectedIndex].value+'='+selects[i].options[selects[i].selectedIndex].text;
    		}
    	}
    }else{
    	var qstr    = '?action=search&input='+input;
    }

	var url     = '/si/veleprodaja/ajax/default.html' + qstr;

    xmlHttp = _xml();
	xmlHttp.open('get', url, true); //RM17052007 Due to the onchange event
	xmlHttp.onreadystatechange = createSuggestionsList;
	xmlHttp.send(null);
	setLoading('show');
}

function createSuggestionsList() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        //RM10052007 Clear all options
        var select_list = getElementByID('product_select_list');
        select_list.length = 0;
        var elements    = xmlHttp.responseXML.getElementsByTagName("elements")[0].firstChild.data;

        for (i = 0; i < elements; i++) {
            var suggestion = xmlHttp.responseXML.getElementsByTagName("title")[i].firstChild.data;
            var price      = xmlHttp.responseXML.getElementsByTagName("price")[i].firstChild.data;
            var id         = xmlHttp.responseXML.getElementsByTagName("id_product_retail")[i].firstChild.data;

            //RM10052007 Add new options
            var option  = document.createElement("option");
  		    option.text = suggestion+' - '+price;
		    option.value= id;

    		try{
    			 //standards compliant; doesnt work in IE
        	     select_list.add(option, null);
  	        }catch(ex){
    			 //IE only
        		 select_list.add(option);
  		    }
        }
       setLoading('hide');
    }
}

function doClick(event) {
    var key = null;
    if (window.event) {
        key = window.event.keyCode; // RM270202007 IE support
    } else {
        key = event.which; // RM270202007 Firefox support
    }
    if (key === 13) { // RM270202007 if the key is "enter" do fake click
        getSuggestions();
    }
}

///////////////////////////////////
/////// LENS SEARCH FUNCTIONS /////
///////////////////////////////////
function showLensSearchForm(){
	var adv_div = getElementByID('advanced_search_div');
	if (adv_div.style.display === ''){
		adv_div.style.display = 'none';
	}else{
		adv_div.style.display = '';
	}
}