/*
    FILE:       : AdvSrch.js
    DESCRIPTION : Defines the functions used in Advanced Search page
                : This is included in Asrch.jsp
    DATE        : 16 Aug 2005
    AUTHOR      : Anjna G
    CHANGE CONTROL:
*/
var d        = document;

/******************************************************************
*	Function:		validate(val pObj)
*	Purpose:		Checks the value and calls the formVector() 
*					function depending on the type of search			
*	Input:			pObj - value of the "go" button clicked
					accessVal - indicating RTE access is present or not
*	Output:			boolean
*	Return:		    TRUE on success, FALSE otherwise
******************************************************************/
/* changed the method signature for ENH-16*/
function validate(pObj,accessVal)
{
  var bFlag=false;
  //On Click of "GO" button
  //Added for the search option bug
  
  if (pObj.name == "AdvSearchForm")
	bFlag=submitForm(pObj.chk1,1,accessVal);
  else
	bFlag = submitForm(pObj.chk2,2,accessVal);
  return bFlag;
 }
// UCF-WB-BUG-237
/******************************************************************
*	Function:		submitForm(val pObj)
*	Purpose:		Checks the value and calls the formVector() 
*					function depending on the type of search			
*	Input:			pObj - value of the "go" button clicked
					accessVal indicating the RTE Access is present or not
					value can "Y" or "N"
*	Output:			boolean 
******************************************************************/
/* changed the method signature for ENH-16*/
 function submitForm(pObj, pType,accessVal)
 {
	
	var strLocation="";
	var strQuery="";
	var bitVector=0;
	var strwpc="";
	var dt = false;
	var a = false;
	// Build media types bit vector from selected checkboxes.
	for(k = 0; k < pObj.length; k++)
	{
	 /* Changed the condition to check the value of k from 3 to 4 after removing the pp media ENH-256*/
	 /* Added the condition to check the RTE access is present depending on which
	  * dictionary checkbox value varies ENH-16
	  */
	 if(accessVal == "Y"){
		if(k == 1)
	  	{
        		if(pObj[k].checked == true)
		  		dt = true;
			else
		  		strQuery += "dt=0";	
	  	}
	  	else
	  	{
        		if(pObj[k].checked == true)
			{
			 	bitVector |= (1 << pObj[k].value);	
			}	
	  	}
	 }else{
	 	/* Changed for ENH-72*/
      		if(k == 6)
	  	{
        		if(pObj[k].checked == true)
		  		dt = true;
			else
		 		strQuery += "dt=0";	
	  	}
	  	else
	 	{
        		if(pObj[k].checked == true)
			{
			 	bitVector |= (1 << pObj[k].value);	
			}	
	  	}
	 }
	 /* Changes for ENH-16 ends here*/
    }
	if(bitVector == 0)
	{
	  if (dt == true)
	  {
		if (pObj[0].name == "chk2")
	      strQuery = d.AdvSearchForm1.st1.value + " " + d.AdvSearchForm1.st2.value; 
		else
		  strQuery = d.AdvSearchForm.wp.value;
		if(accessVal == "Y")
			window.location.href="/researchtools/dict?lu=" + strQuery;
		else
			window.location.href="/advanced/dict?lu=" + strQuery;
		return false;
	  }
	  else
	  {
		alert("Please select one or more content areas to search before clicking on 'Search'.");
		return false;
	  }
	}
	// Put the query string together from the input fields.
	  if (pObj[0].name == "chk2")
	  {
		  var iInitCount	= 0;
		  var iTotalCount	= 0;
		  var iCt			= 0;
		  
		  if (d.AdvSearchForm1.show.value != 'S')
		  {
			iInitCount = 1;
			iTotalCount = 4;
		  }
		  else
		  {
			iInitCount = 1;
			iTotalCount = 7;
		  }
		  for (i=iInitCount; i < iTotalCount; i++)
		  {
			strTerm = d.AdvSearchForm1['st' + i].value;
			if (i == 1)
			  strQuery += "";
			else
			{
				strOper = getRadioValue(d.AdvSearchForm1.elements['op' + i]);
				if (strTerm != "")
					strQuery += "&op" + i + "=" + (strTerm ? strOper : "");	
			}
			// URL-encode the term.
			for (j = strTerm.indexOf(" ", 0); j > -1; j = strTerm.indexOf(" ", j))
			{
				strTerm = strTerm.substring(0, j) + "+" + strTerm.substring(j + 1);
			}
			if (strTerm != "")
				strQuery += "&st" + i + "=" + escape(strTerm);
		 }
	  }
	  else
	  {
		 strwpc = getRadioValue(d.AdvSearchForm.wpc);
         if (strwpc != "1")
         {
		   strQuery += "&wpc="+ strwpc;
         }
		  
		  
		  
		  if (strwpc == "5")
                    {
		           if(d.AdvSearchForm.year.value != '')
		               {
		     	           if(d.AdvSearchForm.month.value!='Select Month')
		     	            {
		     	    	if(d.AdvSearchForm.day.value!='Select Day')
		                   {
		    		   
		                  d.AdvSearchForm.wp.value=d.AdvSearchForm.month.value+" "+
				  d.AdvSearchForm.day.value+", "+d.AdvSearchForm.year.value;
		                    }
		                  else
		                   {
		                  	d.AdvSearchForm.wp.value=d.AdvSearchForm.month.value+" "+d.AdvSearchForm.year.value;
		                   } 
		              }
		                   	
		          else
		             {  	       
		   	          	if(d.AdvSearchForm.day.value!='Select Day')
		                        {
		   		          d.AdvSearchForm.wp.value=d.AdvSearchForm.day.value+","+d.AdvSearchForm.year.value;
		   		        }   
		   		        else
		   		           {
		   		           	
		   			        d.AdvSearchForm.wp.value=d.AdvSearchForm.year.value;
		   		            }
		   	          
			          }
		      
		        }
		     
		      else
		      	{
		      		
		      	     if(d.AdvSearchForm.month.value!='Select Month')
		     	         {
		     	         	
		     	    	    if(d.AdvSearchForm.day.value!='Select Day')
		                        {
		                        	
		    		     
		                          d.AdvSearchForm.wp.value=d.AdvSearchForm.month.value+" "+d.AdvSearchForm.day.value;
		                        }
		                     else
		                       {
		                 
		                  	d.AdvSearchForm.wp.value=d.AdvSearchForm.month.value;
		                       } 
		                  }
		              else
		        	{
		        		a=true;
		        		var sURL = unescape(window.location.pathname);
                                        window.location.href = sURL;
		        	}
		      	}
		   strQuery += "&wp="+ '"'+ escape(d.AdvSearchForm.wp.value) +'"';
		   strQuery += "&wpc="+ strwpc;
		   if (d.AdvSearchForm.year.value.length > 0 && d.AdvSearchForm.year.value.length < 4)
			{
				alert("year should be in YYYY format"); 
				return false;
			}
		 }
		 else
		 {	// Commented for MNT-search term with spl charcter
			strQuery += "&wp=" + escape(d.AdvSearchForm.wp.value);
			//strQuery += "&wp=" + encodeURIComponent(d.AdvSearchForm.wp.value);
		 }
	
	  }
	  //ENH-242 bit value is changed due to addition of advanced media types and removal of BTH
      if (bitVector != 65525) 
      {
	    strQuery += "&bv=" + bitVector;
      }
	  //Changes made for the search option bug, Check for the no search term clause, DR-WB-ENH-098-215
      if(pType == 1 && d.AdvSearchForm.wp.value == '' )
	  {
			alert("Please enter search term to search before clicking on 'Search'.");
			return false;
	  } 
	  if(pType == 2 && d.AdvSearchForm1.st1.value == '' && d.AdvSearchForm1.st2.value == '' )
	  {
 			alert("Please enter search terms to search before clicking on 'Search'.");
			return false;
	  }
	  //Addition ends here
	  // Do the search.
	  // UCF-WB-BUG-237
	  strLocation = "search";
	  strLocation = strLocation + "?" + strQuery;
      window.location.href=strLocation;
	  return false;
}

