var ajax_response_div = 'ajax-response';
var loading_time = 1000;
var timeout_time = 10000;
var ajax_button = 'ajax-button';
var ajax_disabled_button = ajax_button;
var submit_button = 'Submit';
var onclick_event;
var help_div_id;
var max_help_id = 50;
var offset_resize = 7.5;
var ajax_submitted = false;
var width = screen.width - 4;
var height = screen.height - 4;
var error_color = '#fcc';
var clear_color = '#fff';
var isCtrl = false;
var keywords_timeout = 0;
var print_hide_counter = 5;

function get_xml_http_object()
 { var xml_http = null;
   
   if (window.XMLHttpRequest) 
	{ try 
	   { xml_http = new XMLHttpRequest(); } 
	  catch (e) 
	   { xml_http = false; }
    }
   else
    { try 
	   { xml_http = new ActiveXObject('Msxml2.XMLHTTP'); } 
      catch (e) 
	   { try 
	      { xml_http = new ActiveXObject('Microsoft.XMLHTTP'); } 
	     catch (e) 
	      { xml_http = false; }
       }
	}   
   
   return xml_http;
 } 

function xml2str(xmlNode)
 { try 
    { return (new XMLSerializer()).serializeToString(xmlNode); } // Gecko-based browsers, Safari, Opera.
   catch (e) 
    { try 
	   { return xmlNode.xml; } // Internet Explorer.
      catch (e)
       { alert('Xmlserializer not supported'); } //Strange Browser
    }
   return false;
 }
 
function ajax_timeout(msg)
 { ajax_clear_response();
   ajax_show_error(msg);
   ajax_enable_button();
   set_focus();
 } 
 
function ajax_show_loading(msg)
 { if (document.getElementById(ajax_response_div) != null)
    { document.getElementById(ajax_response_div).style.display = 'block';
	  document.getElementById(ajax_response_div).innerHTML = '<div class="ajax-loading"><div class="ajax-loading-wrapper"><div class="ajax-loading-default"><img class="padding-r-05" src="images/loading.gif" alt="' + msg + '" title="' + msg + '">' + msg + '</div></div></div><div class="ajax-loading-mask-default" style="filter: alpha(opacity=60); -moz-opacity: 0.6;"></div>'; 
	}
 } 
 
function ajax_show_error(msg)
 { if (document.getElementById(ajax_response_div) != null)
    { document.getElementById(ajax_response_div).style.display = 'block';
	  document.getElementById(ajax_response_div).innerHTML = '<div class="ajax-loading"><div class="ajax-loading-wrapper"><div class="ajax-loading-error"><div class="float-l"><img class="padding-r-05" src="images/icons/error.gif"></div><div class="float-l">' + msg + '</div><div class="ajax-clear-response"><img class="hand" src="images/icons/button-close.gif" onclick="ajax_clear_response();"></div></div></div></div><div class="ajax-loading-mask" style="filter: alpha(opacity=60); -moz-opacity: 0.6;"></div>'; 
	}
 } 

function ajax_show_success(msg)
 { if (document.getElementById(ajax_response_div) != null)
    { document.getElementById(ajax_response_div).style.display = 'block';
	  document.getElementById(ajax_response_div).innerHTML = '<div class="ajax-loading"><div class="ajax-loading-wrapper"><div class="ajax-loading-success"><div class="float-l"><img class="padding-r-05" src="images/icons/success.gif"></div><div class="float-l">' + msg + '</div><div class="ajax-clear-response"><img class="hand" src="images/icons/button-close.gif" onclick="ajax_clear_response();"></div></div></div></div><div class="ajax-loading-mask" style="filter: alpha(opacity=60); -moz-opacity: 0.6;"></div>'; 
	} 
 }
 
function ajax_clear_field(el)
 { if (document.getElementById(el) != null)
    { document.getElementById(el).value = ''; }
   set_focus();	
 }  
 
function ajax_clear_response()
 { if (document.getElementById(ajax_response_div) != null)
    { document.getElementById(ajax_response_div).innerHTML = ''; 
	  document.getElementById(ajax_response_div).style.display = 'none';
	}
 } 
 
