﻿var _formName = 'frmCountWizard';
var _countResultPanel = 'SearchCountResult';
var _countLoader = 'SearchCountLoader';
var _progressBar = 'WizProgress';
var _wizPageArray = [];
var _totalPages = 8;
var _resultPage = 8;

function rdShowProgress(currentPage) {
    if ($(_progressBar)) {
        $(_progressBar).innerHTML = '';
        for (var x = 0; x < _wizPageArray.length; x++) {
            if (undefined != _wizPageArray[x]) {
                var suffix = x < (parseInt(currentPage)) ? 'on' : 'off' ;
                $(_progressBar).appendChild(new Element('img', { 'src': _appPath + 'images/icons/dot_' + suffix + '.gif' }));
            }
        }
    }
}

function rdToggleHideShow(panel) {
    if ($(panel)) {
        if ($(panel).className == 'hide') {
            $(panel).className = 'show';
        }
        else {
            $(panel).className = 'hide';
            
            // do I need to clear out any values?
            if ('dvZipProx' == panel) {
                rdClearZipProximity();
            }
        }       
    }
}

function rdToggleSelectedItems(obj, itemName) {

    if (obj) {

        if (obj.checked) {        
            rdClearCheckboxes(itemName);
        }
        /*
        else {
            rdEnableCheckboxes(itemName);
        }
        */
    }
}

function rdClearCheckboxes(itemName) {
    var itemArray = $(_formName).getInputs('checkbox', itemName);

    for (var x = 0; x < itemArray.length; x++) {
        itemArray[x].checked = false;
    }

    //itemArray.invoke('disable');
}

/*
function rdEnableCheckboxes(itemName) {
    var itemArray = $(_formName).getInputs('checkbox', itemName);
    itemArray.invoke('enable');
}
*/

function rdClearZipProximity() {
    if ($('zip_prox_dist') && $('zip_prox')) {
        $('zip_prox_dist').selectedIndex = 0;
        $('zip_prox').value = '';
    }
}

function rdClearTextBoxes(itemName) {
    var itemArray = $(_formName).getInputs('text', itemName);

    for (var x = 0; x < itemArray.length; x++) {
        itemArray[x].value = '';
    }
}

function rdInitWizardPages() {

    _wizPageArray = [];
    
    _wizPageArray[0] = 1;
    _wizPageArray[1] = 2;

    if ($('chkUsePRC')) {
        if ($('chkUsePRC').checked) {
            _wizPageArray[2] = 3;
        }
    }

    if ($('chkUseNAICS')) {
        if ($('chkUseNAICS').checked) {
            _wizPageArray[3] = 4;
        }
    }

    if ($('chkUseSIC')) {
        if ($('chkUseSIC').checked) {
            _wizPageArray[4] = 5;
        }
    }
    
    _wizPageArray[5] = 6;
    _wizPageArray[6] = 7;
    _wizPageArray[7] = 8;
}

function rdNextWizPage(currentPage, action) {

    rdSaveSearch();
    rdInitWizardPages();
    var nextPage = parseInt(currentPage);
    var x = parseInt(currentPage);

    if ('n' == action) {
        for (var x = parseInt(currentPage); x < _totalPages; x++) {
            if (undefined != _wizPageArray[x]) {
                nextPage = _wizPageArray[x];
                break;
            }
        }
    }
    else {
        for (var x = parseInt(currentPage)-2; x >= 0; x--) {
            if (undefined != _wizPageArray[x]) {
                nextPage = _wizPageArray[x];
                break;
            }
        }
    }

    //var nextPage = 'wizard.aspx?sid=' + GetSearchID() + '&wp=' + nextPage;
    //window.location = nextPage;
    
    
    rdHideAllWizPages();
    rdShowWizPage(nextPage);
    rdShowProgress(nextPage);
   
    if (_resultPage === nextPage) {
        rdShowCountResult();
    }
}

function rdHideAllWizPages() {
    var arr = $$('.WizardPageOn');

    for (var x = 0; x < arr.length; x++) {
        arr[x].className = 'WizardPage';
    }
}

