
/*------------------------ For ListRenderer ---------------------------*/

function gosize()
{
	g_modular_form.submit();
}

function gopage(num)
{
	g_modular_form.__pagenum.value = num;
	g_modular_form.submit();
}
		
function goselpage(ctrl)
{
	if (ctrl.selectedIndex < 0)
	{
		alert("Please select a page number.");
		ctrl.selectedIndex = 0;
		ctrl.focus();
	}
	else if (ctrl.options[ctrl.selectedIndex].value == "-1")
	{
		ctrl.selectedIndex = 0;
		ctrl.focus();
	}
	else
	{
		gopage(ctrl.options[ctrl.selectedIndex].value);
	}
}
		
function exportxls(b_addact, format)
{
	var frm, export_win;
	frm = g_modular_form;
	frm.__export_or_printable.value = (format == "xls" ? "1" : "4");
	export_win = window.open("", "export", "menubar=yes,resizable,width=400,height=" + (b_addact ? "260" : "210"));
	export_win.document.write("Preparing data...");
	frm.target = "export";
	frm.submit();
	//reset value
	frm.__export_or_printable.value = "";
	frm.target = "_self";
	return false;
}
	
function printable()
{
	var frm, print_win;
	frm = g_modular_form;
	frm.__export_or_printable.value = "3";	//single page
	//if (!confirm("Showing all can take a long time. Are you sure you want to show them all?\nClick OK to show them all, click Cancel to show just the current page."))
	//	frm.__export_or_printable.value = "2";	//current page
	print_win = window.open("", "printable", "");
	print_win.document.write("Loading...");
	frm.target = "printable";
	frm.submit();
	//reset value
	frm.__export_or_printable.value = "";
	frm.target = "_self";
	return false;
}

function chgeAllboxes(maxCount)
{
	var all, i, b_has_unchecked=false;
	all = g_modular_form.elements;
	for (i=0; i<all.length; i++)
		if (all[i].name == "__chkdelme")
			if (!all[i].checked && !all[i].disabled)
			{
				b_has_unchecked = true;
				break;
			}
			
	for (i=0; i<all.length; i++)
		if (all[i].name == "__chkdelme")
		{
			if (check_NumberOfBoxesChecked() >= maxCount && b_has_unchecked)
			{
				alert("Cannot select more records.");
				return;
			}
			else if (!all[i].disabled)
				all[i].checked = b_has_unchecked;
		}
}

function do_search()
{
	if (__validate1_1())
	{
		g_modular_form.__ren_submit_type.value = "1";	//1 means do searching on server side
		g_modular_form.submit();
	}
}		
	
function onAlpha(ch)
{
	g_OpValueCtrl4Alpha.value = ch;
	do_search();
}
	
function onClear()
{
	__clear_crit1_1();	//this is defined in criterion
	g_modular_form.__ren_submit_type.value = "1";	//1 means do searching on server side
	g_modular_form.submit();
}
	
function process_ENTER()
{
	e = window.event;
	if (e.type == "keydown" && e.keyCode == 13) 
		do_search();
}

function gosort(col, asc)
{
	var frm;
	frm = g_modular_form;
	frm.__orderby.value = col;
	frm.__isasc.value = asc;
	frm.submit();
}
	
function showfull(text)
{
	jsTools_popup("", 500, 300);
	l_popup_win.document.write(text.replace(/\n/g, "<br>"));
}

function msOver(src,clr,cls)
{ 
	if (!src.contains(event.fromElement))
	{ 
		src.style.cursor = 'hand';
		if (cls == null)
			src.bgColor = clr; 
		else if (cls.length > 0)
			src.className = cls; 
		else
		{
			src.bgColor = clr; 
			src.className = "";	//cls could previously have a value.
		}
	} 
}

function msOut(src,clr,cls)
{ 
	if (!src.contains(event.toElement))
	{ 
		src.style.cursor = 'default'; 
		if (cls == null)
			src.bgColor = clr;
		else if (cls.length > 0)
			src.className = cls; 
		else
		{
			src.bgColor = clr; 
			src.className = "";
		}
	} 
}

function msClick(src)
{
	if(event.srcElement.tagName=='TD')
		src.children.tags('A')[0].click();
}
	
function check_NumberOfBoxesChecked()
{
	var i, frm, cnt = 0;
	frm = g_modular_form;
	for (i = 0; i < frm.elements.length; i++)
		if (frm.elements[i].name == "__chkdelme")
			if (frm.elements[i].checked)
				cnt++;
					
	return cnt;
}

function save_def_view()
{
	if (confirm('If saved, the current configuration will become the default view for this page after login. Are you sure you want to save current configuration as your default view?'))
	{
		g_modular_form.__persist.value = "1";
		g_modular_form.submit();
	}
}

