﻿var XmlHttpContactForm;
var TimeToFade = 700.0;
var AdDisplayTime = 5000.0;
var TimeoutDelay = 50;
var CurrentFrontPageAdIndex = 0, LastFrontPageAdIndex = 1;
var divFpAdPrefix = 'divFrontPageAd';
var txtNameDefaultValue = 'Navn', txtEmailDefaultValue = 'E-post', txtPhoneDefaultValue = 'Telefon';
var NavImgL, NavImgR;
var DivAd1Id = '', DivAd2Id = '', DivAd3Id = '', ImgAdNavLeftId = '', ImgAdNavRightId = '';

function ChangeFrontPageAd(div1Id, div2Id, div3Id, goNext, navImgLeftId, navImgRightId)
{
    var div1 = document.getElementById(div1Id);
    var div2 = document.getElementById(div2Id);
    var div3 = document.getElementById(div3Id);
    DivAd1Id = div1Id;
    DivAd2Id = div2Id;
    DivAd3Id = div3Id;
    NavImgL = document.getElementById(navImgLeftId);
    NavImgR = document.getElementById(navImgRightId);
    ImgAdNavLeftId = navImgLeftId;
    ImgAdNavRightId = navImgRightId;
    if (div1 && div2 && div3)
    {
        var prevAdIndex = CurrentFrontPageAdIndex;
        var prevAdDivId = '', nextAdDivId = '';
        if (goNext)
        {
            CurrentFrontPageAdIndex += 1;
            if (CurrentFrontPageAdIndex > LastFrontPageAdIndex)
            {
                CurrentFrontPageAdIndex = 0;
            }
        }
        else
        {
            CurrentFrontPageAdIndex -= 1;
            if (CurrentFrontPageAdIndex < 0)
            {
                CurrentFrontPageAdIndex = LastFrontPageAdIndex;
            }
        }
        switch (prevAdIndex)
        {
            case 0:
                {
                    prevAdDivId = div1Id;
                    break;
                }
            case 1:
                {
                    prevAdDivId = div2Id;
                    break;
                }
            case 2:
                {
                    prevAdDivId = div3Id;
                    break;
                }
        }        
        switch (CurrentFrontPageAdIndex)
        {
            case 0:
                {
                    nextAdDivId = div1Id;
                    break;
                }
            case 1:
                {
                    nextAdDivId = div2Id;
                    break;
                }
            case 2:
                {
                    nextAdDivId = div3Id;
                    break;
                }
        }
        HideUnusedAdBlock(div1Id, prevAdDivId, nextAdDivId);
        HideUnusedAdBlock(div2Id, prevAdDivId, nextAdDivId);
        HideUnusedAdBlock(div3Id, prevAdDivId, nextAdDivId);
        if (NavImgL)
        {
            NavImgL.style.visibility = 'hidden';
        }
        if (NavImgR)
        {
            NavImgR.style.visibility = 'hidden';
        }
        FadeElement(prevAdDivId, nextAdDivId);
    }
}

function HideUnusedAdBlock(blockId, prevAdDivId, nextAdDivId)
{
    if ((blockId != prevAdDivId) && (blockId != nextAdDivId))
    {
        document.getElementById(blockId).style.display = 'none';
    }
}

function ButtonMO(btn, isActive, src1, src2, imgUrl)
{
    if (btn)
    {
        if (isActive)
        {
            btn.src = imgUrl + src2;
        }
        else
        {
            btn.src = imgUrl + src1;
        }
    }
}

function ChildButtonMO(domObj, isActive)
{
    var cn = domObj.childNodes;
    if (cn.length > 0)
    {
        var t = '';
        for (var i = 0;i<cn.length;i++)
        {
            if ((cn[i].tagName != null) && (cn[i].tagName.toLowerCase() == 'img'))
            {
                var src = cn[i].src;
                if (isActive)
                {
                    cn[i].src = src.replace(".gif","_mo.gif");
                }
                else
                {
                    cn[i].src = src.replace("_mo.gif",".gif");
                }
            }
        }
    }
}

function ContactFormSubmitted()
{
    if (XmlHttpContactForm.readyState == 4 || XmlHttpContactForm.readyState == "complete")
    { 
        var rt = XmlHttpContactForm.responseText;
        document.getElementById('ContactForm').innerHTML = rt;
        
    }
}

