<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- coded and designed by erwin warps. DO not remove -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php
include ("./title-include.php");
?>

<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<?php Require "initialise.php"; ?>

<?php
	// one define to use as part of security mesure
	define('VoipOrbit', 1);
	// lets load our settings
	require("config.php");
	// lets load our functions
	require("functions.php");
	//define variables
	$Variables = Array	( 	"send" => 		array("Name"=> "send",		"DisplayName"=> "Send",		"type" => "string","sql" => False,"length" => 5,"Required"=> True,"InputType" => "hidden", "Value" => "True"),
							"login" => 		array("Name"=> "login",		"DisplayName"=> "User Name","type" => "string","sql" => True,"length" => 20,"Required"=> True,"InputType" => "text"),
							"passwd" => 	array("Name"=> "passwd",	"DisplayName"=> "Password",	"type" => "string","sql" => True,"length" => 40,"Required"=> True,"InputType" => "password"),
							"passwd2" => 	array("Name"=> "passwd2",	"DisplayName"=> "Confirm Password","type" => "string","sql" => False,"length" => 40,"Required"=> True,"InputType" => "password"),
							"language" => 	array("Name"=> "language",	"DisplayName"=> "Language",	"type" => "string","sql" => True,"length" => 5,"Required"=> True,"InputType" => "select", "Query" => "SELECT Language, Short FROM `$A2BDB`.`Languages`;", "SelectName" => "Language", "SeletValue" => "Short", "Value" => "en"),
							"company" => 	array("Name"=> "company",	"DisplayName"=> "Company",	"type" => "string","sql" => True,"length" => 50,"Required"=> True,"InputType" => "text"),
							"lastname" => 	array("Name"=> "lastname",	"DisplayName"=> "LastName",	"type" => "string","sql" => True,"length" => 50,"Required"=> True,"InputType" => "text"),
							"firstname" => 	array("Name"=> "firstname",	"DisplayName"=> "FirstName","type" => "string","sql" => True,"length" => 50,"Required"=> True,"InputType" => "text"),
							"address" => 	array("Name"=> "address",	"DisplayName"=> "Address",	"type" => "string","sql" => True,"length" => 100,"Required"=> True,"InputType" => "text"),
							"city" => 		array("Name"=> "city",		"DisplayName"=> "City",		"type" => "string","sql" => True,"length" => 40,"Required"=> True,"InputType" => "text"),
							"state" => 		array("Name"=> "state",		"DisplayName"=> "State",	"type" => "string","sql" => True,"length" => 40,"Required"=> True,"InputType" => "text"),
							"country" => 	array("Name"=> "country",	"DisplayName"=> "country",	"type" => "string","sql" => True,"length" => 40, "Required"=> True,"InputType" => "select", "Query" => "SELECT countryname FROM `$A2BDB`.`cc_country`;", "SelectName" => "countryname", "SeletValue" => "countryname", "Value" => "Select one"),
							"zipcode" => 	array("Name"=> "zipcode",	"DisplayName"=> "Zipcode",	"type" => "string","sql" => True,"length" => 20,"Required"=> True,"InputType" => "text"),
							"email" => 		array("Name"=> "email",		"DisplayName"=> "Email",	"type" => "string","sql" => True,"length" => 70,"Required"=> True,"InputType" => "text"),
							"phone" => 		array("Name"=> "phone",		"DisplayName"=> "Phone",	"type" => "string","sql" => True,"length" => 20,"Required"=> True,"InputType" => "text"),
							"fax" => 		array("Name"=> "fax",		"DisplayName"=> "Fax",		"type" => "string","sql" => True,"length" => 20,"Required"=> True,"InputType" => "text"),
							"bank_info" => 	array("Name"=> "bank_info",	"DisplayName"=> "BankInfo",	"type" => "string","sql" => True,"length" => 1024,"Required"=> True,"InputType" => "textarea"),
							"currency" => 	array("Name"=> "currency",	"DisplayName"=> "Currency",	"type" => "string","sql" => True,"length" => 3,"Required"=> True,"InputType" => "select", "Query" => "SELECT name, currency FROM `$A2BDB`.`cc_currencies`;", "SelectName" => "name", "SeletValue" => "currency", "Value" => "USD")
							
						);
	// porces post variables 
	GetPostVariables();
	$DBinfo = Array ("Server"=> $MYSQL_Server,"User"=> $MYSQL_User,"Password"=> $MYSQL_Password,"DBName"=> $MYSQL_DBName);
?>

