/*
    FILE:       : AdvSrch.js
    DESCRIPTION : Defines the functions used in Advanced Search page
    DATE        : 25 Aug 2008
    AUTHOR      : Ramya R
    CHANGE CONTROL:
*/


var d        = document;
function validate(pObj)
{
  var bFlag=false;
  //On Click of "GO" button
   if (pObj.name == "NormalSearchForm")
	bFlag=submitForm(pObj.ck,1);
  else
	bFlag = submitForm(pObj.chk2,2);
  return bFlag;
 }

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

function DoTheCheck(pBS, obj) {
  if (pBS==0)
  {
	if(obj==1)
	{
	  if (d.NormalSearchForm.check.checked == true)
	  	selectAllAreas(pBS);
	  else
	    unselectAllAreas(pBS);
	}
    else {
	  if(d.NormalSearchForm.uncheck.checked == true)
	    unselectAllAreas(pBS);
    }
  }
  else {
	if(obj==1)
	{
	  if (d.AdvSearchForm1.check.checked == true)
	  	selectAllAreas(pBS);
	  else
	    unselectAllAreas(pBS);
	}
	else {
	  if(d.AdvSearchForm1.uncheck.checked == true)
		unselectAllAreas(pBS);
	}
  }
}
/******************************************************************
*	Function:		selectAllAreas(val pObj)
*	Purpose:		Make all the checkboxes selected
*	Input:			pObj - Name of the checkbox
*	Output:			
*	Return:		    None
******************************************************************/

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

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

  if(pBS==0) {
	  if(document.NormalSearchForm.ck.length > 0){
	for (var j = 0; j < d.NormalSearchForm.ck.length; j++)
    {
		d.NormalSearchForm.ck[j].checked=false;
	}
		if(d.NormalSearchForm.check != null){
			d.NormalSearchForm.check.checked=false;
		}
	  }else{
		d.NormalSearchForm.ck.checked=false;
	  }
   
  }
  else {
	  for (var j = 0; j < d.AdvSearchForm1.chk2.length; j++)
    {
		d.AdvSearchForm1.chk2[j].checked=false;
	}
	if(d.NormalSearchForm.check != null){
    	d.AdvSearchForm1.check.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) 
{
	var checkBoxObj1=d.NormalSearchForm.ck; 
	var checkBoxObj2= d.NormalSearchForm.check;
	var checkBoxObj3= d.NormalSearchForm.uncheck;
	if(pBS == 1) { 
		checkBoxObj1=d.AdvSearchForm1.chk2; 
		checkBoxObj2= d.AdvSearchForm1.check;
		checkBoxObj3= d.AdvSearchForm1.uncheck;
	}
	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;
			}
		}
	}

	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;
}

/******************************************************************
*	Function:		submitForm(val pObj)
*	Purpose:		Checks the value and calls the formVector() 
*					function depending on the type of search			
*	Input:			pObj - value of the "search" button clicked
					
*	Output:			boolean 
******************************************************************/
/* changed the method signature for ENH-16*/
 function submitForm(pObj, pType)
 {

	var strLocation="";
	var strQuery="";
	var bitVector=0;
	var strwpc="";
	var dt = false;
	var a = false;
	var wbKids = "false";
	// Build media types bit vector from selected checkboxes.
	for(k = 0; k < pObj.length; k++)
	{
		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);
				
				if( pObj[k].value == 12){
					wbKids="true";
				}
			}	
	  	}
    }
	
	if(bitVector == 0)
	{
	  if (dt == true)
	  {
		if (pObj[0].name == "chk2")
	      strQuery = d.AdvSearchForm1.st1.value + " " + d.AdvSearchForm1.st2.value; 
		else
		  strQuery = d.NormalSearchForm.wp.value;
		window.location.href="/student/dictionary?lu=" + strQuery;
		return false;
	  }
	  else
	  {
		alert("Please select one or more content areas to search before clicking on Submit");
		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.NormalSearchForm.wpc);
         if (strwpc != "1")
         {
		   strQuery += "&wpc="+ strwpc;
         }
//changes for ENH-27
		
		 
		  if (strwpc == "5")
                    {
		           if(d.NormalSearchForm.year.value != '')
					   
		               {
		     	           if(d.NormalSearchForm.month.value!='Select Month')
		     	            {
							    
		     	    	if(d.NormalSearchForm.day.value!='Select Day')
		                   {
		    				  
		                  d.NormalSearchForm.wp.value=d.NormalSearchForm.month.value+" "+
							d.NormalSearchForm.day.value+", "+d.NormalSearchForm.year.value;
		                    }
		                  else
		                   {
							   
		                  	d.NormalSearchForm.wp.value=d.NormalSearchForm.month.value+" "+d.NormalSearchForm.year.value;
		                   }   
		              }
		                   	
		          else
		             {  	   
		   	          	if(d.NormalSearchForm.day.value!='Select Day')
		                        {
							 
		   		          d.NormalSearchForm.wp.value=d.NormalSearchForm.day.value+","+d.NormalSearchForm.year.value;
		   		        }   
		   		        else
		   		           {
		   		           	   
		   			        d.NormalSearchForm.wp.value=d.NormalSearchForm.year.value;
		   		            }
		   				 
			          }
		        
		        }
		     
		      else
		      	{
		      		
		      	     if(d.NormalSearchForm.month.value!='Select Month')
		     	         {
		     	         	
		     	    	    if(d.NormalSearchForm.day.value!='Select Day')
		                        {
		                        	
		    		     
		                          d.NormalSearchForm.wp.value=d.NormalSearchForm.month.value+" "+d.NormalSearchForm.day.value;
		                        }
		                     else
		                       {
		                 
		                  	d.NormalSearchForm.wp.value=d.NormalSearchForm.month.value;
		                       } 
		                  }
		              else
		        	{
		        		a=true;
		        		var sURL = unescape(window.location.pathname);
                                        window.location.href = sURL;
		        	}
		      	}		   strQuery += "&wp="+ '"'+ escape(d.NormalSearchForm.wp.value) +'"';
		   strQuery += "&wpc="+ strwpc;
		   if (d.NormalSearchForm.year.value.length > 0 && d.NormalSearchForm.year.value.length < 4)
			{
				alert("year should be in YYYY format"); 
				return false;
			}
		 
		 } 
		 
		 
			 
		 
		 else
		 {	
			strQuery += "&wp=" + escape(d.NormalSearchForm.wp.value);
			
		 }
	
	  }
	

      if (bitVector != 65525 ) 
      {
	    strQuery += "&bv=" + bitVector ;
      }
	  if(wbKids == 'true'){
		   strQuery += "&searchprop=WBSK&kids=true" ;
		 
					
	  }

      if(pType == 1 && d.NormalSearchForm.wp.value == '' && a == false)
	  {
			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 'Submit'.");
			return false;
	  }
	 
	  if(a == false)
	  {
	  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;
	}
}