function SubmitContactForm(ajaxUrl, txtNameId, txtEmailId, txtPhoneId, currentWebPageId, currentWebPageTitle, currentWebPageRelativeUrl, currentWebPageFullUrl)
{
    var txtName = document.getElementById(txtNameId);
    var txtEmail = document.getElementById(txtEmailId);
    var txtPhone = document.getElementById(txtPhoneId);
    if (txtName && txtEmail && txtPhone)
    {
        var name = txtName.value;
        var email = txtEmail.value;
        var phone = txtPhone.value;
        var formError = false;
        if ((name == "") || (name == txtNameDefaultValue))
        {   
            formError = true;
        }
        if (((email == "") || (email == txtEmailDefaultValue)) && ((phone == "") || (phone == txtPhoneDefaultValue)))
        {
            formError = true;
        }
        if (formError)
        {
            alert("Navn og e-post og/eller telefonnumer må fylles ut.");
        }
        else
        {
            if (email == txtEmailDefaultValue)
            {
                email = "";
            }
            if (phone == txtPhoneDefaultValue)
            {
                phone = "";
            }
            var ajaxParams = 'Name=' + name;
            ajaxParams += '&Email=' + email;
            ajaxParams += '&Phone=' + phone;
            ajaxParams += '&WebPageId=' + currentWebPageId;
            ajaxParams += '&WebPageTitle=' + currentWebPageTitle;
            ajaxParams += '&WebPageRelativeUrl=' + currentWebPageRelativeUrl;
            ajaxParams += '&WebPageFullUrl=' + currentWebPageFullUrl;
            XmlHttpContactForm = new GetXmlHttpObject();
            if (XmlHttpContactForm == null)
            {
                return;
            }
            else
            {
                document.getElementById('ContactForm').innerHTML = "Sender...";
                XmlHttpContactForm.open("POST",ajaxUrl,true);
                XmlHttpContactForm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                XmlHttpContactForm.setRequestHeader("Content-length", ajaxParams.length);
                XmlHttpContactForm.setRequestHeader("Connection", "close");
                XmlHttpContactForm.onreadystatechange = ContactFormSubmitted;
                XmlHttpContactForm.send(ajaxParams);
            }
        }
    }
}

function ContactFormFieldFocus(cfField, hasFocus, fieldType)
{
    var defaultValue = '';
    switch(fieldType)
    {
        case "name" :
        {
            defaultValue = txtNameDefaultValue;
            break;
        }
        case "email" :
        {
            defaultValue = txtEmailDefaultValue;
            break;
        }
        case "phone" :
        {
            defaultValue = txtPhoneDefaultValue;
        }
    }
    if ((cfField) && (defaultValue != ""))
    {
        if (hasFocus)
        {
            if (cfField.value == defaultValue)
            {
                cfField.value = "";
            }
        }
        else
        {
            if ((cfField.value == defaultValue) || (cfField.value == ""))
            {
                cfField.value = defaultValue;
            }
        }
    }
}


function FadeElement(elmId, nextElmId)
{
    var element = document.getElementById(elmId);
    if (element == null)
    {
        return;
    }
    if (element.FadeState == null)
    {
        if (element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1')
        {
            element.FadeState = 2;
        }
        else
        {
            element.FadeState = -2;
        }
    }
    if (element.FadeState == 1 || element.FadeState == -1)
    {
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
    }
    else
    {
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade;
        setTimeout("ExecFade(" + new Date().getTime() + ",'" + elmId + "','" + nextElmId + "')", TimeoutDelay);
    }
}

function ExecFade(lastTick, elmId, nextElmId)
{
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
    var element = document.getElementById(elmId);
    if (element.FadeTimeLeft <= elapsedTicks)
    {
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
        element.FadeState = element.FadeState == 1 ? 2 : -2;
        if ((element.FadeState == -2) && (nextElmId != ''))
        {
            document.getElementById(nextElmId).style.opacity = '0';
            document.getElementById(nextElmId).style.filter = 'alpha(opacity = 0)';
            document.getElementById(nextElmId).style.display = 'block';
            document.getElementById(elmId).style.display = 'none';
            FadeElement(nextElmId, '', '');
        }
        else if ((element.FadeState == 2))
        {
            if (NavImgL)
            {
                NavImgL.style.visibility = 'visible';
            }
            if (NavImgR)
            {
                NavImgR.style.visibility = 'visible';
            }
            setTimeout("ChangeFrontPageAd('" + DivAd1Id + "','" + DivAd2Id + "','" + DivAd3Id + "',true,'" + ImgAdNavLeftId + "','" + ImgAdNavRightId + "')", AdDisplayTime);
        }
        return;
    }
    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft / TimeToFade;
    if (element.FadeState == 1)
    {
        newOpVal = 1 - newOpVal;
    }
    element.style.opacity = newOpVal;
    element.style.filter = 'alpha(opacity = ' + (newOpVal * 100) + ')';
    setTimeout("ExecFade(" + curTick + ",'" + elmId + "','" + nextElmId + "')", TimeoutDelay);
}

function Mail(name)
{
    open('mailto:' + name + '@infotech.no');
}