/*
	s4.js
	enhanced user search experience
	4/23/08 arb Xartech.com

	3/20/09 - new version adds the condition dropdown
*/

var esearch = {
	//flag to tell if the txtM field is blank
	misBlank: 1,
	elm: 0,
	elmdd: 0,
	

	checkmode: function(sel) {
		var tval = this.getddval(sel);


		switch(tval) {
		case "-mode2":
		case "--mode2":
			//mode 2 is where users can type in whatever brand they like
			esearch.setmode2("");
			break;
			
		case "-listbrands":
		case "--listbrands":
			//handle frames and no-frames situations
			if (top==self) 
				location = "/newcart/brandlist.php";
			else
				//frames version 4/28/08
				top.frames[1].location = "/newcart/brandlist.php?frame";
			break;
		case "-":	
			//do nothing! This is index 0 in the list
			break;
		
		default:
			//an actual brand has been selected from the dropdown
			//hide the textbox, set the focus to txtS
			this.setmode2blank();
			document.getElementById("txtS").focus();
		}
	},


	setmode2: function(txt) {

		//modify the label
		document.getElementById("ddLabel").innerHTML = " &nbsp; Brand:";

		//move the floating txtM into position and display it
		var pos = esearch.cumulativeOffset(esearch.elmdd);
		(1+pos[0]) + "px";
		
		
		this.elm.style.top = (1+pos[0]) + "px";
		this.elm.style.left= (1+pos[1]) + "px";
		
		this.elm.value = txt;
		this.elm.style.display = "";
		this.elm.focus();
		
		//is there a shim (ie6)
		if (this.shim) {
			this.shim.style.top = (1+pos[0]) + "px";
			this.shim.style.left= (1+pos[1]) + "px";
			this.shim.style.display="";
		}
		
	},


	setmode2blank: function() {
		m=this.elm;
	
		esearch.misBlank = 1;
		//m.value="All Brands";
		//m.style.color = "#808080";
		m.value="";
		m.style.display="none";		
		
		if (this.shim) this.shim.style.display="none";
		
		document.getElementById("ddLabel").innerHTML = " &nbsp; ";

	},


	setall: function(vsearch, vbrand, vtype, vsmartc) {

		document.getElementById('txtS').value = vsearch;
		this.setdd(document.getElementById('ddBrand'), vbrand);
		this.setdd(document.getElementById('ddType'), vtype);
		this.setdd(document.getElementById('ddC'), vsmartc);
		
		//if (vbrand!="") document.getElementById('txtM').value = vbrand;
		document.getElementById('txtS').focus();
		
                var c=this.getcookieo().refhist;
                for(var i=document.forms.length-1;i>0;i--){
                        var frm=document.forms[i];
                        if (frm.refhist) frm.refhist.value=c;
                }		
	},

	getddval: function(sel) {
		if (sel.value) return sel.value;
		if (sel[sel.selectedIndex]) return sel[sel.selectedIndex].innerHTML;
	},

	setdd: function(sel, val) {
		//assume the first selector is always ""
		if (val=="" || val=="-") {
			sel.selectedIndex = 0;
			sel.focus();
			return;
		}

		//walk through the sel object's options looking for the value we need (x)
		//alert(sel[0].value); return;
		var z=sel.length;
		for(i=0; i<z; i++) {
			if (sel[i].value != "") {
				if (val==sel[i].value) {
					sel.selectedIndex = i;
					return;
				}
			} else if (sel[i].innerHTML == val) {
				//in IE .value only matches the specific value="" attribute
				sel.selectedIndex = i;
				return;				
			}
		}

		//if that value was not found insert a new value
		//alert("not found [" + val + "]");
		var temp = new Option(val);
		sel[z] = temp;
		sel.selectedIndex = z;
	},
	

	listen: function(element, name, handler) {
	    if (element.addEventListener)
	      element.addEventListener(name, handler, false);
	    else if (element.attachEvent) 
	      element.attachEvent('on' + name, handler);
    }, 
	
	
	//check the input values, clear the "Enter Brand Name Here" message.
	presend: function() {
		if (this.misBlank) document.getElementById("txtM").value = "";
		return true;
	},
	
saveref:function(){var temp;var friends=" ezsystems.com pinnaclemicro.com pinnaclemicrogb.co.uk";
temp=document.referrer.match(/^\w+:\/\/(www.|)([\w.]+)/);if(temp!=null&&friends.indexOf(temp[2])>0)
return;this.refpush(document.referrer||"-");this.showSearchHelp(document.referrer);},

showSearchHelp: function(rf,qf){if(!jQuery)return;if(rf=="")return;if($('#landingsearch').length==
0)$('<div id="landingsearch"></div>').prependTo("body");$('#landingsearch').load(
'/newcart/landingsearch.php',{r:rf});},

getcookieo:function(){var obj,list,i,temp;obj={};list=document.cookie.split(/; */);for(i in list){
temp=list[i].split(/=/);if(temp.length==2)obj[unescape(temp[0])]=unescape(temp[1]);}return obj;},

refpush:function(loc){var c,hist,ehist,stamp,now,dl;dl=document.location;if(dl.protocol=="https:" 
)return;loc=loc+" ->"+dl.pathname+dl.search;now=new Date();stamp="\\\\"+(100*(now.getMonth()+1)+ 
now.getDate())+" "+loc;c=this.getcookieo();if(c.refhist&&c.refhist.length>1){hist=c.refhist;if(
stamp!=hist.substr(0,stamp.length))hist=stamp+hist;}else hist=stamp;if(hist.length>3000)hist=
hist.substr(0,3000);this.setcookie("refhist",hist,30);return ehist;},

setcookie:function(name,val,days){
var cescape=function(v){return v.replace(/%/g,"%25").replace(/=/g,"%3D").replace(/;/g,"%3B")};
var cook=escape(name) + "=" + cescape(val);var today=new Date();var expire=new Date();if(days>0){
expire.setTime(today.getTime()+3600000*24*days);cook+=";expires="+expire.toGMTString();}document
.cookie=cook+"; path=/";},

	start: function() {
                this.saveref();
		document.getElementById("txtS").focus();	//ff,ie7
		document.getElementById("txtS").select();	//ie6

	

		//setup the elements
		this.elm = document.getElementById("txtM");
		//alert(pos[0] + "," + pos[1]);
		

		//when the form submits, be sure to clear the m= value
		var frm = document.forms['s3form'];
		if (frm) this.listen(frm, 'submit', function() {
			if (esearch.misBlank) esearch.elm.value="";
		});
			

		var bdd= document.getElementById("ddBrand");
		this.elmdd = bdd;

		//txtM is now positioned when it activates
		//move the floating txtM to the same top as ddBrand
		//var pos = esearch.cumulativeOffset(bdd);
		//alert("pos: " + pos[0] + "," + pos[1]);
		//this.elm.style.top = pos[1] + "px";
		//this.elm.style.left= pos[0] + "px";



		//add the iframe shim as needed for ie6
		if (navigator.userAgent.indexOf("MSIE 6")>1) {
			this.shim = document.createElement( 'iframe' );
			this.shim.setAttribute( 'src', "javascript:''" );
			this.shim.setAttribute( 'frameBorder', '0' );
			this.shim.setAttribute( 'frameBorder', '0' );
			this.shim.style.position = "absolute";
			this.shim.style.width = "160px";
			this.shim.style.height = "20px";
			this.shim.style.display = "none";
			this.shim.style.zIndex= "9";	//the textbox is 10
			this.shim.scrolling = "no";
			bdd.parentNode.insertBefore(this.shim, bdd);
			//alert("ie6 shim " + navigator.userAgent);
			
			
			//modify the textbox so it looks a little better in ie6
			this.elm.style.border = "1px white";
			
			
		}

	

		//setup the m box events
		var m = this.elm;
		this.setmode2blank(m);

		this.listen(m, "focus", function() {
			if (esearch.misBlank) {
				m.value="";
				m.style.color = "";
				esearch.misBlank=0;
			}
		});

		this.listen(m,'blur', function() {
			if (m.value=="") {
				esearch.setmode2blank(m);
				esearch.elmdd.selectedIndex = 0;
			}
		});
		

		//setup the dropdown brand events
		this.listen(bdd, "focus", function() {
			if (m.value=="") {
				esearch.setmode2blank(m);
			}
			
			//reset dropdown if it is on any command
			if (esearch.getddval(esearch.elmdd).charAt(0) == "-")
				bdd.selectedIndex = 0;
			
		});


	},


  cumulativeOffset: function(element) {
    var vtop = vleft = 0;
    var el = element;
    
    do {
      vtop += el.offsetTop  || 0;
      vleft += el.offsetLeft || 0;
      el= el.offsetParent;
    } while (el);
    return [vtop, vleft];
  }
  
  
  
};



