<?php
class PaaS_PHP {

  protected $guid; // A unique identifier provided by Click & Pledge:  Listed in the administrative system.
  
  protected $accountid; // Trio account number.

  var $html_element;
  
  var $ordermode; // Specify the order mode.  Test mode may be used for testing and development.In test mode only test credit card numbers may be used.In Production mode 			 transactions are only performed with real credit cards
  
  var $tracker; // Tracker is a simple text that is an optional parameter which is passed along with each transaction.Transactions may be queried in the reporting section based on the Tracker text
  
  var $campaign_name; // Campaign is a simple text that is an optional parameter which is passed along with each transaction.Transactions may be queried in the reporting section based on the Campaign text.
  
  var $receipt = true; // If True the receipt will be emailed to the card holder.
  
  var $wid; // The receipt information as set by the checkout page will be used if the CheckOutPageID is specified.  If specified, other Receipt elements will NOT be used.
  
  var $organization_information; // Organization information to be used.
  
  var $currency_code = '840';
  
  var $file_path;
  
  public function __construct($file_path = 'ClickandPledge_PaaS_Processor.php')
  {
    //$errors = array();
    $sucess = array();
    $this->new_file_path = $file_path;
    $this->f_l_name = '/^([a-zA-Z0-9\.\,\#\&\-\ \']{2,50})$/';
    $this->shipping_name = '/^([A-Za-z0-9_\-\.\#\,\'\"]{2,20})$/';
    $this->m_name = '/^([a-zA-Z0-9]{1})$/';
    $this->email = '/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/';
    $this->phone = '/^([0-9\.\-\(\)\+]){10,50}$/';
    $this->address = '/^([a-zA-Z0-9\.\,\#\&\-\ \']{2,100})$/';
    $this->city = '/^([a-zA-Z0-9\.\,\&\-\ \']{2,50})$/';
    $this->cardname = '/^([a-zA-Z0-9\.\,\#\-\ \']){2,50}$/';
    $this->stateprovince = '/^([a-zA-Z0-9\.\,\&\-\'\ ])+$/';
    $this->postalcode = '/^([a-zA-Z0-9\.\-]{2,20})$/';
    $this->country = '/^([0-9]{3})$/';
  
    $this->cvv = '/^([0-9]){3,4}$/';
    $this->tax_value = '/^([0-9]){1,50}$/';
    $this->installments = '/^([0-9]){0,3}$/';
    //$this->allowed_paymenttypes = array('CreditCard', 'Check', 'Invoice', 'PurchaseOrder');
	$this->allowed_custompaymenttypes = array('Cash', 'Pay Later');
    $this->allowed_paymenttypes = array('CreditCard', 'Check','CustomPaymentType');
    $this->allowed_echeck_accounttype = array('CheckingAccount', 'SavingsAccount');
    $this->allowed_echeck_checktype = array('Company', 'Personal');
    $this->periodicity_options = array('Week' => 'Week', '2 Weeks' => '2 Weeks', 'Month' => 'Month', '2 Months' => '2 Months', 'Quarter' => 'Quarter', '6 Months' => '6 Months', 'Year' => 'Year');
    $this->allowed_recurringmethod=array('Installment', 'Subscription');
      $this->allowed_currency = array('$' => '840','�' => '978','�' => '826','C$' => '124','HK$' => '344');
	  $countryxmlpath		 = 'https://aws.assets.clickandpledge.com/global/xml/ISO-Countries-States-Provinces.xml'; 
	  $countryxml          = simplexml_load_file($countryxmlpath);
	  $countryjson_string  = json_encode($countryxml);
	  $countryresult_array = json_decode($countryjson_string, TRUE);
	  $cntr=""; $inc=0; $cnpcntryarr= array();

	  foreach($countryresult_array as $cntryrslt){ }
	  foreach($cntryrslt as $cntryrslt123)	{  }
	  foreach($cntryrslt123 as $cntryrslt456)	 
		{ 
			
		  $cnpcntryarr[$cntryrslt456["@attributes"]["Code"]] =$cntryrslt456["@attributes"]["Name"];
		  $states_code_.$cntryrslt456["@attributes"]["Abbrev"] = array();

		  foreach($cntryrslt456 as $cntryrslt789)	{
			$states_code_.$cntryrslt456["@attributes"]["Abbrev"][$cntryrslt789["@attributes"]["Abbrev"]] =$cntryrslt789["@attributes"]["Name"];
			} 
	
		}
	 
    $this->country_code = $cnpcntryarr;
    
  
  }

	// Account Information handling fumctions.
	public function Set_Authentication($guid, $accountid)
		{
			if (!preg_match("/^[0-9]+$/", $accountid)) {
				return $this->_t('Error', 'Invalid AccountID.');
			}
			
			if (!preg_match("/^[a-zA-Z0-9\-]+$/", $guid)) {
				return $this->_t('Error', 'Invalid GUID.');
			}
					
			$html_element = $this->_html_input_element('hidden', 'Guid', $guid, 50);
			$html_element .= $this->_html_input_element('hidden', 'Accountid', $accountid, 50);
			return $html_element;
		}
	
	public function Set_AccountToken()
		{
			$html_element = 'AccountToken';
			return $html_element;
		}
	// End of Account Information single account and multi acoount handling fumctions.
	
	// Order Mode (OrderMode,CampainName,TrackerName).	
	public function Set_Order_Mode($ordermode = 'Test')
		{
			if ($ordermode == 'Test' || $ordermode == 'Production') {
				$html_element = $this->_html_input_element('hidden', 'Order Mode', $ordermode, 50);
				return $html_element;
			}
			return $this->_t('Error', 'Invalid Order Mode Argument.');
		}
	
	public function Set_Tracker($tracker)
		{
			if (!preg_match("/^([a-zA-Z0-9\-\_ ]){2,50}+$/", $tracker)) {
			    return $this->_t('Error', 'Invalid Tracker.');
			}
			$html_element = $this->_html_input_element('hidden', 'Tracker', $tracker, 50);
			return $html_element;
		}
		
	public function Set_Campaign_Name($campaign_name)
		{
			if (!preg_match("/^([a-zA-Z0-9\ ]){2,80}+$/", $campaign_name)) {
			    return $this->_t('Error', 'Invalid Campaign.');
			}
			$html_element = $this->_html_input_element('hidden', 'Campaign', $campaign_name, 50);
			return $html_element;
		}
	//End of Order Mode (OrderMode,CampainName,TrackerName)	

	//Receipt Information
	public function Set_SendReceipt($receipt)
		{
			if ($receipt == 'true' || $receipt == 'false') {
				$html_element = $this->_html_input_element('hidden', 'SendReceipt' ,$receipt, 50);
				return $html_element;
			}
			return $this->_t('Error', 'Invalid Send Receipt Argument.');
		}
	
	public function Set_Wid($wid)
		{
			if (!preg_match("/^[0-9]{1,10}+$/", $wid) || $wid == 0) {
				 $errors[]="<span style=color:red;>Invalid WID.</span>";
				 return array('Errors'=> $errors);
			}
			$html_element .= $this->_html_input_element('hidden', 'WID', $wid, 50);
			return $html_element;
		}
	//End of Receipt Information

	public function Set_OrganizationInformation($organization_information)
		{
			$html_element = $this->_html_input_element('hidden', 'OrganizationInformation', $organization_information, 50);
			return $html_element;
		}
	
	public function Set_ThankYouMessage($thank_you_message)
		{
			$html_element = $this->_html_input_element('hidden', 'ThankYouMessage', $thank_you_message, 50);
			return $html_element;
		}
		
	public function Form_Open()
		{
			$html_element = '<form id="Donation" name="Donation" method="post">';
			return $html_element;
		}
	
	public function Form_Submit()
		{
			$html_element = '<input type="submit" name="submit" value="Submit" id="PostData"  />';
			return $html_element;
		}
	
	public function Form_Close()
		{
			$html_element = '</form>';
			return $html_element;
		}

public function Set_Currency($currency_code = '')
{
	 if ($currency_code == '') {
		 $currency_code = $this->currency_code;
     }
	 if (in_array($currency_code, $this->allowed_currency)) {
		 $html_element = $this->_html_input_element('hidden', 'currency', $currency_code);
		 return $html_element;
	 } else { 
	     return $this->_t('Error','Invalid Currency Code.');
	 }
}

	// Billing/Personal Information
	
	// Card holder?s first name as it appears on the credit card.
	public function Set_First_Name()
		{	
			$html_element = 'BillingFirstName'; 
			return $html_element;
		}
	
	// Last name of the card holder as it appears on the credit card.
	public function Set_Last_Name()
		{
			$html_element = 'BillingLastName'; 
			return $html_element;
		}
	
	// Card holder?s middle initial as it appears on the credit card.
	public function Set_Middle_Initial()
		{
			$html_element = 'BillingMI'; 
			return $html_element;
		}
	
	// Card holder?s email address.  Email address will be used for sending the receipt if SendReceipt element is True.
	public function Set_Email()
		{
			$html_element = 'BillingEmail'; 
			return $html_element;
		}
	
	// Card holder?s phone number.
	public function Set_Phone()
		{
			$html_element = 'BillingPhone'; 
			return $html_element;
		}
	//End of Card Holder Information
	
	//Billing Address
	
	// Line 1 of the billing address.
	public function Set_Billing_Address1()
		{
			$html_element = 'BillingAddress1'; 
			return $html_element;
		}
	
	// Line 2 of the billing address.
	public function Set_Billing_Address2()
		{
			$html_element = 'BillingAddress2'; 
			return $html_element;
		}
	
	// Line 3 of the billing address.
	public function Set_Billing_Address3()
		{
			$html_element = 'BillingAddress3'; 
			return $html_element;
		}
	
	// Billing address city.
	public function Set_Billing_City()
		{
			$html_element = 'BillingCity'; 
			return $html_element;
		}
	
	// Zip or postal code of cardholder.
	public function Set_Billing_Postal_Code()
		{
			$html_element = 'BillingPostalCode'; 
			return $html_element;
		}
	
	// End Billing Address

	// Shipping Address
	public function Set_Shipping_First_Name()
		{
			$html_element = 'ShippingFirstName';
			return $html_element;
		}
	
	public function Set_Shipping_Last_name()
		{
			$html_element = 'ShippingLastName';
			return $html_element;
		}
	public function Set_Shipping_Middle_Initial()
		{
			$html_element = 'ShippingMI';
			return $html_element;
		}
	
	public function Set_Shipping_Email()
		{
			$html_element = 'ShippingEmail';
			return $html_element;
		}
	
	public function Set_Shipping_Phone()
		{
			$html_element = 'ShippingPhone';
			return $html_element;
		}

// Card Shipping Address
	
	// Line 1 of the shipping address.
	public function Set_Shipping_Address1()
		{
			$html_element = 'ShippingAddress1'; 
			return $html_element;
		}
	
