var ie=document.all?true:false;

window.onload = function(){

	rao($('SearchSite'));

	if($('SearchSite')){
		window.q = new qs();    
		q.init($('SearchSite'), '/socitm/asi.nsf/(azsearch)', 10, null,'', 3, null, 'b_qs_panel');
	} 
	
	if($('supplier_search')){
		window.q2 = new qs();    
		q2.init($('supplier_search'), '/socitm/asi.nsf/(azsupplier)', 5, null,'', 3, 2, 'b_qs_panel_sup'); 
		rao($('supplier_search'));
	}	

	if($('product_search')){
		window.q3 = new qs();    
		q3.init($('product_search'), '/socitm/asi.nsf/(azproduct)', 5, null,'', 3, 2, 'b_qs_panel_prod'); 
		rao($('product_search'));
	}	
	
	if($('la_search')){
		window.q4 = new qs();    
		q4.init($('la_search'), '/socitm/asi.nsf/(azla)', 5, null,'', 3, 2, 'b_qs_panel_la'); 
		rao($('la_search'));
	}	
	
	if($('pt_search')){
		window.q4 = new qs();    
		q4.init($('pt_search'), '/socitm/asi.nsf/(azpt)', 5, null,'', 3, 2, 'b_qs_panel_pt'); 
		rao($('pt_search'));
	}	
	
	if($('group_search')){
		window.q5 = new qs();    
		q5.init($('group_search'), '/socitm/asi.nsf/(azgroup)', 5, null,'', 3, 2, 'b_qs_panel_group'); 
		rao($('group_search'));
	}	
		
	if(window.addEventListener){
		window.addEventListener('click', function() { killPanels ()},false); 
	}else{
		window.document.attachEvent ('onclick', killPanels );
	}
}

function rao(f){
	if(f) f.setAttribute( "autocomplete", "off" );
}

function ms_clear(o, b, t){
	if(!b) if(o.value==t)	o.value = '';
	if(b) if(o.value=='')	o.value = t;
}

function ax(url, cb, opts) {
    var r = init();
    r.onreadystatechange = pR;
    function init(){ if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");};
    function pR () { if (r.readyState == 4) { if (cb) cb(r.responseText, r.status, opts);  r=null; } };
    this.dG = function() {r.open("GET", url, true);r.send(null); };
    this.dP = function(body) { r.open("POST", url, true); r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); r.send(body);};
}

function cx(op){
	var ief = true;
	if (window.ActiveXObject){ xd = new ActiveXObject("Microsoft.XMLDOM"); xd.async=false;
	 }else if (document.implementation && document.implementation.createDocument){
	 	  xd= document.implementation.createDocument("","",null); ief = false;
	 }else alert('Your browser is not compatible with this system, please upgrade to the latest version ...');
  	if(ief) xd.loadXML(op); else{ var oR = new DOMParser();xd = oR.parseFromString(op, "text/xml");	}	
	return xd;
}


var iconbase = '/socitm/asi.nsf/structure';
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


bind = function(obj,fn){
	return function(e){
		fn.call(obj,e);
	}
}

function $t(o,t,f){
	if(f)
		return o.getElementsByTagName(t)[0].firstChild.nodeValue;
	else
		return o.getElementsByTagName(t);
}

function $() {
  var els = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var el = arguments[i];
    if (typeof el == 'string')el = document.getElementById(el);
    if(arguments.length == 1)return el;
    els.push(el);
  }
 return els;
}

/*qs*/
function qs(){

	this.box;
	this.url;
	this.co;
	this.panel;
	this.prepend;
	this.addc;
}

