﻿var _commonLoaderID = 'Loader';
var _appPath = 'http://www.richsdata.com/';
var _appPathSecure = 'https://richsdata.com/Secure/';
var _loading = false;
var _searchID;
var _collectionID;

/*--------------------------------------------------------------- */
function rdRefreshPage()
{
    window.location.reload( false );
}
/*--------------------------------------------------------------- */
function rdNotAvailable()
{
	alert('Feature not yet available');
}

/*--------------------------------------------------------------- */
function rdShowHelp(id) {

    var url = _appPath + "Help.aspx";

    if (id.length > 0) {
        url += "#" + id;
    }
    
    var helpme = window.open(url, "HelpWindow", "menubar=0,location=0,resizable=0,scrollbars=1,toolbar=0,width=600,height=550");
    helpme.focus();
}

/*--------------------------------------------------------------- */
function rdShowLoader(loaderPrefix) {

    var id = '';

    if (loaderPrefix.length > 0) {
        id = loaderPrefix + '_' + _commonLoaderID;
    }
    else {
        id = _commonLoaderID;
    }

    if ($(id)) { $(id).style.display = 'block'; }
}
/*--------------------------------------------------------------- */
function rdHideLoader(loaderPrefix) {

    var id = '';
    
    if (loaderPrefix.length > 0) {
        id = loaderPrefix + '_' + _commonLoaderID;
    }
    else {
        id = _commonLoaderID;
    }

    if ($(id)) { $(id).style.display = 'none'; }
    
}
/*--------------------------------------------------------------- */
function rdPrintPage()
{
	window.print();
}
/*--------------------------------------------------------------- */
function rdEmailPage()
{
	var who = '[email]';
	var what = 'This might be of interest to you: ' + document.title;
	var link = window.location.href;
	var body = document.title + '  \r\n' + link;
	parent.location.href = 'mailto:' + who + '?subject=' + what + '&body=' + body;
}
/*--------------------------------------------------------------- */
function rdBookmarkPage()
{
	var urlAddress = window.location.href;
	var pageName = document.title;
	
	if (window.external)
	{
		window.external.AddFavorite(urlAddress,pageName)
	} 
	else
	{
		alert("Sorry, your browser doesn't support this function.");
	}		
}
/*--------------------------------------------------------------- */
function rdValidateEmail(str)
{    
    var validStr = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.-';
    var mandStr = '@.';
    var iPresentCount = 0;
        
    if(str.length > 0)
    {
        for (i = 0; i < str.length; i++)
        {
            if (validStr.indexOf(str.charAt(i)) == -1)
                return false;
        }
        
        for (i = 0; i < str.length; i++)
        {
            if (mandStr.indexOf(str.charAt(i)) != -1)
                iPresentCount++;
        }

        if (iPresentCount < mandStr.length)
            return false;
    }
    else
    {
        return false;
    }

    return true;
}

/*--------------------------------------------------------------- */
function rdShowTab(tabid) {
    rdHideTabs();

    if ($('tb' + tabid)) {
        $('tb' + tabid).className = 'TabSel';
    }

    if ($('ts' + tabid)) {
        $('ts' + tabid).className = 'TabSectionShow';
    }
}

/*--------------------------------------------------------------- */
function rdHideTabs() {

    // reset the tabs
    var tabs = $$('.TabSel');

    if (tabs != null) {
        for (var i = 0; i < tabs.length; i++) {
            tabs[i].className = 'Tab';
        }
    }

    // reset the sections
    var sections = $$('.TabSectionShow');

    if (sections != null) {
        for (var i = 0; i < sections.length; i++) {
            sections[i].className = 'TabSection';
        }
    }
}

/*--------------------------------------------------------------- */
function rdShowCountyLoader() {
    if ($('CountyList')) {
        $('CountyList').innerHTML = '<img src="' + _appPath + 'images/loaders/loader7.gif" alt="loading"/>';
    }
}

/*--------------------------------------------------------------- */
function rdDoCancel() {
    history.go(-1);
}

/*--------------------------------------------------------------- */
function rdGetPackageListWidget() {
    new Ajax.Request(_appPath + "callbacks/PackageListWidget.aspx" + rdGetRandomNumber(),
    {
        method: 'post',
        onSuccess: function(transport) {
            rdGetPackageListWidgetSuccess(transport.responseText);
        },
        onFailure: function(transport) {
            alert('Error retrieving packages list');
        }
    });
}