	// Line 2 of the shipping address.
	public function Set_Shipping_Address2()
		{
			$html_element = 'ShippingAddress2'; 
			return $html_element;
		}
	
	// Line 3 of the shipping address.
	public function Set_Shipping_Address3()
		{
			$html_element = 'ShippingAddress3'; 
			return $html_element;
		}
	
	// Shipping address city.
	public function Set_Shipping_City()
		{
			$html_element = 'ShippingCity'; 
			return $html_element;
		}
	
	// Shipping address zip or postal code.
	 public function Set_Shipping_Postal_Code()
		{
			$html_element = 'ShippingPostalCode'; 
			return $html_element;
		}
	
	// End of Shipping Address
	
	// Billing or shipping country code functions using arguments
	
	public function Set_Country_Code($type = 'billing', $default_country_code = '000') // Billing Address/Shipping address ISO country code [see ISO table ? Appendix A]
		{	
			if ($type == 'billing' || $type == 'shipping') {
			
				if ($type == 'billing') {
					echo "<script>nonusa('non_usa_billing_text','billing','".$default_country_code."','".$this->new_file_path."');</script>";
					$name = 'BillingCountryCode';
					$onchange = 'onchange = cnpstspicklst(\'non_usa_billing_text\',\'billing\',this.value)';
				}
				if ($type == 'shipping')	{
					echo "<script>nonusa('non_usa_shipping_text','shipping','".$default_country_code."','".$this->new_file_path."');</script>";
					$name = 'ShippingCountryCode';
					$onchange = 'onchange=cnpstspicklst(\'non_usa_shipping_text\',\'shipping\',this.value)';
				}
				
				$html_element = '<select name="'.$name.'" size="1" id="'.$name.'" '.$onchange.' >';
				foreach ($this->country_code as $k_country => $v_country) {
					if ($k_country == $default_country_code) {
					    $selected = 'selected';
					} else {
					    $selected = '';
					}
					$html_element .= '<option value="'.$k_country.'" '.$selected.'>'.$v_country.'</option>';
				}
				$html_element .= '</Select>';
				return $html_element;
			} else {
			 return $this->_t('Error', 'Invalid Argument.');
			}
			
		}
	// Billing or shipping country code functions using arguments
	
	// Billing or shipping state province code public functions using arguments
	
	public function Set_State_Province($type = 'billing') //  Billing Address/Shipping address state or province
		{
			if ($type == 'billing' || $type == 'shipping') {
				if ($type == 'billing') {
					$name = 'BillingStateProvince';
					$div_id = 'non_usa_billing_text';
				}
				if ($type == 'shipping') {
					$name = 'ShippingStateProvince';
					$div_id = 'non_usa_shipping_text';
				}
				

					$countryxmlpath		 = 'https://aws.assets.clickandpledge.com/global/xml/ISO-Countries-States-Provinces.xml'; 
					$countryxml          = simplexml_load_file($countryxmlpath);
					$countryjson_string  = json_encode($countryxml);
					$countryresult_array = json_decode($countryjson_string, TRUE);
					$cntr=""; $inc=0; $cnpcntryarr= array();$html_element ="";
					foreach($countryresult_array as $cntryrslt){ }
				   
				   foreach($cntryrslt as $cntryrslt123)	{  }
				   foreach($cntryrslt123 as $cntryrslt456)	 
					 { 
						 
					   
					   $cntryabrv  = $cntryrslt456["@attributes"]["Abbrev"]."_".$name;
				   
					   foreach($cntryrslt456 as $cntryrslt789)	{
				   
						 } 
						 	
						 foreach($cntryrslt789 as $cntryrslt689)	{
							//echo $html_element =  "hi".$cntryrslt689["@attributes"]["Code"];
							
						if($cntryrslt456["@attributes"]["Code"] == '840'){
							$html_element .= '<div id="'.$cntryrslt456["@attributes"]["Code"].'" class="CnPClass">';
						}
						else{
							$html_element .= '<div id="'.$cntryrslt456["@attributes"]["Code"].'" style="display:none;">';
						}
						   $html_element .= '<select name="'.$cntryabrv.'" size="1" id="'.$cntryabrv.'"  >';
						   if (count($cntryrslt689) ==1)
						   {
							$cnpstateabrv = $cntryrslt689["@attributes"]["Abbrev"];
							$cnpstatenm = $cntryrslt689["@attributes"]["Name"];
							$html_element .= '<option value="'.$cnpstateabrv.'" '.$selected.'>'.$cnpstatenm.'</option>';
						   }
						   
						   else{
							 for($stinc =0; $stinc < count($cntryrslt689); $stinc++ )
							 {
								   $cnpstateabrv = $cntryrslt689[$stinc]["@attributes"]["Abbrev"];
								   $cnpstatenm = $cntryrslt689[$stinc]["@attributes"]["Name"];
								   $html_element .= '<option value="'.$cnpstateabrv.'" '.$selected.'>'.$cnpstatenm.'</option>';
							   }
						   }	   
							 }
				   
				   $html_element .= '</select></div>';
				   
					   } 
					   $html_element .='<div id="cnpnonstate" style="display:none;"><input type="text" name="cnpnonstatev" id="cnpnonstatev" value="" maxlength="50"/><div>'; 
					   return $html_element;
			} else {
				   return $this->_t('Error', 'Invalid Argument.');
			}
		}
	//End of Billing or shipping state province code functions using arguments
	
		
	// Custom Questions
	
	// Additional information collected from the card holder.  Field name is typically used as the question for the field and the FieldValue used as the answer.  Example:  Who is your employer?  FieldName will be saved and may be exported through the report section.
	public function Set_Custom_Question($id, $question)
		{
			$html_element = $this->_html_input_element('hidden', 'FieldName['.$id.']', $question, 200);
			return $html_element;
		}
		
	// Answer to the FieldName question.  Example:  Click & Pledge for the Who is your Employer?  FieldName.  FieldValue will be saved and may be exported through the report section.
	public function Set_Custom_Answer($id)
		{
			$html_element = 'FieldValue['.$id.']';
			return $html_element;
		}
	// End of Custom Questions
	
	
	// Product Information
	
	// item�s unit price.  Total price will be calculated based on quantity times the unit price.
	public function Set_Unit_Price($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'UnitPrice['.$basket_id.']';
			return $html_element;
		}
	
	// The tax deductible portion of the unit price of the item.  For example:  T-shirt is $20 and the tax deductible portion is $10.  The receipt will show the total tax deductible portion of the total price.  Tax deductible portion will be included in the receipt as well as the report.
	public function Set_Unit_Deductible($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'UnitDeductible['.$basket_id.']';
			return $html_element;
		}
		
	 // An ID number identifying the item being paid for.  The number is for information only and will be listed in the report.  The item may be treated as a line number or internal inventory number.
	public function Set_ItemID($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'ItemID['.$basket_id.']';
			return $html_element;
		}
		
	// The name of the item in the transaction.  Example: Donation, T-Shirt, Event Ticket, etc.
	public function Set_ItemName($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'ItemName['.$basket_id.']';
			return $html_element;
		}
	
	// Quantity to be purchased.  The quantity will be included in the receipt.  The total calculation will be checked against the quantity and unit price multiplication.	
	public function Set_Quantity($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'Quantity['.$basket_id.']';
			return $html_element;
		}
		
	public function Set_SKU($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'SKU['.$basket_id.']';
			return $html_element;
		}
	public function Set_ConnectCampaign($basket_id = '')
		{
			$html_element = 'ConnectCampaign'; 
			return $html_element;
		}
	// The tax for the item purchased.  Total tax will be calculated based on the quantity purchased and the unit tax.  Total tax will be included in the receipt as well as the report.	
	public function Set_UnitTax($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'UnitTax['.$basket_id.']';
			return $html_element;
		}
	
	// The discount to be applied towards the purchase.  Total discount will appear in the receipt as well as the report.	
	public function Set_UnitDiscount($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'UnitDiscount['.$basket_id.']';
			return $html_element;
		}
	
	public function Set_Other($basket_id = '')
		{
			if (!preg_match("/^[1-9]+$/", $basket_id)) {
			    return $this->_t('Error', 'Invalid Basket Id.');
			}
			$html_element = 'Other['.$basket_id.']';
			return $html_element;
		}
		
	public function Set_OtherValue()
		{
			return $html_element = 'Other';
		}
	// End of Product Information
	
	// Recurring
	public function Set_Periodicity()
		{
			$html_element = 'Periodicity';
			return $html_element;
		}
	public function Set_Recurring_Method()
		{
			$html_element = "RecurringMethod";
			return $html_element;
		}
	public function Set_No_Of_Recurring()
		{
			$html_element = "No_Of_Recurring";
			return $html_element;
		}
	// End of Recurring
	
	// Payment Information CreditCard,Check,Invoice,Purchase Order
	
	// CreditCard
	public function Set_PaymentType()
		{
			$html_element = 'PaymentType';
			return $html_element;
		}
	
	// Name as it appears on credit card
	public function Set_NameOnCard()
		{
			$html_element = 'NameOnCard'; 
			return $html_element;
		}
		
	// Credit card number to be charged.  Use 4111111111111111 if transacting in Test mode.
	public function Set_CardNumber()
		{
			$html_element = 'CardNumber'; 
			return $html_element;
		}
		
	// Card verification number or security code on the back of the credit card.  Use 123 if transacting in Test mode.
	public function Set_Cvv()
		{
			$html_element = 'Cvv2';
			return $html_element;
		}
	// Credit card expiration date.  Use any date in future if transacting in Test mode.		
	public function Set_Expiration_Date() 
		{
			$html_element = '<div class="styled-select"><select name="ExpirationMonth" id="ExpirationMonth">';
			for ($i = 1; $i <= 12; $i++) {
			  $month_num =  date("m");
			  $all =date("m",strtotime(date('Y-'.$i.'-01')));
			  
				if ($all == $month_num) {
					$selected = 'selected';
				} else {
					$selected='';
				}
				$html_element .= '<option value="'.$all.'" '.$selected.'>'.date("F",strtotime(date('Y-'.$i.'-01'))).'</option>';
			}
			
			$html_element .= '</select></div><div class="styled-select" style="width:70px;"><select name="ExpirationYear" id="ExpirationYear" style="width:90px;">';
			for ($i = 0; $i <= 20; $i++) {
				$year = date('Y')+$i;
				$year_two = date('y')+$i;
				$html_element .=	'<option value="'.$year_two.'">'.$year.'</option>';
			}
			$html_element .=	'</select></div>';
			return $html_element;
		}
	//End of CreditCard
	
	//check
	public function Set_NameOnAccount()
		{
			$html_element = 'NameOnAccount';
			return $html_element;
		}
	
	public function Set_RoutingNumber()
		{
			$html_element = 'RoutingNumber';
			return $html_element;
		}
		
	public function Set_CheckNumber()
		{
			$html_element = 'CheckNumber';
			return $html_element;
		}
	