function rdHideWizPage(pg) {
    if ($('wizp' + pg)) {
        $('wizp' + pg).className = 'WizardPage';
    }
}

function rdShowWizPage(pg) {
    if ($('wizp' + pg)) {
        $('wizp' + pg).className = 'WizardPageOn';
    }
}

function rdSaveSearch() {
    if ($(_formName)) {

        var params = $(_formName).serialize()

        // save the search criteria
        new Ajax.Request(_appPath + "Count/Callbacks/SaveSearch.aspx" + rdGetRandomNumber(),
        {
            method: 'post',
            parameters: params,
            onSuccess: function(transport) {
                rdSaveSearchSuccess(transport.responseText);
            },
            onFailure: function(transport) {
                alert('Error saving search selections');
            }
        });
    }
}

function rdSaveSearchSuccess(retVal) {
    if ('success' == retVal) {
        rdShowSearchCriteriaSummary();
    }
}

/*--------------------------------------------------------------- */
function rdShowSearchCriteriaSummary() {

    var sid = GetSearchID();

    // create prototype request
    new Ajax.Request(_appPath + 'callbacks/Search/GetSearchCriteriaSummary.aspx' + rdGetRandomNumber(),
	{
	    method: 'post',
	    parameters: { search_id: sid },
	    onSuccess: function(transport) {
	        rdDisplaySearchCriteria(transport.responseText);
	    },
	    onFailure: function(transport) {
	        alert('Error retrieving search summary criteria');
	    }
	});
}

/*--------------------------------------------------------------- */
function rdDisplaySearchCriteria(retval) {
    if ($('CriteriaSummary')) {
        $('CriteriaSummary').innerHTML = retval;
    }
}

/*--------------------------------------------------------------- */
function rdRunCount() {

    if ($(_countResultPanel)) {
        $(_countResultPanel).innerHTML = '';
    }
    
    if ($(_countLoader)) {
        $(_countLoader).style.display = 'block';
    }

    var sid = GetSearchID();

    // create prototype request
    new Ajax.Request(_appPath + 'Count/Callbacks/GetSearchCount.aspx' + rdGetRandomNumber(),
	{
	    method: 'post',
	    parameters: { sid: sid },
	    onSuccess: function(transport) {
	        rdRunCountSuccess(transport.responseText);
	    },
	    onFailure: function(transport) {
	        alert('Error retrieving search count');
	    }
	});
}

function rdRunCountSuccess(retval) {
    if ($(_countLoader)) {
        $(_countLoader).style.display = 'none';
    }
    
    if ($(_countResultPanel)) {
        $(_countResultPanel).innerHTML = retval;
    }
}

function rdShowCountResult() 
{
    var sid = GetSearchID();

    if (sid.length > 0)
    {
        //window.location = _appPath + "Count/ResultSummary.aspx?sid=" + sid;
        setTimeout('redirect("'+ sid +'")', 4000);
    }
}

function redirect(sid)
{
    url = _appPath + 'Count/ResultSummary.aspx?sid=' + sid + rdGetRandomNumber(true);
    window.location.href = url;
}

function rdGetCountResults(pg) {

    var sid = GetSearchID();

    if (sid.length > 10) {

        // create prototype request
        new Ajax.Request('Callbacks/GetCountResults.aspx' + rdGetRandomNumber(),
	    {
	        method: 'post',
	        parameters: { sid: sid, pg: pg },
	        onSuccess: function(transport) {
	            rdGetCountResultsSuccess(transport.responseText);
	        },
	        onFailure: function(transport) {
	            alert('Error retrieving search count');
	        }
	    });
    }
}

function rdGetCountResultsSuccess(retval) {
    if ($('dvCountResults')) {
        $('dvCountResults').innerHTML = retval;
    }
}

