<?php
// Function to protect SQL Injection
function clean($str)
{
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = addslashes($str);
	}
	return mysql_real_escape_string($str);
}

// Function to check the login status For the ADMIN
function checkLoginAdmin()
{
	if (!isset($_SESSION['user_login_id']) || empty($_SESSION['user_login_id']) ){
		$url = STATE_URL."login.php";
		header("Location: $url");
	}
}

///  Function that will capitalized 1st Chanrater of each word.
function capitalize($str)
{
	$lowercaseTitle = strtolower($str);
	$ucTitleString = ucwords($lowercaseTitle);
	return $ucTitleString;
}
// Function that will delete the file physically from the directory.
function deleteFile($dir, $fileName)
{
	$handle=opendir($dir);
	
	while (($file = readdir($handle))!==false)
	{
		if ($file == $fileName)
		{
			@unlink($dir.'/'.$file);
		}
	}
	closedir($handle);
}

//Generate thumbnail of Image file
function generate_image_thumbnail($source_image_path, $thumbnail_image_path,$width,$height)
{
	list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );
	switch ( $source_image_type )
	{
		case IMAGETYPE_GIF:
			$source_gd_image = imagecreatefromgif( $source_image_path );
		break;
		
		case IMAGETYPE_JPEG:
			$source_gd_image = imagecreatefromjpeg( $source_image_path );
		break;
		
		case IMAGETYPE_PNG:
			$source_gd_image = imagecreatefrompng( $source_image_path );
		break;
	}
	
	if ( $source_gd_image === false ) {
		return false;
	}
	
	$thumbnail_image_width  = $width;
	$thumbnail_image_height = $height;
	
	$source_aspect_ratio    = $source_image_width / $source_image_height;
	$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;
	
	if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
	{
		$thumbnail_image_width = $source_image_width;
		$thumbnail_image_height = $source_image_height;
	}
	elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
	{
		$thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
	}else
	{
		$thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
	}
	
	$thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );
	/*var_export(imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height ) ); exit();*/
	imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );
	
	//var_export( imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 90 )); exit();	
	imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 90 );
	
	imagedestroy( $source_gd_image );
	
	imagedestroy( $thumbnail_gd_image );
	
	return true;
}

/// Functions to display error messages
function successMsg($e)
{
	if($e == 1){
		$msg = "Password has been sent to your email.";
	}if($e == 2){
		$msg = "Password has been updated.";
	}if($e == 3){
		$msg = "Email has been updated.";
	}if($e == 4){
		$msg = "Record has been added.";
	}if($e == 5){
		$msg = "Record has been updated.";
	}if($e == 6){
		$msg = "Record has been deleted.";
	}
	//echo "<div style='color:#060;font-family:\"Comic Sans MS\",cursive;font-size:15px;'>$msg</div>";
	
	?>
	<link rel="stylesheet" type="text/css" href="<?php echo STATE_NOTIFICATIONS;?>css/jquery_notification.css" />
    <script type="text/javascript" src="<?php echo STATE_NOTIFICATIONS;?>js/jquery_notification_v.1.js"></script>
    <script type="text/javascript">
		showNotification({
			message: "<?php echo $msg; ?>",
			type: "success",
			autoClose: true,
			duration: 5
		});
    </script>
<?php
}

function errorMsg($error)
{
	if($error == 1){
		$errMsg = "Invalid password.";
	}
	?>
    
	<link rel="stylesheet" type="text/css" href="<?php echo STATE_NOTIFICATIONS;?>css/jquery_notification.css" />
    <script type="text/javascript" src="<?php echo STATE_NOTIFICATIONS;?>js/jquery_notification_v.1.js"></script>
    <script type="text/javascript">
		showNotification({
			message: "<?php echo $errMsg; ?>",
			type: "error",
			autoClose: true,
			duration: 5
		});
    </script>
<?php
}
?>