package control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserMod;

/**
 * Servlet implementation class Ajax
 */
public class Ajax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ajax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setHeader("Cache-Control", "no-cache");
	    response.setContentType("text/html; charset=UTF-8");
	    
		
		String rep = request.getParameter("rep");
		String userName = request.getParameter("user");
		String email = request.getParameter("email");
		UserMod usermod = new UserMod();
		boolean isUserExist = usermod.checkRegister(userName);
		boolean isEmailex = usermod.checkEmail(email);
		
		if(rep.equals("checkuser")){ // Check User Available
			if (isUserExist == true){				
				response.getWriter().write("false");
			} else {
				response.getWriter().write("true");
			}
		}
		
		if(rep.equals("checkemail")){ // Check Email Availabale
			if (isEmailex == true){
				response.getWriter().write("false");
			} else {
				response.getWriter().write("true");
			}
		}
	    
	}

}
