﻿function handleSearchPanelEnter(evt) {
    var e = evt || window.event;
    var pK = evt.which || e.keyCode;
    if (pK == 13) {
        doSearch();
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        return false;
    }
}

function doSearch() {
    var IsGovOnly = $('#IsGovernmentOnly').attr('checked');
    var SearchText = $("[id$='txtSearch']");
    if (SearchText.val() != '' && SearchText.val() != null) {
        location.href = '/search/search.aspx?q=' + SearchText.val().replace(/"/g, '') + '&addconfig=true'; // Remove quotes from search string
    } else {
        alert('Please specify your search text.');
    }
}

function switchMode() {
    var cookie = readCookie('gwsaContrastSwitch');
    var isHighContrast = (cookie != null && cookie == '1');
    createCookie('gwsaContrastSwitch', isHighContrast ? '0' : '1', 300);
    location.href = location.href;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function confirmRevert() {
    return confirm('Are you sure you would like to Revert this content? (This will delete your draft version completely)');
}

String.prototype.replaceLast = function (what, replacement) {
    var n = this.lastIndexOf(what);
    if (n == -1) return this;
    var s = this.substring(0, n) + replacement + this.substring(n + what.length);
    return s;
}
function mouseOver() {
    this.src = this.src.replaceLast(".", "_on.");
}
function mouseOut() {
    this.src = this.src.replaceLast("_on.", ".");
}
function top5MouseOver() {
    var jdiv = jQuery(this);
    var img1 = jdiv.find('img').get(0);
    var img2 = jdiv.find('img').get(1);
    img1.src = img1.src.replaceLast(".", "_on.");
    img2.src = img2.src.replaceLast(".", "_on.");
}
function top5MouseOut() {
    var jdiv = jQuery(this);
    var img1 = jdiv.find('img').get(0);
    var img2 = jdiv.find('img').get(1);
    img1.src = img1.src.replaceLast("_on.", ".");
    img2.src = img2.src.replaceLast("_on.", ".");
}
$(document).ready(function () {
    $('.rollover').hover(mouseOver, mouseOut);
    $('.top5-rollover').hover(top5MouseOver, top5MouseOut);
    $('.top5-rollover').hover(top5MouseOver, top5MouseOut);
});

// Event handler for RadEditor for setting title tag to same value as alt tag...
function RadEditor_ClientPasteHtml(sender, args) {
    var commandName = args.get_commandName();
    var value = args.get_value();
    if (commandName == "ImageManager") {
        //See if an img has an alt tag set
        var div = document.createElement("DIV");
        //Do not use div.innerHTML as in IE this would cause the image's src or the link's href to be converted to absolute path.
        //This is a severe IE quirk.
        Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, value);
        var img = div.firstChild;
        var altText = img.alt ? img.alt : '';
        img.setAttribute("title", altText);
        //Set new content to be pasted into the editor
        args.set_value(div.innerHTML);
    }
}

function GraphicSelector_ClientSelectedIndexChanged(sender, eventArgs) {
        var item = eventArgs.get_item();
        updateGraphicTypeComboImage(item);
}

function GraphicTypeCombo_ClientLoad(sender, eventArgs) {
        var comboItem = sender.get_selectedItem();
        updateGraphicTypeComboImage(comboItem);
}

function updateGraphicTypeComboImage(item) {
        var url = item.get_attributes().getAttribute('_url');
        var combo = item.get_comboBox();
        var container = document.getElementById(combo.get_id()).parentNode;
        var img = $(container).find('img[id$="TheImage"]').get(0);
        img.src = url;
        img.style.display = '';
        img.style.height = item.get_index() == 0 ? '1px' : '50px';
}