//IE displays the absolute positioned element two pixels up from where it should be
//and ie6 needs an iframe shim to cover the dropdown



document.write(
'<p style="font:bold 13px arial,sans-serif;color:white;margin:0;"><span id=ddLabel> &nbsp; </span>',"\n",
'<input id=txtM name=m style="font-size:13px; width:160px; height:20px; border: 1px white;',
'position:absolute;color:#505080;background:#FFFFF0;z-index:10">',"\n",
'<select id=ddBrand name=brand style="font-size:13px;width:180px;height:20px" onchange="esearch.checkmode(this)">',"\n",
'<option value="-">All Brands</option>',"\n",
'<option value="-mode2" style="color:#505080;background:#FFFFF0">Enter Brand Name</option>',"\n",
'<option value="-listbrands" style="color:blue;text-decoration:underline;">More Brands...</option>',"\n"
);

brandslist();

document.write(
'<option value="--mode2" style="color:#808080;background:#FFFFF0">Enter Brand Name</option>',"\n",
'<option value="--listbrands" style="color:blue;text-decoration:underline;">More Brands...</option>',"\n",
'<option value="--">All Brands</option>',"\n",
'</select>',"\n",
'',"\n",
'<select id=ddType name="stype" style=" height:20px;font-size:13px">',"\n",
'<option value="P">Part/Model</option>',"\n",
'<option value="K">Keyword(s)</option>',"\n",
'</select>',"\n",
'',"\n",
'<input id=txtS type="text" name="s" value="" style="height:22px;width: 180px;">',"\n",
'<select id="ddC" name="smartc" style="height:20px"><option value="">All Conditions</option><option value="ref">Refurbished</option><option value="new">New</option><option value="rep">Repair</option></select>',
'&nbsp;<input type="submit" value="Search">',"\n",
'<a style="font:bold 13px arial,sans-serif;color:white" href="http://www.ezsystems.com/newcart/advanced.php">Advanced</a>',"\n",
'&nbsp; <a style="font:bold 13px arial,sans-serif;color:white" href="http://www.ezsystems.com/newcart/converter.php">Currency Conversion</a>',"\n"
);