qs.prototype = {
	init : function(b, u, c, l, p, a, flip, aid, extra, ender){
		this.box = b;
		this.url = u;
		this.co = c;
		this.addc = a;
		this.prepend = p;
		this.flip = flip;
		this.aid = aid;
		this.extra = extra;
		this.ender = ender;
		if(window.addEventListener){
			this.box.addEventListener('keyup',bind(this,function(){ this.qs_search() }),false); 
			window.addEventListener('click',bind(this,function(){ this.check() }),false); 
		}else{
			this.box.attachEvent ('onkeyup',bind(this,function(){ this.qs_search()}));
			window.attachEvent ('onclick',bind(this,function(){ this.check() }));
		}
		this.qs_create();
	},
	qs_create : function(l){
		var a = document.createElement("div");
		a.id = this.aid;
		a.innerHTML = 
			'<div>'+
				'<div class="lf b_qs_hlp' + (this.flip==1||this.flip==2?'" id="b_qs_tl"':' b_db"') + '></div>'+
				'<div class="lf b_db b_qs_mid"></div>'+
				'<div class="rf b_qs_hlp' +  (this.flip==1?' b_db"':'" id="b_qs_tr"') + '></div>'+
				'<div class="cb"></div>'+
			'</div>'+
			'<div class="b_db f4">'+
				'<div class="ip c_wh b" id="' + this.aid + '_upper"></div>'+
				'<div class="ip c_wh" id="' + this.aid + '_content"></div>'+
			'</div>'+
			'<div>'+
				'<div class="lf b_qs_hlp' + (this.flip==2?' b_db"':'" id="b_qs_bl"') + '></div>'+
				'<div class="lf b_db b_qs_mid"></div>'+
				'<div class="rf b_qs_hlp" id="b_qs_br"></div>'+
				'<div class="cb"></div>'+
			'</div>'+
		'</div>';
		this.panel = document.body.appendChild(a);
	},
	qs_search : function(){
		if(this.box.value!=''){
			$(this.aid + '_upper').innerHTML = '<div class="lf ip">Searching ...</div><div class="b_qs_cl rf mo" onclick="killPanels(this);" onmouseover="this.className=\'b_qs_cl rf b_qs_clo mo\';" onmouseout="this.className=\'b_qs_cl rf mo\';">&nbsp;</div><div class="rf ip"><img src="' + iconbase + '/spin.gif" />&nbsp;</div><div class="cb"></div>';
			var a = new ax(this.url + '?ReadViewEntries&Count=' + this.co + '&Rnd=' + Math.random().toString() + '&StartKey=' + this.box.value, function(op, stat, con){
				if(stat==200){
					var xd = cx(op);
					if(xd){
						var ve = xd.getElementsByTagName('viewentry');
						var cc = '';
						var m = con.box.value;
						m = m.replace(/ /g, '&nbsp;');
						var rx = new RegExp(m, 'gi');
						for(var x=0;x<ve.length;x++){	
							cc += '<div class="qs_m l_wh mo h_cb"  onmouseover="this.className=\'qs_mo b_lb c_bl mo h_cb\';" onmouseout="this.className=\'qs_m l_wh mo h_cb\';" onclick="' + (con.extra?con.extra:'window.location = \'') + (con.prepend&&(con.qs_gc(ve[x], 2).indexOf('http')==-1)?con.prepend:'') + con.qs_gc(ve[x], 2)  + (con.ender?con.ender:'\';') + '"><div class="ip">' + con.qs_gc(ve[x], 1).replace(rx, '<span>' + m + '</span>') +  (con.addc?'<div class="f2">'+con.qs_gc(ve[x], con.addc)+'</div>':'')  +'</div></div>';
					}
					$(con.aid + '_content').innerHTML = cc;
					$(con.aid + '_upper').innerHTML = '<div class="lf ip">Search suggestions</div><div class="b_qs_cl rf mo" onclick="killPanels(this);" onmouseover="this.className=\'b_qs_cl rf b_qs_clo mo\';" onmouseout="this.className=\'b_qs_cl rf mo\';">&nbsp;</div><div class="cb"></div>';
													
											
				}else
					$(con.aid + '_content').innerHTML = 'Sorry, the search function is unavailable at this time ...';
				}
							
				con.panel.style.display = 'block';
				if(con.flip==2){
						var chop = con.panel.offsetHeight;
						$(con.aid).style.top = (findPosY(con.box) - chop - (ie?0:-2)).toString() + 'px';
					}else
						$(con.aid).style.top = (findPosY(con.box) + con.box.offsetHeight).toString() + 'px';

					$(con.aid).style.left = (con.flip==1?(con.box.offsetWidth*-1)-(!document.all?12:8):0) + (ie?0:(con.flip==2?2:0)) + findPosX(con.box) + 'px';

			}, this);
			a.dG();
		}else
			this.panel.style.display = 'none';
	},
	qs_gc : function(e, c){
		var ed = e.getElementsByTagName('entrydata');
		var t = ed[c].getElementsByTagName('text');
		if(t[0].firstChild)
			if(t[0].firstChild.nodeValue)
				return t[0].firstChild.nodeValue;
			else
				return '';	
	},
	check : function(e){
		this.panel.style.display = 'none';
	}
}

function killPanels(){
	if(window.q) q.panel.style.display = 'none';
	if(window.q2) q2.panel.style.display = 'none';
	if(window.q3) q3.panel.style.display = 'none';
	if(window.q4) q4.panel.style.display = 'none';
	if(window.q5) q5.panel.style.display = 'none';
}

function numbersonly(e, decimal) {
var key;
var keychar;

if (window.event) {
   key = window.event.keyCode;
}
else if (e) {
   key = e.which;
}
else {
   return true;
}
keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
   return true;
}
else if (decimal && (keychar == ".")) { 
  return true;
}
else
   return false;
}

function toggleCategory(c, o){

	var b = $('block_'+c);
	var cn = b.className;
	var on = o.className;
	
	if(cn=='treeClose'){
		b.className = 'treeOpen';
		o.className = 'b socBack mb mo blockOpen';
	}else{
		b.className = 'treeClose';
		o.className = 'b socBack mb mo blockClosed';
	}
}

