﻿var keywordFewerElementName;
var keywordElementName;
var ratingElementName;
var contentTypeElementName;
var employeeSizeElementName;
var publicationDateFromElementName;
var publicationDateToElementName;
var pCCountElementName;
var industryElementName;
var countryElementName;
var audienceElementName;
var competitorElementName;
var languageElementName;
var mediaElementName;
var productElementName;

function clearMoreSearchOptions() {

    
    var keyword = document.getElementById(keywordElementName);
    var rating = document.getElementById(ratingElementName);
    var contentType = document.getElementById(contentTypeElementName);
    var employeeSize = document.getElementById(employeeSizeElementName);
    var publicationDateFrom = document.getElementById(publicationDateFromElementName);
    var publicationDateTo = document.getElementById(publicationDateToElementName);
    var pCCount = document.getElementById(pCCountElementName);
    var industry = document.getElementById(industryElementName);
    var country = document.getElementById(countryElementName);
    var audience = document.getElementById(audienceElementName);
    var competitor = document.getElementById(competitorElementName);
    var language = document.getElementById(languageElementName);
    var media = document.getElementById(mediaElementName);
    var product = document.getElementById(productElementName);

    if (keyword != null)
        keyword.value = "";
    
    if (rating != null)
        rating.selectedIndex = 0;

    if (contentType != null)
        contentType.selectedIndex = 0;

    if (employeeSize != null)
        employeeSize.selectedIndex = 0;

    if (publicationDateFrom != null) 
        publicationDateFrom.value = "Publication Date - From";

    if (publicationDateTo != null) 
        publicationDateTo.value = "Publication Date - To";
    

    if (pCCount != null)
        pCCount.selectedIndex = 0;

    if (industry != null)
        industry.selectedIndex = 0;

    if (country != null)
        country.selectedIndex = 0;

    if (audience != null)
        audience.selectedIndex = 0;

    if (competitor != null)
        competitor.selectedIndex = 0;

    if (language != null)
        language.selectedIndex = 0;

    if (media != null) 
        media.selectedIndex = -1;
   
    if (product != null) {
        for (var i = 0; i < product.length; i++) {
            product.options[i].selected = false;
        }
    }

}


function clearFewerSearchOptions() {


    var keyword = document.getElementById(keywordFewerElementName);

    if (keyword != null)
        keyword.value = "";

}


function CallServer(dropDown, spanContainer, controlArrayIndex)
{   
    currentSpan = spanContainer;    
    currentDropDown = dropDown;           
    currentTaxonomyIndex = controlArrayIndex;
    
    if(dropDown.selectedIndex > 0)
    {
        //var parentID = dropDown.options[dropDown.selectedIndex].value.replace(/,/g, ":");
        var parentID = dropDown.options[dropDown.selectedIndex].value;
        UseCallBack(parentID + ',' + controlArrayIndex);        
    }         
}


function GetHelp()
{
    newResourceFileEditWindow = window.open("AdvHelp.htm",'newResourceFileEditWindow','width=700,height=450,menubar=no,toolbar=no,location=no,status=yes,directories=no,copyhistory=no,resizable=no,scrollbars=yes');
	newResourceFileEditWindow.focus();
}


function GetFromServer(retVal, context)
{
    var ddlspan;
    var controlArrayIndex;
    var returnValues;   
    
    returnValues = retVal.split(',.,');
    controlArrayIndex = returnValues[0];
    
    ddlspan = document.getElementById(currentSpan);           
    ddlspan.innerHTML = returnValues[1];
    
    // add 2nd level item to the side list
    CreateTaxonomySummaryList(currentDropDown, controlArrayIndex, true);      
}




function CreateKeyWordList(object, sideDiv)
{
    var keyword;
    var isEmpty = false;
    
    keyword = object.value;
    sideDiv = document.getElementById(sideDiv);
    
    // validate entry
    // if the keyword is empty
    if(keyword == '' || keyword == null)
    {
        // dont add it
        isEmpty = true;
        sideDiv.innerHTML = '';        
    }
    
    // otherwise add it
    if(!isEmpty)
    {
        htmlString = "";            

        // build up the sidelist item html/css
        htmlString += "<div id='tdivKeyWord";    
        htmlString += "' class='selectedTaxonomy'><div class='selectedTaxonomyName'><img src='images/dot.gif' border='0' />" + " "  + keyword + " ";
        htmlString += "<a class='hyperlink' href='error.aspx' onclick=\"RemoveKeyWordTaxonomy('" + object.id;       
        htmlString += "'); return false;\" />«remove</a></div></div>";           
        
        // add the new item to the side list
        sideDiv.innerHTML = htmlString;
    }
    PopulateSearchCriteriaDropDown();
}



