/*
 * ContactHandler.java
 *
 * Created on 3 de noviembre de 2004, 15:40
 */

package com.fenalco.servlets;



import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.fenalco.utilities.mail.MailHandler;

import com.fenalco.entities.DataAccess;
import com.fenalco.entities.Country;
import com.fenalco.entities.Province;
import com.fenalco.entities.City;

import java.util.Vector;
import java.util.Enumeration;
/**
 *
 * @author  Administrador
 * @version
 */
public class ContactHandler extends HttpServlet {
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Destroys the servlet.
     */
    public void destroy() {
        
    }
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String message = "";
        String url = "/contactenos.jsp";
        String socialName = "";
        String contact = "";
        String email = "";
        String address = "";
        String phoneNumber = "";
        String nit = "";
        String action = request.getParameter("action");
        String combo = "";
        String type = "Minorista";
        Vector countriesList = DataAccess.getValidCountries();
        Vector provincesList = null;
        Vector citiesList = null;

               
        int countryId = 1;
        int provinceId = 1;
        int cityId =1;
      
        provincesList = DataAccess.getValidProvinces(countryId);
        citiesList = DataAccess.getCities(provinceId);
        
           
        if(action != null){
            type = request.getParameter("company");
            socialName = request.getParameter("socialName");
            contact = request.getParameter("contact");
            email = request.getParameter("email"); 
            address = request.getParameter("address");
            phoneNumber = request.getParameter("phoneNumber");
            nit = request.getParameter("nit");
            
          
   
            if (action.equals("load")){
                
                combo = request.getParameter("combo");
                
                 countryId = ( new Integer ( request.getParameter("country") )).intValue();
                 provinceId = ( new Integer ( request.getParameter("province") )).intValue();
                 cityId = ( new Integer ( request.getParameter("city") )).intValue();
                 
                  if(combo.equals("country")){
              
                      Enumeration en = provincesList.elements();
                      provinceId = ((Province)en.nextElement()).getId();
                      citiesList = DataAccess.getCities( provinceId);
                      en = citiesList.elements();
                      cityId = ((City)en.nextElement()).getId();
                  }
                  else if(combo.equals("province")){
                      citiesList = DataAccess.getCities( provinceId);
                      Enumeration en = citiesList.elements();
                      cityId = ((City)en.nextElement()).getId();
                  }
                 
            }
                      
            else if(action.equals("submit")){
                MailHandler mh = MailHandler.getInstance();
        
                String mail = "Nueva solicitud de contacto para e-fenalco.com: \n" +
                              "-----------------------------------------------\n" +
                              "RAZON SOCIAL :" + socialName+
                              "\nNIT :" + nit+
                              "\nCONTACTO :" +contact+
                              "\nDIRECCION :" +address+
                              "\nTELEFONO :" +phoneNumber+
                              "\nPAIS :" +(new Country(countryId)).getName()+
                              "\nDEPARTAMENTO :" +(new Province(provinceId)).getName()+
                              "\nCIUDAD :" +(new City(cityId)).getName()+
                              "\nEMAIL :" +email+
                              "\nTIPO DE EMPRESA :"+type ;


                String from = (email == null || email.equals("")?"usuario@e-fenalco.com":email );
                String to = DataAccess.getPvlEmail();

                String subject = "Nueva solicitud contáctenos";
                if(mh.sendMail(from, to, subject, mail)){
                    message = contact+ " su solictud ha sido registrada exitosamente.";
                    socialName = "";
                    contact = "";
                    email = "";
                    address = "";
                    phoneNumber = "";
                    nit = "";
                    provincesList = DataAccess.getValidProvinces(1);
                    citiesList = DataAccess.getCities(1);
                    countryId = 1;
                    provinceId = 1;
                    cityId = 1;		
                }
                else{
                    message = contact+" en este momento su solictud no puede ser atendidar. Por favor inténtelo más tarde";
                }
            }
        }
        
    
        
        
        
        request.setAttribute("message",message);
        request.setAttribute("socialName",socialName);
        request.setAttribute("company",type);
        request.setAttribute("contact",contact);
        request.setAttribute("email",email);
        request.setAttribute("address",address);
        request.setAttribute("phoneNumber",phoneNumber);
        
        request.setAttribute("nit",nit);
        request.setAttribute("countriesList",countriesList);
        request.setAttribute("provincesList",provincesList);
        request.setAttribute("citiesList",citiesList);
        request.setAttribute("countryId",new Integer(countryId));
        request.setAttribute("provinceId",new Integer(provinceId));
        request.setAttribute("cityId",new Integer(cityId));
        
        
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);
        
        
    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    
}
