// $Id: form_scripts.js 1604 2006-04-28 07:38:09Z zeke $

var onload_handlers = new Array();
//
// Show/hide any tag by its id.
// parameters:
// id - element id
function fn_show_tag(id, status)
{
	if (document.getElementById(id)) {
		if (status == true || status == false) {
			document.getElementById(id).style.display = (status == true)?"none":"";
		} else {
			document.getElementById(id).style.display = (document.getElementById(id).style.display == "")?"none":"";
		}
	}
}

//
// Select text in text input
//
function fn_select_input(select)
{
	select.select();
}

//
// Image Rollover for Menu's
//

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//
// Disable/enable all form elements inside the tag 
// @id - element id
// @status - boolean (true to disable and false to enable)
function fn_disable_elements(id, status)
{

	node = document.getElementById(id);
	// Process INPUT types...
	inputs = node.getElementsByTagName('INPUT');
	for (i=0;i<inputs.length;i++) {
		node.getElementsByTagName('INPUT')[i].disabled = status;
	}

	// Process TEXTAREA type...
	texts = node.getElementsByTagName('TEXTAREA');
	for (i=0;i<texts.length;i++) {
		node.getElementsByTagName('TEXTAREA')[i].disabled = status;
	}

	// Process SELECT type...
	selects = node.getElementsByTagName('SELECT');
	for (i=0;i<selects.length;i++) {
		node.getElementsByTagName('SELECT')[i].disabled = status;
	}
}

//
// Open pop-up window with detailed image
//
function fn_open_popup_image(popup_script, image_width, image_height)
{
	if (image_width == 0) {
		image_width = 400;
	}
	if (image_height == 0) {
		image_height = 400;
	}
	image_width += 10;
	image_height += 10;

	if ((typeof(handle_popup_image)!='undefined') && (handle_popup_image.closed == false)) {
		handle_popup_image.close();
	}
    handle_popup_image = window.open(popup_script, 'popup_image', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,left=200,top=100,width=' + image_width + ',height=' + image_height + ',resizable=yes');
}

//
// Perform request via <SCRIPT> tag
//
function fn_http_request(script_id,url)
{
	fn_switch_loading_msg();

	var now = new Date();
	url = url + '&prevent_cache='+now.getTime(); // Prevent script from caching

	var script_tag = document.getElementById(script_id);
	if (script_tag) {
		script_tag.parentNode.removeChild(script_tag);
	}

	script_tag = document.createElement('SCRIPT');
	script_tag.type = 'text/javascript';
	script_tag.language = 'javascript';
	script_tag.id = script_id;
    script_tag.src = url;
	document.body.appendChild(script_tag);
}

//
// String utility functions
//

// extract front part of string prior to searchString
function fn_str_get_front(mainStr,searchStr)
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function fn_str_get_end(mainStr,searchStr) 
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

// insert insertString immediately before searchString
function fn_str_insert_string(mainStr,searchStr,insertStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + insertStr + searchStr + end
	}
	
	return null
}

// remove deleteString
function fn_str_delete_string(mainStr,deleteStr) 
{
	return fn_str_replace_string(mainStr,deleteStr,"");
}

// replace searchString with replaceString
function fn_str_replace_string(mainStr,searchStr,replaceStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + replaceStr + end
	}
	
	return null
}

// Compare 2 strings
// @haystack - where search
// @needle - what search
// @strict - exact compare or partial
function fn_compare_strings(haystack, needle, strict)
{
	if (strict == true) {
		return (haystack == needle);
	} else {
		return (haystack.indexOf(needle) == -1) ? false : true;
	}
}


//
// Check / uncheck all checkboxes in form
// @form_name - form name whose checkboxes should be checked or unchecked
// @checkbox_id - tag identifier of checkboxes that should be checked or unchecked
// @flag - can be true or false
function fn_check_all_checkboxes(form_name, flag, checkbox_id, strict)
{
	if (!checkbox_id) {
		checkbox_id = 'delete_checkbox';
	}

	if (typeof(strict) == 'undefined') {
		strict = true;
	}

	if(!(d_form = document.forms[form_name]))
		return false;

	for(i=0; i < d_form.length; i++) {
		if (fn_compare_strings(d_form.elements[i].id, checkbox_id, strict) && !d_form.elements[i].disabled)
			d_form.elements[i].checked = flag;
	}
	return true;
}

// Check email address for validity
function fn_check_email(email, msg) {

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		return true;
	} else{
		alert(msg);
		return false;
	}
}

//
// This function checks required fields and set a mark if something wrong
//
function fn_check_fields(form_name, extra_ids)
{
	var is_ok = true;
	var seqb_exists = document.getElementById('seqb');
	var error_fields = new Array();

	if (!document.forms[form_name]) {
		return false;
	}
	
	var is_ok = true;
	var set_mark = false;

	elms = document.forms[form_name].elements;
	for (i=0; i<elms.length; i++) {
		set_mark = false;

		// Check the email field
		if (extra_ids[elms[i].id] == 'E') {
			if (fn_check_email(elms[i].value, email_invalid) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					is_ok = false;
					set_mark = true;
				}
			}
		// Check for integer field
		} else if (extra_ids[elms[i].id] == 'I') {
			if (fn_is_integer(elms[i].value) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					is_ok = false;
					set_mark = true;
				}
			}
		// Check for blank value
		} else {

			if (required_fields[elms[i].id] == 'Y' && fn_is_blank(elms[i].value) == true) {
				is_ok = false;
				set_mark = true;
				error_fields[elms[i].id] = true;
				if (seqb_exists && document.getElementById('seqb').checked == true && elms[i].id.substr(0,2)=='s_' && !error_fields['b_'+elms[i].id.substr(2)]) {
					document.getElementById('seqb').click();
				}
			}
		}
		if (document.getElementById('status_'+elms[i].id)) {
			document.getElementById('status_'+elms[i].id).innerHTML = (set_mark == true)? warning_mark : "&nbsp;";
		}
	}

	if (is_ok == false) {
		alert(message);
	}
	return is_ok;
}

