<?php 
//include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view_ess";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "ess";
$id	 = "";
$query_string = "";
$page_name      = "ESS Imperial Form";
$from = $_REQUEST['from'];
$sample_and_provisional_1 = "SAMPLE & PROVISIONAL";
$sample_and_provisional_2 = "SAMPLE & PROVISIONAL";
$label_box_1 = "PACKAGE CONTENT";
$label_box_2 = "EXTRA INFORMATION";
$label_box_3 = "TRAVEL";
$label_box_4 = "YOUR ROLE AS HOST PRIOR TO THE EVENT";
$label_time_details = "EVENT ITINERARY";
$manage_color = "#999999";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string","time","details","email"));
	
	$template_id            = $_REQUEST['template_id'];
	$unique_reference 		= $_REQUEST['unique_reference'];
	$title 			        = $_REQUEST['title'];
	$url                  	= $siteURL.'/'."pdfs".'/'.$title.'.'.pdf;
	$ess_created_by 	    = $_REQUEST['ess_created_by'];
	$event_duration_from  	= utility::setFormatedDate($_REQUEST['event_duration_from']);
	$event_duration_to 	    = utility::setFormatedDate($_REQUEST['event_duration_to']);
	$booking_duration_from  = utility::setFormatedDate($_REQUEST['booking_duration_from']);
	$booking_duration_to 	= utility::setFormatedDate($_REQUEST['booking_duration_to']);
    
    $page_title				= $_REQUEST['page_title'];
	$description			= $_REQUEST['description'];
	$sample_and_provisional_1 =$_REQUEST['sample_and_provisional_1'];
	$sample_and_provisional_2 =$_REQUEST['sample_and_provisional_2'];
	
	$label_box_1		    = $_REQUEST['label_box_1'];
    $detail_box_1		    = $_REQUEST['detail_box_1'];
	$label_box_2		    = $_REQUEST['label_box_2'];
    $detail_box_2		    = $_REQUEST['detail_box_2'];
	$label_box_3		    = $_REQUEST['label_box_3'];
    $detail_box_3		    = $_REQUEST['detail_box_3'];
	$label_box_4		    = $_REQUEST['label_box_4'];
    $detail_box_4		    = $_REQUEST['detail_box_4'];
	$manage_color	    	= $_REQUEST['manage_color'];
	$label_time_details	    = $_REQUEST['label_time_details'];
	$time		            = $_REQUEST['time'];
	$details			    = $_REQUEST['details'];
    $email 	    			= $_REQUEST['email'];
    $two_page_pdf 	    	= $_REQUEST['two_page_pdf'];
    $category 	    	    = $_REQUEST['category'];
	$category_name_new 		= $_REQUEST['category_name_new'];
	$tem_img_1              = $_REQUEST['tem_img_1'];
	$tem_img_2              = $_REQUEST['tem_img_2'];
	$tem_img_3              = $_REQUEST['tem_img_3'];
	$tem_img_4              = $_REQUEST['tem_img_4'];
	if(isset($category_name_new) && $category_name_new !=""){
		$max_order = Keycode::getMaxKeycodeOrderByName("ess_category") + 1;
		$sql1= "INSERT INTO keycode(name,code,ord) VALUES('ess_category','$category_name_new','$max_order')";
		mysqli_query($conn,$sql1);
		$category = mysqli_insert_id($conn);
	}
    
	$created_on = date("Y-m-d H:i:s");
	$created_by = $_SESSION["user_id"];
    $updated_by = $_SESSION["user_id"];
	$updated_on = date("Y-m-d H:i:s");
	
	$sql= "INSERT INTO $table_name (template_id,unique_reference,title,url,ess_created_by,event_duration_from,event_duration_to,booking_duration_from,booking_duration_to,page_title,description,sample_and_provisional_1,sample_and_provisional_2,label_box_1,detail_box_1,label_box_2,detail_box_2,label_box_3,detail_box_3,label_box_4,detail_box_4,colour,label_time_details,category,two_page_pdf,created_on,created_by,updated_by,updated_on) VALUES('$template_id','$unique_reference','$title','$url','$ess_created_by','$event_duration_from','$event_duration_to','$booking_duration_from','$booking_duration_to','$page_title','$description','$sample_and_provisional_1','$sample_and_provisional_2','$label_box_1','$detail_box_1','$label_box_2','$detail_box_2','$label_box_3','$detail_box_3','$label_box_4','$detail_box_4','$manage_color','$label_time_details','$category','$two_page_pdf','$created_on',
	'$created_by','$updated_by','$updated_on')";
	mysqli_query($conn,$sql);
	$ess_id = mysqli_insert_id($conn);
	
	if(count($_REQUEST['time'])>0){	
		foreach($_REQUEST['time'] as $key => $time){
			$time	= $_REQUEST['time'][$key];
			$details	= $_REQUEST['details'][$key];	
            if($time!="" ){
                $sql= "INSERT INTO ess_event_itinerary_mapping (ess_id,time,details) VALUES('$ess_id','$time','$details')";
				mysqli_query($conn,$sql);
			}
		}
	}
	if(count($_REQUEST['email'])>0){	
		foreach($_REQUEST['email'] as $key => $email){
			$email		= $_REQUEST['email'][$key];			
			if($email!=""){
                $email=utility::app_crypt($email,'e');
				$sql= "INSERT INTO ess_email_mapping (ess_id,email) VALUES('$ess_id','$email')";
				mysqli_query($conn,$sql);
			}
		}
	}
	
	if(isset($_FILES['ess_image_1']['name']) && $_FILES['ess_image_1']['name'] !=""){
		$path = $_FILES['ess_image_1']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		//if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG"){
			
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$ess_image_1_name_array = explode(".",$_FILES['ess_image_1']['name']);
			
			$ess_1_ext = array_pop($ess_image_1_name_array);
			$doc_name_1 = implode('.', $ess_image_1_name_array);
			$ess_image_1_name = $ess_id."_".$doc_name_1."_".$random_number.".".$ess_1_ext;
			$ess_image_1_name = str_replace(" ","",$ess_image_1_name);
			$uploadfile = $uploaddir . $ess_image_1_name;
			move_uploaded_file($_FILES['ess_image_1']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET ess_image_1 = '$ess_image_1_name' WHERE id = $ess_id ";
			mysqli_query($conn,$sql);
			
			$tmp = ucfirst(str_replace("_"," ", $table_name));
		/* }else{
			$message = "Please upload only png or jpg lift_image";
		} */
    }else{
		$ess_image_1_name = $tem_img_1;
		$sql= "UPDATE $table_name SET ess_image_1 = '$ess_image_1_name' WHERE id = $ess_id ";
		mysqli_query($conn,$sql);
	}
	if(isset($_FILES['ess_image_2']['name']) && $_FILES['ess_image_2']['name'] !=""){
		$path = $_FILES['ess_image_2']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		//if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG"){
			
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$ess_image_2_name_array = explode(".",$_FILES['ess_image_2']['name']);
			
			$ess_2_ext = array_pop($ess_image_2_name_array);
			$doc_name_2 = implode('.', $ess_image_2_name_array);
			$ess_image_2_name = $ess_id."_".$doc_name_2."_".$random_number.".".$ess_2_ext;
			$ess_image_2_name = str_replace(" ","",$ess_image_2_name);
			$uploadfile = $uploaddir . $ess_image_2_name;
			move_uploaded_file($_FILES['ess_image_2']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET ess_image_2 = '$ess_image_2_name' WHERE id = $ess_id ";
			mysqli_query($conn,$sql);
			
			$tmp = ucfirst(str_replace("_"," ", $table_name));
		/* }else{
			$message = "Please upload only png or jpg lift_image";
		} */
    }else{
		$ess_image_2_name = $tem_img_2;
		$sql= "UPDATE $table_name SET ess_image_2 = '$ess_image_2_name' WHERE id = $ess_id ";
		mysqli_query($conn,$sql);
	}
	if(isset($_FILES['ess_image_3']['name']) && $_FILES['ess_image_3']['name'] !=""){
		$path = $_FILES['ess_image_3']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		//if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG"){
			
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$ess_image_3_name_array = explode(".",$_FILES['ess_image_3']['name']);
			
			$ess_3_ext = array_pop($ess_image_3_name_array);
			$doc_name_3 = implode('.', $ess_image_3_name_array);
			$ess_image_3_name = $ess_id."_".$doc_name_3."_".$random_number.".".$ess_3_ext;
			$ess_image_3_name = str_replace(" ","",$ess_image_3_name);
			$uploadfile = $uploaddir . $ess_image_3_name;
			move_uploaded_file($_FILES['ess_image_3']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET ess_image_3 = '$ess_image_3_name' WHERE id = $ess_id ";
			mysqli_query($conn,$sql);
			
			//$tmp = ucfirst(str_replace("_"," ", $table_name));
		/* }else{
			$message = "Please upload only png or jpg lift_image";
		} */
    }else{
		$ess_image_3_name = $tem_img_3;
		$sql= "UPDATE $table_name SET ess_image_3 = '$ess_image_3_name' WHERE id = $ess_id ";
		mysqli_query($conn,$sql);
	}
	if(isset($_FILES['ess_image_4']['name']) && $_FILES['ess_image_4']['name'] !=""){
		$path = $_FILES['ess_image_4']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		//if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "JPG" || $ext == "JPEG" || $ext == "PNG"){
			
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$ess_image_4_name_array = explode(".",$_FILES['ess_image_4']['name']);
			
			$ess_4_ext = array_pop($ess_image_4_name_array);
			$doc_name_4 = implode('.', $ess_image_4_name_array);
			$ess_image_4_name = $ess_id."_".$doc_name_4."_".$random_number.".".$ess_4_ext;
			$ess_image_4_name = str_replace(" ","",$ess_image_4_name);
			$uploadfile = $uploaddir . $ess_image_4_name;
			move_uploaded_file($_FILES['ess_image_4']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET ess_image_4 = '$ess_image_4_name' WHERE id = $ess_id ";
			mysqli_query($conn,$sql);
			
			//$tmp = ucfirst(str_replace("_"," ", $table_name));
		/* }else{
			$message = "Please upload only png or jpg lift_image";
		} */
    }else{
		$ess_image_4_name = $tem_img_4;
		$sql= "UPDATE $table_name SET ess_image_4 = '$ess_image_4_name' WHERE id = $ess_id ";
		mysqli_query($conn,$sql);
	}
	$_SESSION['info_message'] = "Record added sucessfully";
	header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	$preview_pdf    = "page=view_ess_pdf&id=$id&hide_top=1&hide_left=1";
	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit ESS";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);
    $row_edit=utility::validate_crypt_data($row_edit);
    //$company_name= $row_edit['company_name'];
	$unique_reference 		= $row_edit['unique_reference'];
	$title 			        = $row_edit['title'];
	$url                  	= $row_edit['url'];
	$ess_created_by         = $row_edit['ess_created_by'];
	$event_duration_from  	= $row_edit['event_duration_from'];
	$event_duration_to 	    = $row_edit['event_duration_to'];
	$booking_duration_from  = $row_edit['booking_duration_from'];
	$booking_duration_to 	= $row_edit['booking_duration_to'];
    
    $page_title				= $row_edit['page_title'];
	$description			= $row_edit['description'];
	$sample_and_provisional_1 =$row_edit['sample_and_provisional_1'];
	$sample_and_provisional_2 =$row_edit['sample_and_provisional_2'];
    $label_box_1		    = $row_edit['label_box_1'];
    $detail_box_1		    = $row_edit['detail_box_1'];
	$label_box_2		    = $row_edit['label_box_2'];
    $detail_box_2		    = $row_edit['detail_box_2'];
	$label_box_3		    = $row_edit['label_box_3'];
    $detail_box_3		    = $row_edit['detail_box_3'];
	$label_box_4		    = $row_edit['label_box_4'];
    $detail_box_4		    = $row_edit['detail_box_4'];
	$manage_color	    	= $row_edit['colour'];
	$label_time_details		= $row_edit['label_time_details'];
	$category	        	= $row_edit['category'];
	$two_page_pdf	        = $row_edit['two_page_pdf'];
	
	$ess_image_1= $row_edit['ess_image_1'];
	$ess_image_2 = $row_edit['ess_image_2'];
	$ess_image_3 = $row_edit['ess_image_3'];
	$ess_image_4 = $row_edit['ess_image_4'];
	

}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","time","details","ess_event_itinerary_mapping_id","email","ess_email_mapping_id"));
	$id= $_REQUEST['id'];
	$unique_reference 		= $_REQUEST['unique_reference'];
	$title 			        = $_REQUEST['title'];
	$url                  	= $siteURL.'/'."pdfs".'/'.$title.'.'.pdf;
	$ess_created_by 	    = $_REQUEST['ess_created_by'];
	$event_duration_from  	= utility::setFormatedDate($_REQUEST['event_duration_from']);
	$event_duration_to 	    = utility::setFormatedDate($_REQUEST['event_duration_to']);
	$booking_duration_from  = utility::setFormatedDate($_REQUEST['booking_duration_from']);
	$booking_duration_to 	= utility::setFormatedDate($_REQUEST['booking_duration_to']);
    
    $page_title				= $_REQUEST['page_title'];
	$description			= $_REQUEST['description'];
	$sample_and_provisional_1 =$_REQUEST['sample_and_provisional_1'];
	$sample_and_provisional_2 =$_REQUEST['sample_and_provisional_2'];
	$label_box_1		    = $_REQUEST['label_box_1'];
    $detail_box_1		    = $_REQUEST['detail_box_1'];
	$label_box_2		    = $_REQUEST['label_box_2'];
    $detail_box_2		    = $_REQUEST['detail_box_2'];
	$label_box_3		    = $_REQUEST['label_box_3'];
    $detail_box_3		    = $_REQUEST['detail_box_3'];
	$label_box_4		    = $_REQUEST['label_box_4'];
    $detail_box_4		    = $_REQUEST['detail_box_4'];
	$manage_color	    	= $_REQUEST['manage_color'];
	$label_time_details		= $_REQUEST['label_time_details'];
	$time		            = $_REQUEST['time'];
	$details			    = $_REQUEST['details'];
    $email 	    			= $_REQUEST['email'];
    $two_page_pdf 	    			= $_REQUEST['two_page_pdf'];
	$category 	    	    = $_REQUEST['category'];
	$category_name_new 		= $_REQUEST['category_name_new'];
	if(isset($category_name_new) && $category_name_new !=""){
		$max_order = Keycode::getMaxKeycodeOrderByName("ess_category") + 1;
		$sql1= "INSERT INTO keycode(name,code,ord) VALUES('ess_category','$category_name_new','$max_order')";
		mysqli_query($conn,$sql1);
		$category = mysqli_insert_id($conn);
	}

    $updated_by = $_SESSION["user_id"];
	
	$hidden_ess_image_1 	= $_REQUEST['hidden_ess_image_1'];
	if(isset($_FILES['ess_image_1']['name']) && $_FILES['ess_image_1']['name'] !=""){
		$path = $_FILES['ess_image_1']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$old_ess_image_1	=	$uploaddir.$hidden_ess_image_1;	
			if(file_exists($old_ess_image_1)){
				unlink($old_ess_image_1);
			}
			$ess_image_1_name_array = explode(".",$_FILES['ess_image_1']['name']);
			$document_ext = array_pop($ess_image_1_name_array);
			$doc_name = implode('.', $ess_image_1_name_array);
			$ess_image_1_name = $id."_".$doc_name."_".$random_number.".".$document_ext;
			
			$uploadfile = $uploaddir . $ess_image_1_name;
			move_uploaded_file($_FILES['ess_image_1']['tmp_name'], $uploadfile);
		 }else{
			$ess_image_1_name = $hidden_ess_image_1;
		} 
	
	$hidden_ess_image_2 	= $_REQUEST['hidden_ess_image_2'];
	if(isset($_FILES['ess_image_2']['name']) && $_FILES['ess_image_2']['name'] !=""){
		$path = $_FILES['ess_image_2']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$old_ess_image_2	=	$uploaddir.$hidden_ess_image_2;	
			if(file_exists($old_ess_image_2)){
				unlink($old_ess_image_2);
			}
			$ess_image_2_name_array = explode(".",$_FILES['ess_image_2']['name']);
			$document_ext = array_pop($ess_image_2_name_array);
			$doc_name = implode('.', $ess_image_2_name_array);
			$ess_image_2_name = $id."_".$doc_name."_".$random_number.".".$document_ext;
			
			$uploadfile = $uploaddir . $ess_image_2_name;
			move_uploaded_file($_FILES['ess_image_2']['tmp_name'], $uploadfile);
		 }else{
			$ess_image_2_name = $hidden_ess_image_2;
		} 
	$hidden_ess_image_3 	= $_REQUEST['hidden_ess_image_3'];
	if(isset($_FILES['ess_image_3']['name']) && $_FILES['ess_image_3']['name'] !=""){
		$path = $_FILES['ess_image_3']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$old_ess_image_3  =	$uploaddir.$hidden_ess_image_3;	
			if(file_exists($old_ess_image_3)){
				unlink($old_ess_image_3);
			}
			$ess_image_3_name_array = explode(".",$_FILES['ess_image_3']['name']);
			$document_ext = array_pop($ess_image_3_name_array);
			$doc_name = implode('.', $ess_image_3_name_array);
			$ess_image_3_name = $id."_".$doc_name."_".$random_number.".".$document_ext;
			
			$uploadfile = $uploaddir . $ess_image_3_name;
			move_uploaded_file($_FILES['ess_image_3']['tmp_name'], $uploadfile);
		 }else{
			$ess_image_3_name = $hidden_ess_image_3;
		} 
	$hidden_ess_image_4 	= $_REQUEST['hidden_ess_image_4'];
	if(isset($_FILES['ess_image_4']['name']) && $_FILES['ess_image_4']['name'] !=""){
		$path = $_FILES['ess_image_4']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../ess_images/';
			$old_ess_image_4	=	$uploaddir.$hidden_ess_image_4;	
			if(file_exists($old_ess_image_4)){
				unlink($old_ess_image_4);
			}
			$ess_image_4_name_array = explode(".",$_FILES['ess_image_4']['name']);
			$document_ext = array_pop($ess_image_4_name_array);
			$doc_name = implode('.', $ess_image_4_name_array);
			$ess_image_4_name = $id."_".$doc_name."_".$random_number.".".$document_ext;
			
			$uploadfile = $uploaddir . $ess_image_4_name;
			move_uploaded_file($_FILES['ess_image_4']['tmp_name'], $uploadfile);
		 }else{
			$ess_image_4_name = $hidden_ess_image_4;
		} 
	
    $sql= "UPDATE $table_name SET unique_reference = '$unique_reference',title = '$title',url = '$url',ess_created_by='$ess_created_by',event_duration_from = '$event_duration_from',event_duration_to = '$event_duration_to',booking_duration_from = '$booking_duration_from',booking_duration_to = '$booking_duration_to', ess_image_1 = '$ess_image_1_name', ess_image_2 = '$ess_image_2_name', ess_image_3 = '$ess_image_3_name',ess_image_4 = '$ess_image_4_name',page_title = '$page_title', description = '$description',sample_and_provisional_1='$sample_and_provisional_1',sample_and_provisional_2='$sample_and_provisional_2',label_box_1 = '$label_box_1',detail_box_1 ='$detail_box_1',label_box_2 = '$label_box_2',detail_box_2 = '$detail_box_2',label_box_3 = '$label_box_3',detail_box_3 = '$detail_box_3',label_box_4 = '$label_box_4',detail_box_4 = '$detail_box_4',colour='$manage_color', label_time_details = '$label_time_details',category = '$category',two_page_pdf='$two_page_pdf',updated_by = '$updated_by' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	if(count($_REQUEST['time'])>0){	
		foreach($_REQUEST['time'] as $key => $time){
			if($time!=""){
			$details		= $_REQUEST['details'][$key];			
			$ess_event_itinerary_mapping_id	= $_REQUEST['ess_event_itinerary_mapping_id'][$key];			
			
				if(mysqli_num_rows(mysqli_query($conn,"SELECT * FROM ess_event_itinerary_mapping WHERE id = '$ess_event_itinerary_mapping_id' ")) > 0){
					$sql2= "UPDATE ess_event_itinerary_mapping SET time = '$time',details = '$details' WHERE id = '$ess_event_itinerary_mapping_id' ";
				}else{
					$sql2= "INSERT INTO ess_event_itinerary_mapping (ess_id,time,details) VALUES('$id','$time','$details')";
				}
				mysqli_query($conn,$sql2);
			}
		}
	}
	if(count($_REQUEST['email'])>0){	
		foreach($_REQUEST['email'] as $key => $email){
			if($email!=""){
			//$details		= $_REQUEST['details'][$key];			
			$ess_email_mapping_id	= $_REQUEST['ess_email_mapping_id'][$key];			
				$email=utility::app_crypt($email,'e');
				if(mysqli_num_rows(mysqli_query($conn,"SELECT * FROM ess_email_mapping WHERE id = '$ess_email_mapping_id' ")) > 0){
					$sql2= "UPDATE ess_email_mapping SET email = '$email' WHERE id = '$ess_email_mapping_id' ";
				}else{
					$sql2= "INSERT INTO ess_email_mapping (ess_id,email) VALUES('$id','$email')";
				}
				mysqli_query($conn,$sql2);
			}
		}
	}

	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add_ess", "page=view_ess", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "Details updated sucessfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
$tem_id = isset($_REQUEST['template_id']) ? $_REQUEST['template_id'] : "";
if($tem_id) {
$tem_row = utility::getTableRowByField("ess_image_template","id",$tem_id);
}
?>
<style>
.custom-trash{
	font-size:20px;
	cursor:pointer;
}
</style>
<!---------------------------------- cloning start for Phone --------------------------------------------->
	<div id="phone_div_to_clone" style="display:none;">
		<div class="form-group" > 
			<label class="col-sm-2 control-label" for="Phone"></label> 
			<div class="col-sm-5"> 
				<input type="text" id="phone" name="phone[]" class="form-control phone" value="<?php echo $phone;?>" placeholder="Phone"> 
			</div>
			<div class="col-sm-4"> 
				<? //utility::getPhoneType("phone_type[]", $phone_type,"100%",false,"form-control  phone_type"); ?> 
			</div>
			<div class="col-sm-1 custom-trash">
				<span class="fa fa-trash delete_phone"></span>
			</div>
		</div>
	</div>
<!----------------------------------- Cloning end for Phone  --------------------------------------------->
<!---------------------------------- cloning start for email --------------------------------------------->
	<div id="email_div_to_clone" style="display:none;">
		<div class="form-group" > 
			<label class="col-sm-2 control-label" for="email">Email</label> 
			<div class="col-sm-8"> 
				<input type="text" id="" name="email[]" class="form-control" value="" placeholder="Email" autocomplete="off"> 
			</div>

			<div class="col-sm-2 custom-trash">
				<span class="fa fa-trash delete_email" alt=""></span>
			</div>
		</div>
	</div>
<!----------------------------------- Cloning end for email  --------------------------------------------->
<!-------------------------- cloning start for parts required ------------------------->
<div id="event_itinerary_div_to_clone" style="display:none;">
	<div class="form-group" > 
		<div class="col-sm-4"> 
			<label class="control-label" for="HJS Part No">Time</label> 
			<input type="text" id="time" name="time[]" class="form-control" value="" placeholder="TIME"> 
		</div>
		
		<div class="col-sm-6"> 
			<label class="control-label" for="details">Details</label> 
			<input type="text" id="details" name="details[]" class="form-control" value="" placeholder="DETAILS"> 						
		</div>
		<div class="col-sm-1 custom-trash">
			<label class="control-label" for="details">&nbsp;</label> 
			<span class="fa fa-trash delete_event_itinerary" alt=""></span>
		</div>
	</div>
</div>
<!----------------------- Cloning end for parts required ------------------------------>

<section class="content-header">
  <h1>
	<?=$page_name;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['message']; unset($_SESSION['info_message']);?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<!-- general form elements -->
              <div class="box box-primary">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add_ess&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="box-body">
				  <div class="col-lg-9 col-md-9 col-sm-12">
				  <? if(!(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit')){?>
				    <div class="form-group">
						<label class="col-sm-2 control-label" for="select">Use Template Image<em></em></label>
						<div class="col-sm-10" style="margin-top:7px;">
							<b>No</b> <input type="radio" name="use"  value="0" class="flat-red use_tem_img" checked>
							<b>YES</b> <input type="radio" name="use" alt="" id="" value="1" class="flat-red use_tem_img">
						</div>
					</div>
				    <div class="form-group" id="tem_img_dd" style="display:none;"> 
						<label class="col-sm-2 control-label" for="Organisation">Image Template</label> 
						<div class="col-sm-10"> 
							<? $gsi->getESSImgTemplateList("template_id",$template_id,"100%",false,"form-control select2","required"); ?>
							<i class="fa fa-refresh fa-spin" style="display:none;" id="select_template_load"></i>
						</div> 
					</div>
				  <? }?>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="unique">Unique Reference </label> 
						<div class="col-sm-10"> 
							<input type="text" id="unique_reference" name="unique_reference" class="form-control" value="<?php echo $unique_reference;?>" placeholder="Unique Reference (NOT SHOWN ON PDF )" required> 
						</div> 
					</div>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="title">Title</label> 
						<div class="col-sm-10"> 
							<input type="text" id="title" name="title" class="form-control" value="<?php echo $title;?>" placeholder="Title" required> 
						</div> 
					</div>
					
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="url">URL</label> 
						<div class="col-sm-10"> 
							<input type="text" id="url" name="url" class="form-control" value="<?php echo $url;?>" placeholder="URL" readonly> 
						</div> 
					</div>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="title">Created BY :</label> 
						<div class="col-sm-10"> 
							<input type="text" id="ess_created_by" name="ess_created_by" class="form-control" value="<?php echo $ess_created_by;?>" placeholder="Created By"> 
						</div> 
					</div>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="event_duration">Event Duration</label>
							<div class="col-sm-5"> 
								<input type="text" id="event_duration_from" name="event_duration_from" class="form-control datepicker" value="<?php (isset($event_duration_from) && $event_duration_from !="") ? utility::getFormatedDate($event_duration_from) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" >
							</div> 
							<div class="col-sm-5"> 
								<input type="text" id="event_duration_to" name="event_duration_to" class="form-control datepicker" value="<?php (isset($event_duration_to) && $event_duration_to !="") ? utility::getFormatedDate($event_duration_to) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" >
							</div>
					</div>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="event_duration">Booking Duration</label> 
						<div class="col-sm-5"> 
							<input type="text" id="booking_duration_from" name="booking_duration_from" class="form-control datepicker" value="<?php (isset($booking_duration_from) && $booking_duration_from !="") ? utility::getFormatedDate($booking_duration_from) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" >
						</div> 
						<div class="col-sm-5"> 
							<input type="text" id="event_duration_to" name="booking_duration_to" class="form-control datepicker" value="<?php (isset($booking_duration_to) && $booking_duration_to !="") ? utility::getFormatedDate($booking_duration_to) : utility::getFormatedDate(date($config->dateFormat));?>" placeholder="DD-MM-YYYY" >
						</div>	
					</div><br/>
					<div class="form-group">
						<label class="col-sm-2 control-label" for=""></label> 
						<div class="col-sm-10" style="padding:0;">
							<div class="col-sm-3">
							    <? $dir = "../../ess_images/";
									if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){ 
										 if($ess_image_1 !=''){
										   $img1=$ess_image_1;
									   }else{
										   $img1="blank_imag.png";
									   }
									?>
										<img id="frame1" src="<?= $dir.$img1;?>" width="100px" height="100px" style="" />
									<? }else {?>
										<img id="frame1" src="../../ess_images/blank_imag.png" width="100px" height="100px" style="" />
									<? }?>
								    <input type="file" value="<?=$tem_row['ess_image_1'];?>" onchange="preview1()" name="ess_image_1" id="ess_image_1" accept="image/*" style="color:transparent;" />
									<input type="button" id="delete1" value="Delete File" />
									<i class="fa fa-refresh fa-spin" style="display:none;" id="delete_load_1"></i>
									<input type="hidden" id="hid1" name="hidden_ess_image_1" value="<? echo $ess_image_1;?>">
							</div>
							<div class="col-sm-3">
							<? $dir = "../../ess_images/";
									if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){ 
									   if($ess_image_2 !=''){
										   $img2=$ess_image_2;
									   }else{
										   $img2="blank_imag.png";
									   }
									?>
										<img id="frame2" src="<?= $dir.$img2;?>" width="100px" height="100px" style="" />
									<? }else{?>
										<img id="frame2" src="../../ess_images/blank_imag.png" width="100px" height="100px" style="" />
									<? }?>
									<input type="file" onchange="preview2()" name="ess_image_2" id="ess_image_2" accept="image/*" style="color:transparent;" />
									<input type="button" id="delete2" value="Delete File" />
									<i class="fa fa-refresh fa-spin" style="display:none;" id="delete_load_2"></i>
									<input type="hidden" id="hid2" name="hidden_ess_image_2" value="<? echo $ess_image_2;?>">
							</div>
							<div class="col-sm-3">
							<? $dir = "../../ess_images/";
									if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){ 
										 if($ess_image_3 !=''){
										   $img3=$ess_image_3;
									   }else{
										   $img3="blank_imag.png";
									   }
									?>
										<img id="frame3" src="<?= $dir.$img3;?>" width="100px" height="100px" style="" />
									<? }else{?>
							          <img id="frame3" src="../../ess_images/blank_imag.png" width="100px" height="100px" style="" />
									<? }?>  
								     <input type="file" onchange="preview3()" name="ess_image_3" id="ess_image_3" accept="image/*" style="color:transparent;" />
									 <input type="button" id="delete3" value="Delete File" />
									 <i class="fa fa-refresh fa-spin" style="display:none;" id="delete_load_3"></i>
									<input type="hidden" id="hid3" name="hidden_ess_image_3" value="<? echo $ess_image_3;?>">
							</div>
							<div class="col-sm-3">
							<? $dir = "../../ess_images/";
									if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){ 
										 if($ess_image_4 !=''){
										   $img4=$ess_image_4;
									   }else{
										   $img4="blank_imag.png";
									   }
									?>
										<img id="frame4" src="<?= $dir.$img4;?>" width="100px" height="100px" style="" />
									<? }else{?>
							          <img id="frame4" src="../../ess_images/blank_imag.png" width="100px" height="100px" style="" />
									<? }?>  
								    <input type="file" onchange="preview4()" name="ess_image_4" id="ess_image_4" accept="image/*" style="color:transparent;" />
									<input type="button" id="delete4" value="Delete File" />
									<i class="fa fa-refresh fa-spin" style="display:none;" id="delete_load_4"></i>
									<input type="hidden" id="hid4" name="hidden_ess_image_4" value="<? echo $ess_image_4;?>">
							</div>
						</div>
					</div>
					<div class="form-group"> 
						<label class="col-sm-2 control-label" for="page_title">Page Title</label> 
						<div class="col-sm-10"> 
							<input type="text" id="page_title" name="page_title" class="form-control" value="<?php echo $page_title;?>" placeholder="Page Title" required> 
						</div> 
					</div>
					<div class="form-group">
                      <label for="" class="col-sm-2 control-label"></label>
                      <div class="col-sm-10">
						<textarea class="form-control" id="description" name="description" rows="2" placeholder=""><?php echo $description;?></textarea>
                      </div>
                    </div>
					<div class="form-group">
					 <label class="col-sm-2 control-label" for=""></label> 
					<div class="col-sm-10" style="padding:0;">
						<div class="col-md-6">
							<input type="text" id="" name="sample_and_provisional_1" class="form-control" value="<?php echo $sample_and_provisional_1; ?>" placeholder="" >
						</div>
						<div class="col-md-6">								
							<input type="text" id="" name="sample_and_provisional_2" class="form-control" value="<?php echo $sample_and_provisional_2; ?>" >
						</div>
					</div>
				  </div>
				  <div class="form-group">
					 <label class="col-sm-2 control-label" for=""></label> 
					<div class="col-sm-10" style="padding:0;">
						<div class="col-md-6">
							<input type="text" id="label_box_1" name="label_box_1" style="background: <?= $manage_color;?>; color:#FFFFFF;" class="form-control" value="<?php echo $label_box_1; ?>" >
							<textarea class="form-control" id="detail_box_1" name="detail_box_1" rows="2" placeholder=""><?php echo $detail_box_1;?></textarea>
							<input type="text" id="label_box_2" name="label_box_2" style="background: <?= $manage_color;?>; color:#FFFFFF;" class="form-control" value="<?php echo $label_box_2; ?>" >
							<textarea class="form-control" id="detail_box_2" name="detail_box_2" rows="2" placeholder=""><?php echo $detail_box_2;?></textarea>
						</div>
						<div class="col-md-6">
							<input type="text" id="label_time_details" name="label_time_details" style="background: <?= $manage_color;?>; color:#FFFFFF;" class="form-control" value="<?php echo $label_time_details; ?>">
							<div class="col-sm-12">
							<?
								$ess_event_query = "SELECT * FROM ess_event_itinerary_mapping WHERE ess_id='".$id."'";
								$ess_event_sql=mysqli_query($conn,$ess_event_query);
								$ess_event_count=mysqli_num_rows($ess_event_sql);
								if($ess_event_count>0){
									while($row_ess_event = mysqli_fetch_array($ess_event_sql)){
							?>
							<div class="initial_row" id="event_itinerary<?php echo $row_ess_event['id'];?>">
								<div class="form-group"> 
									<div class="col-sm-4"> 
										<label class="control-label" for="time">Time</label> 
										<input type="text" id="time" name="time[]" class="form-control" value="<?php echo $row_ess_event['time'];?>" placeholder="Time"> 
									</div>
									<div class="col-sm-6"> 
										<label class="control-label" for="details">Details</label> 
										<input type="text" id="details" name="details[]" class="form-control" value="<?php echo $row_ess_event['details'];?>" placeholder="DETAILS"> 						
									</div>
									<div class="col-sm-1 custom-trash">
										<input type="hidden" name="ess_event_itinerary_mapping_id[]" value="<?php echo $row_ess_event['id'];?>">
										<label class="control-label" for="details">&nbsp;</label> 
										
										<span class="fa fa-trash delete_event_itinerary" alt="<?php echo $row_ess_event['id'];?>"></span>
										<i class="fa fa-refresh fa-spin" id="load_event_itinerary_<?=$row_ess_event['id'];?>" style="font-size: 20px;display:none;"></i>
										<i class="fa fa-check" id="status_update_done_<?=$row_ess_event['id'];?>" style="color:#bad23a;font-size: 20px;display:none;"></i>
									</div>
								</div>
							</div>
								<? } }else{ ?>
								<div class="initial_row">
									<div class="form-group" > 
										<div class="col-sm-4"> 
											<label class="control-label" for="">TIME</label> 
											<input type="text" id="time" name="time[]" class="form-control" value="" placeholder="TIME"> 
										</div>
										<div class="col-sm-6"> 
											<label class="control-label" for="details">Details</label> 
											<input type="text" id="details" name="details[]" class="form-control" value="" placeholder="DETAILS"> 						
										</div>
										<div class="col-sm-1 custom-trash">
											<label class="control-label" for="details">&nbsp;</label> 
											<span class="fa fa-trash delete_event_itinerary"></span>
										</div>
									</div>
								</div>
								<? } ?>
								<div class="form-group col-sm-12 event_itinerary_div_to_append">		
									<label class="add_one_more_event_itinerary" style="cursor:pointer"><span class="fa fa-plus"></span> add one more </label>
								</div>
							</div>
						</div>
					</div>
				  </div>
				    <div class="form-group">
					 <label class="col-sm-2 control-label" for=""></label> 
					<div class="col-sm-10" style="padding:0;">
						<div class="col-md-6">
							<input type="text" id="" name="label_box_3" style="background: #696969; color:#FFFFFF;" class="form-control" value="<?php echo $label_box_3; ?>">
							<textarea class="form-control" id="detail_box_3" name="detail_box_3" rows="2" placeholder=""><?php echo $detail_box_3;?></textarea>
							
						</div>
						<div class="col-md-6">
							<input type="text" id="" name="label_box_4" style="background: #696969; color:#FFFFFF;" class="form-control" value="<?php echo $label_box_4; ?>">
							<textarea class="form-control" id="detail_box_4" name="detail_box_4" rows="2" placeholder=""><?php echo $detail_box_4;?></textarea>
						</div>
					</div>
				  </div>
				  <div class="form-group">  
						<label class="col-sm-2 control-label" for=""></label>
						<div class="col-sm-10">
							<label class="" for="event_manager">ICE EVENT ADMIN MANAGER</label>
							<div class="col-sm-12">
							<?
								$ess_email_query = "SELECT * FROM ess_email_mapping WHERE ess_id='".$id."'";
								$ess_email_sql=mysqli_query($conn,$ess_email_query);
								$ess_email_count=mysqli_num_rows($ess_email_sql);
								if($ess_email_count>0){
									while($row_ess_email = mysqli_fetch_array($ess_email_sql)){
										 $row_ess_email=utility::validate_crypt_data($row_ess_email);
							?>
							<div class="initial_row" id="ess_email<?php echo $row_ess_email['id'];?>">
								<div class="form-group"> 
									<label class="col-sm-2 control-label" for="">Email</label>
									<div class="col-sm-8">  
										<input type="text" id="email" name="email[]" class="form-control" value="<?php echo $row_ess_email['email'];?>" placeholder="Email" autocomplete="off"> 
									</div>
									<div class="col-sm-2 custom-trash">
										<input type="hidden" name="ess_email_mapping_id[]" value="<?php echo $row_ess_email['id'];?>" >
										<label class="control-label" for="details">&nbsp;</label> 
										<span class="fa fa-trash delete_email" alt="<?php echo $row_ess_email['id'];?>"></span>
										<i class="fa fa-refresh fa-spin" id="load_ess_email_<?=$row_ess_email['id'];?>" style="font-size: 20px;display:none;"></i>
										<i class="fa fa-check" id="status_update_done_email_<?=$row_ess_email['id'];?>" style="color:#bad23a;font-size: 20px;display:none;"></i>
									</div>
								</div>
							</div>
								<? } }else{ ?>
								<div class="initial_row">
									<div class="form-group" > 
										<label class="col-sm-2 control-label" for="">Email</label> 
										<div class="col-sm-8">
											<input type="text" id="email" name="email[]" class="form-control" value="" placeholder="Email"> 
										</div>
										<div class="col-sm-2 custom-trash">
											<label class="control-label" for="">&nbsp;</label> 
											<span class="fa fa-trash delete_email"></span>
										</div>
									</div>
								</div>
								<? }?>
								<div class="form-group col-sm-12 email_div_to_append">
									<label class="add_one_more_email" style="cursor:pointer"><span class="fa fa-plus"></span> add one more </label>
								</div>
							</div>	
						</div> 
						<!--<div class="col-sm-2">
							<div class="form-group col-sm-12 email_div_to_append">					         <label class="add_one_more_email" style="cursor:pointer">
							<span class="fa fa-plus"></span>Add</label>
							</div>
						</div>-->
					</div>
				   </div>
				   <div class="col-lg-3 col-md-3 col-sm-12">
					<div class="form-group"> 
						<label class="col-sm-3 control-label">Category</label>  
						<div class="col-sm-9"> 
							<? echo Keycode::getKeycodeListValue("category", $category, $width = "100%", "Category", "id", "code", "ess_category","","form-control select2",false,"required"); ?>
							<div id="new_category_link" style="color:#367FA9;cursor:pointer;">New Category</div>
						</div> 
					</div>
					<div id="new_category_div" style="display:none;">
						<div class="form-group"> 
							<label class="col-sm-3 control-label" for="New Category">New Category</label> 
							<div class="col-sm-9"> 
								<input type="text" id="caegory_name_new" name="category_name_new" class="form-control" value="" placeholder="New Category"> 
							</div> 
						</div>
					</div>
					<div class="form-group" style="margin-top:115px;"> 
						<label class="col-sm-3 control-label" for="Sector">Two Page PDF</label> 
						<div class="col-sm-9"> 
							<input type="checkbox" class="flat-red" id="ex_work_date_confirmed" name="two_page_pdf" value="1" <? echo ($two_page_pdf == 1) ? "checked" : ""; ?>>
						</div> 
						
					</div>
					<div class="form-group" style="margin-top:710px;"> 
						<label class="col-sm-3 control-label"  for="Sector">Colour</label> 
						<div class="col-sm-9"> 
							<input type="text" class="form-control my-colorpicker1 colorpicker-element" name="manage_color" id="manage_color" value="<?php echo $manage_color;?>" autocomplete="off">
							<div id="ess_color" style="width:32px;height:32px;margin: 3px 0;background:<?php echo $manage_color;?>;"></div>
						</div>
					</div>
				   </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="tem_img_1" name="tem_img_1" value="<?php echo $tem_img_1;?>">
					<input type="hidden" id="tem_img_2" name="tem_img_2" value="<?php echo $tem_img_2;?>">
					<input type="hidden" id="tem_img_3" name="tem_img_3" value="<?php echo $tem_img_3;?>">
					<input type="hidden" id="tem_img_4" name="tem_img_4" value="<?php echo $tem_img_4;?>">
					<!--<input type="hidden" id="total_hours" name="total_hours" value="<?php echo $total_hours;?>">-->
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
					<button type="button" value="#" onclick="#" class="btn btn-primary">Send PDF</button>
					<? if($_REQUEST['action']=="edit"){?>
						<button type="button" value="preview" onclick="javascript:window.location='?<?php echo $preview_pdf;?>'" class="btn btn-primary">Preview</button>
					<? }else {?>
						<button type="button" value="preview" onclick="" class="btn btn-primary" disabled>Preview</button>
					<? }?> 
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$('#form1').submit(function(){
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});
$(".use_tem_img").on('ifChecked', function(){
		if($(this).val() == 1){
			$('#tem_img_dd').show();
		}else{
			$('#tem_img_dd').hide();
			/* $("#frame1").attr("src", "../../ess_images/blank_imag.png");
			$("#frame2").attr("src", "../../ess_images/blank_imag.png");
			$("#frame3").attr("src", "../../ess_images/blank_imag.png");
			$("#frame4").attr("src", "../../ess_images/blank_imag.png");
			$('#tem_img_1').find('input:text').val('');
			$('#tem_img_2').find('input:text').val('');
			$('#tem_img_3').find('input:text').val('');
			$('#tem_img_4').find('input:text').val('');
			 */
		}
	});	