function restore_def_view()
{
	if (confirm('Are you sure you want to restore system default view?'))
	{
		g_modular_form.__ren_submit_type.value = "3";
		g_modular_form.submit();
	}
}
	
/*------------------------ For Criteria ---------------------------*/

function get_fmt(idx)
{
	return (g_FieldFlags[idx] & 0x1F);
}
	
function get_summable(idx)
{
	return (g_FieldFlags[idx] & 0x4000);
}
	
function get_enumonly(idx)
{
	return (g_FieldFlags[idx] & 0x80);
}
	
function get_equalonly(idx)
{
	return (g_FieldFlags[idx] & 0x40000);
}
	
function fill_oper(oper_sel, b_text, b_is_listed, b_is_enumonly, b_equal_only)
{
	var i;
	i = 0;
	while (oper_sel.options.length > 0)	//clear all existing op
		oper_sel.options[0] = null;
		
	if (b_is_listed)
	{
		oper_sel.options[i++] = new Option("Select From ->", 7);
		if (b_is_enumonly) return;	//only show one operator if enumonly
	}
		
	if (b_text)
	{
		if (b_equal_only)
		{
			oper_sel.options[i++] = new Option("Equals", 1);
			return;
		}
		oper_sel.options[i++] = new Option("Starts With", 4);
		oper_sel.options[i++] = new Option("Equals", 1);
		oper_sel.options[i++] = new Option("Contains", 5);
		oper_sel.options[i++] = new Option("Contains Any Of", 51);
		oper_sel.options[i++] = new Option("Contains All Of", 52);
		
		oper_sel.options[i++] = new Option("In", 50);
			
		if (!g_bLimited_Ops)
		{
			oper_sel.options[i++] = new Option("Greater Than", 2);
			oper_sel.options[i++] = new Option("Less Than", 3);
		}

		oper_sel.options[i++] = new Option("Not Equal", 8);
		oper_sel.options[i++] = new Option("Not Contain", 12);
		oper_sel.options[i++] = new Option("Not In", 57);
	}
	else
	{
		oper_sel.options[i++] = new Option("=", 1);
		if (b_equal_only) return;
		oper_sel.options[i++] = new Option(">", 2);
		oper_sel.options[i++] = new Option("<", 3);

		oper_sel.options[i++] = new Option(">=", 10);
		oper_sel.options[i++] = new Option("<=", 9);
		oper_sel.options[i++] = new Option("<>", 8);
		oper_sel.options[i++] = new Option("In", 50);
		oper_sel.options[i++] = new Option("Not In", 57);
	}	
}
	
function __is_to_show_enum(field_sel, oper_sel, b_operator_chgd)
{
	//if the selected column is enumerable, then use dropdown list, but only when user
	//	changed field (not operator), or changed operator to 'The Listed'.
	return g_EnumStartIndex[field_sel.value] > 0 && 
			(!b_operator_chgd || (b_operator_chgd && oper_sel.selectedIndex==0));
}
	
function upd_crit(field_sel, oper_sel, layer_edit, layer_enum, layer_cal, op_edit, enum_sel, inp_using_enum, b_operator_chgd)
{
	var idx, b_txt, flg, i;
		
	g_OpValueCtrl4Alpha = op_edit;	//to support Renderer search, so that it knows which editbox to update
		
	if (field_sel.selectedIndex == -1 || field_sel.value == 0)
	{
		//if undefined, show empty editbox and reset controls.
		fill_oper(oper_sel, true, false, false, false);
		oper_sel.selectedIndex = -1;
		layer_edit.style.display = "block";
		layer_enum.style.display = "none";
		layer_cal.style.display = "none";
		op_edit.value = "";
		inp_using_enum.value = "0";
	}
	else
	{
		idx = field_sel.value;
		flg = get_fmt(idx);
		b_txt = (flg==1 || flg==5 || flg==7);
			
		if (__is_to_show_enum(field_sel, oper_sel, b_operator_chgd))
		{
			if (!b_operator_chgd)
			{
				//if op not changed, fill in ops depending on txt/non-txt, with "The Listed" op
				fill_oper(oper_sel, b_txt, true, get_enumonly(idx), get_equalonly(idx));
				oper_sel.selectedIndex = 0;		//"The Listed ->" is the first one.
			}
			layer_edit.style.display = "none";
			layer_enum.style.display = "block";	//show enum layer
			layer_cal.style.display = "none";
			
			while (enum_sel.options.length > 0)	//remove all enum values
				enum_sel.options[0] = null;
			//add enum values for the selected field
			for (i=g_EnumStartIndex[idx]; i <= g_EnumEndIndex[idx]; i++)
			{
				//add all enumerable to the dropdown, select the selected when encountered.
				enum_sel.options[i-g_EnumStartIndex[idx]] = 
							new Option(g_EnumDispValues[i], g_EnumQueryValues[i]);
				//if the input box's value happens to be one of the enums, choose the enum item
				if (op_edit.value.toUpperCase() == g_EnumQueryValues[i].toUpperCase())
					enum_sel.selectedIndex = i - g_EnumStartIndex[idx];
			}
			//tell asp to use enum, not editbox			
			inp_using_enum.value = "1";
				
			if (g_funcShowAlpha != null)
			{
				g_bCurrentlyShowAlpha = false;
				//g_funcShowAlpha is setup for ListRenderer. for text, we show alphabetic.
				(g_funcShowAlpha)(false);
			}
		}
		else
		{
			if (!b_operator_chgd)
			{
				//if op not changed, fill in ops depending on txt/non-txt, without "The Listed" op
				fill_oper(oper_sel, b_txt, false, false, get_equalonly(idx));
				oper_sel.selectedIndex = 0;
			}
			layer_edit.style.display = "block";	//show input box layer
			layer_enum.style.display = "none";
			if (flg == 3)		//show calendar icon if the type is date
				layer_cal.style.display = "block";
			else
				layer_cal.style.display = "none";
			//tell asp not to use enum, use editbox			
			inp_using_enum.value = "0";

			var op;
			op = oper_sel.value;

			//g_bCurrentlyShowAlpha will be used to show or hide alpha layer in Renderer during initialization. see renderer. not useful in builder
			if (b_txt && (op==2 || op==3 || op==4 || op==9 || op==10))
				g_bCurrentlyShowAlpha = true;
			else
				g_bCurrentlyShowAlpha = false;
				
			if (g_funcShowAlpha != null)
				(g_funcShowAlpha)(g_bCurrentlyShowAlpha);
		}
	}
}
	
