// 
// Copyright (c) 2001, 2002, 2003 Gartner Inc.  All rights reserved.
//
// Filename:   Results.js
//
// Location:   public_html/7_search/js
//
// Editing: Tab stops set to 4.
//
//  10-08-2002 - Added code to change value of op field in
//               Options frame on submit Search to SimpleSearch,
//               no matter what search operation, simple or advanced,
//               is actually to be submitted.  This is done so that
//               visually user would be able to connect clicking
//               on searchnow in Results frame with the options
//               image.  Also, added same code for browse links in b()
//               which sets op field to corresponding browse operation.
//               For PCR WCW7789.  --Marianna Vinitsky
//  10-17-2002 - Changed archived default to not include archived content.
//               For PCR WCW7898.  --Marianna Vinitsky
//  11-12-2002 - New nescape compatible locking mechanism for search within
//               checkbox.  For PCR WCW8030 and WCW8038.  --Marianna Vinitsky
//  01-04-2003 - Created new function bn (=browse node) to handle author and
//               date views. For PCRs WCW8209 & WCW8279.  --Marianna Vinitsky
//  02-13-2003 - Added support for SEARCH_BY_TOPIC and SEARCH_BY_AUTHOR
//               search operations.  For PCR WCW8368.  --Marianna Vinitsky
//  03-04-2003 - Removed mainWindow variable (was not working in NS 4.75) and
//               replaced function arguments using it with call to 
//               nameForWindow('homepage').  For PCR WCW8127. 
//               --Marianna Vinitsky
//  03-09-2003 - Rewrote b() and bn() to remove frame flicker and resizing.
//               For PCR WCW8597.  --Marianna Vinitsky
//  06-04-2003 - Removed local nameForWindow() function.  It already exists in utility.js.
//                  --R. Pigora
//  07-03-2003 - Modified submitSearch() to return false if the form is submitted.
//               This is part of the fix for PCR#WCW9369.
//                  --R. Pigora
//  07-17-2003 - Renamed JavaScript functions openHelp and openBio to 
//               openSearchHelp and openAuthorBio, repectively, to avoid
//               conflicts with other JavaScript functions of the same name.
//               This fixes PCR #WCW9570 and #WCW9502.
//                  - R. Pigora
//  10-14-2003 - Added support for the Browse By Dataquest Document Clusters.
//               -- R. Pigora
//  12-06-2006 - PCR WCW12881 - Added support for the Browse By Dataquest Quarterly Statistics Clusters.
//                 -- Shrileckha Chaithanya    

var      cAgent = navigator.userAgent.toLowerCase();

var      cIsNetscape = ((cAgent.indexOf('mozilla') != -1)
         && (cAgent.indexOf('spoofer') == -1)
         && (cAgent.indexOf('compatible') == -1)
         && (cAgent.indexOf('opera') == -1)
         && (cAgent.indexOf('webtv') == -1)
         && (cAgent.indexOf('hotjava') == -1));

function openArchiveTips(href) {
   var newname = nameForWindow('archiveTips');
   newWin = window.open(href,newname,
         'width=350,height=280,resizable=no,scrollbars=no,toolbar=no,menubar=no');
   newWin.focus();
}

function openTips(href, windowName) {
    	var newname = nameForWindow(windowName);
    	newWin = window.open(href, newname,
    			'width=350,height=280,resizable=no,scrollbars=no,toolbar=no,menubar=no');
    	newWin.focus();
    	return false;
    }
    
function openPreferences(href, windowName) {
	var newname = nameForWindow(windowName);
	newWin = window.open(href, newname,
			'width=350,height=335,resizable=no,scrollbars=no,toolbar=no,menubar=no');
	newWin.focus();
	return false;
}


// this function will open browse frame unless it is already present,
// it is to be used only for document topics as it assumes view with id 0.
function bn(node, nFrames) {

    top.Options.document['optionsImage'].src = '/7_search/images/browsing_hdr.gif';

    if (top.Content.Body && top.Content.Body.document.resultsForm) {
        rForm = document.resultsForm;
        simple1 = (rForm.allContentTop[0].checked)?0:1;
        archived = (rForm.archivedTop.checked)?1:0;
    } else if (top.Content.Left && top.Content.Left.document.advancedForm) {
        aForm = top.Content.Left.document.advancedForm;
        simple1 = (aForm.simple1[0].checked)?0:1;
        archived = (aForm.archived.checked)?1:0;
    } else {
        //go for all content, archived not included
        simple1 = 0;
        archived = 0;
    }

    top.Options.document.optionsForm.op.value = 15;
            
    if (top.Content.Left) {
        top.Content.Body.location="/Search?op=15&bop=1&v=0"
            + "&node=" + node
            + "&n=" + node
            + "&simple1=" + simple1
            + "&archived=" + archived
            + "&resultsPerSearch=0"; 

        top.Content.Left.location="/Browse?bop=4&f=3&n=" 
            + node 
            + "&s=0" 
            + "&d=0"
            + "&m=0" 
            + "&v=0";
    } else {
        top.Content.location="/7_search/Body2Frame.jsp?bc=1&op=15&bop=4&v=0" 
         + "&node=" + node
            + "&n=" + node
            + "&simple1=" + simple1
            + "&archived=" + archived
            + "&resultsPerSearch=0" 
    }
}


