//-----
function open_window(s_url, s_name, s_prop) {
  if (!(s_prop)) s_prop = 'status=yes,scrollbars=yes,resizable=yes,width=400,height=500'
  window.open(s_url, s_name, s_prop)
}
//-----
function copy_to_parent(s_elm, s_val) {
  eval('window.opener.frm.' + s_elm + ".value = '" + s_val + "'");
}
//-----
function confirm_action(vs_dialog, vs_href) {
  if (confirm(vs_dialog)) {
    document.location.href = vs_href;
  }
}
// return if is blank
function is_not_blank(o_ctl, s_msg) {
	if ("" == trim_str(o_ctl.value)) {
    return show_err(s_msg, o_ctl, true, true);
  }
  return true;
}
//-----
function is_valid_date(o_ctl, s_msg) {
  var b_temp =
    is_num(o_ctl[0], s_msg + ". Please enter numeric value between 1 and 31", 1, 31) &&
    is_selected(o_ctl[1], s_msg + ". Please select month") &&
    is_not_blank(o_ctl[2], s_msg + ". Please enter the year");
  if (!b_temp) return false;
  return true;
}
//-----
function is_valid_email(o_ctl, s_msg) {
  if (!(valid_email(o_ctl.value)))
  {
    return show_err(s_msg, o_ctl, true, true);
  }
  return true;
}
// return if is selected
function is_selected(o_ctl, s_msg) {
	if (o_ctl.selectedIndex == 0) {
    return show_err(s_msg, o_ctl, true, false);
  }
  return true;
}

// return if is numeric
function is_num(o_ctl, s_msg, n_low, n_high)
{
	if ("" != trim_str(o_ctl.value)) {
    if (!(is_int_in_range(o_ctl.value, n_low, n_high))) {
      return show_err(s_msg, o_ctl, true, true);
    }
  } else {
    return show_err(s_msg, o_ctl, true, true);
  }
  return true;
}

// trim string
function trim_str(s)
{
	var slen = ("" + s).length;
	for (var i = 0; i < slen && s.charAt(i) == " "; i++);
	for (var n = slen - 1; n > i && s.charAt(n) == " "; n--);
	return s.substring(i, n+1);
}
// string length
function is_str_in_range(s, lowBound, hiBound)
{
	var slen = ("" + s).length;
	return ((1 * slen >= 1* lowbound) && (1 * slen <= 1 * hiBound));
}
// integer in range
function is_int_in_range(chk, lowBound, hiBound)
{
	var chkStr = "" + chk, chkInt = parseInt(chk, 10);
	return (!isNaN(chkInt) && ("" + chkInt == chkStr) && 
    (1 * chkInt >= 1 * lowBound) && (1 * chkInt <= 1 * hiBound));
}
// floating
function is_float_in_range(chk, lowBound, highBound)
{
	var chkStr = "" + chk
	return (!isNaN(parseFloat(chk)) && ("" + parseFloat(chk) == chkStr) && (1 * parseFloat(chk) >= 1 * lowBound) && (1 * parseFloat(chk) <= 1 * highBound));
}
// valid email
function valid_email(strchkEmail)
{
	var nullChar = 0
  var normChar = 1
  var dotChar = 2
  var atChar = 3
  var dashChar = 4
  var aChar
  var currentCharType = nullChar
  var atExist = false
  var dotExist = false
  var sLen = strchkEmail.length;

	if (sLen < 7) return false;

	strchkEmail = strchkEmail.toLowerCase();

	for (var i = 0; i < sLen; i++) {
		aChar = strchkEmail.charAt(i);
		if (aChar == "@") {
			if (atExist || (i < 1) || (i > sLen - 6) || (currentCharType == dotChar)) return false;
			atExist = true;
			currentCharType = atChar;
		}
		else if (aChar == ".") {
			if ((i > sLen - 3) || (currentCharType != normChar)) return false;
			dotExist = true;
			currentCharType = dotChar;
		}
		else if ((aChar == "_") || (aChar == "-")) {
			if (i > sLen - 5 || currentCharType != normChar) return false;
			currentCharType = dashChar;
		}
		else {
			if (("abcdefghijklmnopqrstuvwxyz01234567890").indexOf(aChar) == -1) return false;
			currentCharType = normChar;
		}
	}
	return (dotExist && atExist);
}
// show error
function show_err(strErr, elmErrSrc, bFocusElm, bSelElmValue)
{
	if (bFocusElm) elmErrSrc.focus();
	if (bSelElmValue) elmErrSrc.select();
	alert(strErr);
	return false;
}

function error(s_err, o_elm, b_focus, b_select) {
   if (errfound) return;
   alert(s_err);
   if (b_focus) o_elm.focus();
	 if (b_select) o_elm.select();
   errfound = true;
}

function valid_character(s_string) {
  s_string = s_string.toLowerCase();
  var n_len = s_string.length;
  b_value = true;
  for (var n_loop=0; n_loop<n_len; n_loop++) {
    s_char = s_string.charAt(n_loop);
    if (("_abcdefghijklmnopqrstuvwxyz0123456789-").indexOf(s_char) == -1) {
		b_value = false;
		break;
	}
  }
  return b_value;
}

