#include "email.h"
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "coregrade_api.h"
#include "safestring.h"
#include "cfg.h"
#include <string>
#include "pgsql_wrapper.h"

void vars2form( CVars &v, C_CGI_Form &form );

extern int mailsend(CVars in,CVars &out);



long AccountPendingMail(CVars in)
{
	REQ_LONG( in, "pending_id", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");	
	
		char * server_name = getenv( "SERVER_NAME" );		
		form.LetStr( "server_name",  server_name );
		form.LetStr( "site_name",  "CoreGrade" );
 
	if ( load_db_record( x, "SELECT * FROM members_pending WHERE id = %lu ", in["pending_id"].Long()) ) 
	{	
	 vars2form( x, form );
	 form.LetStr( "email",   x["email"].c_str() );
	 form.Email( "ACCOUNT/member_pending.mailfile" );	
	}
  return 0;
}

long WelcomeAccountMail(CVars in)
{
	REQ_LONG( in, "member_id", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");	
	
		char * server_name = getenv( "SERVER_NAME" );		
		form.LetStr( "server_name",  server_name );
		form.LetStr( "site_name",  "CoreGrade" );
 
	if ( load_db_record( x, "SELECT * FROM members WHERE id = %lu ", in["member_id"].Long()) ) 
	{	
	 vars2form( x, form );
	 form.LetStr( "email",   x["email"].c_str() );
	 form.Email( "ACCOUNT/member_welcome.mailfile" );	
	}
  return 0;
	
}

  long CreateCoreGradeGroupMail(CVars in)
 {
 	REQ_LONG( in, "group_id", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");	
	
		char * server_name = getenv( "SERVER_NAME" );		
		form.LetStr( "server_name",  server_name );
		form.LetStr( "site_name",  "CoreGrade" );
 
	if ( load_db_record( x, "select g.*,m.email,m.firstname,m.lastname from members_groups g LEFT JOIN members m ON m.id = g.member_id WHERE g.id = %lu ", in["group_id"].Long()) ) 
	{	
	 vars2form( x, form );
	 form.LetStr( "email",   x["email"].c_str() );
	 form.Email( "GROUP/group_added.mailfile" );	
	}
   return 0;
 }
 
long GroupCreateMemberMail(CVars in)
 {
  	REQ_LONG( in, "group_id", 1, -1 );
	REQ_LONG( in, "group_member_id", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");	
	
		char * server_name = getenv( "SERVER_NAME" );		
		form.LetStr( "server_name",  server_name );
		form.LetStr( "site_name",  "CoreGrade" );
 
	if ( load_db_record( x, "SELECT gm.*,m.*,e.email FROM group_members gm LEFT JOIN members_groups m ON m.id=gm.group_id LEFT JOIN members e ON e.id=gm.member_id WHERE gm.id = %lu", in["group_member_id"].Long()) ) 
	{	
	 vars2form( x, form );
	 form.LetStr( "email",   x["email"].c_str() );
	 form.Email( "GROUP/group_member_added.mailfile" );	
	}
   return 0;
 return 0;
 }
//==============================================================================================================


long send_email(CVars in, CVars &out)
{
 logfmt( logINFO, "send_email()" );
 long ret = PHP_API_BAD_PARAM;
 try {
  REQ_LONG( in, "whattodo", 1, -1 );
  switch(in["whattodo"].Long()) {
   default: logfmt( logINFO, "Not implemented" );
  }
 } catch(bad_parameter) {
  out["status"] = "Unknow action supplied";
 }
 logfmt( logINFO, "/send_email()" );
 return ret;
}


long EmailCustBalUpdt(CVars in)
{
 REQ_LONG( in, "balance_update_id", 1, -1 );
 C_CGI_Form form("","");
 char * server_name = getenv( "SERVER_NAME" );
 CVars x;

 vars2form( x, form );
 form.LetStr( "site",  server_name );
 form.Email( "test.mailfile" );
 return 0;
}

long SendPromoEmail(CVars in)
{
	long pmode = REQ_LONG( in, "pmode", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");

	vars2form( in, form );	
	form.LetStr( "email",   in["email"].c_str() );
	
	 switch( pmode )
    {  
	case 1:
	  form.Email( "promo/promotion1.mailfile" );
	break;
	
	case 2:
	  form.Email( "promo/promotion2.mailfile" );
	break;
	
	}
	

	
  return 0;
}

long SendBalanceRequestAlert(CVars in)
{
	REQ_LONG( in, "request_id", 1, -1 );
	CVars x;
	CVars out; 
		C_CGI_Form form("","");
	out["topupemail"]  = CfgReadChar("transnotif.topupemail");

	if ( load_db_record( x, "SELECT a.*,a.amount*0.01 AS req_amount,c.*,a.ip AS ip_loc,u.name FROM customer_addbalance a LEFT JOIN customer c ON c.id=a.customer_id  LEFT JOIN bko_users u ON u.id=a.csr_id WHERE a.id = '%lu' ", in["request_id"].Long()) ) 
	{
	
	 vars2form( x, form );	
	 form.LetStr( "email",   out["topupemail"].c_str() );
	 form.Email( "alerts/balancereq.mailfile" );
	
	}
  return 0;
}

long SendBalanceCompleteAlert(CVars in)
{
	REQ_LONG( in, "request_id", 1, -1 );
	CVars x;
	CVars out; 
	C_CGI_Form form("","");
	out["topupemail"]  = CfgReadChar("transnotif.topupemail");

	if ( load_db_record( x, "SELECT a.*,a.amount*0.01 AS req_amount,c.*,a.ip2 AS ip_loc,u.name,a.cur_balance*0.01  AS cur2, a.fin_balance*0.01 AS fin2  FROM customer_addbalance a LEFT JOIN customer c ON c.id=a.customer_id  LEFT JOIN bko_users u ON u.id=a.csr_id WHERE a.id = '%lu' ", in["request_id"].Long()) ) 
	{	
	 vars2form( x, form );
	 form.LetStr( "email",   out["topupemail"].c_str() );
	 form.Email( "alerts/balancecomplete.mailfile" );
	
	}
  return 0;
}





long SystemStatus()
{
 logfmt( logINFO, "SystemStatus()*************~************");

 C_CGI_Form form("","");
 const char * server_name = getenv( "SERVER_NAME" );
 CVars x;

 vars2form( x, form );
 form.LetStr( "site",  server_name );
 
 form.LetStr( "message",  server_name );
 form.LetStr( "email",  "ses66181@gmail.com" );
// form.LetStr( "email",  "acidumirae@gmail.com" );

 form.Email( "test.mailfile" );
 return 0l;
}

long StartPassResetEmail(CVars in)
{
//customer_id
// reset_id 
	C_CGI_Form form("","");
	const char * server_name = getenv( "SERVER_NAME" );
	CVars out; 
	CVars x;
	load_db_record( x, "SELECT * FROM customer WHERE id = %lu",in["customer_id"].Long() ) ;
	vars2form( x, form );

		
	form.LetStr( "site",  server_name );
		/*
		CREATE TABLE lostpass (
		 id serial NOT NULL,
		 customer_id INT REFERENCES customer(id),
		 added TIMESTAMP DEFAULT NOW(),
		 updated TIMESTAMP DEFAULT NOW(),
		 loc INET,
		 reset_link VARCHAR(200) NOT NULL,
		 expire TIMESTAMP ,
		 status integer DEFAULT 1  
		);
		*/
    load_db_record( out, "SELECT added AS lost_added,* FROM lostpass WHERE id = %lu",in["reset_id"].Long() ) ;
	vars2form( out, form );
	
	
	form.LetStr( "email",   x["email"].c_str() );
	form.Email( "start_losspass.mailfile" );  //customer notification
	
	
 return 0;
 //start_losspass.mailfile
}

long CompletePassResetEmail(CVars in)
{
//customer_id
// reset_id 
	C_CGI_Form form("","");
	const char * server_name = getenv( "SERVER_NAME" );
	CVars out; 
	CVars x;
	load_db_record( x, "SELECT * FROM customer WHERE id = %lu",in["customer_id"].Long() ) ;
	vars2form( x, form );

		
	form.LetStr( "site",  server_name );

	
	
	form.LetStr( "email",   x["email"].c_str() );
	form.Email( "complete_losspass.mailfile" );  //customer notification
	
	
 return 0;
 //start_losspass.mailfile
}

long CustomerAirSalePayment(CVars in)
{
/*
payquic_test=> select t.*,c.* from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id  limit 1;
 id | customer_id |  orderid   | description | amount | txnref | payref | retref | status |           added            | resp | recipient_no | completed | appramt | rspcode | id |      username      | firstname | lastname |       email        |               pass
               | status |           added            | balance 
----+-------------+------------+-------------+--------+--------+--------+--------+--------+----------------------------+------+--------------+-----------+---------+---------+----+--------------------+-----------+----------+--------------------+-------------------
---------------+--------+----------------------------+---------
  2 |           1 | 1452581056 | 08174596143 |   1000 |        |        |        |      1 | 2014-09-15 21:00:39.921535 |      | 08174596144  |           |       0 |         |  1 | ses66181@gmail.com | Olu       | Amey     | ses66181@gmail.com | d0fbea2563b377ea70
74bced45c88dcb |      1 | 2014-07-09 13:52:02.478932 |  189700
(1 row)

*/
CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x,y;
	load_db_record( x, "select t.*,c.*,c.loc AS loc_c,c.balance*0.01 AS balance2,t.amount AS amount2,c.email AS email2,t.added AS trans_date from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id WHERE t.orderid='%s'",in["orderid"].c_str() ) ;
    vars2form( x, form );

	load_db_record( y, "SELECT country_code,country_name,continent_code,continent_name FROM ipligence2 WHERE ip_from <= ip2int('%s') AND ip_to>=ip2int('%s') LIMIT 1",x["loc_c"].c_str(),x["loc_c"].c_str());
	/*
	country_code | country_name  | continent_code | continent_name
	--------------+---------------+----------------+----------------
	US           | UNITED STATES | NA             | NORTH AMERICA
	*/	   
	vars2form( y, form );
	  
	
			if ( in["pmode"].Long() == PAY_MODE_BALANCE )  // paying with balance
			{
                   form.LetStr( "paymode",  "Payment with balance" );
			}
			else
			{
                    form.LetStr( "paymode",  "Payment with card" );
			}
					   
	
	form.LetStr( "site",  server_name );
    form.LetStr( "email",   x["email2"].c_str() );
	form.Email( "topup_payment.mailfile" );  //customer notification
	
	
	
	 form.LetStr( "email",   out["topupemail"].c_str() );
	form.Email( "topup_payment_alert.mailfile" );  //customer notification
	
	
 return 0l;


}

long CustomerAirBonus(CVars in)
{
/*
payquic_test=> select t.*,c.* from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id  limit 1;
 id | customer_id |  orderid   | description | amount | txnref | payref | retref | status |           added            | resp | recipient_no | completed | appramt | rspcode | id |      username      | firstname | lastname |       email        |               pass
               | status |           added            | balance 
----+-------------+------------+-------------+--------+--------+--------+--------+--------+----------------------------+------+--------------+-----------+---------+---------+----+--------------------+-----------+----------+--------------------+-------------------
---------------+--------+----------------------------+---------
  2 |           1 | 1452581056 | 08174596143 |   1000 |        |        |        |      1 | 2014-09-15 21:00:39.921535 |      | 08174596144  |           |       0 |         |  1 | ses66181@gmail.com | Olu       | Amey     | ses66181@gmail.com | d0fbea2563b377ea70
74bced45c88dcb |      1 | 2014-07-09 13:52:02.478932 |  189700
(1 row)

*/
CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x;
	load_db_record( x, "select t.*,c.*,c.balance*0.01 AS balance2,t.amount AS amount2,c.email AS email2,t.added AS trans_date from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id WHERE t.orderid='%s'",in["orderid"].c_str() ) ;
    vars2form( x, form );

	
			if ( in["pmode"].Long() == PAY_MODE_BALANCE )  // paying with balance
			{
                   form.LetStr( "paymode",  "Payment with balance" );
			}
			else
			{
                    form.LetStr( "paymode",  "Payment with card" );
			}
					   
	
	form.LetStr( "site",  server_name );
    form.LetStr( "email",   x["email2"].c_str() );
	form.Email( "topup_bonus_completed.mailfile" );  //customer notification
 return 0l;


}


long CustomerAirSale(CVars in)
{
/*
payquic_test=> select t.*,c.* from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id  limit 1;
 id | customer_id |  orderid   | description | amount | txnref | payref | retref | status |           added            | resp | recipient_no | completed | appramt | rspcode | id |      username      | firstname | lastname |       email        |               pass
               | status |           added            | balance 
----+-------------+------------+-------------+--------+--------+--------+--------+--------+----------------------------+------+--------------+-----------+---------+---------+----+--------------------+-----------+----------+--------------------+-------------------
---------------+--------+----------------------------+---------
  2 |           1 | 1452581056 | 08174596143 |   1000 |        |        |        |      1 | 2014-09-15 21:00:39.921535 |      | 08174596144  |           |       0 |         |  1 | ses66181@gmail.com | Olu       | Amey     | ses66181@gmail.com | d0fbea2563b377ea70
74bced45c88dcb |      1 | 2014-07-09 13:52:02.478932 |  189700
(1 row)

*/
CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x,y;
	load_db_record( x, "select t.*,c.*,c.loc AS loc_c,c.balance*0.01 AS balance2,t.amount AS amount2,c.email AS email2,t.added AS trans_date from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id WHERE t.orderid='%s'",in["orderid"].c_str() ) ;
    vars2form( x, form );

 load_db_record( y, "SELECT country_code,country_name,continent_code,continent_name FROM ipligence2 WHERE ip_from <= ip2int('%s') AND ip_to>=ip2int('%s') LIMIT 1",x["loc_c"].c_str(),x["loc_c"].c_str());
	/*
	country_code | country_name  | continent_code | continent_name
	--------------+---------------+----------------+----------------
	US           | UNITED STATES | NA             | NORTH AMERICA
	*/	   
	vars2form( y, form );
	
	
			if ( in["pmode"].Long() == PAY_MODE_BALANCE )  // paying with balance
			{
                   form.LetStr( "paymode",  "Payment with balance" );
			}
			else
			{
                    form.LetStr( "paymode",  "Payment with card" );
			}
					   
	
	form.LetStr( "site",  server_name );
    form.LetStr( "email",   x["email2"].c_str() );
	form.Email( "topup_completed.mailfile" );  //customer notification
 return 0l;


}


long VirtualAirSaleCustomer(CVars in)
{

CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x,y;
	load_db_record( x, "select t.*,c.*,c.loc AS loc_c,c.balance*0.01 AS balance2,t.amount AS amount2,c.email AS email2,t.added AS trans_date from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id WHERE t.orderid='%s'",in["orderid"].c_str() ) ;
    vars2form( x, form );

	load_db_record( y, "SELECT country_code,country_name,continent_code,continent_name FROM ipligence2 WHERE ip_from <= ip2int('%s') AND ip_to>=ip2int('%s') LIMIT 1",x["loc_c"].c_str(),x["loc_c"].c_str());
	/*
	country_code | country_name  | continent_code | continent_name
	--------------+---------------+----------------+----------------
	US           | UNITED STATES | NA             | NORTH AMERICA
	*/	   
	vars2form( y, form );
	
	CVars z;
	load_db_record( z, "SELECT t.paymentcode,i.responsecode,i.transactionref FROM interswitch_bill_payment_advice t LEFT JOIN interswitch_transaction i ON i.detail_id=t.id WHERE t.order_id ='%s'",x["orderid"].c_str());
	vars2form( z, form );
	
			if ( in["pmode"].Long() == PAY_MODE_BALANCE )  // paying with balance
			{
                   form.LetStr( "paymode",  "Payment with balance" );
			}
			else
			{
                    form.LetStr( "paymode",  "Payment with card" );
			}
					   
	
 form.LetStr( "site",  server_name );
 
 form.LetStr( "message",  server_name );
 form.LetStr( "email",   out["topupemail"].c_str() );
 form.Email( "virtualtopup.mailfile" );  // this is alert
 return 0l;


}

void  CustomerBalanceEmail(CVars in)
{
	REQ_STRING( in, "orderid", 1, 50, "(.*)");
	CVars x;
		C_CGI_Form form("","");
	if ( load_db_record( x, "select c.*,t.*,t.added AS balance_date,appramt*0.01 AS appramt2,t.added AS trans_date,c.balance*0.01 AS balance2 from trans_balance_topup t LEFT JOIN customer c ON c.id =  t.customer_id where t.orderid = '%s' AND completed IS NOT NULL", in["orderid"].c_str()) ) 
	{
		 vars2form( x, form );
		 form.Email( "balance_updated.mailfile" );
	}

	
}


long  CustomerBalanceBonusEmail(CVars in)
{
	//REQ_STRING( in, "orderid", 1, 50, "(.*)");
	 REQ_LONG( in, "customer_id", 1, -1 );
	CVars x;
	C_CGI_Form form("","");
    if ( load_db_record( x, "select * FROM customer WHERE id =   %lu  ", in["customer_id"].Long()) ) 
	{
		 vars2form( x, form );
		 form.Email( "bonus_balance.mailfile" );
	}

	return 0;
}

void  PaymentAlert(CVars in)
{

}

void  BalanceUpdateAlert(CVars in)
{
	REQ_STRING( in, "orderid", 1, 50, "(.*)");
	CVars x;
	CVars out; 
		C_CGI_Form form("","");
	out["topupemail"]  = CfgReadChar("transnotif.topupemail");

	if ( load_db_record( x, "select c.*,t.*,t.added AS balance_date,appramt*0.01 AS appramt2 ,c.balance*0.01 AS balance2 from trans_balance_topup t LEFT JOIN customer c ON c.id =  t.customer_id where t.orderid = '%s' AND completed IS NOT NULL", in["orderid"].c_str()) ) 
	{
	
	 vars2form( x, form );
	
	
	 form.LetStr( "email",   out["topupemail"].c_str() );
	 form.Email( "alert_balance_updated.mailfile" );
	
	}

	
	
}

void  CustomerEmailWelcome(CVars in)
{
    long customer_id  = in["user_id"].Long();
	C_CGI_Form form("","");
	const char * server_name = getenv( "SERVER_NAME" );     
	CVars x;
	load_db_record( x, "select *,email as email2 from customer   WHERE id =%lu ",customer_id ) ;
    vars2form( x, form );
	form.Email( "welcome.mailfile" );
}

/*****************************************************************************/
long VirtualAirSaleAlert(CVars in)
{
/*
payquic_test=> select t.*,c.* from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id  limit 1;
 id | customer_id |  orderid   | description | amount | txnref | payref | retref | status |           added            | resp | recipient_no | completed | appramt | rspcode | id |      username      | firstname | lastname |       email        |               pass
               | status |           added            | balance 
----+-------------+------------+-------------+--------+--------+--------+--------+--------+----------------------------+------+--------------+-----------+---------+---------+----+--------------------+-----------+----------+--------------------+-------------------
---------------+--------+----------------------------+---------
  2 |           1 | 1452581056 | 08174596143 |   1000 |        |        |        |      1 | 2014-09-15 21:00:39.921535 |      | 08174596144  |           |       0 |         |  1 | ses66181@gmail.com | Olu       | Amey     | ses66181@gmail.com | d0fbea2563b377ea70
74bced45c88dcb |      1 | 2014-07-09 13:52:02.478932 |  189700
(1 row)

*/
CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x,y;
	load_db_record( x, "select t.*,c.*,c.loc as loc_c,c.balance*0.01 AS balance2,t.amount AS amount2,c.email AS email2 from trans_notification t LEFT JOIN customer c ON c.id = t.customer_id WHERE t.orderid='%s'",in["orderid"].c_str() ) ;
    vars2form( x, form );

	
	load_db_record( y, "SELECT country_code,country_name,continent_code,continent_name FROM ipligence2 WHERE ip_from <= ip2int('%s') AND ip_to>=ip2int('%s') LIMIT 1",x["loc_c"].c_str(),x["loc_c"].c_str());
	/*
	country_code | country_name  | continent_code | continent_name
	--------------+---------------+----------------+----------------
	US           | UNITED STATES | NA             | NORTH AMERICA
	*/	   
	vars2form( y, form );
	
	CVars z;
    load_db_record( z, "SELECT t.paymentcode,i.responsecode,i.transactionref FROM interswitch_bill_payment_advice t LEFT JOIN interswitch_transaction i ON i.detail_id=t.id WHERE t.order_id ='%s'",x["orderid"].c_str());
	vars2form( z, form );
	
			if ( in["pmode"].Long() == PAY_MODE_BALANCE )  // paying with balance
			{
                   form.LetStr( "paymode",  "Payment with balance" );
			}
			else if ( in["pmode"].Long() == PAY_MODE_BONUS )  // paying bonus
			{
			  form.LetStr( "paymode",  "Bonus Payment" );
			}
			else
			{
                    form.LetStr( "paymode",  "Payment with card" );
			}
					   
	
 form.LetStr( "site",  server_name );
 
 form.LetStr( "message",  server_name );
 //form.LetStr( "email",  "ses66181@gmail.com,oladeji@gmail.com" );
  form.LetStr( "email",   out["topupemail"].c_str() );

// form.LetStr( "email",  "acidumirae@gmail.com" );

 form.Email( "virtualtopup.mailfile" );
 return 0l;
}
/*****************************************************************************/

void SendAccountCreateAlert(long customer_id)
{

CVars out; 
out["topupemail"]  = CfgReadChar("transnotif.topupemail");

 logfmt( logINFO, "SystemStatus()*************~************");

    C_CGI_Form form("","");
    const char * server_name = getenv( "SERVER_NAME" );
	CVars x,y;
	load_db_record( x, "SELECT *,email as email2,balance*0.01 AS balance2,dollar_balance*0.01 AS dollar_balance2 from customer   WHERE id =%lu ",customer_id ) ;
    vars2form( x, form );
	
    load_db_record( y, "SELECT country_code,country_name,continent_code,continent_name FROM ipligence2 WHERE ip_from <= ip2int('%s') AND ip_to>=ip2int('%s') LIMIT 1",x["loc"].c_str(),x["loc"].c_str());
 /*
 country_code | country_name  | continent_code | continent_name
--------------+---------------+----------------+----------------
 US           | UNITED STATES | NA             | NORTH AMERICA
*/	   
	  vars2form( y, form );
	form.LetStr( "site",  server_name );
	form.LetStr( "message",  server_name );
	form.LetStr( "email",   out["topupemail"].c_str() );

// form.LetStr( "email",  "acidumirae@gmail.com" );

 form.Email( "alert_accountcreated.mailfile" );
//alert_accountcreated.mailfile
}
/*****************************************************************************/
void vars2form( CVars &v, C_CGI_Form &form )
{
	CVars::const_iterator i;
	for ( i=v.begin(); i != v.end(); i++ ) {
		char name[30];
		strsafecpy( name, i->first.c_str(), sizeof(name) );
		// &fee, tax, total
		if ( strcasecmp( name, "tax" )==0 ||
			 strcasecmp( name, "total" )==0 ||
			 strcasecmp( name, "price" )==0 ||
			 strcasecmp( name, "amount" )==0 ||
			 strcasecmp( name, "shipping" )==0 ||
			 strcasecmp( name, "subtotal" )==0 ||
			( strlen(name)>=3 && strcasecmp( &name[strlen(name)-3], "fee" )==0 )) {
			form.LetStrf( name, "%.2f", i->second.Long()/100.0 );
		} else {
			form.LetStr( i->first.c_str(), i->second.c_str() );
		}
	}
}
/*****************************************************************************/