// this function won't open browse frame but will stay in 2 frames if this is
// the original state
function b(node, view) {
    top.Options.document['optionsImage'].src = '/7_search/images/browsing_hdr.gif';

    if (top.Content.Body && top.Content.Body.document.resultsForm) {
        rForm = document.resultsForm;
        simple1 = (rForm.allContentTop[0].checked)?0:1;
        archived = (rForm.archivedTop.checked)?1:0;
    } else if (top.Content.Left && top.Content.Left.document.advancedForm) {
        aForm = top.Content.Left.document.advancedForm;
        simple1 = (aForm.simple1[0].checked)?0:1;
        archived = (aForm.archived.checked)?1:0;
    } else {
        //go for all content, archived not included
        simple1 = 0;
        archived = 0;
    }

    if (view == 1) {
        top.Content.Body.location="/Search?op=15&bop=1&v=0"
            + "&node=" + node
            + "&n=" + node
            + "&simple1=" + simple1
            + "&archived=" + archived
            + "&resultsPerSearch=0"; 

        top.Options.document.optionsForm.op.value = 15;
    } else if ((view == 6)||(view == 7)) {
            top.Content.Body.location="/Search?op=18&bop=1"
            + "&node=" + node
            + "&n=" + node
            + "&v=" + view 
            + "&simple1=" + simple1
            + "&archived=" + archived
            + "&resultsPerSearch=0"; 

        top.Options.document.optionsForm.op.value = 18;
    } else if (view == 0) {
        top.Content.Body.location="/Search?op=16&bop=1"
            + "&node=" + node
            + "&authorId=" + node 
            + "&n=" + node 
            + "&v=" + view 
            + "&resultsPerSearch=0" 
            + "&simple1=" + simple1
            + "&archived=" + archived;

        top.Options.document.optionsForm.op.value = 16;
    } else {
        snode = node + '';
        month1 = snode.substring(4, 6);
        year1 = snode.substring(0, 4);
        top.Content.Body.location="/Search?op=17&bop=1"
            + "&node=" + node
            + "&month1=" + month1 
            + "&year1=" + year1 
            + "&month2=" + month1 
            + "&year2=" + year1 
            + "&resultsPerSearch=0" 
            + "&v=" + view 
            + "&n=" + node 
            + "&simple1=" + simple1
            + "&archived=" + archived;

        top.Options.document.optionsForm.op.value = 17;
    }

    if (top.Content.Left) {
        //these 2 view ids got switched somewhere
        if (view == 1) {
            view = 0;
        } else if (view == 0) {
            view = 1;
        }
            
        top.Content.Left.location="/Browse?bop=4&f=3&n=" 
            + node 
            + "&s=0" 
            + "&d=0"
            + "&m=0" 
            + "&v="
            + view;
    }
}

function openAuthorBio(url){
    window.open(url, "_blank", "width=565,height=450,"
    + "scrollbars=yes,resizable=no");
}


function openDoc(url,target) {
    if (target == "_blank") {
        window.open(url, "_blank", "height=569,width=798,"
        + "scrollbars=yes,resizable=yes,menubar=yes,status=yes");
    } else {
        var newname = nameForWindow(target);

        newWin = window.open(url, newname);
        newWin.focus();
    }
}


function openCreateAlert(thing) {
    w = window.open("/Search?op=" + thing, nameForWindow('homepage'));
    w.focus();
}


function openSearchHelp() {
   w = window.open('/6_help/site_help/site_functionality.html#4','_blank',
      'height=520,width=632,scrollbars=yes');
   w.focus();
}


function openManageAlerts(alertop) {
   w = window.open('/Alert?alertop=' + alertop, nameForWindow('homepage'));
   w.focus();
}


function printView(thing) {
   window.open("/Search?op=" + thing, '_blank',
      'width=750,height=550,scrollbars=yes,resizable=yes,menubar=yes');
}

function setFocus() {
   document.resultsForm.keywordsTop.focus();
}


function searchWithinClear(location) {
    child = eval('document.resultsForm.searchWithin' + location);
   eval("child.checked=false");
}


function searchWithinCheck(location) {
    child = eval('document.resultsForm.searchWithin' + location);
   return child.value==0?true:false;
}


function searchWithinLock(location) {
    child = eval('document.resultsForm.searchWithin' + location);
   child.disabled=true;
   child.value="L";
}


function searchWithinUnlock(location) {
    child = eval('document.resultsForm.searchWithin' + location);
   child.disabled=false;
   child.value="U";
}


