<?PHP 

include("config.php");

if(!is_logged_in($user)){ 
  header('Location: '.$site_url);
  exit(); 
}

$useruid = base64_decode($_SESSION['user_id']);

// set the page title
$pagetitle = "Add a Coach";

// set meta tags
$meta_keywords = "add coach";
$meta_description = "Do you want to include a coach in our online triathlon coach database? Then use this form to send us your information, and we will add it to our website.";

?>

<? include("include_common_head.php"); ?>

<?  

if($_POST[add_coach] == true){
  include("add_X.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( $insert_success == true || $_GET[confirmed] == "yes" ) { ?>
                    <h1 class="content-title"><strong>Thank you</strong></h1>
                    <p class="cont">Thank you. Your coach information should be added within 24 hours. You will now be redirected back to the add page.</p>
                    <META HTTP-EQUIV=Refresh CONTENT="4; URL=add.php">
                    <? 
                        unset($insert_success);
                        unset($confirm_coach);
                    ?>
                <? } else if( $confirm_coach == true ) { ?>
                    <h1 class="content-title"><strong>Add a Coach: Step 2 of 2</strong></h1>
                <? }else if( ($confirm_coach != true) || ($_POST[add_coach] != true) ){ ?>
                    <h1 class="content-title"><strong>Add a Coach: Step 1 of 2</strong></h1>
                    <? if (!$errmsg) { ?>
                        <p class="cont">If you would like to add a coach to our website, simply fill out the form below. Be sure to fill out every field. After filling out each field, click submit. <span style="color: red;">You will then be prompted to confirm your information before the information is sent to us.</span></p>
                    <? } else { ?>
                        <p class="cont"><span style="color: red;"><? echo $errmsg; ?></span></p>
                    <? } ?>
                <? } 

                // begin insert form
                if( (($_POST[add_coach] != true) || ($errmsg) || (($confirm_coach != true) && ($_POST[insert_coach] != true))) && $_GET[confirmed] != "yes") { 

                ?>
                
                <form enctype="multipart/form-data" action="add.php" method="post">
                    <div class="dtable form">
                      <div class="drow">
                        <div class="dcell name">Name</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="<? echo stripslashes($_POST[name]); ?>" type="text" name="name" class="txt lngtext"  maxlength="80">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Street Address</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <input value="<? echo stripslashes(htmlspecialchars($_POST[address])); ?>" type="text" name="address" class="txt lngtext">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Street Address Two (optional)</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
	                       <input value="<? echo stripslashes(htmlspecialchars($_POST[address_two])); ?>" type="text" name="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 value="<? echo stripslashes(htmlspecialchars($_POST[city])); ?>" type="text" name="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="state">
		<OPTION value="0">Choose a State/Province/Country</OPTION>
		
	<? //use loop for dropdown box of states
	foreach($state_list as $key=>$value){
	   if(strcmp($_POST[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 value="<? echo stripslashes(htmlspecialchars($_POST[zip])); ?>" type="text" name="zip" class="txt shotext">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Phone</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
	<input value="<? echo stripslashes(htmlspecialchars($_POST[phone])); ?>" type="text" name="phone" class="txt shotext">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Fax / Alternate Phone</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
	<input value="<? echo stripslashes(htmlspecialchars($_POST[fax])); ?>" type="text" name="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 value="<? echo stripslashes(htmlspecialchars($_POST[email])); ?>" type="text" name="email" class="txt midtext">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Website</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
	<input value="<? echo $_POST[website]; ?>" type="text" name="website" class="txt lngtext" placeholder="http://">
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name"></div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
                            <h3>Info</h3>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Certifications</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <table cellpadding=0 cellspacing=0 width="100%"><?
	
	$explosion = array("");
	if (!is_array($_POST[certifications])){ $explosion = explode(",", $_POST[certifications]); } else { $explosion = $_POST[certifications]; }
	
	$fetch = mysql_query("SELECT * FROM ".$prefix."CoachesCertifications ORDER BY coach_certification_id ASC") OR die(mysql_error());
	
	$i = 0;
	$grouping = 1;
	echo("<tr>");
	echo("<input type=\"hidden\" name=\"certifications[]\" value=\"0\" >");
	while ($list = mysql_fetch_array($fetch)) {
		if(($grouping != $list[coach_certification_group]) OR !($i%3)) {
    		if (!($i%3)) echo ("<td width=\"33%\">&nbsp;</td>");
    		echo "</tr><tr>";
    		$grouping =  $list[coach_certification_group];
    		$i = 0;
   	 	}
		echo("<td width=\"33%\"><input type=\"checkbox\" name=\"certifications[]\" value=\"$list[coach_certification_id]\" ");
		if (in_array($list['coach_certification_id'], $explosion)){ echo "checked "; }
		echo(">$list[coach_certification_name]</td>\n");
		$i++;
	}
	while ($i<3) {
	    echo ("<td width=\"33%\">&nbsp;</td>\n");
		$i++;
	}
	echo("</tr>");
	
	?>
	</table>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Services</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <table cellpadding=0 cellspacing=0 width="100%"><?
	
	$explosion = array("");
	if (!is_array($_POST[services])){ $explosion = explode(",", $_POST[services]); } else { $explosion = $_POST[services]; }
	
	$results = mysql_query("SELECT * FROM ".$prefix."CoachesServices ORDER BY coach_service_id ASC");
	
	$i = 0;
	echo("<tr>");
	echo("<input type=\"hidden\" name=\"services[]\" value=\"0\" >");
	while ($row = mysql_fetch_array($results)) {
		if(!($i%1)) echo "</tr><tr>";
		echo("<td width=\"100%\"><input type=\"checkbox\" name=\"services[]\" value=\"$row[coach_service_id]\" ");
		if (in_array($row[coach_service_id], $explosion)){ echo "checked "; }
		echo(">$row[coach_service_name]</td>\n");
		$i++;
	}
	echo("</tr>");
	
	?>
	</table>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Education</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <table cellpadding=0 cellspacing=0 width="100%"><?
	
	$explosion = array("");
	if (!is_array($_POST[degrees])){ $explosion = explode(",", $_POST[degrees]); } else { $explosion = $_POST[degrees]; }
	
	$results = mysql_query("SELECT * FROM ".$prefix."CoachesDegrees ORDER BY coach_degree_id ASC");
	
	$i = 0;
	echo("<tr>");
	echo("<input type=\"hidden\" name=\"degrees[]\" value=\"0\" >");
	while ($row = mysql_fetch_array($results)) {
		if(!($i%2)) echo "</tr><tr>";
		echo("<td width=\"50%\"><input type=\"checkbox\" name=\"degrees[]\" value=\"$row[coach_degree_id]\"  ");
		if (in_array($row[coach_degree_id], $explosion)){ echo "checked "; }
		echo(">$row[coach_degree_name]</td>\n");
		$i++;
	}
	echo("</tr>");
	
	?>
	</table>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">General Info</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <textarea name="info" class="txt lngtext" rows="7" placeholder="Please write some information about your coaching business."><? if($_POST[info]){ echo strip_tags(stripslashes($_POST[info])); } ?></textarea>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Rates &amp; Fees</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
 <textarea name="cost" rows="7" class="txt lngtext" placeholder="Please describe your rates and fees."><? if($_POST[cost]){ echo strip_tags(stripslashes($_POST[cost])); } ?></textarea>
                        </div>
                      </div>
                      <div class="drow">
                        <div class="dcell name">Camps</div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <textarea name="camps" rows="7" class="txt lngtext" placeholder="Please describe any training camps your put on."><? if($_POST[camps]){ echo strip_tags(stripslashes($_POST[camps])); } ?></textarea>
                        </div>
                      </div>
                      <div class="drow actions">
                        <div class="dcell name"></div>
                        <div class="dcell divider"></div>
                        <div class="dcell value">
    <input type="submit" value="Submit" class="btn">
	<input type="hidden" name="add_coach" value="true">
                        </div>
                      </div>
                    </div>

	</form>
 
<? }
// end insert form

// begin confirmation form
if($confirm_coach == true){

?>
	Please confirm your information. If everything is correct, click the submit button. Otherwise, you may click the edit button to change your information before submitting.</div>
	<br />

    <div class="dtable form">
      <div class="drow">
        <div class="dcell name">Name</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
            <? echo stripslashes($_POST[name]); ?>
        </div>
      </div>
	<? // check the db for similar matches
	    $name_tag = strtolower(str_replace(" ","", $_POST['name']));
		$name_superstart = substr($name_tag, 0, 4);
		$name_superend = substr($name_tag, -4, 4);
		$name_supermid = substr($name_tag, ((strlen($name_tag))/2 - 2), 4);
		//echo $name_supermid;
		$match_sql = "SELECT coach_id, coach_name, coach_name_tag, coach_city, coach_state FROM ".$prefix."Coaches WHERE coach_valid = 1 AND (coach_name_tag LIKE '%".$name_superstart."%' OR coach_name_tag LIKE '%".$name_superend."%' OR coach_name_tag LIKE '%".$name_supermid."%')";
		
		//echo $match_sql;
		
		$match_fetch = mysql_query($match_sql) OR die(mysql_error());
		
		$i = false;
		while ($match_row = mysql_fetch_array($match_fetch)) { 
			similar_text($match_row['coach_name'], $_POST['name'], $percent); 
			if ($percent >= 50) { 
				if ($i == false) { 
					echo ("<tr><td align=\"right\">Duplicate Alert:</td><td>");
				}
				if ($i == true) {echo ("<br />");}
				echo ("<a href=\"".$site_url."/individual.php?coach_id=".$match_row['coach_id']."\">".$match_row['coach_name']." (".$match_row['coach_city'].", ".$match_row['coach_state'].")</a> (Similarity: ".(round($percent, 0))."%)"); 
				$i = true;
			}
		}
		if ($i == true) { echo("</td></tr>"); }
	?>
      <div class="drow">
        <div class="dcell name">Street Address</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[address])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Street Address Two</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[address_two])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">City</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[city])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">State</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[state])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Zip/Postal Code</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[zip])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Phone</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[phone])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Fax</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[fax])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">E-mail</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo stripslashes(htmlspecialchars($_POST[email])); ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Website</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <a href="<? echo $_POST[website]; ?>" target="_blank"><? echo $_POST[website]; ?></a>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name"></div>
        <div class="dcell divider"></div>
        <div class="dcell value">
            <h3>Info</h3>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Certifications</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
    <?
	
	$explosion = implode(",", $_POST[certifications]);
	
	$sql = ("SELECT * FROM ".$prefix."CoachesCertifications WHERE coach_certification_id IN ($explosion) ORDER BY coach_certification_name ASC");
	
	//echo $sql;
	
	$results = mysql_query($sql) or die(mysql_error());
	
	$certification_list = "";
	while ($row = mysql_fetch_array($results)) {
		$certification_list  = $certification_list."$row[coach_certification_name], ";
	}
	$certification_list = rtrim($certification_list, ', ');
	
	echo $certification_list;
	
	?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Services</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
    <?
	
	$explosion = implode(",", $_POST[services]);
	
	$sql = ("SELECT * FROM ".$prefix."CoachesServices WHERE coach_service_id IN ($explosion) ORDER BY coach_service_name ASC");
	
	//echo $sql;
	
	$results = mysql_query($sql) or die(mysql_error());
	
	$service_list = "";
	while ($row = mysql_fetch_array($results)) {
		$service_list  = $service_list."$row[coach_service_name], ";
	}
	$service_list = rtrim($service_list, ', ');
	
	echo $service_list;
	
	?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Education</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
    <?
	
	$explosion = implode(",", $_POST[degrees]);
	
	$sql = ("SELECT * FROM ".$prefix."CoachesDegrees WHERE coach_degree_id IN ($explosion) ORDER BY coach_degree_name ASC");
	
	//echo $sql;
	
	$results = mysql_query($sql) or die(mysql_error());
	
	$degree_list = "";
	while ($row = mysql_fetch_array($results)) {
		$degree_list  = $degree_list."$row[coach_degree_name], ";
	}
	$degree_list = rtrim($degree_list, ', ');
	
	echo $degree_list;
	
	?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">General Info</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo strip_tags(stripslashes($_POST[info])) ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Rates &amp; Fees</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo strip_tags(stripslashes($_POST[cost])) ?>
        </div>
      </div>
      <div class="drow">
        <div class="dcell name">Camps</div>
        <div class="dcell divider"></div>
        <div class="dcell value">
	        <? echo strip_tags(stripslashes($_POST[camps])) ?>
        </div>
      </div>
      <div class="drow actions">
        <div class="dcell name"></div>
        <div class="dcell divider"></div>
        <div class="dcell value">
            <form action="add.php" method="post">
                <input type="hidden" name="name" value="<? echo stripslashes(htmlspecialchars($_POST[name])); ?>">
                <input type="hidden" name="address" value="<? echo stripslashes(htmlspecialchars($_POST[address])); ?>">
                <input type="hidden" name="address_two" value="<? echo stripslashes(htmlspecialchars($_POST[address_two])); ?>">
                <input type="hidden" name="city" value="<? echo stripslashes(htmlspecialchars($_POST[city])); ?>">
                <input type="hidden" name="state" value="<? echo $_POST[state]; ?>">
                <input type="hidden" name="zip" value="<? echo $_POST[zip]; ?>">
                <input type="hidden" name="phone" value="<? echo $_POST[phone]; ?>">
                <input type="hidden" name="fax" value="<? echo $_POST[fax]; ?>">
                <input type="hidden" name="email" value="<? echo $_POST[email]; ?>">
                <input type="hidden" name="website" value="<? echo $_POST[website]; ?>">
                <input type="hidden" name="certifications" value="<? echo(implode(',', $_POST[certifications])); ?>">
                <input type="hidden" name="services" value="<? echo(implode(',', $_POST[services])); ?>">
                <input type="hidden" name="degrees" value="<? echo(implode(',', $_POST[degrees])); ?>">
                <input type="hidden" name="cost" value="<? echo stripslashes(htmlspecialchars($_POST[cost])); ?>">
                <input type="hidden" name="info" value="<? echo stripslashes(htmlspecialchars($_POST[info])); ?>">
                <input type="hidden" name="camps" value="<? echo stripslashes(htmlspecialchars($_POST[camps])); ?>">
                <input type="hidden" name="add_coach" value="true">
                <input type="hidden" name="insert_coach" value="true">
                <input type="hidden" name="submitted_by" value="<? echo $useruid; ?>" >
                <input type="submit" name="submit" value="Submit Info" class="btn float-left" style="margin-right: 5px;"/>
            </form>
            <form action="add.php" method="post">
                <input type="hidden" name="name" value="<? echo stripslashes(htmlspecialchars($_POST[name])); ?>">
                <input type="hidden" name="address" value="<? echo stripslashes(htmlspecialchars($_POST[address])); ?>">
                <input type="hidden" name="address_two" value="<? echo stripslashes(htmlspecialchars($_POST[address_two])); ?>">
                <input type="hidden" name="city" value="<? echo stripslashes(htmlspecialchars($_POST[city])); ?>">
                <input type="hidden" name="state" value="<? echo $_POST[state]; ?>">
                <input type="hidden" name="zip" value="<? echo $_POST[zip]; ?>">
                <input type="hidden" name="phone" value="<? echo $_POST[phone]; ?>">
                <input type="hidden" name="fax" value="<? echo $_POST[fax]; ?>">
                <input type="hidden" name="email" value="<? echo $_POST[email]; ?>">
                <input type="hidden" name="website" value="<? echo $_POST[website]; ?>">
                <input type="hidden" name="certifications" value="<? echo(implode(',', $_POST[certifications])); ?>">
                <input type="hidden" name="services" value="<? echo(implode(',', $_POST[services])); ?>">
                <input type="hidden" name="degrees" value="<? echo(implode(',', $_POST[degrees])); ?>">
                <input type="hidden" name="cost" value="<? echo stripslashes(htmlspecialchars($_POST[cost])); ?>">
                <input type="hidden" name="info" value="<? echo stripslashes(htmlspecialchars($_POST[info])); ?>">
                <input type="hidden" name="camps" value="<? echo stripslashes(htmlspecialchars($_POST[camps])); ?>">
                <input type="hidden" name="confirm_coach" value="0">
                <input type="submit" name="submit" value="Edit Information" class="btn float-left" />
	        </form>
        </div>
      </div>
    </div>
<?
} 
//end confirmation form
?>

              </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>