CKEDITOR.replace('description', {toolbar:'Basic'});  
CKEDITOR.replace('detail_box_1',{toolbar:'Basic'}); 
CKEDITOR.replace('detail_box_2',{toolbar:'Basic'});  
CKEDITOR.replace('detail_box_3',{toolbar:'Basic'});  
CKEDITOR.replace('detail_box_4',{toolbar:'Basic'});  
function preview1() {
    frame1.src=URL.createObjectURL(event.target.files[0]);
}
function preview2() {
    frame2.src=URL.createObjectURL(event.target.files[0]);
}
function preview3() {
    frame3.src=URL.createObjectURL(event.target.files[0]);
}
function preview4() {
    frame4.src=URL.createObjectURL(event.target.files[0]);
}
$("#new_category_link").click(function(){
		$("#new_category_div").toggle();
	});
$(function(){
	$('.add_one_more_event_itinerary').click(function() {			
		$('#event_itinerary_div_to_clone').show();		
		var cloned_div = $('#event_itinerary_div_to_clone').clone();		
		$('.event_itinerary_div_to_append').before(cloned_div);		
		$('#event_itinerary_div_to_clone').hide();			
	});
	$('.add_one_more_email').click(function() {			
		$('#email_div_to_clone').show();		
		var cloned_div = $('#email_div_to_clone').clone();		
		$('.email_div_to_append').before(cloned_div);		
		$('#email_div_to_clone').hide();			
	});
	$("#form1").on("click", ".delete_event_itinerary",function() {
	    if(confirm('Are you sure! You want to delete')){		
			if($(this).attr("alt") != ""){
				var event_itinerary_mapping_id = $(this).attr("alt");
				$('#load_event_itinerary_'+event_itinerary_mapping_id).show();
				var form_data   = {'event_itinerary_mapping_id':event_itinerary_mapping_id};       
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_event_itinerary",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#status_update_done_'+event_itinerary_mapping_id).show();
							$('#load_event_itinerary_'+event_itinerary_mapping_id).hide();
							
							setTimeout(function() {
								$('#status_update_done_'+event_itinerary_mapping_id).hide();
								$('#event_itinerary'+event_itinerary_mapping_id).remove();
							},1000);
						}
					}
				});
			}else{
				$(this).parent().parent().parent().remove();
			}
		}
	});
	
	$("#form1").on("click", ".delete_email",function() {
	    if(confirm('Are you sure! You want to delete')){		
			if($(this).attr("alt") != ""){
				var email_mapping_id = $(this).attr("alt");
				$('#load_ess_email_'+email_mapping_id).show();
				var form_data   = {'email_mapping_id':email_mapping_id};       
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_email",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#status_update_done_email_'+email_mapping_id).show();
							$('#load_ess_email_'+email_mapping_id).hide();
							setTimeout(function() {
								$('#status_update_done_email_'+email_mapping_id).hide();
								$('#ess_email'+email_mapping_id).remove();
							},1000);
						}
					}
				});
			}else{
				$(this).parent().parent().parent().remove();
			}
		}
	});
		
	$('#manage_color').colorpicker().on('changeColor', function(){
		$("#ess_color").css("background-color", $(this).val());
		$("#label_box_1").css("background-color", $(this).val());
		$("#label_time_details").css("background-color", $(this).val());
		$("#label_box_2").css("background-color", $(this).val());
	});
	$("#form1").on("change","#template_id",function(){
		$("#select_template_load").show();
		var template_id = $(this).val();
		if(template_id != ""){
		var form_data   = {'template_id':template_id};       
			////////////////////// For fetching images /////////////////////////
			$.ajax({
				async: true,
				type: "POST",
				url: site_url+"/modules/ess/index.php?page=ajax_actions&action=get_template_by_id",	
				data: form_data,
				success: function(result){  
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == 1){
						$("#frame1").attr("src", "../../ess_images/"+resultObj.ess_image_1);
						$("#frame2").attr("src", "../../ess_images/"+resultObj.ess_image_2);
						$("#frame3").attr("src", "../../ess_images/"+resultObj.ess_image_3);
						$("#frame4").attr("src", "../../ess_images/"+resultObj.ess_image_4);
						$("#tem_img_1").val(resultObj.ess_image_1);
						$("#tem_img_2").val(resultObj.ess_image_2);
						$("#tem_img_3").val(resultObj.ess_image_3);
						$("#tem_img_4").val(resultObj.ess_image_4);
						$("#delete1").attr("style", "display:block");
						$("#delete2").attr("style", "display:block");
						$("#delete3").attr("style", "display:block");
						$("#delete4").attr("style", "display:block");
						$("#select_template_load").hide();
					}
				}
			});
			////////////////////// End For fetching images /////////////////////////
		} 
	});
});

