function LoadAjax() {
    http_request = false;
    if (window.XMLHttpRequest) { 
        http_request = new XMLHttpRequest();
    } 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) {
        return false;
    }
    return http_request;
}

function ProvocationRequest(url) {
    http = LoadAjax();
    http.open('GET', url, true);
    http.send(null);
}

function CloseProvocation(uid) {
    document.getElementById('provocation_note').style.visibility = 'hidden';
    document.getElementById('provocation_text').style.visibility = 'hidden';
    if(uid){
        ProvocationRequest("/provocation/close/" + uid + "/");
    }
}

function ShowAlert(text){
    if(!document.getElementById('provocation_text')){
        document.getElementById('provocation_area').innerHTML += '<form name="prov_form"><div id="provocation_note"><a class="close" href="#" onclick="CloseProvocation();"><img src="http://static.pennergame.de/img/pv4/premiummedia/img/premium/provocation/provocation_note_close.png" border="0"></a><div id="provocation_text" class="provocation_text"><div class="prov_links"></div></div></div></form>';
    }
    if(text=='no_provocations'){
        text = _("Du hast keine Provokationen mehr über!") + '<br/><a href="/itemsale/">' + _("Neue Provokationen kaufen") + '</a>'
    }
    document.getElementById('provocation_text').innerHTML = text;
    document.getElementById('provocation_text').style.visibility = 'visible';
    document.getElementById('provocation_note').style.visibility = 'visible';

}

function ShowPopup(text){
    if(document.getElementById('lightbox')){
        document.getElementById('lightbox').innerHTML = '<a class="close" onclick="CloseAlert();"><img src="http://static.pennergame.de/img/pv4/premiummedia/img/premium/provocation/provocation_note_close.png" border="0"></a><center><span class="text">' + text + '</span></center>';
        document.getElementById('provocation_message').style.visibility = 'visible';
        document.getElementById('lightbox').style.visibility = 'visible';
    }else{
    	document.getElementById('fix').innerHTML += '<div id="provocation_message"></div><div id="lightbox"><a class="close" onclick="CloseAlert();"><img src="http://static.pennergame.de/img/pv4/premiummedia/img/premium/provocation/provocation_note_close.png" border="0"></a><center>' + text + '</center></div>';
    }
}

function Provoke(self,uid,item,alert){
    if(!item){
        item = document.prov_form.prov_selection.value;
    }
    http = LoadAjax();
    http.open('GET', "/provocation/create/" + uid + "/" + item + "/", true);
    http.send(null);
    http.onreadystatechange = function(){
        if(http.readyState==4){
            var response = http.responseText;
            if(response){
                ProvocationRequest("/provocation/close/" + self + "/");
                if(alert=="popup"){
                    ShowPopup(response);
                }else{
                    ShowAlert(response);
                }
            }else{
                alert("Error");
            }
        }
    }  
} 

function ProvokeForm(self,uid,item,alert){
    scroll(0,0);
    http = LoadAjax();
    http.open('GET', "/provocation/ajax/itemlist/", true);
    http.send(null);
    http.onreadystatechange = function(){
    if(http.readyState==4){
        var itemlist = http.responseText;
        if(itemlist){
            var text = '<select name="prov_selection">' + itemlist + '</select></div><div class="prov_links"><center><a class="prov_start" href="#" onclick="Provoke('+self+','+uid+');"></a></center>'; 
            ShowAlert(text);
        }
    }
    }
}

function CloseAlert() {
    document.getElementById('provocation_message').style.visibility = 'hidden';
    document.getElementById('lightbox').style.visibility = 'hidden';
}
