﻿/// <reference path="jquery-1.4.2.js" />
/// 
// JavaScript Document
// DHTML Menu

var openmenus = new Array();
var pressed = new Array();
var timer;
var okDOM = (document.getElementById);

// Creates timer that will close DHTML Menu
function setTimer() {
    timer = setTimeout('closeAll(1)', 1750);
}

// Get object info in order to set the style for displaying or hiding the submenu
function getObj(name) {
    if (document.getElementById) { return document.getElementById(name).style; }
}

// Close all dhtml menus that are open
function closeAll(lvl) {
    for (i = openmenus.length - 1; i >= lvl; i--) {
        if (openmenus[i]) {
            var x = getObj(openmenus[i]);
            x.visibility = 'hidden';
        }
        openmenus[i] = null;
        
        // If not the current page, remove the hover class so that the button does not appear to be in the over state
        if (pressed[i]) {
            if (pressed[i].className != 'current') {
                pressed[i].className = '';
            }
            pressed[i] = null;
        }
    }
}
function display(name, lvl, obj) {
    if (!okDOM) return;
    clearTimeout(timer);
    if (openmenus[lvl] && openmenus[lvl] == name) return;
    if (openmenus[lvl]) closeAll(lvl);
    if (name) {
        var x = getObj(name);
        x.visibility = 'visible';
        
        // If there is no name, there is no dhtml menu, so no don't see the class as the css will handle this...Also, we don't want to change the class
        if (obj.className != 'current') {
            obj.className = 'hover';
        }
    }
    openmenus[lvl] = name;
    pressed[lvl] = obj;

}

// Open a new Window
function nw(url) {
    var wname = '_blank';
    open(url, wname);
}

// Show/Hide Elements
function showHide(lnk,div) 
{
    if (document.getElementById(div).style.display == "block")
    {
        document.getElementById(div).style.display = "none";
        lnk.className= "showHideExpand";
    }
    else
    {
        document.getElementById(div).style.display = "block";
        lnk.className = "showHideCollapse";
    }
}

// General Mailto Link Builder
function email(user, domain) {
    document.write('<a href="mailto:' + user + '@' + domain + '">' + user + '@' + domain + '</a>');
}
// Mailto Link builder with Subject
function emailSubject(user, domain, subject) {
    document.write('<a href="mailto:' + user + '@' + domain + '?subject=' + subject + '">' + user + '@' + domain + '</a>');
}


// Onload script for Home Page
if (document.title.match(/National Title Insurance and Settlement (Solutions|Services)/)) {
    window.onload = playShow;

    /* Number of slide show frames */
    var frameCount = 3;
}

// Homepage slide show
// Variables for slideshow
var currentFrame = 0;
var nextFrame = 0;
var elementPrefix = 'ism';
var frameDelay = 6000; // 1000 = 1 second
var playSet = 0;
var func = 'nextSlide()';
var pauseSet = 0;

function swapSlides() {
    // Display next slide
    document.getElementById(elementPrefix + nextFrame).setAttribute("class", "ismshow"); // Works for all standard browsers
    document.getElementById(elementPrefix + nextFrame).setAttribute("className", "ismshow"); // IE workaround

    // Hide previous slide
    document.getElementById(elementPrefix + currentFrame).setAttribute("class", "ismhide"); // Works for all standard browsers
    document.getElementById(elementPrefix + currentFrame).setAttribute("className", "ismhide"); // IE workaround

    // If playing, reset timer and run play function
    if (playset == 1) {
        clearTimeout(playID);
        playShow();
    }
}

function previousSlide() {
    // Set func variable to function in process
    func = 'previousSlide()';

    // If Pause button has been set to true, then don't automatically run slide show	
    if (pauseSet == 1) {
        playset = 0;
    }

    if (nextFrame == 0) {
        currentFrame = 0;
        nextFrame = frameCount - 1;
        swapSlides();
    } else {
        currentFrame = nextFrame;
        nextFrame = nextFrame - 1;
        swapSlides();
    }
}

function nextSlide() {
    // Set func variable to function in process
    func = 'nextSlide()';

    // If Pause button has been set to true, then don't automatically run slide show
    if (pauseSet == 1) {
        playset = 0;
    }

    if (nextFrame == (frameCount - 1)) {
        currentFrame = nextFrame;
        nextFrame = 0;
        swapSlides();
    } else {
        currentFrame = nextFrame;
        nextFrame = nextFrame + 1;
        swapSlides();
    }
}

