var pn=0;
var glb_i;
var glb_j;
var glb_id;

function init_preloader() {
  loader=new Image(16,16);
  loader.src="img/ajax_loader.gif";
  return loader;
}

function start_preloader(image, src) {
  t_image=new Image(100,80);
  t_image.src=src;
  return t_image;
}

function run_preloader(t_image,image) {
  if (t_image.complete) {
    document.getElementById(image.id).src=t_image.src;
  }
  timerID=setTimeout("run_preloader(t_image,image)",10);
}

LOADER=init_preloader();

function preload(image, src) {
  t_image=start_preloader(image,src);
  run_preloader(t_image,image);
}

function getHandle() {
  var handle;
  try { handle=new XMLHttpRequest(); }
  catch (e) {
    try { handle=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { handle=new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) {
        return false;
      }
    }
  }
  return handle;
}

function change_attrib(nodes) {
  for (var i=0; i<nodes.length; i++) {
    var id=nodes[i].id;
    if (id) nodes[i].id=id+pn;
    var name=nodes[i].name;
    if (name) nodes[i].name=name+"["+pn+"]";
  }
  return nodes;
}

function add_person() {
  // Delete old button.
  if (pn>0) {
    var par=document.getElementById("plus_button"+(pn-1)).parentNode;
    par.removeChild(document.getElementById("plus_button"+(pn-1)));

    var par=document.getElementById("submit_button"+(pn-1)).parentNode;
    par.removeChild(document.getElementById("submit_button"+(pn-1)));
  }
  var person=document.getElementById("formbox_root").cloneNode(true);
  person.id="formbox";
  var childs=person.childNodes;

  // Change the id name and for attribute for all input.
  inputs=person.getElementsByTagName("input");
  inputs=change_attrib(inputs);

  select=person.getElementsByTagName("select");
  select=change_attrib(select);

  button=person.getElementsByTagName("button");
  button=change_attrib(button);

  label=person.getElementsByTagName("label");
  label=change_attrib(label);

  var insert=document.getElementById("write_form_list");
  insert.parentNode.insertBefore(person,insert);
  pn++;
}

function to_soft(id,i,j,age,pl,pu,pers) {
  var handle=getHandle();
  glb_i=i;
  glb_j=j;
  handle.onreadystatechange=function() {
    if (handle.readyState==4) {
      document.getElementById("puff "+glb_i+" "+glb_j).innerHTML=handle.responseText;
	  var id=document.getElementById("id "+glb_i+" "+glb_j).value;
	  pre_preload(id,"image "+glb_i+" "+glb_j);
    }
  }
  handle.open("GET","to_soft.php?id="+id+"&i="+i+"&j="+j+"&age="+age+"&pl="+pl+"&pu="+pu+"&person="+pers,true);
  handle.send(null);
}

function has_already(id,i,j,age,pl,pu,pers) {
  var handle=getHandle();
  glb_i=i;
  glb_j=j;
  handle.onreadystatechange=function() {
    if (handle.readyState==4) {
      document.getElementById("puff "+glb_i+" "+glb_j).innerHTML=handle.responseText;
	  var id=document.getElementById("id "+glb_i+" "+glb_j).value;
	  pre_preload(id,"image "+glb_i+" "+glb_j);
	}
  }
  handle.open("GET","has_already.php?id="+id+"&i="+i+"&j="+j+"&age="+age+"&pl="+pl+"&pu="+pu+"&person="+pers,true);
  handle.send(null);
}

function change_puff(id,i,j,age,pl,pu,pers) {
  var handle=getHandle();
  glb_i=i;
  glb_j=j;
  handle.onreadystatechange=function() {
    if (handle.readyState==4) {
      document.getElementById("puff "+glb_i+" "+glb_j).innerHTML=handle.responseText;
	  var id=document.getElementById("id "+glb_i+" "+glb_j).value;
	  pre_preload(id,"image "+glb_i+" "+glb_j);
	}
  }
  handle.open("GET","change_puff.php?action=&id="+id+"&i="+i+"&j="+j+"&age="+age+"&pl="+pl+"&pu="+pu+"&person="+pers,true);
  handle.send(null);
}

function pre_preload(id,image_id) {
  // Preload image.
  document.getElementById(image_id).src=LOADER.src;
  handle=getHandle();
  glb_id=image_id;
  handle.onreadystatechange=function() {
  	if (handle.readyState==4) {
	  image=document.getElementById(glb_id);
	  src="thumbs/"+handle.responseText;
	  preload(image,src);
	}
  }
  handle.open("GET","get_image.php?id="+id,true);
  handle.send(null);
}


