<?PHP
function build_hosting_extras($addon_choices)
	{
	$addon_list="";
	$coupon_list="";
	$ct=(substr_count($addon_choices, "|")-1);
	$array=explode("|", $addon_choices);

	for ($i=0; $i<=$ct; $i++)
		{
		$query0="select ";
		$query0.="addon_name, ";				// 0
		$query0.="setup_cost, ";				// 1
		$query0.="setup_activate, ";			// 2
		$query0.="addon_cost, ";				// 3
		$query0.="addon_state, ";				// 4
		$query0.="addon_activate, ";			// 5
		$query0.="coupon, ";					// 6
		$query0.="coupon_terms, ";				// 7
		$query0.="coupon_code, ";				// 8
		$query0.="coupon_discount_type, ";		// 9
		$query0.="coupon_discount_percent, ";	// 10
		$query0.="coupon_discount_whole ";		// 11
		$query0.="from addon_specs ";
		$query0.="where aid='".addslashes(trim($array[$i]))."'";

		$rs0=mysql_fetch_row(mysql_query($query0));

		$addon_name=stripslashes(trim($rs0[0])); 
		$setup_cost=stripslashes(trim($rs0[1])); 
		$setup_activate=stripslashes(trim($rs0[2]));
		$addon_cost=stripslashes(trim($rs0[3]));
		$addon_state=stripslashes(trim($rs0[4]));
		$addon_activate=stripslashes(trim($rs0[5]));
		$coupon=stripslashes(trim($rs0[6]));
		$coupon_terms=stripslashes(trim($rs0[7]));
		$coupon_code=stripslashes(trim($rs0[8]));
		$coupon_discount_type=stripslashes(trim($rs0[9]));
		$coupon_discount_percent=stripslashes(trim($rs0[10]));
		$coupon_discount_whole=stripslashes(trim($rs0[11]));
		
		if ($coupon==1)
			{
			$coupon_list.="Coupon Code: ".$coupon_code." ";
			
			# 1. figure out one time or monthly
			if ($coupon_terms==1) # one time
				{
				if ($coupon_discount_type==1) # %
					{
					# - %
					$coupon_list.="one time discount of ".$coupon_discount_percent."% off hosting fees\n";
					}
				else if ($coupon_discount_type==2) # whole dollar amount
					{
					# - WHOLE DOLLAR
					$coupon_list.="one time discount of ".($currency.sprintf("%01.2f", $coupon_discount_whole)." ".$currency_type)." off hosting fees\n";
					}
				}
			else if ($coupon_terms==2&&strcmp("2checkout", strtolower($payment_type_name))!=0) # monthly
				{
				if ($coupon_discount_type==1) # %
					{
					# - %
					$coupon_list.="recurring discount of ".$coupon_discount_percent."%\n";
					}
				else if ($coupon_discount_type==2) # whole dollar amount
					{ 
					# - WHOLE DOLLAR 
					$coupon_list.="recurring discount of ".$currency.sprintf("%01.2f", $coupon_discount_whole)." ".$currency_type."\n";
					}
				}
			}

	if (trim($addon_name)!=""&&$coupon!=1)
			{
			$addon_list.=$addon_name." ";
			if ($addon_activate==1)
				{
				$addon_list.=$currency.sprintf("%01.2f", $addon_cost)." ".$currency_type." ";

				if ($addon_state==1) { $addon_list.="one time fee\n"; }
				else if ($addon_state==2) { $addon_list.="monthly recurring\n"; }
				else if ($addon_state==3) { $addon_list.="annually recurring\n"; }
				}
			else { $addon_list.="No Cost \n"; }

			if ($set_activate==1) 
				{ 
				$addon_list=trim($addon_list);
				$addon_list.="+ ".$currency.sprintf("%01.2f", $setup_cost)." ".$currency_type." setup fee\n"; 
				}
			}

		unset($addon_name);
		unset($setup_cost);
		unset($setup_activate);
		unset($addon_cost);
		unset($addon_state);
		unset($addon_activate);
		unset($coupon);
		unset($coupon_terms);
		unset($coupon_code);
		unset($coupon_discount_type);
		unset($coupon_discount_percent);
		unset($coupon_discount_whole);
		}
	
	$extras="";
	if (trim($addon_list)!="") { $extras.=$addon_list."\n"; }
	if (trim($coupon_list)!="") { $extras.=$coupon_list; }
	
	if (trim($extras)=="") { return 0; }
	else { return $extras; }
	}