$(document).ready(function() {
  de(); 
  if(($("#hid1").val() != "")) {
	$("#delete1").attr("style", "display:block");
  }	
  $("#ess_image_1").on("change", function() {
  if (($("#ess_image_1").val() != "") ) {
	$("#delete1").attr("style", "display:block");
  } else {
	de();
  } 
}); 	
 $("#delete1").click(function() {
	var id = $("#id").val();
	var hid1 = $("#hid1").val();
	if(id !=''){ 
      if(hid1 ==''){
		$("#ess_image_1").val('');
		$("#frame1").attr("src", "../../ess_images/blank_imag.png");
		de();  
	  }else{ 	
		var con = confirm("Are you sure you want to delete ?");
		if(con){
			$("#delete_load_1").show();
			var form_data   = {'id':id}; 
			$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_ess_image_1",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#ess_image_1").val('');
							$("#hid1").val('');
							$("#delete_load_1").hide();
							$("#frame1").attr("src", "../../ess_images/blank_imag.png");
							de();
						}
					}	
				 });
		   }
      }		   
	}else{
		$("#ess_image_1").val('');
		$("#frame1").attr("src", "../../ess_images/blank_imag.png");
		de();
	}
  });
  
  df(); 
  if(($("#hid2").val() != "")) {
	$("#delete2").attr("style", "display:block");
  }	
  $("#ess_image_2").on("change", function() {
  if (($("#ess_image_2").val() != "") ) {
	$("#delete2").attr("style", "display:block");
  } else {
	df();
  } 
}); 	
 $("#delete2").click(function() {
	var id = $("#id").val();
	var hid2 = $("#hid2").val();
	if(id !=''){ 
      if(hid2 ==''){
		$("#ess_image_2").val('');
		$("#frame2").attr("src", "../../ess_images/blank_imag.png");
		df();  
	  }else{ 
		var con = confirm("Are you sure you want to delete ?");
		if(con){
			$("#delete_load_2").show();
			var form_data   = {'id':id}; 
			$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_ess_image_2",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#ess_image_2").val('');
							$("#hid2").val('');
							$("#frame2").attr("src", "../../ess_images/blank_imag.png");
							$("#delete_load_2").hide();
							df();
						}
					}	
				 });
		    }
	  }			
	}else{
		$("#ess_image_2").val('');
		$("#frame2").attr("src", "../../ess_images/blank_imag.png");
		df();
	}
  });
  
  dg(); 
  if(($("#hid3").val() != "")) {
	$("#delete3").attr("style", "display:block");
  }	
  $("#ess_image_3").on("change", function() {
  if (($("#ess_image_3").val() != "") ) {
	$("#delete3").attr("style", "display:block");
  } else {
	dg();
  } 
}); 	
 $("#delete3").click(function() {
	var id = $("#id").val();
	var hid3 = $("#hid3").val();
	if(id !=''){ 
      if(hid3 ==''){
		$("#ess_image_3").val('');
		$("#frame3").attr("src", "../../ess_images/blank_imag.png");
		dg();  
	  }else{ 
		var con = confirm("Are you sure you want to delete ?");
		if(con){
			$("#delete_load_3").show();
			var form_data   = {'id':id}; 
			$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_ess_image_3",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#ess_image_3").val('');
							$("#hid3").val('');
							$("#frame3").attr("src", "../../ess_images/blank_imag.png");
							$("#delete_load_3").hide();
							dg();
						}
					}	
				 });
		    }
	  }			
	}else{
		$("#ess_image_3").val('');
		$("#frame3").attr("src", "../../ess_images/blank_imag.png");
		dg();
	}
  });
  
  dh(); 
  if(($("#hid4").val() != "")) {
	$("#delete4").attr("style", "display:block");
  }	
  $("#ess_image_4").on("change", function() {
  if (($("#ess_image_4").val() != "") ) {
	$("#delete4").attr("style", "display:block");
  } else {
	dh();
  } 
}); 	
 $("#delete4").click(function() {
	var id = $("#id").val();
	var hid4 = $("#hid4").val();
	if(id !=''){ 
      if(hid4 ==''){
		$("#ess_image_4").val('');
		$("#frame4").attr("src", "../../ess_images/blank_imag.png");
		dh();  
	  }else{ 
		var con = confirm("Are you sure you want to delete ?");
		if(con){
			$("#delete_load_4").show();
			var form_data   = {'id':id}; 
			$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/ess/index.php?page=ajax_actions&action=delete_ess_image_4",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#ess_image_4").val('');
							$("#hid4").val('');
							$("#frame4").attr("src", "../../ess_images/blank_imag.png");
							$("#delete_load_4").hide();
							dh();
						}
					}	
				 });
		   }
	  }		   
	}else{
		$("#ess_image_4").val('');
		$("#frame4").attr("src", "../../ess_images/blank_imag.png");
		dh();
	}
  });
  
});
function de() {
  $("#delete1").attr("style", "display:none");
}
function df() {
  $("#delete2").attr("style", "display:none");
}
function dg() {
  $("#delete3").attr("style", "display:none");
}
function dh() {
  $("#delete4").attr("style", "display:none");
}

var editor = CKEDITOR.inline( 'editable', {
    removePlugins: 'toolbar'
} );

</script>