﻿// <copyright file="MyBioCenturyScriptCompany.js" company="BioCentury Publications Inc."> 
// All contents Copyright © 2005-2009 BioCentury Publications, Inc. ALL RIGHTS RESERVED
// 
// http://www.biocentury.com 
// </copyright>

// JScript File

var checkCount = 0;

//---------------------------------------------------------------------------
//Used in mybvToolList , for checkboxes
//---------------------------------------------------------------------------
function ToggleCheckAllCompany(cb)
{
	var bAllChecked = true;
    var checks = document.getElementsByName("cb");
		
    for(var i=0; i<checks.length; ++i)
    {				
        var checkbox = checks[i];
        if(!checkbox.checked)
        {
            bAllChecked = false;
            break;
        }
    }    
    var strSelected = document.getElementById('ctl00_contentRight_toolListDataGridCompany_CompanyListSelections').value;
    
    var criteriaId = cb.id.split('_')[1]; 
    if (cb.checked)
        {
        strSelected = strSelected + criteriaId + ',';
        checkCount++;
        }
    else
    {
        strSelected = strSelected.split(criteriaId + ',')[0] + strSelected.split(criteriaId+',')[1];
        checkCount--;
    }
		
	
    document.getElementById('ctl00_contentRight_toolListDataGridCompany_CompanyListSelections').value = strSelected;
    document.getElementById("cbAll").checked = bAllChecked;
}

//----------------------------------------------------------------------------
//Used in mybcToolList , Check all function
//----------------------------------------------------------------------------
function CheckAllCompany(bCheck)		
{
	var strSelected = "";
    if (document.getElementById('cbAll') != null)
        document.getElementById('cbAll').checked = bCheck;	
    var checks = document.getElementsByName('cb');
	
    checkCount =(bCheck )?checks .length:0;
    for(var i = 0; i < checks.length; ++i)
    {				
        var criteriaIds = checks[i].id.split('_')[1]; 
        checks[i].checked = bCheck;
        if (bCheck == true)
            strSelected += criteriaIds + ","; 					
    }
    document.getElementById("ctl00_contentRight_toolListDataGridCompany_CompanyListSelections").value = strSelected;
}
/*
//--------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------
function confirmOverwriteCriteriaCompany()
{
   if (confirm("This Criteria Name already exists.\n Are you sure you want to overwrite it?"))
   {
//        var btn = document.getElementById('ctl00_contentRight_toolListDataGridCompany_hdnButtonConfirm');
//        if (btn) btn.click()

        document.getElementById("ctl00_contentRight_toolListDataGridCompany_hdnButtonConfirm").click();      
   }
   else
   {
        return false;
   }   
}
*/

//-------------------------------------------------------------------------------
// "Confirm Delete Selected"
//-------------------------------------------------------------------------------
function ConfirmDeleteSelectedCompaniesList(CompanyListSelectionsID, CompanyListNamesID, userId) 
{
	try
	{
	    var hdnIds = document.getElementById(CompanyListSelectionsID).value;
	    if (hdnIds != "") {
	        var hdnIdsArray = hdnIds.split(',');
	        var hdnNames = document.getElementById(CompanyListNamesID).value;
	        var hdnNamesArray = hdnNames.split(',');
	        var name = "";
	        companiesInAlert = 0;
	        for (var i = 0; i < hdnIdsArray.length - 1; i++) {
	            for (var j = 0; j < hdnNamesArray.length - 1; j++) {
	                if (hdnIdsArray[i] == hdnNamesArray[j].split('|')[1]) {
	                    var isBCList = AjaxUtilities.IsReservedListName(hdnNamesArray[j].split('|')[0], userId);
	                    if (isBCList.value) {
	                        alert('The ' + hdnNamesArray[j].split('|')[0] + ' is a reserved list name. You cannot delete it.');
	                        return false;
	                    }

	                    var IsCompanyListUsedInAlerts = AjaxUtilities.IsCompanyListUsed(hdnNamesArray[j].split('|')[1]);
	                    if (IsCompanyListUsedInAlerts.value) {
	                        if (companiesInAlert > 0) {
	                            name += ", "
	                        }
	                        name += "'" + hdnNamesArray[j].split('|')[0] + "'";
	                        companiesInAlert++;
	                    }
	                }
	            }
	        }
	        if (companiesInAlert == 0) {
	            return confirm("Are you sure you want to delete selected list(s)?");
	        }
	        else {
	            if (companiesInAlert == 1) {
	                return confirm("The " + name + " company list is used for email alerts. \nDeleting the company list will also delete the email alerts. \nAre you sure you want to delete selected list(s)?");
	            }
	            else {
	                return confirm("The " + name + " company lists are used for email alerts. \nDeleting the company lists will also delete the email alerts. \nAre you sure you want to delete selected list(s)?");
	            }
	        }
	    }
	    else {
	        alert("You must select a Companies List first.");
	    }
	}
	catch(err) 
	{
		alert("Some error occured:\n" + err.message);
	}
	return false;
}