esearch.start();


function brandslist() {
	var temp, i;
	temp = '3Com, ACER, ACP MEMORY, Adaptec, ALCATEL, AMD, APC American Power, Apple Computer, ASUS TEK, ATI TECHNOLOGIES, AT&T, Avaya, AXIOM MEMORY, Belkin, BROTHER, CA Computer Ass, CABLES TO GO, CANON, CISCO Systems, Citoh, Compaq, DA-LITE, DATAPRODUCTS, DEC Digital Equipment, Decision Data, Dell, Digi International, EMC, ERICSSON, EXTREME NETWORKS, FORTINET, FUTURE MEMORY, Gateway, Genicom, HITACHI America, HP Hewlett Packard, IBM, Intel Corporation, INTERMEC, Juniper Networks, Kaspersky Lab, KENTROX, KINGSTON TECH, KONICA MINOLTA, LENMAR, LENOVO, Lexmark, LUCENT TECH, MCAFEE, Microsoft, Motorola, NCR, NEC AMERICA, Nortel, OKIDATA, Oracle Corporation, Panasonic, PLANTRONICS, Polycom, PREMIUM POWER, Printronix, Quantum, Ricoh, RSA, SAMSUNG, Seagate Technology, Sharp Electronics, SIEMENS, SONICWALL, Sony, STARTECH, Sun Microsystems, SUPERMICRO, SYMANTEC, SYMBOL TECHNOLOGIES, TALLY GENICOM, Texas Instruments, TOSHIBA, TRIPP LITE, WDC Western Digital, Xerox, ZEBRA TECHNOLOGIES';
	temp = temp.split(/,\s*/);
	for(i=0;i<temp.length;i++) {
		document.write("<option>" + temp[i] + "</option>\n");
	}
}

function runtest() {


//	esearch.setall("","ASUS","K");
//
//	var el = esearch.elmdd;
//	var s = el[el.selectedIndex];
//	
//	
//	if (s.value == null) alert("value is null");
//
//
//
//
//	alert('value=' + el[el.selectedIndex].value);
//	alert('innerhtml=' + el[el.selectedIndex].innerHTML);


}
