﻿$(function() {
    $.fn.resize = function(a) { var d = Math.ceil; if (a == null) a = 200; var e = a, f = a; $(this).each(function() { var b = $(this).height(), c = $(this).width(); if (b > c) f = d(c / b * a); else e = d(b / c * a); $(this).css({ height: e, width: f }) }) };
    $('.logg-inn-knapp').click(function() {
        //blockUI leker ikke bra sammen med påloggingsdelen (Form is null - feilmelding)
        //        $.blockUI({
        //            message: $('#divautotalLogin'),
        //            css: { width: '250px' }
        //        });
        //        $('.blockOverlay').attr('title', 'Klikk for å lukke').click($.unblockUI);
        popup('divautotalLogin', 800, 650);
        return false;
    });


    //***********************************************
    //PREMIUMPARAM
    //***********************************************
    if ($('#ctl00_FullRegion_ucpremiumParam_lbKundeEtterNavn').length) {
        $('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData').change(function(event) {
            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '0') {
                $('#ctl00_FullRegion_ucpremiumParam_lblMeldingTilSelger').html('');
            }
            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '1') {
                $('#ctl00_FullRegion_ucpremiumParam_lblMeldingTilSelger').html('Husk å sende salgsmelding til gammelt selskap.');
            }
            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '2') {
                $('#ctl00_FullRegion_ucpremiumParam_lblMeldingTilSelger').html('Husk å sende inn overføringsskjema.');

            }
        });

        /*Kopier Regcha ved selskapsbytte*/
        $('#ctl00_FullRegion_ucpremiumParam_tbRegChaNr').change(function() {
            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '2') {
                $('#ctl00_FullRegion_ucpremiumParam_tbBonusRegNr').val($('#ctl00_FullRegion_ucpremiumParam_tbRegChaNr').val());
            }
        });


        /*Disable antallmåneder hvis pant er satt til nei*/
        if ($('#ctl00_FullRegion_ucpremiumParam_lblPant option:selected').val() == '0') {
            $('#ctl00_FullRegion_ucpremiumParam_ddAntalMnd').attr('disabled', 'disabled');
        }


        /*Endring av pant*/
        $('#ctl00_FullRegion_ucpremiumParam_ddPant').change(function() {
            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '1') {
                $('#ctl00_FullRegion_ucpremiumParam_ddAntalMnd').attr('disabled', 'disabled');
            }

            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '2') {
                $('#ctl00_FullRegion_ucpremiumParam_ddAntalMnd').removeAttr('disabled');
            }

            if ($('#ctl00_FullRegion_ucpremiumParam_ddBilspesifikkeData option:selected').val() == '3') {
                $('#ctl00_FullRegion_ucpremiumParam_ddAntalMnd').removeAttr('disabled');
            }

        });

        /*Sync på navnefelt*/
        $('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn2').change(function() {
            $('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn').val($('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn2').val());
        });
        $('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn').change(function() {
            $('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn2').val($('#ctl00_FullRegion_ucpremiumParam_tbKundeForNavn').val());
        });

        $('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn2').change(function() {
            $('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn').val($('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn2').val());
        });
        $('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn').change(function() {
            $('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn2').val($('#ctl00_FullRegion_ucpremiumParam_tbKundeEtterNavn').val());
        });
    }

});



//***********************************************
//POPUPS
//***********************************************
function toggle(div_id) {
    var el = document.getElementById(div_id);
    if (el.style.display == 'none') { el.style.display = 'block'; }
    else { el.style.display = 'none'; }
}

function blanket_size(popUpDivVar, divWidth, divHeight) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    //	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
    popUpDiv_height = blanket_height / 2 - divHeight;
    popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar, divWidth, divHeight) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    //window_width=window_width/2-150;//150 is half popup's width
    //	window_width=GetWidth()/2-200;
    window_width = GetWidth() / 2 - divWidth;
    popUpDiv.style.left = window_width + 'px';
    popUpDiv.style.top = (GetHeight() / 2) - 250;
}

function popup(windowname, divWidth, divHeight) {
    var WidthHalfSize = divWidth / 2;
    var HeightHalfSize = divHeight / 2;
    blanket_size(windowname, WidthHalfSize, HeightHalfSize);
    window_pos(windowname, WidthHalfSize, HeightHalfSize);
    toggle('blanket');
    toggle(windowname);
}

function GetWidth() {
    var x = 0;
    if (self.innerHeight) {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}

function GetHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}


//***********************************************
//LOGIN
//***********************************************
function TriggerLogin() {
    //    var uv = $('#usr').val();
    //    var pv = $('#pwr').val();
    //    $('#ctl00_Login1_txtPassword').val(pv);
    //    $('#ctl00_Login1_txtUsername').val(uv);


    //document.getElementById('ctl00_Login1_txtUsername').value();
    //document.getElementById('ctl00_Login1_txtPassword').value();

    document.getElementById('ctl00_Login1_btnLogin').click();
    return false;

}

function setUPvalues() {
    var uv = $('#usr').val();
    var pv = $('#pwr').val();
    $('#ctl00_Login1_txtPassword').val(pv);
    $('#ctl00_Login1_txtUsername').val(uv);
    console.log($('#pwd').val());
    console.log($('#usr').val());
    console.log($('#ctl00_Login1_txtPassword').val());
    console.log($('#ctl00_Login1_txtUsername').val());
}