function RemoveKeyWordTaxonomy(textBoxID)
{
    var displayDiv;
    var textBox;
    
    textBox = document.getElementById(textBoxID);
    
    displayDiv = document.getElementById('tdivKeyWord');   
    displayDiv.parentNode.removeChild(displayDiv);
    
    textBox.value = '';
    // update search summary field
    PopulateSearchCriteriaDropDown();  
}




function PopulateSearchCriteriaDropDown()
{
    var controlCount;
    var iPointer;
    var jPointer;
    var currentArray;
    var namesElement;
    var namesArray;
    var summaryElement;
    var newOption;
    var radioElement;
    var textElement;
    var dateElementFrom;
    var dateElementTo;
    
    summaryElement = document.getElementById('drpSearchSummary');   
    
    summaryElement.options.length = 0;
    
    controlCount = taxonomyControls.length;  

    textElement = document.getElementById(keywordTextBox);
        
    // add keyword
    if(textElement.value != null && textElement.value.length != 0)
    {
        newOption = document.createElement("OPTION");
        newOption.text = "Keywords: " + textElement.value;
        summaryElement.options.add(newOption);
    }
    
    // add everything else
    for(iPointer = 0; iPointer < controlCount; iPointer++)
    {
        currentArray = taxonomyControls[iPointer];                    
        namesElement = document.getElementById(currentArray[1]);

        if (namesElement != null && namesElement.value.length > 0) {

            if (currentArray[4] == true) {

                radioElement = document.getElementById(currentArray[3] + '_0');
                newOption = document.createElement("OPTION");
                if (radioElement.checked) {
                    newOption.text = "Any of these";
                }
                else {
                    newOption.text = "All of these";
                }
            }
            else {
                newOption = document.createElement("OPTION");
                newOption.text = "Any of these";
            }
            newOption.style.color = 'gray';
            summaryElement.options.add(newOption);

            namesArray = namesElement.value.split(_delimiter);

            for (jPointer = 0; jPointer < namesArray.length; jPointer++) {
                newOption = document.createElement("OPTION");
                newOption.text = namesArray[jPointer];
                summaryElement.options.add(newOption);
            }
        }        
    }
    // add publication date
    dateElementFrom = document.getElementById(publicationDateFromField);
    if (dateElementFrom.value != null && dateElementFrom.value != 'From')
    {
        newOption = document.createElement("OPTION");
        newOption.value = dateElementFrom.value;
        newOption.text = "Publication date - From: " + dateElementFrom.value;
        summaryElement.options.add(newOption);
    }

    dateElementTo = document.getElementById(publicationDateToField);
    if (dateElementTo.value != null && dateElementTo.value != 'To') {
        newOption = document.createElement("OPTION");
        newOption.value = dateElementTo.value;
        newOption.text = "Publication date - To: " + dateElementTo.value;
        summaryElement.options.add(newOption);
    } 
}

