<?php
if(isset($_POST['update'])){
	if(!empty($_POST['name']) && trim($_POST['name']) != ""){
	if(($_GET['id'] != "" && !empty($_GET['id']))){

		$name = addslashes($_POST['name']);
		$certification_allias = addslashes($_POST['certification_allias']);
		$description = addslashes($_POST['description']);
		$image = $_FILES['image']['name'];
		$image_type = $_FILES['image']['type'];
		$image_temppath = $_FILES["image"]["tmp_name"];
		$image_path="/var/www/html/uploads/blog_image/".$_FILES['image']['name'];
		list($orgwidth, $orgheight)=getimagesize($image_temppath);
		$img_alt	= addslashes($_POST['img_alt']);
		$status =addslashes($_POST['status']);
		//$meta_title = addslashes($_POST['meta_title']);
		$meta_keyword = addslashes($_POST['meta_keyword']);
		$meta_description = addslashes($_POST['meta_description']);
		$id = $_GET['id'];
		$msg = "";
		if(!empty($_FILES['image']['name'])) 
		{
			if($orgwidth == $orgheight){
	  			$case = 1;
	  		}
	  		if($orgwidth > $orgheight){
	  			$case = 2;
	  		}
	  		if($orgwidth < $orgheight){
	  			$case = 3;
	  		}
		  switch ($case) {
		  	case 1:
	  		$newwidth  = 300;
	  		$newheight = 300;
	  		break;
	  		case 2:
	  		$newheight = 300;
	  		$aspectratio = $newheight/$orgheight;
	  		$newwidth  = round($orgwidth*$aspectratio);
	  		break;
	  		case 3:
	  		$newwidth = 300;
	  		$aspectratio = $newwidth/$orgwidth;
	  		$newheight  = round($orgheight*$aspectratio);
	  		break;
	  	}
	  if($image_type == 'image/jpeg'){
	  		$img = imagecreatefromjpeg($image_temppath);
			$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	 }

	  if($image_type == 'image/png'){
	  		$img = imagecreatefrompng($image_temppath);
			$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	
	  }
	  if($image_type == 'image/gif'){
	  	$img = imagecreatefromgif($image_temppath);
			$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	
	  }
	  // move_uploaded_file($image_temppath, $image_path);
	}
 // check title
$sq = mysql_query("SELECT COUNT(*) AS total FROM ".allied_certification." WHERE allias = '".$certification_allias."' AND id != '".$id."'");
$rsobj = mysql_fetch_object($sq);
	if($rsobj->total > 0){
		$msg = "Certification Name Already Exists!";
	}else{
	// update to database
	mysql_query("UPDATE ".allied_certification." SET name = '".$name."', allias = '".$certification_allias."', description = '".$description."',image = '".$image."',img_alt='".$img_alt."',status = '".$status."',meta_keyword='".$meta_keyword."',meta_description='".$meta_description."' WHERE id = '".$id."'");
	if(mysql_affected_rows() > 0){
	//redirect
	header("location:account.php?page=certification_list");
	}else{
	$msg = "Updation Failed";
	}
	}
	}else{
	$msg = "Operation Aborted due to parameter insufficiency!";
	}}else{
	// error message
	$msg = "Enter Certification Allias";
	}
	}

