<?php session_start(); ?>
<?php // include("debuglib.php"); ?>
<?php include("class.upload.php"); ?>
<?php include ("class.upload.inc.php") ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); // HTTP/1.0 
?>
<?php include ("db.php") ?>
<?php include ("phpmkrfn.php") ?>
<?php
ob_start();

// debug
// error_reporting( E_ALL );


function getRandomString( $len=40, $num=1, $uc=1, $lc=1 )
{
  if ( !$len || $len<1 || $len>100 )
  {
    return "";
  }
  if (!$num && !$uc && !$lc)
  {
    return "";
  }

  $s = "";
  $i = 0;
  do
  {
    switch ( mt_rand( 1, 3 ) )
    {
      // get number - ASCII characters (0:48 through 9:57)
      case 1:
        if ( $num == 1 )
        {
          $s .= chr( mt_rand( 48, 57 ) );
          $i++;
        }
        break;

      // get uppercase letter - ASCII characters (a:65 through z:90)
      case 2:
        if ( $uc == 1 )
        {
          $s .= chr( mt_rand( 65, 90 ) );
          $i++;
        }
        break;

      // get lowercase letter - ASCII characters (A:97 through Z:122)
      case 3:
        if ( $lc == 1 )
        {
          $s .= chr( mt_rand( 97, 122 ) );
          $i++;
        }
        break;
    }
  } while ( $i < $len );

  return $s;
}



// get action
$a = @$HTTP_POST_VARS["a"];
if (empty($a)) {
  $key = @$HTTP_GET_VARS["key"];
  if ($key <> "") {
    $a = "C"; // copy record
  } else{
    $a = "I"; // display blank record
  }
}