<body>
	<div class="container">
		<?php include "header.php"; ?>
		<div class="content">
		<H1>Become a Reseller</H1><br />
		<?php
				$Succes = False;
				$Sening_Email = False;
				// we need to check that the passwords are the same and that the username is not yet in use
				// lets check the passwords first
				If ( $passwd != $passwd2)
				{
					$Errors["passwd"] = "passwd";
					$Variables["passwd"]["Error"] = True;
					$Variables["passwd"]["ErrorMessage"] = "passwords do not match";
					$Errors["passwd2"] = "passwd2";
					$Variables["passwd2"]["Error"] = True;
				}
				// lets make sure the password is alpha numeric
				If (!ctype_alnum($passwd) AND ($send == "True"))
				{
					$Errors["passwd"] = "passwd";
					$Variables["passwd"]["Error"] = True;
					$Variables["passwd"]["ErrorMessage"] = "passwords can only consist of letters and or numbers <br>&nbsp;&nbsp;&nbsp;a to z or A to Z or 0 to 9";
					$Errors["passwd2"] = "passwd2";
					$Variables["passwd2"]["Error"] = True;
				}
				// is the username in use or has it been requested before
				$query = "SELECT login FROM `$agent_requests_DB`.`UserNames` WHERE login LIKE '$login';";
				$ResultArray = array();
				$ResultArrayIndex = 0;
				$result = QuerySQL($DBinfo["Server"], $DBinfo["User"], $DBinfo["Password"], $DBinfo["DBName"], $query);
				If ( mysql_num_rows($result) > 0 )
				{	//the same username has been found at least once
					$Errors["login"] = "login";
					$Variables["login"]["Error"] = True;
					$Variables["login"]["ErrorMessage"] = 'the User Name "'.$login.'"is already in use';
				}
				If ($PostRecived) // did we recive post data
				{
					If ( Count($Errors) > 0)
					{	// dipsplay the difrent error messages
						If ($Errors["EmptyField"])
						{
							Echo '<br><font color="#FF0000"><b>Required fields are missing</b></font><br>';
						}
						foreach($Errors as $Error)
						{
							If (!empty($Variables[$Error]["ErrorMessage"]))
							Echo '<font color="#FF0000"><b>'.$Variables[$Error]["ErrorMessage"].'</b></font><br>';
						}
						Echo "<br>";
					}
					Else
					{
						// putting all the variables in an array for easy escaping
						$EscapeArray = array();
						foreach($Variables as $Variable)
						{
							IF ($Variable["sql"])
							{
								$EscapeArray[$Variable["Name"]] = $$Variable["Name"];
							}
						}
						//Escape the variable before putting them in our query
						$EscapedArray = StrEscape($MYSQL_Server, $MYSQL_User, $MYSQL_Password, $contact_requests_DB, $EscapeArray);
						// contruct the query
						$Query = "INSERT INTO `$agent_requests_DB`.`$agent_requests_table` (";
						$first = True;
						foreach($Variables as $Variable)
						{
							IF ($Variable["sql"])
							{
								If ($first)
								{
									$Query .= "`".$Variable["Name"]."`";
									$first = False;
								}
								Else
								{
									$Query .= ", `".$Variable["Name"]."`";
								}
							}
						}
						$Query .= ")VALUES (";
						$first = True;
						foreach($EscapedArray as $EscapedValue)
						{
							If ($first)
							{
								$Query .= "'".$EscapedValue."'";
								$first = False;
							}
							Else
							{
								$Query .= ", '".$EscapedValue."'";
							}
						}
						$Query .= ");";
						$result = QuerySQL($MYSQL_Server, $MYSQL_User, $MYSQL_Password, $contact_requests_DB, $Query);
						If ( !ResultErrorHandler($result) )
						{
							$Sening_Email = True;
							// in oder to send html formatter @mail
							$headers  = 'MIME-Version: 1.0' . "\r\n";
							$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
							// Additional headers
							$headers .= "To: VoipOrbit <$Contact_Email>\r\n";
							$headers .= "From: $name <$email>\r\n";
							$Message = "$lastname $firstname has requested to become an agent";
							$Email_Send = mail($Contact_Email, $Subject, $Message, $headers);
							If ($Email_Send)
							{
								$Succes = True;
							}
						}
					}
				}
				
			If(!$Succes)
			{//display form
			?>
			<?php If((!$Email_Send) and ($Sening_Email)) Echo "We are sorry an error occured while sending your message<br>";
				// insert the actual form
				require("formcreator.php");
			}
			Else
			{
			?>
				<font color="#ff6400"><b>Your Request was sent succesfully<br>Once accepted you will receive a notification @mail </b></font><br><br>
			<?php
			// insert the actual form
				require("formconfirm.php");
			}
			?>
		</div>
		<?php include "footer.php"; ?>
	</div>
</body>
</html>
