﻿function RedirectLangue(form)
{
    window.location = this.document.getElementById('ctl00_ddlLanguage').value;
}

function IsSignedIn() {
    ReadCookie('.CommunityServer');
}

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();
}

function ReadCookie(cookieName) {
//    alert(document.domain);
//    alert(document.cookie);
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
//    alert(ind);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}


function EraseCookie(cookieName) {
    SetCookie(cookieName, "", -1);
}

// Simple helper to return the "exMaxLen" attribute for
// the specified field.  Using "getAttribute" won't work
// with Firefox.
function GetMaxLength(targetField) {
    return targetField.exMaxLen;
}

//
// Limit the text input in the specified field.
//
function LimitInput(targetField, sourceEvent) {
    var isPermittedKeystroke;
    var enteredKeystroke;
    var maximumFieldLength;
    var currentFieldLength;
    var inputAllowed = true;
    var selectionLength = parseInt(GetSelectionLength(targetField));

    if (GetMaxLength(targetField) != null) {
        // Get the current and maximum field length
        currentFieldLength = parseInt(targetField.value.length);
        maximumFieldLength = parseInt(GetMaxLength(targetField));

        // Allow non-printing, arrow and delete keys
        enteredKeystroke = window.event ? sourceEvent.keyCode : sourceEvent.which;
        isPermittedKeystroke = ((enteredKeystroke < 32)								// Non printing
					          || (enteredKeystroke >= 33 && enteredKeystroke <= 40)	// Page Up, Down, Home, End, Arrow
					          || (enteredKeystroke == 46))							// Delete

        // Decide whether the keystroke is allowed to proceed
        if (!isPermittedKeystroke) {
            if ((currentFieldLength - selectionLength) >= maximumFieldLength) {
                inputAllowed = false;
            }
        }

        // Force a trim of the textarea contents if necessary
        if (currentFieldLength > maximumFieldLength) {
            targetField.value = targetField.value.substring(0, maximumFieldLength)
        }
    }

    sourceEvent.returnValue = inputAllowed;
    return (inputAllowed);
}

//
// Limit the text input in the specified field.
//
function LimitPaste(targetField, sourceEvent) {
    var clipboardText;
    var resultantLength;
    var maximumFieldLength;
    var currentFieldLength;
    var pasteAllowed = true;
    var selectionLength = GetSelectionLength(targetField);

    if (GetMaxLength(targetField) != null) {
        // Get the current and maximum field length
        currentFieldLength = parseInt(targetField.value.length);
        maximumFieldLength = parseInt(GetMaxLength(targetField));

        clipboardText = window.clipboardData.getData("Text");
        resultantLength = currentFieldLength + clipboardText.length - selectionLength;
        if (resultantLength > maximumFieldLength) {
            pasteAllowed = false;
        }
    }

    sourceEvent.returnValue = pasteAllowed;
    return (pasteAllowed);
}

//
// Returns the number of selected characters in 
// the specified element
//
function GetSelectionLength(targetField) {
    if (targetField.selectionStart == undefined) {
        return document.selection.createRange().text.length;
    }
    else {
        return (targetField.selectionEnd - targetField.selectionStart);
    }
}


function InvisibleDiv(targetDiv) {
    var divToHide = document.getElementById(targetDiv);
    if (divToHide != null) {
        divToHide.style.display = 'none';
    }
}

function InvisibleVisibleDiv(targetDiv,targetImage,ImageNameOn,ImageNameOff) {
    var divToHideOrShow = document.getElementById(targetDiv);
    var imgToChange = document.getElementById(targetImage);
    if (divToHideOrShow != null && imgToChange != null) {
        if (divToHideOrShow.style.display == 'block') {
            divToHideOrShow.style.display = 'none';
            imgToChange.src = ImageNameOn;
        }
        else {
            divToHideOrShow.style.display = 'block';
            imgToChange.src = ImageNameOff;
        }
    }
}