/******************************************************************
*	Function:		getRadioValue(val pObj)
*	Purpose:		To the get the value of the radio button selected
*	Input:			pObj - Radio Button
*	Output:			RadioButton Value
*	Return:		    Value of Radio Button
******************************************************************/
function getRadioValue(pObj)
{
	//Get the Radio button value
	for(r = 0; r< pObj.length; r++)
	{
		if (pObj[r].checked == true)
			return pObj[r].value;
	}
}
/******************************************************************
*	Function:		selectAllAreas(val pObj)
*	Purpose:		Make all the checkboxes selected
*	Input:			pObj - Name of the checkbox
*	Output:			
*	Return:		    None
******************************************************************/
// UCF-WB-BUG-237
// New Method is added 
function DoTheCheck(pBS, obj) {
  if (pBS==0)
  {
	if(obj==1)
	{
	  if (d.AdvSearchForm.chkmn.checked == true)
	  	selectAllAreas(pBS);
	  else
	    unselectAllAreas(pBS);
	}
    else {
	  if(d.AdvSearchForm.chkunmn.checked == true)
	    unselectAllAreas(pBS);
    }
  }
  else {
	if(obj==1)
	{
	  if (d.AdvSearchForm1.chkmn.checked == true)
	  	selectAllAreas(pBS);
	  else
	    unselectAllAreas(pBS);
	}
	else {
	  if(d.AdvSearchForm1.chkunmn.checked == true)
		unselectAllAreas(pBS);
	}
  }
}

