<?php
$job_fields = array(
    "job_jobs_id" => "",
    "job_cat_id" => "",
    "catdom_id" => "",
    "CompanyName" => "",
    "Address" => "",
    "Town" => "",
    "Phone" => "",
    "Mobile" => "",
    "Fax" => "",
    "ContactPerson" => "",
    "EmailAdd" => "",
    "JobTitle" => "",
    "JobDescription" => "",
    "NumberOfPersons" => "",
    "Qualifications" => "",
    "Other" => "",
    "Location" => "",
    "Conditions" => "",
    "added_on" => "",
    "added_on_time" => "",
    "member_id" => "",
    "active" => "",
    "clicks" => ""
);

if (!isset($_SESSION['member_id'])) {
    ?>
    <div class="alert alert-info">
        <b>To post a job you must be registered member. Please <a href="account/add_member.html">register</a>. It is absolutely free. </b>
        <br>
        <b>Inportant Information:</b>
        <p>Please be aware that any information posted here will be available to the public and it will be crawled by search engines (eg. Google, Yahoo, Bing).</p>
    </div>
    <?php
}
else {

    if (isset($_POST['chcAction'])) {
        foreach ($_POST as $k => $v) {
            ${$k} = $v;
        }
    }

    ?>
    <div class="box box-primary">
        <div class="box-header with-border">Post a Job</div>

        <div class="box-body">
            <?php
            if (isset($_REQUEST["Submit"]) && $_REQUEST["Submit"] == "Renew") {
                $job_jobs_id_to_renew = $_REQUEST["job_jobs_id"];
                mysql_query("UPDATE job_jobs SET added_on=NOW(), added_on_time=NOW() WHERE job_jobs_id='$job_jobs_id_to_renew'") or die(mysql_error());
                ?>
                Your Posted Job was Renewed successfully. Thank you!!<br>
                <?php
            }
            ?>
            <?php
            $query_member_advert = "SELECT * FROM job_jobs WHERE member_id='{$_SESSION['member_id']}'";
            $result_member_advert = mysql_query($query_member_advert);
            $table = "job_jobs";
            $rows_array = array("job_cat_id", "CompanyName", "Address", "Town", "Phone", "Mobile", "Fax", "ContactPerson", "EmailAdd", "JobTitle", "JobDescription", "NumberOfPersons", "Qualifications", "Other", "Location", "Conditions");
            $id = "job_jobs_id";
            $member_id = $_SESSION["member_id"];

            if (isset($_POST['chcAction'])) {
                if ($Town == "other") {
                    $Town = $othertown;
                }
                $Location = $Locations[0];
                for ($i = 1; $i <= count($Locations) - 1; $i++) {
                    $Location .= ";";
                    $Location .= $Locations[$i];
                }
                if (isset($chcAction) && strlen($Location) < 4) {
                    ?>
                    Some required fields have not been completed. Please try again.<br>
                    <?php
                } else {
                    switch ($chcAction) {
                        case "Add":
                            $catdom_id = $domain_id;
                            $active = "no";
                            $added_on = date("Y-m-d");
                            $rows = "";
                            $variables = "";
                            for ($i = 0; $i <= (count($rows_array) - 2); $i++) {
                                $rows .= $rows_array[$i];
                                $variables .= "'";
                                $variables .= $$rows_array[$i];
                                $variables .= "'";
                                $rows .= ", ";
                                $variables .= ", ";
                            }
                            $rows .= $rows_array[count($rows_array) - 1];
                            $variables .= "'";
                            $variables .= $$rows_array[count($rows_array) - 1];
                            $variables .= "'";
                            $query1 = "INSERT INTO $table ($rows,catdom_id,active,member_id,added_on,added_on_time) VALUES ($variables,'$catdom_id','$active','$member_id','$added_on', NOW())";
                            $result1 = mysql_query($query1) or die (mysql_error());
                            $job_jobs_id = mysql_insert_id();
                            ?>
                            <div class="attention">Your Job was Posted successfully. Thank you!!</div>
                            <?php
                            break;

                        case "Edit":
                            $rows = "";
                            $variables = "";
                            for ($i = 0; $i <= (count($rows_array) - 2); $i++) {
                                $rows .= $rows_array[$i];
                                $rows .= "=";
                                $rows .= "'";
                                $rows .= ${$rows_array[$i]};
                                $rows .= "'";
                                $rows .= ", ";
                            }
                            $rows .= $rows_array[count($rows_array) - 1];
                            $rows .= "=";
                            $rows .= "'";
                            $rows .= ${$rows_array[count($rows_array) - 1]};
                            $rows .= "'";
                            $query2 = "UPDATE $table SET $rows WHERE $id='" . ${$id} . "'";
                            $result2 = mysql_query($query2) or die(mysql_error());
                            ?>
                            <div class="attention">Your Posted Job Updated successfully. Thank you!!</div>
                            <?php
                            break;

                        case "Delete":
                            $query1 = "SELECT * FROM $table WHERE $id='${$id}'";
                            $result1 = mysql_query($query1);
                            $line1 = mysql_fetch_array($result1);
                            $query1 = "DELETE FROM $table WHERE $id='${$id}'";
                            $result1 = mysql_query($query1) or die(mysql_error());
                            $job_jobs_id = 0;
                            ?>
                            <div class="attention">Your Posted Job Deleted successfully. Thank you!!</div>
                            <?php
                            break;
                    }
                }

                ?>

                <div class="attention">
                    <span>Inportant Information:</span>

                    <p>Please be aware that any information posted here will be available to the public and it will be
                        crawled by search engines (eg. Google, Yahoo, Bing).</p>
                </div>
                <br/>
                <?php
                if (isset($_REQUEST['job_cat_id']) && intval($_REQUEST['job_cat_id']) > 0) {
                }
                $query_job_cvs = "SELECT * FROM job_cvs WHERE job_cat_id='{$_REQUEST['job_cat_id']}' AND JobLocation='{$_REQUEST['Town']}'";
                $line_config = mysql_fetch_assoc(mysql_query("SELECT * FROM job_config"));
                $NeedActivation = $line_config["NeedActivation"];
                $DaysToLive = $line_config["DaysToLive"];
                $Expiration = $line_config["Expiration"];
                if ($NeedActivation == "yes") {
                    ?>
                    <div class="attention">Attention your Job Listings may not be displayed as the Administrator of the
                        site
                        should activate them.
                    </div>
                    <?php
                }
            }
            $result_member_advert = mysql_query($query_member_advert);
            if (mysql_num_rows($result_member_advert) > 0){
            ?>
            <p>You have already posted the following Jobs:</p>

            <table width="100%" border="0" cellpadding="3" cellspacing="2">
                <tr bgcolor="#D9ECFF">
                    <td class="darkBlueBold">Job Title</td>
                    <td align="right" class="darkBlueBold">Clicks</td>
                    <?php
                    if ($Expiration == "yes") {
                        ?>
                        <td nowrap="nowrap" class="darkBlueBold" colspan="2" align="center">Expires on</td>
                        <?php
                    }
                    ?>
                </tr>
                <?php
                $formcount = 1;
                $count = 1;
                while ($line_member_advert = mysql_fetch_array($result_member_advert)) {
                    ?>
                    <tr <?php if ($count > 1) {
                        echo " bgcolor=#F5FAFF";
                        $count = 0;
                    } ?>>
                        <td>
																			<span class="darkBlueBold">
																				<a href="my_jobs/jobs_add.html?job_jobs_id=<?php echo $line_member_advert["job_jobs_id"]; ?>">
                                                                                    <?php print $line_member_advert["JobTitle"]; ?>
                                                                                </a>
																			</span>
                        </td>
                        <td align="right">
																			<span class="darkBlueBold">
																				<?php print $line_member_advert["clicks"]; ?>
																			</span>
                        </td>
                        <?php
                        if ($Expiration == "yes") {
                            ?>
                            <td align="center">
																				<span class="darkBlueBold">
																					<?php
                                                                                    $date_for_format = $line_member_advert["added_on"];
                                                                                    $date_for_format = explode("-", $date_for_format);
                                                                                    $expire = mktime(0, 0, 0, $date_for_format[1], $date_for_format[2] + $DaysToLive, $date_for_format[0]);
                                                                                    echo $ExpiresOn = date("d/m/Y", $expire);
                                                                                    ?>
																				</span>
                            </td>
                            <td align="center">
                                <?php
                                if ($expire <= mktime(0, 0, 0, date("m"), date("d"), date("Y"))) {
                                    ?>
                                    <form name="form<?php echo $formcount; ?>" method="post"
                                          action="my_jobs/jobs_add.html">
                                        <input type="submit" name="Submit" value="Renew" class="input">
                                        <input name="module_page" type="hidden" id="module_page" value="my_jobs">
                                        <input type="hidden" name="jobs_url" value="jobs_add">
                                        <input type="hidden" name="job_jobs_id"
                                               value="<?php echo $line_member_advert["job_jobs_id"]; ?>">
                                    </form>
                                    <?php
                                } else {
                                    print "&nbsp;";
                                }
                                ?>
                            </td>
                            <?php
                        }
                        ?>
                    </tr>
                    <?php
                    $formcount++;
                    $count++;
                }
                ?>
            </table>
            <span class="darkBlueBold">
            <?php
            }

                $job_jobs_id = 0;
                $line_job_jobs  = $job_fields;
                if (isset($_REQUEST['job_jobs_id']) && intval($_REQUEST['job_jobs_id']) > 0) $job_jobs_id = intval($_REQUEST['job_jobs_id']);
                if ($job_jobs_id > 0) {
                    $query1 = "SELECT * FROM job_jobs";
                    $query1 .= " WHERE job_jobs_id='$job_jobs_id' AND member_id='{$_SESSION['member_id']}'";
                    $result1 = mysql_query($query1);
                    if (mysql_num_rows($result1) < 1) {
                        ?>
                        <div class="alert alert-danger">
                            <p>
                                <i class="fa fa-warning"></i> You have no right to edit this Posted Job. Please select from your own ones.
                            </p>
                        </div>
                        <?php
                    } else {
                        $line_job_jobs = mysql_fetch_assoc($result1);
                        $line_job_jobs = array_map("stripslashes", $line_job_jobs);
                    }

                }
                ?>
                <form action="my_jobs/jobs_add.html" method="post" enctype="multipart/form-data" name="form_job_jobs" id="form_job_jobs" class="form-horizontal" role="form" onSubmit="return checkFields();">
                    <table width="100%" border="0" cellspacing="0"
                           cellpadding="2" id="job_form">
                        <tr>
                            <td class="darkBlueBold">Note: * fields
                                required
                            </td>
                            <td valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <?php
                                if ($job_jobs_id > 0) {
                                    echo '<h3 style="color: red;">Edit Job #' . $job_jobs_id . '</h3>';
                                }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Job category:</td>
                            <td valign="top">
                                <?php
                                $queryjob_cat = "SELECT * FROM  job_cat WHERE active='yes' ORDER BY order_seq ASC";
                                $resultjob_cat = mysql_query($queryjob_cat);
                                $NumberOfFieldsjob_cat = mysql_num_rows($resultjob_cat);
                                if ($NumberOfFieldsjob_cat <> 0) {
                                    ?>
                                    <select name="job_cat_id"
                                            class="input"
                                            id="job_cat_id">
                                        <option value="" selected>CHOOSE
                                            CATEGORY
                                        </option>
                                        <?php
                                        while ($linejob_cat = mysql_fetch_array($resultjob_cat)) {
                                            ?>
                                            <option value="<?php echo $linejob_cat["job_cat_id"]; ?>" <?php if ($linejob_cat["job_cat_id"] == $line_job_jobs["job_cat_id"]) echo " selected"; ?>><?php echo $linejob_cat["title_en"]; ?></option>
                                            <?php
                                        }
                                        ?>
                                    </select> *
                                    <?php
                                } else {
                                    ?>
                                    No Categories exist yet
                                    <?php
                                }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="darkBlueBold">&nbsp;</td>
                            <td valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="darkBlueBold">Mailing details :
                            </td>
                            <td valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td>Company name:</td>
                            <td valign="top"><input name="CompanyName"
                                                    type="text"
                                                    class="input"
                                                    id="CompanyName"
                                                    value="<?php echo $line_job_jobs["CompanyName"]; ?>"
                                                    size="30"
                                                    maxlength="100"> *
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Address:</td>
                            <td valign="top"><textarea name="Address"
                                                       cols="30"
                                                       rows="2"
                                                       wrap="VIRTUAL"
                                                       class="input"
                                                       id="Address"><?php echo $line_job_jobs["Address"]; ?></textarea>
                                *
                            </td>
                        </tr>
                        <tr>
                            <td>Town:</td>
                            <td valign="top">
                    <span class="right">
                        <span class="moreInfo">
                        <?php
                        $querydistricts = "SELECT * FROM  districts WHERE active='yes' ORDER BY order_seq ASC";
                        $resultdistricts = mysql_query($querydistricts);
                        if (mysql_num_rows($resultdistricts) <> 0) {
                            ?>
                            <select class="input" name="Town" id="Town">
                                <option value="" selected>Select
                                </option>
                                <?php
                                $TownFound = 0;
                                while ($linedistricts = mysql_fetch_array($resultdistricts)) {
                                    ?>
                                    <option value="<?php echo $linedistricts["title_en"]; ?>"<?php if ($linedistricts["title_en"] == $line_job_jobs["Town"]) { echo " selected"; $TownFound++; } ?>>
                                        <?php print $linedistricts["title_en"]; ?>
                                    </option>
                                    <?php
                                }
                                ?>
                                <option
                                    value="other"<?php if (isset($line_job_jobs["Town"]) && $TownFound < 1) echo " selected"; ?>>
                                    Other
                                </option>
                            </select>
                            <?php
                        } else {
                            ?>
                            No districts added yet.
                            <?php
                        }
                        ?>*
                        <input class="input" name="othertown"
                               type="text" id="othertown"
                               value="<?php if (isset($line_job_jobs["Town"]) && $TownFound < 1) {
                                   echo $line_job_jobs["Town"];
                               } else { ?>other town<?php } ?>"
                               size="30" maxlength="50">
                        </span>
                    </span>
                            </td>
                        </tr>
                        <tr>
                            <td>Phone:</td>
                            <td valign="top"><input name="Phone"
                                                    type="text"
                                                    class="input"
                                                    value="<?php echo $line_job_jobs["Phone"]; ?>"
                                                    size="15"
                                                    maxlength="15">*
                            </td>
                        </tr>
                        <tr>
                            <td>Mobile:</td>
                            <td valign="top"><input name="Mobile"
                                                    type="text"
                                                    class="input"
                                                    value="<?php echo $line_job_jobs["Mobile"]; ?>"
                                                    size="15"
                                                    maxlength="15"></td>
                        </tr>
                        <tr>
                            <td>Fax:</td>
                            <td valign="top"><input name="Fax"
                                                    type="text"
                                                    class="input"
                                                    id="Fax"
                                                    value="<?php echo $line_job_jobs["Fax"]; ?>"
                                                    size="15"
                                                    maxlength="15"></td>
                        </tr>
                        <tr>
                            <td>Contact person:</td>
                            <td valign="top"><input name="ContactPerson"
                                                    type="text"
                                                    class="input"
                                                    id="ContactPerson"
                                                    value="<?php echo $line_job_jobs["ContactPerson"]; ?>"
                                                    size="30"
                                                    maxlength="100">*
                            </td>
                        </tr>
                        <tr>
                            <td>E-mail address:</td>
                            <td valign="top"><input name="EmailAdd"
                                                    type="text"
                                                    class="input"
                                                    id="EmailAdd"
                                                    value="<?php echo $line_job_jobs["EmailAdd"]; ?>"
                                                    size="30"
                                                    maxlength="100">
                            </td>
                        </tr>
                        <tr>
                            <td class="darkBlueBold">&nbsp;</td>
                            <td valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="darkBlueBold">Position details:
                            </td>
                            <td valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td>Job title :</td>
                            <td valign="top"><input name="JobTitle"
                                                    type="text"
                                                    class="input"
                                                    id="JobTitle"
                                                    value="<?php echo $line_job_jobs["JobTitle"]; ?>"
                                                    size="30"
                                                    maxlength="100">*
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Job description:</td>
                            <td valign="top"><textarea
                                    name="JobDescription" cols="50"
                                    rows="3" wrap="VIRTUAL"
                                    class="input"
                                    id="JobDescription"><?php echo $line_job_jobs["JobDescription"]; ?></textarea>*
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Number of persons
                                required:
                            </td>
                            <td valign="top"><input
                                    name="NumberOfPersons" type="text"
                                    class="input" id="NumberOfPersons"
                                    value="<?php echo $line_job_jobs["NumberOfPersons"]; ?>"
                                    size="3" maxlength="3"></td>
                        </tr>
                        <tr>
                            <td valign="top">Qualifications required:
                            </td>
                            <td valign="top"><textarea
                                    name="Qualifications" cols="50"
                                    rows="3" wrap="VIRTUAL"
                                    class="input"
                                    id="Qualifications"><?php echo $line_job_jobs["Qualifications"]; ?></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Other requirements:</td>
                            <td valign="top"><textarea name="Other"
                                                       cols="50"
                                                       rows="3"
                                                       wrap="VIRTUAL"
                                                       class="input"
                                                       id="Other"><?php echo $line_job_jobs["Other"]; ?></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Location(s):</td>
                            <td valign="top">
                                <?php
                                $querydistricts = "SELECT * FROM districts WHERE active='yes' ORDER BY order_seq ASC";
                                $resultdistricts = mysql_query($querydistricts);
                                if ($resultdistricts && mysql_num_rows($resultdistricts) > 0) {
                                    while ($linedistricts = mysql_fetch_assoc($resultdistricts)) {
                                        ?>
                                        <input type="checkbox"
                                               name="Locations[]"
                                               value="<?php echo $linedistricts["title_en"]; ?>" <?php if (strstr($line_job_jobs["Location"], $linedistricts["title_en"])) {
                                            echo " checked";
                                        } ?>>
                                        <?php print $linedistricts["title_en"]; ?>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    No districts added yet.
                                    <?php
                                }
                                ?> *
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Employment conditions:</td>
                            <td valign="top">
                                <select name="Conditions" class="input">
                                    <option
                                        value="full-time" <?php if ($line_job_jobs["Conditions"] == "full-time") {
                                        echo " selected";
                                    } ?>>Full Time
                                    </option>
                                    <option
                                        value="part-time" <?php if ($line_job_jobs["Conditions"] == "part-time") {
                                        echo " selected";
                                    } ?>>Part Time
                                    </option>
                                    <option
                                        value="contract" <?php if ($line_job_jobs["Conditions"] == "contract") {
                                        echo " selected";
                                    } ?>>Contract
                                    </option>
                                    <option
                                        value="temp" <?php if ($line_job_jobs["Conditions"] == "temp") {
                                        echo " selected";
                                    } ?>>Temporary
                                    </option>
                                    <option
                                        value="internship" <?php if ($line_job_jobs["Conditions"] == "internship") {
                                        echo " selected";
                                    } ?>>Internship
                                    </option>
                                    <option
                                        value="other" <?php if ($line_job_jobs["Conditions"] == "other") {
                                        echo " selected";
                                    } ?>>Other
                                    </option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center">
                                <input name="module_page" type="hidden"
                                       id="module_page" value="my_jobs">
                                <input type="hidden" name="jobs_url"
                                       value="jobs_add">
                                <input name="job_jobs_id" type="hidden"
                                       id="job_jobs_id"
                                       value="<?php echo $job_jobs_id; ?>">
                                <?php
                                if ($job_jobs_id > 0 && mysql_num_rows($result1) > 0) {
                                    ?>
                                    <input type="radio" name="chcAction"
                                           value="Edit" checked> Edit
                                    <input type="radio" name="chcAction"
                                           value="Add"> Add New
                                    <input type="radio" name="chcAction"
                                           value="Delete"
                                           onClick="Confirm_delete();">Delete |
                                    <input type="submit" name="Submit2"
                                           value="Submit">
                                    <?php
                                } else {
                                    ?>
                                    <input type="submit" name="Submit22"
                                           value="Add">
                                    <input name="chcAction"
                                           type="hidden" id="chcAction"
                                           value="Add">
                                    <?php
                                }
                                ?>
                            </td>
                        </tr>
                    </table>
                </form>

        </div>
    </div>
    <?php
}

