// webcsrutils.js
//
// generic utility functions for form validation, display, etc.
// that can be called from other modules
//


// error string defines
var EMAILFORMATERROR    = "Please enter your valid email address in the form of user@domain.com";
var EMAILMATCHERROR     = "Please enter email addresses that match exactly";
var CSRIDERROR          = "Please enter your Ticket Number in the form of \'CSRXXXXXXXX\'";

// other defines
var CSRIDPREFIX         = "CSR";
var CSRIDLENGTH         = "11";


// simple validation of one email address
function JSWEBCSRValidateEmail( strEmail )
{
    // email must be > 0 and < 128 chars, and contain a '@' and a '.'
    if (    strEmail.length < 1 ||
            strEmail.length > 128 ||
            strEmail.indexOf( "@" ) == -1 ||
            strEmail.indexOf( "." ) == -1 )
    {
        return false;
    }

    return true;
}


// simple validation of a CSRID
function JSWEBCSRValidateCSRID( strCSRID )
{
    // convert to upper case
    strCSRID = strCSRID.toUpperCase();

    // CSRID must begin with "CSR", we won't
    // care about case
    if( strCSRID.substring( 0, 3 ) != CSRIDPREFIX )
    {
        return false;
    }

    // CSRID must be exactly 11 characters long
    if ( strCSRID.length != CSRIDLENGTH )
    {
        return false;
    }

    return true;
}


// displays the error message box and focuses the element in question
function JSWEBCSRFormError( strError, objField )
{
    alert( strError );
    objField.focus();
    return false;
}


// alternates hidden/visible state of an element (by ID)
function JSWEBAlternateShowElement( objID )
{
    var objElement = document.getElementById( objID );

    if( objElement.style.display == "" )
    {
        objElement.style.display = "none";
    }
    else
    {
        objElement.style.display = "";
    }
}

// sets the "on" visibility of an element
function JSWEBShowElement( objID )
{
    var objElement = document.getElementById( objID );

    objElement.style.display = "";
}


// sets the "off" visibility of an element
function JSWEBHideElement( objID )
{
    var objElement = document.getElementById( objID );

    objElement.style.display = "none";
}



// alternates text strings in innerText of the text-containing element
// (e.g. span) based in the display style of the corresponding content element
// (e.g. div)
//
// strHidText is displayed when the content (e.g. div) is hidden
// strVisText is displayed when the content (e.g. div) is visible
//
function JSWEBAlternateText( objContentElementID, objTextElementID, strHidText, strVisText )
{
    var objContentElement = document.getElementById( objContentElementID );
    var objTextElement = document.getElementById( objTextElementID );

    if( objContentElement.style.display == "" )
    {
        objTextElement.innerText = strVisText;
    }
    else
    {
        objTextElement.innerText = strHidText;
    }
}

function JSWEBAlternateHeight( objContentElementID, objParentElementID, strHidHeight, strVisHeight )
{
    var objContentElement = document.getElementById( objContentElementID );
    var objParentElement = parent.document.getElementById( objParentElementID );

    if( objContentElement.style.display == "" )
    {
        objParentElement.style.height = strVisHeight;
    }
    else
    {
        objParentElement.style.height = strHidHeight;
    }
}

// sets the target attribute of all anchors in the document to strTarget
function JSWEBSetDocumentAnchorTargets( strTarget )
{
    var iCount = document.links.length;

    for( var iIndex = 0; iIndex < iCount; iIndex++ )
    {
        // the current anchor
        var objAnchor = document.links[ iIndex ];

        // get a string object out of the anchor
        var strAnchor = objAnchor + '';

        /* HACK HACK HACK */
        // at some point, make this function generic so we can override
        // the strTarget argument with another target based on arbitrary search
        // critera in indexed links
        if( strAnchor.indexOf( 'diagnostic.html' ) >= 0 )
        {
            objAnchor.target = '_parent';
        }
        else
        {
            objAnchor.target = strTarget;
        }
    }
}
