/*****************************************************************************************
* checking installed OWC at client
*
* 2003-08-27 10:29 <pardo@konsep.net>
*****************************************************************************************/
function Check_OWC() {
  try {
    var dsc= new ActiveXObject("owc10.datasourcecontrol.10");
    if ( dsc.MajorVersion==10 & parseInt(dsc.BuildNumber)>=4109) {
      fnShowControls();
      return true
    } else {
      alert('Although you have MicroSoft Office XP Web Components installed heeft, your system does not have the proper version.\nYour version is:'+dsc.Version+'\nPlease contact your support rep. to install the latest message(min. : 10.0.0.4109)');
      return false;
    }
   }

  catch(e) {
    if ((e.number & 0xFFFF) == 429) // default error thrown when you don't have OWC at all
    {
      alert('Your system needs MicroSoft Office XP Web Components installed.Please contact your support rep. to have it installed ');
      return false;
    } else {
      alert("A unforeseen error has occurred when trying to test for availabiblity of MS Office XP Web Components, here is all avaible error information: \n\nerrornumber: "+(e.number & 0xFFFF)+"\nerrormessage:"+e.description);
      return false;
    }
  }
}

/*****************************************************************************************
* Valid multiple input (radio button or check boxes)
*
* @created	2003-05-20 20:38 <ivan@konsep.net>
*****************************************************************************************/
function valid_inp_multi(ro_fm, vs_name, vs_msg, vb_auto) {
	var n_loop = ro_fm.elements[vs_name].length;
	for (i = 0; i < n_loop; i++) {
		if (ro_fm.elements[vs_name][i].checked) {
			return true;
		}
	}
	alert(vs_msg);
	if (vb_auto) {
		ro_fm.elements[vs_name][0].checked = true;
		ro_fm.elements[vs_name][0].focus();
	}
	return false;
}

/*****************************************************************************************
* Show tab
*
* @created	2003-04-12 12:45 <ivan@konsep.net>
*****************************************************************************************/
function show_tab(vn_idx, vn_count) {
	var o_tab = document.getElementById('tab' + vn_idx + '');
	if (o_tab != null) {
		o_tab.style.display = '';
		for (var i = 0; i <= vn_count; i++) {
			var o_tab = document.getElementById('tab' + i + '');
			if (o_tab != null && i != vn_idx) o_tab.style.display='none';
		}
	}
}

/*****************************************************************************************
* Trapping routing for F2
*
* @created	2003-03-29 18:05 <ivan@konsep.net>
*****************************************************************************************/
function show_popup_onkey(vs_type, vs_name, vn_idx) {
	n_key = event.keyCode;
	if (n_key == 113) { // F2
		show_popup(vs_type, 'elm=' + vs_name + '&amp;idx=' + vn_idx + '');
		event.returnValue = false;
		return false;
	}
}


/*****************************************************************************************
* Copy code
*
* @created	2003-03-29 18:05 <ivan@konsep.net>
*****************************************************************************************/
function copy_code(vs_elm, vn_idx, vs_val1) {
	var vs_elm = '' + vs_elm + '';
	vs_elm = vs_elm + ((vn_idx != '') ? '[' + vn_idx + ']' : '');
	eval('window.opener.frm.' + vs_elm + '.value=\'' + vs_val1 + '\'');
	eval('window.opener.frm.' + vs_elm + '.focus()');
	window.close();
}