//----------------------------------------------------------------------------------------
//  Opens a popup for the selected company list, to EDIT. It opens the FetchListOfCompanies.aspx
//  from BCIX/Company 
//----------------------------------------------------------------------------------------
function editList()
{
	var listNames = document.getElementById('ctl00_contentRight_toolListDataGridCompany_CompanyListNames').value.split(',');
	
	var cbs = document.getElementsByName('cb');
	var count = 0;
	var id = 0;
	for (var i=0;i<cbs.length;i++)
	{
		if (cbs[i].checked)
		{
			count++;
			id = cbs[i].id.split('_')[1];			
		}
	}
	
	var tempId = 0;
	var lName;
	for(var i=0;i<listNames.length;i++)
	{
	    tempId = listNames[i].split('|')[1];
	    if(tempId == id)
	    {
	        lName=listNames[i].split('|')[0];
	        break;
	    }
	}	
	
	if (count == 0)
		alert('You must select a list.');
	else if (count == 1)
		//window.open('../BCIQ/Company/FetchListOfCompanies.aspx?popup=1&listId='+id+'&name='+lName, '_popup', 'status=yes,resizable=yes,toolbar=no,menubar=no,scrollbars=yes');
	    window.open('../bciq/Common/ViewModifyCompanyList2.aspx?ControlType=TablesCompanies&CurrentListID=' + id + '&CurrentListName=' + lName + '&MyBiocentury=1&Module=5&DisabledEditing=false', '_popup', 'status=yes,resizable=yes,toolbar=no,menubar=no,scrollbars=yes');
	else
		alert('Please select one list only.');
	return false;
}

//--------------------------------------------------------------------------------------------
//Merge Company Lists
//--------------------------------------------------------------------------------------------
var mergeWindow = null;

function mergeLists() 
{
	//asd;
	//var select = document.getElementById("UserCompanyLists");
	var hdnIds = document.getElementById("ctl00_contentRight_toolListDataGridCompany_CompanyListSelections").value;
	if(hdnIds == "")
	{
		alert("You must select a list.");
		return false;
	}
	else
	{
		//var intoListID = document.getElementById("intoListID").value;
		//if (intoListID == 0)
		//{//new list
			if (mergeWindow) 
			{
				try
				{
					mergeWindow.focus();
					return false;
				}
				catch(e)
				{
					mergeWindow = null;
				}
			}
			if (document.getElementById('ctl00_contentRight_toolListDataGridCompany_btnMergeListsClicked').value == '0')
			{
				leftVal = (screen.width - 500) / 2;
				topVal = (screen.height - 150) / 2;
				mergeWindow = window.open("MergeCompanyList.aspx?isMyBioCentury=1", "_blank", 'width=500,height=200,left='+leftVal+',top='+topVal+',toolbar=0,resizable=0');
				return false;
			}
			else
			{
				document.getElementById('ctl00_contentRight_toolListDataGridCompany_btnMergeListsClicked').value = '0';
			}
		//}
		//else
		//{//some other list
		//	document.getElementById("intoListID").value = intoListID;
		//}
		return true;
	}
}
	
//---------------------------------------------------------------------------
// Added by Claudiu Sabau, October 04, 2007
// Opens the FetchListOfCompanies.aspx from BCIQ for a selected company name
// Used in GenerateArchivesDataMember.ascx
//---------------------------------------------------------------------------
/*function viewModifyListCompany(fullListNames, listName)
{       
    var companyList = fullListNames.value.split(',');
    var tempId = 0;
    
    for (var i = 0; i < companyList.length - 1; i++)
    {
        if(companyList[i].split('|')[0] == listName)
        {
            tempId = companyList[i].split('|')[1];
            break;        
        }
    }
    window.open('../BCIX/Company/FetchListOfCompanies.aspx?popup=1&listId='+tempId+'&name='+listName, '_popup', 'status=yes,resizable=yes,toolbar=no,menubar=no,scrollbars=yes');    
}
*/

//---------------------------------------------------------------------------
// Added by Silvia Tanase
//---------------------------------------------------------------------------
function viewModifyList(select, lname, lid, disabledEditing) 
{
    window.open('../bciq/Common/ViewModifyCompanyList2.aspx?ControlType=TablesCompanies&CurrentListID=' + lid + '&CurrentListName=' + lname + '&MyBiocentury=1&Module=5&DisabledEditing=' + disabledEditing, '_popup', 'status=yes,resizable=yes,toolbar=no,menubar=no,scrollbars=yes');
}
