﻿function showPromptText(field, promptValue) {
    if (field.value == "") {
        field.style.color = "gray";
        field.value = promptValue;
    }
}

function clearPromptText(field, promptValue, oldStyleColor) {
    if (field.value == promptValue) {
        if (oldStyleColor)
            field.style.color = oldStyleColor;
        else
            field.style.color = "";
        field.value = "";
    }
}

function inlineFieldLabel(label, inputid) {
    var fieldLabel = label;     // string to put in your text input
    var textInput = document.getElementById(inputid)  // your text input field

    /* remove the placeholder string when field gets focus */
    textInput.onfocus = function() { return function() { clearPromptText(this, fieldLabel) } } ();
    /* add the field label string when field looses focus */
    textInput.onblur = function() { return function() { showPromptText(this, fieldLabel, textInput.style.color) } } ();
    showPromptText(textInput, fieldLabel);
}

function passit(ip) {
    try {
        var np = ip.cloneNode(true);
        np.type = 'password';
        if (np.value != ip.value)
            np.value = ip.value;
        ip.parentNode.replaceChild(np, ip);
        np.setAttribute('onblur', "showpassit(this)");
        np.setAttribute('onfocus', "");
        np.focus();
        np.value = "password";
    }
    catch (e) {
        window.status = e.message;
    }
}

function showpassit(ip) {
    try {
        var np = ip.cloneNode(true);
        np.type = 'text';
        if (np.value != ip.value)
            np.value = ip.value;
        ip.parentNode.replaceChild(np, ip);
        np.setAttribute('onfocus', "passit(this)");
        np.setAttribute('onblur', "");
        np.value = "clear password";
    }
    catch (e) {
        window.status = e.message;
    }
}

///  oldElm, // a reference to the input element
///  iType, // value of the type property: 'text' or 'password'
///  iValue, // the default value, set to 'password' in the demo
///  blankValue,  true if the value should be empty, false otherwise
///  noFocus // set to true if the element should not be given focus
function changeInputTypeNew(oldElm, iType,  iValue,  blankValue, noFocus, oldStyleColor) {
    var newElm;
    var isMSIE = /*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
    newElm = oldElm;
    if (!isMSIE) {
        newElm.type = iType;
    }
    else {
        // for IE later use;
    }
    newElm.onfocus = function() {
        return function() {
            if (this.hasFocus) return;
            var newElm = changeInputTypeNew(this, 'password', iValue, (this.value.toLowerCase() == iValue.toLowerCase()) ? true : false, false, oldStyleColor);
            if (newElm) newElm.hasFocus = true;
        }
    } ();
    newElm.onblur = function() {
        return function() {
            if (this.hasFocus)
                if (this.value == '' || (this.value.toLowerCase() == iValue.toLowerCase())) {
                    changeInputTypeNew(this, 'text', iValue, false, true, oldStyleColor);
            }
        }
    } ();
    // hasFocus is to prevent a loop where onfocus is triggered over and over again
    newElm.hasFocus = false;
    // some browsers need the value set before the element is added to the page
    // while others need it set after
    if (!blankValue) {
        newElm.value = iValue;
        newElm.style.color = "gray";
    }
    else {
        newElm.value = "";
        newElm.style.color = oldStyleColor;
    }
    if (!noFocus || typeof (noFocus) == 'undefined') {
        window.tempElm = newElm;
        setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
    }
    return newElm;
}

function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
    if (!oldElm || !oldElm.parentNode || (iType.length < 4) || !document.getElementById || !document.createElement) return;
    var isMSIE = /*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
    if (!isMSIE) {
        var newElm = oldElm;   //document.createElement('input');
        newElm.type = iType;
    } 
    else {
        var newElm = document.createTextNode("password"); // createElement('span');
        var newString;
        newString = oldElm.outerHTML.toLowerCase();
        if (iType == "password") {
            var strPass = "type=\"password\"";
            if (newString.indexOf("type") == -1) {
                //var firstSpace = newString.indexOf(" ");
                newString = newString.replace(" ", " " + strPass + " ");
            }
            else {
                newString = newString.replace("type='text'", strPass);
                newString = newString.replace("type=\"text\"", strPass);
                newString = newString.replace("type=text", strPass);
            }
        }
        else
        {
            newString = newString.replace("type='password'", "type=\"text\"");
            newString = newString.replace("type=\"password\"", "type=\"text\"");
            newString = newString.replace("type=password", "type=\"text\"");
        }
        newElm.innerHMTL = newString;  //'<input type="' + iType + '" name="' + oldElm.name + '">';
        //newElm = newElm.firstChild;
        newElm.value = "";
    }

    newElm.onfocus = function() 
        {
            return function() 
            {
                if (this.hasFocus) return;
                var newElm = changeInputType(this, 'password', iValue,
                (this.value.toLowerCase() == iValue.toLowerCase()) ? true : false);
                if (newElm) newElm.hasFocus = true;
            } 
        } ();
    newElm.onblur = function() 
        {
            return function() 
            {
                if (this.hasFocus)
                    if (this.value == '' || (this.value.toLowerCase() == iValue.toLowerCase())) {
                    changeInputType(this, 'text', iValue, false, true);
                }
            } 
        } ();
    // hasFocus is to prevent a loop where onfocus is triggered over and over again
    newElm.hasFocus = false;
    // some browsers need the value set before the element is added to the page
    // while others need it set after
    if (!blankValue) newElm.value = iValue;
    oldElm.parentNode.replaceChild(newElm, oldElm);
    if (!isMSIE && !blankValue) newElm.value = iValue;
    if (!noFocus || typeof (noFocus) == 'undefined') {
        window.tempElm = newElm;
        setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
    }
    return newElm;
}

function ShowHidePassword(passbox, prompt) {
    var passInput = document.getElementById(passbox)  // your text input field
    //    passInput.setAttribute('onfocus', "passit(this)");
    //    passInput.setAttribute('onblur', "showpassit(this)");
    //passit(passInput);
    changeInputTypeNew(passInput, 'text', prompt, false, true, passInput.style.color);
}

function addPageLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function GoDashboard() {
    window.location = "~/";
}

function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}
function CloseOnReload() {
    GetRadWindow().close();
}
function RefreshParentPage() {
    GetRadWindow().BrowserWindow.location.href = GetRadWindow().BrowserWindow.location;
}

function ParentTo(newURL) {
    GetRadWindow().BrowserWindow.location.href = newURL;
}

function SizeToFit()
{
window.setTimeout(
    function()
    {
        var oWnd = GetRadWindow();
        oWnd.SetWidth(document.body.scrollWidth + 4);
        oWnd.SetHeight(document.body.scrollHeight + 70);
        
    }, 400);
}

function ShowWindow(windowID, targetUrl) {
    var manager = GetRadWindowManager();
    var window1 = manager.getWindowByName(windowID);
    window1.setUrl(targetUrl);
    window1.show();
}

function OpenLegalWindow(windowID, targetUrl) {
        var oWnd = $find(windowID);
        oWnd.setUrl(targetUrl);
        oWnd.show();
}