/*******************************************************************************
* Show popup
*
* @updated	2002-12-31 13:41 <ivan@konsep.net>
* @created	2002-12-31 13:41 <ivan@konsep.net>
*******************************************************************************/
function show_popup(vs_type, vs_prop) {
	var n_w = 500;
	var n_h = 500;
	switch (vs_type) {
		case 'supp':
			var s_url = BASE_DIR + '/popup/popup_adr.asp?t=3&amp;' + vs_prop + '';
			var s_name = 'pop_supp';
			break;
		case 'cust':
			var s_url = BASE_DIR + '/popup/popup_adr.asp?t=2&amp;' + vs_prop + '';
			var s_name = 'pop_cust';
			break;
		case 'prod':
			var s_url = BASE_DIR + '/popup/popup_prod.asp?' + vs_prop + '';
			var s_name = 'pop_prod';
			break;
		case 'sit':
			var s_url = BASE_DIR + '/popup/popup_sit.asp?' + vs_prop + '';
			var s_name = 'pop_sit';
			break;
	}
	var s_prop = 'scrollbars = yes, resizable = yes'
	s_prop += ',width = ' + n_w + ', height = ' + n_h + '';
	window.open(s_url, s_name, s_prop);
}
/*******************************************************************************
* Open calendar
*
* @updated	2002-12-31 13:41 <ivan@konsep.net>
* @created	2002-12-31 13:41 <ivan@konsep.net>
*******************************************************************************/
function popup_cal(vs_pop, vs_d, vs_elm) {
	var n_width = 250;
	var n_height = 280;
	var n_left = (window.screenLeft + event.clientX) - (n_width / 2);
	var n_top = (window.screenTop + event.clientY) - (n_height / 2);
	var s_url = vs_pop + '?d=' + vs_d + '&amp;elm=' + vs_elm + '';
	var s_name = 'pop_cal';
	var s_prop = 'scrollbars=no,resizable=no,width=' + n_width + ',height=' + n_height + ',left=' + n_left + ',top=' + n_top + '';
	window.open(s_url, s_name, s_prop);
}

/*****************************************************************************************
* Toggle check mark
*
* @created	2002-09-14 16:45 <ivan@konsep.net>
*****************************************************************************************/
function toggle_check_all(o_fm, s_chk, vs_elm) {
	var o_chk=o_fm.elements[s_chk];
	var o_elm=o_fm.elements[vs_elm];
	var b_val=(o_chk.checked);
	for (var n_loop=0; n_loop<o_elm.length-1; n_loop++) {
		o_elm[n_loop].checked = b_val;
	}
}

//-----------------------------------------------------------------------------
function relevel_list(o_list, b_add, s_alert) {
	var s_sep = '- ';
	if (!s_alert) s_alert = "Please select the list first";
	n_idx = o_list.selectedIndex;
	if (n_idx != -1) {
		var s_new = '';
		var s_txt = o_list.options[n_idx].text;
		var n_cnt = 0;
		var n_pos = s_txt.lastIndexOf(s_sep) + 2;
		var n_level = Math.floor(n_pos / 2);
		if (n_pos == 1) n_pos = n_pos - 1; // for zero level
		n_level = (b_add == 0) ? (n_level - 1) : (n_level + 1);
		if (n_level < 0) {
			s_new = s_txt;
		} else {
			s_new = s_txt.substr(n_pos);
			for (i = 0; i < n_level; i++) {
				s_new = s_sep + s_new;
			}
		}
		o_list.options[n_idx].text = s_new;
		o_list.selectedIndex = n_idx;
	} else {
		alert(s_alert);
		o_list.selectedIndex = 0;
	}
}