function controlRadioTwin(loc1, loc2, loc3) {
    childSrc = eval('document.resultsForm.' + loc1);
    childDest = eval('document.resultsForm.' + loc2);
    eval(childDest[0].checked = childSrc[0].checked);
    eval(childDest[1].checked = childSrc[1].checked);

    if (top.Content.Left) {
        if (top.Content.Left.document.advancedForm) {
            childLeft = eval('top.Content.Left.document.advancedForm.' + loc3);
            eval(childLeft[0].checked = childSrc[0].checked);
            eval(childLeft[1].checked = childSrc[1].checked);
        }
    }
}

function controlCheckboxTwin2(loc1, loc2, loc3, emptyBox)
{
	text1 = eval('document.resultsForm.keywordsTop');
    	text2 = eval('document.resultsForm.keywordsBottom');
	childSrc = eval('document.resultsForm.' + loc1);
	if (childSrc.checked) {
		text1.value = "";
		text2.value = "";
	}
	controlCheckboxTwin(loc1, loc2, loc3);
}

function controlCheckboxTwin(loc1, loc2, loc3) {
	
    childSrc = eval('document.resultsForm.' + loc1);
    childDest = eval('document.resultsForm.' + loc2);

    if (childSrc.value == 'L') {
        childSrc.checked = false;
        childDest.checked = false;
        childDest.value = 'L';
    } else {
        childDest.checked = childSrc.checked;
        childDest.value = 'U';
    }

    if (top.Content.Left) {
        if (top.Content.Left.document.advancedForm) {
            childLeft = eval('top.Content.Left.document.advancedForm.' + loc3);
            if (childSrc.value == 'L') {
                childLeft.disabled = true;
                childLeft.checked = false;
                childLeft.value = 'L';
            } else {
                childLeft.checked = childSrc.checked;
                childLeft.value = 'U';
            }
        }
    }
}


function isValidKeyword(location) {
    child = eval('document.resultsForm.keywords' + location);
    keywords = child.value;

    if (keywords.match(/[A-Z]+/g) ||
        keywords.match(/[a-z]+/g) ||
        keywords.match(/[0-9]+/g)) {
        return true;
    }

    return false;
}


function isEmptyKeyword(location) {
    child = eval('document.resultsForm.keywords' + location);
    keywords = child.value;

    if (keywords.match(/^ *$/)) {
        return true;
    }

    return false;
}


function isAdvancedSearch(location) {
//   rForm = document.resultsForm;

 //   child = eval('rForm.archived' + location);

  //  if (child.checked == true) {
  //    return true;
  // }

   return false;
}
function changeKeyword(keyword, op)
{	
	rForm = document.resultsForm;
	child = eval('rForm.allContentTop');
	var simple1 = eval(child[0].checked) ? 0 : 1;
	
	child = eval('rForm.searchWithinTop');
    	var simple2 = child.checked?1:0;

    	child = eval('rForm.archivedTop');
    	var archived = child.checked?1:0;
	var link = "/Search?op=" + op + "&keywords=" + keyword + "&simple1=" + simple1 + "&simple2=" + simple2 + "&archived=" + archived;	
	window.location=link;
	
            
	
            

}
function changePage(operation, pageNumber) {
	rForm = document.resultsForm;
	top.Options.document['optionsImage'].src
                            = '/7_search/images/searching_hdr.gif';
        top.Content.Body.location.href='/Search?op=' + operation + '&page=' + pageNumber;	
}

function submitSearch(location, otherLocation) {

    rForm = document.resultsForm;
    opSimple = rForm.opSimple.value;
    opAdvanced = rForm.opAdvanced.value;

    //copy keywords over
    child = eval('rForm.keywords' + location);
    child2 = eval('rForm.keywords' + otherLocation);
    child2.value = child.value;

    if (top.Content.Left && top.Content.Left.document.advancedForm) {
    	
        child = eval('rForm.keywords' + location);
        top.Content.Left.document.advancedForm.keywords.value = child.value;
        top.Content.Left.submitSearch(opSimple, opAdvanced);
    } else {
        if (isValidKeyword(location)) {
            child = eval('rForm.allContent' + location);
            rForm.simple1.value = eval(child[0].checked) ? 0 : 1;

            child = eval('rForm.searchWithin' + location);
            rForm.simple2.value = child.checked?1:0;

            child = eval('rForm.archived' + location);
            rForm.archived.value = child.checked?1:0;

            child = eval('rForm.keywords' + location);
            rForm.keywords.value = child.value;

            if (isAdvancedSearch(location)) {
            	rForm.op.value = opAdvanced;
            } else {
            	rForm.op.value = opSimple;
            }

            if (top.Content.Left) {
                if (top.Content.Left.document.advancedForm) {
                    top.Content.Left.document.advancedForm.keywords.value =
                    rForm.keywordsTop.value;
                }
            }

            top.Options.document['optionsImage'].src
                            = '/7_search/images/searching_hdr.gif';
            top.Options.document.optionsForm.op.value = opSimple;
            rForm.submit();
            return false;
        } else if (isEmptyKeyword(location)) {
            alert("Please provide keywords for your search");
        } else {
            alert("Your search is too general.  Please provide keywords for your search.");
        }
    }
}