function update_invoice_internal($oid)
	{
	$query0="select ";
	$query0.="hosting_order.next_due_date, ";		// 0
	$query0.="hosting_order.domain_name, ";			// 1
	$query0.="hosting_order.payment_method, ";		// 2
	$query0.="hosting_order.payment_term, ";		// 3
	$query0.="hosting_order.total_due_reoccur, ";	// 4
	$query0.="user.first_name, ";					// 5
	$query0.="user.email, ";						// 6
	$query0.="hosting_order.total_due_today, ";		// 7
	$query0.="hosting_order.total_due_reoccur, ";	// 8
	$query0.="hosting_order.addon_choices, ";		// 9
	$query0.="hosting_order.pid ";					// 10
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$next_due_date=stripslashes(trim($rs0[0]));
	$domain_name=stripslashes(trim($rs0[1]));
	$payment_method=stripslashes(trim($rs0[2]));
	$payment_term=stripslashes(trim(strtolower(str_replace("-", "_", $rs0[3]))));
	$total_due_reoccur=stripslashes(trim($rs0[4]));
	$first_name=stripslashes(trim($rs0[5]));
	$email=stripslashes(trim($rs0[6]));
	$total_due_today=stripslashes(trim($rs0[7]));
	$total_due_reoccur=stripslashes(trim($rs0[8]));
	$addon_choices=stripslashes(trim($rs0[9]));
	$processor_id=stripslashes(trim($rs0[10]));

	# ----------------------------------------------------------------------------

	$query7="select ";
	$query7.="monthly_pid, ";		// 0
	$query7.="quarterly_pid, ";		// 1
	$query7.="semi_annual_pid, ";	// 2
	$query7.="annual_pid ";			// 3
	$query7.="from ";
	$query7.="plan_specs ";
	$query7.="where pid='".addslashes($processor_id)."'";

	$rs7=mysql_fetch_row(mysql_query($query7));

	$monthly_pid=stripslashes(trim($rs7[0]));
	$quarterly_pid=stripslashes(trim($rs7[1]));
	$semi_annual_pid=stripslashes(trim($rs7[2]));
	$annual_pid=stripslashes(trim($rs7[3]));

	# ----------------------------------------------------------------------------

	if ($payment_term=="monthly") { $payment_term=1; $checkout_id=$monthly_pid; }
	else if ($payment_term=="quarterly") { $payment_term=3; $checkout_id=$quarterly_pid; }
	else if ($payment_term=="semi_annual") { $payment_term=6; $checkout_id=$semi_annual_pid; }
	else if ($payment_term=="annual") { $payment_term=12; $checkout_id=$annual_pid; }

	# ----------------------------------------------------------------------------

	$query2="select ";
	$query2.="name ";
	$query2.="from ";
	$query2.="payment_process ";
	$query2.="where ";
	$query2.="pid='".addslashes(trim($payment_method))."'";

	$rs2=mysql_fetch_row(mysql_query($query2));

	$payment_processor_name=stripslashes(trim($rs2[0]));

	# ----------------------------------------------------------------------------
				
	# create new master
	$query3="update ";
	$query3.="invoice ";
	$query3.="set ";
	$query3.="due_date='".addslashes(trim($next_due_date))."', ";
	$query3.="created='".time()."', ";
	$query3.="total_due_today='".addslashes(trim($total_due_today))."', ";
	$query3.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";

	$extras=build_hosting_extras($addon_choices);
	if ($extras!==0) { $query3.="extras='".addslashes(trim($extras))."', "; }

	$query3.="payment_method='".addslashes(trim($payment_method))."' ";
	$query3.="where invoice_type='0' and oid='".addslashes(trim($oid))."' and master='1'";

	mysql_query($query3);
	}


