<?php require_once('Connections/conn_newland.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="registration_failed.php";
  $loginUsername = $_POST['username'];
  $LoginRS__query = sprintf("SELECT username FROM tbl_users WHERE username=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_conn_newland, $conn_newland);
  $LoginRS=mysql_query($LoginRS__query, $conn_newland) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frm_register")) {
  $insertSQL = sprintf("INSERT INTO tbl_users (firstName, lastName, username, pwd, userGroup, address1, city, state_province, zip_postal, country) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['firstName'], "text"),
                       GetSQLValueString($_POST['lastName'], "text"),
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['pwd'], "text"),
                       GetSQLValueString($_POST['userGroup'], "text"),
                       GetSQLValueString($_POST['address1'], "text"),
                       GetSQLValueString($_POST['city'], "text"),
                       GetSQLValueString($_POST['state_province'], "text"),
                       GetSQLValueString($_POST['zip_postal'], "text"),
                       GetSQLValueString($_POST['country'], "text"));

  mysql_select_db($database_conn_newland, $conn_newland);
  $Result1 = mysql_query($insertSQL, $conn_newland) or die(mysql_error());

  $insertGoTo = "login.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?><!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Newland Tours: Rejestracja</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
//-->
</script>
</head>

<body class="oneColFixCtrHdr">
<a href="#top"><img src="images/spacer.gif" alt="Przejdź do głównej treści strony" border="0" /></a>
<div id="container">
  <div id="header">
    <img src="images/banner_left.gif" width="451" height="68" alt="Banner Newland Tours, strona lewa." /><img src="images/banner_right.jpg" width="276" height="68" id="bannerRight" alt="Banner Newland Tours, strona prawa" />
  <!-- end #header --></div>
  <div id="navigation">
  	<ul>
        <li><a href="index.php">Główna</a></li>
        <li><span class=""><span class=""><a href="about.cfm">O firmie</a></span></span></li>
<li><a href="tours.php">Znajdź wyjazd</a></li>
        <li><a href="profiles.php">Opisy krajów</a></li>
        <li><a href="contact.php">Kontakt z nami</a></li>
    </ul>
    </div>
  <div id="mainContent">
    <h1><a name="top" id="top"></a>Zarejestruj  się, aby korzystać z naszej witryny</h1>
    <p>Wymagane jest wypełnienie wszystkich pól.</p>
    <form id="frm_register" name="frm_register" method="POST" action="<?php echo $editFormAction; ?>">
      <table width="95%" border="0" cellspacing="0" cellpadding="3">
        <tr>
          <td>Imię</td>
          <td><input type="text" name="firstName" id="firstName" /></td>
        </tr>
        <tr>
          <td>Nazwisko</td>
          <td><input type="text" name="lastName" id="lastName" /></td>
        </tr>
        <tr>
          <td>Adres e-mail</td>
          <td><input name="username" type="text" id="username" size="45" /></td>
        </tr>
        <tr>
          <td>Hasło</td>
          <td><input type="password" name="pwd" id="pwd" /></td>
        </tr>
        <tr>
          <td>Adres</td>
          <td><input name="address1" type="text" id="address1" size="60" /></td>
        </tr>
        <tr>
          <td>Miasto</td>
          <td><input name="city" type="text" id="city" size="45" /></td>
        </tr>
        <tr>
          <td>Województwo</td>
          <td><input type="text" name="state_province" id="state_province" /></td>
        </tr>
        <tr>
          <td>Kod pocztowy</td>
          <td><input type="text" name="zip_postal" id="zip_postal" /></td>
        </tr>
        <tr>
          <td>Państwo</td>
          <td><input type="text" name="country" id="country" /></td>
        </tr>
        <tr>
          <td><input name="userGroup" type="hidden" id="userGroup" value="gość" /></td>
          <td><input name="button" type="submit" id="button" onclick="MM_validateForm('firstName','','R','lastName','','R','username','','RisEmail','pwd','','R','address1','','R','city','','R','state_province','','R','zip_postal','','R','country','','R');return document.MM_returnValue" value="Wyślij" /></td>
        </tr>
      </table>
      <input type="hidden" name="MM_insert" value="frm_register" />
    </form>
    <br clear="all" />
  <!-- end #mainContent --></div>
  <div id="footer">
    <p>&copy; 2007 Newland Tours</p>
  <!-- end #footer --></div>
<!-- end #container --></div>

</body>
</html>
