//$Id: imprints.js,v 1.30 2010/02/05 15:52:16 rrkramer Exp $
var Viewer_win;
var Imprint=new Object();
	Imprint.Col_num=0,Imprint.Col_desc=false;
	Imprint.UDid=new Array();
	Imprint.View_group = new Object,Imprint.View_group.val=false,Imprint.View_group.idx=-1;
	Imprint.View_login = new Object,Imprint.View_login.val=false,Imprint.View_login.idx=-1;
	Imprint.View_i_num = new Object,Imprint.View_i_num=false,Imprint.View_i_num.idx=-1;
	Imprint.SectionLbls=new Array();
	Imprint.SectionInum='', Imprint.SectionField='', Imprint.SectionFirst='', Imprint.SectionLast='', Imprint.SectionKeyIdx='';
	Imprint.UseSections=new Object();

function ad_hoc_imprints(w,tbl_id,do_edit,do_del) {
	var i,j,k,c,n,q,r,s,row,cell,buf;
	var d=w.document;
	var tbl=d.getElementById(tbl_id);
	var f=d.forms[0];
	var form_no = w.pItems[0];
	var cells = new Array();
	var sub_seq = new Array();
	var opts = '<A HREF="javascript:go2(';
	opts += "'edit','user_data_id','#ID#','user_data_backer_id','#ID#','imp_idx','#IDI#','clone','','not_add','1')";
	opts += '"><B>Edit</B></A>';
	opts += '&nbsp;|&nbsp;<A HREF="javascript:go2(';
	opts += "'edit','user_data_id','#ID#','user_data_backer_id','#ID#','imp_idx','#IDI#','clone','2','not_add','')";
	opts += '"><B>Clone</B></A>';
	if (do_del) {
		opts += '&nbsp;|&nbsp;<A HREF="javascript:confirm_delete(';
		opts += "'#ID#')";
		opts += '"><B>Delete</B></A>';
	}
	opts += '<INPUT TYPE="hidden" NAME="ID#ID#" VALUE="#IDSTR#">';

	var nf = w.pIDfields.length;
	var nb = 0;
	if (typeof w.pIDfieldsBk != "undefined") {
		nb = w.pIDfieldsBk.length;
	}
	n = nf + nb;
	r =  hasImprintInfo(form_no);
	if ( r )  {
		if (! IsMSIE) {
			tbl.style.display="table";
		} else {
			tbl.style.display="block";
		}
		q = On_order[r];
		i = c = 0;
		for (j = 0; j < q.cnt; j++) {
			if (q.db_idx[j] != q.imp_idx[j]) {
				sub_seq[i] = j;
				for (k = 0; k < nf; k++) {
					cells[c++] = q.imprint[j][w.pIDfields[k]];
				}
				for (k = 0; k < nb; k++) {
					cells[c++] = q.imprintBk[j][w.pIDfieldsBk[k]];
				}
				i++;
			}
		}
		if (c) {
			i = 0;
			s = sub_seq[0];
			var db_idx = q.db_idx[s];
			var imp_idx = q.imp_idx[s];
			for (j = 0; j < c; j++) {
				k = j % n;
				if (! k) {
					if (j) {
						cell = row.insertCell(n+1);
						buf = opts.replace(/#ID#/g, db_idx);
						buf = buf.replace(/#IDI#/g, imp_idx);
						cell.innerHTML = buf.replace(/#IDSTR#/g, makeID(q,s,true,false));
						cell.align="center";
						cell.className="gen";
						i++;
						s = sub_seq[i];
						db_idx = q.db_idx[s]
						imp_idx = q.imp_idx[s];
					}
					row =tbl.insertRow(1);
					cell = row.insertCell(0);
					cell.innerHTML = '<INPUT TYPE="text" CLASS="gen" NAME="qty'+db_idx+'_'+imp_idx+':'+form_no+'" VALUE='+q.qty[s]+' SIZE=6 MAXLENGTH=6 onChange="parent.parent.Order(this)">';
					cell.className="gen";
				}
				cell = row.insertCell(k+1);
				cell.innerHTML = (cells[j].length ? cells[j] : " ");
				cell.className="gen";
			}
			if (j) {
				cell = row.insertCell(n+1);
				buf = opts.replace(/#ID#/g, db_idx);
				buf = buf.replace(/#IDI#/g, imp_idx);
				cell.innerHTML = buf.replace(/#IDSTR#/g, makeID(q,s,true,false));
				cell.className="gen";
				cell.align="center";
			}
		}
	}
}

function asc_compare(a,b) {
	if(a.val == b.val) {
		return 0;
	}
	if(a.val.toLowerCase() > b.val.toLowerCase()) {
		return 1;
	}
	return -1;
}
  
function default_UDid(w) {
	var idx, ppi=parent.parent.Imprint;
	var f = w.document.forms[0];
	var i_num = w.pI_nums[0];
	if (ppi.SectionInum != i_num) {
		ppi.SectionInum = i_num;
		ppi.SectionField = ppi.SectionFirst = ppi.SectionLast = ppi.SectionKeyIdx = "";
		ppi.Col_num = 0;
		ppi.Col_desc = false;
		if (typeof ppi.UseSections[i_num.toString()] == "undefined") {
			ppi.UseSections[i_num.toString()] = f.use_sections.value; 
		}
	}
	if (typeof f.groupSel != "undefined" && ppi.View_group.idx != -1) {
		f.groupSel.selectedIndex = ppi.View_group.idx;
	}
	if (typeof f.loginSel != "undefined" && ppi.View_login.idx != -1) {
		f.loginSel.selectedIndex = ppi.View_login.idx;
	}
	var el = f.sectionKeysSel;
	if (typeof el != "undefined") {
		if (ppi.SectionField === "") {
			ppi.SectionField = w.document.forms[0].sectionKeysSel[0].value;
		} else {
			for (idx = 0; idx < el.options.length; idx++) {
				if (el.options[idx].value == ppi.SectionField) {
					el.options[idx].selected = true;
					break;
				}
			}
		}
		set_section_keys(w, ppi.SectionField, 'sect_sel', false);
		if (ppi.SectionFirst !== "") {
			if (ppi.SectionKeyIdx !== "") {
				el = w.document.getElementById("key:"+ppi.SectionKeyIdx);
				el.style.backgroundColor = "#C9E6AC";
			}
			if(! w.pF.length && ! w.pB.length) {
				get_section(w, ppi.SectionField, ppi.SectionFirst, ppi.SectionLast, ppi.SectionKeyIdx)
			}
		}
	}
	set_colsort(w, ppi.Col_num, true);
}

function get_ad_hoc_text(f,i_num,db_idx,imp_idx) {
	var j,buf="",fid,field,q="",imprint="",imprintBk="";
	for (j = 1; j <= Item_cnt; j++) {
		if (On_order[j].i_num == i_num) {
			q = On_order[j];
			break;
		} 
	}
	if (q) {
		for (j = 0; j < q.cnt; j++) {
			if (q.imp_idx[j] == imp_idx) {
				imprint = q.imprint[j];
				if (typeof q.imprintBk[j] != "undefined") {
					imprintBk = q.imprintBk[j];
				}
				break;
			}
		}
	}
	if (imprint) {
		write_content(f,imprint,"field_");
	}
	if (imprintBk) {
		write_content(f,imprintBk,"bfield_");
	}
	for (j = 0; j < f.elements.length; j++) {
		if (f.elements[j].name.indexOf("field_") == 0 || f.elements[j].name.indexOf("bfield_") == 0) {
			buf += f.elements[j].value;
		}
	}
	f.cksum.value = Crc32Str(buf);
}

function get_section(w,field,first,last,idx) {
	var pp=parent.parent, ppi=pp.Imprint;
	var f = w.document.forms[0];
	var el = w.document.getElementById("records");
	if (el) {
		el.innerHTML = '<H3>Loading ... <IMG SRC="/catalog/pic/loading.gif" ALT="Loading ..." TITLE="Loading ..."></H3>';
	}
	if (ppi.SectionKeyIdx !== "") {
		el = w.document.getElementById("key:"+ppi.SectionKeyIdx);
		el.style.backgroundColor = "";
	}
	el = w.document.getElementById("key:"+idx);
	if (el) {
		el.style.backgroundColor = "#C9E6AC";
	}
	ppi.SectionKeyIdx = idx;
	ppi.SectionField = field;
	ppi.SectionFirst = first;
	ppi.SectionLast = last;
	f.option.value = "select";
	f.section_field.value = "field_"+field;
	f.section_first.value = first;
	f.section_last.value = last;
	
	var args = "key="+pp.Key+";i_num="+f.i_num.value+";group="+f.group.value
	args += ";section_field=field_"+field+";section_first="+first+";section_last="+last;
	args += ";sort_col="+ppi.Col_num+";sort_desc="+(ppi.Col_desc ? "1" : "");

	var _request = pp.http_request();
	_request.open('post', CgiDir+'/imp_section.cgi', true);
	_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	_request.onreadystatechange = function() {
		if (_request.readyState == 4) {
			get_section_status(w,_request.responseText);
		}
	}
	_request.send(args);
}

function get_section_status(w,reply) {
	var j,k,ptr,arr,data = new Array(2),lines=new Array(),key_val=new Array();
	
	if (typeof reply == "undefined") {
		return false;
	}
	if (typeof reply == "string") {
		for (k in w.pF) {
			delete w.pF[k];
		}
		for (k in w.pB) {
			delete w.pB[k];
		}
		lines = reply.split("\x1E");

		for (j = 0; j < lines.length; j++) {
			data = lines[j].split("\t");
			for (k = 0; k < data.length; k++) {
				key_val = data[k].split("=");
				switch (key_val[0]) {
					case "records":
						var el = w.document.getElementById("records");
						if (el) {
							var s = (key_val[1] == 1) ? "" : "s";
							var txt = key_val[1] + " record" + s + ".";
							el.innerHTML=txt;
						}
						break;
					case "arr":
						arr = w[key_val[1]];
						break; 
					case "idx":
						arr[key_val[1]] = new Object();
						ptr = arr[key_val[1]];
						break;
					default:
						ptr[key_val[0]] = key_val[1]; 
						break;
				}
			}
		}
		set_colsort(w,parent.parent.Imprint.Col_num,true);
		repaint(w,"data");
		getQty(true);
		ad_hoc_imprints(w,"data",false);
	}
	return true;
}

function open_window(f,what,udid,udbkid,cust_id,i_num,has_face,has_bk,menu_bar) {
	if (Viewer_win) {
		Viewer_win.close();
	}
	var url = CgiDir+"/imp_main.cgi?cust_id="+cust_id+";i_num="+i_num+";key="+Key+";option="+what
	url += ";user_data_id="+udid+";user_data_backer_id="+udbkid+";has_face_fields="+has_face+";has_backer="+has_bk;
	var j,sidx;
	var re_plus = /\+/g;
	var elems = f.elements;
	for (j = 0; j < elems.length; j++) {
		if (elems[j].name.substring(0,6) == "field_" || elems[j].name.substring(0,7) == "bfield_") {
			url += ";"+elems[j].name+"=";
			switch (elems[j].type) {
			case "text":
			case "hidden":
				url += escape(elems[j].value).replace(re_plus, "%2B");
				break;
			case "select-one":
				sidx = elems[j].selectedIndex;
				if (sidx > -1) {
					url += escape(elems[j].options[sidx].value).replace(re_plus, "%2B");
				}
				break;
			}
		}
	}
	url=url.replace(/%u/g, "\\u");
	Viewer_win=window.open(url,"","height=600,width=800,menubar="+menu_bar+",resizable=1,scrollbars=yes,status=yes,toolbar=no,left=0,top=0");
	Viewer_win.focus();
}

function Order_nondb_imprint(qty,imp_idx,db_idx,fields,fieldsBk,qty_limit,min_order,has_bk) {
	var j,k,q,form_no,flags,seq,sub_seq,fld;
	var re,buf,isImp,isCs,billable,cols=0,weight="",cbreaks="",cprices="",color="",size="",style="",extra="";
	var df=ks_main.sub_main;
	var doc=df.document;
	form_no = skrunch(df.pItems[0]);
	flags = df.pFlags[0];
	isImp = (flags & 0x8000) ? 1 : 0;
	isCs = (flags & 0x400) ? 1 : 0;
	billable = (flags & 0x4000) ? 1 : 0;
	if ( df.pWbps[0] > "" ) {
		buf = df.pWbps[0].split("|");
		weight = buf[0];
		cbreaks = buf[1]+"";
		cprices = buf[2]+"";
	}
	cols = df.pFlags[0] & 0x000F;
	if (typeof df.pColors != "undefined") {
		color = df.pColors[0];
	}
	if (typeof df.pSizes != "undefined") {
		size = df.pSizes[0];
	}
	if (typeof df.pStyles != "undefined") {
		style = df.pStyles[0];
	}
	if (typeof df.pExtras != "undefined") {
		color = df.pExtras[0];
	}

	seq = putOnOrder(form_no,df.pI_nums[0],df.pForm_names[0],df.pUnits[0],df.pQty_units[0],df.pPrices[0],
				qty,isImp,isCs,weight,cols,cbreaks,cprices,billable,df.pFlags[0],df.pPus[0],
				df.pQty_pus[0],df.pMfgrs[0],df.pVpart_nos[0],color,size,style,extra,qty_limit,min_order,has_bk);
	if (seq) { // item is already there
		q = On_order[seq];
		sub_seq = get_sub_seq(q, imp_idx);
	} else {
		q = On_order[Item_cnt];
		q.IDfields = df.pIDfields;
		if (has_bk) {
			q.IDfieldsBk = df.pIDfieldsBk;
		}
	}
	if (typeof sub_seq == "undefined") {
		if (seq) {
			sub_seq = q.cnt++;
		} else {
			sub_seq = q.cnt - 1;
		}
	}
	q.imprint[sub_seq] = fields;
	if (has_bk) {
		q.imprintBk[sub_seq] = fieldsBk;
	}
	q.price[sub_seq] = q.price[0];
	q.db_idx[sub_seq] = db_idx;
	q.imp_idx[sub_seq] = imp_idx;
	q.qty[sub_seq] = Number(qty);
	q.qty_is_set[sub_seq] = true;
	return true;
}

function read_scr(f,b) {
	var j,k,idx;
	var f_ = b+"field_";
	var p = 6, addr_sel = "ship2_";
	if (b) {
		p = 7;
		addr_sel += "1";
	} else {
		addr_sel += "0";
	}
	for (j = 0; j < f.elements.length; j++) {
		if (f.elements[j].name.substr(0,p) == f_) {
			k = f.elements[j].name.substr(p);
			switch (f.elements[j].type) {
			case "text":
			case "hidden":
				this[k] = f.elements[j].value;
				break;
			case "select-one":
				idx = f.elements[j].selectedIndex;
				if (idx > -1) {
					this[k] = f.elements[j].options[idx].value;
				} else {
					this[k] = "";
				}
				break;
			}
		} else if (f.elements[j].name == addr_sel) {
			if (f.elements[j].type == "select-one") {
				idx = f.elements[j].selectedIndex;
				if (idx > -1) {
					this[addr_sel] = f.elements[j].options[idx].value;
				} else {
					this[addr_sel] = "";
				}
			}
		}
	}
}

function repaint(w,tbl_id) {
	var c,j,k,n,r,fb,db_idx,db_idx_bk,row,cell,buf,id_str,txt,dptr,login_group;
	var login="",restrict=false,mark="",sel="";
	var d=w.document; 
	var f=d.forms[0];
	var disp = f.do_edit.value ? "inline" : "none";
	var tbl=d.getElementById(tbl_id);
	var form_no = w.pItems[0];
	var opts = '<SPAN ID="ed#ID#" STYLE="display:'+disp+'"><A HREF="javascript:go2(';
	opts += "'edit','user_data_id','#ID#','user_data_backer_id','#ID#','clone','','not_add','qty#ID#:"+form_no+"')";
	opts += '"><B>Edit</B></A>';
	opts += '&nbsp;|&nbsp;</SPAN><A HREF="javascript:go2(';
	opts += "'edit','user_data_id','#ID#','user_data_backer_id','#ID#','clone','1','not_add','')";
	opts += '"><B>Clone</B></A>';
	if (f.do_del.value == "1") {
		opts += '&nbsp;|&nbsp;<A HREF="javascript:confirm_delete(';
		opts += "'#ID#')";
		opts += '"><B>Delete</B></A>';
	}
	opts += '<INPUT TYPE="hidden" NAME="ID#ID#" VALUE="#IDSTR#">';

	while (tbl.rows.length > 1) {
		tbl.deleteRow(1);
	}
	r = 1;
	var login_grp = new Array();
	if (typeof f.loginSel != "undefined") {
		sel = f.loginSel;
		login_group="login";
	} else if (typeof f.groupSel != "undefined") {
		sel = f.groupSel;
		login_group="group";
	}
	if (sel) {
		for (j = 0; j < sel.length; j++) {
			if (sel.options[j].value) {
				login_grp[sel.options[j].value] = sel.options[j].text;
			} else {
				if (login_group == "login") {
					login_grp[""] = "-- Anyone --";
				} else { 
					login_grp[0] = "-- No Group --";
				}
			}
		}
		restrict=true;
	}
	for (j = 0; j < Imprint.UDid.length; j++) {
		db_idx = Imprint.UDid[j].F;
		db_idx_bk = Imprint.UDid[j].B;
		if (typeof w.pF[db_idx] != "undefined") {
			dptr = w.pF[db_idx];
		} else if (typeof w.pB[db_idx_bk] != "undefined") {
			dptr = w.pB[db_idx_bk];
		}
		if (restrict) {
			login = '<BR><SPAN STYLE="color:darkred;">&nbsp;'+login_grp[dptr[login_group]]+'</SPAN>';
		}
		if (( ! Imprint.View_login.val || Imprint.View_login.val == dptr.login) && ( ! Imprint.View_group.val || Imprint.View_group.val == dptr.group)) {
			row =tbl.insertRow(r++);
			cell = row.insertCell(0);
			if (typeof dptr['must_look'] != "undefined") {
				if (dptr['must_look']) {
					mark = '<span style="color:#ff0033;">*</span>';
				} else {
					mark = '&nbsp';
				}
			} else {
				mark = '';
			}
			cell.innerHTML = mark+'<INPUT TYPE="text" NAME="qty'+db_idx+':'+form_no+'" VALUE="" SIZE=6 MAXLENGTH=6 onChange="parent.parent.Order(this);set_edit(\'ed'+db_idx+'\')">';
			cell.className="gen";
			n = 1;
			id_str = "";
			for (k = 0; k < w.pIDfields.length; k++) {
				c = w.pIDfields[k];
				cell = row.insertCell(n++);
				if (typeof w.pF[db_idx] != "undefined") {
					txt = w.pF[db_idx][c];
				} else {
					txt = "";
				}
				cell.innerHTML = txt+login;
				login = "";
				cell.className="gen";
				id_str += prettyID(txt,true,false);
			}
			for (k = 0; k < w.pIDfieldsBk.length; k++) {
				c = w.pIDfieldsBk[k];
				cell = row.insertCell(n++);
				if (typeof w.pB[db_idx_bk] != "undefined") {
					txt = w.pB[db_idx_bk][c];
				} else {
					txt = "";
				}
				cell.innerHTML = txt+login;
				login = "";
				cell.className="gen";
				id_str += prettyID(txt,true,false);
			}
			cell = row.insertCell(n);
			buf = opts.replace(/#ID#/g, db_idx);
			cell.innerHTML = buf.replace(/#IDSTR#/g, id_str);
			cell.className="gen";
			cell.align="center";
		}
	}
}

function set_colsort(w,col,nosort) {
	var arrow,fid,fb,c,c1,j,k,r,id,bkid,pFB,fbp;

	// UDid is an array of objects. UDid[].val is a string of values used for sorting.
	// val is the concat of the value of the selected column and up to 3 other columns
	// start from the left, excluding the selected column.
	
	// clear out any column up/down sort arrows
	for (j = 0; j < w.pIDfields.length + w.pIDfieldsBk.length; j++) {
		arrow = w.document.getElementById("UpDn"+j);
		if (arrow) {
			arrow.innerHTML = "";
		}
	}

	if (col == Imprint.Col_num && ! nosort) {
		Imprint.Col_desc = ! Imprint.Col_desc; // same column as last time
		Imprint.UDid.reverse();
	} else if (col != Imprint.Col_num || (Imprint.UseSections[Imprint.SectionInum.toString()])) {
		Imprint.Col_num = -1;  // re-sort

		var cols = new Array(); // will hold field indices, as in field_X. AKA a fid
		var cols_pFB = new Array();  // will hold ref to array of face or backer data.
		var cols_fb = new Array();
		
		if (col < w.pIDfields.length) {
			fid = w.pIDfields[col];
			fb = "F";
			fbp = w.pF;
		} else {
			fid = w.pIDfieldsBk[col - w.pIDfields.length];
			fb = "B";
			fbp = w.pB;
		}

		cols[0] = fid;
		cols_fb[0] = fb;
		cols_pFB[0] = fbp;
		
		c = 1;
		for (j = 0; j < w.pIDfields.length; j++) {
			if ( c < 3 && (w.pIDfields[j] != fid || fbp != w.pF)) {
				cols[c] = w.pIDfields[j];
				cols_fb[c] = "F";
				cols_pFB[c++] = w.pF;
				if (c == 3) {
					break;
				}
			}
		}
		if ( c < 3) {
			for (j = 0; j < w.pIDfieldsBk.length; j++) {
				if (w.pIDfieldsBk[j] != fid || fbp != w.pB) {
					cols[c] = w.pIDfieldsBk[j];
					cols_fb[c] = "B";
					cols_pFB[c++] = w.pB;
					if (c == 3) {
						break;
					}
				}
			}
		}
		delete Imprint.UDid;
		Imprint.UDid = new Array();
		// For the following, if there are face fields, then the only backers are those linked to a face.
		// If there are no face fields, all backers are fair game. 
		j = 0;
		if (w.pIDfields.length > 0) {
			pFB = w.pF;
			for (r in pFB) {
				Imprint.UDid[j] = new Object();
				Imprint.UDid[j]["F"] = r;
				if (typeof pFB[r].bkid != "undefined") {
					bkid = pFB[r].bkid;
				} else {
					bkid = "";
				}
				Imprint.UDid[j].B = bkid;
				Imprint.UDid[j].val = "";
				for (c1=0; c1<c; c1++) {
					if (cols_fb[c1] == "F") {
						id = r;
					} else {
						id = bkid;
					}
					if (typeof cols_pFB[c1][id] != "undefined") {
						Imprint.UDid[j].val += cols_pFB[c1][id][cols[c1]]+"\t";
					} else {
						Imprint.UDid[j].val += "\t";
					}
				}
				j++;
			}
		} else {
			pFB = w.pB;
			for (r in pFB) {
				Imprint.UDid[j] = new Object();
				Imprint.UDid[j]["B"] = r;
				Imprint.UDid[j].val = "";
				for (c1=0; c1<c; c1++) {
					if (typeof cols_pFB[c1][r] != "undefined") {
						Imprint.UDid[j].val += cols_pFB[c1][r][cols[c1]]+"\t";
					} else {
						Imprint.UDid[j].val += "\t";
					}
				}
				j++;
			}
		}
		if (! nosort) {
			Imprint.UDid.sort(asc_compare);
		}
		Imprint.Col_num = col;
		Imprint.Col_desc = false;
	}
	if (Imprint.UDid.length && Imprint.Col_num >= 0) {
		arrow = w.document.getElementById("UpDn"+Imprint.Col_num);
		if (arrow) {
			var arrow_chr = Imprint.Col_desc ? "&#9660;" : "&#9650;";
			arrow.innerHTML = arrow_chr;
		}
	}
	if (! nosort) {
		repaint(w,"data");
		getQty(true);
		ad_hoc_imprints(w,"data",false);
	}
}

function set_section_keys(w,field,keys_id,from_select) {
	if (field === "") {
		return;
	}
	var d=w.document, ppi=parent.parent.Imprint, k, out=new Array();
	var el = d.getElementById(keys_id);
	if (from_select) {
		ppi.SectionFirst = ppi.SectionLast = ppi.SectionKeyIdx = "";
	}
	var buttons = ppi.SectionLbls[field].split("\t");
	var n = buttons.length;
	if (n == 1) {
		el.innerHTML = "";
	} else {
		for (k = 0; k < n; k++) {
			var first_last = buttons[k].split("-");
			if (typeof first_last[1] == "undefined") {
				first_last[1] = first_last[0] + "~~~";
			} else {
				first_last[1] += "~~~";
			}
			first_last[0] = first_last[0].replace(/&#039;/g, "\\'");
			first_last[1] = first_last[1].replace(/&#039;/g, "\\'");
			out.push('<INPUT TYPE="button" NAME="s" ID="key:'+k+'" VALUE="'+buttons[k]+'" CLASS="button" STYLE="width:6.6em;" onClick="parent.parent.get_section(w,'+field+',\''+first_last[0]+'\',\''+first_last[1]+'\','+k+')">');
		}
		el.innerHTML = out.join("\r");
	}
}

function set_view(w,el,what) {
	var pp = parent.parent, ppi=pp.Imprint;
	switch (what) {
		case "login":
			ppi.View_login.val = el.value;
			ppi.View_login.idx = el.selectedIndex;
			break;
		case "i_num":
			ppi.View_i_num.val = el.value;
			ppi.View_i_num.idx = el.selectedIndex;
			break;
		case "group":
			ppi.View_group.val = el.value;
			ppi.View_group.idx = el.selectedIndex;
			break;
	}
	if (ppi.UseSections[ppi.SectionInum.toString()] == 1) {
		var f = w.document.forms[0];
		var args = "key="+pp.Key+";option=select;i_num="+f.i_num.value+";group="+f.group.value
		args += ";view_"+what+"="+el.value;
		args += ";sort_col="+ppi.Col_num+";sort_desc="+(ppi.Col_desc ? "1" : "");

		w.location.href = CgiDir + "/imp_main.cgi?" + args;
	} else {
		repaint(w,"data");
	}
}

function update_order(f,where,i_num,db_idx,imp_idx,clone,to_cart) {
	var j, nF,nB, q="", imprint="", limit_warning = "", min_warning = "";
	var buf="", cksum, qty=1, seq, sub_seq;
	var has_bk = f.backer.value == "Y" ? true : false;
	var df=ks_main.sub_main, qty_limit=999999999, min_order=0;

	if (typeof f.quantity != "undefined" && f.quantity.type != "hidden") {
		qty = check_qty(f.quantity);
		if (typeof qty == "undefined") {
			return;
		}
		if (qty <= 0) {
			alert("Not a good order quantity.");
			f.quantity.focus();
			f.quantity.select();
			return;
		}
	}
	Imp_unsaved=false;

	for (j = 1; j <= Item_cnt; j++) {
		if (On_order[j].i_num == i_num) {
			q = On_order[j];
			seq = j;
			break;
		} 
	}
	for (j = 0; j < f.elements.length; j++) {
		if (f.elements[j].name.indexOf("field_") == 0 || f.elements[j].name.indexOf("bfield_") == 0 ) {
			buf += f.elements[j].value;
		} else if (f.elements[j].name.indexOf("ship2_") == 0) {
			buf += f.elements[j].value;
		}
	}
	if ( typeof df.pQty_limits != "undefined") {
		qty_limit = Number(df.pQty_limits[0]);
	}
	if ( typeof df.pMin_ord_qtys != "undefined") {
		min_order = Number(df.pMin_ord_qtys[0]);
	}

	cksum = Crc32Str(buf);
	if (! q) { // The qty box was not filled in and item not already on the order.
		if (imp_idx == "" || f.cksum.value != cksum || clone) {
			imp_idx = --Last_imp_idx;
		} else {
			imp_idx = db_idx;
		}
		nF = new read_scr(f,"");
		if (has_bk) {
			nB = new read_scr(f,"b");
		}
		qty = f.quantity.value;
		if (qty == "" || qty == 0) {
			qty = 1;
		}
		limit_warning = check_item_qty_limit(f.quantity, "value", qty_limit, df.pItems[0]);
		if (limit_warning) {
			qty = qty_limit;
		}
		min_warning = check_item_min_order(f.quantity, "value", min_order, df.pItems[0]);
		if (min_warning) {
			qty = min_order;
		}
		Order_nondb_imprint(qty,imp_idx,db_idx,nF,nB,qty_limit,min_order,has_bk);
	} else {
		if (db_idx && clone) {
			sub_seq = get_sub_seq(q, db_idx);
			if (typeof sub_seq != "undefined") {
				qty = q.qty[sub_seq];
				if (! q.qty_is_set[sub_seq]) {
					deleteFromOrd(seq, sub_seq);
				}
			} else {
				limit_warning = check_item_qty_limit(f.quantity, "value", Number(df.pQty_limits[0]), df.pItems[0]);
				min_warning = check_item_min_order(f.quantity, "value", Number(df.pMin_ord_qtys[0]), df.pItems[0]);
				qty = f.quantity.value;
			}
		}
		if (f.cksum.value != cksum) {
			if (clone) {
				imp_idx = --Last_imp_idx;
				nF = new read_scr(f,"");
				if (has_bk) {
					nB = new read_scr(f,"b");
				}
				Order_nondb_imprint(qty,imp_idx,db_idx,nF,nB,qty_limit,min_order,has_bk);
			} else {
				if (! imp_idx) {
					for (j = 0; j < q.cnt; j++) {
						if (db_idx == q.db_idx[j] && db_idx == q.imp_idx[j]) {
							imp_idx = q.imp_idx[j];
							q.imprint[j] = new read_scr(f,"");
							if (has_bk) {
								q.imprintBk[j] = new read_scr(f,"b");
							}
							break;
						}
					}
					if (! imp_idx) {
						imp_idx = --Last_imp_idx;
						nF = new read_scr(f,"");
						if (has_bk) {
							nB = new read_scr(f,"b");
						}
						Order_nondb_imprint(qty,imp_idx,db_idx,nF,nB,qty_limit,min_order,has_bk);
					}
				} else {
					for (j = 0; j < q.cnt; j++) {
						if (q.imp_idx[j] == imp_idx) {
							q.imprint[j] = new read_scr(f,"");
							if (has_bk) {
								q.imprintBk[j] = new read_scr(f,"b");
							}
							break;
						}
					}
				}
			}
		} else {  // no change in text
			if (! imp_idx) {  // has to work, yes?
				for (j = 0; j < q.cnt; j++) {
					if (db_idx == q.db_idx[j] && db_idx == q.imp_idx[j]) {
						imp_idx = q.imp_idx[j];
						q.imprint[j] = new read_scr(f,"");
						if (has_bk) {
							q.imprintBk[j] = new read_scr(f,"b");
						}
						break;
					}
				}
				if (! imp_idx) {
					if (db_idx != "" && ! clone) {
						imp_idx = db_idx;
					} else {
						imp_idx = --Last_imp_idx;
					}
					nF = new read_scr(f,"");
					if (has_bk) {
						nB = new read_scr(f,"b");
					}
					Order_nondb_imprint(qty,imp_idx,db_idx,nF,nB,qty_limit,min_order,has_bk);
				}
			}
		}
	}
	if (limit_warning) {
		alert(limit_warning);
	}
	if (min_warning) {
		alert(min_warning);
	}
	f.imp_idx.value = imp_idx;
	if (! where) {
		where = "select";
	}
	f.option.value = where;
	if (typeof to_cart != "undefined" && to_cart) {
		show_basket();
	} else {
		f.submit();
	}
}

function write_content(f,imprint,lbl) {
	var j, fid, field;
	for (fid in imprint) {
		if (fid.search(/^\d*$/) != -1) {
			field = lbl+fid;
			if (f[field].type == "text") {
				f[field].value = imprint[fid];
			} else if (f[field].type == "select-one") {
				for (j = 0; j < f[field].length; j++) {
					if (f[field].options[j].text == imprint[fid]) {
						f[field].selectedIndex = j;
						break;
					}
				}
			}
		}
	}
}