// open connection to the database
$conn = mysql_connect(HOST, USER, PASS);
mysql_select_db(DB);
switch ($a) {
  case "A": // add
    // get the form values
    $x_contact_id = @$HTTP_POST_VARS["x_contact_id"];
    $x_category_id = @$HTTP_POST_VARS["x_category_id"];
    $x_cat_id = @$HTTP_POST_VARS["x_cat_id"];
    $x_company_name = @$HTTP_POST_VARS["x_company_name"];

//    $x_image = @$HTTP_POST_VARS["x_image"];
    $x_image = @$_SESSION['kplink_image_scaled'];
    if ( !empty( $x_image ) )
    {
      $x_image = IMAGE_DB . $x_image;
    }

//    $x_sponsor = @$HTTP_POST_VARS["x_sponsor"];
    $x_sponsor = "Y";

    $x_description = @$HTTP_POST_VARS["x_description"];
    $x_website_address = @$HTTP_POST_VARS["x_website_address"];
    $x_hours = @$HTTP_POST_VARS["x_hours"];
    $x_ages = @$HTTP_POST_VARS["x_ages"];
    $x_cost = @$HTTP_POST_VARS["x_cost"];
    $x_contact_name = @$HTTP_POST_VARS["x_contact_name"];
    $x_location_name = @$HTTP_POST_VARS["x_location_name"];
    $x_address1 = @$HTTP_POST_VARS["x_address1"];
    $x_address2 = @$HTTP_POST_VARS["x_address2"];
    $x_city = @$HTTP_POST_VARS["x_city"];
    $x_state = @$HTTP_POST_VARS["x_state"];
    $x_zip = @$HTTP_POST_VARS["x_zip"];
    $x_phone = @$HTTP_POST_VARS["x_phone"];
    $x_email_address = @$HTTP_POST_VARS["x_email_address"];
    $x_directions = @$HTTP_POST_VARS["x_directions"];
    $x_contract_start_date = @$HTTP_POST_VARS["x_contract_start_date"];
    $x_contract_length = @$HTTP_POST_VARS["x_contract_length"];
    $x_account_manager_id = @$HTTP_POST_VARS["x_account_manager_id"];

//    $x_link_type_id = @$HTTP_POST_VARS["x_link_type_id"];
    $x_link_type_id = 1;

    $x_submitting_events = @$HTTP_POST_VARS["x_submitting_events"];
    $x_loc2_location_name = @$HTTP_POST_VARS["x_loc2_location_name"];
    $x_loc2_address = @$HTTP_POST_VARS["x_loc2_address"];
    $x_loc2_city = @$HTTP_POST_VARS["x_loc2_city"];
    $x_loc2_phone = @$HTTP_POST_VARS["x_loc2_phone"];
    $x_loc2_email_address = @$HTTP_POST_VARS["x_loc2_email_address"];
    $x_loc3_location_name = @$HTTP_POST_VARS["x_loc3_location_name"];
    $x_loc3_address = @$HTTP_POST_VARS["x_loc3_address"];
    $x_loc3_city = @$HTTP_POST_VARS["x_loc3_city"];
    $x_loc3_phone = @$HTTP_POST_VARS["x_loc3_phone"];
    $x_loc3_email_address = @$HTTP_POST_VARS["x_loc3_email_address"];
    $x_loc4_location_name = @$HTTP_POST_VARS["x_loc4_location_name"];
    $x_loc4_address = @$HTTP_POST_VARS["x_loc4_address"];
    $x_loc4_city = @$HTTP_POST_VARS["x_loc4_city"];
    $x_loc4_phone = @$HTTP_POST_VARS["x_loc4_phone"];
    $x_loc4_email_address = @$HTTP_POST_VARS["x_loc4_email_address"];

    // add the values into an array
    // cat_id
    if ( count( $x_cat_id ) > 0 )
    {
      $theValue = ',' . implode(',', $x_cat_id ) . ',';
    }
    else
    {
      $theValue = '';
    }
//    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "''";
    $fieldList["`cat_id`"] = $theValue;

    // company_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_company_name) : $x_company_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`company_name`"] = $theValue;

    // image
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_image) : $x_image;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`image`"] = $theValue;

    // sponsor
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_sponsor) : $x_sponsor;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`sponsor`"] = $theValue;

    // description
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_description) : $x_description;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`description`"] = $theValue;

    // website_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_website_address) : $x_website_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`website_address`"] = $theValue;

    // hours
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_hours) : $x_hours;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`hours`"] = $theValue;

    // ages
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_ages) : $x_ages;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`ages`"] = $theValue;

    // cost
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_cost) : $x_cost;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`cost`"] = $theValue;

    // contact_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_contact_name) : $x_contact_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`contact_name`"] = $theValue;

    // location_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_location_name) : $x_location_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`location_name`"] = $theValue;

    // address1
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_address1) : $x_address1;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`address1`"] = $theValue;

    // address2
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_address2) : $x_address2;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`address2`"] = $theValue;

    // city
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_city) : $x_city;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`city`"] = $theValue;

    // state
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_state) : $x_state;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`state`"] = $theValue;

    // zip
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_zip) : $x_zip;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`zip`"] = $theValue;

    // phone
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_phone) : $x_phone;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`phone`"] = $theValue;

    // email_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_email_address) : $x_email_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`email_address`"] = $theValue;

    // directions
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_directions) : $x_directions;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`directions`"] = $theValue;

    // loc2_location_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc2_location_name) : $x_loc2_location_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc2_location_name`"] = $theValue;

    // loc2_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc2_address) : $x_loc2_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc2_address`"] = $theValue;

    // loc2_city
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc2_city) : $x_loc2_city;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc2_city`"] = $theValue;

    // loc2_phone
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc2_phone) : $x_loc2_phone;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc2_phone`"] = $theValue;

    // loc2_email_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc2_email_address) : $x_loc2_email_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc2_email_address`"] = $theValue;

    // loc3_location_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc3_location_name) : $x_loc3_location_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc3_location_name`"] = $theValue;

    // loc3_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc3_address) : $x_loc3_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc3_address`"] = $theValue;

    // loc3_city
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc3_city) : $x_loc3_city;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc3_city`"] = $theValue;

    // loc3_phone
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc3_phone) : $x_loc3_phone;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc3_phone`"] = $theValue;

    // loc3_email_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc3_email_address) : $x_loc3_email_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc3_email_address`"] = $theValue;

    // loc4_location_name
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc4_location_name) : $x_loc4_location_name;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc4_location_name`"] = $theValue;

    // loc4_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc4_address) : $x_loc4_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc4_address`"] = $theValue;

    // loc4_city
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc4_city) : $x_loc4_city;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc4_city`"] = $theValue;

    // loc4_phone
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc4_phone) : $x_loc4_phone;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc4_phone`"] = $theValue;

    // loc4_email_address
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_loc4_email_address) : $x_loc4_email_address;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`loc4_email_address`"] = $theValue;

    // link_type_id
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($x_link_type_id) : $x_link_type_id;
    $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
    $fieldList["`link_type_id`"] = $theValue;

    // insert into database
    $strsql = "INSERT INTO `kpl_link` (";
    $strsql .= implode(",", array_keys($fieldList));
    $strsql .= ") VALUES (";
    $strsql .= implode(",", array_values($fieldList));
    $strsql .= ")";