?>

<script type="text/javascript">
    function Confirm_delete() {
        alert("ATTENTION!! If you press Submit, your Posted Job will be DELETED.")
    }
    function checkFields() {
        missinginfo = "";
        if (document.form_job_jobs.job_cat_id.value == "") {
            missinginfo += "\n     -  Job Category";
        }
        if (document.form_job_jobs.CompanyName.value == "") {
            missinginfo += "\n     -  Company Name";
        }
        if (document.form_job_jobs.Address.value == "") {
            missinginfo += "\n     -  Address";
        }
        if (document.form_job_jobs.Town.value == "") {
            missinginfo += "\n     -  Town";
        }
        if (document.form_job_jobs.Town.value == "other" && (document.form_job_jobs.othertown.value == "other town" || document.form_job_jobs.othertown.value == "")) {
            missinginfo += "\n     -  Other Town";
        }
        if (document.form_job_jobs.Phone.value == "") {
            missinginfo += "\n     -  Phone";
        }
        if (document.form_job_jobs.ContactPerson.value == "") {
            missinginfo += "\n     -  Contact Person";
        }
        if (document.form_job_jobs.JobTitle.value == "") {
            missinginfo += "\n     -  Job Title";
        }
        if (document.form_job_jobs.JobDescription.value == "") {
            missinginfo += "\n     -  Job Description";
        }


        if (missinginfo != "") {
            missinginfo = "_____________________________\n" +
                "You failed to correctly fill in your:\n" +
                missinginfo + "\n_____________________________" +
                "\nPlease re-enter and submit again!";
            alert(missinginfo);
            return false;
        }
        else
            return true;
    }
</script>