﻿function addHandler(target, eventName, handlerName) {
    if (target.addEventListener)
        target.addEventListener(eventName, handlerName, false);
    else if (target.attachEvent)
        target.attachEvent("on" + eventName, handlerName);
    else
        target["on" + eventName] = handlerName;
}

function clickButton(element) {
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 8)
        element.parentNode.click();
}

function navigateResults(start) {
    document.searchResultForm.start.value = start;
    document.searchResultForm.submit();
}

function resetForm() {
    document.loginForm.reset();
}

function startLogin() {
	$('#login').fadeTo('fast', 0.5);
	$('#loginThrobber').show();
}

function resizeComponent() {
    if (document.getElementById("newsandhighlightspane") != null)
	    $("#newsandhighlightspane").height(150);
        var rightBottom = $("#ls-ContentBottom").offset().top + $("#ls-ContentBottom").height();
		var leftBottom = $("#newsandhighlightspane").offset().top + $("#newsandhighlightspane").height();
		if (leftBottom < rightBottom) {
			var newHeight = (rightBottom - leftBottom) + $("#newsandhighlightspane").height();
			$("#newsandhighlightspane").height(newHeight);
		}
}

function scroll(elementid, offset) {
    var element = document.getElementById(elementid);
    var margin = parseInt(element.style.marginTop);

    margin += offset;

    element.style.marginTop = String(margin) + "px";
    
    // top of scroll
    if (offset > 0 && margin > 0) {
        stopScroll();

        return;
    }
    
    // bottom of scroll
    if (offset < 0 && element.parentNode.offsetHeight > element.offsetHeight + margin - offset) {
        stopScroll();

        return;
    }
}

function setRotation() {
    if (rotation != null)
        isRotationPaused = !isRotationPaused;
}

function startScroll(elementid, offset) {
    interval = window.setInterval(function () { scroll(elementid, offset); }, 100);
}

function stopScroll() {
    if (interval != null) {
        window.clearInterval(interval);

        interval = null;
    }
}

function submitForm(e) {
    var keycode;

    if (window.event) {
        keycode = window.event.keyCode;
    }
    else if (e) {
        keycode = e.which;
    }
    else {
        return true;
    }
    
    if (keycode == 13) {
        document.getElementById('loginButton').click();
        return false;
    }
    else {
        return true;
    }
}

function switchBanner() {
    if (!isRotationPaused) {
        var elementList = document.getElementById("thumbnail").getElementsByTagName("div");

        if (currentAd < elementList.length)
            toggleBanner(currentAd + 1, false);
        else
            toggleBanner(1, false);
    }
}

function toggleBanner(adNumber, isSelected) {
    if (isSelected && rotation != null) {
        window.clearInterval(rotation);

        rotation = null;
    }

    var elementList = document.getElementById("banner").getElementsByTagName("div");

    for (var i = 0; i < elementList.length; i++)
        if (elementList[i].id.indexOf("ad") == 0)
            elementList[i].style.display = "none";

    elementList = document.getElementById("thumbnail").getElementsByTagName("div");

    for (var i = 1; i <= elementList.length; i++)
        elementList[i - 1].className = "thumbnail_disabled";

    document.getElementById("thumbnail" + adNumber).className = "thumbnail";
    document.getElementById("ad" + adNumber).style.display = "block";

    currentAd = adNumber;
}