//-----------------------------------------------------------------------------
function reorder_list(o_list, b_down, s_alert) {
	if (!s_alert) s_alert = 'Please select the list first';
	n_idx = o_list.selectedIndex;
	if (n_idx != -1) {
		s_txt = o_list.options[n_idx].text;
		u_val = o_list.options[n_idx].value;
		if (n_idx > 0 && b_down == 0) {
			o_list.options[n_idx].text = o_list.options[n_idx-1].text;
			o_list.options[n_idx].value = o_list.options[n_idx-1].value;
			o_list.options[n_idx-1].text = s_txt;
			o_list.options[n_idx-1].value = u_val;
			o_list.selectedIndex--;
		} else if (n_idx < o_list.length-1 && b_down == 1) {
			o_list.options[n_idx].text = o_list.options[n_idx+1].text;
			o_list.options[n_idx].value = o_list.options[n_idx+1].value;
			o_list.options[n_idx+1].text = s_txt;
			o_list.options[n_idx+1].value = u_val;
			o_list.selectedIndex++;
		}
	} else {
		alert(s_alert);
		o_list.selectedIndex = 0;
	}
}
//-----------------------------------------------------------------------------
function select_list(o_list, o_val) {
	var n_len = o_list.length;
	var s_tmp = '';
	for (var i = 0; i < n_len; i++) {
		s_tmp += o_list.options[i].value;
		if (i < n_len-1) s_tmp += ',';
	}
	o_val.value = s_tmp;
	return true;
}
//-----------------------------------------------------------------------------
function select_list_depth(o_list, o_val) {
	var n_len = o_list.length;
	var s_tmp = '';
	for (var i = 0; i < n_len; i++) {
		s_tmp += o_list.options[i].value + ':' + get_sep_level(o_list.options[i].text);
		if (i < n_len-1) s_tmp += ',';
	}
	o_val.value = s_tmp;
	return true;
}
//-----------------------------------------------------------------------------
function get_sep_level(vs_val) {
	var s_sep = '- ';
	var n_pos = vs_val.lastIndexOf(s_sep) + 2;
	var n_level = Math.floor(n_pos / 2);
	return n_level;
}

/*****************************************************************************************
* Show modal window - IE4 up only!
*
* @created	2002-08-30 12:37 <ivan@konsep.net>
*****************************************************************************************/
function modal_window(s_url, n_x, n_y) {
	if (!n_x) n_x=360;
	if (!n_y) n_y=240;
	b_IE5 = true;
	n_y+=(b_IE5)?20:40;
	window.showModalDialog(s_url,'','dialogHeight:'+n_y+'px; dialogWidth:'+n_x+'px;resizable:no;status:no;scroll:no;help:no;center:no;');
}

//-----------------------------------------------------------------------------
function open_window(s_url, s_name, s_prop) {
	if (!(s_prop)) s_prop = 'scrollbars=yes,resizable=yes,width=400,height=500'
	window.open(s_url, s_name, s_prop)
	//window.showModalDialog(s_url, s_name, 'dialogHeight:400px; dialogWidth:500px;resizable:yes;status:no;scroll:yes;help:no;center:no;');
}
//-----------------------------------------------------------------------------
function copy_to_parent(vs_elm, s_val) {
	eval('window.opener.frm.' + vs_elm + '.value = \'' + s_val + '\'');
}
//-----------------------------------------------------------------------------
function confirm_action(vs_dialog, vs_href) {
	if (confirm(vs_dialog)) {
		document.location.href = vs_href;
	}
}
/*****************************************************************************************
* Expand DOM
*
* @created	2002-09-05 21:53 <ivan@konsep.net>
*****************************************************************************************/
function togl_disp_dom(vs_dom, vs_img) {
	var o_dom = document.getElementById(vs_dom);
	var o_img = document.getElementById(vs_img);
	if (o_dom.style.display == 'none') {
		o_dom.style.display = 'block';
		if (vs_img) {
			o_img.src = ICO_HIDE;
			o_img.alt = 'Hide';
		}
	} else {
		o_dom.style.display = 'none';
		if (vs_img) {
			o_img.src = ICO_SHOW;
			o_img.alt = 'Show';
		}
	}
}
/*****************************************************************************************
* Expand Menu
*
* @created	2002-09-04 02:23 <ivan@konsep.net>
*****************************************************************************************/
function expand_menu(vn_menu) {
	var o_current=document.getElementById('menu'+vn_menu+'');
	if (o_current!=null) {
		//eval('var o_current=menu'+vn_menu+'');
		if (o_current.style.display=='none') {
			o_current.style.display='table';
		} else {
			o_current.style.display='none';
		}
		for (var i=1;i<=4;i++) {
			var o_mnu=document.getElementById('menu'+i+'');
			if (o_mnu!=null && i!=vn_menu) o_mnu.style.display='none';
		}
	}
}

