<?PHP

include("config.php");

if(!isset($_GET[fitter_id]) AND !$_POST AND !is_numeric($_GET[fitter_id])) {
    header('Location: '.$site_url.'/?error=no_id_or_no_post');
    exit(); 
}

if(!is_logged_in($user)){ 
    header('Location: '.$site_url.'/?error=not_logged_in_one');
    exit(); 
}

// Populate with post data or populate with edit return data
if (isset($_POST['edit_return']) || isset($_POST['edit_fitter'])) { 
    $row = $_POST;
} else {
    $fitter_idhere = intval(mysql_escape_string($_GET[fitter_id]));
    $results = mysql_query("SELECT * FROM ".$prefix."Fitters WHERE fitter_id='".$fitter_idhere."'");
    $row = mysql_fetch_array($results) OR die(mysql_error());
}


//Get user's session data information
$username = base64_decode($_SESSION['user']);
$useruid = base64_decode($_SESSION['user_id']);

if ($username != "Slowman" && $username != "Rappstar" && $username != "Herbert") { 
    if ( $row[fitter_valid] != 1 ) {
        header('Location: '.$site_url.'/?error=fitter_not_valid');
        exit();
    }
}

// set the page title
$pagetitle = "Edit A Bike Fitter";

// set meta tags
$meta_keywords = "edit a bike fitter";
$meta_description = "edit screen for bike fitter information";

?>

<?  

if($_POST[edit_fitter] == true){
  include("wiki_edit_X.php");
}

?>