function expand_dom(vs_dom) {
  imgnonevis = new Image();
  imgnonevis.src = "/images/nav/icon_hide.gif";
  imgvis = new Image();
  imgvis.src = "/images/nav/icon_show.gif";
  if (vs_dom.style.display=="none") {
    vs_dom.style.display="";
    document["srchshow"].src = imgnonevis.src;
  } else {
    vs_dom.style.display="none";
    document["srchshow"].src = imgvis.src;
  }
}

function expand_elm(vs_elm) {
  if (vs_elm.style.display=="none") {
    vs_elm.style.display="";
  } else {
    vs_elm.style.display="none";
  }
}
/*
function toggle_check_all(o_fm, s_chk, s_elm) {
	var o_chk=o_fm.elements[s_chk];
	var o_elm=o_fm.elements[s_elm];
	var b_val=(o_chk.checked);

	for (var n_loop=0; n_loop<o_elm.length; n_loop++) {
		o_elm[n_loop].checked = b_val;
	}
	o_elm.checked = b_val;	
}

*/
function toggle_check_all(s_chk, s_elm) {
	var b_val=(document.frm[s_chk].checked);
	
	for (var n_loop=0; n_loop<document.frm.tot_item.value; n_loop++) {
		document.frm[s_elm + '[' + n_loop +']'].checked = b_val;
	}
}

//Format a number with 2 decimal places.
function format_decimal(u_var) {
	temp = u_var * 100;
	temp = Math.round(temp);
	var str_temp = "" + temp;
	var len = str_temp.length;
	str_temp = str_temp.substring(0,len-2) + "." + str_temp.substring(len-2,len);
	if (str_temp.charAt(str_temp.length-1)=="0") {
		str_temp = str_temp.substring(0,str_temp.length-1);
	}
	if (str_temp.charAt(str_temp.length-1)=="0") {
		str_temp = str_temp.substring(0,str_temp.length-1);
	}
	if (str_temp.charAt(str_temp.length-1)==".") {
		str_temp = str_temp.substring(0,str_temp.length-1);
	}
	
	
	return str_temp;
}


// date library
function is_month(o_ctl, s_msg, u_req) {
	var b_temp = true;
	if (u_req==1) {
		b_temp =
			is_selected(o_ctl[0], s_msg + ". Please select month") &&
			is_num(o_ctl[1], s_msg + ". Please enter valid year", 1, 1900, 2100);
	} else {
		if ((o_ctl[0].selectedIndex!=0)||(""!=trim_str(o_ctl[1].value))) {
			b_temp =
				is_selected(o_ctl[1], s_msg + ". Please select month") &&
				is_num(o_ctl[1], s_msg + ". Please enter valid year", 1, 1900, 2100);
		}
	}
	if (!b_temp) return false;
	return true;
}
//-----------------------------------------------------------------------------
function is_date(o_frm, s_elm, s_msg, u_req) {
	var b_temp = true;
	if (u_req==1) {
		b_temp = is_date_box(o_frm, s_elm, s_msg);
	} else {
		if ((""!=trim_str(o_frm.elements[s_elm+'[d]'].value))||
			(o_frm.elements[s_elm+'[m]'].selectedIndex != 0)||
			(""!=trim_str(o_frm.elements[s_elm+'[y]'].value))) {
			b_temp = is_date_box(o_frm, s_elm, s_msg);
		}
	}
	if (!b_temp) return false;
	return true;
}
//-----------------------------------------------------------------------------
function is_date_box(o_frm, s_elm, s_msg) {
	var b_temp = true;
	b_temp =
		is_num(o_frm.elements[s_elm+'[d]'], s_msg + " Masukkan tanggal antara 1 sampai 31", 1, 1, 31) &&
		is_selected(o_frm.elements[s_elm+'[m]'], s_msg + " Pilih bulan.") &&
		is_num(o_frm.elements[s_elm+'[y]'], s_msg + " Masukkan tahun yang valid", 1, 1900, 2100);
	if (!b_temp) return false;
	var n_max_date = max_date(o_frm.elements[s_elm+'[y]'].value
		,o_frm.elements[s_elm+'[m]'].value
		,o_frm.elements[s_elm+'[d]'].value)
	if (n_max_date!=0) return show_err('Tanggal maksimum untuk bulan ini adalah '+n_max_date+'.', o_frm.elements[s_elm+'[d]'], true, true);
	return true;
}
//-----------------------------------------------------------------------------
function max_date(n_y, n_m, n_d) {
	var n_m_mod = (n_m!=7)?(n_m%7):n_m;
	var n_max_day = (n_m_mod%2==1)?31:30;
	if (n_m==2) n_max_day = (n_y%4!=0)?28:((n_y%100!=0)?29:((n_y%400==0)?29:28));
	return (n_d<=n_max_day)?0:n_max_day;
}
