//---------------------------------------------------------------------------
// Function used by misc_searchbox.jsp to control form elements
//---------------------------------------------------------------------------
function Search2() {
	if((document.searchForm2.searchterms2.value == 'Description or Item #')||(document.searchForm2.searchterms2.value == '')){
			alert('Please enter search terms');
			return;
			}
			
	//check for item number or catalog search
	// Get search property and term values from form elements
		var terms 	= escape(document.searchForm2.searchterms2.value);
		var posnot	= terms.indexOf("NOT");
		var posand	= terms.indexOf("AND");
		var posor	= terms.indexOf("OR");
		var poswild	= terms.indexOf("*");
		var searchActionCode = "A02";
		//var property 	= document.search_prop_form.searchprop[document.search_prop_form.searchprop.selectedIndex].value;
		var property 	= "All";

		var searchTerms = GetValue("CURRENTURL","Ntt");
		var node = GetValue("CURRENTURL","N");
		var withinResults = 0;
		
		// check for IW number
		var upperTerms = terms.toUpperCase();
		var where_is_IW = upperTerms.indexOf("IW");
		var where_is_R = upperTerms.indexOf("R");
		var where_is_ART = upperTerms.indexOf("ART");
		
		// check for the new ows pad IW numbers
		var arrseekOwsPadNumbers = new Array(24);
		arrseekOwsPadNumbers[0]='IW0170002';
		arrseekOwsPadNumbers[1]='IW0170003';
		arrseekOwsPadNumbers[2]='IW0170004';
		arrseekOwsPadNumbers[3]='IW0170005';
		arrseekOwsPadNumbers[4]='IW0170006';
		arrseekOwsPadNumbers[5]='IW0170007';
		arrseekOwsPadNumbers[6]='IW0170008';
		arrseekOwsPadNumbers[7]='IW0170009';
		arrseekOwsPadNumbers[8]='IW0170010';
		arrseekOwsPadNumbers[9]='IW0170011';
		arrseekOwsPadNumbers[10]='IW0170012';
		arrseekOwsPadNumbers[11]='IW0170013';
		arrseekOwsPadNumbers[12]='IW0170038';
		arrseekOwsPadNumbers[13]='IW0170039';
		arrseekOwsPadNumbers[14]='IW0170040';
		arrseekOwsPadNumbers[15]='IW0170041';
		arrseekOwsPadNumbers[16]='IW0170042';
		arrseekOwsPadNumbers[17]='IW0170043';
		arrseekOwsPadNumbers[18]='IW0170044';
		arrseekOwsPadNumbers[19]='IW0170045';
		arrseekOwsPadNumbers[20]='IW0170046';
		arrseekOwsPadNumbers[21]='IW0170047';
		arrseekOwsPadNumbers[22]='IW0170048';
		arrseekOwsPadNumbers[23]='IW0170049';
		
	
		
		// variable to hold flag indicating this is an OWS Pad search
		var	isOwsPad = false;	
		var k=0; 
		for (k=0; k<24; k++){ 
			if(arrseekOwsPadNumbers[x] == upperTerms){
				isOwsPad = true;
				return
			}
		}
				
		// only if this is art or IW and is not an OWS Pad IW from the catalog, redirect		
		if (((where_is_IW == 0)&&(isOwsPad = false))){
			var third_Pos = upperTerms.charAt(2);
			var fourth_Pos = upperTerms.charAt(3);
			var ValidChars = "0123456789";
			if(where_is_IW == 0){
				if (!(ValidChars.indexOf(third_Pos) == -1)) {
					document.location = "/rugsdirectory/iw_hijack_vb.aspx?N=0&Ne=119&Nf=IsActive|GTEQ+1&IW=" + upperTerms;
					return
				}
			}
			
			if (where_is_ART == 0){
				if (!(ValidChars.indexOf(fourth_Pos) == -1) && (fourth_Pos != '')) {
					document.location = "/rugsdirectory/iw_hijack_vb.aspx?N=0&Ne=119&Nf=IsActive|GTEQ+1&IW=" + upperTerms;
					return
				}
			}
		}
		// check for R number
		//jkk HACK: The fall 2006 catalog had several items listed that are being held by the vendor
		//that should be shown even though they are technically inactive. This branch checks to see if this is any of those
		//R numbers.
		var arrseekRNumbers = new Array(4);
		arrseekRNumbers[0]='R116609';
		arrseekRNumbers[1]='R116697';
		arrseekRNumbers[2]='R116724';
		arrseekRNumbers[3]='R116621';
		arrseekRNumbers[4]='R116631';
		var x=0; 
		for (x=0; x<4; x++){ 
			//alert(arrseekRNumbers[x]);
			if(arrseekRNumbers[x] == upperTerms){
			//alert(upperTerms);
			//alert(arrseekRNumbers[x]);
			document.location = "/rugsdirectory/search_hijack.aspx?N=0&Ne=119&ColorID=" + upperTerms;
				return
			}
		}
		
	    //check for items that need to be replace
		var arrTerraRemoval = new Array(4);
		arrTerraRemoval[0] = 'R139643'
		arrTerraRemoval[1] = 'R139642'
		arrTerraRemoval[2] = 'R139640'
		arrTerraRemoval[3] = 'R139641'
		for (x=0; x<4; x++)
		{
			if(arrTerraRemoval[x] == upperTerms){
				document.location = "/rugsdirectory/promotions/2008/08/tera_collection_special_message.aspx"
				return
			}
		}
		
		// check for R number
		if (where_is_R == 0){
			var third_Pos = upperTerms.charAt(2);
			var ValidChars = "0123456789";
			if (!(ValidChars.indexOf(third_Pos) == -1)){
				document.location = "/rugsdirectory/search_hijack.aspx?N=0&Ne=119&Nf=IsActive|GTEQ+1&ColorID=" + upperTerms;
				return
			}
		}
				
	if(document.searchForm2.searchselect.selectedIndex == 0){
	
		
		
		if (terms == ""|| terms == "Type%20keyword%20or%20item%20%23")
				alert("Please Enter Search Terms");
		else {
			for (var i=0; i<document.searchForm2.elements.length; i++) {
				var name = document.searchForm2.elements[i].name;
				var ischeckbox = name.indexOf("within");

				if (ischeckbox != -1) {
					var value = document.searchForm2.elements[i].value;
					if (document.searchForm.elements[i].checked) {

						if (searchTerms != -1) {
							terms = searchTerms+"+"+terms;
						}
						withinResults++;
					}
				}
			}

			// Create new url with property search and dimension search

			if (withinResults != 0) {

				var removeterms = ["Ne","No","Nao","Ntk","Ntt","D", "Dx", "Ds", "act", "Nty","Ntx", "Od", "R", "Ns", "Nso", "Sup", "Nf", "displayCount", "Nu", "color_id", "Ns", "Search"];
				var addterms = ["Ntk="+property,"Ntt="+terms, "Ntx=mode+matchallpartial", "act="+searchActionCode, "Nf=IsActive%7CGTEQ+1", "Nu=color_id", "Ns=Most_Popular|1", "Search=Adv"];

			}
			else {
			
				if (posnot>=0 || posand>=0 || posor>=0) {

				var removeterms = ["N","Ne","No","Nao","Ntk","Ntt","D", "Dx", "Ds", "Di","act", "Ntx","Nty", "Od", "R", "Ns", "Nso", "Sup", "Nf", "displayCount", "Nu", "color_id", "Ns", "Search"];
				var addterms = ["N=0","Ntk="+property,"Ntt="+terms,"Nty=1","Ntx=mode+matchboolean", "D="+terms,"Dx=mode+matchboolean", "Sup=1", "act="+searchActionCode, "Nf=IsActive%7CGTEQ+1", "Nu=color_id", "Ns=Most_Popular|1", "Search=Adv"];
							
				} else {
				
					if (poswild>=0) {
				
					var removeterms = ["N","Ne","No","Nao","Ntk","Ntt","D", "Dx", "Ds", "Di","act", "Ntx","Nty", "Od", "R", "Ns", "Nso", "Sup", "Nf", "displayCount", "Nu", "color_id", "Ns", "Search"];
					var addterms = ["N=0","Ntk="+property,"Ntt="+terms,"Nty=1","Ntx=mode+matchallpartial", "D="+terms,"Dx=mode+matchallany", "Sup=1", "act="+searchActionCode, "Nf=IsActive%7CGTEQ+1", "Nu=color_id", "Ns=Most_Popular|1", "Search=Adv"];
				
					} else {

					var removeterms = ["N","Ne","No","Nao","Ntk","Ntt","D", "Dx", "Ds", "Di","act", "Ntx","Nty", "Od", "R", "Ns", "Nso", "Sup", "Nf", "displayCount", "Nu", "color_id", "Ns", "Search"];
					var addterms = ["N=0","Ntk="+property,"Ntt="+terms,"Nty=1","Ntx=mode+matchallpartial", "D="+terms,"Dx=mode+matchallany", "Sup=1", "act="+searchActionCode, "Nf=IsActive%7CGTEQ+1", "Nu=color_id", "Ns=Most_Popular|1", "Search=Adv"];
					
					}
				}
			}

			var newurl = ConstructURL("CURRENTURL", removeterms, addterms);
			//note: hard coding the new document location due to problems with pages in 
			//subdirectories looking for home_page.aspx in that sub-directory rather than in the root of the webstie
			document.location = "/rugsdirectory/home_page.aspx" + newurl;
		}
	}else{ //not a default search of "All" categories so record search for the selected category
	
	// Get search property and term values from form elements
		var terms = escape(document.searchForm2.searchterms2.value);
		var Nvalue = document.searchForm2.searchselect.options[document.searchForm2.searchselect.options.selectedIndex].value
		var searchActionCode = "A02";
		var property = "All";
		if ( Nvalue == 125 ){
			property = "Rugs"	
		}
		else if(Nvalue == 126){
			property = "Art"
		}else if(Nvalue == 147){
			property = "Furniture"
		}

		var searchTerms = GetValue("CURRENTURL","Ntt");
		var node = GetValue("CURRENTURL","N");
		var withinResults = 0;
	
		var removeterms = ["N","Ne","No","Nao","Ntk","Ntt","D", "Dx", "Ds", "act", "Nty","Ntx", "Od", "R", "Ns", "Nso", "Sup", "Nf", "displayCount", "Nu", "color_id", "Ns", "Search", "Ntx", "Dx"];
		var addterms = ["N="+Nvalue, "Ntk="+property, "Ntt="+terms, "D="+terms, "act="+searchActionCode, "Nf=IsActive%7CGTEQ+1", "Nu=color_id", "Ns=Most_Popular|1", "Search=Adv", "Ntx=mode+matchallany", "Dx=mode+matchallany"];
		var newurl = ConstructURL("CURRENTURL", removeterms, addterms);
		document.location = "/rugsdirectory/home_page.aspx" + newurl;
	}
}
