function set_focus()
{
	var childs = document.all;
	for(i=0; i<childs.length; i++)
	{
		var child = childs(i);
		if ( child.tagName == "INPUT" && child.type != "hidden" && !child.disabled && !child.readOnly) {
			child.focus();
			return;
		}
		if ( child.tagName == "SELECT" && !child.disabled && !child.readOnly) {
			child.focus();
			return;
		}
		if ( child.tagName == "TEXTAREA" && !child.disabled && !child.readOnly) {
			child.focus();
			return;
		}
	}
}

function do_resize()
{
	if ( opener ) {
		var ww = mainTable.clientWidth; 
		if ( ww > 1000 ) {
			ww = 1000;
			divContent.style.width = 900;
			divContent.style.overflow = "scroll";
		}
		var w =  ww - document.body.clientWidth + document.body.clientLeft;
		window.resizeBy(w,0);
		var hh = mainTable.clientHeight; 
		if ( hh > 640 ) {
			window.resizeBy(20,0);
			hh = 640;
			divContent.style.width = ww + 20;
			divContent.style.height = 590;
			divContent.style.overflow = "scroll";
		}
		var h =  hh - document.body.clientHeight + document.body.clientTop;
		window.resizeBy(0,h);
	}
}

function open_input_form(url, title)
{
	window.open(url, title, "width=600,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
}


function open_select_form(url, title, element)
{
	window.src_element = element;
	window.open(url, title, "width=460,height=280,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
}

function delete_confirm()
{
	return confirm("Are you sure to delete selected record?");
}

function checkSingleSelect(check_me){
    if(isMultiSelect)
        return ;
    if(check_me.checked==false)
        return ;
	var inp = document.all.tags("INPUT");
	for(var i=0; i<inp.length; i++)
	{
		if( inp[i].type == "checkbox" && inp[i]!=check_me)
		{
			if(inp[i].checked){
			    inp[i].checked=false;
			    return;
			}
	    }
	}
    
}

function checkbox_init(key)
{
	var A = key.split(",");
	var inp = document.all.tags("INPUT");
	for(var i=0; i<inp.length; i++)
	{
		if( inp[i].type == "checkbox" )
		{
			var id = inp[i].value;
			for(var j=0; j<A.length; j++)
			{
				if ( id == A[j] ) 
				{
					inp[i].checked = true;
				}
			}
		}
	}
}

function document_loaded()
{
	if ( document.all.bnDelete ) 
	{
		document.all.bnDelete.onclick = delete_confirm;
	}
	if ( opener && opener.src_element ) 
	{
		if ( opener.src_element.value != "" )
		{
			var A = opener.src_element.value.split(",");
			var inp = document.all.tags("INPUT");
			for(var i=0; i<inp.length; i++)
			{
				if( inp[i].type == "checkbox" )
				{
					var id = inp[i].name.substring(4);
					for(var j=0; j<A.length; j++)
					{
						if ( id == A[j] ) 
						{
							inp[i].checked = true;
						}
					}
				}
			}
		}
	}
}

function add_element(A, key)
{
	var exist = false;
	for(var i=0; i<A.length; i++)
	{
		if ( A[i] == key )
		{
			exist = true;
			break;
		}
	}
	if ( !exist ) 
	{
		A[A.length] = key;
	}
}

function remove_element(A, key)
{
	var B = new Array();
	for(var i=0; i<A.length; i++) {
		if ( A[i] != key ) {
			B[B.length] = A[i];
		}
	}
	return B;
}

function select_save()
{
        
	if ( opener && opener.src_element ) 
	{

		var A = new Array();
		//if ( isMultiSelect && opener.src_element.value != "" ) 
		if ( opener.src_element.value != "" ) 
		{
			A = opener.src_element.value.split(",");
		}
		var inp = document.all.tags("INPUT");
		for(var i=0; i<inp.length; i++)
		{
			if( inp[i].type == "checkbox" )
			{
				var id = inp[i].value;
				if ( inp[i].checked ) 
				{
					add_element(A,id);
				} 
				else 
				{
					A = remove_element(A,id);
				}
			}
		}
		opener.src_element.value = A.join(",");
	}
	//self.close();
}

function xmlEncode(value)
{
	var A = new Array();
	A = value.split("&");
	str = A.join("&amp;");
	A = str.split("<");
	str = A.join("&lt;");
	A = str.split(">");
	str = A.join("&gt;");
	A = str.split("\"");
	str = A.join("&quot;");
	A = str.split("\'");
	str = A.join("&apos;");
	return str;
}

function getSingleControlValue(obj)
{
	if ( obj.tagName == "INPUT" && obj.type == "checkbox" ) {
		if ( obj.checked == true )
			obj.value = 'Y';
		else
			obj.value = 'N';
	}
	return obj.value;
}

function getControlValue(obj)
{
	if ( obj != null ) {
		if ( obj.tagName ) {
			return getSingleControlValue(obj);
		} else {
			if ( obj.length ) {
				for (var i=0; i<obj.length; i++) {
					if ( obj[i].tagName == "INPUT" ) {
						if ( obj[i].type == "radio" ) {
							if ( obj[i].checked ) {
								return obj[i].value;
							}
						} else 
						if ( obj[i].type == "checkbox" ) {
							if ( obj[i].checked ) {
								return obj[i].value;
							}
						}
					}
				}
			}
		}
	}
}

function getXML(obj, tag)
{
	var strXml = "";
	if ( obj != null ) {
		var v = getControlValue(obj);
		if ( v != null && v != '' ) {
			strXml = "<" + tag + ">";
			strXml += xmlEncode(v);
			strXml += "</" + tag + ">";
		}
	}
	return strXml;
}

function getBooleanXML(obj, tag)
{
	var strXml = "";
	if ( obj != null ) {
		var v = getControlValue(obj);
		strXml = "<" + tag + ">";
		strXml += (v == "Y") ? "true" : "false";
		strXml += "</" + tag + ">";
	}
	return strXml;
}

function getDateXML(obj, tag)
{
	
}

function getDetailXML(obj, tag, tag2)
{
	var strXml = "";
	if ( obj != null ) {
		if ( obj.tagName ) {
			if ( obj.value != '' ) {
				var keys = new Array();
				keys = obj.value.split(",");
				for(var i=0; i<keys.length; i++)
				{
					strXml += "<" + tag + "><" + tag2 + ">" + keys[i] + "</" + tag2 + "></" + tag + ">";
				}
			}
		} else {
			if ( obj.length ) {
				for(var j=0; j<obj.length; j++) {
					if ( obj[j].tagName == "INPUT" ) {
						if ( obj[j].type == "checkbox" ) {
							if ( obj[j].checked ) {
								strXml += "<" + tag + "><" + tag2 + ">" + obj[j].value + "</" + tag2 + "></" + tag + ">";
							}							
						}
					} 
				}
			}
		}
	}
	return strXml;
}

function input_form_init(f, query)
{
	var A = new Array();
	A = query.split("&");
	for(var i=0; i<A.length; i++)
	{
		if ( A[i].charAt(0) == '_' )
		{
			var index = A[i].indexOf("=");
			if ( index != -1 )
			{
				var ename = A[i].substring(0,index);
				var e = f[ename];
				if ( e != null ) {
					var v = A[i].substring(index+1);
					e.value = v;
					e.disabled = true;
				}
			}
		}
	}
	
}

function setOrderBy(f, n)
{
	var e = f["_."+n];
	if ( e.value == "" ) {
		e.value = "ASC";
	} else if ( e.value == "ASC" ) {
		e.value = "DESC";
	} else {
		e.value = "";
	}
	f.submit();
	return false;
}

function invalid_data(f,m)
{
  alert(m);
  f.focus();
  return false;
}

function check_string(f,l,m,re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  if ( f.value.length > l ) {
    return invalid_data(f,m);
  }
  return true;
}

function check_VARCHAR2(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_VARCHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_CHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_NUMBER(f,l,m,re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  var exp = new RegExp("^\\d{0," + l + "}$");
  if ( ! exp.test(f.value) ) {
    return invalid_data(f,m);
  }
  return true;
}

function check_CLOB(f,l,m,re)
{
  return true;
}

function check_DATE(f,df, m, re)
{
  return true;
}