function validateForm(form) {
    switch (form.name) {
        case "emailUsForm":
            var sb = "";

            if (document.emailUsForm.first.value.length == 0)
                sb += "First name\n";
            if (document.emailUsForm.last.value.length == 0)
                sb += "Last name\n";
            if (document.emailUsForm.phone.value.length == 0)
                sb += "Phone (xxxxxxxxx)\n";
            if (document.emailUsForm.time.selectedIndex < 1)
                sb += "Best time to call\n";
            if (document.emailUsForm.city.value.length == 0)
                sb += "City\n";
            if (document.emailUsForm.state.selectedIndex < 1)
                sb += "State\n";
            if (document.emailUsForm.zip.value.length == 0)
                sb += "Zip code (xxxxx)\n";
            if (document.emailUsForm.inquiry.selectedIndex < 1)
                sb += "Type of inquiry\n";
            if (document.emailUsForm.question.selectedIndex < 1 && document.emailUsForm.question.disabled == false && document.emailUsForm.question.options.length > 1)
                sb += "How may we help you?\n";
            if (document.emailUsForm.information.value.length == 0)
                sb += "Additional information\n";
			if (document.emailUsForm.recaptcha_response_field.value.length == 0)
                sb += "Challenge words\n";
				
            if (sb.length > 0) {
                alert("Please enter the required fields:\n" + sb);
            }
            else {
                if (document.emailUsForm.phone.value.length != 10) {
                    alert("Please enter a 10 digit phone number");
                    document.emailUsForm.phone.focus();
                }
                else {
                    if (document.emailUsForm.zip.value.length != 5) {
                        alert("Please enter a 5 digit zip code");
                        document.emailUsForm.zip.focus();
                    }
                    else {
                        var regx = new RegExp("^[A-Z0-9\._%\+\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4}$", "i");

                        if (document.emailUsForm.email.value.length > 0 && !regx.test(document.emailUsForm.email.value)) {
                            alert("Please enter a valid email address");
                            document.emailUsForm.email.focus();
                        }
                        else {
                            document.emailUsForm.method = "get"; // value set inline overridden at runtime
                            return true;
                        }
                    }
                }
            }
            break;
        case "loginForm":
            if (document.getElementById("usernamefield").value.length == 0 || document.getElementById("passwordfield").value.length == 0) {
                alert("Please enter your credentials.");
            }
            else {
                window.setTimeout(startLogin, 500);

                if ($('#usernamefield').val()!="test") return true;
            }
            break;
        case "locationForm":
            if (document.locationForm.city.value.length > 0 || document.locationForm.zip.value.length > 0) {
                if (document.locationForm.zip.value.length > 0 && document.locationForm.zip.value.length != 5) {
                    alert("Please enter a 5 digit zip code");
                    document.locationForm.zip.focus();
                }
                else {
                    return true;
                }
            }
            else {
                alert("Please enter at least a city or zip code.");
            }
            break;
        case "rateForm":
            if (document.rateForm.zip.value.length == 0) {
                alert("Please enter a zip code.");
                document.rateForm.zip.focus();
            }
            else {
                document.rateForm.method = "get"; // value set inline overridden at runtime
                return true;
            }
            break;
        case "searchForm":
            if (document.searchForm.q.value.length == 0) {
                alert("Please enter a search criteria.");
                document.searchForm.q.focus();
            }
            else {
                document.searchForm.method = "get"; // value set inline overridden at runtime
                return true;
            }
            break;
    }

    return false;
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}

$(window).unload(function() {
  	$('#loginThrobber').hide(); // reset login box
});