// UCF-WB-BUG-237
// Method changed
function selectAllAreas(pBS) //Changes made for DR-WB-ENH-098-215
{
  // Search through the checkboxes, and check each one.
  if(pBS==0) {
	for (var i = 0; i < d.AdvSearchForm.chk1.length; i++)
    {
	  d.AdvSearchForm.chk1[i].checked=true;
	  
    }
    d.AdvSearchForm.chkunmn.checked=false;
  }
  else {
    for (var i = 0; i < d.AdvSearchForm1.chk2.length; i++)
    {
	  d.AdvSearchForm1.chk2[i].checked=true;
    }
    d.AdvSearchForm1.chkunmn.checked=false;
  }

}
/******************************************************************
*	Function:		unselectAllAreas(val pObj)
*	Purpose:		Make all the checkboxes unselected
*	Input:			pObj - Name of the checkbox
*	Output:			
*	Return:		    None
******************************************************************/

// UCF-WB-BUG-237
// Method changed
function unselectAllAreas(pBS) //Changes made for DR-WB-ENH-098-215
{
  // Search through the checkboxes, and check each one.
  if(pBS==0) {
	for (var j = 0; j < d.AdvSearchForm.chk1.length; j++)
    {
		d.AdvSearchForm.chk1[j].checked=false;
	}
    d.AdvSearchForm.chkmn.checked=false;
  }
  else {
	  for (var j = 0; j < d.AdvSearchForm1.chk2.length; j++)
    {
		d.AdvSearchForm1.chk2[j].checked=false;
	}
    d.AdvSearchForm1.chkmn.checked=false;
  }
  
}

