﻿

var recipientList = new Array();

function emailRecipient(rname,remail)
{
    this.name = rname;
    this.email = remail;
}

function validateAddRecipient(rnameid,remailid)
{
    var rname = dId(rnameid);
    var remail = dId(remailid);
    
    if(remail)
    {
        if(emailCheck(remail.value) != true)
        {
            alert('Invalid Recipient Email. Unable to add recipient.');
            return;
        }

        if(rname.value == '')
        {
            alert('Recipient Name is Empty. Please enter the recipient name and try again.');
            return false;
        }   
        
        return true;
    }
    else
        return false;
}


function addRecipient(rnameid,remailid)
{
    var rname = dId(rnameid);
    var remail = dId(remailid);
    
    if(remail)
    {
        if(emailCheck(remail.value) != true)
        {
            alert('Invalid Recipient Email. Unable to add recipient.');
            return;
        }

        if(rname.value == '')
        {
            alert('Recipient Name is Empty. Please enter the recipient name and try again.');
            return;
        }          
        
        var newRecipient = new emailRecipient(rname.value,remail.value);
        recipientList.push(newRecipient);
        
        var rlistHTML = '<table cellspacing="0" cellpadding="0" style="width:100%">';
        
        for(var i=0;i<recipientList.length;i++)
        {
            rlistHTML += '<tr><td width="80%" class="RecipientField">' + recipientList[i].name + ' (' + recipientList[i].email + ') </td>'
            rlistHTML += '<td class="RecipientField"><input type="image" name="cmdrlistDelete' + i + '" src="siteImages/deleteButton.jpg" onclick="delRecipient(\'' + recipientList[i].email + '\');" /></td></tr>'
        }
        
        rlistHTML += '</table>';
        
        var rlistPanel = dId('eshelfRecipients');
        
        rlistPanel.innerHTML = rlistHTML;
    }    
}

function delRecipient(remail)
{
    var found=false;
    
    for(var i=0;i<recipientList.length;i++)
    {
        if(recipientList[i].email == remail)
        {
            recipientList.splice(i,1);
            found = true;
        }
    }
    
    if(found)
    {
        var rlistHTML = 'No Recipients';
        var rlistPanel = dId('eshelfRecipients');
        
        if(recipientList.length > 0)
        {
            rlistHTML = '<table cellspacing="0" cellpadding="0" style="width:100%">';
            
            for(var i=0;i<recipientList.length;i++)
            {
                rlistHTML += '<tr><td width="80%" class="RecipientField">' + recipientList[i].name + ' (' + recipientList[i].email + ') </td>'
                rlistHTML += '<td  class="RecipientField"><input type="image" name="cmdrlistDelete' + i + '" src="siteImages/deleteButton.jpg" onclick="delRecipient(\'' + recipientList[i].email + '\');" /></td></tr>'
            }
            
            rlistHTML += '</table>';
        }
        
        rlistPanel.innerHTML = rlistHTML;    
    }
    else
    {
        alert('Recipient doesn\'t exists'); 
    }
}

function emailEShelf(userid,catTypeid, sourcelboxid,subjectid,bodyid)
{
    var eSubject = dId(subjectid);   
    var eBody = dId(bodyid);
    var catType  = dId(catTypeid);
    var bodyText = (eBody==null)?'':eBody.value;
    
    if(!eSubject)
    {
        alert('Unable to validate the form. Cannot proceed.');         
        return false;
    }
    
    if(eSubject.value == '')
    {
        alert('Subject field is a required field. Please enter a valid subject and try again.');         
        return false;    
    }
    
    if(recipientList.length <= 0)
    {
        alert('No recipient exists to email the lightbox. Please one or more recipient(s) and try again.');         
        return false;     
    }
    
    var catTypeItem = rdoListSelected(catType);
    
    if(catTypeItem == null)
    {
        alert('Unable to read the catalog type selection. Please select the catalog template and try again.');         
        return false;       
    }
    
    ActionImages.AIMedia.EShelfService.AddNewEmailShelf(userid, catTypeItem.value, sourcelboxid, recipientList,eSubject.value,bodyText, eShelfSucceededCallbackGeneric, eShelfFailedCallbackGeneric);
    
    return true;
}

function eshelfCatalogChange(catType)
{
    var catTypeItem = null;
    
    catTypeItem = rdoListSelected(catType);
    
    if(catTypeItem)
    {
        var catImage = dId('eshelfcatalogImage');
        
        if(catImage)
        {
            catImage.src = 'siteImages/' + catTypeItem.value + '.jpg';
        }
    }
}

function rdoListSelected(objList)
{
    var objItem = null;
    var selectedItem = null;
    
    for(var i=0;i<objList.rows.length;i++)
    {
        objItem = dId(objList.id + "_" + i);
        
        if(objItem )
        {
            if(objItem.checked)
            {
                break;
            }
        }
    }
    
    return objItem;
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function eShelfSucceededCallbackGeneric(result, eventArgs)
{
    // Page element to display feedback.
    if(result == true)
    {
        alert('The lightbox successfully emailed to your recipients.');
        self.close();
    }
    else
        alert('Error occured while emailing your lightbox.');
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function eShelfFailedCallbackGeneric(error)
{
    // Display the error.    
    //var RsltElem = document.getElementById("ResultId");
    //RsltElem.innerHTML = "Service Error: " + error.get_message();
    alert('Error occured while emailing your lightbox.');
}