// echo $strsql;
// echo "<br />";

    mysql_query($strsql, $conn) or die(mysql_error());
    mysql_close($conn);
    ob_end_clean();

    unset( $_SESSION['kplink_image_scaled'] );
    unset( $_SESSION['kplink_image'] );

    header("Location: thankyou.php");
    break;
}

  // handle upload image
  $x_submit_image_error = '';
  $x_kplink_image = '';
  $file_upload_error_text = '';

  $remove_image = @$_GET['remove_image'];
  if ( !empty( $remove_image ) )
  {
    $image_to_delete = @$_SESSION['kplink_image_scaled'];
    if ( !empty( $image_to_delete ) )
    {
      $image_to_delete = IMAGE_PATH . $image_to_delete;
      if ( file_exists( $image_to_delete ) )
      {
        unlink( $image_to_delete );
      }
    }

    $image_to_delete = @$_SESSION['kplink_image'];
    if ( !empty( $image_to_delete ) )
    {
      $image_to_delete = IMAGE_PATH . $image_to_delete;
      if ( file_exists( $image_to_delete ) )
      {
        unlink( $image_to_delete );
      }
    }

    unset( $_SESSION['kplink_image_scaled'] );
    unset( $_SESSION['kplink_image'] );
  }

  $x_submit_image = @$_POST['submit_image'];
  if ( isset( $x_submit_image ) )
  {
    $MyObject = new upload($_FILES['image_field']);
    $x_submit_image_error .= $MyObject->log;
    $x_submit_image_error .= '<hr>';
    if ($MyObject->uploaded)
    {
//      $images_file_name = getRandomString();

      $images_file_name = $MyObject->file_src_name_body;

      $images_file_name = str_replace ( array ( ' ' ), array ( '_' ), $images_file_name );

      // save unconverted image
      $MyObject->file_new_name_body     = $images_file_name;
      $MyObject->process( IMAGE_PATH );

      // save scaled image
      $base_image_name = $MyObject->file_dst_name;
      $MyObject->image_resize           = true;
      $MyObject->image_convert          = 'jpg';
      $MyObject->image_x                = 300;
      $MyObject->image_y                = 1000;
      $MyObject->image_ratio_no_zoom_in = true;
      $MyObject->file_new_name_body     = $images_file_name;
      $MyObject->file_name_body_add     = '_scaled';
      $MyObject->process( IMAGE_PATH );

      $x_scaled_image_name = $MyObject->file_dst_name;

      if ($MyObject->processed)
      {
        $x_kplink_image = $base_image_name;
        $_SESSION['kplink_image'] = $x_kplink_image;
        $_SESSION['kplink_image_scaled'] = $x_scaled_image_name;

        $x_submit_image_error .= 'original image<br />';
        $x_submit_image_error .= '<b>file_src_pathname</b>:  ' . $MyObject->file_src_pathname . '<br>';
        $x_submit_image_error .= '<img src="' . IMAGE_URL . $x_kplink_image . '"><br>';


        $x_submit_image_error .= 'image resized<br />';
        $x_submit_image_error .= $MyObject->log;
        $x_submit_image_error .= '<b>file_dst_pathname</b>:  ' . $MyObject->file_dst_pathname . '<br>';
        $x_submit_image_error .= '<img src="' . IMAGE_URL . $MyObject->file_dst_name . '"><br>';

        $MyObject->clean();
      }
      else
      {
        $x_submit_image_error .= 'error : ' . $MyObject->error;
        $x_submit_image_error .= $MyObject->log;
      }
    }
    else
    {
      $x_submit_image_error .= 'error : ' . $MyObject->error;
      $x_submit_image_error .= $MyObject->log;
    }

    if ( !$MyObject->processed || !$MyObject->uploaded )
    {
      $file_upload_error_text = $MyObject->error;
    }
  }




