// Function to send a page by email
function sendPageByEmail(send_page_url, language) {
  // Retrieve form object
  form_object = document.getElementById('send_page_form');
  // Retrieve input values
  email_address = document.getElementById('send_page_email').value;
  mail_text = document.getElementById('send_page_text').value;
  page_to_send = document.getElementById('send_page_page').value;
  request_code = document.getElementById('send_page_request_code').value;

  if (page_to_send == '') {
    if (language == 'en') {
      alert('Sorry, but this page cannot be sent.');
    } else {
      alert('Purtroppo, questa pagina non puo\' essere spedita.');
    }
    return;
  }

  // All browsers but Internet Explorer
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } 
  // Only for Internet Explorer
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  // If the XMLHttpRequest object has not been initialized, submit the form
  if ((typeof(xmlhttp) == 'undefined') || (!xmlhttp)) {
    form_object.action = send_page_url;
    form_object.submit();
    return;
  }

  // Function that process the XML data returned by the request
  xmlhttp.onreadystatechange = function() {
    // When the request is completed
    if (xmlhttp.readyState == 4) {
      // The response status will be 200 if everything goes well
      if (xmlhttp.status == 200) {
        if (language == 'en') {
          alert('The webpage has been sent successfully.');
        } else {
          alert('La pagina e\' stata spedita con successo.');
        }
      // The response status is not 200: there's a problem
      } else {
        if (language == 'en') {
          alert('There\'s been a problem sending the e-mail. Check data and try again.');
        } else {
          alert('Si e\' verificato un problema durante la spedizione. Verifica i dati e prova di nuovo.');
        }
      }
    }
  }

  // Send the request  
  xmlhttp.open("POST", send_page_url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send('send_page_email=' + email_address + '&send_page_text=' + mail_text + '&send_page_page=' + page_to_send + '&send_page_request_code=' + request_code);
}

// Function to suggest a charity
function sendCSR(send_page_url, language) {
  // Retrieve form object
  form_object = document.getElementById('csr_form');
  // Retrieve input values
  csr_name = document.getElementById('csr_name').value;
  csr_website = document.getElementById('csr_website').value;
  csr_responsible = document.getElementById('csr_responsible').value;
  csr_telephone = document.getElementById('csr_telephone').value;
  csr_email = document.getElementById('csr_email').value;
  csr_description = document.getElementById('csr_description').value;
  csr_request_code = document.getElementById('csr_request_code').value;

  if ((csr_name == "") ||
      (csr_website == "") ||
      (csr_responsible == "") ||
      (csr_telephone == "") ||
      (csr_email == "") ||
      (csr_description == "")) {
    if (language == 'it') {
      alert('Tutti i campi devono essere compilati!');
    } else {
      alert('All the fields should be filled!');
    }    
    return;  
  }

  // All browsers but Internet Explorer
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } 
  // Only for Internet Explorer
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  // If the XMLHttpRequest object has not been initialized, submit the form
  if ((typeof(xmlhttp) == 'undefined') || (!xmlhttp)) {
    form_object.action = send_page_url;
    form_object.submit();
    return;
  }

  // Function that process the XML data returned by the request
  xmlhttp.onreadystatechange = function() {
    // When the request is completed
    if (xmlhttp.readyState == 4) {
      // The response status will be 200 if everything goes well
      if (xmlhttp.status == 200) {
        if (language == 'en') {
          alert('Your suggestion has been sent successfully. You will be contacted as soon as possible.');
        } else {
          alert('La tua proposta e\' stata inviata con successo. Verrai contattato quanto prima.');
        }
      // The response status is not 200: there's a problem
      } else {
        if (language == 'en') {
          alert('There\'s been a problem sending the e-mail. Check data and try again.');
        } else {
          alert('Si e\' verificato un problema durante la spedizione. Verifica i dati e prova di nuovo.');
        }
      }
    }
  }

  // Send the request  
  xmlhttp.open("POST", send_page_url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send('csr_email=' + csr_email + '&csr_name=' + csr_name + '&csr_responsible=' + csr_responsible + '&csr_telephone=' + csr_telephone + '&csr_description=' + csr_description + '&csr_website=' + csr_website + '&csr_request_code=' + csr_request_code);
}