function rdToggleContactInclusion(recid) {
    if ($('chkRec' + recid)) {

        var action = '';

        if (true === $('chkRec' + recid).checked) {
            action = 'inc';
        }
        else {
            action = 'exc';
        }

        var sid = GetSearchID();

        // create prototype request
        new Ajax.Request('Callbacks/ToggleContactInclusion.aspx' + rdGetRandomNumber(),
	    {
	        method: 'post',
	        parameters: { sid: sid, recid: recid, action: action },
	        onSuccess: function(transport) {
	        rdToggleContactInclusionSuccess(recid, action, transport.responseText);
	        },
	        onFailure: function(transport) {
	            alert('Error updating the record inclusion parameter');
	        }
	    });
    }
}

function rdToggleContactInclusionSuccess(recid, action, retval) {

    if ('success' == retval) {
        if ($('rec' + recid)) {
            
            switch(action) {
                case "inc":
                    $('rec' + recid).style.textDecoration = "none";
                    break;

                case "exc":
                    $('rec' + recid).style.textDecoration = "line-through";
                    break;
            }
        }
    }
}

function rdIncludeAllSearchRecords() {
    var sid = GetSearchID();

    new Ajax.Request('Callbacks/ToggleContactInclusion.aspx' + rdGetRandomNumber(),
    {
        method: 'post',
        parameters: { sid: sid, recid: 0, action: 'all' },
        onSuccess: function(transport) {
            rdGetCountResults(1);
        },
        onFailure: function(transport) {
            alert('Error updating the record inclusion parameter');
        }
    });
}

function rdToggleOneContact(setting) {
    rdUpdateSearchOneContact(setting);    
    //rdRefreshPage();
}

function rdGetRandomNumber(append)
{
    var d = new Date();
    var time = d.getTime();

    if (arguments.length > 0)
    {
        return "&rnd=" + time;
    }
    else
    {
        return "?rnd=" + time;
    }
}

function rdShowSaveSubscriberSearch() 
{
    //Dialog.alert($('dvSaveSubscriberSearch').innerHTML, { className: "dialog", height: 300, width: 400, okLabel: "Close Window" });
    Modalbox.show($('dvSaveSubscriberSearch'), { title: 'Save Search', widgetid: '9', height:300, width:500, overlayClose: false });
}

function rdHideSaveSubscriberSearch()
{
    //    if ('dvSaveSubscriberSearch')
    //    {
    //        $('dvSaveSubscriberSearch').style.display = 'none';
    //    }

    if (Modalbox)
    {
        Modalbox.hide();
    }

}

function rdSaveSubscriberSearch(collID, collName)
{
    searchName = document.getElementById('txtSearchName').value.replace("'","");
    if (searchName == '')
    {
        alert('Enter search name.');
        document.getElementById('txtSearchName').focus();
        return false;
    }

    var sid = GetSearchID();

    if (!sid || sid == 0 || sid == '')
    {
        alert('error saving search.');
        return false;
    }

    // save the search criteria
    new Ajax.Request(_appPath + "Count/Callbacks/SaveSubscriberSearch.aspx" + rdGetRandomNumber(),
        {
            method: 'post',
            parameters: { sid: sid, searchName: searchName, collName: collName, collID: collID},
            onSuccess: function(transport)
            {
                rdSaveSubscriberSearchSuccess(transport.responseText);
            },
            onFailure: function(transport)
            {
                alert('Error saving search selections');
            }
        });

    return false;
}

function rdSaveSubscriberSearchSuccess(retVal)
{
    rdHideSaveSubscriberSearch();
    if ('success' == retVal)
    {
    
        alert('You have saved your search successfully. To view this search and any other saved searches, please go to the "My Account" page located in the top right menu bar.');
        //Modalbox.show($('dvSaveSubscriberSearch'), { title: 'You have saved your search successfully', widgetid: '9', height:10, width:500, overlayClose: false });
    }
    else
    {
        alert('You Search could not be saved.');
        //Modalbox.show($('dvSaveSubscriberSearch'), { title: 'Your Search could not be saved', widgetid: '9', height:10, width:500, overlayClose: false });
    }
}




