// Detailed Search - AWS/PartNum Entry
var handler, qt;
function handlekey(ev, t) {
	if (handler) window.clearTimeout(handler);
	qt = t;
	if (ev.which==13) {
		doit();
	} else {
		handler = window.setTimeout('doit()', 1000);
	}
}
function doit() {
	if ( $('in_'+qt).value=='' ) return emptyit();
	var q = qt + '=' + $('in_'+qt).value;
	new Ajax.Request('/includes/ajax_prodsearch.php?' + q, {asynchronous:true, onSuccess:popit, onFailure:emptyit});
}
var popit = function(r) {
	var opts = r.responseText.split("\n");
	if (opts.length<=1) {
		emptyit(r);
		return false;
	}
	// clear select box
	var i, opt, txt;
	for ( i=$('sel_'+qt).options.length; i>=0; i--) {
		$('sel_'+qt).options[i] = null;
	}
	// add items to select box
	for (var i=0; i<opts.length-1; i++) {
		$('sel_'+qt).options[$('sel_'+qt).options.length] = new Option(opts[i]);
	}
}
var emptyit = function(r) {
	// empty select box
	var i;
	for ( i=$('sel_'+qt).options.length; i>=0; i--) {
		$('sel_'+qt).options[i] = null;
	}
	$('sel_'+qt).options[$('sel_'+qt).options.length] = new Option("No matches");
}

function clearsel(x) {
	for (var i=x.options.length; i>=0; i--) {
		x.options[i] = null;
	}
}

// Detailed Search - Droplists
var vtype='', vcat='', vind='', vbrand='';
function selchange(fe) {
	if (fe.name=='selType') {
		vtype = fe.value;
	} else if (fe.name=='selCat') {
		vcat = fe.value;
	} else if (fe.name=='selInd') {
		vind = fe.value;
	} else if (fe.name=='selBrand') {
		vbrand = fe.value;
	}
	new Ajax.Request('/includes/ajax_prodsearch.php?dss=' + fe.name + '&selType=' + vtype + '&selCat=' + vcat + '&selInd=' + vind + '&selBrand=' + vbrand, {asynchronous:true, onSuccess:narrowsel, onFailure:stepfail})
}
var narrowsel = function(r) {
	var newsel = r.responseText.split("***\n");
	var opts, o;
	for (var i=0; i<newsel.length-1; i++) {
		opts = newsel[i].split("\n");
		if (opts.length>2) {
			// Clear Select Box
			clearsel($(opts[0]));
			for (var j=1; j<opts.length-1; j++) {
				o = opts[j].split("|");
				$(opts[0]).options[$(opts[0]).options.length] = new Option(o[1], o[0]);
			}
		}
	}
}

// Step by Step Search
var v1, v2, v3, v4;
var vs = 1;
function sendstep(st, f) {
	if (st==1) {
		// Restart search
		new Ajax.Request('/includes/ajax_prodsearch.php?sbs=' + st, {asynchronous:true, onSuccess:stepgood, onFailure:stepfail});

	} else if (st==2) {
		// Send Step 1 Response
		var v = '';
		if (vs <= st) {
			for (var i=0; i<f.basemetal.length; i++) {
				if (f.basemetal[i].checked) {
					v = f.basemetal[i].value;
					break;
				}
			}
			if (!v) {
				alert('Please select a base metal...');
				return false;
			}
			v1 = v;
		}
		vs = 2;
		new Ajax.Request('/includes/ajax_prodsearch.php?sbs=' + st + '&basemetal=' + v1, {asynchronous:true, onSuccess:stepgood, onFailure:stepfail});

	} else if (st==3) {
		// Send Step 2 Response
		var v = '';
		if (vs <= st) {
			if (typeof f.weldproc.length == 'undefined') {
				v = f.weldproc.value;
			} else {
				for (var i=0; i<f.weldproc.length; i++) {
					if (f.weldproc[i].checked) {
						v = f.weldproc[i].value;
						break;
					}
				}
			}
			if (!v) {
				alert('Please select a welding process...');
				return false;
			}
			v2 = v;
		}
		vs = 3;
		new Ajax.Request('/includes/ajax_prodsearch.php?sbs=' + st + '&basemetal=' + v1 + '&weldproc=' + v2, {asynchronous:true, onSuccess:stepgood, onFailure:stepfail});

	} else if (st==4) {
		// Send Step 3 Response
		var v = '';
		if (vs <= st) {
			if (f.weldpos.type=='radio') {
				v = f.weldpos.value;
			} else {
				for (var i=0; i<f.weldpos.length; i++) {
					if (f.weldpos[i].checked) {
						v = f.weldpos[i].value;
						break;
					}
				}
			}
			if (!v) {
				alert('Please select a welding position...');
				return false;
			}
			v3 = v;
		}
		vs = 4;
		new Ajax.Request('/includes/ajax_prodsearch.php?sbs=' + st + '&basemetal=' + v1 + '&weldproc=' + v2 + '&weldpos=' + v3, {asynchronous:true, onSuccess:stepgood, onFailure:stepfail});

	} else if (st==5) {
		// Send Step 4 Response
		var v = '';
		if (vs <= st) {
			for (var i=0; i<f.shldmed.length; i++) {
				if (f.shldmed[i].checked) {
					v = f.shldmed[i].value;
					break;
				}
			}
			if (!v) {
				alert('Please select a shielding medium...');
				return false;
			}
			v4 = v;
		}
		vs = 5;
		new Ajax.Request('/includes/ajax_prodsearch.php?sbs=' + st + '&basemetal=' + v1 + '&weldproc=' + v2 + '&weldpos=' + v3 + '&shldmed=' + v4, {asynchronous:true, onSuccess:stepgood, onFailure:stepfail});
	}
}
var stepgood = function(r) {
	if (r.responseText=='SKIP4') {
		document.stepByStepSearch.skipnext.value='Skipped';
		document.stepByStepSearch.submit();
	} else {
		$('psss').innerHTML = r.responseText;
	}
}
var stepfail = function(r) {
	$('psss').innerHTMl = "<p>Sorry, an error has occurred, please refresh the page and try again.</p>\n";
}