public boolean sanity_check()throws SanityCheckException{
     ip_address=req.getRemoteAddr().trim();

     try {
         Scode= req.getParameterValues("Scode")[0].trim();
         if ( Scode.length()>20||!areallDigits(Scode,Scode.length()))throw new SanityCheckException("-1");
     } catch (NullPointerException e) {
//            appl_id=req.getParameterValues("appl_id")[0].trim();   
         order_number=req.getParameterValues("order_number")[0].trim();
         amount=retrieve_amount()+"USD";
         //req.getParameterValues("amount")[0].trim()+"USD";
         description=req.getParameterValues("description")[0].trim(); 
         refer_url=req.getParameterValues("refer_url")[0].trim();
         return true;
     }

     order_rank=new Long(Scode).longValue ();
     int i=0;
     for (i=0;i<25;i++) {
         //          out.println(i);out.flush();        placeholders for added diagnostics
         if (order_rank==Orderrefs[i]) {
             if (!IPs[i].equals(ip_address) ) {
                 //                   out.println(1);out.flush();
                 Orderrefs[i]=0;
                 throw new SanityCheckException("-2");
             }
             if (++trys[i]==10) {
                 Orderrefs[i]=0;
                 throw new SanityCheckException("-3");
             }
             sanity_spot=i;
             break;
         }
     }
     if (i==25)throw new SanityCheckException("-4");

     amount=Amounts[sanity_spot];
     order_number=Ordernums[sanity_spot];
     appl_id=ApplIDs[sanity_spot];
     refer_url=RefURLs[sanity_spot];
     description=Descrips[sanity_spot];


     try {
         full_name=req.getParameterValues("full_name")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("-5");
     }
     if (full_name.length()>32|| !areallPrintable(full_name,full_name.length()))throw new SanityCheckException("0");

     try {
         address1=req.getParameterValues("address1")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("1");
     }
     if ( address1.length()>20||!areallPrintable(address1,20))throw new SanityCheckException("2");

     try {
         address2=req.getParameterValues("address2")[0].trim();
     } catch (NullPointerException e) {
         address2=" ";
     }
     if ( !areallPrintable(address2,address2.length()))throw new SanityCheckException("3");


     try {
         address3=req.getParameterValues("address3")[0].trim();
     } catch (NullPointerException e) {
         address3=" ";
     }
     if ( !areallPrintable(address3,address3.length()))throw new SanityCheckException("4");
     if ( address1.length()+address2.length()+address3.length()>100)
         throw new SanityCheckException("5");

     try {
         city=req.getParameterValues("city")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("6");
     }
     if ( city.length()>30||!areallPrintable(address1,30))throw new SanityCheckException("7");

     try {
         state=req.getParameterValues("state")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("8");
     }
     if ( state.length()!=2||!areallPrintable(state,2))throw new SanityCheckException("9");

     try {
         zip=req.getParameterValues("zip")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("10");
     }
     if ( zip.length()!=5||!areallDigits(zip,5))throw new SanityCheckException("11");

     try {
         CreditCardType=req.getParameterValues("CreditCardType")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("12");
     }
     if ( CreditCardType.length()!=2||!areallPrintable(CreditCardType,2))throw new SanityCheckException("13");

     try {
         CreditCardNo=req.getParameterValues("CreditCardNo")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("14");
     }
     if (CreditCardNo.length()!=16||!areallDigits(CreditCardNo,16))throw new SanityCheckException("15");

     try {
         Month=req.getParameterValues("Month")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("16");
     }

     try {
         Year=req.getParameterValues("Year")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("16a");
     }

     if ( Month.length()!=2||!areallDigits(Month,2))throw new SanityCheckException("17");
     if ( Year.length()!=2||!areallDigits(Year,2))throw new SanityCheckException("17a");
     ExpDate=Month+"/"+Year;
     try {
         CVV=req.getParameterValues("CVV")[0].trim();
     } catch (NullPointerException e) {
         throw new SanityCheckException("18");
     }
     if (CVV.length()!=3||!areallDigits(CVV,3))throw new SanityCheckException("19");

     return false;
 }
     

    public void creditOK() throws IOException{
        boolean redir=true;
        String Redirect=null;
        try {
            Redirect=req.getParameterValues("Redirect")[0].trim();
        } catch (NullPointerException e) {
            redir=false;
        }
        if (redir) {
            res.sendRedirect(refer_url);
        } else {
            FormBuilder(file_base+appl_id+"thanks.tmpl");
            out.flush ();
        }   //if

    }
    String retrieve_amount()throws SanityCheckException{
        return  "11.00";
            }


    public  void record_transaction() {
           }



    

    public void FormBuilder(String file_name)throws IOException {
        BufferedReader linein;
        String Starter,Closer;
        Replacer Replacement_list;
        //PrintWriter fo=null;

        linein = new BufferedReader(new FileReader(file_name));
        try {
            //fo=new PrintWriter(new FileOutputStream(file_base+"tempform"));

            new ReWriter(linein,out, "}}","{{",new MakerList()).rewrite();
        } catch (ReWriteException r) {
        }
        linein.close ();
        out.flush();

    }

    public boolean ccvANDavs_OK()throws SanityCheckException{ 
        String avs_error="Please review your address." ;
        String avs_zip_error="Please check your ZIP code.";
        String cvv_error="Please check the 3 digit code on the back of your credit card.";

        if (CreditCardType.equals ("DS")) {
            if (!error[1].equals("YYA")) {
                if (error[1].equals ("YNA"))
                    error[1]=avs_zip_error;
                else
                    error[1]= avs_error;
                return false;
            }
          /*   if (!error[2].equals("M")) {
                 error[1]= cvv_error;
                 return false;
             }  */ 
            return true;
        }

        if (CreditCardType.equals ("MC")) {

            if (!error[1].equals("YYY")) {
                if (error[1].equals ("YNA"))
                    error[1]=avs_zip_error;
                else
                    error[1]= avs_error;
                return false;
            }
            if (!error[2].equals("M")) {
                error[1]= cvv_error;
                return false;
            }
            return true;
        }
        if (CreditCardType.equals ("VS")) {

            if (!error[1].equals("YYY")) {
                if (error[1].equals ("YNA"))
                    error[1]=avs_zip_error;
                else
                    error[1]= avs_error;
                return false;
            }
            if (!error[2].equals("M")) {
                error[1]= cvv_error;
                return false;
            }
            return true;
        }
        return false;
    }


    class MakerList implements Replacer {

        public String replace(String s) {
            if (s.equals ("amount"))return amount.substring (0,amount.indexOf("USD"));
            if (s.equals ("order_number"))return order_number;
            if (s.equals ("Transaction_Reference"))return Transaction_Reference;
            if (s.equals ("appl_id"))return appl_id;
            if (s.equals ("refer_url"))return refer_url;
            if (s.equals ("description"))return description;
            if (s.equals ("present_date_time"))return present_date_time;
            if (s.equals ("full_name"))return full_name;
            if (s.equals ("ID")) {
                return new Long(ID).toString ();
            }
            if (s.equals ("Etext")) {
                String error_text=error[0];
                if (error[0].equals ("OK"))error_text=error[1];//set in  ccvANDavs_notOK()
                else {
                    if (error[0].length()>15) {
                        if (error[0].substring (0,15).equals("General failure")) {
                            int i=error[0].indexOf ("Exception");
                            if (i>0)
                                error_text=error[0].substring (i+10,error[0].length ());
                        }
                    }
                }
                return "

"+error_text+"


" ; } if (s.equals ("Scode"))return Scode; try { return req.getParameterValues(s)[0]; } catch (NullPointerException f) { return " "; } } } // end MakerList void debug_check(String s){ /*synchronized(testit) { try { OutputStreamWriter fo=new OutputStreamWriter(new FileOutputStream(log_base+"debug.log",true)) ; fo.write(s+new Long(System.currentTimeMillis ()).toString()+"\n"); fo.close(); } catch (Exception e) { } }*/ } boolean areallPrintable(String str, int stsize){ for (int i=0;i