// Function to open a popup window to allow the selection of Action Letter Recipients

function showhide (id)  
{ 
 var style = document.getElementById(id).style  
 if (style.visibility == "hidden")  
  style.visibility = "visible";  
 else  
  style.visibility = "hidden";  
}


function checkShow ( checkbox, div_name )
{
    var div = document.getElementById(div_name);

    //if( checkbox.checked ) {
    if( div.style.display == "none") {
        div.style.display = "block";
    }
    else {
        div.style.display = "none";
    }
}


function openSelector() {
    window.open( "fc_action_letter_recipients.php", "SelectorWindow", "status=1, height=400, width=700, resizable=0" )
}

// Function to update the list of recipients in the Recipient Selection Popup

function updateSelectList(){
    var http = getHTTPObject();
    var url="lib/ax_al_recipient_selector.php?recip_type=" + document.getElementById('recipient_type').value;

    http.onreadystatechange=function(){
        if(http.readyState == 4){
            document.getElementById('div_recipient_select_list').innerHTML = http.responseText;
        }
    }

    http.open("GET", url, true);
    http.send(null);
}

// Function to add an options to the combo boxes (for IE compatibility)
function addOption(r_type,r_id,r_name)
{
    var lstRecipient = document.getElementById('recipient_list');
    var lstRecipientType = document.getElementById('recipient_type_list');

    lstRecipientType.options[lstRecipientType.length] = new Option(r_type, r_type);

    var r_type_name = 'Group';

    if( r_type == 1 ) { r_type_name = 'Individual'; }
    if( r_type == 2 ) { r_type_name = 'Group'; }

    var display_name = r_type_name + ' - ' + r_name;

    lstRecipient.options[lstRecipient.length] = new Option(display_name, r_id);
}

// Function to add a form field to the Action Letter recipient form (for IE compatibility)

/*function addFormField( f_name, f_index, f_value )
{
    var form = document.forms['al_template'];
    var field_name = f_name + "[" + f_index + "]";

    var el = document.createElement("input");
    el.type = "hidden";
    el.name = field_name;
    el.value = f_value;
    form.appendChild(el);
}*/

function addFormField( f_name, f_value )
{
    var form = document.forms['al_template'];

    var el = document.createElement("input");
    el.type = "hidden";
    el.name = f_name;
    el.value = f_value;
    form.appendChild(el);
}

function updateRecipients() {
    // Initialize local variables
    var lstRecipient = document.getElementById('recipient_list');           // Action Letter recipient list
    var lstRecipientType = document.getElementById('recipient_type_list');  // Action letter recipient type list
    var h_ids = document.getElementById('al_recipient_id');
    var h_types = document.getElementById('al_recipient_type');


    // Clear the hidden list of recipients
    h_ids.value = "";
    h_types.value = "";

    // Update the hidden list of recipients for the form
    for( var i=0; i<lstRecipient.length; i++ ) {
        h_ids.value = h_ids.value + lstRecipient.options[i].value + ",";
        h_types.value = h_types.value + lstRecipientType.options[i].value + ",";
    }

    return true;
}

// Adds the selected recipient from the Recipient Selection Popup to the Action Letter Recipient List

function addRecipient() {
    if	(window.name=="SelectorWindow") {
        // Initialize the local variables
        var selected = document.getElementById('recipient_id').selectedIndex;                   // The selectedIndex of the recipient
        var recipient_type = document.getElementById('recipient_type').value;                   // The selected recipient type
        var recipient_id   = document.getElementById('recipient_id').value;                     // The selected recipient id
        var recipient_name = document.getElementById('recipient_id').options[selected].text;    // The display name of the selected recipient

        // Initialize the target objects
        var lstRecipient = window.opener.document.getElementById('recipient_list');             // The Action Letter recipient list
        var lstRecipientType = window.opener.document.getElementById('recipient_type_list');    // the Action Letter recipient type list (hidden)

        // Check to see if the recipient and type already exist in the Action Letter recipients
        var does_not_exist = true;

        for( var i=0; i<lstRecipient.length; i++) {
            if( lstRecipient.options[i].value == recipient_id && lstRecipientType.options[i].value == recipient_type ) {
                does_not_exist = false;
                i = lstRecipient.length;
            }
        }

        if( does_not_exist ) {
            // Add the selected recipient to the target objects
            window.opener.addOption(recipient_type,recipient_id,recipient_name);
        }


    }
}

// Removes the selected recipient from the Action Letter Recipient List

function removeRecipient() {
    // Initialize the local form items
    var lstRecipient = document.getElementById('recipient_list');           // Action Letter recipient list
    var lstRecipientType = document.getElementById('recipient_type_list');  // Action letter recipient type list

    for( var i=lstRecipient.length-1; i>=0; i-- ) {
        if( lstRecipient.options[i].selected ) {
            lstRecipient.remove(i);
            lstRecipientType.remove(i);
        }
    }

    lstRecipient.selectedIndex = -1;
}

function updateRiding(selected_province)
{
    var div_fed = document.getElementById('fed_selector');
    selected_country = 40;

    if( div_fed ){
        var url = "lib/ax_gd_fed_select.php?cid=" + selected_country + "&pid=" + selected_province;

        var http = getHTTPObject();
        http.open("GET", url, true);

        http.onreadystatechange = function(){
            if(http.readyState == 4){
                div_fed.innerHTML = http.responseText;
            }
        }

        http.send(null);
    }
}

function updateMP(riding)
{
       var div_mp = document.getElementById('mp_name');

        if (div_mp){
                var url = "lib/ax_getMP.php?riding=" + riding;
                var http = getHTTPObject();
                http.open("GET", url, true);

                http.onreadystatechange = function(){
                        if(http.readyState == 4){
                                div_mp.innerHTML = http.responseText;
                        }
                }
                http.send(null);
        }
}


