var ROOT = '/tmw';

var savetext='.....saving.....done.....';
var colcounter=0;
function animsavediv(id,state)	{
  if (state<savetext.length+1)	{
    colcounter=0;
    s='';
    for(i=0;i<state;i++) s+=savetext.substring(i,i+1);
    document.getElementById(''+id+'').innerHTML=s+'&nbsp';	
    if (state==0) document.getElementById(''+id+'').style.color='rgb(128,128,128)';
	  setTimeout('animsavediv(\''+id+'\','+(state+1)+');',10);
  } else	{
    num=10;s=0;
    colcounter++;cc=colcounter;
    if (cc>num)	{
      var a=document.getElementById(''+id+'').style;
      s=128+(cc-1-num)*16;
      if (s>255) s=255;
      a.color='rgb('+s+','+s+','+s+')';
    }
    if (s!=255) setTimeout('animsavediv(\''+id+'\','+(state+1)+');',30);
  }
}

function openPopup(link,sx,sy) {
  if (sx==null) sx=100;
  if (sy==null) sy=100;
  px=screen.width;
  py=screen.height;
  posx=px/2-sx/2;
  posy=py/2-sy/2;
  openerstr='height='+sy+',width='+sx+',scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,top='+posy+',left='+posx;
  window.open(link,null,openerstr);
}

function checkDate(date) {
  var re_date = /^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/;
  if (!re_date.test(date)) {
    return false;
  }
  var parts = date.split('/');
  var day = parseInt(parts[0], 10);
  var month = parseInt(parts[1], 10)-1;
  var year = parseInt(parts[2], 10);
  var date = new Date(year, month, day);
  date.setFullYear(year);
  date.setMonth(month);
  date.setDate(day);
  if (date.getFullYear() != year || date.getMonth() != month || date.getDate() != day) {
    return false;
  }
  return true;
}

function clearCookie(name) {
  alert(document.cookie);
  var d = new Date();
  d.setTime(d.getTime() - 1);
  document.cookie = name + "=; expires=" + d.toGMTString() + "; path=/";
  alert(document.cookie);
}

function clearForm(formid) {
  var inputs = $$('#' + formid + ' input');
  for (var i=0; i<inputs.length; i++) {
    var input = $(inputs[i]);

    // special fields
    if (input.id == 'operation' || input.id == 'destination') {
      continue;
    }

    // leave buttons alone
    if (input.type  == 'text' || input.type == 'hidden') {
      input.value = '';
    }
  }

  var selects = $$('#' + formid + ' select');
  for (var i=0; i<selects.length; i++) {
    var select = $(selects[i]);
    select.value = '0';
  }
}

function saveForm(formid, highlight) {
//  if (typeof(highlight) != 'string') {
//    highlight = 'save_' + pref;
//  }
  var data = {};
  
  var inputs = $$('#' + formid + ' input');
  var len = inputs.length;
  for (var i=0; i<len; i++) {
    var input = inputs[i];
    // special fields
    if (input.id == 'operation' || input.id == 'destination') {
      continue;
    }

    // don't save buttons
    if (input.type  == 'text' || input.type == 'hidden') {
      data[input.id] = $F(input);
    }

  }
  
  var selects = $$('#' + formid + ' select');
  var len = selects.length;
  for (var i=0; i<len; i++) {
    var select = selects[i];
    //data[select.id] = $F(select.id);
  }

  var url = ROOT + '/saveform.php';

  var params = $H(data).toQueryString();
  var req = new Ajax.Request(url, {method: 'post', parameters: params, onComplete: onSaveFormComplete.bind(this, highlight)});  
}

function onSaveFormComplete(savedivid) {
  animsavediv(savedivid, 0);
}