function ajax_disable_button(f, id)
 { ajax_disabled_button = ajax_button;
   if (id)
    { ajax_disabled_button = ajax_button + id; }

   if (document.getElementById(ajax_response_div) != null)
    { document.getElementById(ajax_response_div).style.display = 'none'; }
   if (document.getElementById(ajax_disabled_button) != null)
    { onclick_event = document.getElementById(ajax_disabled_button).getAttribute('onclick');
	  document.getElementById(ajax_disabled_button).setAttribute('onclick', null);
	  document.getElementById(ajax_disabled_button).className = 'button-disabled';
	  ajax_submitted = true;
	}
   if (document.getElementById(submit_button) != null)
    { document.getElementById(submit_button).disabled = true; 
	  ajax_submitted = true;
	}

   if (f)
    { var form = document.forms[f];
	  if (form != 'undefined')
	   { var field;
		 for (var i = 0; i < form.elements.length; i++) 
          { field = form.elements[i].name;
		    if (form.elements[i].type != 'hidden' && document.getElementById(field) != null)
			 { if (form.elements[i].type == 'radio' || form.elements[i].type == 'checkbox')
			    { document.getElementById(field).style.background = ''; }
			   else
			    { document.getElementById(field).style.background = clear_color; }
			 }
		  }
	   }	  
	} 
 } 
 
function ajax_enable_button()
 { if (document.getElementById(ajax_disabled_button) != null)
    { document.getElementById(ajax_disabled_button).setAttribute('onclick', onclick_event);
	  document.getElementById(ajax_disabled_button).className = 'button-enabled';
	  ajax_submitted = false; 
	}
   if (document.getElementById(submit_button) != null)
    { document.getElementById(submit_button).disabled = false; 
	  ajax_submitted = false; 
	}
 }
 
function silent_error_handler() 
 { return true; }

function disable_enter_key(e)
 { if (e.keyCode == 13 || e.which == 13)
    { return false; }
   return true;
 } 

function get_px_in_em(el)
 { var temp_div = el.appendChild(document.createElement('div'));
   with(temp_div.style)
    { margin = '0em';
      padding = '0em';
      border = 'none';
      height = '1em';
      width = '1em';
    }
   var px_in_em = temp_div.offsetHeight;
   el.removeChild(temp_div);
   return px_in_em;
 }

function browser_is(b) // MSIE, Firefox, Opera, Netscape, Firebird, Konqueror, Chrome, OmniWeb, Apple, iCab, Safari, Camino
 { if (navigator.userAgent.indexOf(b) != -1)
    { return true; }
   return false;
 }

function ie_version() // MSIE x.x
 { if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
    { var ieversion = new Number(RegExp.$1)
      return ieversion;
	}
   return 0;	
 }

function ff_version() // Firefox/x.x or Firefox x.x
 { if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
    { var ffversion = new Number(RegExp.$1)
      return ffversion;
	}
   return 0;	
 }

function opera_version() // Opera/x.x or Opera x.x
 { if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
    { var oprversion = new Number(RegExp.$1)
      return oprversion;
	}
   return 0;	
 }
 
function show_hide(id)
 { id = document.getElementById(id);
   if (id != null)
    { id.className = ('none' == id.className) ? '' : 'none'; }
   return false;	
 }
 
function show_hide_book(id_show, id_hide)
 { id_show = document.getElementById(id_show);
   if (id_show != null)
    { id_show.className = ('none' == id_show.className) ? 'hand' : 'none'; }
    
   id_hide = document.getElementById(id_hide);
   if (id_hide != null)
    { id_hide.className = ('none' == id_hide.className) ? 'hand' : 'none'; }
   
   return false;	
 }
 
function show_hide_news(id, total)
 { for (var i = 1; i < total; i++)
    { if (i == id)
       { if (document.getElementById('q' + i).className == 'none')
          { document.getElementById('h' + i).className = 'none'; 
            document.getElementById('ls' + i).className = 'none'; 
            document.getElementById('q' + i).className = 'news-text'; 
          }
         else
          { document.getElementById('h' + i).className = 'block'; 
            document.getElementById('ls' + i).className = 'news-link'; 
            document.getElementById('q' + i).className = 'none'; 
          }
       }
      else
       { if (document.getElementById('q' + i) != null && document.getElementById('h' + i) != null)
          { document.getElementById('h' + i).className = 'block'; 
            document.getElementById('ls' + i).className = 'news-link'; 
            document.getElementById('q' + i).className = 'none';
          }
       }
    }
   return false;	
 }
  
