//$Id: order.js,v 1.56 2010/03/21 23:06:28 rrkramer Exp $
var Dw = null;
var Pw=null;
var Iw=null;
var Mw=null;
var Page;
var jumpField = 0;
var DocDir = ""+window.location;
if (! DocDir.match(/https:\/\/.*\/~.*?\//)) {
	DocDir = "";
}
function close_wins(which) {
	if ( ! which ) {
		which = "desc pict info msgs shipto";
	}
	if (which.search(/desc/i)!= -1) {
		if ( Dw && ! Dw.closed ) {
			Dw.close();
		}
	}
	if (which.search(/pict/i)!= -1) {
		if ( Pw && ! Pw.closed ) {
			Pw.close();
		}
	}
	if (which.search(/info/i)!= -1) {
		if ( Iw && ! Iw.closed ) {
			Iw.close();
		}
	}
	if (which.search(/msgs/i)!= -1) {
		if ( Mw && ! Mw.closed ) {
			Mw.close();
		}
	}
	if (which.search(/shipto/i)!= -1) {
		if (typeof ShiptoWin != "undefined" && ShiptoWin && ! ShiptoWin.closed ) {
			ShiptoWin.close();
		}
	}
}
function hasItemOrder(formNo) {
	var j;
	var found=false;
	var sform;
	
	sform = skrunch(formNo);
	for (j = 1; j <= Item_cnt; j++) {
		if (On_order[j]) {
			if (skrunch(On_order[j].formNo) == sform) {
				return j;
			}
		}
	}
	return 0;
}
function hasImprintInfo(formNo) {
	var j = hasItemOrder(formNo);
	if ( j && ( ! On_order[j].is_imprint || ! On_order[j].imprint )) {
		j = 0;
	}
	return j;
}
function check_qty(f,item) {
	var only = "";
	var re = /\s/g;
	var qty = f.value = f.value.replace(re,"");
	re = /^-*\d+$/;
	if (qty.match(re) == null) {
		var msg = (typeof item == "undefined" || ! item) ? "here." : " for item "+item+".";
		if (qty.length != 0) {
			only = "ONLY ";
		}
		alert('Please enter a numeric quantity '+only+msg);
		f.focus();
		f.select();
		return void(0);
	}
	return qty;
}
function check_item_qty_limit(obj, property_, limit, item) {
	var qty, msg;
	if (no_limits) {
		return "";
	}
	if (obj) {
		qty = Number(obj[property_]);
	} else {
		qty = property_;
	}
	if (qty > limit && limit > 0) {
		if (Qty_overlimit_text) {
			msg = Qty_overlimit_text;
		} else {
			msg = "You've exceeded your ordering limit for this item ("+item+") which is "+limit+". The quantity has been reduced to this limit.";
		}
		if (obj) {
			obj[property_] = limit;
		}
		return msg;
	}
	return "";
}
function check_item_min_order(obj, property_, limit, item) {
	var qty, msg;
	if (obj) {
		qty = Number(obj[property_]);
	} else {
		qty = property_;
	}
	if (qty < limit && limit > 0) {
		msg = "The minimum order quantity for this item ("+item+") is "+limit+". The quantity has increased to this minimum.";
		if (obj) {
			obj[property_] = limit;
		}
		return msg;
	}
	return "";
}
function Order(f,not_imp,formSeq,df_override) {  
	// f is qty field. Use Order_nondb_imprint() for "Add Imprint" page. Imprints that are *added* here will already
	// have the data fields defined in the df.pF[] and df.pB[] array.
	var j,k,form_no,qty,flags,seq,sub_seq,db_idx,db_idxBk,imp_idx,fld;
	var re,buf,isImp=0,isCs,billable,cols=0,weight="",cbreaks="",cprices="",color="",size="",style="",extra="";
	var qty_limit="", min_order="", has_backer=false, file_upload="", text_attach="";
	var df=ks_main.sub_main;
	if (typeof df_override != "undefined") {
		df=df_override; 
	}
	var df_pQL = "", df_pMO = "", limit_warning = "", min_warning = "";
	if ( typeof df.pQty_limits != "undefined") {
		df_pQL = df.pQty_limits;
	}
	if ( typeof df.pMin_ord_qtys != "undefined") {
		df_pMO = df.pMin_ord_qtys;
	}

	var doc_form=df.document.forms[0];
	if (typeof formSeq != "undefined") {
		doc_form = df.document.forms[formSeq];
	}

	j = f.name.indexOf(":");
	if (j > 3) {
		k = f.name.indexOf("_");
		if (k > 3) {
			imp_idx = Number(f.name.substring(k+1,j));
			if (k > 4) {
				db_idx = Number(f.name.substring(3,k));
			} else {
				db_idx = "";
			}
		} else {
			imp_idx = db_idx = Number(f.name.substring(3,j));
		}
	}
	form_no = skrunch(f.name.substring(j+1));
	for (j = 0; j < df.pItems.length; j++) {
		if (typeof formSeq != "undefined") {
			j = formSeq;
		}
		if (form_no == skrunch(df.pItems[j])) {
			qty = check_qty(f,df.pItems[j]);
			if (typeof qty == "undefined") {
				return false;
			}
			qty = Number(qty);
			if (df_pQL) {
				limit_warning = check_item_qty_limit(f, "value", Number(df_pQL[j]), df.pItems[j]);
				if (limit_warning) {
					qty = df_pQL[j];
				}
			}
			if (df_pMO) {
				min_warning = check_item_min_order(f, "value", Number(df_pMO[j]), df.pItems[j]);
				if (min_warning) {
					qty = df_pMO[j];
				}
			}
			if ( qty == 0 ) {
				seq = hasItemOrder(form_no);
				if (seq) {
					q = On_order[seq];
					if (isImp) {
						sub_seq = get_sub_seq(q, imp_idx);
						if (typeof sub_seq == "undefined") {
							sub_seq = 0;
						}
					}
					deleteFromOrd(seq, sub_seq);
				}
			} else {
				flags = df.pFlags[j];
				isCs = (flags & 0x400) ? 1 : 0;
				if (flags & 0x8000) {
					isImp = not_imp ? 0 : 1;
					if (doc_form.has_backer.value == "Y") {
						has_backer = true;
					}
				}
				if (flags & 0x40) {
					isImp = 1;
				}
				billable = (flags & 0x4000) ? 1 : 0;
				if ( df.pWbps[j] > "" ) {
					buf = df.pWbps[j].split("|");
					weight = buf[0];
					cbreaks = buf[1]+"";
					cprices = buf[2]+"";
				}
				cols = df.pFlags[j] & 0x000F;
				if (typeof df.pColors != "undefined") {
					color = df.pColors[j];
				}
				if (typeof df.pSizes != "undefined") {
					size = df.pSizes[j];
				}
				if (typeof df.pExtras != "undefined") {
					color = df.pExtras[j];
				}
				if (typeof df.pStyles != "undefined") {
					style = df.pStyles[j];
				}
				if (df_pQL) {
					qty_limit = df_pQL[j];
				} else {
					qty_limit = 999999999;
				}
				if (df_pMO) {
					min_order = df_pMO[j];
				} else {
					min_order = 0;
				}
				seq = putOnOrder(form_no,df.pI_nums[j],df.pForm_names[j],df.pUnits[j],df.pQty_units[j],df.pPrices[j],
							qty,isImp,isCs,weight,cols,cbreaks,cprices,billable,df.pFlags[j],df.pPus[j],
							df.pQty_pus[j],df.pMfgrs[j],df.pVpart_nos[j],color,size,style,extra,qty_limit,
							min_order,has_backer,file_upload,text_attach);
				if (isImp) {
					var q;
					if (seq) {
						q = On_order[seq];
					} else {
						q = On_order[Item_cnt];
						q.IDfields = df.pIDfields;
						if (doc_form.has_backer.value == "Y") {
							if (typeof df.pIDfieldsBk[0] != "undefined") {
								q.IDfieldsBk = df.pIDfieldsBk;
							} else {
								q.IDfieldsBk = "";
							}
						}
						q.cnt--;
					}

					sub_seq = get_sub_seq(q, imp_idx);

					if (db_idx >= 0) { // from database
						if (typeof sub_seq == "undefined") {
							sub_seq = q.cnt++;
							q.qty_is_set[sub_seq] = false;
							Has_unset_qty = true;
							q.imprint[sub_seq] = new Object();
							if (doc_form.has_face_fields.value) {
								q.imprint[sub_seq] = df.pF[db_idx];
								if (doc_form.has_backer.value == "Y") {
									q.imprintBk[sub_seq] = new Object();
									if (typeof df.pF[db_idx]['bkid'] != "undefined") {
										db_idxBk = df.pF[db_idx]['bkid'];
										q.imprintBk[sub_seq] = df.pB[db_idxBk];
									}
								}
							} else {
								q.imprintBk[sub_seq] = df.pB[db_idx];
								q.imprint[sub_seq] = new Object();
							}
						}
					}
					q.qty[sub_seq] = qty;
					q.price[sub_seq] = df.pPrices[j];
					q.db_idx[sub_seq] = db_idx;
					q.imp_idx[sub_seq] = imp_idx;
					if (typeof db_idxBk != "undefined") {
						q.db_idxBk[sub_seq] = db_idxBk;
					}

					if (typeof formSeq != "undefined") {
						//sub_seq = doc_form.id.value;
						q.extLabel = q.imprint[sub_seq] = doc_form.extId.value;
					} 
				}
			}
			break;
		}
		if (typeof formSeq != "undefined") {
			break;	
		}
	}
	if (limit_warning) {
		alert(limit_warning);
	}
	if (min_warning) {
		alert(min_warning);
	}
	return true;
}
function putOnOrder(formNo,i_num,formName,unit,qty_unit,price,qty,isImp,isCs,weight,ncp,cbreaks,cprices,billable,flags,
				pu,qty_pu,mfgr,vpart_no,color,size,style,extra,qty_limit,min_order,has_backer,file_upload,text_attach) {
	var seq, r;
	seq = hasItemOrder(formNo);
	if (seq == 0) {
		if (qty != 0) {
			On_order[++Item_cnt] = new addItem(formNo,i_num,formName,unit,qty_unit,price,qty,isImp,isCs,weight,ncp,cbreaks,cprices,
			billable,flags,pu,qty_pu,mfgr,vpart_no,color,size,style,extra,qty_limit,min_order,has_backer,file_upload,text_attach);
		}
	} else if (On_order[seq].is_imprint == 0) {
		if (qty == 0) {
			deleteFromOrd( seq, 0);
		} else {
			On_order[seq].qty[0] = qty;
		}
	}
	orderInProgress = true;
	return seq;
}
function get_sub_seq(q, imp_idx) {
	var sub_seq;
	for (sub_seq = 0; sub_seq < q.cnt; sub_seq++) {
		if (typeof q.imp_idx[sub_seq] != "undefined" && q.imp_idx[sub_seq] == imp_idx) {
			return sub_seq;
		}
	}
	return (void 0);
}

function addItem(formNo,i_num,formName,unit,qty_unit,price,qty,is_imp,is_cs,weight,ncp,cbreaks,cprices,billable,flags,pu,qty_pu,mfgr,vpart_no,color,size,style,extra,qty_limit,min_order,has_backer,file_upload,text_attach) {
	this.formNo = formNo;
	this.i_num = i_num;
	this.formName = formName;
	this.unit = unit;
	this.qty_unit = qty_unit;
	this.cnt = 1;
	this.price = new Array();
	this.price[0] = price;
	this.qty = new Array();
	this.qty[0] = qty;
	this.qty_is_set = new Array();
	this.imp_idx = new Array();
	this.db_idx = new Array();
	this.db_idxBk = new Array();
	this.imprint = new Array();
	this.imprintBk = new Array();
	this.IDfields = new Array();
	this.IDfieldsBk = new Array();
	this.is_imprint = is_imp;
	this.is_cs = is_cs;
	this.weight = weight;
	this.ncp = ncp;
	this.cbreaks = cbreaks;
	this.cprices = cprices;
	this.billable = billable;
	this.flags = flags;
	this.pu = pu;
	this.qty_pu = qty_pu;
	this.mfgr = mfgr;
	this.vpart_no = vpart_no;
	this.color = color;
	this.size = size;
	this.style = style;
	this.extra = extra;
	this.qty_limit = qty_limit;
	this.min_order = min_order;
	this.status = "";
	this.has_backer = has_backer;
	this.file_upload = file_upload;
	this.text_attach = text_attach;
}
function deleteFromOrd(seq, sub_seq) {
	var r;
	var q = On_order[seq];
	for ( r = sub_seq; r < q.cnt - 1; r++ ) {
		q.price[r] = q.price[r+1];
		q.qty[r] = q.qty[r+1];
		q.imprint[r] = q.imprint[r+1];
		q.db_idx[r] = q.db_idx[r+1];
		q.imp_idx[r] = q.imp_idx[r+1];
		if (typeof q.imprintBk[r] != "undefined") {
			q.imprintBk[r] = q.imprintBk[r+1];
		}
	}
	if ( --q.cnt > 0 ) {
		r = q.cnt;
		delete q.price[r];
		delete q.qty[r];
		delete q.imp_idx[r];
		delete q.db_idx[r];
		for (fld in q.imprint[r]) {
			delete fld;
		}
		delete q.imprint[r];
		if (typeof q.imprintBk[r] != "undefined") {
			for (fld in q.imprintBk[r]) {
				delete fld;
			}
			delete q.imprintBk[r];
		}
	} else {
		delete q.price[0];
		delete q.price;
		delete q.qty[0];
		delete q.qty;
		delete q.imp_idx[0];
		delete q.imp_idx;
		delete q.db_idx[0];
		delete q.db_idx;
		if (typeof q.imprint[0] != "undefined") {
			for (fld in q.imprint[0]) {
				delete fld;
			}
		}
		delete q.imprint;
		if (typeof q.imprintBk[0] != "undefined") {
			for (fld in q.imprintBk[0]) {
				delete fld;
			}
		}
		delete q.imprintBk;
		delete q.IDfields;
		delete q.IDfieldsBk;
		for ( r = seq; r < Item_cnt; r++) {
			On_order[r] = On_order[r+1];
		}
		delete On_order[Item_cnt--];
	}
}
function hasItemQty(formNo) {
	var j, k, q, tot=0;       

	for ( j = 1; j <= Item_cnt; j++ ) {
		if ( On_order[j] ) {
			q = On_order[j];
			if ( formNo == q.formNo ) {
				for ( k = 0; k < q.cnt; k++ ) {
					tot += parseFloat(q.qty[k]);
				}
				return tot;
			}
		}
	}
	return 0;
}
function getQty(is_imp) {
	var d = ks_main.sub_main.document;
	var f = d.forms[0];
	var j,p,q,qty="",form_no,item,max_items,seq,imp_idx,db_idx,edid;
	var k = 0, j2 = 0;
	var jf = jumpField;
	
	if ((typeof f != "undefined") && (typeof f.elements.length != "undefined")) {
		max_items = f.elements.length;
		for (j = 0; j < max_items; j++) {
			form_no = f.elements[j].name;
			if ("qty" == form_no.substring(0,3)) {
				p = form_no.indexOf(":");
				item = form_no.substring(p+1);
				if (is_imp) {
					qty = "";
					imp_idx = form_no.substring(3,p);
					p = imp_idx.indexOf("-");
					if (p > 0) {
						imp_idx = imp_idx.substring(p);
					}
					imp_idx = Number(imp_idx);
					seq = hasItemOrder(item);
					if (seq) {
						q = On_order[seq];
						for (k = 0; k < q.cnt; k++) {
							if (q.imp_idx[k] == imp_idx) {	
								qty = q.qty[k];
								break;
							}
						}
					}
				} else {	
					qty = hasItemQty(item);
				}
				if (qty == '0'){ 
					qty = '';
				}
				f.elements[j].value = qty;
				edid = d.getElementById('ed'+imp_idx);
				if (qty && edid) {
					edid.style.display = "inline";
				}
				if ( k++ == jf ) {
					j2 = j;
				}
			}
		}
		if ( max_items && f.elements[j2].type=="text") {
			f.elements[j2].focus();
		}
	}
	location.href="javascript:void(document.close())";
}
function mk_base(form,hot,base) {
	if ( ! form ) {
		return base;
	}
	form = skrunch(form);
	if ( hot ) {
		return (typeof Hot_list[form] != "undefined" && Hot_list[form]) ? "onh":"offh";
	}
	return (typeof My_list[form] != "undefined" && My_list[form]) ? "on":"off";
}
function mk_title(form,hot) {
	form = skrunch(form);
	if ( hot ) {
		return (typeof Hot_list[form] != "undefined" && Hot_list[form]) ? "Remove from hot list":"Add to hot list";
	}
	return (typeof My_list[form] != "undefined" && My_list[form]) ? "Remove from my list":"Add to my list";
}
function press(image,base,form,hot) {
	var smd=ks_main.sub_main.document;
	smd.images[image].src = dn_image[mk_base(form,hot,base)].src
}
function unpress(image,base,form,hot) {
	var smd=ks_main.sub_main.document;
	smd.images[image].src = up_image[mk_base(form,hot,base)].src
	if ( form && image) {
		smd.images[image].title = mk_title(form,hot);
	}
}
function to_quicky(item,i_num,hot,image) {
	var cnt, q, k, fidx, img, add, base, root;
	var smd=ks_main.sub_main.document;
	item = skrunch(item);
	if ( hot ) {
		if ( typeof Hot_list[item] == "undefined" ) { 
			add = Hot_list[item] = 1;
		} else {
			add = (Hot_list[item] = ! Hot_list[item]) ? 1 : "";
		}
		root = "my_clist";
		base = (add == 1) ? "onh" : "offh";
	} else {
		if ( typeof My_list[item] == "undefined" ) {
			add = My_list[item] = 1;
		} else {
			add = (My_list[item] = ! My_list[item]) ? 1 : "";
		}
		root = "my_plist";
		base = (add == 1) ? "on" : "off";
	}
	if ( document.title == "Shopping Basket" ) {
		fidx = 0;
		for ( cnt = 1; cnt <= Item_cnt; cnt++) {
			if (On_order[cnt]) {
				q = On_order[cnt];
				if ( item == q.formNo ) {
					for ( k = 0; k < q.cnt; k++ ) {
						fidx++;
						img = root+fidx;
						smd.images[img].src = up_image[base].src
					}
				} else {
					fidx += q.cnt;
				}
			}
		}
	}
	smd.images[image].src = dn_image[mk_base(item,hot,base)].src
	var cat = hot ? "Hot_list" : "My_list";
	var cat_id = hot ? Hot_name : My_name;
	ks_dump.location.href=CgiDir+"/quickyup.cgi?dbn="+DbName+";cust_id="+cust_id+";catname="+cat+";cat_id="+cat_id+";item="+item+";i_num="+i_num+";add="+add;
}

function showImage(file, item) {   
	Page = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\r';
	Page += '<HTML><HEAD><TITLE>Larger Picture</TITLE>\r';
	Page += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"></HEAD>\r';

	var arr = file.match(/(\.gif|\.jpg|\.pdf|\.png)$/i);
	if (! arr) {
		var loc = DocDir+'/catalog/images/'+file;
		document.location.href=loc;
		return;
	} else if (arr[0].toLowerCase() == ".pdf") {
		Page += '<BODY onLoad="document.location.href=&#039;/catalog/images/'+file+'&#039;">\r';
	} else {
		Page += '<BODY><DIV ALIGN="CENTER">Item Number: '+item+'</DIV>\r';
		Page += '<IMG SRC="'+DocDir+'/catalog/images/'+file+'">';
	}

	Page += '</BODY></HTML>';
                 
	if (!Pw || Pw.closed) {
		Pw=window.open("","picture","scrollbars,status,resizable,height=550,width=600,left=10,top=10");
		if (!Pw.opener) {
			Pw.opener = window;
		}
		setTimeout("Pw.document.write(Page);Pw.document.close();", 500);
	} else {
		Pw.focus()
		Pw.document.write(Page);
		Pw.document.close();
	}
}

function showDesc(comment, desc, len) {
	if ( Dw != null && ! Dw.closed ) {
		Dw.close()
	}
	Page = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\r';
	Page +=	'<HTML><HEAD><TITLE>Item ' + desc + ' Description</TITLE>';
	Page += '<STYLE TYPE="text/css">BODY{color:Black;background-color:#FFFFCC;font-family:Arial,Helvetica,sans-serif;</STYLE>'
	Page += '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"></HEAD>\r<BODY>\r';
	Page += '<A HREF="javascript:close()"><B>Close</B></A>\r<BR>\r';
	comment = comment.replace(/&quot;/g,'"');
	Page += '<DIV STYLE=font-size:10pt>' + comment + '\r<BR>';
	if (! No_desc_email) {
		Page += '</DIV><A HREF="mailto:'+escape(distEmail)+'?subject=Instructions%20for%20Item%20'+desc+'%2C%20Req.%20No.%20'+reqNo+'"><BR>Send Special Instructions via Email</A><BR>';
	}
	Page += '</BODY></HTML>';
	Dw = window.open("","","TOOLBAR=no,WIDTH="+len+",HEIGHT=210,DIRECTORIES=no,STATUS=no,SCROLLBARS=yes,RESIZABLE=yes,MENUBAR=no");
	Dw.document.write(Page);
	Dw.document.close();
}
var Prior_permit;
function itemLookup(item,no_inventory) {
	var nd = new Date(), edate, sdate, permit_eff;
	if ((Permit & 0x3E00) == 0x3E00 ) {
		return;
	}
	if ( NoHome ) {
		alert('Item information is unavailable at this time');
		return;
	}
	edate = (nd.getMonth()+1) + '/'+ nd.getDate() + '/'+ nd.getFullYear();
	sdate = (nd.getMonth()+1) + '/1/'+ nd.getFullYear();
	permit_eff = Permit;
	if ((typeof no_inventory != "undefined") && no_inventory) {
		permit_eff |= 512;
	}
	if (Iw && ! Iw.closed && permit_eff != Prior_permit) {
		Iw.close();
	}
	Prior_permit = permit_eff;
	if (!Iw || Iw.closed) {
		var cust_id = ks_main.left_menu.document.forms[0].cust_id.value;
		var url=nonSSLCgiDir+"/lookup_inventory_info.cgi?key="+Key+";cust_id="+cust_id+";item="+item;
		url += ";permit="+permit_eff+";sdate="+sdate+";edate="+edate;
		Iw=window.open(url,"LookupItemInfo","scrollbars=yes,toolbar=yes,status=yes,resizable=yes,left=10,top=10,height=450,width=700");
	} else {
		Iw.focus()
		Iw.lii_top.document.forms[0].formNo.value=item;
		Iw.lii_top.document.forms[0].default_val.value=item;
	}
}

function show_messages(cust,login) {
	if (! takeWin()) {
		return;
	}
	if (typeof cust == "undefined") {
		cust = gCust_id;
		login = gLogin;
	}
	if (!Mw || Mw.closed) {
		var url=CgiDir+"/show_messages.cgi?key="+Key+";cust_id="+cust+";login="+login;
		Mw=window.open(url,"Messages","scrollbars,status,resizable,screenx=10,screeny=10,height=200,width=620");
	} else {
		Mw.focus()
	}
}

function highlight(rid,turn_on) {
	var r = ks_main.sub_main.document.getElementById("r"+rid);
	if (turn_on) {
		r.style.backgroundColor="#FFFF90";
	} else {
		r.style.backgroundColor="transparent";
	}
}

var reply = new String();
reply.value = "";

function search(last_what, last_value, pass_str, pass_vars) {
	var j, tcnt, url, str, prev_chr, c;
	var in_quote=false, in_single=false;
	var which = reqfile ? Review : this;

	if (! takeWin() ) return;
	
	if ( last_what == null ) last_what = "";
	if ( last_value == null ) last_value = "";
	if ( last_value == "" || last_what == "" ) {
		if (pass_str) {	
			str = pass_str;
		} else {
			str = ks_main.sub_main.prompt("Enter descriptive words separated by spaces, or items separated by commas. Use '-' to avoid, * as wildcard, '?' for help.", reply.value);
		}
		if (!str) return;
		reply.value = trim(str);
	}
	str = "";
	for (j = 0; j< reply.value.length; j++) {
		c = reply.value.charAt(j);
		switch ( reply.value.charCodeAt(j) ) {
			case 34:
				str += "%22";
				if (! in_single && reply.value.indexOf('"', j+1) != -1) {
					if (! in_quote) { // see if " is inches
						var maybe_inch = false;
						if (j > 1 && prev_chr == " ") {
							if (isNaN(reply.value.charAt(j-2))) {
								in_quote = true;
							}
						} else if (isNaN(prev_chr)) {
							in_quote = true;
						}
					} else {
						in_quote = false;
					}
				}
				break;
			case 39:
				str += "%27";
				if (! in_quote) {
					if (in_single && (j == reply.value.length -1 || reply.value.charCodeAt(j+1) == " ")) {
						in_single = false;
					} else if (! in_single && (j == 0 || prev_chr == " ")) {
						in_single = true;
					}
				}
				break;
			case 43:
				if (! in_quote && ! in_single && j > 0 && prev_chr != " ") {
					str += " ";
				}
				str += "%2b";
				break;
			case 45:
				str += "-";
				break;
			case 60: str += "%3c";
				break;
			case 62: str += "%3e";
				break;
			default: str += c;
		}
		prev_chr = c;
	}
	if (typeof which.Subs_cust == "undefined") {
		which.Subs_cust = cust_id;
	}
	url = CgiDir+"/item_lookup3.cgi?key="+Key+";cust_id="+which.Subs_cust+";login="+login+";login_cust="+gCust_id;
	url += ";pcat="+My_name+";ccat="+Hot_name+";set_qp="+Set_personal+";set_qc="+Set_company+";to_match="+str;
	if ( last_what != "" && last_value != "" ) {
		url += ";last_what="+last_what+";last_value="+last_value;
	}
	if ( pass_vars ) {
		url += ";" + pass_vars;
	}
	ks_main.sub_main.location.href = url;

	return;
}

function orderSpecItems(doc) {
	var cnt = 0, item = 0, qty = 0, f, maxItems, index, z, v;

	f=doc.forms[0];
	if (typeof f.specInst != "undefined") {
		Special_instrs = f.specInst.value;
	}
	if (typeof f.specInst1 != "undefined") {
		Special_instrs1 = f.specInst1.value;
	}
	if (typeof f.specInst2 != "undefined") {
		Special_instrs2 = f.specInst2.value;
	}
	maxItems = (f.elements.length-1) / 2;
	for (z=0; z<maxItems; z++){
		index = z*2;
		item = f.elements[index].value.toUpperCase();
		if (! isEmpty(item)) {
			v = stripWhitespace(f.elements[index+1].value);
			if (isEmpty(v) || !isInteger(v)) {
				alert('Please enter a numeric quantity for item '+item);
				f.elements[index+1].focus();
				return false;
			}
			qty = f.elements[index+1].value;
			if (specify != 'one'){
				if ( unknownItems == '' ) {
					unknownItems = skrunch(item);
				} else {
					unknownItems += ','+ skrunch(item);
				}
//formNo,i_num,formName,unit,qty_unit,price,qty,isImp,isCs,weight,ncp,cbreaks,cprices,billable,flags,pu,qty_pu,mfgr,vpart_no,color,size,style,extra,qty_limit,min_order,has_backer,file_upload,text_attach)
				putOnOrder(item,'','***Not Found***','','','',qty,0,0,0,0,'','','',0,'',0,'','','','','','','',false,'');
			} else {
				putOnOrder(item,'','','','','',qty,0,0,0,0,'','','',0,'',0,'','','','','','','',false,'');
			}
		}
	}
	return true;
}
// Called from item_details.cgi:
function replaceItem(formNo,i_num,formName,unit,qty_unit,price,flags,weight,cbreaks,cprices,pu,qty_pu,mfgr,vpart_no,color,size,style,extra,min_order,qty_limit) {
	var j, k, q, limit_warning, min_warning, skrForm = skrunch(formNo);
	for ( j = 1; j <= Item_cnt; j++ ) {
		q = On_order[j];
		if ( q && skrForm == skrunch( q.formNo) ) {
			if (flags & (1<<9) || qty_limit < 0) {
				formName = "***Do Not Order***";
			}
			q.formNo = formNo;
			q.i_num = i_num;
			q.formName = formName;
			q.unit = unit;
			q.qty_unit = qty_unit;
			for ( k = 0; k < q.cnt; k++ ) {
				q.price[k] = price;
			}
			q.weight = weight;
			q.is_imprint = (flags & 0x8000) ? 1 : 0;
			q.is_cs = (flags & 0x400) ? 1 : 0;
			q.weight = weight;
			q.ncp = flags & 0x000F;
			q.cbreaks = cbreaks;
			q.cprices = cprices;
			q.billable = (flags & 0x4000) ? 1 : 0;
			q.flags = flags;
			q.pu = pu;
			q.qty_pu = qty_pu;
			q.mfgr = mfgr;
			q.vpart_no = vpart_no;
			q.color = color;
			q.size = size;
			q.style = style;
			q.extra = extra;
			q.qty_limit = qty_limit;
			q.min_order = min_order;
			q.status = "";
			if (! (flags & (1<<9) || qty_limit < 0)) {
				limit_warning = check_item_qty_limit("", Number(q.qty[0]), Number(q.qty_limit), q.formNo);
				min_warning = check_item_min_order("", Number(q.qty[0]), Number(q.min_order), q.formNo);
				if (limit_warning) {
					q.qty[0] = q.qty_limit;
					alert(limit_warning);
				}
				if (min_warning) {
					q.qty[0] = q.min_order;
					alert(min_warning);
				}
			}
		}
	}
}
