/* 
	Manage one to one look up list

*/
function oneToOne(o,f,r) {
	this.obj = o;
	this.foreign = f;
	this.required = r;
}
function swapOutOneToOne(fields) {
	var obj;
	var foreign;
	var inp;
	var txt;
	var hide;
	var lbl;
	var ref;
	var href;
	var image;
	var required;
	var txtWidth = '580px';
	
	for (i=0;i<fields.length;i++) {
		obj = $get('in_' + fields[i].obj);
		foreign = fields[i].foreign;
		required = fields[i].required;
		lbl = obj.options[obj.selectedIndex].text;
		ref = obj.options[obj.selectedIndex].value;
		inp = obj.parentNode;
		while (obj.childNodes[0]) {
    		obj.removeChild(obj.childNodes[0]);
		}
		obj.parentNode.removeChild(obj);
		/*
			Set up label field - displays selected items title only
		*/
		txt = document.createElement('input');
		txt.setAttribute('type','text');
		txt.setAttribute('class','formTEXT');
		txt.setAttribute('disabled','disabled');
		txt.setAttribute('name','tmp_' + fields[i].obj);
		txt.setAttribute('id','tmp_' + fields[i].obj);
		if (ref!=0) {
			txt.setAttribute('value',lbl);
		}
		/* 
			Set up link to new window
		*/
		href = document.createElement('a');
		href.setAttribute('href','#');
		href.setAttribute('class','add121');
		href.setAttribute('title','Add item');
		/*
			Setup image for link
		*/
		image = document.createElement('img');
		image.setAttribute('src','/admin/assets/gfx/search.gif');
		image.setAttribute('alt','Add item');
		/*
			Set up hidden field with value for selected item
		*/
		if (!required) {
			/* 
				Set up link to reset attachment
			*/
			href2 = document.createElement('a');
			href2.setAttribute('href','#');
			href2.setAttribute('class','remove121');
			href2.setAttribute('title','Clear association');
			/*
				Setup image for link
			*/
			image2 = document.createElement('img');
			image2.setAttribute('src','/admin/assets/gfx/icon_cross_lg.gif');
			image2.setAttribute('alt','Clear association');
		}
		/*
			Set up hidden field with value for selected item
		*/
		hide = document.createElement('input');
		hide.setAttribute('type','hidden');
		hide.setAttribute('name',fields[i].obj);
		hide.setAttribute('id','in_' + fields[i].obj);
		hide.setAttribute('value',ref);
		/*
			Start to insert our items
		*/		
		inp.appendChild(txt);
		inp.appendChild(href);
		href.appendChild(image);
		href.onclick = function() {
			newSearchWin(obj.id,foreign);
		}
		if (!required) {
			inp.appendChild(href2);
			href2.appendChild(image2);
			href2.onclick = function() {
				clearOneToOne(obj.id,foreign);
			}
			href2.style.margin = '0 0 0 1em';
			txtWidth = '560px';
		}
		inp.appendChild(hide);
		txt.style.width = txtWidth;
		href.style.margin = '0 0 0 1em';
	}
}
function addOneToOne(o) {
	var tmp = '@' + o
	var obj = p.document.getElementById(o);
	tmp = tmp.replace('@in_','tmp_');
	var tmplbl;
	var lbl = p.document.getElementById(tmp);
	var frm = $get('attachments-form');
	var rad = frm.getElementsByTagName('input');
	
	for (i=0;i<rad.length;i++) {
		if(rad[i].checked) {
			obj.value = rad[i].value;
			tmplbl = rad[i].id
			tmplbl = tmplbl.replace('attachments-form_k','attachments-form_t');
			lbl.value = $get(tmplbl).value;
		}
	}
	window.close();
	return false;
}
function clearOneToOne(o) {
	var tmp = '@' + o
		tmp = tmp.replace('@in_','tmp_');
	var obj = $get(o);
	var lbl = $get(tmp);
	
	obj.value = 0;
	lbl.value = '';
	return false;
	
}
function newSearchWin(o,f) {
		window.open('poppa.jsp?foreign=' + f + '&obj=' + o,'attachmentsWin','width=540,height=640,scrollbars=no');	
}	

/*
	End one to one process

*/
