//**********************************************************************************************************
//	scroller_utils.js
// 
// 	These JavaScript utility functions enable HTML code to open a sub windows (scrollers).
//	The scrollers can return values to the window that opened them
//	The utilities assist in the creation of the interface for opening the scrollers (flashlight icon button)
//
//**********************************************************************************************************

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	openScroller: use this function to open a scroller window
//
//	codeField: a form filed to which code information returened from the scroller is assigned (null if for no field)
//	textField: a form filed to which text information returened from the scroller is assigned (null if for no field)
//	url:			the url to be loaded in the scroller
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var mywindow

function f_exists (a_object) {
	if (typeof a_object == 'object' && a_object != null && a_object != undefined) {
		return true;
	} else {
		return false;
	}
}

function f_get_element_by_id (a_parent_element, a_element_name) {
	var l_el;

	if (a_parent_element.getElementById) {
		l_el = a_parent_element.getElementById (a_element_name);
	} 
	if (f_exists (l_el) == false) {
		l_el = a_parent_element.all [a_element_name];
	}
	return l_el;
}

function openScroller(codeField,textField,url) {
	var codeFormNum = -1;
	var codeElementNum = -1;
	var textFormNum = -1;
	var textElementNum = -1;
	var l_info_form;

    if (codeField != null) {
		for (var i = 0; i < document.forms.length; i++) {
			if (document.forms[i] == codeField.form) {
				codeFormNum = i;
				break;
			}
		}
		if (codeFormNum >= 0) {
			for (var i = 0; i < codeField.form.elements.length; i++) {
				if (codeField.form.elements[i] == codeField) {
					codeElementNum = i;
					break;
				}
			}
		}
	}

	if (textField != null) {
		for (var i = 0; i < document.forms.length; i++) {
			if (document.forms[i] == textField.form) {
				textFormNum = i;
				break;
			}
		}
		if (textFormNum >= 0) {
			for (var i = 0; i < textField.form.elements.length; i++) {
				if (textField.form.elements[i] == textField) {
					textElementNum = i;
					break;
				}
			}
		}
	}

	//alert("Code (" + codeFormNum + "," + codeElementNum + ")")
	//alert("Text (" + textFormNum + "," + textElementNum + ")")

	// yair 9\10\06 : code field is optional
	if (/* codeFormNum < 0 && */ textFormNum < 0)
		return;

//url = "scroller.htm";

	frameSetContent  = '<HTML><FRAMESET ROWS="0,100%" BORDER=0 FRAMBORDER=0><FRAME NAME="Utility">';
	frameSetContent += '<FRAME NAME="Content" SRC="' + url + '"></FRAMESET></HTML>';

	utilityFrameContent = '<HTML><BODY><FORM NAME="Info"><INPUT TYPE="HIDDEN" NAME="textForm"><INPUT TYPE="HIDDEN" NAME="textElement"><INPUT TYPE="HIDDEN" NAME="codeForm"><INPUT TYPE="HIDDEN" NAME="codeElement"></FORM></BODY></HTML>';
	var winX1 = (screen.availWidth / 2) - 300;
    var winY1 = (screen.availHeight / 2) - 200;

	//child = window.open("","Scroller","HEIGHT=60,WIDTH=600,Left="+winX1+",Top="+winY1 + ",resizable=1");
	child = window.open("","Scroller","HEIGHT=460,WIDTH=600,Left="+winX1+",Top="+winY1 + ",resizable=1");

	g_scroller_popup = child;
	child.document.write(frameSetContent);
	child.document.close();
	child.Utility.document.write(utilityFrameContent);
	child.Utility.document.close();

	l_info_form = f_get_element_by_id (child.Utility.document, "Info");

	// yair 9\10\06 : code field is optional
	if (codeFormNum >= 0) {		
		l_info_form.codeForm.value = codeFormNum;
		l_info_form.codeElement.value = codeElementNum ;
	}

	l_info_form.textForm.value = textFormNum;
	l_info_form.textElement.value = textElementNum ;
	child.document.title = name;
	child.focus();
	var winX2 = (screen.availWidth / 2) - 100;
    var winY2 = (screen.availHeight / 2) - 50;
	
	//mywindow = window.open("wait.htm","","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=200,height=100,Left="+winX2+",Top="+winY2);
	//mywindow.focus();
}

var g_scroller_whiteText, g_scroller_readOnlyOnEmptyReturn;