?>
<?php include ("header.php") ?>
<script language="JavaScript" src="ew.js"></script>
<script language="JavaScript">
<!-- start Javascript
function  EW_checkMyForm(EW_this) {
return true;
}

// end JavaScript -->
</script>
<script language="JavaScript">
// Toggles the layer visibility on
function showLayer(layerName)
{
  document.getElementById(layerName).style.visibility="visible";
}

// Toggles the layer visibility off
function hideLayer(layerName)
{
  document.getElementById(layerName).style.visibility="hidden";
}
</script>

<?php
/*
if ( !empty( $x_submit_image_error ) )
{
  echo "image message<br />";
  echo $x_submit_image_error;
  echo "<br />";
}
*/

  $x_scaled_image_name = @$_SESSION['kplink_image_scaled'];
  $x_kplink_image = @$_SESSION['kplink_image'];

  if ( !empty( $x_scaled_image_name ) )
  {
    echo "<img src=\"" . IMAGE_URL . "$x_scaled_image_name\">";
    echo '<br /><a href="newlisting.php?remove_image=1">Remove logo</a><br /><br />';
  }
  else
  {
?>
    <div style="border-style: solid; border-color: #0000cc; padding:15px;margin-bottom:10px;width:500px;">
      Please, upload your logo if you are<br />
      a 3, 6 or 9-month advertiser.<br />
      <form action="newlisting.php" method="post" enctype="multipart/form-data" onsubmit="hideLayer('sbmt');showLayer('upld');">
        <input name="submit_image" value="1" type="hidden" />
        <input type="file" size="50" name="image_field" value="">
        <div class="action" style="padding-top:2px;">
          <div style="position: relative;" >
            <div id="sbmt" style="position:absolute;left:0px;top:0px;">
              <input type="submit" value="Upload Logo">
            </div>
            <div id="upld" style="visibility:hidden;position:absolute;left:0px;top:0px;padding:0px;margin:0px;">
              Uploading logo. Please wait...
            </div>
          </div>
        </div>
      </form>
    </div>

<?php
  }
?>

<form onSubmit="return EW_checkMyForm(this);"  action="newlisting.php" method="post">
<input type="submit" name="Action" value="Submit Listing">
<p>
<input type="hidden" name="a" value="A">
<table border="0" cellspacing="1" cellpadding="4" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Company Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_company_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_company_name); ?>"></font>&nbsp;</td>
</tr>

<!--
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Image</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_image" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_image); ?>"></font>&nbsp;</td>
</tr>
-->

<!--
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Sponsor</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><?php if (empty($x_sponsor)) { $x_sponsor = "N"; } // set default value ?><input type="radio" name="x_sponsor"<?php if ($x_sponsor == "Y") { echo " checked"; } ?> value="<?php echo htmlspecialchars("Y"); ?>"><?php echo "Yes"; ?>
<input type="radio" name="x_sponsor"<?php if ($x_sponsor == "N") { echo " checked"; } ?> value="<?php echo htmlspecialchars("N"); ?>"><?php echo "No"; ?>
</font>&nbsp;</td>
</tr>
-->

