

function iCMSURL( text, object )
{
    var c;
    endtext = '';

    for ( n = 0 ; n < text.length ; n++ )
    {
        c = text.substr( n, 1 );

        if ( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '-' || c == '/' )
        {
            endtext += c;
        }
    }

    object.value = endtext.toLowerCase();
}

function ShowForm( id )
{
    document.getElementById( 'popups' ).style.display = 'block';
    document.body.style.overflow = 'hidden';
    document.getElementById( id ).style.display = 'block';
}

function HideForm( id )
{
    document.getElementById( id ).style.display = 'none';
    document.body.style.overflow = 'auto';
    document.getElementById( 'popups' ).style.display = 'none';
}

function SwapForm( id1, id2 )
{
    document.getElementById( id1 ).style.display = 'none';
    document.getElementById( id2 ).style.display = 'block';
}

/***** AJAX *****/

var xmlhttp = new XMLHttpRequest;

function GetXML( url )
{
    var popup = document.getElementById( 'popup' )
    if ( popup )
        popup.style.cursor = 'wait';

    document.getElementById( 'popups' ).style.display = 'block';

    xmlhttp.open( 'get', url, true );
    xmlhttp.onreadystatechange = XMLLoaded;
    xmlhttp.send();
}

function XMLLoaded()
{
    if ( xmlhttp.readyState == 4 )
    {
        var popups = document.getElementById( 'popups' );
        popups.innerHTML = xmlhttp.responseText;

        var popup = document.getElementById( 'popup' );
        popup.style.display = 'block';
        popup.style.cursor = "";
    }
}

// Get XML - put result in DIV
function GetXMLToDiv( url, divid )
{
    xmlhttp.id = divid;
    xmlhttp.open( 'get', url, true );
    xmlhttp.onreadystatechange = XMLToDivLoaded;
    xmlhttp.send();
}

function XMLToDivLoaded()
{
    if ( xmlhttp.readyState == 4 )
    {
        var div = document.getElementById( xmlhttp.id );
        div.innerHTML = xmlhttp.responseText;
    }
}

// Used to close form and update page
function PostXML( url, formid, id, debug )
{
    document.getElementById( 'popup' ).style.display = 'none';

    NakedPostXML( url, formid, id, PostXMLLoaded, debug );
}

// Used to dynamically update forms
function PostXMLForm( url, formid, id, debug )
{
    var popup = document.getElementById( 'popup' )
    if ( popup )
        popup.style.cursor = 'wait';

    NakedPostXML( url, formid, id, PostXMLFormLoaded, debug );
}

function NakedPostXML( url, formid, id, callback, debug )
{
    var params = FormIntoParms( formid );

    var element = document.getElementById( id );

    if ( ! debug )
    {
        xmlhttp.id = id;
        xmlhttp.open( 'post', url, true );
        xmlhttp.onreadystatechange = callback;

        xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
        xmlhttp.setRequestHeader( "Content-length", params.length );
            
        xmlhttp.send( params );
    }
    else
        alert( params );
}

function PostXMLLoaded()
{
    if ( xmlhttp.readyState == 4 )
    {
        document.getElementById( xmlhttp.id ).innerHTML = xmlhttp.responseText;
        document.getElementById( 'popups' ).style.display = 'none';
    }
}

function PostXMLFormLoaded()
{
    if ( xmlhttp.readyState == 4 )
    {
        document.getElementById( xmlhttp.id ).innerHTML = xmlhttp.responseText;
        document.getElementById( 'popup' ).style.cursor = "";
    }
}

var tempid = 0;

function SetUpRefresh( folderid )
{
    var dump = document.getElementById('dump');
    tempid = folderid;
    dump.onload = RefreshFiles;

    document.getElementById( 'uploadfileform' ).submit()
}

function RefreshFiles()
{
    var dump = document.getElementById( 'dump' );
    dump.onload = null;

    PostXMLForm( '?ajax=files&id=' + tempid, '', 'files' );
}

function FormIntoParms( formid )
{
    var form = document.getElementById( formid );

    var parms = "";

    if ( form )
        for ( n = 0 ; n < form.elements.length ; n++ )
        {
            e = form.elements[n];

            if ( e.name )
            {
                switch ( e.tagName )
                {
                case 'TEXTAREA':
                case 'SELECT':
                    parms += ( parms ? "&" : "" ) + e.name + "=" + encodeURIComponent( e.value );
                    break;
                case 'INPUT':
                    switch ( e.type )
                    {
                    case 'hidden':
                    case 'text':
                    case 'password':
                        parms += ( parms ? "&" : "" ) + e.name + "=" + encodeURIComponent( e.value );
                        break;
                    case 'checkbox':
                        if ( e.status == true )
                            parms += ( parms ? "&" : "" ) + e.name + "=" + encodeURIComponent( e.value );
                        break;
                    case 'radio':
                        if ( e.checked == true )
                            parms += ( parms ? "&" : "" ) + e.name + "=" + encodeURIComponent( e.value );
                        break;
                    case 'button':
                        break;
                    case 'file':
                        parms += ( parms ? "&" : "" ) + e.name + "=" + encodeURIComponent( e.value );
                        break;
                    default:
                        alert( "Unhandled type: " + e.type );
                        return "";
                    }
                    break;
                }
            }
        }

    return parms;
}

function Search()
{
    var obj = document.getElementById( 'search' )
    if ( obj.value.length > 2 )
    {
        document.getElementById( 'searchresults' ).innerHTML = "<p>Searching...</p>";
        GetXMLToDiv( '?search=' + escape( obj.value ), 'searchresults' );
    }
}