function openScroller_descr(textField,url, whiteText, readOnlyOnEmptyReturn) {
	textField.setAttribute ("sapir_scroller_whiteText", whiteText);
	textField.setAttribute ("sapir_scroller_readOnlyOnEmptyReturn", readOnlyOnEmptyReturn);
	
	var codeField = null;

	openScroller (codeField, textField, url);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	returnScroller: use this function to return values from a scroller to the calling window and close the scroller 
//
//	text: returened text string encoded in a !ascCode; syntex
//	code: returened code string
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function returnScroller(text,code, a_pb_dw) {
	var formNum;
	var elementNum;
	var field;
	var dataForm_name, dataForm_suffix, dataForm_suffix_pos, dw_name;
	var col_name, row_num, row_num_pos, item_val;
	var l_returned_descr_filled, l_operator ;
	
	l_returned_descr_filled = false;
	l_operator = "";

	if (a_pb_dw == '') {
		a_pb_dw = "Y";
	}

	if (parent == null) {
		alert("There is no parent for the window. Return value is: " + text);
		return;
	}
	if (parent.opener.closed) {
		alert("The opener window is closed. Return value is: " + text);
		parent.close();
		return;
	}

	formNum = getCodeForm();
	if (formNum == "") {
		formNum = null;
	}

	elementNum = getCodeElement();
	if (elementNum == "") {
		elementNum = null;
	}

	if (formNum != null && elementNum != null ) {
		if (formNum >= 0 && elementNum >= 0) {
			field = parent.opener.document.forms[formNum].elements[elementNum];

			//field.fireEvent("onfocus");

			//// yair 11\11\04 : rem on focus & click
			//field.focus();
			//field.click();
			field.value = code;

			// yair 8\12\04
			if (a_pb_dw.toLowerCase () == "y")  	{
				setDwScrollerValues (field, parent.opener, formNum);
			}

			//// yair 11\11\04 : rem on focus & click
			//field.fireEvent("onblur");
			//field.blur();
		}
	}

	formNum = getTextForm();
	elementNum = getTextElement();
	//parent.close()
	if (formNum >= 0 && elementNum >= 0) {
		field = parent.opener.document.forms[formNum].elements[elementNum];

		var whiteText = field.getAttribute ('sapir_scroller_whiteText');
		var readOnlyOnEmptyReturn = field.getAttribute ('sapir_scroller_readOnlyOnEmptyReturn');

		//// yair 11\11\04 : rem on focus & click
		//field.focus();
		//field.click();
		
		// ran
		// yair 25\2\08 : support multi values, separated by an and\or operator

		if (!l_returned_descr_filled) {
			if (typeof (g_scroller_append_selection) != "undefined") {
				if (g_scroller_append_selection) {
					if (field.value == whiteText) {
						field.value = decodeParam(text)  ;
					} else if (field.value > "") {
						if (typeof (g_scroller_append_operator) == "undefined") {
							l_operator = "OR";
						} else {
							l_operator = g_scroller_append_operator;
						}
						field.value = field.value + " " + l_operator + " " + decodeParam(text)  ;
					} else {
						field.value = decodeParam(text)  ;
					}
					l_returned_descr_filled = true;
				}
			}
		}

		if (!l_returned_descr_filled) {
			if (getCodeElement() == "" )
				{
					if (field.value == whiteText) {
						field.value = decodeParam(text)  ;
					} else if (field.value > "") {
						field.value = field.value + " או " + decodeParam(text)  ;
					} else {
						field.value = decodeParam(text)  ;
					}
			} else {
				field.value = decodeParam(text)  ;
			}
		}

		if (readOnlyOnEmptyReturn) {
			field.readOnly = true;
		}

		// yair 8\12\04
		if (a_pb_dw.toLowerCase () == "y")  	{
			setDwScrollerValues (field, parent.opener, formNum);
		}

		//// yair 11\11\04 : rem on focus & click
		//field.fireEvent("onblur");
		//field.blur();

	}

	parent.close();
}

function getTextForm() {
	var l_info_form;
	l_info_form = f_get_element_by_id (parent.Utility.document, "Info");
	return l_info_form.textForm.value;
}
function getTextElement() {
	var l_info_form;
	l_info_form = f_get_element_by_id (parent.Utility.document, "Info");
	return l_info_form.textElement.value;
}
function getCodeForm() {
	var l_info_form;
	l_info_form = f_get_element_by_id (parent.Utility.document, "Info");
	return l_info_form.codeForm.value;
}
function getCodeElement() {
	var l_info_form;
	l_info_form = f_get_element_by_id (parent.Utility.document, "Info");
	return l_info_form.codeElement.value;
}
function scrImgD(img) {
  document.images[img].src = 'images/lookup.bmp';
  return false
}
function scrImgU(img) {
  document.images[img].src = 'images/lookup.bmp';
  return false
}
function setScrollerTitle(text) {
  parent.document.title = text;
}
function decodeParam(src) {
	var pos,end_pos;
	var asc, dst;
	dst = src;

	pos = dst.indexOf("!");
	while (pos >= 0) {
		end_pos = dst.indexOf(";",pos);
		asc = dst.substr(pos + 1,(end_pos - pos) - 1);
		var temp;
		temp = dst.substr(0,pos);
		temp += String. fromCharCode(parseInt(asc));
		temp += dst.substr(end_pos + 1);
		dst = temp;
		pos = dst.indexOf("!");
	}
	
	return dst;
}

function setDwScrollerValues (aScrollerField, aOpener, aOpenerFormNum) {
	// yair 8\12\04 : SetItem in addition to setting the HTML value of the field
	// so dw buffer will contain the new value from scroller

	var dataForm_name, dataForm_suffix, dataForm_suffix_pos, dw_name;
	var col_name, row_num, row_num_pos, item_val;

	dataForm_name = aOpener.document.forms[aOpenerFormNum].name;
	dataForm_suffix = new String ("_dataForm");
	dataForm_suffix_pos = new Number (dataForm_name.lastIndexOf (dataForm_suffix));
	dw_name = new String (dataForm_name.substring (0, dataForm_suffix_pos));

	col_name = new String (aScrollerField.name);
	row_num_pos = new Number (col_name.lastIndexOf ("_") + 1);
	row_num = new Number (col_name.substring (row_num_pos, col_name.length));
	col_name = col_name.substring (0, row_num_pos - 1);

	eval ("aOpener." + dw_name + ".SetItem (row_num + 1, col_name, aScrollerField.value)");
}


var g_scroller_popup = null;
function f_scroller_fit_to_data (a_popup_win, a_width, a_height) {
	if (!a_width) {
		a_width = "600";
	}
	if (!a_height) {
		a_height = "460";
	}
	g_scroller_popup.resizeTo (a_width, a_height);
	//a_popup_win.resizeTo (a_width, a_height);
}