/*****************************************************************************************
* valid numeric
*
* @created	2002-09-14 17:00 <ivan@konsep.net>
*****************************************************************************************/
function is_float(ro_ctl, s_name, n_prec, n_scale, vn_req, u_zero) {
	var s_str = ro_ctl.value;
	var n_len = s_str.length;
	if (n_len < 1) return show_err(s_name + ' must be filled.', ro_ctl, true, true);
	for (var i = 0; i < n_len; i++) {
		s_char = s_str.charAt(i);
		if (('01234567890.').indexOf(s_char) == -1) {
			return show_err(s_name+' must be filled with numbers.', ro_ctl, true, true);
		}
	}
	if ((parseFloat(s_str)<=0) && (u_zero!=0)) return show_err(s_name + ' may not be zero.', ro_ctl, true, true);
	return true;
}

/*****************************************************************************************
* is filled?
*
* @updated	2003-04-16 12:12 <ivan@konsep.net>
* @created	2002-09-14 15:09 <ivan@konsep.net>
*****************************************************************************************/
function is_filled(ro_ctl, vs_msg, vn_min, vn_max) {
	if (!vn_min) { // simple format, no min/max value
		if ('' == trim_str(ro_ctl.value)) {
			return show_err(vs_msg, ro_ctl, true, true);
		}
		vn_min = 0;
	}
	if (!(is_str_in_range(ro_ctl.value, vn_min, vn_max))) {
		vs_msg = 'Minimum length for this field is ' + vn_min + ', and maximum length is ' + vn_max + '.';
		return show_err(vs_msg, ro_ctl, true, true);
	}
	return true;
}

