/*move window*/
var isdrag=false;
var x,y;
var dobj;
var is_moved=false;

function movemouse(e)
{
  if (isdrag)
  {
    is_moved=true;
    if (e!=null) dobj.style.left = tx + e.clientX - x;
    else dobj.style.left = tx + event.clientX - x;
    if (e!=null) dobj.style.top  = ty + e.clientY - y;
    else dobj.style.top  = ty + event.clientY - y;
    return false;
  }
}

function selectmouse(e) 
{
  if (e!=null) var fobj=e.target;
  else fobj=event.srcElement;

  if (fobj!=null)
  {
    isdrag = true;
    dobj = document.getElementById('part_info_window');
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    if (e!=null) x=e.clientX;
    else x = event.clientX;
    if (e!=null) y = e.clientY;
    else y = event.clientY;
    fobj.onmousemove=movemouse;
    fobj.onmouseout=movemouse;
    return false;
  }
}  
/*move window*/


function hideshow_advanced_search(){
  obj=document.getElementById('id_advanced_search');
  obj1=document.getElementById('id_advanced_search_link');
  obj2=document.getElementById('id_simple_search');
  if ((obj!=null)&&(obj1!=null)&&(obj2!=null)){
    if (obj.style.display == 'none') {
      obj.style.display = 'block';
      obj2.style.display = 'none';
      obj1.innerHTML = '&ndash;advanced search';
      obj_is_adv=document.getElementById('is_adv');
      if (obj_is_adv!=null) obj_is_adv.value='1';
    }
    else {
      obj.style.display = 'none';
      obj2.style.display = 'block';
      obj1.innerHTML='+advanced search';
      obj_is_adv=document.getElementById('is_adv');
      if (obj_is_adv!=null) obj_is_adv.value='0';      
    }
  }
}

function  print_window(){
  obj=document.getElementById('id_window_data');
  if (obj!=null){
    nw = window.open('', 'print');
    if (nw != null) {
      ttl='';
      obj_title=document.getElementById('div_window_caption');
      if (obj_title!=null){
        ttl='<h1>'+obj_title.innerHTML+'<h2>';
      }
      
			i_html=obj.innerHTML;
			i_html=i_html.replace('height="100%"','');
			i_html=i_html.replace("height='100%'",'');			
			
      txt ='<html><head><link rel="stylesheet" type="text/css" href="default.css">';
      txt+='<link href="myfreetemplates.css" rel="stylesheet" type="text/css"><head>';
      txt+='<body>'+ttl+'<center>'+i_html+'</center>'+'</body>';
      nw.document.write(txt);      
      els=nw.document.getElementsByTagName('form');
      len=els.length;
      for (var n=0;n<len;n++){
        el=els[n];
				var pel=el.parentNode;
				pel.removeChild(el);
      }
      nw.focus();
      nw.print();
    }
  }
}


/*Begin parts*/
function hide_part(header_name){
  obj = document.getElementById(header_name);
  if (obj!=null){
    txt=obj.innerHTML;
    if (txt=='') obj.parentNode.parentNode.parentNode.parentNode.style.display='none';    
  }  
}

function hide_clear_parts(){
  n=1;
  while (n <= 10) {
    hide_part('part'+n+'_header');
    n++;
  }
  
}
/*End parts*/

/*Begin window*/
function set_vis_to_select(visib){
  name_forms=new Array('lines','items','contacts','committees','committee_users','extensions');
  count=name_forms.length;;
  for (var n = 0; n < count; n++)
  {
    doc_form=document.forms[name_forms[n]];
    if (doc_form!=null){
      //alert(doc_form.name);
      var l=doc_form.elements;
      for (i=0; i < l.length; i++) {
        if (l[i].type=='select-one') {
          l[i].style.visibility=visib;
        }
      }
    }
  }
}

function close_wind(){
  obj = document.getElementById('part_info_window');
  obj.className='info_window_hide';
  hide_obj = document.getElementById('hide_back');
  hide_obj.className = 'show_all';
  set_vis_to_select('visible');
}

