<?php
include('../config/config.php');
include(ADMIN_HTML.'html.inc.php');
include(ADMIN_INCLUDES.'functions.php');
checkLoginAdmin();

if ( isset($_POST['submit']) )
{
	$fileName = time().str_replace(' ', '_', $_FILES['partyImage']['name']); // replace the space with '_'
	move_uploaded_file($_FILES['partyImage']['tmp_name'],UPLOADS_PATH.'partylogo/'.$fileName);
	
	// Generate Images thunmnails		
	generate_image_thumbnail(UPLOADS_PATH.'partylogo/'.$fileName, UPLOADS_PATH.'partylogo/thumbs/thumb_'.$fileName,80,60);
	
	$insQry = "INSERT INTO tbl_candidate
			SET
				Name = '".clean($_POST['candi_title'])."',
				Description = '".clean($_POST['candiDes'])."',
				Party = '".clean($_POST['party_title'])."',
				Logo = '".$fileName."'";
				
	mysql_query($insQry);
	
	$redirectUrl = 'view-candidates.php';
	$_SESSION['succesMessage'] = 4;
	header("Location: $redirectUrl");
}

startHtml($title = "Add Candidate");
tophead($title);
leftNav();

?>

<script type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "simple",
		// update validation status on change
		onchange_callback: function(editor) {
			tinyMCE.triggerSave();
			$("#" + editor.id).valid();
		}
	});
	
	jQuery.validator.addMethod("imageExt", function(value, element) { 
		return this.optional(element) || /^.*\.(jpg|jpeg|gif|JPG|png|PNG)$/.test(value);
	 }, ("Only [jpeg, gif, JPG, PNG] formats are allowed."));
	 
	// Form validation
	$(function() {
		var validator = $("#addCandiForm").submit(function() {
			// update underlying textarea before submit validation
			tinyMCE.triggerSave();
		}).validate({
			ignore: "",
			rules: {
				candi_title: "required",
				candiDes: "required",
				party_title: "required",
				partyImage: {
					required: true,
					imageExt: true
				}
			},
			messages: {
				candi_title: "Enter candidate name.",
				candiDes: "Enter candidate description.",
				party_title: "Enter party name",
				partyImage: {
					required: 'Select Party image.'
				}
				
			},
			errorPlacement: function(label, element) {
				// position error label after generated textarea
				if (element.is("textarea")) {
					label.insertAfter(element.next());
				} else {
					label.insertAfter(element)
				}
			}
		});
		validator.focusInvalid = function() {
			// put focus on tinymce on submit validation
			if( this.settings.focusInvalid ) {
				try {
					var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []);
					if (toFocus.is("textarea")) {
						tinyMCE.get(toFocus.attr("id")).focus();
					} else {
						toFocus.filter(":visible").focus();
					}
				} catch(e) {
					// ignore IE throwing errors when focusing hidden elements
				}
			}
		}
	});
</script>

<section id="main" class="column">
    <article class="module width_full">
        <header>
            <h3>Add Candidate</h3>
        </header>
        <form name="addCandiForm" id="addCandiForm" action="" method="post" enctype="multipart/form-data">
            <fieldset style="margin:15px">
            	<table width="100%">
                	<tr>
                    	<td>
                            <label>Candidate Name</label>
                            <input type="text" name="candi_title" id="candi_title" />
                        </td>
                    </tr>
                    <tr>
                    	<td>
                            <label>Candidate Party</label>
                            <input type="text" name="party_title" id="party_title" />
                        </td>
                    </tr>
                	<tr>
                    	<td>
                        	<label>Description</label>
			                <span class="tinyMCE"><textarea id="candiDes" name="candiDes" rows="15" cols="80" style="width: 99%;float:left;"></textarea></span>
                            <label for="candiDes" class="error" style="display:none;">Enter description.</label>
                        </td>
                    </tr>
                	<tr>
                    	<td>
                        	<label>Party Logo</label>
                			<input type="file" name="partyImage" id="partyImage" />
                        </td>
                    </tr>
                    <tr>
                    	<td>
                        	<input type="submit" name="submit" id="submit" value="Add Record" />
                    		<input type="button" name="cancel" id="cancel" value="Cancel" onclick="window.location='<?php echo ADMIN_URL;?>view-candidates.php'" />
                        </td>
                    </tr>
                </table>
            </fieldset>
        </form>
    </article>
    <div class="spacer"></div>
</section>

<?php
endHtml();
?>