function in_array (ary, val) 
{
    for (x in ary) 
    {
        if ( ary[x] == val ) return true;
    }
    return false;
}

// Confirmation as a condition for moving to the link
function LinkConfirm(link, message)
{
    if (confirm(message))
    {
        window.location.href = link;
    }
}

// check email syntax
function isEmailSyntaxOk(email)
{
    email = email.toLowerCase();
    email = email.replace(/^\s+|\s+$/g,"");
    
    match = email.match(/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/);
    return (match != null);
}

function CheckEmail(textBox, msgDivName)
{
    email = textBox.value; 
    email = email.replace(/\s+/g, "");
    textBox.value = email;
    
    // case blank string - return false
    if (email.length == 0)
    {
        return false;
    }

    // case email syntax ok
    if (isEmailSyntaxOk(email))
    {
        return email;
    }

    // case email syntax bad
    else
    {
        DispHidBlockMsg(jsld["invalid email"], msgDivName);
        return false;
    }
}

// showHide
function showHide(id) 
{
    if (document.getElementById(id))
    {
        var isDisplayed = (document.getElementById(id).style.display == 'block');
        document.getElementById(id).style.display = (isDisplayed) ? 'none' : 'block';
    }
}
function Show(id)
{
    if (document.getElementById(id))
    {
        if (document.getElementById(id).style.visibility)
        {
            document.getElementById(id).style.visibility = 'visible';
        }
        else if (document.getElementById(id).style.display)
        {
            document.getElementById(id).style.display = 'block';
        }
    }
    
    else
    {
        alert('The ID '+id+' does not exist');
    }
}
function ShowInline(id)
{
    if (!document.getElementById(id))
    {
        return;
    }
    
    if (document.getElementById(id))
    {
        document.getElementById(id).style.display = 'inline';
    }
    
    else
    {
        alert('The ID '+id+' does not exist');
    }
}

function Hide(id)
{
    if (!document.getElementById(id))
    {
        return;
    }
    
    if (document.getElementById(id).style.visibility)
    {
        document.getElementById(id).style.visibility = 'hidden';
    }
    else if (document.getElementById(id).style.display)
    {
        document.getElementById(id).style.display = 'none';
    }
}
function IsHide(id)
{
    if (!document.getElementById(id))
    {
        alert('ID does not exist, at IsHide('+id+')');
        return;
    }
    
    if (document.getElementById(id).style.visibility)
    {
        return document.getElementById(id).style.visibility == 'hidden';
    }
    else if (document.getElementById(id).style.display)
    {
        return document.getElementById(id).style.display == 'none';
    }
    else
    {
        return false;
    }
}


// Clear field
// Clears the field contents on mouse click, unless its current value is different
// from the original default value.
function ClearField(fldId, orgVal)
{
    if (fldId.value == orgVal)
    {
        fldId.value = '';
    }
}

// Does the opposite of clear-field().
function ResetField(fldId, orgVal)
{
    var val = fldId.value;
    val = val.replace( /^\s+/g, "" );
    
    if (val == '')
    {
        fldId.value = orgVal;
    }
}


// Display a message inside the page message box.
function DisplayMessage(msg)
{
    if (document.getElementById('BetaMessage'))
    {
        document.getElementById('BetaMessage').innerHTML = msg;
        document.getElementById('BetaMessage').style.backgroundColor = '#fc0';
        document.getElementById('BetaMessage').style.color = 'black';
        Show('BetaMessage');
        
        //t = setTimeout("Hide('BetaMessage')",3000);
    }
    
    else
    {
        alert('The ID BetaMessage does not exist');
    }
}

function HideMessage()
{
    Hide('BetaMessage');
}

// Display a message inside a hidden block.
function DispHidBlockMsg(msg, blockId)
{                                     
    if (document.getElementById(blockId))
    {
        document.getElementById(blockId).innerHTML = msg;
        Show(blockId);
        
        var durMin = 5;   // [seconds]
        var durMax = 12;  // [seconds]
        var lenMax = 200; // [characters]
        var duration = Math.max(1000*durMin, Math.round(1000*(durMin-1) + 1000*durMax * Math.min(1, (msg.length / lenMax))));
        t = setTimeout("Hide('"+blockId+"')", duration);
        return true;
    }
    
    else
    {
        return false;
    }
}
function DispHidBlockMsgPermanently(msg, id)
{                                     
    if (document.getElementById(id))
    {
        document.getElementById(id).innerHTML = msg;
        Show(id);
    }
    
    else
    {
        alert('Block ID ('+id+') does not exist at DispHidBlockMsgPermanently()');
    }
}

// Returns the value of the radio button that is checked.
// Returns an empty string if none are checked, or there are no radio buttons.
function GetCheckedRadioValue(radioObj)
{
    if(!radioObj)
    {
        return "";
    }
    
    var radioLength = radioObj.length;
    
    if(radioLength == undefined)
    {
        return (radioObj.checked) ? radioObj.value : "";
    }
    
    for(var i = 0; i < radioLength; i++)
    {
        if(radioObj[i].checked)
        {
            return radioObj[i].value;
        }
    }
    
    return "";
}