/******************************************************************
*	Function:		chkSrchAllContents(val pObj)
*	Purpose:		Uncheck "Search All Contents" if any of the 
*					checkboxes(Text, Sound and Pictures) is unchecked.
*	Input:			pObj - value of the "chk" checkbox 
*	Output:			
*	Return:		    None
******************************************************************/
function chkSrchAllContents(pObj, pBS) //Changes made for DR-WB-ENH-098-215
{
	var checkBoxObj1=d.AdvSearchForm.chk1; //Changes made for DR-WB-ENH-098-215
	var checkBoxObj2= d.AdvSearchForm.chkmn;
	var checkBoxObj3= d.AdvSearchForm.chkunmn;
	if(pBS == 1) { 
		checkBoxObj1=d.AdvSearchForm1.chk2; 
		checkBoxObj2= d.AdvSearchForm1.chkmn;
		checkBoxObj3= d.AdvSearchForm1.chkunmn;
	}
	for (i = 0; i<checkBoxObj1.length; i++)
	{
		if (checkBoxObj1[i].checked == false)
		{
			if (checkBoxObj2.checked == true) {
				checkBoxObj2.checked = false;
			}
		}
		if (checkBoxObj1[i].checked == true)
		{
			if (checkBoxObj3.checked == true) {
				checkBoxObj3.checked = false;
			}
		}
	}

	// UCF-WB-BUG-237
	// Conditions added.
	var bFlag=false;
	for (i = 0; i<checkBoxObj1.length; i++)
	{
		if (checkBoxObj1[i].checked == false)
			continue;
		else {
			bFlag=true;
			break;
		}
	}
	if (!bFlag)
	{
		checkBoxObj3.checked = true;
	}
	var aFlag=false;
	for (i = 0; i<checkBoxObj1.length; i++)
	{
		if (checkBoxObj1[i].checked == true)
			continue;
		else {
			aFlag=true;
			break;
		}
	}
	if (!aFlag)
	{
		checkBoxObj2.checked = true;
	}
}

/******************************************************************
*	Function:		showMoreTerms
*	Purpose:		To show "More" words table 
*	Input:			
*	Output:			
*	Return:		    none
******************************************************************/
function showMoreTerms()
{
	
	var strLocation = "";
	var strQuery    = "";
	for (i=1; i < 4; i++) {
	  strTerm = d.AdvSearchForm1['st' + i].value;
	  if (i == 1)
		strQuery += "&op1=";
	  else {
		strOper = getRadioValue(d.AdvSearchForm1.elements['op' + i]);
		if (strTerm != "")
			strQuery += "&op" + i + "=" + (strTerm ? strOper : "");	
      }
	  // URL-encode the term.
	  for (j = strTerm.indexOf(" ", 0); j > -1; j = strTerm.indexOf(" ", j)) {
		strTerm = strTerm.substring(0, j) + "+" + strTerm.substring(j + 1);
      }
	  if (strTerm != "")
		strQuery += "&st" + i + "=" + escape(strTerm);
	}
    strQuery += "&show=S";
//	if (edition != "na")
	edition = "na";
	  strQuery += "&ed=" + edition;
	strLocation = "advsearch" + "?" + strQuery+"#boolean";
	// UCF-WB-BUG-237
	window.location.href=strLocation;
}
// UCF-WB-BUG-237
// Method is not required.
/******************************************************************
*	Function:		checkBrowser()
*	Purpose:		Checks the browser type
*	Input:			None
*	Output:			boolean
*	Return:		    TRUE if Netscape3 and above, FALSE if IE4 and above
******************************************************************/
//var sBrowserName, sBrowserVersion;
//function checkBrowser(){
//        sBrowserName = navigator.appName;
//        sBrowserVersion = parseInt(navigator.appVersion);
//        if (sBrowserName == "Netscape" && sBrowserVersion >= 3){
//                return true;
//        }
//        else if(sBrowserName == "Microsoft Internet Explorer" && sBrowserVersion >=4){
//                return false;
//        }
//}

/******************************************************************
*	Function:		myResearchPs
*	Purpose:		To form pipe separated id list when 
				more than one id is selected to saved
				to MyResearch.
*	Input:			
*	Output:			
*	Return:		    	none
* Added for ENH-242 for saving document in MyResearch
******************************************************************/