/*****************************************************************************************
* is selected?
*
* @created	2002-09-14 15:09 <ivan@konsep.net>
*****************************************************************************************/
function is_selected(ro_ctl, vs_msg) {
	if (ro_ctl.selectedIndex == 0) {
		return show_err(vs_msg, ro_ctl, true, false);
	}
	return true;
}
//-----------------------------------------------------------------------------
function is_num(ro_ctl, vs_msg, vn_req, n_low, n_high) {
	var s_val = trim_str(ro_ctl.value);
	if ('' != s_val) {
		if (!(is_int_in_range(s_val, n_low, n_high))) {
			return show_err(vs_msg, ro_ctl, true, true);
		}
	} else {
		if (vn_req == 1) return show_err(vs_msg, ro_ctl, true, true);
	}
	return true;
}
//-----------------------------------------------------------------------------
function is_month(ro_ctl, vs_msg, vn_req) {
	var b_temp = true;
	if (vn_req==1) {
		b_temp =
			is_selected(ro_ctl[0], vs_msg + '. Please select month') &&
			is_num(ro_ctl[1], vs_msg + '. Please enter valid year', 1, 1900, 2100);
	} else {
		if ((ro_ctl[0].selectedIndex!=0)||(''!=trim_str(ro_ctl[1].value))) {
			b_temp =
				is_selected(ro_ctl[1], vs_msg + '. Please select month') &&
				is_num(ro_ctl[1], vs_msg + '. Please enter valid year', 1, 1900, 2100);
		}
	}
	if (!b_temp) return false;
	return true;
}
/*****************************************************************************************
* Expand Menu
*
* @created	2002-09-04 02:23 <ivan@konsep.net>
*****************************************************************************************/
function is_date(ro_frm, vs_elm, vs_msg, vn_req, b_higher) {
	var b_temp = true;
	var b_isdate = false;
	var b_test = false;
	var o_dd =  ro_frm.elements[vs_elm][0];
	var o_mo =  ro_frm.elements[vs_elm][1];
	var o_yr =  ro_frm.elements[vs_elm][2];
	if (vn_req) {
		b_test = true;
	} else {
		if (('' != trim_str(o_dd.value)) || (o_mo.selectedIndex != 0) || ('' != trim_str(o_yr.value))) {
			b_test = true;
		}
	}
	if (b_test) {
		b_temp = is_date_box(ro_frm, vs_elm, vs_msg);
		b_isdate = b_temp;
	}
	if (b_isdate && b_higher) {
		var d_date = new Date(o_yr.value, o_mo.value - 1, o_dd.value);
		if (d_date < CURRENT_DATE) {
			alert(vs_msg + ' Isilah tanggal yang lebih kecil dari hari ini.');
			ro_frm.elements[vs_elm][0].focus();
			return false;
		}
	}
	if (!b_temp) return false;
	return true;
}
/*****************************************************************************************
* check date box
*
* @created	2002-09-13 17:54 <ivan@konsep.net>
*****************************************************************************************/
function is_date_box(ro_frm, vs_elm, vs_msg) {
	var b_temp = true;
	var o_dd =  ro_frm.elements[vs_elm][0];
	var o_mo =  ro_frm.elements[vs_elm][1];
	var o_yr =  ro_frm.elements[vs_elm][2];
	b_temp =
		is_num(o_dd, vs_msg + ' Tanggl yang valid adalah antara 1 dan 31', true, 1, 31) &&
		is_selected(o_mo, vs_msg + ' Pilih bulan.') &&
		is_num(o_yr, vs_msg + ' Tahun yang valid adalah antara ' + VALID_FROM_YEAR + ' dan ' + VALID_THRU_YEAR + '.', true, VALID_FROM_YEAR, VALID_THRU_YEAR);
	if (!b_temp) return false;
	var n_max_date = max_date(o_yr.value, o_mo.value, o_dd.value)
	if (n_max_date != 0) {
		return show_err(vs_msg + ' Max date for the selected month/year is ' + n_max_date + '.', o_dd, true, true);
	}
	return true;
}
/*****************************************************************************************
* Get max date for a day
*
* @created	2002-09-13 17:53 <ivan@konsep.net>
*****************************************************************************************/
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;
}
//-----------------------------------------------------------------------------
function is_valid_email(ro_ctl, vs_msg, vn_req) {
	var s_val = ro_ctl.value;
	if ((s_val == '') && (!vn_req)) return true;
	if (!(valid_email(s_val))) {
		return show_err(vs_msg, ro_ctl, true, true);
	}
	return true;
}
//-----------------------------------------------------------------------------
function trim_str(vs_var) {
	var n_len = ('' + vs_var).length;
	for (var i = 0; i < n_len && vs_var.charAt(i) == ' '; i++);
	for (var n = n_len - 1; n > i && vs_var.charAt(n) == ' '; n--);
	return vs_var.substring(i, n+1);
}
//-----------------------------------------------------------------------------
function is_str_in_range(vs_var, vn_min, vn_max) {
	var n_len = ('' + vs_var).length;
	if (vn_max) {
		return ((1 * n_len >= 1 * vn_min) && (1 * n_len <= 1 * vn_max));
	} else {
		return ((1 * n_len >= 1 * vn_min));
	}
	return true;
}
//-----------------------------------------------------------------------------
function is_int_in_range(chk, vn_min, vn_max)
{
	var chkStr = '' + chk, chkInt = parseInt(chk, 10);
	if (vn_min && vn_max) {
		return (!isNaN(chkInt) && ('' + chkInt == chkStr) && 
			(1 * chkInt >= 1 * vn_min) && (1 * chkInt <= 1 * vn_max));
	} else {
		return (!isNaN(chkInt) && ('' + chkInt == chkStr));
	}
}
//-----------------------------------------------------------------------------
function is_float_in_range(chk, vn_min)
{
	var chkStr = '' + chk
	return (!isNaN(parseFloat(chk)) && 
		('' + parseFloat(chk) == chkStr) && (1 * parseFloat(chk) >= 1 * vn_min));
}