<? include("include_common_head.php"); ?>
<body class="listings">
  <? include($common_path . "/ads/ad_wallpaper.html"); ?>

  <div class="container">
    <? include($common_path . "/templates/include_header.php"); ?>
    <div class="main">
      <div class="contentwrapper clearfix">
        <? include("include_breadcrumb.php"); ?>

        <section class="section listings section-has-widgets section-static remove-sidebar">
          <div class="sidebar-b">
            <? include("include_sidebar.php"); ?>
          </div>

          <div class="content content-has-widgets">
            <div class="grid">

              <div class="clearfix">

                <? if( $confirm_fitter == true ) { ?>
                    <h1>Edit a Fitter: Step 2 of 2</h1>
                    <? } else if( ($confirm_fitter != true) || ($_POST[edit_fitter] != true) ){ ?>
                    <h1>Edit a Fitter: Step 1 of 2</h1>
                    <hr class="line" />
                    <? include("../runshops/include_region_list.php"); ?>
                    <? if (!$errmsg) { ?>
                    <p>To edit a fitter, simply change the data you wish. If you make a mistake, just hit the reset button. <span style="color: red;">To save the changes as a NEW fitter (for instance, if you have multiple chapters), hit the "SAVE AS A NEW FITTER" button. To save changes to the existing fitter entry, hit the "SAVE CHANGES TO YOUR FITTER" button. You will have a chance to review the data before confirming.</span> <span style="color: red;">You will then be prompted to confirm your information before the information is sent to us.</p>
                    <? } else { ?>
                     <p class="cont"><span style="color: red;"><? echo $errmsg; ?></span></p>
                    <? } ?>
                <? } 

                // begin insert form
                if( ($_POST[edit_fitter] != true) || ($errmsg) || (($confirm_fitter != true) && ($_POST[insert_fitter] != true)) ) { ?> 
                <form enctype="multipart/form-data" action="wiki_edit.php" method="post">
                    <div class="dtable form">
                      <div class="drow">
                        <div class="dcell name">Store Name</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_name" value="<? echo stripslashes($row[fitter_name]); ?>" class="txt lngtext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Address</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_address" value="<? echo stripslashes(htmlspecialchars($row[fitter_address])); ?>" class="txt lngtext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Address 2</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_address_two" value="<? echo stripslashes(htmlspecialchars($row[fitter_address_two])); ?>" class="txt lngtext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">City</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_city" value="<? echo stripslashes(htmlspecialchars($row[fitter_city])); ?>" class="txt shotext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">State</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <select name="fitter_state">
                            <option value="0">Choose a State/Province</option>
                            <?
                            foreach($state_list as $key=>$value) {
                                if(strcmp($row[fitter_state], $value) == 0){
                                    $str = " selected=\"selected\"";
                                }
                                echo "<option value=\"$value\"$str>$value</option>";
                                $str = "";
                            }
                            ?>
                          </select>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Zip/Postal Code</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_zip" value="<? echo stripslashes(htmlspecialchars($row[fitter_zip])); ?>" class="txt shotext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Override Address With Lat/Lng</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="true" type="checkbox" name="fitter_latlng_override" <? if ($row[fitter_latlng_override] == true) { echo ("checked"); }?> > Check this if you know your physical address doesn't geocode properly using Google's API.<br />Then enter the desired lat/lng in the boxes below.
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Latitude</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="<? echo stripslashes(htmlspecialchars($row[fitter_lat])); ?>" type="text" name="fitter_lat" class="txt shotext"/>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Longitude</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="<? echo stripslashes(htmlspecialchars($row[fitter_lng])); ?>" type="text" name="fitter_lng" class="txt shotext"/>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Phone</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_phone" value="<? echo stripslashes(htmlspecialchars($row[fitter_phone])); ?>" class="txt shotext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Fax</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_fax" value="<? echo stripslashes(htmlspecialchars($row[fitter_fax])); ?>" class="txt shotext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">E-mail</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_email" value="<? echo stripslashes(htmlspecialchars($row[fitter_email])); ?>" class="txt lngtext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Website</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_website" value="<? echo stripslashes(htmlspecialchars($row[fitter_website])); ?>" class="txt lngtext" placeholder="http://" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Portfolio</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                          <input type="text" name="fitter_portfolio" value="<? echo stripslashes(htmlspecialchars($row[fitter_portfolio])); ?>" class="txt lngtext" placeholder="http://" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name"></div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        <? 
                        for ($i = 0; $i < 25; $i++) echo("*"); 
                        echo (" FIT INFO ");
                        for ($i = 0; $i < 25; $i++) echo("*"); 
                        ?>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Default Tri-Fit Methodology</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        <?
                        $fetch = mysql_query("SELECT * FROM ".$prefix."FittersMethods ORDER BY fitter_method_name ASC") OR die(mysql_error());
                        echo("<input type=\"hidden\" name=\"fitter_method\" value=\"0\" >");
                        while ($list = mysql_fetch_array($fetch)) {
                            echo("<span class=\"col2\"><input type=\"radio\" name=\"fitter_method\" value=\"$list[fitter_method_id]\" ");
                            if ($list['fitter_method_id'] == $row[fitter_method]){ echo "checked "; }
                            echo(">$list[fitter_method_name]</span>\n");
                        }
                        ?>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Certifications</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        <?
                        $explosion = array("");
                        if (!is_array($row[fitter_certifications])){ $explosion = explode(",", $row[fitter_certifications]); } else { $explosion = $row[fitter_certifications]; }

                        $fetch = mysql_query("SELECT * FROM ".$prefix."FittersCertifications ORDER BY fitter_certification_name ASC") OR die(mysql_error());

                        echo("<input type=\"hidden\" name=\"fitter_certifications[]\" value=\"0\" >");
                        while ($list = mysql_fetch_array($fetch)) {
                            echo("<span class=\"col\"><input type=\"checkbox\" name=\"fitter_certifications[]\" value=\"$list[fitter_certification_id]\" ");
                            if (in_array($list['fitter_certification_id'], $explosion)){ echo "checked "; }
                            echo(">$list[fitter_certification_name]</span>\n");
                        }
                        ?>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Fit Bikes</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        <?
                        $explosion = array("");
                        if (!is_array($row[fitter_fitbikes])){ $explosion = explode(",", $row[fitter_fitbikes]); } else { $explosion = $row[fitter_fitbikes]; }
                        $results = mysql_query("SELECT * FROM ".$prefix."FittersFitbikes ORDER BY fitter_fitbike_name ASC");
                        echo("<input type=\"hidden\" name=\"fitter_fitbikes[]\" value=\"0\" >");
                        while ($fetch = mysql_fetch_array($results)) {
                            echo("<span class=\"col\"><input type=\"checkbox\" name=\"fitter_fitbikes[]\" value=\"$fetch[fitter_fitbike_id]\" ");
                            if (in_array($fetch[fitter_fitbike_id], $explosion)){ echo "checked "; }
                            echo(">$fetch[fitter_fitbike_name]</span>\n");
                        }
                        ?>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Motion-Capture Systems</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        <?
                        $explosion = array("");
                        if (!is_array($row[fitter_motioncapture])){ $explosion = explode(",", $row[fitter_motioncapture]); } else { $explosion = $row[fitter_motioncapture]; }

                        $results = mysql_query("SELECT * FROM ".$prefix."FittersMotioncapture ORDER BY fitter_motioncapture_name ASC");

                        echo("<input type=\"hidden\" name=\"fitter_motioncapture[]\" value=\"0\" >");
                        while ($fetch = mysql_fetch_array($results)) {
                            echo("<span class=\"col\"><input type=\"checkbox\" name=\"fitter_motioncapture[]\" value=\"$fetch[fitter_motioncapture_id]\"  ");
                            if (in_array($fetch[fitter_motioncapture_id], $explosion)){ echo "checked "; }
                            echo(">$fetch[fitter_motioncapture_name]</span>\n");
                        }
                        ?>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Brand Friendly</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                        Brands where the fitter has a specific *verified* expertise in this brand.<br />  
                        <?
                        $explosion = array("");
                        if (!is_array($row[fitter_brandfriendly])){ $explosion = explode(",", $row[fitter_brandfriendly]); } else { $explosion = $row[fitter_brandfriendly]; }

                        $results = mysql_query("SELECT * FROM ".$prefix."FittersBrandFriendly ORDER BY fitter_brand_name ASC");

                        echo("<input type=\"hidden\" name=\"fitter_brandfriendly[]\" value=\"0\" >");
                        while ($fetch = mysql_fetch_array($results)) {
                            echo("<span class=\"col\"><input type=\"checkbox\" name=\"fitter_brandfriendly[]\" value=\"$fetch[fitter_brand_id]\"  ");
                            if (in_array($fetch[fitter_brand_id], $explosion)){ echo "checked "; }
                            echo(">$fetch[fitter_brand_name]</span>\n");
                        }
                        ?>
                      </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Cost of Fit</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="<? echo stripslashes(htmlspecialchars($row[fitter_cost])); ?>" type="text" name="fitter_cost" class="txt shotext" />
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">General Info</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
<textarea name="fitter_info" class="txt lngtext" placeholder="Please write some information about your fitting business.">
<? if($row[fitter_info]){ echo strip_tags(stripslashes($row[fitter_info])); } ?>
</textarea>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Directions</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
<textarea name="fitter_directions" class="txt lngtext" placeholder="Please describe how to get to your shop from your surrounding area.">
<? if($row[fitter_directions]) { echo strip_tags(stripslashes($row[fitter_directions])); } ?>
</textarea>
                        </div>
                      </div>
                      <div class="drow actions">
                        <div class="dcell name"></div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input type="submit" name="save_changes" value="Save Your Fitter Info" class="btn" />
                            <input type="submit" name="new_fitter" value="Save As A New Fitter" class="btn" />
                            <input type="reset" value="Reset" class="btn" />
                            <input type="hidden" name="edit_fitter" value="true" />
                            <input type="hidden" name="fitter_id" value="<? echo($row[fitter_id]); ?>" />
                            <? // need to temporary set the field as being valid ?>
                            <input type="hidden" name="fitter_valid" value="1" />
                            <input type="hidden" name="fitter_submitted_by" value="<? echo($row[fitter_submitted_by]); ?>" />
                            <input type="hidden" name="edited_by" value="<? echo($useruid); ?>" />
                        </div>
                      </div>
                    </div>
                </form>

                <? }
                if ($confirm_fitter == true){
                    include("include_edit_confirm.php");
                } ?>

              </div><!-- end col-2/3 -->

            </div><!-- end grid -->
          </div><!-- end content -->
        </section>


      </div><!-- end contentwrapper -->
    </div> <!-- end main -->

  <? include($common_path . "/templates/include_footer.php") ?>
  </div> <!-- container  -->
</body>
<? include($common_path . "/templates/include_global_js.php") ?>
</html>
