// Send email from form
// Nicola Zordan, Wednesday 21 February 2001


var FormObject2Validate;
var MissingDataText='Missing data:';
var RequiredText="is required";
var NotMatchingDataText='NOT Matching:';
var DoesNotMatch="does NOT match";

//var ValidateFields=['Company','CompanyAddress','CompanyCity','CompanyState','CompanyZip','Name','Position','eMail','Tel','OS400version','AS400IPaddress','AS400User','AS400password','LANSAversion','AS400optical'];
//var ValidateFields=['CompanyName','CompanyAddress','CompanyCity','CompanyState','CompanyZip','ContactName','ContactPosition','ContactEMail','ContactTel','OS400version','AS400ipAddress','AS400user'];
//var ValidateFields=['CompanyName','CompanyAddress','CompanyCity','CompanyState','CompanyZip','ContactName','ContactPosition','ContactEMail','ContactTel','OS400version','AS400ipAddress','AS400user','CompiledBy','CommunicationVerified'];
var ValidateFields=[];
//var ValidateFieldsMatch=[['AS400password','AS400passwordVerify'],['DMSSYSpasswordVerify','DMSSYSpasswordVerify']];
var ValidateFieldsMatch=[];

document.write('<form method="GET" name="ValidateSendEmailForm"></form>');


function ValidateSend() {
//var form=document.NewClient,i, Message='',r=false;
//var form=document.NewClient,i, Message='',r=false, formSend=document.NewClientSend;
var form=FormObject2Validate,i, Message='',r=false, formSend=ValidateSendEmailForm;
var value=null;
  //form.FormURL.value=document.URL;
//  if (form.AS400password.value!='' && form.AS400password.value!=form.AS400passwordVerify.value) {
/*
  if (form.AS400password.value!=form.AS400passwordVerify.value) {
    Message=Message+'\nUser Passwords do NOT match';
    form.AS400password.focus();
  };
  if (form.DMSSYSpassword.value!=form.DMSSYSpasswordVerify.value) {
    Message=Message+'\nDMSSYS Passwords do NOT match';
    form.DMSSYSpassword.focus();
  };
*/
  for (i=0;i<ValidateFields.length;i++) {
    //alert(i+'. '+ValidateFields[i]+'=['+form[ValidateFields[i]].value+']');
    value=form[ValidateFields[i]].value;
    if (form[ValidateFields[i]].type=='checkbox') {
      value='';
      if (form[ValidateFields[i]].checked) {
        value=form[ValidateFields[i]].value;
      }
    };
    if (value.length==0) {  
      //if (form[ValidateFields[i]].value=='') {  
      if(Message=='') form[ValidateFields[i]].focus();
      //Message=Message+'\n'+ValidateFields[i]+' is required';
      Message=Message+'\n'+ValidateFields[i]+' '+RequiredText;
    };
  };  
  Message=Message+ValidateMatch();
  r=(Message=='');
//  if (r=()) {
//    form.submit();
//   } else {
//    alert('Missing data: \n'+Message);
//  }; 
//  alert(r);
  if (!r) {
    //alert('Missing data: \n'+Message);
    alert(MissingDataText+'\n'+Message);
   } else {
    var message=CreateEmail(form);
    //form.action=message;
    if (nextpage!=null) {
      form.action=nextpage;
    };
    formSend.action=message;
    //alert('before '+nextpage);
    formSend.submit();
    //form.submit();
    if (nextpage!=null) {
      //-alert('jumping: '+nextpage)
      document.location=nextpage;
      //window.location=nextpage;
    };
    //alert('after '+nextpage);
  };
//alert(r+'\n'+nextpage);
  //return(r); 
  // do not submit, redirect
  return false;
};


function ValidateOnly() {
//var form=document.NewClient,i, Message='',r=false;
//var form=document.NewClient,i, Message='',r=false, formSend=document.NewClientSend;
var form=FormObject2Validate,i, Message='',r=false, formSend=ValidateSendEmailForm;
var value=null;
  //form.FormURL.value=document.URL;
//  if (form.AS400password.value!='' && form.AS400password.value!=form.AS400passwordVerify.value) {
/*
  if (form.AS400password.value!=form.AS400passwordVerify.value) {
    Message=Message+'\nUser Passwords do NOT match';
    form.AS400password.focus();
  };
  if (form.DMSSYSpassword.value!=form.DMSSYSpasswordVerify.value) {
    Message=Message+'\nDMSSYS Passwords do NOT match';
    form.DMSSYSpassword.focus();
  };
*/
  for (i=0;i<ValidateFields.length;i++) {
    //alert(i+'. '+ValidateFields[i]+'=['+form[ValidateFields[i]].value+']');
    value=form[ValidateFields[i]].value;
    if (form[ValidateFields[i]].type=='checkbox') {
      value='';
      if (form[ValidateFields[i]].checked) {
        value=form[ValidateFields[i]].value;
      }
    };
    if (value.length==0) {  
      //if (form[ValidateFields[i]].value=='') {  
      if(Message=='') form[ValidateFields[i]].focus();
      //Message=Message+'\n'+ValidateFields[i]+' is required';
      Message=Message+'\n'+ValidateFields[i]+' '+RequiredText;
    };
  };  
  Message=Message+ValidateMatch();
  r=(Message=='');
//  if (r=()) {
//    form.submit();
//   } else {
//    alert('Missing data: \n'+Message);
//  }; 
//  alert(r);
  if (!r) {
    //alert('Missing data: \n'+Message);
    alert(MissingDataText+'\n'+Message);
    return false;
   } else {
    var message=CreateEmail(form);
//alert(message);
    //form.action=message;
    if (nextpage!=null) {
      form.action=nextpage;
    };
    //--formSend.action=message;
    //alert('before '+nextpage);
    //--formSend.submit();
    //form.submit();
    if (nextpage!=null) {
      //-alert('jumping: '+nextpage)
      //--document.location=nextpage;
      //window.location=nextpage;
    };
    return true;
    //alert('after '+nextpage);
  };
//alert(r+'\n'+nextpage);
  //return(r); 
  // do not submit, redirect
  return false;
};



