<!--   Author : Jagbir Singh -->
<!--   Debug : Jagbir Singh -->
<!--   Tested by : Amanbir Singh -->

    <%
    response.setHeader("Cache-Control","no-store");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
    %>

   
    <%@ page language="java" import="java.sql.*,javax.sql.DataSource, javax.naming.*" errorPage="" %>
<%
DataSource pool;
int flag =0;
String registerQuery = null;
String drname =null;
String spclization = null;
String radioInJSP = request.getParameter("active");

double bmi = 0;
  Connection conn = null;
  
 PreparedStatement pstmt = null;
  String usrId = request.getParameter("userId");
  String pswd1 = request.getParameter("pswd1");
  String pswd2 = request.getParameter("pswd2");
  String fname = request.getParameter("firstname");
  String lname = request.getParameter("surname");
  String email = request.getParameter("email");
  
  String sex = request.getParameter("sex");


  String phnumber = request.getParameter("phNumber");
  String height = request.getParameter("height");
  String weight = request.getParameter("weight");
  String Occupation = request.getParameter("Occupation");
  
  
  String add2 = request.getParameter("address2");
  String age = request.getParameter("age");
  
  if(radioInJSP.equals("actpatient")){			//calculating BMI from height and weight
  double heightInch = Double.parseDouble(height);
  double weightLbs = Double.parseDouble(weight);
  		heightInch = (heightInch/2.5);
  		 bmi = ((weightLbs/(heightInch*heightInch))*703);		//BMI = weight(lbs)/power(height(inch),2) * 703
  			bmi = bmi * 100;
  			bmi = Math.round(bmi);
  			bmi = bmi/100;
  	
  }

  if(radioInJSP.equals("actpatient")){
   drname = request.getParameter("drName");
   }
  else{
	   spclization = request.getParameter("spcl");
  }
 
  
  try{
	  InitialContext ctx = new InitialContext();
      
       pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
      if (pool == null)
         throw new SQLException("Unknown DataSource 'jdbc/TestDB'");


      conn = pool.getConnection();
      // Open a connection

      if(conn != null){
	flag = 1; 
		
        }
      else
       
      if(radioInJSP.equals("actpatient")){
    	  registerQuery = "SELECT * from userInfo where user_Id = ?";
    	        }
      else{
    	  registerQuery = "SELECT * from drInfo where doc_Id = ?";
      }
      pstmt = conn.prepareStatement(registerQuery);
     
      pstmt.setString(1, usrId);
      ResultSet rs = pstmt.executeQuery();
      if(rs.next()){
    	  pstmt.close();
          conn.close();
          rs.close();
         
          pageContext.forward("errorRegister.jsp");		//if user exists give error
    	  
      }
      
      if(radioInJSP.equals("actpatient")){
      registerQuery =  ("INSERT into userInfo " +
              "(user_Id, passWord, email, firstName, lastName,seX, addressPerm, age, drName, conNumber, height, weight, docId, occupation, BMI ) "+
              "values ('"+usrId+"', '"+ pswd1+"', '"+email+"', '"+fname+"', '"+lname+"', '"+sex+"', '"+add2+"', '"+age+"', '"+drname+"', '"+phnumber+"', '"+height+"', '"+weight+"', '"+drname+"', '"+Occupation+"', '"+bmi+"')") ;
      }
      else{
    	  
    	  registerQuery =  ("INSERT into drInfo " +
                  "(doc_Id, passWord, email, firstName, lastName, addressPerm,  conNumber, spclZtion ) "+
                  "values ('"+usrId+"', '"+ pswd1+"', '"+email+"', '"+fname+"', '"+lname+"', '"+add2+"', '"+phnumber+"','"+spclization+"')") ;
      }
      pstmt = conn.prepareStatement(registerQuery);
      pstmt.executeUpdate();
    
      pstmt.close();
      conn.close();
      
  }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(pstmt!=null)
            pstmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
         conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   } //end try
   pageContext.forward("login.jsp");
%>