	public function Set_AccountNumber()
		{
			$html_element = 'AccountNumber';
			return $html_element;
		}
	
	public function Set_AccountType()
		{
			$html_element = 'AccountType';
			return $html_element;
		}
	
	public function Set_CheckType()
		{
			$html_element = 'CheckType';
			return $html_element;
		}
	//End of Check
	
	// Invoice
	/*public function Set_InvoiceNumber()
		{
			$html_element = 'InvoiceNumber';
			return $html_element;
		}*/
	// End of Invoice
	
	// CustomPaymentType
	public function Set_CustomPaymentNumber()
	{
		$html_element = 'CustomPaymentTypeNumber';
		return $html_element;
	}
	public function Set_CustomPaymentType()
		{
			$html_element = 'CustomPaymentName';
			return $html_element;
		}
	// End of CustomPaymentType
	
	// Charge date.  		
	public function Set_Charge_Date() 
		{
			$html_element = '<div class="styled-select"><select name="chargeDay" id="chargeDay">';
			for ($i = 1; $i <= 31; $i++) {
				$cnpday = date('d');
				if ($i == $cnpday) {
					$selected = 'selected';
				} else {
					$selected='';
				}

				$html_element .=	'<option value="'.$i.'"'.$selected.'>'.$i.'</option>';
			}
			$html_element .= '</select><select name="chargeMonth" id="chargeMonth">';
			for ($i = 1; $i <= 12; $i++) {
			  $month_num =  date("m");
			  $all =date("m",strtotime(date('Y-'.$i.'-01')));
			  
				if ($all == $month_num) {
					$selected = 'selected';
				} else {
					$selected='';
				}
				$html_element .= '<option value="'.$all.'" '.$selected.'>'.date("F",strtotime(date('Y-'.$i.'-01'))).'</option>';
			}
			
			$html_element .= '</select><select name="chargeYear" id="chargeYear" style="width:90px;">';
			for ($i = 0; $i <= 20; $i++) {
				$year = date('Y')+$i;
				$year_two = date('y')+$i;
				$html_element .=	'<option value="'.$year_two.'">'.$year.'</option>';
			}
			$html_element .=	'</select></div>';
			return $html_element;
		}
	// The terms & conditions to be added at the bottom of the receipt.  Typically the text provides proof that the patron has read the terms & conditions and has agreed to it.
	public function Set_Terms_Conditions($Terms_Conditions, $required = 'no')
		{
			if ($Terms_Conditions == '') { 
			    return $this->_t('Error', 'Invalid Argument.');
			}
			if (($required == 'yes' || $required == 'no')) {
			
			    $html_element = $this->_t('','<div><b>Terms & Conditions</b></div>');
				if ($required == 'yes') {
				   $html_element .= $this->_html_input_element('hidden', 'Terms', $required).$this->_html_input_element('checkbox', 'Terms_Condition_Required', $required);
				}
				$html_element .= '<span class="terms_span">'.$this->_html_input_element('hidden', 'Terms_Condition', $Terms_Conditions, 1500).$Terms_Conditions.'</span>';
				return $html_element;
				
			} else {
			   return $this->_t('Error', 'Invalid Argument.');
			}
		}
	
	//End of Payment Information CreditCard,Check,Invoice,Purchase Order
	
	
	// shipping method
	
	// The shipping method to be used for this order.  Example:  FedEx, UPS, Regular Mail.  Shipping method will appear in the receipt as well as the report.
	public function Set_ShippingMethod()
		{
			$html_element = 'ShippingMethod';
			return $html_element;
		}
	
	// Total shipping cost of the order.  Shipping cost will appear in the receipt as well as the report.
	public function Set_ShippingValueTax($method, $methodvalue = 0, $methodtax = 0)
		{
			if (!preg_match($this->tax_value, trim($methodvalue))) {
			    return $this->_t('Error', 'Invalid  Shipping Value.');
			}
			if (!preg_match($this->tax_value, trim($methodtax))) {
			    return $this->_t('Error', 'Invalid  Shipping Tax.');
			}
			$html_element = $this->_html_input_element('hidden', $method.'[ShippingValue]', $methodvalue);
			$html_element .= $this->_html_input_element('hidden', $method.'[ShippingTax]', $methodtax);
			return $html_element;
		}
	
	//End of shipping method
	
	//display text message
	
	public function _t($type, $message)
	   {
			switch ($type) {
			case 'Error':
			  return "<span class='error'>".$message."</span>";
			  break;
			  
			case 'Sucess':
			  return "<span class='sucess'>".$message."</span>";
			  break;
			  
			default:
			  return $message;
			} 		
		}
			
	public function _html_input_element($type, $element_name, $element_value, $maxlength = '')
	   {
			if (!empty($maxlength)) {
			    $length = 'maxlength="'.$maxlength.'"';
			} else {
			    $length ='';
			}
			$element = '<input type="'.$type.'" name="'.$element_name.'" id="'.$element_name.'" value="'.$element_value.'" '.$length.'/>';
			return $element;
	   }
			
	public function _isset($data)
	   {
		   return isset($data);
	   }
		   
	public function _getpostdata($data)
	   {
		 $data = $_POST["$data"];
		  return $data;
	   }
	
	public function _addslashesdata($data)
		{
		  $data = addslashes($_POST["$data"]);
		  return $data;
		}
	   