// save
if(isset($_POST['save'])){
if(!empty($_POST['certification_allias']) && trim($_POST['certification_allias']) != ""){
// get param
$name = addslashes($_POST['name']);

$string = $_POST['certification_allias'];
$str=str_replace(array(':', ',', '/', '*'), ' ', $string);
$certification_allias = str_replace(" ", "-", $str);

$description = addslashes($_POST['description']);
$image = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_temppath = $_FILES["image"]["tmp_name"];
$image_path="../uploads/blog_image/".$_FILES['image']['name'];
list($orgwidth, $orgheight)=getimagesize($image_temppath);
$img_alt	= addslashes($_POST['img_alt']);
$status = addslashes($_POST['status']);
$msg = "";
$meta_title= addslashes($_POST['meta_title']);
$meta_keyword = addslashes($_POST['meta_keyword']);
$meta_description = addslashes($_POST['meta_description']);

// check title
if(!empty($_FILES['image']['name'])){
	if($orgwidth == $orgheight){
	  	$case = 1;
	  }
	  if($orgwidth > $orgheight){
	  	$case = 2;
	  }
	  if($orgwidth < $orgheight){
	  	$case = 3;
	  }

	  switch ($case) {
	  	case 1:
	  		$newwidth  = 300;
	  		$newheight = 300;
	  		break;
	  	case 2:
	  		$newheight = 300;
	  		$aspectratio = $newheight/$orgheight;
	  		$newwidth  = round($orgwidth*$aspectratio);
	  		break;
	  	case 3:
	  		$newwidth = 300;
	  		$aspectratio = $newwidth/$orgwidth;
	  		$newheight  = round($orgheight*$aspectratio);
	  		break;
	  }

	  if($image_type == 'image/jpeg'){
	  		$img = imagecreatefromjpeg($image_temppath);
			//echo $img;exit;
	  		$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	 }

	  if($image_type == 'image/png'){

	  	$img = imagecreatefrompng($image_temppath);
			//echo $img;exit;
	  		$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	
	  }
	  if($image_type == 'image/gif'){
	  	$img = imagecreatefromgif($image_temppath);
			//echo $img;exit;
	  		$thumbimg = imagecreatetruecolor($newwidth, $newheight);
	  		imagecopyresized($thumbimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $orgwidth, $orgheight);
	  		imagejpeg($thumbimg, $image_path);
	  		//$out_image=addslashes(file_get_contents($image_path));
	  	
	  }		
			
		}


$sq = mysql_query("SELECT COUNT(*) AS total FROM ".allied_certification." WHERE name = '".$name."'");
$rsobj = mysql_fetch_object($sq);
if($rsobj->total > 0){
$msg = "Certificaton Name Already Exists!";
}else{
// save to database
mysql_query("INSERT INTO ".allied_certification." (name,allias, description,image,img_alt,status,meta_title,meta_keyword,meta_description,create_date) VALUES('".$name."', '".$certification_allias."', '".$description."','".$image."','".$img_alt."','".$status."','".$meta_title."','".$meta_keyword."','".$meta_description."',now())")or die(mysql_error());

$rs2=mysql_query("select max(id) from allied_certification");
if($row2=mysql_fetch_row($rs2)){
$prid='CRT00'.$row2[0];
mysql_query("update allied_certification set certification_id='".$prid."' where id='".$row2[0]."'")or die(mysql_error());
}

if(mysql_affected_rows() > 0){
//redirect
header("location:account.php?page=certification_list");
}else{
$msg = "Certification Saving Failed";
}
}
}else{
// error message$
$msg = "Enter Certification Allias";
}
}

// trace Program information from database
if($_GET['id'] != "" && !empty($_GET['id']))
{
	$sq = mysql_query("SELECT * FROM ".allied_certification." WHERE id = '".$_GET['id']."'");
	if(mysql_num_rows($sq) > 0)
	{
		$res = mysql_fetch_object($sq);
		// get param
		$name = stripslashes($res->name);
		$certification_allias = stripslashes($res->allias);
		$description = stripslashes($res->description);
		$status = stripslashes($res->status);
		$img_alt= stripslashes($res->img_alt);

		$image=stripslashes($res->image);
			//echo $image;exit;
	}
}
?>

<script type="text/javascript" language="JavaScript">
window.onload=function(){
	document.getElementById('name').onkeyup=function(){
		var txt1 = document.getElementById('name').value;
		
		txt1 = txt1.replace(/ /g,'-');
		
		txt1 =txt1.replace(/[,[''\]/\""]/g,'');
		document.getElementById('certification_allias').value = txt1;
		
	}
}

