//Written by Chris Williams

String.prototype.replaceAll=function(s1, s2) {return this.split(s1).join(s2)}

function searchTerms(){
if (getURLParam('PID') != ''){											//if PID exists
	var TermsExist = SetPartnerID('');									//set PID to variable
	if(GetSearchTerms()){												//if natural search also exists
		var TermsExist2 = GetSearchTerms();                             //set natural search to variable
		TermsExist2 = TermsExist2.replaceAll(", ", "_");                 //replace ", " with "_" to match the format of our term
		
		var dashLoc = TermsExist.indexOf(':');                          // remove search engine from PID to compare
		var sEngine;                                                    //   raw search terms
		var sTerm;
		var len = TermsExist.length;
		if (dashLoc > 0){
			sEngine = TermsExist.substr(0, dashLoc);
			sTerm= TermsExist.substr(dashLoc + 1, len);
		}
		
		var dashLoc2 = TermsExist2.indexOf(':');                          // remove search engine from natural search to compare
		var sEngine2;                                                    //   raw search terms
		var sTerm2;
		var len2 = TermsExist2.length;
		if (dashLoc2 > 0){
			sEngine2 = TermsExist2.substr(0, dashLoc2);
			sTerm2= TermsExist2.substr(dashLoc2 + 1, len2);
		}
		
			var fullTermString = TermsExist + ":" + sTerm2;
			document.writeln("<input name='searchID' id='searchID' type='hidden'  value=\"" + fullTermString + "\">")
			document.writeln("<input name='naturalSearchID' id='naturalSearchID' type='hidden'  value=\"" + TermsExist2 + "\">")

	}
	else {                                                             //natural search doesn't exist for some reason
		if (TermsExist != false)                                       
		//check PID term and store for database w/ no natural search entry
    	{
			TermsExist = TermsExist + ":";
			document.writeln("<input name='searchID' id='searchID' type='hidden'  value=\"" + TermsExist + "\">")
			//document.writeln("<input name='naturalSearchID' id='naturalSearchID' type='hidden'  value='null'>")
     	}
	}
}

else {                                                                  //no PID exists
	if(GetSearchTerms()){
		var TermsExist2 = GetSearchTerms();                                 //set natural search to variable
		TermsExist2 = TermsExist2.replaceAll(", ", "_");
	
		var dashLoc2 = TermsExist2.indexOf(':');                          // remove search engine from natural search to compare
		var sEngine2;                                                    //   raw search terms
		var sTerm2;
		var len2 = TermsExist2.length;
		if (dashLoc2 > 0){
			sEngine2 = TermsExist2.substr(0, dashLoc2);
			sTerm2= TermsExist2.substr(dashLoc2 + 1, len2);
		}
		
		if (TermsExist2 != false)                                       
		//check natural search terms and store for database w/ no PID entry
    	{
			var emptySearchID = "::";
			var fullTermString = emptySearchID + sTerm2;
			document.writeln("<input name='searchID' id='searchID' type='hidden'  value=\"" + fullTermString + "\">")
			document.writeln("<input name='naturalSearchID' id='naturalSearchID' type='hidden'  value=\"" + TermsExist2 + "\">")
   	 	}
	}
	else{
		//no PID or natural search
			document.writeln("<input name='searchID' id='searchID' type='hidden'  value='::'>")
	}
}
}