	// Processing form
	public function Check()
	   {
        $errors = array();
		 if ($this->_isset($this->_getpostdata('submit')) && $this->_getpostdata('submit') == 'Submit') {
			$post_data = $_POST;
			$checkkeys = array_keys($post_data);
			
			if (!$this->_isset($this->_getpostdata('currency'))) {
				$errors[] = $this->_t('Error', 'Currency Field Missing.');
			}
			
			//curl checking
			if (!function_exists('curl_init')) {
				$errors[] = $this->_t('Error', 'Enable cURL in your service.');
			}
			
			//Accountid and guid checking
			if (!$this->_isset($this->_getpostdata('AccountToken')))	{
				if ($this->_isset($this->_getpostdata('Accountid')) && $this->_isset($this->_getpostdata('Guid'))) {
					$accountid = $this->_getpostdata('Accountid');
					$guid = $this->_getpostdata('Guid');
					if ($this->_isset($this->_getpostdata('WID')) && trim($_POST['WID']) != '') {
						$wid = $this->_getpostdata('WID');
						if (!preg_match("/^[0-9]{1,10}+$/", $wid) || $wid == 0) {
							$errors[] = $this->_t('Error', 'Invalid WID.');
						}
					}
				} else {
					$errors[]= $this->_t('Error', 'Accountid and Guid are not declared.');
				}
			} else {
				 $AccountToken  = $this->_getpostdata('AccountToken');
				 $AccountToken_data = explode("/", $AccountToken);
				 if (count($AccountToken_data) > 1) {
					if (!preg_match("/^[0-9]+$/", $AccountToken_data[1])) {
						$errors[] =  $this->_t('Error', 'Invalid AccountID.');
					}
					if (!preg_match("/^[a-zA-Z0-9\-]+$/", $AccountToken_data[0])) {
						$errors[] = $this->_t('Error', 'Invalid GUID.');
					}
					$guid = $AccountToken_data[0];
					$accountid = $AccountToken_data[1];
					if ($this->_isset($AccountToken_data[2]) && trim($AccountToken_data[2]) != '') {
						$wid  = $AccountToken_data[2];
						if (!preg_match("/^[0-9]{1,10}+$/", $wid) || $wid == 0) {
							$errors[] = $this->_t('Error','Invalid WID.');
						}
					}
					if ($this->_isset($AccountToken_data[3]) && trim($AccountToken_data[3]) != '') {
						$tracker = $AccountToken_data[3];
						if (!preg_match("/^([a-zA-Z0-9\-\_ ]){2,50}+$/", $tracker)) {
							$errors[] = $this->_t('Error', 'Invalid Tracker.');
						}
					}
				} else {
				   $errors[] = $this->_t('Error', 'Invalid AccountToken Declaration.');
				}
		   }
		   
		   $compulsoryfields = array('BillingFirstName', 'BillingLastName', 'Quantity', 'ItemName', 'Order_Mode');
		   $mode = $this->_getpostdata('Order_Mode');
		   $ConnectCampaign = $this->_getpostdata('ConnectCampaign');
		   if (in_array('Invalid_Basket_Id', $checkkeys)) {
			   $errors[] = $this->_t('Error', 'Invalid Basket Declaration.');
		   }
		
		   for ($i = 0,$m = 0;$i < count($compulsoryfields);$i++ ) {
				if (!in_array($compulsoryfields[$i], $checkkeys)) {
					 if ($compulsoryfields[$i] == 'Order_Mode') {
						 $compulsoryfields[$i] = 'Order Mode';
					 }
					$errors[] = $this->_t('Error', "Required field $compulsoryfields[$i] Missing.");
					$n=$m++;
				}
		   }
		   
		   if ($n == 0) {
			  if ($this->_isset($this->_getpostdata('BillingFirstName'))) {
				  if (!preg_match($this->f_l_name, trim($this->_getpostdata('BillingFirstName')))) {
					  $errors[] = $this->_t('Error', 'Invalid First Name.');
				  }
			  }	   
			  if ($this->_isset($this->_getpostdata('BillingLastName'))) {
				  if (!preg_match($this->f_l_name, trim($this->_getpostdata('BillingLastName')))) {
					  $errors[] = $this->_t('Error', 'Invalid Last Name.');
				  }
			  }
			  if ($this->_getpostdata('BillingMI') != '') {
				  if (!preg_match($this->m_name, $this->_getpostdata('BillingMI'))) {
					  $errors[] = $this->_t('Error', "Invalid Middle Name.");
				  }
			  }
			  if ($this->_getpostdata('BillingEmail') != '') {
				  if (!preg_match($this->email, $this->_getpostdata('BillingEmail'))) {
					  $errors[] = $this->_t('Error', "Invalid Email.");
				  }
				  if (strlen($_POST['BillingEmail']) >50) {
					  $errors[] = $this->_t('Error', "Invalid Email Length.");
				  }
			  }
			  if ($this->_getpostdata('BillingPhone') != '') {
				  if (!preg_match($this->phone, trim($this->_getpostdata('BillingPhone')))) {
					  $errors[] = $this->_t('Error', "Invalid Phone Number.");
				 }
			  }
			  if ($this->_getpostdata('BillingAddress1') != '') {
				  if (!preg_match($this->address,trim($this->_getpostdata('BillingAddress1')))) {
					  $errors[] = $this->_t('Error',"Invalid Address1.");
				  }
			  }
			  if ($this->_getpostdata('BillingAddress2') != '') {
				  if (!preg_match($this->address, trim($this->_getpostdata('BillingAddress2')))) {
					  $errors[] = $this->_t('Error', "Invalid Address2.");
				  }
			  }
			  if ($this->_getpostdata('BillingAddress3') != '') {
				  if (!preg_match($this->address, trim($this->_getpostdata('BillingAddress3')))) {
					  $errors[] = $this->_t('Error', "Invalid Address3.");
				  }
			  }
			  if ($this->_getpostdata('BillingCity') != '') {
				 if (!preg_match($this->city, trim($this->_getpostdata('BillingCity')))) {
					 $errors[] = $this->_t('Error', "Invalid City.");
				 }
			  }
			  if ($this->_getpostdata('BillingStateProvince') != '') {
				  $cnpbilling =  $this->_getpostdata('BillingCountryCode').'_BillingStateProvince';
				  if (!preg_match($this->stateprovince, $this->_getpostdata($cnpbilling))) {
					  $errors[] = $this->_t('Error', "Invalid State Province.");
				  }
			  }
			  if ($this->_getpostdata('BillingPostalCode') != '') {
				  if (!preg_match($this->postalcode, trim($this->_getpostdata('BillingPostalCode')))) {
					  $errors[] = $this->_t('Error', "Invalid Postal Code.");
				  }
			  }
			  if ($this->_getpostdata('BillingCountryCode') != '') {
				  if (!preg_match($this->country, $this->_getpostdata('BillingCountryCode'))) {
					  $errors[] = $this->_t('Error', "Invalid Country.");
				  }
			  }
		   }
		   if ($this->_isset($this->_getpostdata('RecurringMethod'))) {
			  $RecurringMethod = $this->_getpostdata('RecurringMethod');
			  $No_Of_Recurring = $this->_getpostdata('No_Of_Recurring');
			  if (!$this->_isset($this->_getpostdata('No_Of_Recurring'))) {
				  $errors[] = $this->_t('Error', "Required field No Of Recurring.");
			  }
			  if (!$this->_isset($this->_getpostdata('Periodicity'))) {
				  $errors[] = $this->_t('Error', "Required field Periodicity.");
			  }
			  if (!in_array($this->_getpostdata('RecurringMethod'), $this->allowed_recurringmethod)) {
				  $errors[] = $this->_t('Error', "Invalid Recurring Method.");
			  }
			  if ($RecurringMethod  == 'Installment') {
				  if (!preg_match($this->installments, $this->_getpostdata('No_Of_Recurring')) ||$this->_getpostdata('No_Of_Recurring') == 1 || $this->_getpostdata('No_Of_Recurring') > 998 ) {
					  $errors[] = $this->_t('Error', "Invalid No Of Installments.");
				  }
			  }
			  if ($RecurringMethod  == 'Subscription') {
				  if (!preg_match($this->installments, $this->_getpostdata('No_Of_Recurring')) || $this->_getpostdata('No_Of_Recurring') == 1) {
					  $errors[] = $this->_t('Error', "Invalid No Of Subscription.");
				  }
			  }		
			  if ($this->_isset($this->_getpostdata('Periodicity'))) {
				  if (!in_array($this->_getpostdata('Periodicity'), $this->periodicity_options)) {
					  $errors[] = $this->_t('Error', "Invalid Periodicity.");
				  }
			  }
		   }
		   $Quantity = $this->_getpostdata('Quantity');
		   $ItemID = $this->_getpostdata('ItemID');
		   $UnitDeductible = $this->_getpostdata('UnitDeductible');
		   $SKU = $this->_getpostdata('SKU');
		   $UnitPrice = $this->_getpostdata('UnitPrice');
		   $Other = $this->_getpostdata('Other');
		   $ItemName = $this->_getpostdata('ItemName');
		   $UnitDiscount = $this->_getpostdata('UnitDiscount');
		   $UnitTax = $this->_getpostdata('UnitTax');
		   for ($i = 1, $m = 0; $i <= count($ItemName); $i++) {
				if (!preg_match("/^[0-9]{0,6}$/", $Quantity[$i])) {
					 $errors[] = $this->_t('Error', "Invalid Quantity.");
				}
				if ($UnitPrice[$i] == 'Other') {
					$UnitPrice[$i] =$Other[$i];
				}  
				if ($Quantity[$i] != '' && $Quantity[$i] != 0 && $UnitPrice[$i] !='') {
					$m++;
				}
                if($UnitPrice[$i] != '' && is_numeric($UnitPrice[$i]) != 1){
                   $errors[] = $this->_t('Error', "Invalid amount, Please use numeric values only.");
                }
		   }
		   if ($m==0) {
			   $errors[] = $this->_t('Error', "Basket is empty.");
		   }
		   if (in_array('ShippingAddress1', $checkkeys) || 
			   in_array('ShippingCity', $checkkeys) ||
			   in_array('ShippingCountryCode', $checkkeys) ||
			   in_array('ShippingStateProvince', $checkkeys)||
			   in_array('ShippingPostalCode', $checkkeys ) ||
			   in_array('ShippingMethod', $checkkeys)) {
			
			   $shippingfields = array('ShippingAddress1', 'ShippingCity', 'ShippingCountryCode');
			   for ($i = 0, $k = 0; $i < count($shippingfields); $i++ ) {
					if (!in_array($shippingfields[$i], $checkkeys)) {
						$errors[] = $this->_t('Error', "Required fields $shippingfields[$i] Missing.");
						$v=$k++;
					}
			   }
			   if ($v==0) {
			   
				   if ($this->_getpostdata('ShippingFirstName') != '') {
					   if (!preg_match($this->f_l_name, trim($this->_getpostdata('ShippingFirstName')))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping First Name.");
					   }
				   }
				   if ($this->_getpostdata('ShippingLastName') != '') {
					   if (!preg_match($this->f_l_name, trim($this->_getpostdata('ShippingLastName')))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping Last Name.");
					   }
				   }
				   if ($this->_getpostdata('ShippingMI') != '') {
					   if (!preg_match($this->m_name, $this->_getpostdata('ShippingMI'))) {
							$errors[] = $this->_t('Error', "Invalid Shipping Middle Name.");
					   }
				   }	   
				   if ($this->_getpostdata('ShippingEmail') != '') {
					   if (!preg_match($this->email, $this->_getpostdata('ShippingEmail'))) {
							$errors[] = $this->_t('Error', "Invalid Shipping Email.");
					   }
				   }
				   if ($this->_getpostdata('ShippingPhone') != '') {
					   if (!preg_match($this->phone, trim($this->_getpostdata('ShippingPhone')))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping Phone Number.");
					   }
				   }
				   if ($this->_isset($this->_getpostdata('ShippingAddress1'))) {
					   if (!preg_match($this->address, trim($this->_getpostdata('ShippingAddress1')))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping Address1.");
					   }
				   }
				   if ($this->_isset($this->_getpostdata('ShippingCity'))) {
					   if (!preg_match($this->city, trim($this->_getpostdata('ShippingCity')))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping City.");
					   }
				   }
				   if ($this->_getpostdata('ShippingStateProvince') != '') {
					$cnpsbilling =  $this->_getpostdata('ShippingCountryCode').'_ShippingStateProvince';
					   if (!preg_match($this->stateprovince, $this->_getpostdata($cnpsbilling))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping State Province.");
					   }
				   }
				   if ($this->_isset($this->_getpostdata('ShippingPostalCode'))) {
					   if (!preg_match($this->postalcode, $this->_getpostdata('ShippingPostalCode'))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping Postal Code.");
					   }
				   }
				   if ($this->_getpostdata('ShippingCountryCode') != '') {
					   if (!preg_match($this->country, $this->_getpostdata('ShippingCountryCode'))) {
						   $errors[] = $this->_t('Error', "Invalid Shipping Country.");
					   }
					}
			   }		   
		   }
		   if ($this->_isset($this->_getpostdata('ShippingMethod'))) {
		   
			   if (!preg_match($this->shipping_name, trim($this->_getpostdata('ShippingMethod')))) {
				   $errors[] = $this->_t('Error', "Invalid Shipping Method Name.");
			   }
			   $val = $this->_getpostdata('ShippingMethod');
			   if (is_array($val)) {
				   $errors[] = $this->_t('Error', "Invalid Shipping Value/Tax Declaration.");
			   } else {
				  if (!preg_match($this->tax_value, trim($_POST[$val]['ShippingValue']))) {
					  $errors[] = $this->_t('Error', "Invalid Shipping Value.");
				  }
				  if (!preg_match($this->tax_value, trim($_POST[$val]['ShippingTax']))) {
					  $errors[] = $this->_t('Error', "Invalid Shipping Tax.");
				  }
			  }
		  }
		  $allowed_paymenttypes = array('CreditCard', 'Check','CustomPaymentType');
		  $PaymentType = $this->_getpostdata('PaymentType');
		  if (in_array($PaymentType, $allowed_paymenttypes)) {
			 $echarge_date =  $this->_getpostdata('chargeYear')."-". $this->_getpostdata('chargeMonth')."-". $this->_getpostdata('chargeDay');
			if($echarge_date != "--"){

			
			 $Date1 = strtotime(date('Y-m-d', strtotime($echarge_date) ) ).' ';
			 $Date2 = strtotime(date('Y-m-d'));
			 
			if($Date1 < $Date2) {
				$errors[] = $this->_t('Error', "Transaction Date should not be less than system date."); 
			}
		}
			  if ($PaymentType == 'CreditCard') {
			  
				  $NameOnCard = $this->_getpostdata('NameOnCard');
				  $exp_date = $this->_getpostdata('ExpirationMonth')."/".$this->_getpostdata('ExpirationYear');
				  $ExpirationYear = $this->_getpostdata('ExpirationYear');
				  $ExpirationMonth = $this->_getpostdata('ExpirationMonth');
				  $CardNumber = $this->_getpostdata('CardNumber');
				  $cvv = $this->_getpostdata('Cvv2');
				  $current_year = date('y');
				  $current_month = date('m');
				 
				  if (!$this->_isset($CardNumber)) {
					  $errors[] = $this->_t('Error', "CreditCard Number Filed Missing.");
				  }
				  if (!$this->_isset($exp_date)) {
					  $errors[] = $this->_t('Error', "ExpirationMonth/ExpirationYear Field Missing.");
				  }
				  if ($this->_isset($NameOnCard)) {
					 if (!preg_match($this->cardname, $NameOnCard)) {
						 $errors[] = $this->_t('Error', "Invalid Credit Card Name.");
					 }
				  }
				  if ($this->_isset($CardNumber)) {
					  if (!preg_match("/^[0-9]{15,17}$/", $CardNumber)) {
						  $errors[] = $this->_t('Error', "Invalid Credit Card Number.");
					  }
				  }
				  if ($cvv != '') {
					  if (!preg_match($this->cvv, $cvv)) {
						  $errors[] = $this->_t('Error', "Invalid Cvv Number.");
					  }
				  }
				  if ($this->_isset($exp_date)) {
					  if (($ExpirationMonth < $current_month)) {
						  $errors[] = $this->_t('Error', "Invalid Month/Year.");
					  }
				  }
				 
			 }
			 if ($PaymentType == 'Check') {
			 
				$NameOnAccount = $this->_getpostdata('NameOnAccount');
				$AccountNumber = $this->_getpostdata('AccountNumber');
				$RoutingNumber = $this->_getpostdata('RoutingNumber');
				$CheckNumber = $this->_getpostdata('CheckNumber');
				
				if (!$this->_isset($AccountNumber)) { 
					$errors[] = $this->_t('Error', "eCheck Account Number Filed Missing.");
				}
				if (!$this->_isset($RoutingNumber)) {
					$errors[] = $this->_t('Error', "eCheck Routing Number Filed Missing.");
				}
				if (!$this->_isset($CheckNumber)) {
					$errors[] = $this->_t('Error', "eCheck Check Number Filed Missing.");
				}
				if ($this->_isset($NameOnAccount)) {
					if (!preg_match($this->cardname, $NameOnAccount)) {
						$errors[] = $this->_t('Error', "Invalid Name On Account.");
					}
				}
				if ($this->_isset($AccountNumber)) {
					if (!preg_match("/^[0-9]{1,17}$/", $AccountNumber)) {
						$errors[] = $this->_t('Error', "Invalid Account Number.");
					}
				}
				if ($this->_isset($RoutingNumber)) {
					if (!preg_match("/^[0-9]{9}$/", $RoutingNumber)) {
						$errors[] = $this->_t('Error', "Invalid Routing Number.");
					}
				}
				if ($this->_isset($CheckNumber)) {
					if (!preg_match("/^[0-9]{1,10}$/", $CheckNumber)) {
						$errors[] = $this->_t('Error', "Invalid Check Number.");
					}
				}
				if ($this->_isset($this->_getpostdata('AccountNumber2'))) {
					if ($AccountNumber != $this->_getpostdata('AccountNumber2')) {
						$errors[] = $this->_t('Error', "AccountNumber and AccountNumber2 Should be Same");
					}
				}
			 }
            /*  
			 if ($PaymentType == 'Invoice') {
			 
				 $Invoice = $this->_getpostdata('InvoiceNumber');
				 if (!$this->_isset($Invoice)) {
					 $errors[] = $this->_t('Error', "Invoice Number Filed Missing.");
				 }
				if ($this->_isset($Invoice)) {
					if (!preg_match("/^[0-9]{1,100}$/", $Invoice)) {
						$errors[] = $this->_t('Error', "Invalid Invoice Number");
					}
				}
			 }*/
			 if ($PaymentType == 'CustomPaymentType') {
			 
				 $CustomPayment_Order = $this->_getpostdata('PaymentType');
				 if (!$this->_isset($CustomPayment_Order)) {
					 $errors[] = $this->_t('Error', "Custom Payment Type Filed Missing.");
				 }
				
			 }
		
		  } else {
			 $errors[] = $this->_t('Error', "Payment Method Is Not Supported1.");
		  }
		  if (count($errors) > 0) {
		  
			  $html_element = '<div id="error"><ul class="error_ul">';
			  foreach ($errors as $message) {
					   $html_element .= "<li class='error_li'>".$message."</li>";
			  }
			  $html_element .= '</ul></div>';
			  echo $html_element;
			  
		  } else {
			$cnpsbilling =  $this->_getpostdata('ShippingCountryCode').'_ShippingStateProvince';
			 $FieldName = $this->_getpostdata('FieldName');
			 $FieldValue = $this->_getpostdata('FieldValue');
			 $ShippingFirstName = $this->_addslashesdata('ShippingFirstName');
			 $ShippingLastName = $this->_addslashesdata('ShippingLastName');
			 $ShippingMI = $this->_addslashesdata('ShippingMI');
			 $ShippingEmail = $this->_getpostdata('ShippingEmail');
			 $ShippingPhone = $this->_getpostdata('ShippingPhone');
			 $ShippingAddress1 = $this->_addslashesdata('ShippingAddress1');
			 $ShippingCity = $this->_addslashesdata('ShippingCity');
			 $ShippingStateProvince = $this->_addslashesdata($cnpsbilling);
			 $ShippingPostalCode = $this->_getpostdata('ShippingPostalCode');
			 $ShippingCountryCode = $this->_addslashesdata('ShippingCountryCode');
			 
			 if ($this->_isset($this->_getpostdata('OrganizationInformation')) && $this->_getpostdata('OrganizationInformation') != '') {
				 $OrganizationInformation = $this->_addslashesdata('OrganizationInformation');
			 }
			 
			 if ($this->_isset($this->_getpostdata('ThankYouMessage')) && $this->_getpostdata('ThankYouMessage') != '') {
				 $ThankYouMessage = $this->_addslashesdata('ThankYouMessage');
			 }
			 
			 $ip_address = $_SERVER['REMOTE_ADDR'];
			 $dom = new DOMDocument('1.0', 'UTF-8');
			 $root = $dom->createElement('CnPAPI', '');
			 $root->setAttribute("xmlns", "urn:APISchema.xsd");
			 $root = $dom->appendChild($root);
			  
			 $version=$dom->createElement("Version", "1.1");
			 $version=$root->appendChild($version);
			 
			 $engine = $dom->createElement('Engine', '');
			 $engine = $root->appendChild($engine);
			 
			 $application = $dom->createElement('Application', '');
			 $application = $engine->appendChild($application);
	
			 $applicationid = $dom->createElement('ID', 'CnP:PaaS:CnP_Form_Processer:1.2');
			 $applicationid = $application->appendChild($applicationid);
			
			 $applicationname = $dom->createElement('Name', 'PHP:CnP_Form_Processer');
			 $applicationid = $application->appendChild($applicationname);
			
			 $applicationversion = $dom->createElement('Version', '1.0');
			 $applicationversion = $application->appendChild($applicationversion);
	
			 $request = $dom->createElement('Request', '');
			 $request = $engine->appendChild($request);
	
			 $operation = $dom->createElement('Operation', '');
			 $operation = $request->appendChild($operation);
			 
			 $operationtype = $dom->createElement('OperationType', 'Transaction');
			 $operationtype = $operation->appendChild($operationtype);
	
			 $ipaddress = $dom->createElement('IPAddress', $ip_address);
			 $ipaddress = $operation->appendChild($ipaddress);
	
			 $authentication = $dom->createElement('Authentication', '');
			 $authentication = $request->appendChild($authentication);
			
			 $accounttype = $dom->createElement('AccountGuid', $guid ); 
			 $accounttype = $authentication->appendChild($accounttype);
	
			 $accountid = $dom->createElement('AccountID', $accountid );
			 $accountid = $authentication->appendChild($accountid);
			 
			 $order = $dom->createElement('Order', '');
			 $order = $request->appendChild($order);
			 
			 $ordermode = $dom->createElement('OrderMode', $mode);
			 $ordermode = $order->appendChild($ordermode);	
			 
			 if ($this->_isset($this->_getpostdata('Tracker')) && $this->_getpostdata('Tracker') != '') {
				 $tracker = $dom->createElement('Tracker', $this->_getpostdata('Tracker'));
				 $tracker = $order->appendChild($tracker);
			 }
	
			 if ($this->_isset($this->_getpostdata('Campaign')) && $this->_getpostdata('Campaign') != '') {


				$campaign  = $dom->createElement('Campaign','');
				$campaign  = $order->appendChild($campaign);
				$campaign->appendChild($dom->createCDATASection($this->_addslashesdata('Campaign')));

			 }
			 if ($this->_isset($this->_getpostdata('ConnectCampaign')) && $this->_getpostdata('ConnectCampaign') != '') {
			

				$ConnectCampaignAlias  = $dom->createElement('ConnectCampaignAlias','');
				$ConnectCampaignAlias  = $order->appendChild($ConnectCampaignAlias);
				$ConnectCampaignAlias->appendChild($dom->createCDATASection($this->_addslashesdata('ConnectCampaign')));
			}
			 $cardholder = $dom->createElement('CardHolder', '');
			 $cardholder = $order->appendChild($cardholder);
			 
			 $billinginfo = $dom->createElement('BillingInformation', '');
			 $billinginfo = $cardholder->appendChild($billinginfo);
	

			 $billfirst_name  = $dom->createElement('BillingFirstName','');
			 $billfirst_name  = $billinginfo->appendChild($billfirst_name);
			 $billfirst_name->appendChild($dom->createCDATASection($this->_addslashesdata('BillingFirstName')));
			 
			 if ($this->_getpostdata('BillingMI') != '') {
			
				 $billmiddle_name  = $dom->createElement('BillingMI','');
				 $billmiddle_name  = $billinginfo->appendChild($billmiddle_name);
				 $billmiddle_name->appendChild($dom->createCDATASection($this->_addslashesdata('BillingMI')));
			 }
			
		

			 $billlast_name  = $dom->createElement('BillingLastName','');
			 $billlast_name  = $billinginfo->appendChild($billlast_name);
			 $billlast_name->appendChild($dom->createCDATASection($this->_addslashesdata('BillingLastName')));
			 
			 
			 if ($this->_getpostdata('BillingEmail') != '') {
			

			 $bill_email  = $dom->createElement('BillingEmail','');
			 $bill_email  = $billinginfo->appendChild($bill_email);
			 $bill_email->appendChild($dom->createCDATASection($this->_addslashesdata('BillingEmail')));
			 }
			 
			 if ($this->_getpostdata('BillingPhone') != '') {
				 $bill_phone = $dom->createElement('BillingPhone', $this->_getpostdata('BillingPhone'));
				 $bill_phone = $billinginfo->appendChild($bill_phone);
			 }
			 
			 $billingaddress = $dom->createElement('BillingAddress', '');
			 $billingaddress = $cardholder->appendChild($billingaddress);
			 
			 if ($this->_getpostdata('BillingAddress1') != '') {
				

				 $billingaddress1  = $dom->createElement('BillingAddress1','');
				 $billingaddress1  = $billingaddress->appendChild($billingaddress1);
				 $billingaddress1->appendChild($dom->createCDATASection($this->_addslashesdata('BillingAddress1')));
			 }
			 
			 if ($this->_getpostdata('BillingAddress2') != '') {
				 $billingaddress2  = $dom->createElement('BillingAddress2','');
				 $billingaddress2  = $billingaddress->appendChild($billingaddress2);
				 $billingaddress2->appendChild($dom->createCDATASection($this->_addslashesdata('BillingAddress2')));
			 }
			 
			 if ($this->_getpostdata('BillingAddress3') != '') {
				 $billingaddress3  = $dom->createElement('BillingAddress3','');
				 $billingaddress3  = $billingaddress->appendChild($billingaddress3);
				 $billingaddress3->appendChild($dom->createCDATASection($this->_addslashesdata('BillingAddress3')));
			 }
			 
			 if ($this->_getpostdata('BillingCity') != '') {
				 $billing_city  = $dom->createElement('BillingCity','');
				 $billing_city  = $billingaddress->appendChild($billing_city);
				 $billing_city->appendChild($dom->createCDATASection($this->_addslashesdata('BillingCity')));
			 }
			 $cnpbilling =  $this->_getpostdata('BillingCountryCode').'_BillingStateProvince';
			
			
			 if($this->_getpostdata($cnpbilling) == "")
			 {
				$cnpbilling ='cnpnonstatev';
			 }
			
			 if ($this->_getpostdata($cnpbilling) != '') {
				 $billing_state  = $dom->createElement('BillingStateProvince','');
				 $billing_state  = $billingaddress->appendChild($billing_state);
				 $billing_state->appendChild($dom->createCDATASection($this->_getpostdata($cnpbilling)));
			 }
			 
			 if ($this->_getpostdata('BillingPostalCode') != '') {
				 $billing_zip = $dom->createElement('BillingPostalCode', $this->_getpostdata('BillingPostalCode'));
				 $billing_zip = $billingaddress->appendChild($billing_zip);
			 }
			 
			 if ($this->_getpostdata('BillingCountryCode') != '') {
				 $billing_country = $dom->createElement('BillingCountryCode','');
				 $billing_country  = $billingaddress->appendChild($billing_country);
				 $billing_country->appendChild($dom->createCDATASection($this->_getpostdata('BillingCountryCode')));
				
			 }
			 
			 if ($ShippingAddress1 != '') {
			 
				 $shippinginfo = $dom->createElement('ShippingInformation', '');
				 $shippinginfo = $cardholder->appendChild($shippinginfo);
				 
				 if ($ShippingFirstName != '' || $ShippingMI !='' || $ShippingLastName != '') {
				 
					 $shippingcontact = $dom->createElement('ShippingContactInformation', '');
					 $shippingcontact = $shippinginfo->appendChild($shippingcontact);
					 
					 if ($ShippingFirstName != '') {
						
						 $ship_first_name  = $dom->createElement('ShippingFirstName','');
						 $ship_first_name  = $shippingcontact->appendChild($ship_first_name);
						 $ship_first_name->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingFirstName')));
					 }
					 
					 if ($ShippingMI != '') {
						
						 $ship_mi  = $dom->createElement('ShippingMI','');
						 $ship_mi  = $shippingcontact->appendChild($ship_mi);
						 $ship_mi->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingMI')));
					 }
					 
					 if ($ShippingLastName != '') {
						
						 $ship_last_name  = $dom->createElement('ShippingLastName','');
						 $ship_last_name  = $shippingcontact->appendChild($ship_last_name);
						 $ship_last_name->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingLastName')));
					 }
					 
					 if ($ShippingEmail != '') {
						
						 $ship_email  = $dom->createElement('ShippingEmail','');
						 $ship_email  = $shippingcontact->appendChild($ship_email);
						 $ship_email->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingEmail')));
					 }
					 
					 if ($ShippingPhone != '') {
						 $ship_phone = $dom->createElement('ShippingPhone', $ShippingPhone);
						 $ship_phone = $shippingcontact->appendChild($ship_phone);    
					 }
				 }
				 
				 $shippingaddress = $dom->createElement('ShippingAddress', '');
				 $shippingaddress = $shippinginfo->appendChild($shippingaddress);
	

				 $shippingaddress1  = $dom->createElement('ShippingAddress1','');
				 $shippingaddress1  = $shippingaddress->appendChild($shippingaddress1);
				 $shippingaddress1->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingAddress1')));
				 
				 if ($this->_getpostdata('ShippingAddress2') != '') {
					
					 $ship_address1  = $dom->createElement('ShippingAddress2','');
					 $ship_address1  = $shippingaddress->appendChild($ship_address1);
					 $ship_address1->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingAddress2')));
				 }
				 
				 if ($this->_getpostdata('ShippingAddress3') != '') {
				
					 $ship_address2  = $dom->createElement('ShippingAddress3','');
					 $ship_address2  = $shippingaddress->appendChild($ship_address2);
					 $ship_address2->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingAddress3')));
				 }
				 
			

				 $ship_city  = $dom->createElement('ShippingCity','');
				 $ship_city  = $shippingaddress->appendChild($ship_city);
				 $ship_city->appendChild($dom->createCDATASection($this->_addslashesdata('ShippingCity')));
			
			
				 $ship_state  = $dom->createElement('ShippingStateProvince','');
				 $ship_state  = $shippingaddress->appendChild($ship_state);
				 $cnpsbilling =  $this->_getpostdata('ShippingCountryCode').'_ShippingStateProvince';
				 $ship_state->appendChild($dom->createCDATASection($this->_addslashesdata($cnpsbilling)));
				
				 $ship_zip = $dom->createElement('ShippingPostalCode', $ShippingPostalCode);
				 $ship_zip = $shippingaddress->appendChild($ship_zip);
				 
				 $ship_country = $dom->createElement('ShippingCountryCode', $ShippingCountryCode);
				 $ship_country = $shippingaddress->appendChild($ship_country);
			 
			 }
			 if ($FieldName != null && count($FieldName) >0) {
				 for ($i = 1, $csc = 0; $i <= count($FieldName); $i++) {
						 if (trim($FieldValue[$i]) !='') {
							 ++$csc;
						 }
				 }
				 if ($csc > 0) {
					 $customfieldlist = $dom->createElement('CustomFieldList', '');
					 $customfieldlist = $cardholder->appendChild($customfieldlist);
			
					 for ($i = 1;$i <= count($FieldName);$i++) {
						  if (trim($FieldValue[$i]) !='') {
							  $customfield = $dom->createElement('CustomField', '');
							  $customfield = $customfieldlist->appendChild($customfield);

						

							  $fieldname  = $dom->createElement('FieldName','');
							  $fieldname  = $customfield->appendChild($fieldname);
							  $fieldname->appendChild($dom->createCDATASection($FieldName[$i]));

							  $fieldvalue  = $dom->createElement('FieldValue','');
							  $fieldvalue  = $customfield->appendChild($fieldvalue);
							  $fieldvalue->appendChild($dom->createCDATASection($substr($FieldValue[$i], 0, 50)));
						  }
					} 
				}
			 }
			 
			 $paymentmethod = $dom->createElement('PaymentMethod', '');
			 $paymentmethod = $cardholder->appendChild($paymentmethod);
			 
			 if ($PaymentType == 'CreditCard') {
			 
				 $payment_type = $dom->createElement('PaymentType', 'CreditCard');
				 $payment_type = $paymentmethod->appendChild($payment_type);
			
				 $creditcard = $dom->createElement('CreditCard', '');
				 $creditcard = $paymentmethod->appendChild($creditcard);
				 
				 if (trim($this->_getpostdata('NameOnCard')) == '') {
					 $NameOnCard = $this->_addslashesdata('BillingFirstName').' '.$this->_addslashesdata('BillingLastName');
				 } else {
					 $NameOnCard = $this->_addslashesdata('NameOnCard');
				 }
				 
			

				 $credit_name  = $dom->createElement('NameOnCard','');
				 $credit_name  = $creditcard->appendChild($credit_name);
				 $credit_name->appendChild($dom->createCDATASection($NameOnCard));
			
				 $credit_number = $dom->createElement('CardNumber', str_replace(' ', '', $this->_getpostdata('CardNumber')));
				 $credit_number = $creditcard->appendChild($credit_number);
			 
				 if ($this->_getpostdata('Cvv2') !='') {
					 $credit_cvv = $dom->createElement('Cvv2', $this->_getpostdata('Cvv2'));
					 $credit_cvv = $creditcard->appendChild($credit_cvv);
				 }
			  
				 $credit_expdate = $dom->createElement('ExpirationDate', $exp_date);
				 $credit_expdate = $creditcard->appendChild($credit_expdate);
			 }
			 
			 if ($PaymentType == 'Check') {
			 
				 $payment_type = $dom->createElement('PaymentType', 'Check');
				 $payment_type = $paymentmethod->appendChild($payment_type);
				 
				 $echeck = $dom->createElement('Check', '');
				 $echeck = $paymentmethod->appendChild($echeck);
				 
				 $AccountNumber = $dom->createElement('AccountNumber', $this->_getpostdata('AccountNumber'));
				 $AccountNumber = $echeck->appendChild($AccountNumber);
				 
				 $AccountType = $dom->createElement('AccountType', $this->_getpostdata('AccountType'));
				 $AccountType = $echeck->appendChild($AccountType);
				 
				 $RoutingNumber = $dom->createElement('RoutingNumber', $this->_getpostdata('RoutingNumber'));
				 $RoutingNumber = $echeck->appendChild($RoutingNumber);
				 
				 $CheckNumber = $dom->createElement('CheckNumber', $this->_getpostdata('CheckNumber'));
				 $CheckNumber = $echeck->appendChild($CheckNumber);
	
				 $CheckType = $dom->createElement('CheckType', $this->_getpostdata('CheckType'));
				 $CheckType = $echeck->appendChild($CheckType);
				 

				 if($this->_getpostdata('NameOnAccount') != "")
				 {
					 $nameacc = $this->_getpostdata('NameOnAccount');
				 }
				 else{
					$nameacc = $this->_addslashesdata('BillingFirstName')." ".$this->_addslashesdata('BillingLastName');
				 }

				 $NameOnAccount  = $dom->createElement('NameOnAccount','');
				 $NameOnAccount  = $echeck->appendChild($NameOnAccount);
				 $NameOnAccount->appendChild($dom->createCDATASection($nameacc));
			 
			 }
			 /*
			 if ($PaymentType == 'Invoice') {
			 
				 $payment_type = $dom->createElement('PaymentType', 'Invoice');
				 $payment_type = $paymentmethod->appendChild($payment_type);
				 
				 $invoice = $dom->createElement('Invoice', '');
				 $invoice = $paymentmethod->appendChild($invoice);
				 
				 $CheckNumber = $dom->createElement('InvoiceCheckNumber', $this->_getpostdata('InvoiceNumber'));
				 $CheckNumber = $invoice->appendChild($CheckNumber);
			 
			 }
			*/
			 if ($PaymentType == 'CustomPaymentType') {

			
			 
				 $payment_type = $dom->createElement('PaymentType', 'CustomPaymentType');
				 $payment_type = $paymentmethod->appendChild($payment_type);
				 
				 $CustomPaymentType = $dom->createElement('CustomPaymentType', '');
				 $CustomPaymentType = $paymentmethod->appendChild($CustomPaymentType);
				 
				 $CustomPaymentName  = $dom->createElement('CustomPaymentName',$this->_getpostdata('CustomPaymentName'));
				 $CustomPaymentName  = $CustomPaymentType->appendChild($CustomPaymentName);
				

				 $CustomPaymentTypeNumber = $dom->createElement('CustomPaymentNumber', $this->_getpostdata('CustomPaymentTypeNumber'));
				 $CustomPaymentTypeNumber = $CustomPaymentType->appendChild($CustomPaymentTypeNumber);
			 
			 }
			 
			 $orderitemlist = $dom->createElement('OrderItemList', '');
			 $orderitemlist = $order->appendChild($orderitemlist);
			 
			 for ($i = 1, $m = 0; $i <= count($ItemName); $i++) {
			 
				  if (!preg_match("/^[0-9]{0,6}$/", $Quantity[$i])) {
					  $errors[] = $this->_t('Error', "Invalid Quantity.");
				  }
				  
				  if ($UnitPrice[$i] == 'Other') {
					  $UnitPrice[$i] = $Other[$i];
				  }
				  
				  if ($Quantity[$i] != '' && $Quantity[$i] != 0 && $UnitPrice[$i] !='') {
					  $orderitem = $dom->createElement('OrderItem', '');
					  $orderitem = $orderitemlist->appendChild($orderitem);
				
					  if ($ItemID[$ItemName[$i]] != '')	{
						  $itemid = $dom->createElement('ItemID', substr($ItemID[$i], 0, 25));
						  $itemid = $orderitem->appendChild($itemid);
					  }
					  
					 /* $itemname = $dom->createElement('ItemName', substr($ItemName[$i], 0, 25));
					  $itemname = $orderitem->appendChild($itemname);*/

					  $itemname  = $dom->createElement('ItemName','');
					  $itemname  = $orderitem->appendChild($itemname);
					  $itemname->appendChild($dom->createCDATASection(substr($ItemName[$i], 0, 25)));
					
					  $quntity = $dom->createElement('Quantity', $Quantity[$i]);
					  $quntity = $orderitem->appendChild($quntity);
					
					  if (($RecurringMethod != 'Installment' &&  ($No_Of_Recurring >= 1 || $No_Of_Recurring == '')) ||
						  ($RecurringMethod == 'Installment' && ($No_Of_Recurring == '' || $No_Of_Recurring == 0)))	{
					
						  $unitprice = $dom->createElement('UnitPrice', $this->floor_dec($UnitPrice[$i],2,".")*100);
						  $unitprice = $orderitem->appendChild($unitprice);
						  
						  if ($UnitDeductible[$i] !='')	{
							  $unit_deduct = $dom->createElement('UnitDeductible', $this->floor_dec(($UnitDeductible[$i]*$UnitPrice[$i]/100),2,".")*100);
							  $unit_deduct = $orderitem->appendChild($unit_deduct);
						  }
						  
						  if ($UnitTax[$i] !='') {
							  $unit_tax = $dom->createElement('UnitTax', $this->floor_dec(($UnitTax[$i]*$UnitPrice[$i]/100),2,".")*100);
							  $unit_tax = $orderitem->appendChild($unit_tax);
						  }
						  
						  if ($UnitDiscount[$i] !='') {
							  $unit_disc = $dom->createElement('UnitDiscount', $this->floor_dec(($UnitDiscount[$i]*$UnitPrice[$i]/100), 2, ".")*100);
							  $unit_disc = $orderitem->appendChild($unit_disc);
						  }
						  
						  if ($SKU[$i] != '') {
							 /* $sku_code = $dom->createElement('SKU', substr($SKU[$i], 0, 25));
							  $sku_code = $orderitem->appendChild($sku_code);*/

							  $sku_code  = $dom->createElement('SKU','');
					 		  $sku_code  = $orderitem->appendChild($sku_code);
					 		  $sku_code->appendChild($dom->createCDATASection(substr($SKU[$i], 0, 25)));
						  }
						  
						  $total_price += ($this->floor_dec($UnitPrice[$i], 2, ".")*$Quantity[$i]*100);
						  $total_unitdetatble += $this->floor_dec(($UnitDeductible[$i]*$UnitPrice[$i])/100, 2, ".")*$Quantity[$i]*100;
						  $total_unittax +=($UnitTax[$i]*$Quantity[$i]*$UnitPrice[$i]/100)*100;
						  $total_unitdiscount +=($this->floor_dec($UnitDiscount[$i]*$UnitPrice[$i], 2, ".")*$Quantity[$i]/100)*100;
						
					} else {
					
						 $unitprice = $dom->createElement('UnitPrice', $this->floor_dec($UnitPrice[$i]/$No_Of_Recurring, 2, ".")*100);
						 $unitprice = $orderitem->appendChild($unitprice);
						 
						 if ($UnitDeductible[$i] !='') {
							 $unit_deduct = $dom->createElement('UnitDeductible', $this->floor_dec(((($UnitDeductible[$i]*$UnitPrice[$i])/$No_Of_Recurring)/100), 2, ".")*100);
							 $unit_deduct = $orderitem->appendChild($unit_deduct);
						 }
						 
						 if ($UnitTax[$i] !='')	{
							 $unit_tax = $dom->createElement('UnitTax', $this->floor_dec(($UnitTax[$i]/$No_Of_Recurring*($UnitPrice[$i]/$No_Of_Recurring)/100), 2, ".")*100);
							 $unit_tax = $orderitem->appendChild($unit_tax);
						 }
						 
						 if ($UnitDiscount[$i] !='') {
							 $unit_disc = $dom->createElement('UnitDiscount', $this->floor_dec(($UnitDiscount[$i]/$No_Of_Recurring*($UnitPrice[$i]/$No_Of_Recurring)/100), 2, ".")*100);
							 $unit_disc = $orderitem->appendChild($unit_disc);
						 }
						 
						if ($SKU[$i] != '')	{
					

							$sku_code  = $dom->createElement('SKU','');
							$sku_code  = $orderitem->appendChild($sku_code);
							$sku_code->appendChild($dom->createCDATASection(substr($SKU[$i], 0, 25)));
						}
						$total_price += ($this->floor_dec(($UnitPrice[$i]/$No_Of_Recurring), 2, ".")*$Quantity[$i]*100);
						$total_unitdetatble += $this->floor_dec(($UnitDeductible[$i]/$No_Of_Recurring*$UnitPrice[$i]/$No_Of_Recurring)/100, 2, ".")*$Quantity[$i]*100;
						$total_unittax +=($UnitTax[$i]/$No_Of_Recurring*$Quantity[$i]*($UnitPrice[$i]/$No_Of_Recurring)/100)*100;
						$total_unitdiscount +=($this->floor_dec($UnitDiscount[$i]/$No_Of_Recurring*($UnitPrice[$i]/$No_Of_Recurring), 2, ".")*$Quantity[$i]/100)*100;
					 }
					$m++;
				 }
			}
			
			if ($m == 0) {
				$error[] = $this->_t('Error', "Basket is empty.");
			}
			
			if (($this->_isset($this->_getpostdata('ShippingMethod')) && $this->_getpostdata('ShippingMethod') != '') || $ShippingAddress1) {
	
				 $shipping = $dom->createElement('Shipping', '');
				 $shipping = $order->appendChild($shipping);
				 
				 if ($this->_getpostdata('ShippingMethod') == '') {
					 $ShippingMethod = 'Shipping';
				 } else {
					 $ShippingMethod = $this->_getpostdata('ShippingMethod');	
				 }
				 
				 $shipping_method = $dom->createElement('ShippingMethod', $ShippingMethod);
				 $shipping_method = $shipping->appendChild($shipping_method);
				 
				 if ($RecurringMethod != 'Installment')	{
				 
					 $shipping_value = $dom->createElement('ShippingValue', $_POST[$val]['ShippingValue']*100);
					 $shipping_value = $shipping->appendChild($shipping_value);
				
					 $shipping_tax = $dom->createElement('ShippingTax', $this->floor_dec(($_POST[$val]['ShippingTax']*$_POST[$val]['ShippingValue']/100), 2, ".")*100);
					 $shipping_tax = $shipping->appendChild($shipping_tax);
				 
					$ShippingValue = $this->floor_dec($_POST[$val]['ShippingValue'], 2, ".")*100;
					$ShippingTax   = $this->floor_dec(($_POST[$val]['ShippingTax']*$_POST[$val]['ShippingValue']/100), 2, ".")*100;
					
				 } else {
				 	if ($_POST[$val]['ShippingValue'] > 0) {
						$shipping_value = $dom->createElement('ShippingValue', $this->floor_dec($_POST[$val]['ShippingValue']/$No_Of_Recurring, 2, ".")*100);
						$shipping_value = $shipping->appendChild($shipping_value);
						$ShippingValue = $this->floor_dec($_POST[$val]['ShippingValue']/$No_Of_Recurring, 2, ".")*100;
					}
					if ($_POST[$val]['ShippingTax'] > 0) {
						$shipping_tax = $dom->createElement('ShippingTax', $this->floor_dec((($_POST[$val]['ShippingTax']/$No_Of_Recurring*$_POST[$val]['ShippingValue']/$No_Of_Recurring)/100), 2, ".")*100);
						$shipping_tax = $shipping->appendChild($shipping_tax);
						$ShippingTax   = $this->floor_dec((($_POST[$val]['ShippingTax']/$No_Of_Recurring*$_POST[$val]['ShippingValue']/$No_Of_Recurring)/100), 2, ".")*100;
					}
				 
			   }			 
			} else {
			
				$ShippingValue = '000';
				$ShippingTax = '000';
				
			}
			
			if ($this->_getpostdata('SendReceipt') == 'true' || $this->_isset($wid) || $this->_isset($this->_getpostdata('Terms_Condition')) || $this->_isset($this->_getpostdata('OrganizationInformation')) || $this->_isset($this->_getpostdata('ThankYouMessage'))) {
			
				 $receipt = $dom->createElement('Receipt', '');
				 $receipt = $order->appendChild($receipt);
				
				 if ($wid != '') {
					 $recipt_wid = $dom->createElement('WID', $wid);
					 $recipt_wid = $receipt->appendChild($recipt_wid);
				 }
				 
				 $recipt_lang = $dom->createElement('Language', 'ENG');
				 $recipt_lang = $receipt->appendChild($recipt_lang);
				
				 if ($this->_isset($this->_getpostdata('OrganizationInformation'))) {
					

					 $recipt_org  = $dom->createElement('OrganizationInformation','');
					 $recipt_org  = $receipt->appendChild($recipt_org);
					 $recipt_org->appendChild($dom->createCDATASection($OrganizationInformation));
				 }
				 
				 if ($this->_isset($this->_getpostdata('ThankYouMessage'))) {
				
					 $recipt_thanks  = $dom->createElement('ThankYouMessage','');
					 $recipt_thanks  = $receipt->appendChild($recipt_thanks);
					 $recipt_thanks->appendChild($dom->createCDATASection($this->_addslashesdata('ThankYouMessage')));
				 }
	
				 if ($this->_isset($this->_getpostdata('Terms_Condition'))) {
				

					 $recipt_terms  = $dom->createElement('TermsCondition','');
					 $recipt_terms  = $receipt->appendChild($recipt_terms);
					 $recipt_terms->appendChild($dom->createCDATASection($this->_addslashesdata('Terms_Condition')));
				 }
				 
				 $recipt_deduct = $dom->createElement('Deductible', '1');
				 $recipt_deduct = $receipt->appendChild($recipt_deduct);
				 
				 if ($this->_isset($this->_getpostdata('BillingEmail'))) {
					$recipt_email = $dom->createElement('EmailNotificationList', '');
					$recipt_email = $receipt->appendChild($recipt_email);
			
					$email_note = $dom->createElement('NotificationEmail', $this->_getpostdata('BillingEmail'));
					$email_note = $recipt_email->appendChild($email_note);
				 }
				
			}
			
			$transation = $dom->createElement('Transaction', '');
			$transation = $order->appendChild($transation);
			
			$trans_type = $dom->createElement('TransactionType', 'Payment');
			$trans_type = $transation->appendChild($trans_type);
			
		
			

			if ($this->_isset($RecurringMethod) && ($No_Of_Recurring != '' && $No_Of_Recurring != 1)) {
			
				$trans_recurr = $dom->createElement('Recurring', '');
				$trans_recurr = $transation->appendChild($trans_recurr);
				
				$total_installment = $dom->createElement('Installment', $No_Of_Recurring);
				$total_installment = $trans_recurr->appendChild($total_installment);
				
				$total_periodicity = $dom->createElement('Periodicity', $this->_getpostdata('Periodicity'));
				$total_periodicity = $trans_recurr->appendChild($total_periodicity);
				
				$total_installment = $dom->createElement('RecurringMethod', $RecurringMethod);
				$total_installment = $trans_recurr->appendChild($total_installment);
				
			}
			
			$trans_totals = $dom->createElement('CurrentTotals', '');
			$trans_totals = $transation->appendChild($trans_totals);
			
			$total_discount = $dom->createElement('TotalDiscount', $total_unitdiscount);
			$total_discount = $trans_totals->appendChild($total_discount);
			
			$total_tax = $dom->createElement('TotalTax', $total_unittax+$ShippingTax);
			$total_tax = $trans_totals->appendChild($total_tax);
			
			$total_ship = $dom->createElement('TotalShipping', $ShippingValue);
			$total_ship = $trans_totals->appendChild($total_ship);
			
			$total_deduct = $dom->createElement('TotalDeductible', $total_unitdetatble);
			$total_deduct = $trans_totals->appendChild($total_deduct);
			
			$total_amount = $dom->createElement('Total', (($total_price+$total_unittax+$ShippingValue+$ShippingTax)-$total_unitdiscount));
			$total_amount = $trans_totals->appendChild($total_amount);

			$charge_dateorg = $this->_getpostdata('chargeYear')."/".$this->_getpostdata('chargeMonth')."/".$this->_getpostdata('chargeDay');
			$charge_date = sprintf("%02d/%02d/%02d", $this->_getpostdata('chargeYear'), $this->_getpostdata('chargeMonth'), $this->_getpostdata('chargeDay'));
		if($charge_date != "00/00/00"){
			$trans_chargedate = $dom->createElement('ChargeDate', $charge_date);
			$trans_chargedate = $transation->appendChild($trans_chargedate);
		}
			$strParam = $dom->saveXML();
			$connect = array('soap_version' => SOAP_1_1, 'trace' => 1, 'exceptions' => 0);		
		//print_r($strParam);die();
		  if (count($errors) > 0) {
			  $html_element = '<div id="error"><ul class="error_ul">';
			  foreach ($errors as $message) {
					   $html_element .= "<li class='error_li'>".$message."</li>";
			  }
			  $html_element .= '</ul></div>';
			  echo $html_element;
		  } else {
	
				try {
				   
				  $client = new SoapClient('https://paas.cloud.clickandpledge.com/paymentservice.svc?wsdl', $connect);
				  $params = array('instruction'=>$strParam);
				  $response = $client->Operation($params);
				  $response_value = $response->OperationResult->ResultData;
				  $result_array = $response->OperationResult->ResultCode;
				  $TransactionNumber = $response->OperationResult->TransactionNumber;
				  $VaultGUID = $response->OperationResult->VaultGUID;
				  $AdditionalInfo = $response->OperationResult->AdditionalInfo;
                   
				  if ($result_array == '00') {
				  
					  $sucess[] = "$response_value";
					  $sucess[] = "Transaction Number : $TransactionNumber";
					  if ($PaymentType == 'Check' || $PaymentType == 'CreditCard') {
						  $sucess[] = "VaultGUID : $VaultGUID";
					  }
					  unset($_POST);
					  
					  $html_element = '<div id="sucess"><ul class="sucess_ul">';
					  foreach ($sucess as $message) {
							   $html_element .= "<li class='sucess_li'>".$message."</li>";
					  }
					  $html_element .= '</ul></div>';
					  echo $html_element;
				  
				  } else {
					 /* commented by ram on 08/06/2021
					  $status_server = array(
					       2055 => 'AccountGuid is not valid',
						   2056 => 'AccountId is not valid',
						   2057 => 'Username is not valid',
						   2058 => 'Password is not valid',
						   2059 => 'Invalid recurring parameters',
						   2060 => 'Account is disabled',
						   2101 => 'Cardholder information is null',
						   2102 => 'Cardholder information is null',
						   2103 => 'Cardholder information is null',
						   2104 => 'Invalid billing country',
						   2105 => 'Credit Card number is not valid',
						   2106 => 'Cvv2 is blank',
						   2107 => 'Cvv2 length error',
						   2108 => 'Invalid currency code',
						   2109 => 'CreditCard object is null',
						   2110 => 'Invalid card type ',
						   2111 => 'Card type not currently accepted',
						   2112 => 'Card type not currently accepted',
						   2210 => 'Order item list is empty',
						   2212 => 'CurrentTotals is null',
						   2213 => 'CurrentTotals is invalid',
						   2214 => 'TicketList lenght is not equal to quantity',
						   2215 => 'NameBadge lenght is not equal to quantity',
						   2216 => 'Invalid textonticketbody',
						   2217 => 'Invalid textonticketsidebar',
						   2218 => 'Invalid NameBadgeFooter',
						   2304 => 'Shipping CountryCode is invalid',
						   2401 => 'IP address is null',
						   2402 => 'Invalid operation',
						   2501 => 'WID is invalid',
						   2502 => 'Production transaction is not allowed. Contact support for activation.',
						   2601 => 'Invalid character in a Base-64 string',
						   2701 => 'ReferenceTransaction Information Cannot be NULL',
						   2702 => 'Invalid Refrence Transaction Information',
						   2703 => 'Expired credit card',
						   2805 => 'eCheck Account number is invalid',
						   2807 => 'Invalid payment method',
						   2809 => 'Invalid payment method',
						   2811 => 'eCheck payment type is currently not accepted',
						   2812 => 'Invalid check number',
						   5001 => 'Declined');			  
					  
					  if (array_key_exists($result_array,$status_server)) {
						  $additional_error = $status_server[$result_array];
					  } else {
						  $additional_error = "Unknown Error.Please Contact Admin".$result_array;
					  }
					  */
					  $errors[] = $response->OperationResult->ResultData;
					 
					  $html_element = '<div id="error"><ul class="error_ul">';
					  foreach ($errors as $message) {
							   $html_element .= "<li class='error_li'>".$message."</li>";
					  }
					  $html_element .= '</ul></div>';
					  echo $html_element;
						  
				  }
				
				} catch (Exception $e) {
				
				   $errors[] = $e->getMessage();
				   $html_element = '<div id="error"><ul class="error_ul">';
				   foreach ($errors as $message) {
						   $html_element .= "<li class='error_li'>".$message."</li>";
				   }
				   $html_element .= '</ul></div>';
				   echo $html_element;
				 
				}
			}	   
		  } // else closing
		 
		 } // submit if closing
	   } // function if closing
	