function myResearchPs(strId,strCollId)
{
	var bFlag = false;
	var strUrl = "research?id=";
	/* Added for ENH-72*/
	var id = strId;
	var collId = strCollId;
	/* Added the condition for BUG-262 if there is only one record in search result*/
	if(document.searchresult1.ck1.length > 0){
		for(i=0; i<document.searchresult1.ck1.length; i++ )
		{
			if(document.searchresult1.ck1[i].checked==true)
			{
				bFlag = true;
				strUrl = strUrl + document.searchresult1.ck1[i].value + "|";
			}
		}
	}else{
		if(document.searchresult1.ck1.checked==true)
		{
			bFlag = true;
			strUrl = strUrl + document.searchresult1.ck1.value ;
		}
	}
	if(strUrl.lastIndexOf("|")==(strUrl.length-1))
	{
		strUrl=strUrl.substring(0, strUrl.length-1);
	}
	if(bFlag==false)
	{
		var element = document.getElementById("Save-Document-to-Research");
		element.innerHTML = 'Please select atleast one document to add to research';
		var errorelememt = document.getElementById("error-classes");
		if(errorelememt != null){
			errorelememt.innerHTML = '';
		}
	}
	else
	{
		//window.location.href=strUrl;
		/* Added for ENH-72*/
		if(collId != null){
				document.searchresult1.action=strUrl+"&cid="+strCollId;	
		}else{
				document.searchresult1.action=strUrl;
		}
		//document.searchresult1.action=strUrl;	
		document.searchresult1.submit();
	}
	
}

/******************************************************************
*	Function:		myResearchAr
*	Purpose:		To form pipe separated id list when 
				more than one id is selected to saved
				to MyResearch.
*	Input:			
*	Output:			
*	Return:		    	none
Added for ENH-242 for saving article in Myresearch
******************************************************************/

function myResearchAr(strId,strCollId)
{
	var bFlag = false;
	var strUrl = "research?id=";
	var id = strId;
	var collId = strCollId;
	if(id=='null'){
		if(document.searchresult.ck.length > 0){
			
			for(i=0; i<document.searchresult.ck.length; i++ )
			{
				
				if(document.searchresult.ck[i].checked==true)
				{
					bFlag = true;
					strUrl = strUrl + document.searchresult.ck[i].value + "|";
					
				}
			}
		}else{
				if(document.searchresult.ck.checked==true)
				{
					bFlag = true;
					strUrl = strUrl + document.searchresult.ck.value ;
				}

		}
		
		if(strUrl.lastIndexOf("|")==(strUrl.length-1))
		{
			strUrl=strUrl.substring(0, strUrl.length-1);
		}
	
		if(bFlag==false)
		{
			var element = document.getElementById("Save-Article-to-Research");
			element.innerHTML = '<font color="red"><b>Please select atleast one article to add to My Research</b></font>';
			var errorelememt = document.getElementById("error-classes");
			if(errorelememt != null){
				errorelememt.innerHTML = '';
			}
		}
		else
		{	
			if(collId != null){
				document.searchresult.action=strUrl+"&cid="+strCollId;	
			}else{
				document.searchresult.action=strUrl;
			}
			document.searchresult.submit();
		}
		
	}else{
		document.ResearchFormBean.action=strUrl+id+"&cid="+strCollId;
		document.ResearchFormBean.submit();
	}
}

/******************************************************************
*	Function:		myResearchEb
*	Purpose:		To form pipe separated id list when 
				more than one id is selected to saved
				to MyResearch.
*	Input:			
*	Output:			
*	Return:		    	none
Added for ENH-249 for saving E-Books in Myresearch
******************************************************************/
function myResearchEb(strId,strCollId)
{
	var bFlag = false;
	var strUrl = "research?id=";
	var id = strId;
	var collId = strCollId;
	/* Added BUG-262 the condition if there is only one record in search result*/
	if(document.searchresult2.ck1.length > 0){
		for(i=0; i<document.searchresult2.ck1.length; i++ )
		{
			if(document.searchresult2.ck1[i].checked==true)
			{
				bFlag = true;
				strUrl = strUrl + document.searchresult2.ck1[i].value + "|";
			}
		}
	}else{
		if(document.searchresult2.ck.checked==true)
		{
			bFlag = true;
			strUrl = strUrl + document.searchresult2.ck1.value ;
		}
	}
	/** Changes ends here*/
	if(strUrl.lastIndexOf("|")==(strUrl.length-1))
	{
		strUrl=strUrl.substring(0, strUrl.length-1);
	}
	if(bFlag==false)
	{
		var element = document.getElementById("Save-Document-to-Research");
		element.innerHTML = 'Please select atleast one E-Book to add to research';
		var errorelememt = document.getElementById("error-classes");
		if(errorelememt != null){
			errorelememt.innerHTML = '';
		}
	}
	else
	{
		/* Added for ENH-72*/
		if(collId != null){
			document.searchresult2.action=strUrl+"&cid="+strCollId;	
		}else{
			document.searchresult2.action=strUrl;
		}
		//window.location.href=strUrl;
		/* Commented for ENH-72*/
		//document.searchresult2.action=strUrl;	
		document.searchresult2.submit();
	}
	
}
/** Added the following two methods for WBA Phase III **/

