﻿function adaptHeight() {
    var col1 = document.getElementById('col1');
    var col2 = document.getElementById('col2');
    var col3 = document.getElementById('col3');

    if (col3) {
        if (col1.offsetHeight) {
            if (col1.offsetHeight >= col2.offsetHeight && col1.offsetHeight >= col3.offsetHeight) {
                col2.style.height = col1.offsetHeight + 'px';
                col3.style.height = col1.offsetHeight + 'px';
            } else if (col2.offsetHeight >= col1.offsetHeight && col2.offsetHeight >= col3.offsetHeight) {
                col1.style.height = col2.offsetHeight + 'px';
                col3.style.height = col2.offsetHeight + 'px';
            } else if (col3.offsetHeight >= col1.offsetHeight && col3.offsetHeight >= col2.offsetHeight) {
                col1.style.height = col3.offsetHeight + 'px';
                col2.style.height = col3.offsetHeight + 'px';
            }
            
        } else {
            if (col1.offsetHeight >= col2.clientHeight && col1.clientHeight >= col3.clientHeight) {
                col2.style.height = col1.clientHeight;
                col3.style.height = col1.clientHeight;
            } else if (col2.clientHeight >= col1.clientHeight && col2.clientHeight >= col3.clientHeight) {
                col1.style.height = col2.clientHeight;
                col3.style.height = col2.clientHeight;
                
            } else if (col3.clientHeight >= col1.clientHeight && col3.clientHeight >= col2.clientHeight) {
                col1.style.height = col3.clientHeight;
                col2.style.height = col3.clientHeight;
            } else {
                if (col1.clientHeight >= col2.clientHeight) {
                    col2.style.height = col1.clientHeight;
                } else {
                    col1.style.height = col2.clientHeight;
                }
            }
        }
    } else {
        if (col1.offsetHeight) {
            if (col1.offsetHeight >= col2.offsetHeight) {
                col2.style.height = col1.offsetHeight + 'px';
            } else {
                col1.style.height = col2.offsetHeight + 'px';
            }
            
        } else {
        if (col1.clientHeight >= col2.clientHeight) {
                col2.style.height = col1.clientHeight;
            } else {
                col1.style.height = col2.clientHeight;
            }

        }
    }

}

function adaptFooter() {
    
    var footerHeight = 323;
    var bodyHeight;
    var footerPosition;

//GET CONTENT HEIGHT:    
    if (document.body.offsetHeight) {
        bodyHeight = document.getElementById('wrap').offsetHeight;
    } else {
        bodyHeight = document.getElementById('wrap').clientHeight;
    }

//IF CONTENT HEIGHT IS HEIGHER THEN WINDOW:
    if (bodyHeight > getWidthHeight()[1]) {
        footerPosition = bodyHeight + 20;
        
    } else {
        //IF THE EMPTY SPACES HEIGHT IS LOWER THEN THE FOOTER:
        if ((getWidthHeight()[1] - bodyHeight) <= footerHeight) {

            footerPosition = getWidthHeight()[1] - 15;   //- (getWidthHeight()[1] - bodyHeight);
        } else {
            footerPosition = getWidthHeight()[1] - 15;  //- footerHeight;
        }
    }

    document.getElementById('wrap_footer').style.top = footerPosition + 'px';
    showFooter();
}

function showFooter() {
    //    document.getElementById('wrap_footer').setAttribute('style', 'display:table');
    document.getElementById('wrap_footer').style.display = 'block';
    //alert(document.getElementById('wrap_footer').style.display);
    
}

function prepareSearch(resultpage) {
//    alert(resultpage + ', ' + treeid);
    //    alert(document.getElementById('searchfield').value);
    //alert(resultpage);
    if (document.getElementById('searchfield')) {
        //alert(resultpage);
        document.location.replace(resultpage + '&SearchString=' + document.getElementById('searchfield').value);
    }
    
}
//********************************************************************************
//WINDOW FUNCTIONS
//********************************************************************************
function getWidthHeight() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}


//********************************************************************************
//EVENT FUNCTIONS
//********************************************************************************
onresize = function() {
    adaptFooter();
}

function validateBookingForm() {
    var valid = true;
    
    if (document.getElementById('BookingName').value.length < 2 || document.getElementById('BookingName').value == 'För och efternamn') {
        valid = false;
        alert('Du måste ange ett namn.');
        document.getElementById('BookingName').style.background = '#f8d9d9';
    } else {
        valid = true
        document.getElementById('BookingName').style.background = '#ffffff';
    }
    
    if (document.getElementById('BookingCompany').value.length < 2 || document.getElementById('BookingCompany').value == 'Ditt företag') {
        valid = false;
        alert('Du måste ange ett företag.');
        document.getElementById('BookingCompany').style.background = '#f8d9d9';
    } else {
        valid = true
        document.getElementById('BookingCompany').style.background = '#ffffff';
    }
 
    if (document.getElementById('BookingPhone').value.length < 2 || document.getElementById('BookingPhone').value == 'Företag/Mobil') {
        valid = false;
        alert('Du måste ange ett telefonnummer.');
        document.getElementById('BookingPhone').style.background = '#f8d9d9';
    } else {
        valid = true
        document.getElementById('BookingPhone').style.background = '#ffffff';
    }
    
    if (document.getElementById('BookingEmail').value.length < 5 || document.getElementById('BookingEmail').value == 'din@email.se') {
        valid = false;
        alert('Du måste ange en e-postadress.');
        document.getElementById('BookingEmail').style.background = '#f8d9d9';
    } else {
        valid = true
        document.getElementById('BookingEmail').style.background = '#ffffff';
    }

    if (valid) {
        document.forms[0].submit();
        return true;
    } else {
        return false;
    }
}

function handleKeyPress(e, form, page) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        prepareSearch(page);
        return false;
    }
    return true;
}