function playShow() {
    playset = 1;
    playID = setTimeout(func, frameDelay);
}

function showHideObject(targetObj, shouldBeShown) {
    targetObj.style.display = shouldBeShown ? "block" : "none";
}

function toggleShowHideObject(targetObj) {
    if (targetObj.style.display == "block") {
        targetObj.style.display = "none";
    }
    else {
        targetObj.style.display = "block";
    }
}

function doPostBackAsync(eventName, eventArgs) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
        prm._asyncPostBackControlIDs.push(eventName);
    }

    if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
        prm._asyncPostBackControlClientIDs.push(eventName);
    }

    __doPostBack(eventName, eventArgs);
}

///=============================================================
/// The following functions require jQuery support and some assume 
/// that jQuery object references are being passed in instead of 
/// standard javascript objects.
///=============================================================

function setVisible(targetObj, shouldBeVisible) {
    if (shouldBeVisible)
        $(targetObj).show('fast');
    else
        $(targetObj).hide('fast');
}

function enableValidators(targetContainerObj, shouldBeEnabled) {
    $(targetContainerObj).find("span.required").each(function() { ValidatorEnable(this, shouldBeEnabled); });
}

function setCheckbox(targetCheckbox, shouldBeChecked) {
    if (shouldBeChecked)
        $(targetCheckbox).attr("checked", true);
    else
        $(targetCheckbox).removeAttr("checked");
}

function setEnabled(targetObj, shouldBeEnabled) {
    if (shouldBeEnabled)
        $(targetObj).removeAttr("disabled");
    else
        $(targetObj).attr("disabled", true);
}

function setRequiredClass(targetObj, shouldBeRequired) {
    setClass(targetObj, "required", shouldBeRequired);
}

function setClass(targetObj, className, shouldAddClass) {
    if (shouldAddClass)
        $(targetObj).addClass(className);
    else
        $(targetObj).removeClass(className);
}

String.prototype.endsWith = function (str) {
    return (this.match(str + '$') == str)
}

jQuery.expr[":"].asp = function (a, i, m) {
    return (id = jQuery(a).attr('id')) && id.endsWith(m[3]);
};

function setControlSetRequired(inputControl, labelControl, requiredValidator, isRequired) {
    val = $(requiredValidator)[0];
    val.enabled = isRequired;
    if (!isRequired)
        val.isvalid = true;
    ValidatorUpdateDisplay(val);
    setRequiredClass($(labelControl), isRequired);
    setRequiredClass($(inputControl), isRequired);
    $(labelControl).text($(labelControl).text().replace("\* ", ""));
    $(labelControl).text(isRequired ? "* " + $(labelControl).text() : $(labelControl).text());
}

function validatePhone(source, clientside_arguments) {
    clientside_arguments.IsValid = checkPhoneNumber(clientside_arguments.Value);
}

function checkPhoneNumber(phoneNo) {
    var phoneRE = /^\(\d{3}\) \d{3}\-\d{4}$/;
    return phoneNo.match(phoneRE);
}

function setElementLeft(sourceElement, targetElement) {
    var scrollbar_present = document.body.clientHeight + 10 > (window.innerHeight ? window.innerHeight : $(window).height());
    if ($(sourceElement).length > 0) {
        var sourceElementLeft = $(sourceElement).position().left;
        var adjustment = 20; // Fixed adjustment value found by trial-and-error.
        var subMenuLeft = sourceElementLeft + adjustment;
        $(targetElement).css("left", subMenuLeft);
    }
}

function scrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it 
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

function ValidateAtLeastOneProductSelected(sender, args, className) {
    args.IsValid = false;

    $("." + className).each(function () {
        if ($(this).find("input").is(":checked")) {
            args.IsValid = true;
            return;
        }
    });
}

function selectAll(listItemControlID, isChecked) {
    $(":asp('" + listItemControlID + "')").find("input:checkbox").each(function () { setCheckbox(this, isChecked); });
}

function addSubmitOnEnterKey(control, buttonUniqueId) {
        $(control).keydown(function () {
            if (event.keyCode == 13 && ($(this)[0].tagName != "TEXTAREA" || event.ctrlKey)) __doPostBack(buttonUniqueId, '');
        });
}
