﻿$(document).ready(function () {
    $('table.suppliers tr:odd').addClass('alt');



    //get current page directory
    var pathname = window.location.pathname;
    var indexEndDir = pathname.lastIndexOf('/');
    var indexStartDir = pathname.substring(0, indexEndDir).lastIndexOf('/');
    var curDir = pathname.substring(indexStartDir + 1, indexEndDir).toLowerCase();

    //set home to content
    if (curDir == 'home') { curDir = 'content' };


    $('#adminNav a').each(function () {
        if (curDir == $(this).text().toLowerCase()) {
            $(this).addClass('current');
        }
        else;
    });




    //Add class of first to every fourth item
    //give last item a class of last
    $("div.product-4-up:nth-child(4n+4)").addClass('last');
    $("div.product-4-up:nth-child(4n+1)").addClass('first');

    //set marketsegment application width
    var numProducts = $("div.product-up").size();
    if (numProducts == '10') { var widthProductsPercentage = '19.75%'; }
    else {
        var widthProductsPercentage = 2 * (Math.floor(100 / numProducts)) + "%";
    }
    $("div.product-up").css('width', widthProductsPercentage);
    var numUp = numProducts / 2;

    $("div.product-up:nth-child(" + numUp + "n+1)").addClass('last');


    $('img').each(function () {
        if ($(this).attr('align') == 'left') {
            $(this).css('margin-right', '10px');
        }
        else if ($(this).attr('align') == 'right') {
            $(this).css('margin-left', '10px');
        }
    });


    //end document.ready
});


//------------------- for clearing and replacing text in form input fields and textareas -------------------//
function clearText(thefield) {
    if (thefield.defaultValue == thefield.value) { thefield.value = "" }
}
function replaceText(thefield) {
    if (thefield.value == "") { thefield.value = thefield.defaultValue }
}


//---------------------ActiveX--EMBED-------------------//
function printActiveX(source, width, height, id) {
    document.write('<object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" width="' + width + '" height="' + height + '" id="' + id + '" />');
    document.write('<param name="FileName" value="' + source + '" />');
    document.write('<param name="Autostart" value="True" />');
    document.write('<param name="ShowControls" value="True" />');
    document.write('<param name="ShowStatusBar" value="False" />');
    document.write('<param name="ShowDisplay" value="False"/>');
    document.write('<param name="AutoRewind" value="True" />');
    document.write('<embed src="' + source + '" width="' + width + '" height="' + height + '" />');
    document.write('</object>');
}


//------------------- Son-Of-Sucker-Fish IE Hack -------------------//
sfHover = function () {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    // for each list item in the menu...
    for (var i = 0; i < sfEls.length; i++) {
        // Is this IE7?  If so, use onmouseleave to fix the fact that onmouseout won't fire
        is_IE7 = navigator.appVersion.indexOf("MSIE 7.0") != -1;

        sfEls[i].onmouseover = function () {
            this.className += " sfHover";
            // is this a top-level menu item?
            var child_ul = this.getElementsByTagName('ul')[0];
            if (child_ul && is_IE7) {
                // fix for IE7
                child_ul.style.position = 'static';
            }
        }

        sfEls[i].onmouseleave = function () {
            // is this a top-level menu item?
            var child_ul = this.getElementsByTagName('ul')[0];
            if (child_ul && is_IE7) {
                // fix for IE7
                child_ul.style.position = 'absolute';
                child_ul.style.left = '-9000px';
            }
        }

        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfHover\\b"), "");
        }

    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//------------------- Son-Of-Sucker-Fish IE Hack 2 -------------------//
sfHover = function () {
    var sfEls = document.getElementById("global-nav").getElementsByTagName("LI");
    // for each list item in the menu...
    for (var i = 0; i < sfEls.length; i++) {
        // Is this IE7?  If so, use onmouseleave to fix the fact that onmouseout won't fire
        is_IE7 = navigator.appVersion.indexOf("MSIE 7.0") != -1;

        sfEls[i].onmouseover = function () {
            this.className += " sfHover";
            // is this a top-level menu item?
            var child_ul = this.getElementsByTagName('ul')[0];
            if (child_ul && is_IE7) {
                // fix for IE7
                child_ul.style.position = 'static';
            }
        }

        sfEls[i].onmouseleave = function () {
            // is this a top-level menu item?
            var child_ul = this.getElementsByTagName('ul')[0];
            if (child_ul && is_IE7) {
                // fix for IE7
                child_ul.style.position = 'absolute';
                child_ul.style.left = '-9000px';
            }
        }

        sfEls[i].onmouseout = function () {
            this.className = this.className.replace(new RegExp(" sfHover\\b"), "");
        }

    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//---------- Check Forms ----------------------------//
function checkEmail() {
    var v = document.getElementById('email').value;
    rx = new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");

    //check email with reg expression
    if (!rx.test(v)) {
        alert('Please enter a valid email address.\t\n');
        document.getElementById('email').style.color = "#990000";
        document.getElementById('btnSubmit').className = "inactive";
        document.getElementById('btnSubmit').disabled = true;
        document.getElementById('btnSubmit').value = "Complete Form"
        document.getElementById('email').focus();
        return submitted = false;
    }
    else {
        pass--;
    }
}
var pass = 12;
function checkFields(thefield) {
    if (thefield.value != "") {
        pass--;
        if (pass < 1) {

            document.getElementById('btnSubmit').className = "active";
            document.getElementById('btnSubmit').disabled = false;
            document.getElementById('btnSubmit').value = "Submit";
            checkEmail();
        }
        else { }
    }
}
function confirmSampleKit() {
    document.getElementById('samplekitForm').style.display = 'none';
    document.getElementById('confirmation').style.display = 'block';
    document.getElementById('samplekit-img').style.display = 'none';
}

function disableSubmit() {
    document.getElementById('btnSubmit').disabled = true;
    document.getElementById('btnSubmit').className = "inactive";
    document.getElementById('btnSubmit').value = "Complete Form"
}
