function frame_width(f,w)
{
  var mainTable = f.mainTable;
  if ( mainTable ) {
    return mainTable.clientWidth < w ? mainTable.clientWidth : w; 
  }
  return 0;
}

function frame_height(f,h)
{
  var mainTable = f.mainTable;
  if ( mainTable ) {
    return mainTable.clientHeight < h ? mainTable.clientHeight : h;
  }
  return 0;
}

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, size, other_features)
{
	if (size == null)
		size = "width=600,height=400";
	if (other_features == null)
		other_features = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var popup = window.open(url, title, size+","+other_features);
	if (popup != null)
		popup.focus();
	return popup;
}


function open_pdf(url, title, size, other_features)
{
	if (size == null)
		size = "width=800,height=600";
	if (other_features == null)
		other_features = "top=0,left=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var popup = window.open(url, title, size+","+other_features);
	if (popup != null)
		popup.focus();
}

function popUp(url, title, size, other_features)
{
	if (size == null)
		size = "width=800,height=600";
	if (other_features == null)
		other_features = "top=0,left=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var popup = window.open(url, title, size+","+other_features);
	if (popup != null)
		popup.focus();
}

function open_select_form(url, title, element)
{
	window.src_element = element;
	var popup = window.open(url, title, "width=460,height=280,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
	if (popup != null)
		popup.focus();
	return popup;
}

function delete_confirm()
{
	return confirm("Are you sure to delete selected record?");
}

function findNext(node, tag, n)
{
  var e = node.nextSibling;
  while ( e != null ) {
    if ( e.tagName == tag ) {
      if ( --n == 0 )
        return e;
    }
    e = e.nextSibling;
  }
  return null;
}


function checkSingleSelect(check_me){
    if(isMultiSelect)
        return ;
    if(check_me.checked==false) {
      opener.src_element.value = "";
      //opener.src_element.nextSibling.value = "";
      findNext(opener.src_element, "INPUT", 1).value = "";
      return ;
    } else {
      opener.src_element.value = check_me.value;
      //opener.src_element.nextSibling.value = findNext(check_me.parentNode, "TD", 2).innerText;
      findNext(opener.src_element, "INPUT", 1).value = findNext(check_me.parentNode, "TD", 2).innerText;
    }
    
	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 ( isMultiSelect && 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 clearOrderBy(f)
{
  var isChange = false;
  var inps = f.all.tags("INPUT");
  if ( inps != null ) {
    for(var i=0; i<inps.length; i++) {
      var inp = inps[i];
      if ( inp.type == "hidden" && inp.name.match(/^_\./) && inp.value.match(/./) ) {
        inp.value = "";
        isChange = true;
      }
    }
  }
  if ( isChange )
    f.submit();
  return false;
}

function setIndexedOrderBy(f, n)
{
  var ndxMax = 0;
  var inps = f.all.tags("INPUT");
  if ( inps != null ) {
    for(var i=0; i<inps.length; i++) {
      var inp = inps[i];
      if ( inp.type == "hidden" && inp.name.match(/^_\./) ) {
        var dd = inp.value.match(/^(\d*),/);
        if ( dd != null ) {
          var sn = parseInt(dd[1],10);
          if ( !isNaN(sn) && sn > ndxMax )
            ndxMax = sn;
        }
      }
    }
  }
  ndxMax++;

	var e = f["_."+n];
  var v = e.value;
	if ( v == "" ) {
		e.value = String(1000+ndxMax).substring(1) + ",ASC";
	} else if ( v.match(/^\d*,ASC$/) ) {
		e.value = v.substring(0,v.indexOf(','))+",DESC";
	} else {
		e.value = "";
    var tn = v.match(/^(\d*),/);
    if ( tn != null && inps != null ) {
      var tnv = parseInt(tn[1],10);
      for(var i=0; i<inps.length; i++) {
        var inp = inps[i];
        if ( inp.type == "hidden" && inp.name.match(/^_\./) ) {
          var dd = inp.value.match(/^(\d*)(,.*)$/);
          if ( dd != null ) {
            var sn = parseInt(dd[1],10);
            if ( !isNaN(sn) && sn > tnv ) {
              inp.value = String(1000+sn-1).substring(1) + dd[2];
            }
          }
        }
      }
    }
	}
	f.submit();
	return false;
}

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 checkInputSize(obj){
      //if (LengthCheck(obj,obj.maxLength)){
	  // TEXTAREA HAVE NO "maxLength"
	  // USE IE features which support custom tag attributes
	  
      var max_length;
      if (obj.type == "text"){
      	  max_length = obj.maxLength;
      } else {
      	  max_length = obj.maxlength;
      }
      if (LengthCheck(obj,Number(max_length))){
         obj.select();
         alert("This field is over the max. content length("+Number(max_length)+")!");
      }
  }

  function LengthCheck(text,size) {
	var len = count_char(text.value);
	if ( len > size ) {
           return true;
	}
	return false;
  }

  function count_char(str) {
        var len = 0;
        for(i=0;i<str.length;i++) {
                var ech = escape(str.charAt(i));
                if ( ech.length > 4 ){
                        len++;
                        if (ech>"%u07ff")
                                len++;
                }
                len++;
        }
        return len;
  }

function invalid_data(f,m)
{
  alert(m);
  f.focus();
  return false;
}

function check_string(f,l,m,re)
{
  if ( re ) {
 	if (f.length && !f.type){
  		var isChecked = false;
  		for (var i=0; i< f.length; i++){
  			if (f[i].checked){
  				isChecked = true;
  				break;
  			}
  		}
  		if (!isChecked)
  			return invalid_data(f[0],m);
  		else
  			return true;
	} else {
	    if ( !re.test(f.value) ) {
	      return invalid_data(f,m);
	    }
	 }
  }
  if (!f.length){
	  //if ( f.value.length > l ) {
	  if ( LengthCheck(f,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)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  
  if ( f.value.match(/./) != null ) {
    var dateValue = f.value;
    var dateFormat = df.toUpperCase();
    var date = myPraseDate(dateValue, dateFormat);
    if ( date == null || dateValue != formatDate(date, dateFormat) ) {
      return invalid_data(f, m );
    }
  }
  return true;
}

function myPraseDate(dateValue,dateFormat)
{
  var y,m,d;
  switch( dateFormat ) {
  case "YYYY/MM/DD":
    y = parseInt(dateValue.substring(0,4),10);
    m = parseInt(dateValue.substring(5,7),10)-1;
    d = parseInt(dateValue.substring(8,10),10);
    break;
  case "MM/dd/yyyy":
    m = parseInt(dateValue.substring(0,2),10)-1;
    d = parseInt(dateValue.substring(3,5),10);
    y = parseInt(dateValue.substring(6,10),10);
    break;
  case "dd/MM/yyyy":
    d = parseInt(dateValue.substring(0,2),10);
    m = parseInt(dateValue.substring(3,5),10)-1;
    y = parseInt(dateValue.substring(6,10),10);
    break;
  default:
    //alert(dateFormat);
    return null;
    //break;
  }
  if ( isNaN(y) || isNaN(m) || isNaN(d) )
    return null;
  var date = new Date(y,m,d);
  return date;  
}

function formatDate(tm, df)
{
  if ( df == null ) {
    df = gDateFormat;
  }
  var yyyy = tm.getFullYear();
  var yy = String(tm.getYear()+100).substring(1);
  var MM = String(tm.getMonth()+101).substring(1);
  var dd = String(tm.getDate()+100).substring(1);
  var dStr = df.replace("YYYY", yyyy);
  dStr = dStr.replace("YY", yy);
  dStr = dStr.replace("MM", MM);
  dStr = dStr.replace("DD", dd);
  return dStr;
}

function check_INT(f, m)
{
  return true;
}

function check_BIGINT(f, m)
{
  return true;  
}

function check_NVARCHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function loadCheckboxValue(inputName,prefix)
{
	if (prefix == null)
		prefix = "";
   var value_obj = opener.document.forms[0][prefix+"_"+inputName];
   if (value_obj != null && value_obj.value != ""){
   		var value = ","+value_obj.value+",";
		var obj = document.forms[0]["_"+inputName];
		if (obj != null){
			if(obj.length) {
				for (var i=0; i<obj.length; i++){
					if ( value.indexOf(",'"+obj[i].value+"',") != -1) 
					{
						obj[i].checked = true;
					} 
				}
			} else {
				if ( value.indexOf(",'"+obj.value+"',") != -1) 
				{
					obj.checked = true;
				} 
			}
		}
	}
}

function setCheckboxValue(inputName,prefix)
{
	if (prefix == null)
		prefix = "";
    var A = new Array();
	var obj = document.forms[0][prefix+"_"+inputName];
	if (obj != null){
		if(obj.length) {
			for (var i=0; i<obj.length; i++){
				if (obj[i].checked) 
				{
					add_element(A,"'"+obj[i].value+"'");
				} 
			}
		} else {
			if (obj. checked) 
			{
				add_element(A,"'"+obj.value+"'");
			} 
		}
	}
	opener.document.forms[0][prefix+"_"+inputName].value = A.join(",");
	//alert(A.join(","));
}

function isValidEmail(email){
       var v_email = email.value
       var v_email2 = email.value.substr(email.value.indexOf("@") + 1)
       if ( (v_email.indexOf("@") == -1) ||
                 (v_email.indexOf("@") == 0)  ||
                  (v_email.indexOf("@") != v_email.lastIndexOf("@")) ||
                    (v_email2.indexOf(".") == -1)  ||  
                      (v_email2.indexOf(".") == 0) ||
                        ((v_email.indexOf(".") + 1) == v_email.length) )
       {
       	    return false;
       }
       return true;
}