var http_request = false;
var targetForm = '';


function Request(url, params, target){

    http_request = false;
    targetForm = target;

    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        // zu dieser Zeile siehe weiter unten
        }
    }else if(window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){}
        }
    }

    if (!http_request){
        //alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
        return false;
    }
    http_request.onreadystatechange = alertInhalt;
    http_request.open('GET', url + '?' + params, true);
    http_request.send(null);
}

function alertInhalt(){
    if (http_request.readyState == 4){
        if (http_request.status == 200 && document.getElementById(targetForm) != null){
            if(http_request.responseText != 'null'){
                var request_result = document.getElementById(targetForm);
                document.getElementById(targetForm).style.visibility = "visible";
                request_result.innerHTML = http_request.responseText;
            }else{
                var request_result = document.getElementById(targetForm);
                document.getElementById(targetForm).style.visibility = "hidden";
                request_result.innerHTML = '';
            }
        }else{
    //alert('Bei dem Request ist ein Problem aufgetreten.');
    }
    }
}

function setProgressBar(target){
    var targetElem = document.getElementById(target);
    targetElem.style.visibility = "visible";
    targetElem.innerHTML = '<img src="/_Templates/default/Images/progress.gif" />';
}

function openWindow(param){
    if(param == "pw_reminder"){	   
        window.open("../up_pw_reminder.php5","pw_reminder","width=350,height=150,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }else if(param == "agb"){		
        window.open("agb_window.php5","agb","width=500,height=400,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }else if(param == "agb_"){		
        window.open("../agb_window.php5","agb","width=500,height=400,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }
}

function showAvailabilities(param){
    availabilities = window.open("rm_availabilities.php5?param=" + param,"availabilities","width=700,height=700,status=0,resizable=0,scrollbars=1,left=20,top=20");
    availabilities.focus();
	
}

function showLocationMap(param){
    locationmap = window.open("raw-materials/rm_show_location_map.php5?param=" + param, "availabilities" , "width=630,height=490,status=0,resizable=1,scrollbars=1,left=350,top=200");
    locationmap.focus();
	
}

function openFaqWindow(param, param2, param3){
    if(param == "te_faq"){		
        window.open("faq_window.php5?x="+param2+"&y="+param3,"agb","width=650,height=500,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }
	
    if(param == "mp_faq"){		
        window.open("/tender/faq_window.php5?x="+param2+"&y="+param3,"agb","width=450,height=130,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }
	
    if(param == "mp_edit_faq"){		
        window.open("/tender/faq_window.php5?x="+param2+"&y="+param3,"agb","width=550,height=400,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }
	
}

function expertWindow(param1, param2, param3){
    if(param3 != ''){
        expertInfo = window.open("pr_experts.php5?action="+param1+"&market_id="+param2+"&category_id="+param3,"experts","width=380,height=400,status=0,resizable=0,scrollbars=1,left=20,top=20");
        expertInfo.focus();
    }else{
        expertInfo2 = window.open("pr_experts.php5?action="+param1+"&user_id="+param2,"expert","width=400,height=400,status=0,resizable=0,scrollbars=1,left=440,top=20");		
        expertInfo2.focus();
    }
}

function showHelp(param1, param2){
    if(param1 == "rm_search" || param1 == "rm_search2"){
        help = window.open("../show_help.php5?b="+param1+"&p="+param2,"help","width=550,height=700,status=0,resizable=0,scrollbars=1,left=20,top=20");
    }else if(param1 == "rm_search_location" ){
        help = window.open("../show_help.php5?b="+param1+"&p="+param2,"help","width=350,height=500,status=0,resizable=0,scrollbars=1,left=20,top=20");	
    } else {
        help = window.open("../show_help.php5?b="+param1+"&p="+param2,"help","width=550,height=700,status=0,resizable=0,scrollbars=1,left=20,top=20");	
    }
    help.focus();
}

function showRMHelp(param1, param2){
    help = window.open("../show_help.php5?b="+param1+"&p="+param2,"help","width=350,height=525,status=0,resizable=0,scrollbars=1,left=20,top=20");
    help.focus();
}

function showProduct(param){
    productInfo = window.open("mp_product_info.php5?product_id="+param, "productInfo","width=700,height=650,status=0,resizable=0,scrollbars=1,left=20,top=20");
    productInfo.focus();
}

function showProduct2(param){
    productInfo = window.open("mp_product_info.php5?product_id="+param+"&noShowButton=true", "productInfo","width=600,height=650,status=0,resizable=0,scrollbars=1,left=20,top=20");
    productInfo.focus();
}

function checkVisible(){
    if(document.getElementById(55).checked){
        document.getElementById(33).style.visibility = "hidden";
        document.getElementById(22).style.visibility = "hidden";
    }else{
        document.getElementById(33).style.visibility = "visible";
        document.getElementById(22).style.visibility = "visible";
    }
}

function checkVisible2(){
    if(document.getElementById(77).checked){
        document.getElementById(88).style.visibility = "hidden";
        document.getElementById(99).style.visibility = "hidden";
    }else{
        document.getElementById(88).style.visibility = "visible";
        document.getElementById(99).style.visibility = "visible";
    }
}

function checkWatchlistMarket(marketId){
    
    var marketCheckbox = $("#market_selector_"+marketId);
    
    var marketChecked = marketCheckbox.attr("checked");
    
    $(".watch_market_"+marketId).each(function(){
        
        $(this).attr('checked', marketChecked);
        
        var currentCatId = ($(this).attr('id'));
        
        var re = new RegExp("checkbox_([^_]*)_([^_]*)", "g");
        
        currentCatId = currentCatId.replace(re, "$2");
        
        checkWatchlistKombination(marketId, currentCatId);
        
    })
    
}

function checkWatchlistCategory(catId){
    
    var categoryCheckbox = $("#cat_selector_"+catId);
    
    var categoryChecked = categoryCheckbox.attr("checked");
    
    $(".watch_category_"+catId).each(function(){
        
        $(this).attr('checked', categoryChecked);
        
        var currentMarketId = ($(this).attr('id'));
        
        var re = new RegExp("checkbox_([^_]*)_([^_]*)", "g");
        
        currentMarketId = currentMarketId.replace(re, "$1");
        
        checkWatchlistKombination(currentMarketId, catId);
        
    })
}

function checkWatchlistKombination(marketId, catId){
    
    var marketChecked = true;
    var categoryChecked = true;
    
    $(".watch_market_"+marketId).each(function(){
        
        marketChecked = marketChecked && $(this).attr("checked");
    });
    
    $(".watch_category_"+catId).each(function(){
        
        categoryChecked = categoryChecked && $(this).attr("checked");
    });
    
    $("#market_selector_"+marketId).attr('checked', marketChecked);
    
    $("#cat_selector_"+catId).attr('checked', categoryChecked);
    
}

var tooltipHover = new Array();
var tooltipLoaded = new Array();
var tooltipWidth = new Array();
var tooltipHeight = new Array();
var tooltipXPosition = new Array();
var tooltipYPosition = new Array();
var dialogConfig = new Array();

function loadTooltip(tooltipId){
    
    if (!tooltipLoaded[tooltipId]) {
    
        var newWidth = tooltipWidth[tooltipId];
        var newHeight = tooltipHeight[tooltipId];
        
        if (!newWidth || isNaN(newWidth) || newWidth === ""){
            newWidth = 250;
        }
        if (!newHeight || isNaN(newHeight) || newHeight === ""){
            newHeight = 150;
        }
    
        tooltipWidth[tooltipId] = newWidth;
        tooltipHeight[tooltipId] = newHeight;
                
        $('#dialog_'+tooltipId).dialog({
            autoOpen: false, 
            width: newWidth, 
            height: newHeight
        });
                
        $('#icon_'+tooltipId).hoverIntent(dialogConfig[tooltipId]);
                
        $("#dialog_"+tooltipId).parent("div.ui-dialog").hover(function(){
            tooltipHover[tooltipId] = true;
        }, function(){
            tooltipHover[tooltipId] = false;
        }).hoverIntent(dialogConfig[tooltipId]);
      
        tooltipLoaded[tooltipId] = true;
    }
    
    return false;
}

function openTooltip(tooltipId){
                
    $(".jqui_tooltip").each(function(){
        if ($(this).dialog('isOpen')){
            $(this).dialog('close');
        }
    });
                    
    $("#dialog_"+tooltipId).dialog('open');
            
    var newWidth = tooltipWidth[tooltipId];
    var newHeight = tooltipHeight[tooltipId];
                
            
    
    if ($.support.boxModel === false) {
        
        $("#dialog_"+tooltipId).height(newHeight);
        $("#dialog_"+tooltipId).width(newWidth);
        $("#dialog_"+tooltipId).dialog('enable');
    }
                
    var x = eval(tooltipXPosition[tooltipId]);
    var y = eval(tooltipYPosition[tooltipId]);


    if (!x || isNaN(x) || x === ""){
        x = $('#icon_'+tooltipId).position().left + $('#icon_'+tooltipId).outerWidth() + 4;
    }

    if (!y || isNaN(y) || y === ""){
        y = $('#icon_'+tooltipId).position().top - $(document).scrollTop() - 4;
    }

    $("#dialog_"+tooltipId).dialog('option', 'position', [x,y]);
    
    return false;
}

function closeTooltip(tooltipId){
    if (tooltipHover[tooltipId] == false){
        $("#dialog_"+tooltipId).dialog('close');
    }
    return false;
}

var popupWidth = new Array();
var popupHeight = new Array();
var popupLoaded = new Array();

function loadPopup(popupId){
    
    if (!popupLoaded[popupId]) {
                
        $('#dialog_'+popupId).dialog({
            autoOpen: false,
            height : (Math.min(popupHeight[popupId], parseInt($(window).height())-80)),
            width : (Math.min(popupWidth[popupId], parseInt($(window).width())-80)),
            close: function(event, ui){
                $("#icon_"+popupId).focus();
            }
        });
                
        $('#icon_'+popupId).click(function(){
            
            $(".jqui_popup").each(function(){
                if ($(this).dialog('isOpen')){
                    $(this).dialog('close');
                }
            });
            
            var height = (Math.min(popupHeight[popupId], parseInt($(window).height())-80));
            var width = (Math.min(popupWidth[popupId], parseInt($(window).width())-80));

            $("#dialog_"+popupId).dialog('open');
            
            $("#dialog_"+popupId).dialog('option', 'height', height);
            $("#dialog_"+popupId).dialog('option', 'width', width);
            
            if ($.support.boxModel == false) {
        
                $("#dialog_"+popupId).height(height);
                $("#dialog_"+popupId).width(width);
                $("#dialog_"+popupId).dialog('enable');
            }
            
            $("#dialog_"+popupId).dialog('option', 'position', 'center');
            
            var iconTop = $(this).position().top - $(document).scrollTop();
            
            var x = $(window).width()/2 - $("#dialog_"+popupId).width()/2;
            
            var y = iconTop + 12;
            
            if ((iconTop + height + 26 ) > ($(window).height())){
                
                y = iconTop - height - 26;
            }
            
            $("#dialog_"+popupId).dialog('option', 'position', [x,y]);
            
            return false;
        });
                
        popupLoaded[popupId] = true;
    }
    
}

function setValue(elem, value, name){
    elem.value = value;
    elem.name = name;
}
