//Functions for Image blending
function opacity(id, opacStart, opacEnd, millisec)
{
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd)
	{
		for(i = opacStart; i >= opacEnd; i--)
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd)
	{
		for(i = opacStart; i <= opacEnd; i++)
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id)
{
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec)
{
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0)
	{
		opacity(id, 0, 100, millisec);
	}
	else
	{
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec)
{
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++)
	{
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec)
{
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100)
	{
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

//Functions for Ajax
var gstrLastCall = "";
var gstrObjectName = "";
var ajaxCall = GetNewHTTPObject();

function GetNewHTTPObject()
{
    var xmlhttp;
    var strAgent = navigator.userAgent.toLowerCase();
    var blnIsIE  = ((strAgent.indexOf("msie") != -1) && (strAgent.indexOf("opera") == -1));

    //IE 7 nicht mehr mit ActiveX Objekt
    if (blnIsIE && (navigator.appVersion < 4 || strAgent.indexOf("msie 4") != -1 ||
                                                strAgent.indexOf("msie 5.0") != -1 ||
                                                strAgent.indexOf("msie 5.5") != -1 ||
                                                strAgent.indexOf("msie 6.") != -1))
    {
        /** Special IE only code ... */
        /*@cc_on
        @if (@_jscript_version >= 5)
            try
            {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (E)
                {
                    xmlhttp = false;
                }
            }
        @else
            xmlhttp = false;
        @end @*/
    }

    /** Every other browser on the planet */
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
        try
        {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e)
        {
            xmlhttp = false;
        }
    }

    return xmlhttp;
}

function GetDynamicData(fstrObjectName, fstrKind, fstrParam1, fstrParam2)
{
    var strParam1 = escape(fstrParam1);
    strParam1 = strParam1.replace("@", "_AT_");
    strParam1 = strParam1.replace("*", "_AST_");
    strParam1 = strParam1.replace("/", "_SLA_");
    strParam1 = strParam1.replace("+", "_PLUS_");

    var strParam2 = escape(fstrParam2);
    strParam2 = strParam2.replace("@", "_AT_");
    strParam2 = strParam2.replace("*", "_AST_");
    strParam2 = strParam2.replace("/", "_SLA_");
    strParam2 = strParam2.replace("+", "_PLUS_");

    if (fstrObjectName.indexOf("&TT&") != -1) { fstrObjectName = fstrObjectName.substring(4); }
    else                                      { historyKeeper.addSite(fstrObjectName + "&TT&" + fstrKind+ "&TT&" + fstrParam1+ "&TT&" + fstrParam2); }
    gstrObjectName = fstrObjectName;

    if (gstrLastCall != (escape(fstrKind) + "&P1=" + strParam1 + "&P2=" + strParam2))
    {
        var url = "e3k_App_Code/aspx/ajaxhelp.aspx?K=" + escape(fstrKind) + "&P1=" + strParam1 + "&P2=" + strParam2;

        ajaxCall.open('GET', url, true);
        ajaxCall.onreadystatechange = CallbackFunction;
        ajaxCall.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        ajaxCall.send(null);
    }

    gstrLastCall = escape(fstrKind) + "&P1=" + strParam1 + "&P2=" + strParam2;
    return;
}

function CallbackFunction()
{
    if (ajaxCall.readyState >= 1 && ajaxCall.readyState <= 3)
    {
        //Display waiting image and message while content loads
        //DisplayBlock("boxpleasewaitbg", true)
        DisplayBlock("boxpleasewait", true)
    }
    else if (ajaxCall.readyState == 4 && ajaxCall.status == 200)
    {
        //setTimeout(function(){document.getElementById(gstrObjectName).innerHTML = ajaxCall.responseText;}, 10);

        //Hidden waiting image and message
        //DisplayBlock("boxpleasewaitbg", false)
        DisplayBlock("boxpleasewait", false)
        var strResponse = ajaxCall.responseText;

        if (strResponse.indexOf("|||") != -1)
        {
            var strArrResponse = strResponse.split("|||");

            for (var intCtr = 0; intCtr < strArrResponse.length; intCtr++)
            {
                var strCode = strArrResponse[intCtr].substring(0, 2).toUpperCase();
                var strText = strArrResponse[intCtr].substring(3);

                if (strCode == "BI")                                            //Blendimage
                {
                    blendimage('mainheader', 'blendimage', 'e3k_App_Themes/Default/images/' + strText, 2000);
                }
                else if (strCode == "TH")                                       //Text header
                {
                    document.getElementById("maincontentheader").innerHTML = strText;
                }
                else if (strCode == "CT")                                       //Content
                {
                    document.getElementById(gstrObjectName).innerHTML = strText;
                    scroll(0, 0);                                               //scroll to top when fill new content
                }
                else if (strCode == "EL")                                       //extern link
                {
                    if (strText.indexOf("http://") == -1 && strText.indexOf("ftp://") == -1) strText = "http://" + strText;
                    window.open(strText, "ExternWindow", "");
                }
                else if (strCode == "ML")                                       //Menu left
                {
                    document.getElementById("mainmenuleft").innerHTML = strText;
                }
                else if (strCode == "MT")                                       //Menu top
                {
                    document.getElementById("mainmenu").innerHTML = strText;
                }
                else if (strCode == "LG")                                       //Login / Logout
                {
                    if (strText == "IN")
                    {
                        document.getElementById("btnlogin").value = "Logout";
                        document.getElementById("username").value = "";
                        document.getElementById("username").disabled = true;
                        document.getElementById("password").value = "";
                        document.getElementById("password").disabled = true;
                    }
                    else
                    {
                        document.getElementById("btnlogin").value = "Login";
                        document.getElementById("username").disabled = false;
                        document.getElementById("password").disabled = false;
                    }
                }
                else if (strCode == "VL")                                       //Fill value
                {
                    if (strText.indexOf("@T|@") != -1)
                    {
                        var strArrText = strText.split("@T|@");
                        document.getElementById(strArrText[0]).value = strArrText[1];
                    }
                    else
                    {
                        document.getElementById(gstrObjectName).value = strText;
                    }
                }
            }
        }
        else
        {
            document.getElementById(gstrObjectName).innerHTML = strResponse;
        }
    }
    else                                                                        //Fehlerfall
    {
        //Hidden waiting image and message
        //DisplayBlock("boxpleasewaitbg", false)
        //alert(ajaxCall.responseText);
        DisplayBlock("boxpleasewait", false)

        var strError = "<p>Es ist ein Fehler aufgetreten, bitte versuchen Sie es erneut. Sollte diese Meldung wieder erscheinen, ";
        strError += "nehmen Sie bitte mit uns Kontakt &uuml;ber folgende E-Mailadresse: <a href='mailto:info@europa3000.ch?subject=Problem:%20europa3000%20Website'>info@europa3000.ch</a> ";
        strError += "auf und schildern das Problem. Danke.</p>";

        strError += "<p>An error occured, could you please try again. If this message remains, please contact us by using the following e-mail-address: ";
        strError += "<a href='mailto:info@europa3000.ch?subject=Problem:%20europa3000%20Website'>info@europa3000.ch</a> and describe the problem. Thank you.</p>";
        document.getElementById("mainright").innerHTML = strError;
    }

    return;
}