function show_wnd(name_wnd){
  set_vis_to_select('hidden');

  obj = document.getElementById(name_wnd);
  obj.className='info_window_show';
  obj.style.width = 400;
  obj.style.height = 100;
  hide_obj = document.getElementById('hide_back');
  hide_obj.className='hide_all';
  hide_obj.style.width=document.body.clientWidth;
  hide_obj.style.left=document.body.scrollLeft;
  hide_obj.style.height= document.body.clientHeight;
  hide_obj.style.top=document.body.scrollTop;
  set_window_size();
}

function set_window_size(){
  obj = document.getElementById('part_info_window');
  hide_obj = document.getElementById('hide_back');
  if ((obj==null)||(hide_obj==null)) return;
  close_me=document.getElementById('close_me');
  if (close_me!=null){
    obj.className = 'info_window_hide';
    obj.style.width = 400;
    obj.style.height = 100;
    hide_obj.className = 'show_all';
  }
  if (obj.className == 'info_window_hide') {
    hide_obj.className = 'show_all';
    obj.innerHTML="<div class='ajax_process_inner'><div class='ajax_process_hide_part'>&nbsp;</div></div>";
    obj.style.width = 400;
    obj.style.height = 100;
    //dy = document.body.scrollTop  + document.body.clientHeight/10;		
    dy = document.body.scrollTop  + 10;		
    //dy = document.body.scrollTop + document.body.clientHeight /2 - $('#id_window_data').height() / 2;
    obj.style.top = dy;					
    set_vis_to_select('visible');
		
	  dx = document.body.clientWidth / 2 - obj.clientWidth / 2;
	  if (!is_moved) obj.style.left = dx;		
  }
  else {
    set_vis_to_select('hidden');
    hide_obj.className='hide_all';
    hide_obj.style.width=document.body.clientWidth+'px';
    hide_obj.style.left=document.body.scrollLeft;
    hide_obj.style.height= document.body.clientHeight+'px';
    hide_obj.style.top=document.body.scrollTop;
		
		if ($('#id_window_data').attr('add_w')!=1){
			$('#id_window_data').attr('add_w',1);
					
		  $('#id_window_data').width($('#id_window_data').width()+30);
			$("#id_window_data").resizer();
			
			if ($('#id_window_data').height()>document.body.clientHeight){
				$('#id_window_data').height(document.body.clientHeight-50);
			}
			
			dx = document.body.clientWidth / 2 - $('#id_window_data').width() / 2;
  	  if (!is_moved) obj.style.left = dx;
		}		
  }
}
/*End window*/

var old_ClassName='btn_normal';
var old_Button='';

/*Begin big buttons*/
function do_over(obj) {
  if (old_Button!='') old_Button.className='btn_normal';
  old_Button=obj;
  old_ClassName=obj.className;
  obj.className='btn_over';
}
function do_out(obj) {
  //obj.className=old_ClassName;
}
/*End big buttons*/

/*Begin data lines*/
function do_over_line(obj) {
  obj.className='line_over';
}
function do_out_line(obj,old_ClassName_line) {
  obj.className=old_ClassName_line;
}
/*End data lines*/

function checkAll(inp,in_form) {
  var l=in_form.elements;
  for (i=0; i < l.length; i++) {
    if ((l[i].type=='checkbox')&&(l[i].id!='whithout_committee')) l[i].checked=inp.checked;
  }
}

/*Begin for calendar*/
var oldLink = null;
// code to change the active stylesheet
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  //if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    //cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}
/*End for calendar*/

function show_details_answer(obj){
	obj_t=document.getElementById('id_table_details');
	if (obj_t!=null){
		if (obj_t.style.display=='block'){
			obj_t.style.display='none';
			obj.innerHTML='+ details';
		}
		else {
			obj_t.style.display='block';
			obj.innerHTML='- details';			
		}		
	}
}

