///////////////////////////////////
///////// SEARCH FUNCTIONS ////////
///////////////////////////////////
var searching = null;

function searchPapers(paper_type) {
    if (searching!=null) {
        clearTimeout(searching);
    }
    searching = setTimeout("submitSearchPapers("+paper_type+")", 600);
}

function submitSearchPapers(paper_type){
    var needle  = $('papers_search').value;

    if (needle.length < 3){
        var select      = $('paper_name');
        while (select.options.length > 0) {
            select.options[0] = null;
        }
    }else{
        var url     = 'm=search&a=papers&needle='+needle+'&t='+paper_type;
        sendRequest(url, 'papersSearched');
    }
}

function papersSearched(http_request){
    var xmldoc = checkHttpRequest(http_request);
    //console.log( http_request );
    if (xmldoc !== false){
        var status = http_request.responseXML.getElementsByTagName('status')[0].firstChild.data;
        if (status === 'true'){
            var num_papers  = http_request.responseXML.getElementsByTagName('elements')[0].firstChild.data;
            var paper_name  = '';
            var paper_id    = '';
            var paper_title = '';
            var paper_type  = '';
            var market_code = '';
            var select      = $('paper_name');

            while (select.options.length > 0) {
                select.options[0] = null;
            }

            for (i=0; i<num_papers; i++){
                try {
                  paper_name = http_request.responseXML.getElementsByTagName('paper_name')[i].firstChild.data;
                }catch(ex){
                  paper_name = '';
                }

                try {
                  paper_id = http_request.responseXML.getElementsByTagName('paper_id')[i].firstChild.data;
                }catch(ex){
                  paper_id = '';
                }

                try {
                  paper_title = http_request.responseXML.getElementsByTagName('paper_title')[i].firstChild.data;
                }catch(ex){
                  paper_title = '';
                }

                try {
                  paper_type  = http_request.responseXML.getElementsByTagName('paper_type')[i].firstChild.data;
                }catch(ex){
                  paper_type  = '';
                }

                try {
                  market_code = http_request.responseXML.getElementsByTagName('market_code')[i].firstChild.data;
                }catch(ex){
                  market_code = '';
                }

                try {
                  paper_price = http_request.responseXML.getElementsByTagName('paper_price')[i].firstChild.data;
                }catch(ex){
                  paper_price = 0;
                }

                try {
                  currency = http_request.responseXML.getElementsByTagName('currency')[i].firstChild.data;
                }catch(ex){
                  currency = '';
                }
                var option  = document.createElement("option");

                if (paper_type == 4) {
                    option.text = market_code+' - '+paper_name+' ('+paper_title+')';
                } else {
                    option.text = paper_name+' - '+paper_title;
                }

                option.title    = currency;
                option.className= paper_price;
                option.value    = paper_id;

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

                if (i == 0){
                    setSelectedIndex(select, paper_id);
                }
            }

            //RM16112007 Fix so chart is refresh automatily
            //if ($('charts_div') !== false && num_papers == 1){
            //  refreshPapersIndexProfitInPortfeilChart();
            //}

        }else{
            var error = http_request.responseXML.getElementsByTagName('error')[0].firstChild.data;
            information(error);
        }
    }
}

///////////////////////////////////
///////// WARNING FUNCTIONS ///////
///////////////////////////////////
function checkEntryPercentage(mode){
    var entry_percentage    = document.getElementById('entry_percentage');
    var entry_sum           = document.getElementById('entry_sum');

    if (mode == 'sum'){
        if (entry_percentage.value !== ''){
            alert('Vpisali ste že vstopni znesek procentualno, zato vam ni treba vpisati vstopni znesek zneskovno');
        }
    }else{
        if (entry_sum.value !== ''){
            alert('Vpisali ste že vstopni znesek zneskovno, zato vam ni treba vpisati vstopni znesek procentualno');
        }
    }
}

function checkSalePercentage(mode){
    var sale_percentage    = document.getElementById('sale_percentage');
    var sale_sum           = document.getElementById('sale_sum');

    if (mode == 'sum'){
        if (sale_percentage.value !== ''){
            alert('Vpisali ste že prodajni znesek procentualno, zato vam ni treba vpisati prodajni znesek zneskovno');
        }
    }else{
        if (sale_sum.value !== ''){
            alert('Vpisali ste že prodajni znesek zneskovno, zato vam ni treba vpisati prodajni znesek procentualno');
        }
    }
}

function setSelectedIndex(list, value){
    for (var intI = 0; intI < list.options.length; intI++) {
        if (list.options[intI].value == value) {
            list.options[intI].selected = true;
        }
    }
}

function getSelectedIndexId(list){
    return document.getElementById(list).options[document.getElementById(list).selectedIndex].title;
}

function getSelectedIndexClassName(list){
    return document.getElementById(list).options[document.getElementById(list).selectedIndex].className;
}

function setCurrencyAndCalculatePaperValute(){
    //RM24102007 Set new currency for that paper
    document.getElementById('currency').value = getSelectedIndexId('paper_name');

    //RM24102007 Calculate new portfeil price based on current paper
    var portfeil_price = document.getElementById('portfeil_price');
    portfeil_price.value = 0;

    var quantity       = document.getElementById('quantity');
    quantity           = quantity.value;

    if (quantity == "" || quantity < 0){
        quantity = 1;
        document.getElementById('quantity').value = 1;
    }
    var paper_price    = getSelectedIndexClassName('paper_name');

    var priceValuesStr = parseFloat(quantity)*parseFloat(paper_price);
    
    var pValues = priceValuesStr.toString();
   
 
    /*
     * Fix. pika => vejca
     */
    portfeil_price.value =  pValues.replace(".", ",");
   
    //alert(  portfeil_price.value );  
    
}

function setToZero(){
    document.getElementById('quantitiy').value = 0;
    document.getElementById('paper_price').value = 0;
}

function checkQuantity() {
	//alert( document.getElementById('quantity').value );
	var value = document.getElementById('quantity').value;
	value = value.replace(",", ".");
	if (!(parseFloat(value) >= 1)) {
		document.getElementById('quantity').value	= 	'1,00';	
	}	
}

function sh_act(id, ids) {
	$("."+id).toggle();	
	
	if ($('.a'+ids).html() == '+') {
		$('.a'+ids).html('-');	
	}
	else {
		$('.a'+ids).html('+');		
	}
}