// Function to subscribe a user to alerts
function pressreleasesSubscribe(send_page_url, language) {
  // Retrieve form object
  form_object = document.getElementById('alert_form');
  // Retrieve input values
  alert_firstname = document.getElementById('alert_firstname').value;
  alert_lastname = document.getElementById('alert_lastname').value;
  alert_company = document.getElementById('alert_company').value;
  alert_newspaper = document.getElementById('alert_newspaper').value;
  alert_address = document.getElementById('alert_address').value;
  alert_city = document.getElementById('alert_city').value;
  alert_province = document.getElementById('alert_province').value;
  alert_zipcode = document.getElementById('alert_zipcode').value;
  alert_country = document.getElementById('alert_country').value;
  alert_telephone = document.getElementById('alert_telephone').value;
  alert_fax = document.getElementById('alert_fax').value;
  alert_email = document.getElementById('alert_email').value;
  alert_type = document.getElementById('alert_type').value;
  alert_privacy = 0;
  if (document.getElementById('alert_privacy_y').checked)
    alert_privacy = 1;
  alert_request_code = document.getElementById('alert_request_code').value;

  if ((alert_firstname == "") ||
      (alert_lastname == "") ||
      (alert_email == "")) {
    if (language == 'it') {
      alert('Tutti i campi obbligatori devono essere compilati!');
    } else {
      alert('All mandatory fields should be filled!');
    }    
    return;  
  }

  if (alert_privacy == 0) {
    if (language == 'it') {
      alert('L\'accettazione dell\'informativa sulla privacy e\' necessaria per poter effettuare l\'iscrizione!');
    } else {
      alert('You must accept the privacy notice in order to subscribe!');
    }    
    return;  
  }

  // All browsers but Internet Explorer
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } 
  // Only for Internet Explorer
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  // If the XMLHttpRequest object has not been initialized, submit the form
  if ((typeof(xmlhttp) == 'undefined') || (!xmlhttp)) {
    form_object.action = send_page_url;
    form_object.submit();
    return;
  }

  // Function that process the XML data returned by the request
  xmlhttp.onreadystatechange = function() {
    // When the request is completed
    if (xmlhttp.readyState == 4) {
      // The response status will be 200 if everything goes well
      if (xmlhttp.status == 200) {
        html_response = xmlhttp.responseText;
        
        start_pos = html_response.indexOf('<span id="print-error">'); 
        
        if (start_pos >= 0) {
          end_pos = html_response.indexOf('</span>', start_pos);
          error_message = html_response.substr((start_pos + 23), (end_pos - start_pos - 23));
          alert(error_message);
        } else {
          if (language == 'en') {
            alert('The subscription request has been sent successfully');
          } else {
            alert('La richiesta di iscrizione e\' stata inviata con successo');
          }
        }
        
      // The response status is not 200: there's a problem
      } else {
        if (language == 'en') {
          alert('There\'s been a problem during the subscription. Check data and try again.');
        } else {
          alert('Si e\' verificato un problema durante l\'iscrizione. Controlla i dati e riprova.');
        }
      }
    }
  }

  // Send the request  
  xmlhttp.open("POST", send_page_url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send('name=' + alert_firstname + '&surname=' + alert_lastname + '&company=' + alert_company + '&newspaper=' + alert_newspaper + '&address=' + alert_address + '&city=' + alert_city + '&prov=' + alert_province + '&zipcode=' + alert_zipcode + '&country=' + alert_country + '&telephone=' + alert_telephone + '&fax=' + alert_fax + '&email=' + alert_email + '&privacy=' + alert_privacy + '&type=' + alert_type + '&language=' + language + '&alert_request_code=' + alert_request_code);
}