//
// Checks if the value is blank
//
function fn_is_blank(val){
	if (val==null){
		return true;
	}
	for (var i=0; i<val.length; i++) {
		if((val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;
}

//
// Checks if the value is integer
//
function fn_is_integer(val)
{
	if(fn_is_blank(val)){
		return false;
	}
	for(var i=0; i<val.length; i++){
		if (!fn_is_digit(val.charAt(i))) {
			return false;
		}
	}
	return true;
}

//
// Checks if the value is digit
//
function fn_is_digit(num)
{
	if(num.length>1){
		return false;
	}
	var string="1234567890";
	if (string.indexOf(num)!=-1){
		return true;
	}
	return false;
}

//
// Execute functions on page load
//
function fn_load_handlers()
{
	var i=0;
	for (i=0; i<onload_handlers.length; i++) {
		eval(onload_handlers[i]);
	}
}

function fn_show_section(id, scts, redraw_all)
{

	// Fix bug with changing hash string in opera 8.5+
	/*if (is_opera && document_loaded == false) {
		onload_handlers.push("fn_show_section('"+id+"', sections ,'"+separate_form+"', "+redraw_all+");");
		return;
	}*/

	for (i in scts)
	{
		if (i == id) {
			if (document.getElementById('content_'+i)) {
				document.getElementById('content_'+i).style.display = '';
			}
			document.getElementById('tab_'+i+'_bg').className = 'section-active-tab-bg';
			document.getElementById('tab_'+i+'_left').src = tab_left_active_image_path;
			document.getElementById('tab_'+i+'_right').src = tab_right_active_image_path;
			if (document.getElementById('selected_section')) {
				document.getElementById('selected_section').value = id;
				location.hash = '#'+id;
			}
		} else {
			if (document.getElementById('tab_'+i+'_bg').className == 'section-active-tab-bg' || redraw_all) {
				if (document.getElementById('content_'+i)) {
					document.getElementById('content_'+i).style.display = 'none';
				}
				document.getElementById('tab_'+i+'_bg').className = 'section-inactive-tab-bg';
				document.getElementById('tab_'+i+'_left').src = tab_left_image_path;
				document.getElementById('tab_'+i+'_right').src = tab_right_image_path;
			}
		}
	}
}

function fn_get_window_sizes()
{
	var wnd_arr = new Array();

	if (window.height) {
		wnd_arr['offset_x'] = self.pageXOffset;
		wnd_arr['offset_y'] = self.pageYOffset;
		wnd_arr['view_height'] = self.innerHeight;
		wnd_arr['view_width'] = self.innerWidth;
		wnd_arr['height'] = window.height;
		wnd_arr['width'] = window.width;
	} else {
		wnd_arr['offset_x'] = document.body.scrollLeft;
		wnd_arr['offset_y'] = document.body.scrollTop;
		wnd_arr['view_height'] = document.body.clientHeight;
		wnd_arr['view_width'] = document.body.clientWidth;
		wnd_arr['height'] = document.body.scrollHeight;
		wnd_arr['width'] = document.body.scrollWidth;
	}

	return wnd_arr;
}

function fn_switch_loading_msg()
{
	var dlg = document.getElementById('dialog_bg');
	var msg = document.getElementById('dialog_msg');

	var wnd_sizes = fn_get_window_sizes();
	dlg.style.left = 0;
	dlg.style.top = 0;
	dlg.style.width=wnd_sizes['width'];
	dlg.style.height=wnd_sizes['height'];

	msg.style.left = wnd_sizes['offset_x'];
	msg.style.top = wnd_sizes['offset_y'];
	msg.style.width = wnd_sizes['view_width'];
	msg.style.height = wnd_sizes['view_height'];

	fn_show_tag('dialog_bg');
}

function fn_check_selected(form_name, checkbox_id, no_alert, strict) {
{
	if (typeof(strict) == 'undefined') {
		strict = true;
	}
	
	if (!checkbox_id) {
		checkbox_id = 'delete_checkbox';
	}

	if(!(d_form = document.forms[form_name])) {
		return false;
	}

	for(i=0; i < d_form.length; i++) {
		if (fn_compare_strings(d_form.elements[i].id, checkbox_id, strict)) {
			if (d_form.elements[i].checked) {
				return true;
			}
		}
	}
	if (!no_alert) {
		alert(error_no_items_selected);
	}
	return false;
}																													    }

function fn_ajax_update_vars(page, url)
{
	// Set page input field to current page
	elms = document.getElementsByName('page');
	if (elms.length>0) {
		for (var i=0; i<elms.length; i++) {
			elms[i].value=page;
		}
	}

	elms = document.getElementsByName('redirect_url');
	if (elms.length>0) {
		for (var i=0; i<elms.length; i++) {
			elms[i].value=url;
		}
	}
	fn_switch_loading_msg();
}