function ClearForm()
{
    var controlCount;
    var iPointer;
    var currentArray;
    var blankDDLString;
    var idsElement;
    var namesElement;
    var logicOption;
    var sideDiv;
    var parentDropDown;
    var childSpan;
    var textElement;
    var dateElementFrom;
    var dateElementTo;
 
    blankDDLString = '<select class="taxonomyListBox" size="6" multiple="multiple"><option></option></select>';
 
    summaryElement = document.getElementById('drpSearchSummary');       
    summaryElement.options.length = 0;

    controlCount = taxonomyControls.length;    

    textElement = document.getElementById(keywordTextBox);
    textElement.value = '';
    sideDiv = document.getElementById('tdivKeyWord');
    if(sideDiv)
    {
        sideDiv.parentNode.removeChild(sideDiv);
    }    
    
    // add everything else
    for(iPointer = 0; iPointer < controlCount; iPointer++)
    {       
        currentArray = taxonomyControls[iPointer];       
        idsElement = document.getElementById(currentArray[0]);
        namesElement = document.getElementById(currentArray[1]);
        if (namesElement != null) {
            sideDiv = document.getElementById(currentArray[2]);
            logicOption = document.getElementById(currentArray[3] + '_0');
            parentDropDown = document.getElementById(currentArray[5]);
            childSpan = document.getElementById(currentArray[6]);
            idsElement.value = '';
            if (namesElement) {
                namesElement.value = '';
            }
            parentDropDown.selectedIndex = 0;
            sideDiv.innerHTML = '&nbsp;';
            if (logicOption) {
                logicOption.checked = true;
            }
            if (childSpan) {
                childSpan.innerHTML = blankDDLString;
            }
        }
    }
    // add publication date

    dateElementFrom = document.getElementById(publicationDateFromField);
    dateElementFrom.value = "From";
    sideDiv = document.getElementById('tdivPubDateFrom');        
    if(sideDiv)
    {        
        sideDiv.parentNode.removeChild(sideDiv);
    }

    dateElementTo = document.getElementById(publicationDateToField);
    dateElementTo.value = "To";
    sideDiv = document.getElementById('tdivPubDateTo');
    if (sideDiv) {
        sideDiv.parentNode.removeChild(sideDiv);
    }    
}



function CreateTaxonomySummaryList(object, controlArrayIndex, hasChildList)
{
    var iterator = 0;
    var isAlreadySelected = false;
    var idElement;
    var secondLevelIDElement;
    var nameElement;                        
    var htmlString;
    var childDropDown;
    var isFirstTaxonomy;    
    var taxonomyID;
    var taxonomyName;
    var sideDiv;     
    var parentDropDown;
    var parentTaxonomyID;
    var isParentSelected;
    var hasSelectedChildren;
    var optionValue;
    var isChild;
    
   
    var parentIDAlreadySelectedBefore;
    
    // if an option in the listbox/drop down is selected
    if(object.selectedIndex > 0 || hasChildList)
    {
        // retrieve the values from the listbox/drop down
        taxonomyID = object[object.selectedIndex].value;          
        taxonomyName = object[object.selectedIndex].text;       
       
        
        idElement = document.getElementById(taxonomyControls[controlArrayIndex][0]);                 
        nameElement = document.getElementById(taxonomyControls[controlArrayIndex][1]);         
        sideDiv = document.getElementById(taxonomyControls[controlArrayIndex][2]);         
        parentDropDown = document.getElementById(taxonomyControls[controlArrayIndex][5]);        
        isChild = object.multiple; // only children are multi-select        
        
        
        if(hasChildList && isChild == false)
        {
            childDropDown = document.getElementById(taxonomyControls[controlArrayIndex][6]).childNodes[0];
        }
        idsArray = idElement.value.split(_delimiter);
        
        
        parentTaxonomyID = parentDropDown[parentDropDown.selectedIndex].value;
        
        
        
        // add this parent id to a list to maintain what parent ids have been selected already
        parentIDTaxonomiesAlreadySelected +=  parentTaxonomyID + _delimiter;            

        // determine if the selected item is already in the side list
        for(iterator = 0; iterator < idsArray.length; iterator++)
        {
            // if the current item id in the array matches the passed in item id
            if(idsArray[iterator] == taxonomyID)
            {
                 // then dont add it to the side list
                 isAlreadySelected = true;                 
            }           
            if(parentTaxonomyID == idsArray[iterator] && object.id != parentDropDown.id)
            {
                isParentSelected = true;         
            }            
        }

        if(childDropDown)
        {
            hasSelectedChildren = false;
            for(iPointer = 0; iPointer< childDropDown.options.length; iPointer++)
            {
                optionValue = childDropDown[iPointer].value;

                for(jPointer = 0; jPointer< idsArray.length; jPointer++)
                {
                    if(optionValue == idsArray[jPointer])
                    {
                        hasSelectedChildren = true;
                    }            
                }
            }
        }
        
        // if flag condition is met
        if(isParentSelected)
        {
            // remove the parent drop down id from the side list
            RemoveTaxonomy(parentTaxonomyID, controlArrayIndex);
            parentIDAlreadySelectedBefore = true;
        }
                                
        // if item has not been added to side list already
        if(!isAlreadySelected && !hasSelectedChildren)
        {            
            
            
            // if any items exist in the side list already then append a ',' to the last item
            if(idElement.value.length > 0)
            {
                idElement.value += _delimiter;
                nameElement.value += _delimiter;
            }
            else
            {
                isFirstTaxonomy = true;
            }
            // append the new item to the side list
            idElement.value += taxonomyID;               
            nameElement.value += taxonomyName; 
            
                       

            htmlString = "";              
            // build up the sidelist item html/css
            htmlString += "<div id='tdiv";
            htmlString += taxonomyID;            
            htmlString += "' class='selectedTaxonomy'><div class='selectedTaxonomyName'><img src='images/dot.gif' border='0' />" + " "  + taxonomyName + " ";
            htmlString += "<a class='hyperlink' href='error.aspx' onclick='RemoveTaxonomy(\"";
            htmlString += taxonomyID + "\"," + controlArrayIndex;
            htmlString += "); return false;' />«remove</a></div></div>";        
            
            // add the new item to the side list
            if(isFirstTaxonomy)
            {
                sideDiv.innerHTML = htmlString;           
            }
            else
            {
                sideDiv.innerHTML += htmlString;           
            }
            
        }      
        // update search summary field
        PopulateSearchCriteriaDropDown();
    }    
}