function create_new_invoice_shell($oid)
	{
	$query0="select ";
	$query0.="hosting_order.domain_name, ";			// 0
	$query0.="user.first_name, ";					// 1
	$query0.="user.email, ";						// 2
	$query0.="hosting_order.next_due_date ";		// 3
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$domain_name=stripslashes(trim($rs0[0]));
	$first_name=stripslashes(trim($rs0[1]));
	$email=stripslashes(trim($rs0[2]));
	$next_due_date=stripslashes(trim($rs0[3]));
	$invoice_number=grab_invoice_number();

	# create new master
	$query1="insert ";
	$query1.="into ";
	$query1.="invoice ";
	$query1.="set ";
	$query1.="oid='".addslashes(trim($oid))."', ";
	$query1.="created='".time()."', ";
	$query1.="invoice_number='".addslashes(trim($invoice_number))."', ";
	$query1.="due_date='".addslashes(trim($next_due_date))."', ";
	$query1.="invoice_type='0', ";
	$query1.="master='1'";

	mysql_query($query1);
	}

function create_new_invoice($oid, $status)
	{
	$query0="select ";
	$query0.="hosting_order.next_due_date, ";		// 0
	$query0.="hosting_order.domain_name, ";			// 1
	$query0.="hosting_order.payment_method, ";		// 2
	$query0.="hosting_order.payment_term, ";		// 3
	$query0.="hosting_order.total_due_reoccur, ";	// 4
	$query0.="user.first_name, ";					// 5
	$query0.="user.email, ";						// 6
	$query0.="hosting_order.total_due_today, ";		// 7
	$query0.="hosting_order.total_due_reoccur, ";	// 8
	$query0.="hosting_order.addon_choices, ";		// 9
	$query0.="hosting_order.pid ";					// 10
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$next_due_date=stripslashes(trim($rs0[0]));
	$domain_name=stripslashes(trim($rs0[1]));
	$payment_method=stripslashes(trim($rs0[2]));
	$payment_term=stripslashes(trim(strtolower(str_replace("-", "_", $rs0[3]))));
	$total_due_reoccur=stripslashes(trim($rs0[4]));
	$first_name=stripslashes(trim($rs0[5]));
	$email=stripslashes(trim($rs0[6]));
	$total_due_today=stripslashes(trim($rs0[7]));
	$total_due_reoccur=stripslashes(trim($rs0[8]));
	$addon_choices=stripslashes(trim($rs0[9]));
	$processor_id=stripslashes(trim($rs0[10]));

	# ----------------------------------------------------------------------------

	$query7="select ";
	$query7.="monthly_pid, ";		// 0
	$query7.="quarterly_pid, ";		// 1
	$query7.="semi_annual_pid, ";	// 2
	$query7.="annual_pid ";			// 3
	$query7.="from ";
	$query7.="plan_specs ";
	$query7.="where pid='".addslashes($processor_id)."'";

	$rs7=mysql_fetch_row(mysql_query($query7));

	$monthly_pid=stripslashes(trim($rs7[0]));
	$quarterly_pid=stripslashes(trim($rs7[1]));
	$semi_annual_pid=stripslashes(trim($rs7[2]));
	$annual_pid=stripslashes(trim($rs7[3]));

	# ----------------------------------------------------------------------------

	if ($payment_term=="monthly") { $payment_term=1; $checkout_id=$monthly_pid; }
	else if ($payment_term=="quarterly") { $payment_term=3; $checkout_id=$quarterly_pid; }
	else if ($payment_term=="semi_annual") { $payment_term=6; $checkout_id=$semi_annual_pid; }
	else if ($payment_term=="annual") { $payment_term=12; $checkout_id=$annual_pid; }

	# ----------------------------------------------------------------------------

	$query2="select ";
	$query2.="name ";
	$query2.="from ";
	$query2.="payment_process ";
	$query2.="where ";
	$query2.="pid='".addslashes(trim($payment_method))."'";

	$rs2=mysql_fetch_row(mysql_query($query2));

	$payment_processor_name=stripslashes(trim($rs2[0]));

	# ----------------------------------------------------------------------------
				
	# create new master
	$query3="insert into ";
	$query3.="invoice ";
	$query3.="set ";
	$query3.="created='".time()."', ";
	$query3.="total_due_today='".addslashes(trim($total_due_today))."', ";
	$query3.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";
	$query3.="due_date='".addslashes(trim($next_due_date))."', ";

	$extras=build_hosting_extras($addon_choices);
	if ($extras!==0) { $query3.="extras='".addslashes(trim($extras))."', "; }

	$query3.="payment_method='".addslashes(trim($payment_method))."', ";
	$invoice_number=grab_invoice_number();
	$query3.="invoice_number='".$invoice_number."', ";
	$query3.="oid='".addslashes(trim($oid))."', ";
	$query3.="master='1', ";
	$query3.="status='".$status."'";
	
	mysql_query($query3);

	return $invoice_number;
	}

