<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 8/25/2016	Ed Lemmers
//

	include ("include.php");
	
	$template_file = $_REQUEST['template_file'];
	$template_file = str_replace("%2E", ".", $template_file);
	$template_file = str_replace("%2e", ".", $template_file);
	$userid = $_REQUEST['userid'];
	$password = $_REQUEST['frongustat'];
	$email = $_REQUEST['email'];
	$first_name = $_REQUEST['first_name'];
	$last_name = $_REQUEST['last_name'];
	$lang_code = $_REQUEST['lang_code'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Running Breeze Website Builder for the First Time</title>
<link type="text/css" href="/manage/theme/classic/manager.css" rel="stylesheet" />
<link type="image/x-icon" href="/manage/images/favicon.ico" rel="shortcut icon" />

<style type="text/css">
	.requires_input	{
		border-style:solid;
		border-color:#FFFF00;
		border-width:medium;
	}
	p, h1	{
		margin-left:10px;
		margin-right:10px;
	}
</style>

<script>
	function GeneratePassword(ele_id)
	{
		var password = "";
		var password_length = rand(6,10);
		var i;
		var assci_segment;
		
		for (i = 0; i < password_length; i++)
		{
			assci_segment = rand(1,3);
			if (assci_segment == 1)
				password = password + String.fromCharCode(rand(48,57));	// numeric
			else if (assci_segment == 2)
				password = password + String.fromCharCode(rand(65,90));	// alpha upper case
			else
				password = password + String.fromCharCode(rand(97,122));	// alpha lower case
		}
		password = password.replace('I', 'i');
		password = password.replace('O', 'o');
		password = password.replace('l', 'L');
		
		var frm_obj = document.getElementById(ele_id);
			
		frm_obj.value = password;
			
		return password;
	}
	
	// Equivalent to PHP rand(min, max);
	function rand ( n, m )
	{
	  return ( n + Math.floor ( Math.random ( ) * (m - n + 1)) );
	}
	
	function GenerateSalts()
	{
		GeneratePassword('user_salt');
		GeneratePassword('public_salt');
	}
	
	function OpenHelpWindow()
	{
		window.open("http://breezewebsitebuilder.com/docs/cms/help.html#firsttime", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
	}
	
	function InitializeForm()
	{
		GenerateSalts();
		var code = "document.getElementById('submit').click();";
		setTimeout(code, 1000);
	}
</script>
</head>

<body onLoad="InitializeForm();">
<div align="center" id="login_logo"><img alt="Breeze Website Builder" src="/manage/theme/classic/images/Breeze_header_logo.png" /></div>
  <div id="header_divider_login"> </div>
<h1>Setting up the Breeze&#8482; Website Builder for the first time!</h1>
<p>Congratulations on choosing the Breeze&#8482; Website Builder!  Before you begin, you need to select a username and a password. You will also need to provide an email in case you forget your password.  Optionally, you may also provide your name and select a different laguage.  Required fields are shown as <span class="requires_input" style="padding-left:5px; padding-right:5px;">highlighted</span>. After you are done filling out the form, click "Continue" to create your account login.  Your template will be installed automatically and you will be able to beign editing your website immediately!</p>
	<form action="do_setup.php" method="post" name="frmUserData" id="frmUserData">
      <table align="center" width="90%" border="0">
        <tbody>
          <tr>
            <td><span class="sub_title">Username: </span></td>
            <td>
            <input type="text" id="userid" name="userid" <?php if (!isset($userid) || ($userid == "")) echo "class='requires_input'"; ?> value="<?php echo $userid; ?>" />
            </td>
            <td class="medium_message">All lower case, no spaces.</td>
          </tr>
          <tr>
            <td><span class="sub_title">Password: </span></td>
            <td>
            <input type="text" id="password" name="password" <?php if (!isset($password) || ($password == "")) echo "class='requires_input'"; ?> value="<?php echo $password; ?>" />
            <input type="button" name="generate_password" value="Generate Password" onClick="GeneratePassword('password');" <?php if (isset($password) && ($password != "")) echo "style='display:none;'"; ?> >
            </td>
            <td class="medium_message">Use a strong password of at least 8 characters.</td>
          </tr>
          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td><span class="sub_title">Email: </span></td>
            <td><input type="text" name="email" id="email" style="width:200px;" <?php if (!isset($email) || ($email == "")) echo "class='requires_input'"; ?> value="<?php echo $email; ?>" /></td>
            <td class="medium_message">Email for password reset.</td>
          </tr>
          <tr>
            <td><span class="sub_title">Name: </span></td>
            <td>
            	<input type="text" name="first_name" id="first_name" style="width:130px;" value="<?php echo $first_name; ?>" />
                <input type="text" name="last_name" id="last_name" value="<?php echo $last_name; ?>" />
            </td>
            <td class="medium_message">(first last)</td>
          </tr>
          
          <tr>
            <td><span class="sub_title">Language: </span></td>
            <td>
            	<select name="language" id="language">
<?php
	$languages = array();
	$path = "languages.txt";
	$fh = fopen($path, "r");
	$contents = fread($fh, filesize($path));
	fclose($fh);
	
	$rows = explode("\n", $contents);
	foreach($rows as $row)
	{
		$lang_pair = explode(";", $row);
		$lang_pair[0] = trim($lang_pair[0]);
		$lang_pair[1] = trim($lang_pair[1]);
		$languages[] = $lang_pair;
	}
	foreach($languages as $language)
	{
		if (($language[0] == "en") && (!isset($lang_code) || ($lang_code == "")))
			$selected = " selected='selected'";
		elseif (isset($lang_code) && ($lang_code != "") && ($language[0] == $lang_code))
			$selected = " selected='selected'";
		else
			$selected = "";
		echo "		<option value='" . $language[0] . "'" . $selected . ">" . $language[1] . "</option>\n";
	}
?>
                </select>
            </td>
            <td class="medium_message">Laguage used for management side.</td>
          </tr>
          
          
          <tr>
          	<td colspan="2" align="right">
           	  <input type="hidden" id="user_salt" name="user_salt" value="nthoeucg)(*,8970909NTSNT" />
              <input type="hidden" id="public_salt" name="public_salt" value="NTSNtoent09nt)(t[#nst" />
              <input type="hidden" id="template_file" name="template_file" value="<?php echo $template_file; ?>" />
            	<input type="submit" name="submit" id="submit" value="Continue" />
            </td>
            <td align="right">
            	<div id="help_button_sm" class="help_button" onClick="OpenHelpWindow();">&nbsp;</div>
            </td>
          </tr>
        </tbody>
      </table>
    </form>
</body>
</html>