<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Description</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><textarea name="x_description" cols="50" rows="8"><?php echo @$x_description ?></textarea></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Website</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_website_address" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_website_address); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Hours</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><textarea name="x_hours" cols="50" rows="3"><?php echo @$x_hours ?></textarea></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Ages</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><textarea name="x_ages" cols="50" rows="3"><?php echo @$x_ages ?></textarea></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Cost</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><textarea name="x_cost" cols="50" rows="3"><?php echo @$x_cost ?></textarea></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Contact Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_contact_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_contact_name); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Location Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_location_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_location_name); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_address1" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_address1); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Address 2</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_address2" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_address2); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">City</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_city" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_city); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">State</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_state" size="2" maxlength="2" value="<?php echo htmlspecialchars(@$x_state); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Zip</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_zip" size="12" maxlength="12" value="<?php echo htmlspecialchars(@$x_zip); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Phone</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_phone" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_phone); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">E-Mail</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_email_address" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_email_address); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Directions</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><textarea name="x_directions" cols="50" rows="5"><?php echo @$x_directions ?></textarea></font>&nbsp;</td>
</tr>
<tr><td style="font-size:3px" bgcolor="#FFFF00" colspan="2">&nbsp;</td></tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc2 Location Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc2_location_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc2_location_name); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc2 Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc2_address" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc2_address); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc2 City</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc2_city" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc2_city); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc2 Phone</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc2_phone" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc2_phone); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc2 E-Mail Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc2_email_address" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc2_email_address); ?>"></font>&nbsp;</td>
</tr>
<tr><td style="font-size:3px" bgcolor="#FFFF00" colspan="2">&nbsp;</td></tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc3 Location Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc3_location_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc3_location_name); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc3 Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc3_address" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc3_address); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc3 City</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc3_city" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc3_city); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc3 Phone</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc3_phone" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc3_phone); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc3 E-Mail Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc3_email_address" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc3_email_address); ?>"></font>&nbsp;</td>
</tr>
<tr><td style="font-size:3px" bgcolor="#FFFF00" colspan="2">&nbsp;</td></tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc4 Location Name</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc4_location_name" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc4_location_name); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc4 Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc4_address" size="50" maxlength="255" value="<?php echo htmlspecialchars(@$x_loc4_address); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc4 City</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc4_city" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc4_city); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc4 Phone</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc4_phone" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc4_phone); ?>"></font>&nbsp;</td>
</tr>
<tr>
<td bgcolor="#00CCFF"><font color="#000000"><font class="phpmaker">Loc4 E-Mail Address</font>&nbsp;</font></td>
<td bgcolor="#F5F5F5"><font class="phpmaker"><input type="text" name="x_loc4_email_address" size="50" maxlength="50" value="<?php echo htmlspecialchars(@$x_loc4_email_address); ?>"></font>&nbsp;</td>
</tr>
<tr><td style="font-size:3px" bgcolor="#FFFF00" colspan="2">&nbsp;</td></tr>

<tr>
<td bgcolor="#00CCFF" valign="top"><font color="#000000"><font class="phpmaker">Categories</font>&nbsp;</font></td>

<td bgcolor="#F5F5F5"><font class="phpmaker">

<?php

// $sqlwrk  = "SELECT `category_id`, `category_desc`, `section_desc` ";
// $sqlwrk .= "FROM `kpl_link_category` c, `kpl_link_section` s ";
// $sqlwrk .= "where c.section_id=s.section_id and category_id > 1 ";
// $sqlwrk .= "ORDER BY `section_desc`, `category_desc` ASC";
// 
// $rswrk = mysql_query($sqlwrk);
// $rows_returned = mysql_num_rows( $rswrk );
// 
// $x_category_idList = '<select size="' . $rows_returned . '" name="x_cat_id[]" multiple="multiple">';
// 
// if ($rswrk) {
//   $rowcntwrk = 0;
//   while ($datawrk = mysql_fetch_array($rswrk)) {
//     $x_category_idList .= "<option value=\"" . htmlspecialchars($datawrk[0]) . "\"";
//     if ( !empty($x_cat_id) && in_array( $datawrk[0], $x_cat_id ) ) {
//       $x_category_idList .= " selected";
//     }
//     $x_category_idList .= ">" . $datawrk[2] . " :: " . $datawrk[1] . "</option>";
//     $rowcntwrk++;
//   }
// }
// @mysql_free_result($rswrk);
// $x_category_idList .= "</select>";
// echo $x_category_idList ;