	public function floor_dec($number, $precision, $separator)
		{
			$numberpart=explode($separator, $number);
			$ceil_number= array($numberpart[0], substr($numberpart[1], 0, 2));
			return implode($separator, $ceil_number);
		}
	
	}
	
//forms html elemts

	if (isset($_GET['action']) && $_GET['action'] == 'nonusa' && $_GET['type'] != '') {
		$type = $_GET['type'];
		if($type == 'billing'){ $name = 'BillingStateProvince';}
		else{$name = 'ShippingStateProvince'; }
		echo $html_element ='<input type="text" name="'.$name.'" id="'.$name.'" value="" maxlength="50"/>'; 
	} 
	
	if (isset($_GET['action']) && $_GET['action'] == 'usa' && $_GET['type'] != '') { echo "in";
		$type = $_GET['type'];
		if($type == 'billing'){ $name = 'BillingStateProvince';}
		else{$name = 'ShippingStateProvince'; }
		$an = new PaaS_PHP();
		echo $an->Set_State_Province($type); 
	} 
	
	if (isset($_GET['action']) && $_GET['action'] == 'shippingmodule') {
		$an = new PaaS_PHP();
		 $html_element ='<table width="100%" id="shippingclass">
				<tbody>
				<td><strong>Shipping Information</strong></td>
				</tr>';
				if ($_GET['sfname'] == 'yes') {
		$html_element.='<tr>
				<td>Shipping First Name:</td>
				<td>'.$an->Set_Shipping_First_Name().'</td>
				</tr>';
				}
				if ($_GET['mname'] == 'yes') {
		$html_element.='<tr>
				<td>Shipping Middle initial:</td>
				<td>'.$an->Set_Shipping_Middle_Initial().'</td>
				</tr>';
				}
				if ($_GET['lnaem'] == 'yes') {
		$html_element.='<tr>
				<td>Shipping Last Name:</td>
				<td>'.$an->Set_Shipping_Last_name().'</td>
				</tr>';
				}
				if ($_GET['semail'] == 'yes') {
		$html_element.='<tr>
				<td>Shipping Email:</td>
				<td>'.$an->Set_Shipping_Email().'</td>
				</tr>';
				}
				if ($_GET['sphone'] == 'yes') {
		$html_element.='<tr>
				<td>Shipping Phone:</td>
				<td>'.$an->Set_Shipping_Phone().'</td>
				</tr>';
				}
		$html_element.='<tr>
				<td>Shipping Address 1:</td>
				<td>'.$an->Set_Shipping_Address1().'</td>
				</tr>
				<tr>
				<td>Shipping City:</td>
				<td>'.$an->Set_Shipping_City().'</td>
				</tr>
				<tr>
				<td>Shipping Country:</td>
				<td>'.$an->Set_Country_Code('shipping').'</td>
				</tr>
				<tr>
				<td>Shipping State:</td>
				<td>'.$an->Set_State_Province('shipping').'</td>
				</tr>
				<tr>
				<td>Zip code:</td>
				<td>'.$an->Set_Shipping_Postal_Code().'</td>
				</tr>
				</tbody></table>';
		echo $html_element;
	}

?>