function __crit_focus(field_sel, oper_sel, op_edit, b_operator_chgd)
{
	//if it's not enum, set focus to operant edit
	if (field_sel.selectedIndex >= 0 && !__is_to_show_enum(field_sel, oper_sel, b_operator_chgd))
	{
		op_edit.select();
		op_edit.focus();
	}	
}
	
function __onFieldChg(field_sel, oper_sel, layer_edit, layer_enum, layer_cal, op_edit, enum_sel, inp_using_enum)
{
	//show prompt per data type
	switch(get_fmt(field_sel.value))
	{
		case 3:
			op_edit.value = "mm/dd/yyyy"; break;
		case 4:
			op_edit.value = "$"; break;
		case 6:
			op_edit.value = "%"; break;
		default:
			op_edit.value = ""; break;
	}
				
	upd_crit(field_sel, oper_sel, layer_edit, layer_enum, layer_cal, op_edit, enum_sel, inp_using_enum, false);
	//during initialization, cannot set focus in that the criterion layer can be invisible altogether. must set focus onxxxchange
	__crit_focus(field_sel, oper_sel, op_edit, false);
}
	
function __onOpChg(field_sel, oper_sel, layer_edit, layer_enum, layer_cal, op_edit, enum_sel, inp_using_enum)
{
	upd_crit(field_sel, oper_sel, layer_edit, layer_enum, layer_cal, op_edit, enum_sel, inp_using_enum, true);
	//during initialization, cannot set focus in that the criterion layer can be invisible altogether. must set focus onxxxchange
	__crit_focus(field_sel, oper_sel, op_edit, true);
}

function __chkOpValue(field_sel, oper_sel, op_edit)
{
	//validate op_edit per field data type
	// we allow field to be empty. that way, no criterion will be creatd.
	if (field_sel.selectedIndex == -1) return true;
		
	if (oper_sel.value != 8 && oper_sel.value != 1)		//if 1 (equal)/8 (not equal), the op_edit can be blank, meaning "blank/not blank"
	{
		if (op_edit.value.length == 0)
		{
			alert("The value field cannot be blank.");
			op_edit.focus();
			return false;
		}
	}
	else
		if (op_edit.value.length == 0) return true;

	switch(get_fmt(field_sel.value))
	{
		case 2: return checkFloat(op_edit);
		case 3: return validDate(op_edit.value, op_edit, null, null);
		case 4: return checkCurrency(op_edit);
		case 6: return checkPercentage(op_edit);
	}
	
	return true;
}

function __onAddCrit(group)
{
	var frm;
	frm = g_modular_form;
	frm.__crit_submit_type.value = 1;	//set proper submit_type per action
	frm.__crit_group_index.value = group;
	frm.submit();
}
	
function __onRemoveCrit(group, idx)
{
	var frm;
	frm = g_modular_form;
	frm.__crit_submit_type.value = 2;	//set proper submit_type per action
	frm.__crit_group_index.value = group;
	frm.__crit_index.value = idx;
	frm.submit();
}
	
function __onAddGroup(builder_name)
{
	var frm;
	frm = g_modular_form;
	frm.__crit_builder.value = builder_name;
	frm.__crit_submit_type.value = 3;	//set proper submit_type per action
	frm.submit();
}