//////////////////////////////////////////////////////////////////
/*
echo '<div style="font-size:18px;">';

echo '$x_cat_id: ' . $x_cat_id;
echo '<hr />';

if ( isset( $x_cat_id ) )
{
  echo 'set<br />';
}
else
{
  echo 'not set<br />';
}

if ( empty( $x_cat_id ) )
{
  echo 'empty<br />';
}
else
{
  echo 'not empty<br />';
}

echo '</div>';
*/
//////////////////////////////////////////////////////////////////

if ( !isset( $x_cat_id ) || empty( $x_cat_id ) )
{
  $x_cat_id = array();
}

//////////////////////////////////////////////////////////////////
/*
echo '<div style="font-size:18px;">';
echo '$x_cat_id: ' . $x_cat_id;
echo '<hr />';

if ( isset( $x_cat_id ) )
{
  echo 'set<br />';
}
else
{
  echo 'not set<br />';
}

if ( empty( $x_cat_id ) )
{
  echo 'empty<br />';
}
else
{
  echo 'not empty<br />';
}

echo '</div>';
*/
//////////////////////////////////////////////////////////////////


  $strsql = 'select section_id, section_desc from kpl_link_section where section_id > 1';
  $rs = mysql_query( $strsql );
  $sectionArr = array();
  while ( ( $row = @mysql_fetch_array( $rs ) ) )
  {
    $sectionArr[$row["section_id"]] = $row["section_desc"];
  }
  // close result
  @mysql_free_result($rs);

  reset($sectionArr);

  $allCatsArr = array();
  while (list($key, $val) = each($sectionArr))
  {
    $categoryArr = array();
    $strsql = 'select category_id, category_desc from kpl_link_category where section_id=' . $key . ' and parent_id = 1';
    $rs = mysql_query( $strsql );

    while ( ( $row = @mysql_fetch_array( $rs ) ) )
    {
      $categoryArr[$row["category_id"]] = $row["category_desc"];
      $x_category_id   = $row["category_id"];
      $x_category_desc = $row["category_desc"];
    }
    $allCatsArr[$key] = array( "sectiondesc" => $val, "cats" => $categoryArr );

    @mysql_free_result($rs);
  }

  reset( $allCatsArr );
  while (list($sectkey, $allcat) = each($allCatsArr) )
  {
    $sectionDesc = $allcat["sectiondesc"];

    echo '<div style="font-size:20px;font-weight:bold;">' . $sectionDesc . '</div>';

    $cats = $allcat["cats"];
    while (list($key, $catDesc) = each($cats) )
    {
      echo '&nbsp;&nbsp;';
      echo '&nbsp;&nbsp;';
      echo '<input type="checkbox" name="x_cat_id[]" value="' . $key . '"' . ( in_array( $key, $x_cat_id ) ? " checked" : " " ) . ' id="catid' . $key . '">';
      echo '<label for="catid' . $key . '">' . $catDesc . '</label><br>' . "\n";

      // First find out if there are any subsections
      $strsql = "select category_id, parent_id, category_desc from kpl_link_category where section_id=$sectkey and parent_id = $key";
      $rs = mysql_query( $strsql );
      $num_rows = mysql_num_rows($rs);
      $subCatsArr = array();
      if ( $num_rows > 0 )
      {
        // Table of Contents
        while ( ( $row = @mysql_fetch_array( $rs ) ) )
        {
          $x_category_id   = $row["category_id"];
          $x_parent_id     = $row["parent_id"];
          $x_category_desc = $row["category_desc"];
          echo '&nbsp;&nbsp;';
          echo '&nbsp;&nbsp;';
          echo '&nbsp;&nbsp;';
          echo '&nbsp;&nbsp;';
          echo '<input type="checkbox" name="x_cat_id[]" value="' . $x_category_id . '"' . ( in_array( $x_category_id, $x_cat_id ) ? " checked" : " " ) . ' id="catid' . $x_category_id . '">';
          echo '<label for="catid' . $x_category_id . '">' . $x_category_desc . '</label><br>' . "\n";
        }
      }
      @mysql_free_result($rs);
    }
  }

//////////////////////////////////////////////////////////////////




?>
</font>&nbsp;</td>


</tr>



</table>
<p>
<input type="submit" name="Action" value="Submit Listing">
</form>
<?php include ("footer.php") ?>
<?php // show_vars(); ?>
