﻿var IEVersion = getInternetExplorerVersion();

if (typeof window.addEventListener != 'undefined') {
    window.addEventListener(
        'load',
        function (evt) {
            init();
        },
        false
    );
    window.addEventListener(
        'resize',
        function (evt) {
            AdjustHeights();
        },
        false
    );
}
else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent(
        'onload',
        function () {
            init();
        }
    );
    window.attachEvent(
        'onresize',
        function () {
            AdjustHeights();
        }
    );
}

function init(){

    AdjustHeights();
    
    var SiteSearchKeywords = getElement("SiteSearchKeywords");
    var SiteSearchForm = getElement("SiteSearchForm");
    
    if(SiteSearchKeywords){
        SiteSearchKeywords.style.color = "#a3a3a3";
        SiteSearchKeywords.value = "Search";        
        SiteSearchKeywords.onfocus = function(){
            SiteSearchKeywords.value = "";
            SiteSearchKeywords.style.color = "#000000";
        }
        SiteSearchKeywords.onblur = function(){
            if(SiteSearchKeywords.value == ""){
                SiteSearchKeywords.style.color = "#a3a3a3";
                SiteSearchKeywords.value = "Search";                
            }
        }
    }
    if(SiteSearchForm){
        SiteSearchForm.onsubmit = function(){
            var SiteSearchKeywords = getElement("SiteSearchKeywords");
            if(SiteSearchKeywords){
                if(SiteSearchKeywords.value == "" || SiteSearchKeywords.value == "Search"){
                    return false;
                }
            }
            else{
                return false;
            }
        }
    }
    
}

// --------- template height ---------- //

function AdjustHeights(){    
    
    var WrapperDiv = getElement('Wrapper');
    var FillerDiv = getElement('Body_Middle_Left_Filler');
    var BodyBottomDiv = getElement('Body_Bottom');
    
    var PageHeight = getWindowHeight();
    var TemplateHeight;
    var FillerHeight;
    
    var FillerBottomY;
    var BodyBottomTopY;
    
    if(WrapperDiv && FillerDiv && BodyBottomDiv){        

        // reset filler div        
        setElementHeight(FillerDiv, 1);
        
        // stretch filler div to fit empty space above footer
        FillerBottomY = getElementYPos(FillerDiv) + getElementHeight(FillerDiv);
        BodyBottomTopY = getElementYPos(BodyBottomDiv);   
        
        if(FillerBottomY < BodyBottomTopY){            
            
            // make adjustments
            if(IEVersion > 0 && IEVersion < 8){
                FillerBottomY = FillerBottomY + 187;
            }
            
            if(BodyBottomTopY > FillerBottomY){
                setElementHeight(FillerDiv, BodyBottomTopY - FillerBottomY - 1);
            }    
            
        }  
        
        // stretch filler div to fit page height 
        TemplateHeight = getElementHeight(WrapperDiv);    
        if(PageHeight > TemplateHeight){
            
            FillerHeight = getElementHeight(FillerDiv);
           
            // make adjustments
            if(FillerHeight > 1){
                FillerHeight = FillerHeight + 2;
            }
            
            FillerHeight = FillerHeight + (PageHeight - TemplateHeight);
            
            setElementHeight(FillerDiv, FillerHeight);
            
        }
    }
        
}

// ------------------------------------ //

function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}

function setElementHeight(myobj, ElementHeight){
    myobj.style.height = ElementHeight + "px";
}

function setElementWidth(myobj, ElementWidth){
    myobj.style.width = ElementWidth + "px";
}

function getElementYPos(myobj) {
	var ElementYPos;
	ElementYPos = myobj.offsetTop;
	return ElementYPos;
}

function getElementXPos(myobj) {
    var ElementXPos;
	ElementXPos = myobj.offsetLeft;
	return ElementXPos;
}

function getElementHeight(myobj) {
	var ElementHeight;
	ElementHeight = myobj.clientHeight;
	return ElementHeight;
}

function getElementWidth(myobj) {
    var ElementWidth;
	ElementWidth = myobj.clientWidth;
	return ElementWidth;
}

function getWindowWidth(){
    var WindowWidth;
	
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        WindowWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        WindowWidth = document.documentElement.clientWidth;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        WindowWidth = document.body.clientWidth;
    }
    return WindowWidth;
}

function getWindowHeight(){
    var WindowHeight;
	
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        WindowHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        WindowHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        WindowHeight = document.body.clientHeight;
    }
    return WindowHeight;
}

function getElement(name) {
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	return o1;
}
function showElement(name,displaytype) {
	if(displaytype==undefined){displaytype='';}
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	if(o1!=null) {
		o1.style.display = displaytype;
	}
}
function hideElement(name) {
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	if(o1!=null) {
		o1.style.display = "none";
	}
}
function toggleElement(name,displaytype){
	if(displaytype==undefined){displaytype='';}
	if(getElement(name) != null){
		if(getElement(name).style.display == displaytype || getElement(name).style.display == ""){
			hideElement(name);
		}
		else{
			showElement(name,displaytype);
		}
	}
}
