// Executes an AJAX Request
function MakeHttpRequest(url, callback_function, destination)
{
	var http_request = false;

	if (window.XMLHttpRequest)
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	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('Dein Browser unterstützt ein verwendetes Feature nicht.');
		return false;
	}

	if(callback_function) {

    	http_request.onreadystatechange = function()
    	{
    		if (http_request.readyState == 4)
    		{
    			if (http_request.status == 200)
    			{
    				if (destination)
    				{
    					eval(callback_function + '(http_request.responseText, \'' +destination+ '\')');
    				}
    				else
    				{
    					eval(callback_function + '(http_request.responseText)');
    				}
    			}
    			else
    			{
    				alert('Bei einem httpRequest ist ein Fehler aufgetreten (Code: ' + http_request.status + ').');
    			}
    		}
    	}
	}

	http_request.open('GET', url, true);
	http_request.send(null);
}


// Adds a new upload field
function addField()
{
    // how many fields do we have?
    var fieldsCount = document.getElementById("FieldsCount").value;

    // next field!
    var fieldId = ++fieldsCount;

    // create the container element
    var fieldContainer = document.createElement("div");

    // set the container options
    fieldContainer.className = "Field";
    fieldContainer.setAttribute("id", "Field-" + fieldId);

    // create the text node
    var fieldText = document.createTextNode("Bild: ");

    // create the input field
    var fieldInput = document.createElement("input");

    // set the optiuons
    fieldInput.setAttribute("type", "file");
    fieldInput.setAttribute("name", "file-" + fieldId);

    // append the text to the container
    fieldContainer.appendChild(fieldText);

    // append the input file to the container
    fieldContainer.appendChild(fieldInput);

    // get the fields list
    var fieldsList = document.getElementById("Fields");

    // append the container to the fields list
    fieldsList.appendChild(fieldContainer);

    // increase the field count
    document.getElementById("FieldsCount").value = fieldId;
}

// Toggles a Field / DIV
function toggleField(target, source)
{
    // get the status
    if(document.getElementById(source).checked)
	{
		document.getElementById(target).style.display = "block";
	}
	else
	{
		document.getElementById(target).style.display = "none";
	}
}

// starts the upload
function startUpload()
{
    // change the buttons value
    document.getElementById("UploadSubmit").value="Bitte warten...";

    // disable the button
    document.getElementById("UploadSubmit").disabled = true;
}

// highlighs a field
function hF(target)
{
    target.focus();
    target.select();
}

// Reports an Image
function reportImage(baseUrl, imageId)
{
    // report it?
    if(confirm('Soll dieses Bild wirklich gemeldet werden?\nMissbrauch dieser Funktion wird mit einer Sperre geahndet.')) {
        // build the request url
        var url = baseUrl + "melden/" + imageId + ".html";

        // execute the request
        MakeHttpRequest(url);

        // clear the span
        document.getElementById("ReportImage" + imageId).innerHTML = "Bild gemeldet";
    }
}

// Shows the opinion fields
function newOpinion()
{
    // hide the list
    document.getElementById("OpinionsList").style.display = "none";

    // show the form
    document.getElementById("NewOpinion").style.display = "block";
}