//-----------------------------------------------------------------------------
// 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;
}
//-----------------------------------------------------------------------------
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param	 object	 the table row
 * @param	 string	 the action calling this script (over, out or click)
 * @param	 string	 the default background color
 * @param	 string	 the color to use for mouseover
 * @param	 string	 the color to use for marking a row
 *
 * @return	boolean	whether pointer is set or not
 */
//-----------------------------------------------------------------------------
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
		var theCells = null;

		// 1. Pointer and mark feature are disabled or the browser can't get the
		//		row -> exits
		if ((thePointerColor == '' && theMarkColor == '')
				|| typeof(theRow.style) == 'undefined') {
				return false;
		}

		// 2. Gets the current row and exits if the browser can't get it
		if (typeof(document.getElementsByTagName) != 'undefined') {
				theCells = theRow.getElementsByTagName('td');
		}
		else if (typeof(theRow.cells) != 'undefined') {
				theCells = theRow.cells;
		}
		else {
				return false;
		}

		// 3. Gets the current color...
		var rowCellsCnt	= theCells.length;
		var domDetect		= null;
		var currentColor = null;
		var newColor		 = null;
		// 3.1 ... with DOM compatible browsers except Opera that does not return
		//				 valid values with 'getAttribute'
		if (typeof(window.opera) == 'undefined'
				&& typeof(theCells[0].getAttribute) != 'undefined') {
				currentColor = theCells[0].getAttribute('bgcolor');
				domDetect		= true;
		}
		// 3.2 ... with other browsers
		else {
				currentColor = theCells[0].style.backgroundColor;
				domDetect		= false;
		} // end 3

		// 4. Defines the new color
		// 4.1 Current color is the default one
		if (currentColor == ''
			|| currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
			if (theAction == 'over' && thePointerColor != '') {
				newColor = thePointerColor;
			} else if (theAction == 'click' && theMarkColor != '') {
				newColor = theMarkColor;
			}
		}
		// 4.1.2 Current color is the pointer one
		else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
				if (theAction == 'out') {
						newColor = theDefaultColor;
				}
				else if (theAction == 'click' && theMarkColor != '') {
						newColor = theMarkColor;
				}
		}
		// 4.1.3 Current color is the marker one
		else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
				if (theAction == 'click') {
						newColor = (thePointerColor != '')
										 ? thePointerColor
										 : theDefaultColor;
				}
		} // end 4

		// 5. Sets the new color...
		if (newColor) {
				var c = null;
				// 5.1 ... with DOM compatible browsers except Opera
				if (domDetect) {
						for (c = 0; c < rowCellsCnt; c++) {
								theCells[c].setAttribute('bgcolor', newColor, 0);
						} // end for
				}
				// 5.2 ... with other browsers
				else {
						for (c = 0; c < rowCellsCnt; c++) {
								theCells[c].style.backgroundColor = newColor;
						}
				}
		} // end 5

		return true;
} // end of the 'setPointer()' function

/*******************************************************************************
* Valid Product
*
* @updated	2003-03-05 12:13 <asep@konsep.net>
* @created	2003-02-05 16:00 <benny@konsep.net>
*******************************************************************************/
function valid_adm_prod(s_frm, s_type) {
	idContent = "idContent" + s_type;	
	idElement = document.getElementById('txtar_desc_'+s_type);

	if (displayMode == 'HTML') {
		eval(idContent).document.body.innerHTML = eval(idContent).document.body.innerText;
	}
	idElement.value = eval(idContent).document.body.innerHTML;
	return true;
}

function empty_item(s_elm)	{
	document.forms["frm1"].elements[s_elm].value = '';
}
