// Conducts a search on the Google search results page
function Perform_Search	(blnSingleMarketSegmentSearch, blnSearchWithinResults, iFormNum, strMarketSegment, strAtoGovCol, strLegalCol)
{   				
    var frmSearchResults = document.forms[iFormNum];
	var query = frmSearchResults.q.value;
	var as_q_Value = '';

	// for a Searh Within Results search, create a new variable to hold the searched value
	if (blnSearchWithinResults) {
		as_q_Value = document.getElementsByName("as_q")[0].value;	
	}

	// display error message if no site selected
	if ((!blnSingleMarketSegmentSearch) && (!frmSearchResults.chkATOGov.checked) && (!frmSearchResults.chkLegal.checked)) {	
		document.getElementById("divNoSiteError").style.display = '';
		document.getElementById("divNoSearchError").style.display = 'none';
	} // display an error message if no search string entered
	else if (query == '' || (blnSearchWithinResults && as_q_Value == '')) {
		document.getElementById("divNoSearchError").style.display = '';
		document.getElementById("divNoSiteError").style.display = 'none';
	} else {
		var ATO_GOV = strAtoGovCol;
		var ATO_LAW = strLegalCol;
		var site;
		var partialFields = '';
	    
		// individual market segment and atogov radio buttons displayed
		if (blnSingleMarketSegmentSearch) {
	        
			var i;
			var rdbPartialFields = frmSearchResults.partialfields;
			
			// the site parameter should always be atogov when searching in a market segment
			site = ATO_GOV;
			
			// loop through the radio buttons collection
			for (i = 0; i < rdbPartialFields.length; i++) {
				// if atogov is selected, set the required fields parameter to empty string
				// and set the market segment to home
				if (rdbPartialFields[i].checked) {
					if (rdbPartialFields[i].value == ATO_GOV) {
						strMarketSegment = "ATO Home";
					} else {	// otherwise set it to the market segment value
						partialFields = rdbPartialFields[i].value;
					}
				}
			}
		} else {	// the ato.gov.au and legal db checkboxes displayed
			
			var chkSearchATOGovControl = frmSearchResults.chkATOGov;
			var chkSearchLegalDBControl = frmSearchResults.chkLegal;
	                
			if (chkSearchATOGovControl.checked && chkSearchLegalDBControl.checked) {
				site = ATO_GOV + '|' + ATO_LAW;
			} else if (chkSearchLegalDBControl.checked) {
				site = ATO_LAW;
			} else {
				site = ATO_GOV;
			}
		}

		// determine if this is a NAT search
		var index = query.toUpperCase().indexOf("NAT");
		var natNumber = '';
		var requiredFields = '';
		
		if (index > -1){
			var charAfterNat = query.charAt(index + 3);
			if (charAfterNat != ' ' & !isNaN(charAfterNat)) {
				natNumber = query.substr(index + 3);
			}
			else if (charAfterNat == ' ' & !isNaN(query.charAt(index + 4)) & query.charAt(index + 4) != ' '){
				natNumber = query.substr(index + 4);
			}
			if (natNumber != '') {
				requiredFields = 'ato.reference.natnumber:' + natNumber;
				query = natNumber;
				partialFields = '';
				strMarketSegment = "ATO Home";
			}
		}   
	        
		// Determine if it is a QC search
        index = query.toUpperCase().indexOf("QC");
        var qcNumber = '';
        if (index > -1) {
            var charAfterQC = query.charAt(index + 2);
            if (charAfterQC != ' ') //& !isNaN(charAfterQC))
            {
                qcNumber = query.substr(index + 2);
            }
            else if (charAfterQC == ' ' & !isNaN(query.charAt(index + 3)) & query.charAt(index + 3) != ' '){
                qcNumber = query.substr(index + 3);
            }
            if (qcNumber != '') {
            
				//partial fields way of doing it             								                 
                partialFields = 'dc.identifier:' + qcNumber;
                query = qcNumber;
				required='';
				strMarketSegment = "ATO Home";
            }
        }
	    	    
		// create a query string and navigate to the GoogleSearchResults.asp page
		var url = "/GoogleSearchResults.asp?";
		
		// Shehzad - Start of changes for search within these results fix
		var startIndex = 0;
		// End of changes for search within these results fix
		
		// add an extra parameter to the url for a 'search within results' search
		if (blnSearchWithinResults)
		{
			// Shehzad - Start of changes for search within these results fix
			url += "as_q=" + escape(as_q_Value.replace(/ /g, "+")) + "&";
			// End of changes for search within these results fix
		}
		
		// Shehzad Shamsi - Start of changes for Date Function
		// find the index of the inmeta tag appended to the search term
		// and remove it so that it is not duplicated
		// Shehzad - Start of changes for search within these results fix
		startIndex = query.indexOf("inmeta");
		if (startIndex > 0)
		{
			query = query.replace(query.substr(startIndex-1, 61), '');
			// End of changes for search within these results fix
		}
		// End of changes for Date Function
		
		// replace the spaces with '+'
		url += "q=" + escape(query.replace(/ /g, "+"));
		
		// Shehzad Shamsi - Start of change for Date Function
		var strDateRange = '';
		
		// add a 'inmeta' date range to get only the documents which
		// are valid from today onwards
		var DEFAULT_END_DATE = '2029-06-30';
		var objDate = new Date();
		var startDate = '';
		
		// Shehzad - Start of changes for search within these results fix
		var month = objDate.getMonth()+1;
		var day = objDate.getDate();
		
		startDate += objDate.getFullYear() + '-';
		
		if (month < 10)
		{
			month = '0' + month;
		}
		
		startDate += month + '-';

		if (day < 10)
		{
			day = '0' + day;
		}
		startDate += day;
		
		strDateRange = 'inmeta:dc.Date.ValidToF:daterange:' + startDate + '..' + DEFAULT_END_DATE;
		url += ' ' + strDateRange;
		// End of changes for search within these results fix
		// End of changes for Date Function

		// replace '|' character with double encoded version
		url += "&site=" + site;
        url += "&requiredfields=" + requiredFields;
        url += "&partialfields=" + partialFields;
        url += "&ms=" + strMarketSegment;

		window.location = url;
	}
	
}

// Performs a search when the Enter key is pressed
function checkEnterKeyPress(e, iFormNum)
{	
	if(e.keyCode==13){
		document.forms[iFormNum].btnSearch.focus();
	}
}