function HtmlEdit(id, tbl, fld)
{
    window.open("e3k_App_Code/asp/htmledit.asp?Q=" + id + "&T=" + tbl + "&F=" + fld, null, "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=800,height=760,top=20,left=50");
}

function NewWindow(s, width, height, mode, name)
{
  if (!name) name = "child";
  hWd = window.open(s, name, "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+mode+",resizable=1,width="+width+",height="+height+",top=20,left=20");
  hWd.focus();
}

// Set visibility of Elements
function DisplayBlock(fstrobjName, fblnDisplay)
{
    var strDisplay = "";

	if (document.layers)
	{
		strDisplay = (fblnDisplay) ? 'block' : 'none';
		document.layers[fstrobjName].display = strDisplay;
	}
	else if (document.all)
	{
		strDisplay = (fblnDisplay) ? 'block' : 'none';
		document.all[fstrobjName].style.display = strDisplay;
	}
	else if (document.getElementById)
	{
		strDisplay = (fblnDisplay) ? 'block' : 'none';
		document.getElementById(fstrobjName).style.display = strDisplay;
	}
}

// Start search function
function StartSearch()
{
    var strSearch = document.getElementById("search").value;
    GetDynamicData('mainright', 'Search', strSearch, '');

    return;
}

// Login
function LoginProof()
{
    if (document.getElementById("btnlogin").value == "Login")
    {
        var strusr = document.getElementById("username").value;
        var strpwd = document.getElementById("password").value;
        GetDynamicData('&TT&mainright', 'Login', strusr, strpwd);
    }
    else
    {
        blnLoginLook = false;
        GetDynamicData('&TT&mainright', 'Logout', '', '');
    }

    return;
}

// Handle Enter key on page
var blnLoginLook = false;

function HandleKeyPress(e)
{
    var key = e.keyCode || e.which;

    // Enter key
    if (key == 13)
    {
        if (document.getElementById("search").value.length != 0)
        {
            StartSearch();
        }
        else if (document.getElementById("username").value.length != 0)
        {
            LoginProof();
        }
    }
    else if (document.getElementById("username").disabled != true)
    {
        if ((document.getElementById("username").value.length >= 3) && (blnLoginLook == false))
        {
            blnLoginLook = true;
            GetDynamicData('&TT&password', 'LoginLook', document.getElementById("username").value, "");
        }
    }
}

// Start routine for website
function e3kStartScript()
{
    //Resize min-height of maincontent
	if (window.opera || navigator.userAgent.indexOf("Opera") != -1)
	{
		document.getElementById("maincontent").style.minHeight = (screen.availHeight - 580) + "px";
	}
    else
	{
		document.getElementById("maincontent").style.minHeight = (screen.availHeight - 350) + "px";
	}

    //Is screen to small, linkMenu hide
    if (screen.availHeight < 810) DisplayBlock("mainmenuleft", false);
}

// Show Box at Mouse Position
function Box_Show(fstrBoxText)
{
    var divbox = document.createElement('div');
    divbox.setAttribute("id", "newsbox");
    divbox.innerHTML = "<p class='newsboxp'>" + fstrBoxText + "</p>";
    divbox.style.top = (gintPosY + 5) + 'px';
    divbox.style.left = (gintPosX + 5) + 'px';

    var form = document.getElementById('maindiv');
    void(form.appendChild(divbox));
}

function Box_Hide()
{
    var form = document.getElementById('maindiv');
    var olddivbox = document.getElementById('newsbox');
    form.removeChild(olddivbox);
}

//Set Mouse positions in gintPosX and gintPosY variable
var gintPosX = 0;
var gintPosY = 0;
var blnIsIE = document.all?true:false;
if (!blnIsIE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e)
{
    if (blnIsIE)
    {
        // grab the x-y pos.s if browser is IE
        gintPosX = event.clientX + document.body.scrollLeft;
        gintPosY = event.clientY + document.body.scrollTop;
    }
    else
    {
        // grab the x-y pos.s if browser is NS
        gintPosX = e.pageX;
        gintPosY = e.pageY;
    }

    if (gintPosX < 0){gintPosX = 0;}
    if (gintPosY < 0){gintPosY = 0;}  
    return true;
}