$(document).ready(function() {

	//login box action handler
	$('#application_login').change(function() {
		startLogin();
		var str = "";
        $("select option:selected").each(function () {
            str += $(this).val();
        });
        window.location=str;
	});

	document.getElementById("application_login").selectedIndex=0;
	$('.zebra tbody tr:nth-child(even)').addClass('rowalternate line2'); //table zebra striping
	$('.zebra tbody tr:nth-child(odd)').addClass('row line2'); //table zebra striping
	
	$('.expandable_body').hide();   //function for hide/show inline widgets
			
	$('.expandable_title').click(function() {
		if ($(this).hasClass('open'))
		{
			$(this).nextAll('div.expandable_body').first().hide();
			$(this).removeClass('open');
			resizeComponent();
		}
		else {		
			$(this).nextAll('div.expandable_body').first().show();
			$(this).addClass('open');
			resizeComponent();
		}
	});

    document.getElementById("ls-ContentBottom").onmouseover = resetMenu;
    document.getElementById("ls-MainNavigationLink").onmouseout = resetMenu;

    // apply online
	if (document.getElementById("application_apply") != null)
		document.getElementById("application_apply").selectedIndex = -1;

    // products & services
    if (document.getElementById("productsandservicespane") != null) {
        var scrollingDiv = document.getElementById("productsandservices");

        var sectionIndex = -1;
        var linkIndex = -1;
        var elementList = scrollingDiv.getElementsByTagName("table")[0].rows;

        // find section with active link and last link in that section
        for (var i = 0; i < elementList.length; i++) {
            if (elementList[i].cells[0].className == "text1 text3") {
                if (linkIndex == -1)
                    sectionIndex = i; // section
                else
                    break; // done
            } else {
                if (elementList[i].cells[0].getElementsByTagName("a")[0].href.toLowerCase().indexOf(page) != -1) {
                    elementList[i].cells[0].getElementsByTagName("a")[0].className = "";
                    linkIndex = i;
                } else {
                    if (linkIndex != -1)
                        linkIndex = i; // active link or link in section with active link
                }
            }
        }

        if (scrollingDiv.parentNode.offsetHeight - 10 <= scrollingDiv.offsetHeight) {
            $(function () {
                $('#productsandservicespane').jScrollPane({ showArrows: true, arrowScrollOnHover: true, verticalDragMinHeight: 13, verticalDragMaxHeight: 13 });
            });

            if (sectionIndex > 0) {
                // move section with active link to top of box - first section already there
                var maxOffset = scrollingDiv.offsetHeight - parseInt(scrollingDiv.offsetHeight);

                var api = $('#productsandservicespane').data('jsp');

                if ((Math.round(elementList[sectionIndex].offsetTop / 13) + 1) * 13 > maxOffset) {
                    // scroll down as much as possible
                    api.scrollByY((Math.round(maxOffset / 13) + 1) * 13);
                } else {
                    api.scrollByY((Math.round(elementList[sectionIndex].offsetTop / 13) + 1) * 13);
                }
            }
        }
    }

    // news & highlights
    if (document.getElementById("morenewsandhighlights") == null) {
        var scrollingDiv = document.getElementById("newsandhighlights");

        if (scrollingDiv.parentNode.offsetHeight - 10 <= scrollingDiv.offsetHeight)
            $(function () {
                $('#newsandhighlightspane').jScrollPane({ showArrows: true, arrowScrollOnHover: true, verticalDragMinHeight: 13, verticalDragMaxHeight: 13 });
            });
    } 

    closeMenu();

    if (document.getElementById("frame") != null) {
        // make iframe as tall as possible without changing follow us height
        var height = copyHeight - (document.getElementById("ls-ContentBottom").offsetHeight - document.getElementById("frame").offsetHeight);

        if (height > document.getElementById("frame").offsetHeight)
            document.getElementById("frame").height = String(height) + "px";
    }

    if (layout == "home") {
        if (document.getElementById("banner") != null)
            document.getElementById("banner").style.height = "341px";

        toggleBanner(1, false);

        var adCount = document.getElementById("thumbnail").getElementsByTagName("div").length;

        if (adCount > 1) {
            var height = (adCount * 15) + (adCount + 1) * 9; // thumbnails + spacing

            document.getElementById("thumbnail").style.height = String(height) + "px";
            document.getElementById("thumbnail").style.marginTop = String(parseInt(document.getElementById("banner").style.height) - height) + "px";

            document.getElementById("thumbnail").style.display = "block";

            rotation = window.setInterval(function () { switchBanner(); }, SPEED);
        }
    } else {
        if (document.getElementById("banner") != null)
            document.getElementById("banner").style.height = "211px";

        if (document.getElementById("newsandhighlightspane") != null) {
			var rightBottom = $("#ls-ContentBottom").offset().top + $("#ls-ContentBottom").height();
			var leftBottom = $("#newsandhighlightspane").offset().top + $("#newsandhighlightspane").height();
			if (leftBottom < rightBottom) {
				var newHeight = (rightBottom - leftBottom) + $("#newsandhighlightspane").height();
				$("#newsandhighlightspane").height(newHeight);
			}
        }
    }

    if (document.location.search.length == 0)
        if (document.emailUsForm)
            document.emailUsForm.first.focus();
        else
            document.searchForm.q.focus();
			
});
