/* js/ipr.js - Simple Javascript support for IPR
   ---------
   CVS: $Id: ipr.js,v 1.15 2009/05/09 03:12:19 kfrost Exp $
*/

// standard-issue DOM-getter.
function $(id) {
    return document.getElementById(id);
}

// initialize the page, dispatching omniture logging as needed.
function init_page() {

    if (typeof s == 'undefined' || !s) {
        // do nothing; omniture stuff is broken.
        return false;
    }
    else {

        // Set the common props and eVars in the Omniture object.
        s.prop1  = 'NAM';                       // region.
        s.eVar26 = s.prop1;
        s.prop2  = 'us';                        // country (of the host?!)
        s.eVar27 = s.prop2;
        s.prop3  = 'en';                        // language
        s.eVar28 = s.prop3;
        s.prop27 = 'Business';                  // visitor segment
        s.eVar50 = s.prop27;
        s.prop33 = window.location.toString();  // page url
        s.prop41 = 'Security Response';         // site section
        s.eVar41 = s.prop41;
        s.prop46 = 'html';                      // content format
        s.prop47 = 'Page';                      // content "type" (not MIME)
        s.prop29 = 'Business';                  // channel

        // All pages get analytics, but only some get events.
        var title  = document.title || 'MISSING TITLE';

        // Which page are we on?
        var id = document.body.id;
        if (id == 'LookupPage') {
            // Welcome to Event 23.  Please take a seat.
            s.events = 'event23';
        }
        else if (id == 'RemovalConfirmationPage') {
            // My name is Event 24 and I'll be your server today.
            s.events = 'event24';
        }
        else if (id == 'RemovalRequestPage') {
            // I have no Event, but my title's a bit too smart.
            title = 'IP Address Found';
        }

        // Add in the title, and the title, and the title.
        s.prop49   = title;
        s.eVar42   = title;
        s.pageName = 'en/us: biz: security-response: ' + title;

        // And away we go!
        try { s.t(); }
        catch(e) {
            // pass, nothing to do with errors in production.
            return false;
        }
        return true;

    }

}

// simple form validation for user convenience; server validates too.
function validate_removal(f) {

    // a removal_reason_N must be checked.
    var has_reasons = false;
    var e = f.elements;
    for (i=0;i<e.length;i++) {
        var name = e[i].name || '';
        if (name.match(/^removal_reason_\d+$/) && e[i].checked) {
            has_reasons = true;
        }
    }
    if (!has_reasons) {
        // alert is evil, i will replace it later.
        alert('Please specify a reason for this request.');
        return false;
    }

    // if a sample message is included in the textbox, it has to be sane.
    var mf = $('messageField');
    if (mf && mf.value) {
        mf.value = mf.value.replace(/^\s+/,'');
        if ( mf.value.length && !mf.value.match(/[^\s\:]+\:/) ) {
            alert('Please enter a valid sample message with headers.');
            return false;
        }
    }
    
    // looks good; we're free to submit.
    return true;

}

// the fake-button trickery requires this.
function submit_form(id) {

    var theForm = document.getElementById(id);
    
    // The submit event is not reliable when you use fake buttons, so:
    if (id == 'IPRinvestigateForm') {
        if (!validate_removal(theForm)) {
           return false;
        }
    }

    theForm.submit();

}

// toggle the message attachment fieldset.
function toggle_message() {

    var link = $('messageToggleLink');
    var div   = $('messageInputs');
    var textarea   = $('messageField');
    var upload     = $('messageFile');
    if (div.style.display == 'block') {
        div.style.display = 'none';
        textarea.value = '';
        upload.value = '';
        link.firstChild.nodeValue = 'Include a sample email message.';
    }
    else {
        div.style.display = 'block';
        link.firstChild.nodeValue = 'Remove sample email message.';
    }

    return false;

}

// enforce a maximum length for a textarea.
function textarea_maxlength(textarea,maxLength) {
    
    if (textarea.value.length > maxLength) {
        var txt = textarea.value.substr(0,maxLength - 14) + ' (TRUNCATED)\n';
        textarea.value = txt;
    }

}