/*$(document).ready(function(){
	$("#menu_allias").keyup(function(){
		$("#menu_title").val($("#menu_allias").val());
	});
});*/
</script>
<script type="text/javascript">
function valForm()
{
	//var str=document.page_fr;
	var name=$("#name").val();
	var pg_status=$("#status").val();
	if(name=='')
	{
		$("#name").css("border-color","#F00");
		$("#name").focus();
		$("#error_div").html("Enter Certification Name");
		return false;
	}
	else
	{
		$("#name").css("border-color","");
		$("#error_div").html("");
	}
	if(pg_status=='')
	{
		$("#status").css("border-color","#F00");
		$("#status").focus();
		$("#error_div").html("Select Status");
		return false;
	}
	else
	{
		$("#status").css("border-color","");
		$("#error_div").html("");
	}
}
</script>
<div style="width: 100%; margin: 0 auto; margin-top: 0px;">
  <table width="100%" border="0" cellspacing="0" cellpadding="2" style="margin-bottom:10px;">
    <tr>
    <td width="10%" class="activeTab"><a href="account.php?page=add_certification">Add Certification</a></td>
    <td width="10%" class="normalTab"><a href="account.php?page=certification_list">Certification List</a></td>
    <td width="0%" style="border-bottom: 1px solid #99C;"></td>
    </tr>
  </table>
  <?php if($msg <> ""){
?>
  <div style="border: 1px solid #990; background-color: #FFC; font-weight: bold; margin-top: 5px; margin-bottom: 5px;">
		<table width="100%" border="0" cellspacing="0" cellpadding="2">
		<tr>
			<td width="4%"><img src="../images/warning.png" width="20" height="20"></td>
			<td width="96%"><?php echo $msg; ?></td>
		</tr>
		</table>
	</div>
	<?php } ?>
  	<form action="" method="post" name="page_fr" onsubmit="return valForm();" enctype="multipart/form-data">
  	<table width="100%" border="0" cellspacing="2" cellpadding="2" style="margin-top: 8px;">
  	<tr>
    	<td>
    		<table width="100%"  border="0" cellpadding="2" cellspacing="0">
     	<tr>
        	<td height="43" >Select Certification Category :</td>
        	<td >
        	<?php
	 		 $row = mysql_query("select * from allied_certification_cat") ;
	  		?>
        	<select name="cat_title" id="cat_title" style="width: 258px;">
            <option value="">--- Select ---</option>
             <?php while($results = mysql_fetch_array($row)){ ?>
            <option value="<?php echo $results['id'] ;  ?>" <?php if($result['cat_id'] == $results['id']) echo "selected='selected'"; ?>><?php echo $results['cat_title'];?></option>
            <?php }?>
        	</select>
       	 </td>
        </tr>
    <tr>
    <td align="left"></td>
    <td align="left"><span id="error_div" style="font:Arial, Helvetica, sans-serif; color:#F00; font-size:12px;"></span></td>
    </tr>
    
    <tr>
        <td width="172" height="43" align="right" >Certification Name:</td>
        <td width="1160" ><input name="name" type="text" class="textbox" id="name" value="<?php echo $name; ?>" style="width: 650px;"> 
        <span id="mtitle" style="font:Arial, Helvetica, sans-serif; color:#930; font-size:12px;"></span></td>
    </tr>

	<tr>
        <td>Certification Allias:</td>
        <td><input name="certification_allias" type="text" class="textbox" id="certification_allias" value="<?php echo $certification_allias;  ?>" style="width: 300px;" onmouseout="this.value=this.value.replace(/ /g,'-');"/></td>
        </tr>
      <tr>

	<tr>
	<td align="right" valign="top" style="padding-left:3px;">Certification Description:</td>
	<td style="padding-left:3px;width:100px;">
        <textarea id="description" name="description"  class="description"><?php echo $description; ?></textarea>
    <script type="text/javascript">CKEDITOR.replace('description',{ width: '750'} );</script>

	</td>
  </tr>
<!--===============================================================================-->
<tr>
       <td height="43" >Image:</td>
        <td ><input name="image" type="file" class="textbox" id="image" style="width: 650px;" value="<?php echo $image ?>" >
         <span id="mtitle" style="font:Arial, Helvetica, sans-serif; color:#930; font-size:12px;"></span></td>
         <td>
         <?php if($image!= ''){ //echo "sdadsdas";exit;?>

         <img src="../uploads/blog_image/<?php echo $image; ?>" alt="N/A"  height="50" width="50">
         <?php } ?></td>
      </tr>

      <tr>
        <td height="43" >Image Alt:</td>
        <td ><input name="img_alt" type="text" class="textbox" id="img_alt" value="<?php echo $img_alt; ?>" style="width: 650px;" > </td>
      </tr>
<!--================================================================================-->
      <tr>
        <td align="right" valign="top">Status:</td>
        <td>
        <select name="status" id="status" style="width: 205px;">
      <option value="">--- Select ---</option>
      <option value="1" <?php if($status == "1") echo "selected='selected'"; ?>>Published</option>
      <option value="0" <?php if($status == "0") echo "selected='selected'"; ?>>Un Published</option>
      </select></td>
       </tr>

       <!-- <tr>
       		 <td height="43" >Meta Title:</td>
        <td >
        	<textarea id="meta_title" name="meta_title" style="width: 650px; height: 100px;"><?php echo stripslashes($res->meta_title); ?></textarea> 
        </td>
      </tr> -->

	     <tr>
       		 <td height="43" >Meta keyword:</td>
        <td >
        	<textarea id="meta_keyword" name="meta_keyword" style="width: 650px; height: 100px;"><?php echo stripslashes($res->meta_keyword); ?></textarea> 
        </td>
      </tr>
      <td height="43" >Meta Description:</td>
        <td ><textarea id="meta_description" name="meta_description" style="width: 650px; height: 100px;"><?php echo stripslashes($res->meta_description); ?></textarea> </td>
      </tr>
		
        <td colspan="2" align="left"><?php
	if($_GET['id'] == ""){
	?>
	<input name="save" type="submit" id="save" value=" Save " class="actionBtn" style="margin-left:140px;">
	<?php
	}else{
	?>
	<input type="hidden" name="pg_id" id="pg_id" value="<?php echo $pg_id;?>">
	<input name="update" type="submit" id="update" value=" Update " class="actionBtn" style="margin-left:140px;">
	<?php } ?>
	</td>
      </tr>
    </table>
  </td>
  </tr>
</table>
</form>
</div>
</div>