/*--------------------------------------------------------------- */
function rdGetPackageListWidgetSuccess(retVal) {
    if ($('PackageList')) {
        $('PackageList').innerHTML = retVal;
    }
}

/*--------------------------------------------------------------- */
function GetCollectionID() {
    if ($('cid')) {
        return $('cid').value;
    }
    else {
        return '0';
    }
}

/*--------------------------------------------------------------- */
function GetSearchID() {
    if ($('sid')) {
        return $('sid').value;
    }
    else {
        return '';
    }
}

/*--------------------------------------------------------------- */
function GetCallCampaignCompanyID() 
{
    if ($('cccid')) 
    {
        return $('cccid').value;
    }
    else {
        return '0';
    }
}

/*--------------------------------------------------------------- */
function GetCompanyID() 
{
    if ($('comid')) 
    {
        return $('comid').value;
    }
    else {
        return '0';
    }
}

/*--------------------------------------------------------------- */
function GetUpdateStatus() {
    if ($('naicsupdate')) {
        return '1';
    }
    else {
        return '0';
    }
}

/*--------------------------------------------------------------- */
function rdHideDownloadOptions() {
    rdHideDownloadError();
    if ('dvDownloadOptions') {
        $('dvDownloadOptions').style.display = 'none';
    }
}

function rdHideDownloadError() {
    if ($('dvDownloadError')) {
        $('dvDownloadError').innerHTML = '';
        $('dvDownloadError').style.display = 'none';
    }
}

function rdGetRandomNumber(append)
{
    var d = new Date();
    var time = d.getTime();

    if (arguments.length > 0)
    {
        return "&rnd=" + time;
    }
    else
    {
        return "?rnd=" + time;
    }
}

/*--------------------------------------------------------------- */
function popup(url, height, width, name) 
{
    var width = width;
    var height = height;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;
    params += ', top=' + top + ', left=' + left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=yes';
    params += ', status=no';
    params += ', toolbar=no';
    newwin = window.open(url, name, params);
    if (window.focus) { newwin.focus() }
    return false;
}

/*--------------------------------------------------------------- */
function rdShowEndofDayStatistics(userID, userType) 
{
    var url = _appPath + 'ResearcherTracking/EndOfDayStatistics.aspx?uid=' + userID + '&utype=' + userType;
    
    popup(url, 560, 930, 'popupwindow');
}

// ---------------------------------------------------------------------------------------
function rdGetSelectedCodeCount(id) 
{
    if (_userType == "Admin") 
    {
        actionUrl = "../../Callbacks/Trees/UpdateCompanyCode.aspx";
    }
    else 
    {
        actionUrl = "../Callbacks/Trees/UpdateCallCampaignCompanyCode.aspx";
    }

    new Ajax.Request(actionUrl,
    {
        method: 'post',
        parameters: { id: id, type: 'count' },
        onSuccess: function(transport) 
        {
            rdGetSelectedCodeCountSuccess(transport.responseText);
        },
        onFailure: function(transport) 
        {
            alert('Error selecting tree node');
        }
    });
}

// ---------------------------------------------------------------------------------------
function rdGetSelectedCodeCountSuccess(retVal) 
{
    var arrlist = retVal.substring(8).split('_');
    var sicCount = arrlist[0];
    var prcCount = arrlist[1];
    var naicsCount = arrlist[2];

    if (retVal.substring(0, 7) == "success") 
    {
        if ($('prccode') != null) 
        {
            if (prcCount != 0) 
            {
                $('prccode').value = "success";
            }
            else 
            {
                $('prccode').value = "";
            }
        }

        if ($('naicscode') != null) 
        {
            if (naicsCount != 0) 
            {
                $('naicscode').value = "success";
            }
            else 
            {
                $('naicscode').value = "";
            }
        }
    }
}

function rdPRCCodeChart() {
    var prccodechart = window.open(_appPathSecure + "PRCCodeChart.aspx", "PRCCodeWindow", "scrollbars=1,menubar = 0, location = 0, resizable = 1, toolbar = 0, width = 450, height = 400");
    prccodechart.focus();
}