function cIn(nam,val){
  var oInput = document.createElement("input");
  oInput.name = nam;
  oInput.value = val;
  oInput.type = 'hidden';
  return oInput;
}

function fixSubmitToMyForm() 
{
  var i;
  var a = document.getElementsByTagName('input'); // find all existing inputs in this document
  for (i=0;i<a.length;i++) {
    if (a[i].type == 'submit') {
      a[i].onclick = sendMyForm; // fix form 'submit' buttons do to what i want :)
      //a[i].type = 'button';
    }
  }
}

function getAllInputData(){
  var s = '';
  var i;
  var a = document.getElementsByTagName('input'); // find all existing inputs in this document
  for (i=0;i<a.length;i++) {
    if (a[i].type == 'submit') {
    } else if (a[i].type == 'checkbox') {
      s += '\n' + a[i].name + ' = ' + (a[i].checked ? '1' : '0') + ';';
    } else {
      s += '\n' + a[i].name + ' = ' + a[i].value + ';';
    }
  }
  var a = document.getElementsByTagName('select');
  for (i=0;i<a.length;i++) {
    if (a[i].selectedIndex>=0) {
      s += '\n' + a[i].name + ' = ' + (a[i].options[a[i].selectedIndex].value || a[i].options[a[i].selectedIndex].innerHTML) + ';';
    }
  }
  var a = document.getElementsByTagName('textarea');
  for (i=0;i<a.length;i++) {
    s += '\n' + a[i].name + ' = ' + (a[i].value || a[i].innerHTML) + '\n;';
  }

  s = s.replace(/<[\/]/g,'< /');  // remove anything that might make the code break

  return s;
}

function getDocName() {
  var s = document.location.href;
  s = s.replace(/.+[\/]/,'');
  s = s.replace(/\.htm[l]?/i,'');
  return s;
}

function sendMyForm()
{debugger
  var sAllData = getAllInputData();
  var oForm = document.createElement("form");
  oForm.name = 'myForm';
  oForm.id = 'myForm';
  oForm.method = 'POST';
  oForm.action = 'sendmail.php';
  document.body.appendChild(oForm);
  oForm.appendChild(cIn('oria_is','king'));
  oForm.appendChild(cIn('email','automail@danidesign.hostrick.co.il'));
  oForm.appendChild(cIn('to','dani@danidesign.hostrick.co.il'));
  oForm.appendChild(cIn('savetofile','admin/logemails.txt'));
  oForm.appendChild(cIn('subject','automail: ' + getDocName()));

  var oInput = document.createElement("textarea");
  oInput.name = 'msg';
  oInput.innerHTML = sAllData;
  oInput.value = sAllData;
  oInput.style.display = 'none';
  oForm.appendChild(oInput);
  document.getElementById('myForm').submit();
  return false;
}

window.onload = fixSubmitToMyForm;
