﻿// JScript File

/*************************************************************************************
*   EMAIL REPORT
*
*       Emails the selections made by the user to the desired recipient.   
*
*************************************************************************************/

// Sends an email to the desired address
function SendEmail( ){    
    var details = "\nThe following selections have been made:\n";
    
    // Topic
    var element = document.getElementById( "DdlTopic");
    var index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\tTopic: ";
        details += element.options[index].text.trim();
    }
    
    // Measure
    element = document.getElementById( "DdlMeasure");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\tMeasure: ";
        details += element.options[index].text.trim();
    }
    
    // Sub Measure
    element = document.getElementById( "DdlSubMeasure");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\tSub Measure: ";
        details +=element.options[index].OptionGroup + " --> ";
        
        details += element.options[index].text.trim();
    }
    
    // Report
    element = document.getElementById( "ddl_Reports_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\tReport: ";
        details +=element.options[index].OptionGroup + " --> ";
        
        details += element.options[index].text.trim();
    }
    
    // Param1
    element = document.getElementById( "DdlParams1_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\t" + document.getElementById( "lblParams1").innerHTML+ " ";
        details += element.options[index].text.trim();
    }
    
    // Param2
    element = document.getElementById( "DdlParams2_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\t" + document.getElementById( "lblParams2").innerHTML+ " ";
        details += element.options[index].text.trim();
    }
    
    // Param3
    element = document.getElementById( "DdlParams3_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\t" + document.getElementById( "lblParams3").innerHTML+ " ";
        details += element.options[index].text.trim();
    }
    
    // Param4
    element = document.getElementById( "DdlParams4_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\t" + document.getElementById( "lblParams4").innerHTML+ " ";
        details += element.options[index].text.trim();
    }
    
    // Param5
    element = document.getElementById( "DdlParams5_AJAX");
    index = element.selectedIndex;
    if( index != -1)
    {
        details += "\n\t" + document.getElementById( "lblParams5").innerHTML+ " ";
        details += element.options[index].text.trim();
    }    
    
    
//    // Age, Income and Year
//    if( document.getElementById("RbtnAgeSex").checked)    
//        details += "\n\tAge/Sex Selected";
//    
//    if( document.getElementById("RbtnIncome").checked)
//        details += "\n\tIncome Selected";
    
//    index = GetYearText();
//    if( index)
//        details += "\n\tYear: " + index;
        
    details += "\n\nTo view this report click the following link: " + GetUrl();   

    // convert extra long dashes into short dashes, then escape all special characters
    // the extra long dash 'escapes' poorly so it must be removed before the escape    
    details = escape(details.replace( /–/m, "-"));    
    
    window.open( "mailto:[Enter e-mail address]?subject=Report from ICES inTool&body=" + details, "xxxx");
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

// Get the complete url for the current selections
function GetUrl(){
    var QS= document.getElementById("hQS");    
    return QS.innerHTML;
}

// Gets the entire list of selected items (shown when printing)
function GetBreadCrumb()
{
    var crumb = "";
    // Topic
    var element = document.getElementById( "DdlTopic");
    var index = element.selectedIndex;
    if( index != -1) { crumb += element.options[index].text.trim(); }
    
    // Measure
    element = document.getElementById( "DdlMeasure");
    index = element.selectedIndex;
    if( index != -1) { crumb += " > " + element.options[index].text.trim(); }
    
    // Sub Measure
    element = document.getElementById( "DdlSubMeasure");
    index = element.selectedIndex;
    if( index != -1) { crumb += " > " + element.options[index].text.trim(); }
    
    // Area
    element = document.getElementById( "DdlArea");
    index = element.selectedIndex;
    if( index != -1) { crumb += " > " + element.options[index].text.trim(); }
    
    // Age, Income and Year
    if( document.getElementById("RbtnAgeSex").checked)    
        crumb += " > Age/Sex";
    
    if( document.getElementById("RbtnIncome").checked)
        crumb += " > Income";
    
    crumb += " > " + GetYearText();
        
    return crumb;
}

// Refreshes the bread crumb for when the report is printed directly in the page
function RefreshBreadCrumb()
{
 //   if( isIE)
 //       document.getElementById("printSelectionText").innerText = GetBreadCrumb();
 //   else
 //       document.getElementById("printSelectionText").textContent = GetBreadCrumb();
}

// Functions for retrieving the value of specific elements (used to in breadcrumb and email summary)

// retrieves the text for the selected year
function GetYearText()
{    
    var index = document.getElementById( "lbYear").innerText;
    if( index == "empty")
    {
        var element = document.getElementById("ddlyear");
        index = element.selectedIndex
        index = element.options[index].text;
        
    }
    if( index)
        index = index.replace( "year:", '').replace( "Year:", '');
        
    return index;
}