function ValidateMatch() {
  var Message='',value0,value1, form=FormObject2Validate;
  for (i=0;i<ValidateFieldsMatch.length;i++) {
    //alert(i+'. \n'+ValidateFieldsMatch[i][0]+'=['+form[ValidateFieldsMatch[i][0]].value+']'+'\n'+ValidateFieldsMatch[i][1]+'=['+form[ValidateFieldsMatch[i][1]].value+']');
    value0=form[ValidateFieldsMatch[i][0]].value;
    value1=form[ValidateFieldsMatch[i][1]].value;
    if (form[ValidateFieldsMatch[i][0]].type=='checkbox') {
      value0='';
      value1='';
      if (form[ValidateFieldsMatch[i][0]].checked) {
        value0=form[ValidateFieldsMatch[i][0]].value;
      };
      if (form[ValidateFieldsMatch[i][1]].checked) {
        value1=form[ValidateFieldsMatch[i][1]].value;
      };
    };
    if (value0!=value1) {  
      if(Message=='') form[ValidateFieldsMatch[i][0]].focus();
      Message=Message+'\n'+ValidateFieldsMatch[i][0]+' '+DoesNotMatch+' '+ValidateFieldsMatch[i][1];
    };
  };  
  if (Message.length>0) {
    Message='\n\n'+NotMatchingDataText+'\n'+Message;
  };
  return Message;
};



var nextpage=null;
function CreateEmail (form) {
// required fields: emailto, nextpage, subject
 var text='', url='',i=0, email,subject, filed='', value='', nextpage0;
  text=text+'SentFromPage: '+document.location+'%0d%0a'+'%0d%0a';
  //url=url+'&body'+text;
  //text=text+'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
  //text=text+'<html>';
  //text=text+'<head><title>New Qwik-Order client</title></head>';
  //text=text+'<body>';
  for (i=0;i<form.length;i++) {
    field=form[i];
    value=form[i].value;    
    if (field.type=='button') { value=null; };
    if (field.type=='reset') { value=null; };
    //if (field.type=='submit') { value=null; };
    if (field.type=='submit') { value=null; };
    if (field.type=='checkbox') { 
      if (!field.checked) { value=null; value='-'; };
    };
    if (field.type=='radio') { 
      if (!field.checked) { value=null; };
    };
    if (field.type=='select') { value=filed[field.selectedIndex].value; };
    if (field.type=='text') { };
    ////if (field.type=='textarea') { value=('\n'+value).replace(/\n/g,'%0D%0A'); };
    //if (field.type=='textarea') { value=('\n'+value).replace(/\n/g,'%0D%0A'); };
    if (field.type=='textarea') { value=('\n'+value).replace(/\n/g,'%0d%0a'); };

    if (field.name=='emailto') {
      email=value;
      value=null;
    };
    if (field.name=='subject') {
      subject=value;
      value=null;
    };
    if (field.name=='nextpage') {
      nextpage=value;
      value=null;
    };

    if (value!=null) {
      //text=text+form[i].name+': '+form[i].value+'\n';    
      //text=text+form[i].name+': '+value+'%0D%0A';
      //text=text+form[i].name+': '+value+'<br>';
      //text=text+form[i].name+': '+value+'\n';
      text=text+form[i].name+': '+value+'%0d%0a';
    };
  };
  //text=text+'</body>';
  //text=text+'</html>';

  if (email==null) { email='Nicola@Zordan.net';  };
  if (subject==null) { subject='Nicola Zordan email form script';  };
  if (nextpage==null) { nextpage='http://www.NicolaZordan.com';  };

  url='mailto:'+email; 
  url=url+'?subject='+subject;
  url=url+'&body='+text;
  url=url+'%0d%0a';
  //alert(url);
  return(url);
};


function CreateMessage (form,email, subject) {
 var text='', url='',i=0;
  url='mailto:'+email; 
  url=url+'?subject='+subject;
  //url=url+'&body'+text;
  for (i=0;i<form.length;i++) {
    //text=text+form[i].name+': '+form[i].value+'\n';
    text=text+form[i].name+': '+form[i].value+'%0D%0A';
  };
  url=url+'&body='+text;
  //alert(url);
  return(url);
};