function print_special(doctype, params)
 { var html = doctype.replace(/\*\*/ig, '"') + '<html ' + params.replace(/\*\*/ig, '"') + '><head>';   
   if (document.getElementsByTagName != null)
    { var head_tags = document.getElementsByTagName('head');
      if (head_tags.length > 0)
       { html += head_tags[0].innerHTML;
	     html = html.replace(/media=screen/ig, 'media="print, screen"');
		 html = html.replace(/<script[^>]*>[\S\s]*?<\/script>/ig, '');
	   }
    }
   html += '</head><body class="visible">';
	  
   var text = new Array();
   for (var i = 1; i <= print_hide_counter; i++)
    { if (document.getElementById('printHide' + i) != null)
       { text[i] = document.getElementById('printHide' + i).innerHTML;
	     document.getElementById('printHide' + i).innerHTML = ''; 
	   }
	} 
   if (document.getElementById('scroller') != null)	
    { document.getElementById('scroller').className =''; }
     
   var print_ready = document.getElementById('print_ready');
   if (print_ready != null)
    { html += print_ready.innerHTML; 
	  html = html.replace(/on\w+\s*=\s*"[\S\s]*?"/ig, '');
	}
   else
    { return false; }
	  
   if (document.getElementById('scroller') != null)	
    { document.getElementById('scroller').className ='scroller'; }
   for (var i = 1; i <= print_hide_counter; i++)
    { if (document.getElementById('printHide' + i) != null)
       { document.getElementById('printHide' + i).innerHTML = text[i]; }
	}
	   
   html += '</body></html>';
   var print_win = window.open('', 'print_special', 'toolbar=1, location=1, directories=1, status=1, menubar=1, scrollbars=1, resizable=1, copyhistory=1, width=' + width + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0');
   print_win.document.open();
   print_win.document.write(html);
   print_win.document.close();
   print_win.print();
   return false;
 } 
 
function get_url_param(name) // use: var param = get_url_param('variable')
 { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
   var regexS = "[\\?\/]" + name + "\/([^\/#]*)";  
   var regex = new RegExp(regexS);  
   var results = regex.exec(window.location.href);  
   if (results == null)    
    { return ''; }
   else 
    { return results[1]; }
 }
 
function get_parameter_names() // get all querystring parameter names
 { var params = new Array();
   var regex = /[\?\/]([^\/]+)\//g;
   while ((results = regex.exec(window.location.href)) != null)
	{ params.push(results[1]); }
   return params;
 }
 
function row_over(object, classname)
 { object.className = classname; }

function submit_form(f) 
 { ajax_disable_button(f);
   document.forms[f].submit();
   return false;   
 }
 
function submit_form_only(f) 
 { document.forms[f].submit();
   return false;   
 }

function popup_ag(url)
 { var ag_win = window.open(url,'popup_window','toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, copyhistory=0, width=' + width + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0'); 
   ajax_print_ag('ag_form', 2, ag_win);
 }
  
 
function popup_window(url)
 { window.open(url,'popup_window','toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, copyhistory=0, width=' + width + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0'); }
 
function popup(url)
 { window.open(url,'popup','toolbar=1, location=1, directories=1, status=1, menubar=1, scrollbars=1, resizable=1, copyhistory=1, width=' + width + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0'); } 
 
function popup2(url, width, height)
 { var win = window.open(url,'popup_width','toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, copyhistory=0, width=' + (width + 20) + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0'); 
   win.moveTo((screen.width / 2) - ((width + 20) / 2), 0);
   win.focus();
 } 
 
function popup3(url, width, height)
 { var win = window.open(url,'popup_width','toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, copyhistory=0, width=' + (width + 20) + ', height=' + height + ', screenX=0, screenY=0, top=0, left=0'); 
   win.moveTo((screen.width / 2) - ((width + 20) / 2), 0);
   win.focus();
 } 
 
function set_focus(pos, action) 
 { pos = pos || 0;
   if (document.forms.length > 0) 
    { var field = document.forms[pos];
      for (i = 0; i < field.length; i++) 
	   { if ((field.elements[i].type != 'image') && (field.elements[i].type != 'hidden') && (field.elements[i].type != 'reset') && (field.elements[i].type != 'submit'))
	      { if (!document.forms[pos].elements[i].disabled && document.forms[pos].elements[i].value == '')
		     { document.forms[pos].elements[i].focus(); }
			if (action == 'select' && ((field.elements[i].type == 'text') || (field.elements[i].type == 'password'))) 
			 { document.forms[pos].elements[i].select(); }
            break;
          }
       }
    }
 }
 
function url_encode(string) 
 { if (window.encodeURIComponent) 
    { return encodeURIComponent(string); }
   return escape(string);
 }

function url_decode(string)
 { if (window.decodeURIComponent)
    { return decodeURIComponent(string); }
   return unescape(string);
 }

function compatibility_encode(string)
 { string = string.replace(/\+/g, '__PLUS__');
   string = string.replace(/\//g, '__SLASH__');
   string = string.replace(/\\/g, '__BACKSLASH__');
   string = string.replace(/&/g, '__AMPERSAND__');
   string = string.replace(/</g, '__LEFT_ANGLE_BRACKET__');
   string = string.replace(/>/g, '__RIGHT_ANGLE_BRACKET__');
   return url_encode(string);
 }

function truebody()
 { return (!window.opera && document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body; } 
 
function find_pos(obj)
 { var curleft = curtop = 0;
   if (obj.offsetParent)
    { do
	   { curleft += obj.offsetLeft;
		 curtop += obj.offsetTop;
	   }
	  while (obj = obj.offsetParent);
	}
   return [curleft, curtop];
 }
 
function get_scroll_width()
 { var w = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
   return w ? w : 0;
 } 

function get_scroll_height()
 { var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
   return h ? h : 0;
 } 
 
Scroller = 
 { speed: 10,
	 	scroll: function(d)
    { i = window.innerHeight || document.documentElement.clientHeight;
		    h = document.body.scrollHeight;
		    a = get_scroll_height();
		    if (d > a)
			    { if (h - d > i)
				      { a += Math.ceil((d - a) / Scroller.speed); }
			      else
				      { a += Math.ceil((d - a - (h - d)) / Scroller.speed); }
       }
		    else
			    { a = a + (d - a) / Scroller.speed; }
		    window.scrollTo(0, a);
	  	  if (a == d || Scroller.offsetTop == a)
       { clearInterval(Scroller.interval); }
	  	  Scroller.offsetTop = a;
	   }
 }
 
function smooth_scroll(id)
 { var pos = find_pos(document.getElementById(id));
   Scroller.speed = 20;
   Scroller.interval = setInterval('Scroller.scroll(' + pos[1] + ')', 10); 
 } 

function check(f, v)
 { var key;
   for (i in document.forms[f])
    { key = i.substr(0, 3);
	  if ((key == 'cid' || key == 'vid') && document.getElementById(i).disabled === false)
       { document.forms[f][i].checked = v; }
	}
 }
 
function check_length(textarea, counter, max)
 { var txt = document.getElementById(textarea);
   var count = document.getElementById(counter);
   if (txt != null && count != null)
    { var length = txt.value.length;
	  if (length > max) 
	   { txt.value = txt.value.substring(0, max); }
	  else
	   { count.innerHTML = max - length; }
	}
 }
 
function hide_div(obj) 
 { if (obj.style.visibility == 'visible') 
    { obj.style.visibility = 'hidden';
      obj.style.display = 'none';
    }
 }

function show_div(obj) 
 { if (obj.style.visibility != 'visible') 
    { obj.style.visibility = 'visible';
      obj.style.display = 'inline';
    }
 }
 
function is_numeric(text)
 { var chars = "0123456789.";
   var is_number = true;
   var character;

   for (i = 0; i < text.length && is_number == true; i++)
    { character = text.charAt(i); 
      if (chars.indexOf(character) == -1) 
       { is_number = false; }
    }
   return is_number;
 }
 
function is_valid_date(str) 
 { var pattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
   var matchArray = str.match(pattern);
   if (matchArray == null) 
    { return false; }
   day = matchArray[1];	
   month = matchArray[3];
   year = matchArray[5];
 
   if (month < 1 || month > 12) 
    { return false; }
   if (day < 1 || day > 31) 
    { return false; }
   if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) 
    { return false; }
   if (month == 2) 
    { var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day > 29 || (day == 29 && !isleap)) 
	   { return false; }
    }
   return true;
 }
 
function display_date(dateObj)
 { month = dateObj.getMonth() + 1;
   month = (month < 10) ? '0' + month : month;
   day = dateObj.getDate();
   day = (day < 10) ? '0' + day : day;
   year  = dateObj.getYear();
   if (year < 2000) 
    { year += 1900; }
   return (day + '/' + month + '/' + year);
 }

function repos_help(id)
 { var offset = 21; // icon help width
   var distance = 4; // distance from window corner
   var xcoord = 0;
   var ycoord = 0;
   var ystep = 0;
   var scroll_top = 0;
   var scroll_left = 0;
   var zero = new Array(0, 0); // top-left "zero-pos" element id
   var px = get_px_in_em(document.getElementById('zero-pos'));
   var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth;
   var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : window.innerHeight;
   docheight -= offset_resize * px;
   
   /*
   if (document.getElementById(scroller_div) != null)
    { scroll_top = document.getElementById(scroller_div).scrollTop; }
   if (document.getElementById('content') != null)
    { scroll_left = document.getElementById('content').scrollLeft; }
   */
   if (document.getElementById('zero-pos') != null)
    { zero = find_pos(document.getElementById('zero-pos')); }

   if (typeof id == 'undefined')
    { for (var i = 0; i < max_help_id; i++)
	   { if (document.getElementById('help-div' + i) != null)
          { var obj = document.getElementById('help-div' + i);
	        var pos = find_pos(document.getElementById('icon-help' + i));
	        var default_width = obj.offsetWidth;
		 
		    if (docwidth - pos[0] + scroll_left < default_width + offset + distance)
	         { ystep = Math.floor(px * 2);
			   ycoord += ystep;
			   docheight -= ystep;
		       xcoord += pos[0] - xcoord - default_width - offset - distance;
		     } 
	        else 
	         { xcoord += pos[0]; }
	  
	        var default_height = obj.offsetHeight;
		 
		    if (docheight - pos[1] + scroll_top < default_height + distance)
	         { ycoord += pos[1] - default_height - ystep; } 
	        else 
	         { ycoord += pos[1]; }

            obj.style.left = (xcoord - zero[0] - scroll_left + offset) + 'px';
            obj.style.top = (ycoord - zero[1] - scroll_top) + 'px';
	      }
	   }
	}   
   else
    { if (document.getElementById('help-div' + id) != null)
       { var obj = document.getElementById('help-div' + id);
	     var pos = find_pos(document.getElementById('icon-help' + id));
	     var default_width = obj.offsetWidth;
		 
		 if (docwidth - pos[0] + scroll_left < default_width + offset + distance)
	      { ystep = Math.floor(px * 2);
			ycoord += ystep;
			docheight -= ystep;
		    xcoord += pos[0] - xcoord - default_width - offset - distance;
		  } 
	     else 
	      { xcoord += pos[0]; }
	  
	     var default_height = obj.offsetHeight;
		 
		 if (docheight - pos[1] + scroll_top < default_height + distance)
	      { ycoord += pos[1] - default_height - ystep; } 
	     else 
	      { ycoord += pos[1]; }

         obj.style.left = (xcoord - zero[0] - scroll_left + offset) + 'px';
         obj.style.top = (ycoord - zero[1] - scroll_top) + 'px';
	   }
	}
 }
 
function show_help(id, msg, close)
 { msg = decodeURIComponent(msg);
   if (document.getElementById('help-div' + id) != null)
    { if (id == help_div_id)
	   { document.getElementById('parent-help-div' + id).removeChild(document.getElementById('help-div' + id)); }
	  else
	   { document.getElementById('help-div' + id).innerHTML = msg + '<p><a onclick="show_help(\'' + id + '\');">' + close + '</a></p>'; 
	     help_div_id = id;
	   }
	}
   else
    { if (document.getElementById('help-div' + help_div_id) != null)
	   { document.getElementById('parent-help-div' + help_div_id).removeChild(document.getElementById('help-div' + help_div_id)); }
	  help_div_id = id;
	  var helpdiv = document.createElement('div');
	  helpdiv.setAttribute('id', 'help-div' + id);
	  helpdiv.className = 'help-div';
	  helpdiv.innerHTML = msg + '<p><a onclick="show_help(\'' + id + '\');">' + close + '</a></p>';
	  document.getElementById('parent-help-div' + id).appendChild(helpdiv);
	  repos_help(id);
	}
 } 
 
window.onerror = function() 
 { silent_error_handler(); }

document.onkeyup = function(e)
 { ev = e || window.event;
   key = ev.keyCode || ev.which;
   if (key == 17 || key == 17 || ev.ctrlKey) 
    { isCtrl = false; }
 }
document.onkeydown = function(e)
 { ev = e || window.event;
   key = ev.keyCode || ev.which;
   if (key == 17 || key == 17 || ev.ctrlKey) 
    { isCtrl = true; }
   if (key == 107 || key == 109 || key == 187 || key == 189 && isCtrl == true) // + -
    { if (document.getElementById('help-div' + help_div_id) != null)
	   { repos_help(help_div_id); }
	  //return false;
	}	
 }

window.onresize = function() 
 { if (document.getElementById('help-div' + help_div_id) != null)
	{ repos_help(help_div_id); }
 } 
 
window.onload = function() 
 { set_focus(); }