// Array.shuffle( deep ) - Randomly interchange elements
function shuffle(ary)
{
    var i = ary.length, j, t;
    
    while( i ) 
    {
        j = Math.floor( ( i-- ) * Math.random() );
        
        t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    return ary;
};

// Cookies.
// http://www.quirksmode.org/js/cookies.html
function _createCookie(name,value,days,isSecure) 
{
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else 
    {
        var expires = "";
    }
    
    (isSecure) ?
        document.cookie = name+"="+value+expires+"; secure" :
        document.cookie = name+"="+value+expires+"; path=/";
}
function createCookie(name,value,days) 
{
    var isSecure = false;
    _createCookie(name,value,days, isSecure) 
}
function createSecuredCookie(name,value,days) 
{
    var isSecure = true;
    _createCookie(name,value,days, isSecure) 
}

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 eraseSecuredCookie(name) 
{
    createSecuredCookie(name,"",-1);
}

function ParseExt(fileName) 
{
  return fileName.substring(fileName.lastIndexOf('.') + 1,fileName.length);
}

function IsAllowedUplFileType(fileName)
{
    fileExt = ParseExt(fileName);
    fileExt = fileExt.toLowerCase();
    return in_array(JS_AllowedFileExts, fileExt);
}

function AppendUrl(url)                                                             
{            
    if 
    (
        (url.match('&sid=') == null) && 
        ( (url.match(JS_MODULE_WLIB_CATEGORY) == null) || (url.match(JS_MODULE_WLIB_ADMIN_CATEGORY) != null) )
    )
    {
        url += '&sid='+siteId;
    }
    
    return url;
}

function AppendMsg(url, msg)
{
    // Case error - blank input.
    if ((url == '') || (msg == ''))
    {
        return false;
    }
    
    // Case error - message contains characters with URL meaning.
    if (msg.match(/&/) || msg.match(/\?/) || msg.match(/%/))
    {
        return false;
    }
    
    // Case error - url already contains a message.
    if (url.match(/msg=/))
    {
        return false;
    }
    
    // Append message to URL.
    $url += (url.match(/\?/)) ? '&' : '?';
    $url += JS_MSG+'='+msg;
    
    // Return the URL with the message.
    return url;
}

// Strips HTML tags from string.
String.prototype.strip_tags = function()
{
    stripped = this;
    stripped = stripped.replace(/\<\/?.+?\>/gi, "");
    stripped = stripped.replace(/\[\/url\]/gi, "");
    return stripped;
}

// Restrict textarea length (as in <input> maxlength tag)
// Taken from: http://www.quirksmode.org/dom/maxlength.html
function setMaxLength() 
{
    var x = document.getElementsByTagName('textarea');
    
    for (var i=0;i<x.length;i++) 
    {
        if (x[i].getAttribute('maxlength')) 
        {
            x[i].onkeyup = x[i].onchange = checkMaxLength;
            x[i].onkeyup();
        }
    }
}
function checkMaxLength() 
{
    var maxLength = this.getAttribute('maxlength');
    var currentLength = this.value.length;
    
    if (currentLength > maxLength)
    {
        this.value = this.value.slice(0, maxLength);
    }
}

// Sets the site map tree.
function SetSitemapTree()
{
    sitemapTreeObj = new JSDragDropTree();
    sitemapTreeObj.setImageFolder(JS_INCLUDE_WLIB+"drag-drop-folder-tree/images/");
    sitemapTreeObj.setTreeId("UlSiteMap");
    sitemapTreeObj.setMaximumDepth(3);
    sitemapTreeObj.setMessageMaximumDepthReached(jsld["maximum depth reached"]);
    sitemapTreeObj.initTree();
    sitemapTreeObj.expandAll();
}

// Returns browser ID: 'ff' or 'ie'.
function get_browser_id()
{
    var browserName=navigator.appName; 

    if (browserName=="Netscape")
    { 
        return 'ff';
    }
    else 
    { 
        if (browserName=="Microsoft Internet Explorer")
        {
            return 'ie';
        }
        else
        {
            return 'ff'; // other
        }
    }
}

function is_browser_ie()
{
    return (get_browser_id() == 'ie');
}

function is_browser_ff()
{
    return (get_browser_id() == 'ff');
}

function ShowBackend(id)
{
    idFront = id + '-front';
    idBack = id + '-back';
    Hide(idFront);
    Show(idBack);
    document.getElementById(idBack).focus();
}
function ShowFrontend(id)
{
    idFront = id + '-front';
    idBack = id + '-back';
    Hide(idBack);
    Show(idFront);
}
function ShowFrontendInline(id)
{
    idFront = id + '-front';
    idBack = id + '-back';
    Hide(idBack);
    ShowInline(idFront);
}

function GetBrowserName()
{
    var browserName = "";

    var ua = navigator.userAgent.toLowerCase();
    
    if ( ua.indexOf( "opera" ) != -1 ) 
    {
        browserName = "opera";
    } 
    else if ( ua.indexOf( "msie" ) != -1 ) 
    {
        browserName = "msie";
    } 
    else if ( ua.indexOf( "safari" ) != -1 ) 
    {
        browserName = "safari";
    } 
    else if ( ua.indexOf( "mozilla" ) != -1 ) 
    {
        if ( ua.indexOf( "firefox" ) != -1 ) 
        {
            browserName = "firefox";
        } 
        else 
        {
            browserName = "mozilla";
        }
    }

    return browserName;
}

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false : true;
}