function PopulateDateField(object, sideValue, isFromDate)
{
    var sideDiv;
    var htmlString;
    var taxonomyID;
    var taxonomyName;
    var isBlank = false;
    var type; 
    
    htmlString = "";

    // retrieve the values from the object
    
    datePicker = object.value;

    if (datePicker == null || datePicker == '')
    {
        isBlank = true;
    }

    sideDiv = document.getElementById(sideValue);

    if (isFromDate)
        type = "From";
    else
        type = "To";    
    
    if(!isBlank) {

        // build up the sidelist item html/css
        htmlString += "<div id='tdivPubDate" + type;
        htmlString += "' class='selectedTaxonomy'><div class='selectedTaxonomyName'><img src='images/dot.gif' border='0' />" + " " + type + ": " + datePicker + " ";
        htmlString += "<a class='hyperlink' href='error.aspx' onclick=\"RemoveDateField('" + object.id + "','" + type + "'); return false;\" />«remove</a></div></div>";
                
        // add the new item to the side list
        sideDiv.innerHTML = htmlString;
    }
    else
    {
       RemoveDateField(object.id, type);
    }
    PopulateSearchCriteriaDropDown();    
}


function RemoveDateField(ddlID, type)
{
    var datePicker;
    var displayDiv;

    datePicker = document.getElementById(ddlID);
    datePicker.value = type;

    displayDiv = document.getElementById('tdivPubDate' + type);       
    displayDiv.parentNode.removeChild(displayDiv);    
    
    // update search summary field
    PopulateSearchCriteriaDropDown();
}




function RemoveTaxonomy(taxID, controlArrayIndex)
{
    var idElement;
    var nameElement;
    var idsArray;
    var namesArray;
    var iterator;
    var displayDiv;           
    
    displayDiv = document.getElementById('tdiv' + taxID);
    displayDiv.parentNode.removeChild(displayDiv);
    
    idElement = document.getElementById(taxonomyControls[controlArrayIndex][0]);            
    nameElement = document.getElementById(taxonomyControls[controlArrayIndex][1]);
       
    idsArray = idElement.value.split(_delimiter);               
    namesArray = nameElement.value.split(_delimiter);
                    
    idElement.value = "";
    nameElement.value = "";
    
    for(iterator = 0; iterator < idsArray.length; iterator++)
    {
        if(idsArray[iterator] != taxID)
        {
            if(idElement.value.length > 0)
            {
                idElement.value += _delimiter;
                nameElement.value += _delimiter;
            }
            idElement.value += idsArray[iterator];
            nameElement.value += namesArray[iterator];
        }               
    }
    // update search summary field
    PopulateSearchCriteriaDropDown(); 
 }