$(document).ready(function() {

    /* Put the correct youtube link on the main window when clicked */
    $('.vid-links').each(function(i, n) {
        $(n).find('a').click(function(event) {
            event.preventDefault();
            $('.vid-links').removeClass('active-vid');
            var href = $(this).attr('href');
            html = "<object width=\"550\" height=\"310\"><param name=\"movie\" value=\"" + href + "?fs=1&amp;hl=en_US\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + href + "?fs=1&amp;hl=en_US\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"transparent\" width=\"550\" height=\"310\"></embed></object>";
            //var vidLink = "http://www.youtube.com/embed/"+href;
            $(this).parent().addClass('active-vid');
            $('.vid-player').empty();
            $('.vid-player').append(html);
            //setTimeout("$('#vid-frame').attr('src','');", 800);
            //setTimeout("$('#vid-frame').attr('src',\""+vidLink+"\");", 800);
            //$('#vid-frame').attr('src','');
            //$('#vid-frame').attr('src',vidLink);
        });
    });
/*
    $(".popUp").overlay({
        target: '#overlay-2',
        expose: '#000'
    });

    $(".popUp-2").overlay({
        target: '#overlay',
        expose: '#000'
    });

    $(".popUp-teammate").overlay({
        target: '#overlayteammate',
        expose: '#000'
    });
*/
    $('a[href="/contact-us.aspx"]').overlay({
        target: '#ContactUsOverlay',
        expose: '#000'
    });
    $('a[href="/contact-us.aspx"]').attr("id", "ContactUsOverlayLink");
    $('a[href="/contact-us.aspx"]').attr("href", "#");
    $("a#ContactUsClose").click(function(e) {
        e.preventDefault();
    });

    $('#overlay').find('.toolTip').hover(function() {
        $(this).next('.toolTipText').show();
    }, function() {
        $(this).next('.toolTipText').hide();
    });

    $('#overlay-2').find('.toolTip').hover(function() {
        $(this).next('.toolTipText').show();
    }, function() {
        $(this).next('.toolTipText').hide();
    });

    $('.forgot-password').click(function(event) {
        event.preventDefault();
        $('#overlay #new-password').fadeOut('slow');
        $('#lost-password').fadeIn('slow');
    });

    $('.new-password').click(function(event) {
        event.preventDefault();
        $('#overlay #lost-password').fadeOut('slow');
        $('#new-password').fadeIn('slow');
    });

    $('.wrapper').find('.toolTip').hover(function() {
        var toolTipObj = $(this);
        pos = toolTipObj.position();
        var posX = pos.left;
        var posY = pos.top;
        $(this).next('.toolTipText').css({ 'top': (posY - 45) + 'px', 'left': (posX + 30) + 'px' }).show();
    }, function() {
        $(this).next('.toolTipText').hide();
    });

    $("div.form > h2").addClass("form-title");

    $("ul.sitemap-sections:first").addClass("first");
});

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function closeOverlay(overlaydiv) {
    $(overlaydiv).overlay().close();
}

function ValidateFile(source, args) {
    try {
        var fileAndPath =
           document.getElementById(source.controltovalidate).value;
        var lastPathDelimiter = fileAndPath.lastIndexOf("\\");
        var fileNameOnly = fileAndPath.substring(lastPathDelimiter + 1);
        var file_extDelimiter = fileNameOnly.lastIndexOf(".");
        var file_ext = fileNameOnly.substring(file_extDelimiter + 1).toLowerCase();
        if (file_ext != "jpg") {
            args.IsValid = false;
            if (file_ext != "gif")
                args.IsValid = false;
            if (file_ext != "png") {
                args.IsValid = false;
                return;
            }
        }
    } catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        txt += document.getElementById(source.controltovalidate).value;
        alert(txt);
    }

    args.IsValid = true;
}

function ValidateSubmission(source, args) {
    try {
        var fileAndPath =
           document.getElementById(source.controltovalidate).value;
        var lastPathDelimiter = fileAndPath.lastIndexOf("\\");
        var fileNameOnly = fileAndPath.substring(lastPathDelimiter + 1);
        var file_extDelimiter = fileNameOnly.lastIndexOf(".");
        var file_ext = fileNameOnly.substring(file_extDelimiter + 1).toLowerCase();
        if (file_ext == "ppt" || file_ext == "pptx" || file_ext == "rar" || file_ext == "zip" || file_ext == "pdf" || file_ext == "xlsx" || file_ext == "xls" || file_ext == "docx" || file_ext == "doc" || file_ext == "jpg" || file_ext == "jpeg" || file_ext == "gif" || file_ext == "png" || file_ext == "pps" || file_ext == "tar" || file_ext == "gz")
            args.IsValid = true;
	    else
		    args.IsValid = false;
                
            return;
    } catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        txt += document.getElementById(source.controltovalidate).value;
        alert(txt);
    }

    args.IsValid = true;
}

function ValidateYouTubeUrl(source, args) {
    if (document.getElementById(source.controltovalidate).value.toLowerCase() == "youtube url") {
        args.IsValid = true;
    }
    else {
        var url = document.getElementById(source.controltovalidate).value;

        var matches = url.match(/^(https?:\/\/)?([^\/]*\.)?youtube\.com\/watch\?([^]*&)?v=\w+(&[^]*)?/i);
        if (matches) {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }

}

function PasswordValidation(source, args) {
    var str = document.getElementById(source.controltovalidate).value;
    args.IsValid = Alphanumeric(str);
}

function Alphanumeric(sText){
    var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890* ";
    var IsAlphabetic=true;
    var Char;


    for (i = 0; i < sText.length; i++)
    {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
    IsAlphabetic = false;
    }
    }
    return IsAlphabetic;
}