function invoice_due_in_x_days($email_admin, $first_name, $domain_name, $total_due_reoccur, $payment_processor_name, $client_email, $days_until_due, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='39' ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$days_until_due=$days_until_due." day".(($days_until_due==1)?"":"s")."";

	$subject=str_replace("{{days_until_due}}", $days_until_due, $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{total_recuur}}", $total_due_reoccur, $subject);
	$subject=str_replace("{{payment_processor}}", $payment_processor_name, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{days_until_due}}", $days_until_due, $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{total_recuur}}", $total_due_reoccur, $message);
	$message=str_replace("{{payment_processor}}", $payment_processor_name, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($client_email, $subject, $message, $headers);

	if ($which==1)
		{
		$message.="\n\n-----------------------------------------------------\nThis is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}
	}
function invoice_resend($iid, $http_web, $email_admin, $id, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='38' ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$query1="select ";
	$query1.="invoice_type "; // 0
	$query1.="from ";
	$query1.="invoice ";
	$query1.="where ";
	$query1.="iid='".addslashes(trim($iid))."'";

	$rs1=mysql_fetch_row(mysql_query($query1));

	$invoice_type=stripslashes(trim($rs1[0])); 
	
	$query2="select ";
	$query2.="first_name, ";
	$query2.="last_name, ";
	$query2.="email ";
	$query2.="from ";
	$query2.="user ";
	$query2.="where ";
	$query2.="uid='".addslashes(trim($id))."' ";

	$rs2=mysql_fetch_row(mysql_query($query2));

	$first_name=stripslashes(trim($rs2[0]));
	$last_name=stripslashes(trim($rs2[1]));
	$client_email=stripslashes(trim($rs2[2]));

	if ($invoice_type==0)
		{	
		$pay_link=$http_web."/invoice_hosting_payable.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $iid));
		}
	else
		{	
		$pay_link=$http_web."/invoice_service_payable.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $iid));
		}
	
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{pay_link}}", $pay_link, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{pay_link}}", $pay_link, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($client_email, $subject, $message, $headers);

	if ($which==1)
		{
		$message.="\n\n-----------------------------------------------------\nThis is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}
	}
function invoice_paid_successfully($iid, $currency, $currency_type, $email_admin)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='37' ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$query1="select ";
	$query1.="uid, ";				// 0
	$query1.="oid, ";				// 1
	$query1.="date_paid, ";			// 2
	$query1.="invoice_number, ";	// 3	
	$query1.="invoice_type, ";		// 4
	$query1.="total_due_today, ";	// 5
	$query1.="total_due_reoccur, ";	// 6
	$query1.="charge_tax ";			// 7
	$query1.="from ";
	$query1.="invoice ";
	$query1.="where ";
	$query1.="iid='".addslashes(trim($iid))."'";

	$rs1=mysql_fetch_row(mysql_query($query1));

	$uid=stripslashes(trim($rs1[0]));
	$oid=stripslashes(trim($rs1[1]));
	$date_paid=stripslashes(trim($rs1[2])); 
	$invoice_number=stripslashes(trim($rs1[3])); 
	$invoice_type=stripslashes(trim($rs1[4])); 
	$total_due_today=stripslashes(trim($rs1[5])); 
	$total_due_reoccur=stripslashes(trim($rs1[6])); 
	$charge_tax=stripslashes(trim($rs1[7])); 

	if ($invoice_type==0)
		{
		if ($total_due_today>0) { $total_invoice_amount=$total_due_today; }
		else { $total_invoice_amount=$total_due_reoccur; }

		$query2="select ";
		$query2.="user.first_name, ";
		$query2.="user.last_name ";
		$query2.="from ";
		$query2.="user, hosting_order ";
		$query2.="where hosting_order.uid=user.uid ";
		$query2.="and ";
		$query2.="hosting_order.oid='".addslashes(trim($oid))."' ";
		$query2.="order by ";
		$query2.="hosting_order.oid asc";

		$rs2=mysql_fetch_row(mysql_query($query2));

		$first_name=stripslashes(trim($rs2[0]));
		$last_name=stripslashes(trim($rs2[1]));
		}
	else
		{
		if ($charge_tax>0)
			{
			$tax_amount=$total_due_today*$charge_tax;
			$total_due_today+=$tax_amount;
			}
		
		$total_invoice_amount=$total_due_today;
		
		$query2="select ";
		$query2.="first_name, ";
		$query2.="last_name ";
		$query2.="from ";
		$query2.="user ";
		$query2.="where ";
		$query2.="uid='".addslashes(trim($uid))."' ";

		$rs2=mysql_fetch_row(mysql_query($query2));

		$first_name=stripslashes(trim($rs2[0]));
		$last_name=stripslashes(trim($rs2[1]));
		}
	
	$total_invoice_amount=$currency.sprintf("%01.2f", $total_invoice_amount)." ".$currency_type;
	
	$subject=str_replace("{{invoice_number}}", $invoice_number, $subject);
	$subject=str_replace("{{date_paid}}", date("m/d/Y h:i:s a", $date_paid), $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{last_name}}", $last_name, $subject);
	$subject=str_replace("{{total_invoice_amount}}", $total_invoice_amount, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{invoice_number}}", $invoice_number, $message);
	$message=str_replace("{{date_paid}}", date("m/d/Y h:i:s a", $date_paid), $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{last_name}}", $last_name, $message);
	$message=str_replace("{{total_invoice_amount}}", $total_invoice_amount, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($email_admin, $subject, $message, $headers);
	}

function translate_invoice_terms($invoice_terms)
	{
	if ($invoice_terms==0) { return "Upon Receipt"; }
	else if ($invoice_terms==30) { return "30 Day Invoice"; }
	else if ($invoice_terms==60) { return "60 Day Invoice"; }
	else if ($invoice_terms==90) { return "90 Day Invoice"; }
	else if ($invoice_terms==120) { return "120 Day Invoice"; }
	else { return "error!"; }
	}


function grab_invoice_number()
	{
	# grab invoice number
	$query0="select ";
	$query0.="invoice_number ";
	$query0.="from ";
	$query0.="invoice ";
	$query0.="order by iid desc ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));
	
	if (trim($rs0[0])=="")
		{
		$query1="select ";
		$query1.="invoice_number ";
		$query1.="from ";
		$query1.="invoice_config ";
		$query1.="limit 0, 1";

		$rs1=mysql_fetch_row(mysql_query($query1));
		
		return $rs1[0];
		}
	else { return $rs0[0]+1; }
	}


function invoice_status($invoice_status)
	{
	if ($invoice_status==0) { return "Unpaid"; }
	else if ($invoice_status==1) { return "Paid"; }
	else if ($invoice_status==2) { return "Past Due"; }
	else { return "Read Error!"; }
	}


function invoice_status_select($invoice_status)
	{
	$output="0@Unpaid|";
	$output.="1@Paid|";
	return $output.="2@Past Due|";
	}

function create_timestamp($formatted_date)
	{
	list($m, $d, $y)=explode("/", $formatted_date);
	return mktime(0, 0, 0, $m, $d, $y);
	}

function create_invoice_session($iid)
	{
	$sid=e("576cb7f68040520768bf51c75f7f4c84", $iid);
	
	$query5="insert ";
	$query5.="into ";
	$query5.="invoice_session ";
	$query5.="set ";
	$query5.="sid='".addslashes(trim($sid))."', ";
	$query5.="created='".time()."'";

	mysql_query($query5);

	return $sid; 
	}

function convert_payment_term_to_number($payment_term)
	{
	if ($payment_term=="monthly") { return 1; }
	else if ($payment_term=="quarterly") { return 3; }
	else if ($payment_term=="semi_annual") { return 6; }
	else if ($payment_term=="annual") { return 12; }
	}

function is_valid_date($next_due_date)
	{
	$invalid=mktime(0, 0, 0, 1, 1, 1970);

	if ($next_due_date>$min) { return 1; }
	else { return 0; }
	}
?>