/******************************************************************
*	Function	:	selectAll(val pObj)
*	Purpose		:	Make all the checkboxes selected
*	Input		:	pObj - Name of the checkbox
*	Output		:	
*	Return		:	None
******************************************************************/

function selectAll(pBS) 
{
	
  // Search through the checkboxes, and check each one.
  if(pBS==0) {
	  if(document.searchresult.ck.length > 0){
		for (var i = 0; i < d.searchresult.ck.length; i++)
		{
		  d.searchresult.ck[i].checked=true;
		}
		if(d.searchresult.uncheck != null){
			d.searchresult.uncheck.checked=false;
		}
	  }
	 else{ 
		d.searchresult.ck.checked=true; 
	  } 
	}
	if(pBS==1) {
	if(document.searchresult1.ck1.length > 0){
		for (var i = 0; i < d.searchresult1.ck1.length; i++)
		{
		  d.searchresult1.ck1[i].checked=true;
		}
		if(d.searchresult1.uncheck != null){
			d.searchresult1.uncheck.checked=false;
		}
	  }
	 else{ 
		d.searchresult1.ck1.checked=true; 
	  }   
  }
  if(pBS==2) {
	if(document.searchresult2.ck1.length > 0){
		for (var i = 0; i < d.searchresult2.ck1.length; i++)
		{
		  d.searchresult2.ck1[i].checked=true;
		}
		if(d.searchresult2.uncheck != null){
			d.searchresult2.uncheck.checked=false;
		}
	  }
	 else{ 
		d.searchresult2.ck1.checked=true; 
	  }   
  }
  
}
/******************************************************************
*	Function	:	unselectAll(val pObj)
*	Purpose		:	Make all the checkboxes unselected
*	Input		:	pObj - Name of the checkbox
*	Output		:			
*	Return		:	None
******************************************************************/

function unselectAll(pBS) {
  // Search through the checkboxes, and uncheck each one.

  if(pBS==0) {
	  if(document.searchresult.ck.length > 0){
	for (var j = 0; j < d.searchresult.ck.length; j++)
    {
		d.searchresult.ck[j].checked=false;
	}
		if(d.searchresult.check != null){
			d.searchresult.check.checked=false;
		}
	  }else{
		d.searchresult.ck.checked=false;
	  }
   
  }
 if(pBS==1) {
	  if(document.searchresult1.ck1.length > 0){
	for (var j = 0; j < d.searchresult1.ck1.length; j++)
    {
		d.searchresult1.ck1[j].checked=false;
	}
		if(d.searchresult1.check != null){
			d.searchresult1.check.checked=false;
		}
	  }else{
		d.searchresult1.ck1.checked=false;
	  }
   
  }
  if(pBS==2) {
	  if(document.searchresult2.ck1.length > 0){
	for (var j = 0; j < d.searchresult2.ck1.length; j++)
    {
		d.searchresult2.ck1[j].checked=false;
	}
		if(d.searchresult2.check != null){
			d.searchresult2.check.checked=false;
		}
	  }else{
		d.searchresult2.ck1.checked=false;
	  }
   
  }
}

/** Changes for WBA Phase III ends here**/