<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Santhosh Thomas Alexander<santhosh.thomas@armiasystems.com> |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
//include('../includes/image.class.php');
include('../includes/class.upload.php');

$maximagesizestringinPHP = ini_get("upload_max_filesize");

$maximagesizeinPHP = $maximagesizestringinPHP * (1024 * 1024);

//$img = new Zubrag_image;
if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
    $adminname = $_SESSION["sess_adminname"];
} else {
    header("Location:login.php");
    exit;
}

if (isset($_GET["catid"]) and $_GET["catid"] != "") {
    $catid = $_GET["catid"];
} else if (isset($_POST["catid"]) and $_POST["catid"] != "") {
    $catid = $_POST["catid"];
}

//getting options for the category


if ($_GET["begin"] == "") {
    $begin = 0;
    $num = 1;
    $numBegin = 1;
} else {
    $begin = $_GET["begin"];
    $num = $_GET["num"];
    $numBegin = $_GET["numBegin"];
    ;
}

if (isset($_GET["txtSearch"]) and $_GET["txtSearch"] != "") {
    $txtSearch = $_GET["txtSearch"];
    $cmbSearchType = $_GET["cmbSearchType"];
} else if (isset($_POST["txtSearch"]) and $_POST["txtSearch"] != "") {
    $txtSearch = $_POST["txtSearch"];
    $cmbSearchType = $_POST["cmbSearchType"];
}
$txtSearch = trim($txtSearch);
$sql = "SELECT * FROM " . $tableprefix . "settings ";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $chkcanship = $row["enable_canadapost"];
    $max_images = $row['nproduct_images'];
}

//echo $txtSearch. "<br>";
$txtCategoryName = trim($_POST["txtCategoryName"]);
$productlist = $_POST["chkproducts"];

$txtProductName = $_POST["txtProductName"];
$txtProductCode = $_POST["txtProductCode"];
$txtDescription = $_POST["txtDescription"];
$txtPrice = $_POST["txtPrice"];
$ddlArtist = $_POST["ddlArtist"];


$txtStock = $_POST["txtStock"];

$shipping_display_block = "";

(isset($_POST['txtDigitalProduct'])) ? $txtDigitalProduct = $_POST['txtDigitalProduct'] : $txtDigitalProduct = 'N';
if ($txtDigitalProduct == "Y") {
    $digitalBlock = "";
    $chkDigitalProduct = "checked";
    $canshipblock = "none";
    $shipping_display_block = "none";
} else {
    $digitalBlock = "none";
    $chkDigitalProduct = "";
    $canshipblock = "";
    $shipping_display_block = "";
}

//$chkDigitalProduct = "checked";

if (isset($_GET['message'])) {
    $message = $_GET['message'];
}



$qryopt = "";
if ($txtSearch != "" && $txtSearch != "all") {
    if ($cmbSearchType == "artistname") {
        $qryopt .= "  AND  s.artist_name like '" . addslashes($txtSearch) . "%'";
    } else if ($cmbSearchType == "dateadded") {
        $dt = $txtSearch;
        $arrdt = explode("/", $dt);
        $day = $arrdt[1];
        $month = $arrdt[0];
        $year = $arrdt[2];
        $dt = $year . "-" . $month . "-" . $day;
        $qryopt .= "  AND  p.date_added like '" . addslashes($dt) . "%'";
    } else if ($cmbSearchType == "productname") {
        $qryopt .= "  AND  p.product_name like '" . addslashes($txtSearch) . "%'";
    }
    $txtSearchcat = $txtSearch;
    if ($txtCategoryName)
        $txtSearchcat = "";
}
if (isset($_POST["btnAddNewCategory"]) && !isNotNull($txtSearchcat)) {
    $message = "";
    if (!isset($catid) || $catid == "") {//at the top level
        $parentcatid = "0";
    } else {
        $parentcatid = $catid;
    }
    if (!isNotNull($txtCategoryName)) {
        $message .= CATNAME_REQD."<br>";
    } else if (categoryExists($txtCategoryName, $catid)) {
        $message .= CAT_EXIST;
    }
    if ($message != "") { // error
        $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . $message;
    } else { // no error so insert category details
        if ($parentcatid == "0") {//top level category
            $sqlinsertcat = "INSERT INTO " . $tableprefix . "categories(category_name,parent_id,route)
							   					 VALUES ('" . addslashes($txtCategoryName) . "', '" . addslashes($parentcatid) . "','0') ";
            $resultinsertcat = mysql_query($sqlinsertcat);
            $route_pr = mysql_insert_id();
            $category_id = $route_pr;
            $catOptArr = $_POST['category_options'];

            if (count($catOptArr)) {
                foreach ($catOptArr as $opt) {
                    $sqlinsertcat = "INSERT INTO " . $tableprefix . "category_options(category_id,option_id)
														 VALUES ('" . addslashes($category_id) . "', '" . addslashes($opt) . "') ";
                    mysql_query($sqlinsertcat);
                }
            }
            $sqlupdaterouteparent = "UPDATE " . $tableprefix . "categories SET route= '" . $route_pr . "' WHERE category_id = '" . $route_pr . "'";
            $resultupdatecat_parent = mysql_query($sqlupdaterouteparent);
            $message.="Category added successfully!";
        } else {
            $sqlcatdetails = "SELECT  route,count,parent_id  FROM " . $tableprefix . "categories
												  WHERE category_id = '$parentcatid' ";

            //echo "<br>".$sqlcatdetails;
            $resultcatdetails = mysql_query($sqlcatdetails);
            $rowcatdetails = mysql_fetch_array($resultcatdetails);
            $route = $rowcatdetails["route"];

            $exp = explode(",", $route);

            if ($route == "0") {
                $route = $parentcatid;
            }
            $qry = "SELECT product_category FROM " . $tableprefix . "products WHERE product_category in($route)";

            $qryexc = mysql_query($qry);
            if (mysql_num_rows($qryexc) == 0) {

                $sqlinsertcat = "INSERT INTO " . $tableprefix . "categories(category_name,parent_id,route)
										  				   VALUES ('" . addslashes($txtCategoryName) . "', '" . addslashes($parentcatid) . "','" . $route . "') ";
                $resultinsertcat = mysql_query($sqlinsertcat);
                $sqlcatid = mysql_insert_id();

                $category_id = $sqlcatid;
                $catOptArr = $_POST['category_options'];

                if (count($catOptArr)) {
                    foreach ($catOptArr as $opt) {
                        $sqlinsertcat = "INSERT INTO " . $tableprefix . "category_options(category_id,option_id)
																	 VALUES ('" . addslashes($category_id) . "', '" . addslashes($opt) . "') ";
                        mysql_query($sqlinsertcat);
                    }
                }

                if ($route != "") {
                    $route .= "," . $sqlcatid;
                    $sqlupdatecat = "UPDATE " . $tableprefix . "categories SET route= '" . $route . "' WHERE category_id = '" . $sqlcatid . "'";
                    $resultupdatecat = mysql_query($sqlupdatecat);
                }
                $message.=CAT_ADDED_SUCC;
            } else {
                $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . SUBCAT_DISABLED;
            }
        }
    }
} else if ($_POST["btnAddProduct"]) {
    $message = "";
    $checkleafcategory = " select *  FROM  " . $tableprefix . "categories where parent_id='$catid'";

    if (mysql_num_rows(mysql_query($checkleafcategory)) > 0) {
        $message .= TXT_THERE_IS_A_SUBCAT."<br>";
    }
    if (!isNotNull($txtProductName)) {
        $message .= PRO_NAME_EMPTY."<br>";
    } else {
        $qry = "SELECT  product_name FROM  " . $tableprefix . "products WHERE product_name='" . addslashes($txtProductName) . "'
		           and product_artist_id='" . $ddlArtist . "' and product_category = '" . addslashes($catid) . "' ";
        if (mysql_num_rows(mysql_query($qry)) > 0) {
            $message .= PRO_WITH_SAME_NAME."<br>";
        }
    }
    if (!isNotNull($txtProductCode)) {
        //$message .= "* Product Code cannot be empty! <br>";
    } else {
        $qry = "SELECT  product_code FROM  " . $tableprefix . "products WHERE product_code='" . addslashes($txtProductCode) . "'
		 and product_artist_id='" . $ddlArtist . "' and product_category = '" . addslashes($catid) . "' ";
        if (mysql_num_rows(mysql_query($qry)) > 0) {
            $message .= PRO_WITH_SAME_CODE."<br>";
        }
    }
    if ($ddlArtist == "") {
        $message .= SELLER_EMPTY."<br>";
    }



    if ($txtDescription == "") {
        $message .= PRO_DESC_EMPTY."<br>";
    }
    $priceposted = $txtPrice;
    $txtPrice = doubleval($txtPrice);
    if (!isNotNull($txtPrice) && !isNotNull($priceposted)) {
        $message .= PRO_PRICE_EMPTY."<br>";
    } else if ($txtPrice <= 0 || !is_numeric($txtPrice)) {
        $message .= PRO_PRICE_POSITIVE."<br>";
    }

    if ($txtDigitalProduct == "Y") {

        if (!isNotNull($_FILES['txtProductZip']['name'])) {
            $message .= DIG_PRO_EMPTY."<br>";
        }

        $uploadfile = ReplaceArrayImage($_FILES['txtProductZip']['name']);
        $types = array('zip', 'pdf', 'rar', 'psd', 'opf', 'azw', 'lit', 'pdb', 'pkg', 'mp3', 'mpeg', 'mp4', 'avi', 'mpg', 'wma');

        //preg_match('/\.([a-zA-Z]+?)$/', $uploadfile, $matches);
        $matches = explode(".", $uploadfile);

        $matches_cnt = count($matches);

        if (!in_array(strtolower($matches[$matches_cnt - 1]), $types)) { // check file format is valid or not
            $message .= INVALID_FILE_FORMAT."<br>";
        } // end format check
    }


    if (!in_array('1', $_POST['default_option'])) {
        $message .= SEL_DEFAULT_OPTION."<br>";
    }

    $options_count = count($_POST['additional_price']);
    for ($i = 0; $i < $options_count; $i++) {
        $default_option = $_POST['default_option'][$i];
        $additional_price = $_POST['additional_price'][$i];
        $product_stock = $_POST['product_stock'][$i];
        $reorder_level = $_POST['reorder_level'][$i];
        $product_shipping_weight = $_POST['product_shipping_weight'][$i];

        $discount = $_POST['discount'][$i];
        $discount_description = $_POST['discount_description'][$i];
        $product_length = $_POST['product_length'][$i];
        $product_width = $_POST['product_width'][$i];
        $product_height = $_POST['product_height'][$i];

        $prodimagedir = "../products/";

        $picsmallname = ReplaceArrayImage($_FILES['image_small']['name'][$i]);
        $picsmallorgname = $_FILES['image_small']['name'][$i];
        $picsmalltempname = $_FILES['image_small']['tmp_name'][$i];
        $picsmalltype = $_FILES['image_small']['type'][$i];

        $picbigname = ReplaceArrayImage($_FILES['image_big']['name'][$i]);
        $picbigtype = $_FILES['image_big']['type'][$i];
        $picbigorgname = $_FILES['image_big']['name'][$i];
        $picbigtempname = $_FILES['image_big']['tmp_name'][$i];

        if ($picbigname != "") {
            $final_image_big = "productlarge_" . $i . time() . $picbigname;
        } else {
            $final_image_big = "";
        }

        if ($picsmallname != "") {
            $final_image_small = "productsmall_" . $i . time() . $picsmallname;
        } else {
            $final_image_small = "";
        }

        $picsmalldest = $prodimagedir . $final_image_small;
        $picbigdest = $prodimagedir . $final_image_big;

        $picsmallname = $final_image_small;
        $picbigname = $final_image_big;

        if (!is_readable($prodimagedir) || !is_writable($prodimagedir)) {
            $error = true;
            $message .= CHANGE_PERMISSION."<br>";
        }

        if ($picsmalltype != "") {
            if (!isValidWebImageType($picsmalltype, $picsmallorgname, $picsmalltempname)) {
                $message .= SMALL_PICTURE." " . $i . "<br>";
                $error = true;
            } else {
                if (file_exists($picsmalldest)) {
                    $message .= SMALL_PIC_EXISTS." " . $i . "<br>";
                    $error = true;
                }
            }
        }
        if ($picbigtype != "") {
            if (!isValidWebImageType($picbigtype, $picbigorgname, $picbigtempname)) {
                $message .= BIG_PICTURE." " . $i . "<br>";
                $error = true;
            } else {
                if (file_exists($picbigdest)) {
                    $message .= BIG_PIC_EXISTS." " . $i . "<br>";
                    $error = true;
                }
            }
        }


        $product_stock = doubleval($product_stock);
        if ($product_stock == "") {
            $message .= PRO_STOCK_EMPTY." " . ($i + 1) . " ! <br>";
        }
        if ($product_stock < 0 || !is_numeric($product_stock)) {
            if ($product_stock < 0) {
                $message .= PRO_STOCK_POSITIVE." " . ($i + 1) . " ! <br>";
            }
        }

        if ($additional_price != "") {
            if ($additional_price < 0 || !is_numeric($additional_price)) {
                $message .= ADDITIONAL_PRICE_POSITIVE." " . ($i + 1) . "! <br>";
            }
        }


        if ($chkcanship == "Y" && $txtDigitalProduct != "Y") {
            if ($product_length == "") {
                $message .= PRO_LEN_EMPTY." " . ($i + 1) . " !<br>";
            }
            if ($product_width == "") {
                $message .= PRO_WIDTH_EMPTY." " . ($i + 1) . " !<br>";
            }
            if ($product_height == "") {
                $message .= PRO_HEIGHT_EMPTY." " . ($i + 1) . " !<br>";
            }
            if ($txtDigitalProduct != "Y") {
                if ($product_shipping_weight == "") {
                    $message .= PRO_SHIPPING_EMPTY." " . ($i + 1) . "!<br>";
                }
            }
        }

        if ($product_length != '') {
            if (!is_numeric($product_length)) {
                $message .= ENTER_VALID_PRO_LEN." " . ($i + 1) . "! <br>";
            }
        }

        if ($product_width != '') {
            if (!is_numeric($product_width)) {
                $message .= ENTER_VALID_WID." " . ($i + 1) . "! <br>";
            }
        }

        if ($product_height != '') {
            if (!is_numeric($product_height)) {
                $message .= ENTER_VALID_HT." " . ($i + 1) . "! <br>";
            }
        }

        if ($txtDigitalProduct != "Y") {
            if (!is_numeric($product_shipping_weight) || $product_shipping_weight == 0) {
                $message .= ENTER_VWT." " . ($i + 1) . "! <br>";
            }
        }
        $product_shipping_weight = doubleval($product_shipping_weight);

        if ($product_shipping_weight < 0) {
            $message .= ENTER_PWT." " . ($i + 1) . "! <br>";
        }
        $reorder_level = doubleval($reorder_level);
        if ($reorder_level != "") {
            if ($reorder_level < 0 || !is_numeric($reorder_level)) {
                $message .= POSITIVE_LEVEL." " . ($i + 1) . "! <br>";
            } else if ($reorder_level > $product_stock) {
                $message .= LEVEL_LESS." " . ($i + 1) . "! <br>";
            }
        }
    }
    /*
     * More Images Validation
    */

    if (isset($_FILES['productMoreImage']['name']) && !empty($_FILES['productMoreImage']['name'])) {
        foreach ($_FILES['productMoreImage']['name'] as $key => $moreImage) {
            if ($moreImage != '') {
                $type = $_FILES['productMoreImage']['type'][$key];
                if (!isValidWebImageType($type)) {
                    $message .= " * Invalid more image " . ($key + 1) . "<br>";
                }
            }
        }
    }

    $moreFiles = array();
    foreach ($_FILES['productMoreImage'] as $k => $l) {
        foreach ($l as $j => $v) {
            if (!array_key_exists($j, $moreFiles)) {
                $moreFiles[$j] = array();
            }
            $moreFiles[$j][$k] = $v;
        }
    }






    /*
     * More Images Validation - End
    */

    if ($message != "") { // error
        $message = "<br>".MSG_CORRECT_THE_ERRORS."<br>" . $message;
    } else { // no error so insert user details
        // Upload Digital Products
        $digital_product_upload = 0;

        $digitalproductname = preg_replace("/^[^a-z0-9.-]?(.*?)[^a-z0-9]?$/i", "$1", $_FILES['txtProductZip']['name']);
        $digitalproductname = trim(str_replace(' ', '_', $digitalproductname));

        $assignedname = randomDigitalProduct() . "_" . $digitalproductname;

        if (@file_exists("../digital_product/" . $assignedname)) {
            $message .= PLEASE_RENAME." " . "<br>";
        }

        if ($txtDigitalProduct == "Y" && $_FILES['txtProductZip']['size'] > $maximagesizeinPHP) {
            $message.= FILE_SIZE_EXCEEDS." $maximagesizestringinPHP. <br>";
        }


        if ($txtDigitalProduct == "Y" && $message == '') {

            if (move_uploaded_file($_FILES['txtProductZip']['tmp_name'], "../digital_product/" . $assignedname)) {
                $digital_product_upload = 1;
                chmod("../digital_product/$assignedname", 0777);
            } else {
                $message .= COULDNT_UPLOAD." " . "<br>";
            }
        }





        if ($message == '') {

            $sql = "INSERT INTO " . $tableprefix . "products (
			product_name ,
			product_code ,
			product_description,
			product_price,
			vdigital_product,
			product_category,
			product_artist_id,
			date_added,
			vapproved,
                        payment_status
			) VALUES (
			'" . addslashes($txtProductName) . "',
			'" . addslashes($txtProductCode) . "',
			'" . addslashes($txtDescription) . "',
			'" . addslashes($txtPrice) . "',
			'" . $txtDigitalProduct . "',
			'" . addslashes($catid) . "',
			'" . addslashes($ddlArtist) . "',
			now(),
                        'Y',
                        'COMPLETED'
			)";
            mysql_query($sql) or die(mysql_error());

            $product_insert_id = mysql_insert_id();

            //Product Options Insertion here Santhosh thomas Alexander
            for ($i = 0; $i < $options_count; $i++) {

                $default_option = $_POST['default_option'][$i];
                $additional_price = $_POST['additional_price'][$i];
                $product_stock = $_POST['product_stock'][$i];
                $reorder_level = $_POST['reorder_level'][$i];
                $product_shipping_weight = $_POST['product_shipping_weight'][$i];
                $discount = $_POST['discount'][$i];
                $discount_description = $_POST['discount_description'][$i];
                $product_length = $_POST['product_length'][$i];
                $product_width = $_POST['product_width'][$i];
                $product_height = $_POST['product_height'][$i];
                $default_option_actual = "NO";
                if ($default_option == 1) {
                    $default_option_actual = "YES";
                }
                $small_image_name_after_upload = "";
                $big_image_name_after_upload = "";

                $dir_dest = "../products";

                $picsmallname = ReplaceArrayImage($_FILES['image_small']['name'][$i]);

                $final_image_small = "";

                if ($picsmallname != "") {
                    $picSmallArr = explode('.', $picsmallname);
                    $cntCountParamsSmall = count($picSmallArr);
                    $name_small_with_out_extension = str_replace('.' . $picSmallArr[$cntCountParamsSmall - 1], '', $picsmallname);
                    $final_image_small = "productsmall_" . $productid . "_" . $i . "_" . time() . $name_small_with_out_extension;
                }
                $picsmallname = $final_image_small;

                // then we check if the file has been uploaded properly
                // in its *temporary* location in the server (often, it is /tmp)

                if ($picsmallname != "") {
                    $files = array();
                    foreach ($_FILES['image_small'] as $k => $l) {
                        foreach ($l as $j => $v) {
                            if (!array_key_exists($j, $files))
                                $files[$j] = array();
                            $files[$j][$k] = $v;
                        }
                    }

                    for ($x = 0; $x < count($files); $x++) {
                        if ($x == $i) {
                            $handle = new Upload($files[$x]);
                            if ($handle->uploaded) {
                                // Modification resize
                                //   $info = getimagesize($inputFileName);

                                $handle->image_resize = true;
                                $handle->image_ratio_y = true;
                                $handle->image_x = 191;
                                $handle->image_y = 183;
                                $handle->file_new_name_body = $picsmallname;
                                $handle->Process($dir_dest);

                                $small_image_name_after_upload = $handle->file_dst_name;
                            }
                        }
                    }
                }
                //small image upload end
                //big Image Upload

                $picbigname = ReplaceArrayImage($_FILES['image_big']['name'][$i]);

                $final_image_big = "";
                if ($picbigname != "") {
                    $picBigArr = explode('.', $picbigname);
                    $cntCountParamsBig = count($picBigArr);
                    $name_big_with_out_extension = str_replace('.' . $picBigArr[$cntCountParamsBig - 1], '', $picbigname);
                    $final_image_big = "productbig_" . $productid . "_" . $i . "_" . time() . $name_big_with_out_extension;
                }
                $picbigname = $final_image_big;

                // then we check if the file has been uploaded properly
                // in its *temporary* location in the server (often, it is /tmp)

                if ($picbigname != "") {
                    $files = array();
                    foreach ($_FILES['image_big'] as $k => $l) {
                        foreach ($l as $j => $v) {
                            if (!array_key_exists($j, $files))
                                $files[$j] = array();
                            $files[$j][$k] = $v;
                        }
                    }
                    for ($x = 0; $x < count($files); $x++) {
                        if ($x == $i) {
                            $handle = new Upload($files[$x]);
                            if ($handle->uploaded) {

                                $handle->image_resize = true;
                                $handle->image_ratio_y = true;
                                $handle->image_x = 283;
                                $handle->image_y = 269;
                                $handle->file_new_name_body = $picbigname;
                                $handle->Process($dir_dest);
                                $big_image_name_after_upload = $handle->file_dst_name;
                            }
                        }
                    }
                }
                //big image upload end
                //Product option Images upload end
                //small Image Upload
                /*
                  $picsmallname 		= ReplaceArrayImage($_FILES['image_small']['name'][$i]);

                  $final_image_small = "";

                  if($picsmallname != "") {
                  $picSmallArr            = explode('.', $picsmallname);
                  $cntCountParamsSmall    = count($picSmallArr);
                  $name_small_with_out_extension      = str_replace('.'.$picSmallArr[$cntCountParamsSmall-1], '', $picsmallname);
                  $final_image_small      = "productsmall_".$product_insert_id."_".$i."_".time().$name_small_with_out_extension;

                  }
                  $picsmallname = $final_image_small;

                  // then we check if the file has been uploaded properly
                  // in its *temporary* location in the server (often, it is /tmp)

                  if($picsmallname !="") {
                  $files = array();
                  foreach ($_FILES['image_small'] as $k => $l) {
                  foreach ($l as $j => $v) {
                  if (!array_key_exists($j, $files))
                  $files[$j] = array();
                  $files[$j][$k] = $v;
                  }
                  }

                  for($x=0;$x < count($files);$x++) {
                  if($x ==$i) {
                  $handle = new Upload($files[$x]);
                  if ($handle->uploaded) {

                  $handle->image_resize            = true;
                  //$handle->image_ratio_y           = true;
                  $handle->image_x                 = 191;
                  $handle->image_y                 = 183;
                  $handle->file_new_name_body      = $picsmallname;
                  $handle->Process($dir_dest);

                  $small_image_name_after_upload  = $handle->file_dst_name;

                  }
                  }
                  }
                  }
                  //small image upload end
                  //big Image Upload

                  $picbigname 		= ReplaceArrayImage($_FILES['image_big']['name'][$i]);

                  $final_image_big = "";
                  if($picbigname != "") {
                  $picBigArr            = explode('.', $picbigname);
                  $cntCountParamsBig    = count($picBigArr);
                  $name_big_with_out_extension      = str_replace('.'.$picBigArr[$cntCountParamsBig-1], '', $picbigname);
                  $final_image_big      = "productbig_".$product_insert_id."_".$i."_".time().$name_big_with_out_extension;

                  }
                  $picbigname = $final_image_big;

                  // then we check if the file has been uploaded properly
                  // in its *temporary* location in the server (often, it is /tmp)

                  if($picbigname !="") {
                  $files = array();
                  foreach ($_FILES['image_big'] as $k => $l) {
                  foreach ($l as $j => $v) {
                  if (!array_key_exists($j, $files))
                  $files[$j] = array();
                  $files[$j][$k] = $v;
                  }
                  }
                  for($x=0;$x < count($files);$x++) {
                  if($x ==$i) {
                  $handle = new Upload($files[$x]);
                  if ($handle->uploaded) {

                  $handle->image_resize            = true;
                  $handle->image_x                 = 283;
                  $handle->image_y                 = 269;
                  $handle->file_new_name_body      = $picbigname;
                  $handle->Process($dir_dest);
                  $big_image_name_after_upload  = $handle->file_dst_name;

                  }
                  }
                  }
                  }
                  //big image upload end
                  //Product option Images upload end
                */

                $sqlOptions = "INSERT INTO " . $tableprefix . "product_options (
			product_id,
			additional_price,
			product_stock,
			reorder_level,
			product_shipping_weight,
                        image_small,
                        image_big,
                        discount,
                        discount_description,
                        product_length,
                        product_width,
                        product_height,
                        default_option
			) VALUES (
			'" . addslashes($product_insert_id) . "',
			'" . addslashes($additional_price) . "',
			'" . addslashes($product_stock) . "',
			'" . addslashes($reorder_level) . "',
			'" . addslashes($product_shipping_weight) . "',
			'" . addslashes($small_image_name_after_upload) . "',
			'" . addslashes($big_image_name_after_upload) . "',
			'" . addslashes($discount) . "',
			'" . addslashes($discount_description) . "',
                        '" . addslashes($product_length) . "',
                        '" . addslashes($product_width) . "',
                        '" . addslashes($product_height) . "',
                        '" . addslashes($default_option_actual) . "'
			)";
                mysql_query($sqlOptions) or die(mysql_error());
                $product_option_id = mysql_insert_id();
                $options_arr = getOptionsFromCategory($catid);
                $num_of_options = count($options_arr);
                if ($num_of_options != "") {
                    foreach ($options_arr AS $eachoption) {

                        $option_Id = $eachoption['option_id'];
                        $option_value = $_POST["option_" . $option_Id][$i];
                        if ($option_value != "") {

                            $query_option_insert = "INSERT INTO " . $tableprefix . "product_option_values
                                                (
                                                product_option_id,
                                                option_id,
                                                value
                                                )
                                                VALUES
                                                (
                                                '" . $product_option_id . "',
                                                '" . $option_Id . "',
                                                '" . $option_value . "'
                                                )";
                            mysql_query($query_option_insert);
                        }
                    }
                }
            }




            if ($txtDigitalProduct == "Y" && $digital_product_upload == '1') {

                $expiry_days = $_POST['txtDownloadExpiry'];
                //$valid_date_upto = date("Y-m-d H:s:m",mktime(date("H"),date("i"),date("s"),date("m"),date("d")+$expiry_days,date("Y")));

                $insert_digital_product = "INSERT INTO " . $tableprefix . "digital_product(ndigital_productid,vdigital_product_name,ndownload_try
							  		  ,ndownload_expiry_days)VALUES(" . $product_insert_id . ","
                        . GetSQLValueString($assignedname, "text") . ",
									  " . $_POST['txtDownloadTry'] . ",
									  '" . $expiry_days . "')";

                mysql_query($insert_digital_product) or die(mysql_error());
            }

            /*
             * Product more images By Binu Chandran.E
            */
            $moreFiles = array();
            foreach ($_FILES['productMoreImage'] as $k => $l) {
                foreach ($l as $j => $v) {
                    if (!array_key_exists($j, $moreFiles)) {
                        $moreFiles[$j] = array();
                    }
                    $moreFiles[$j][$k] = $v;
                }
            }
            $dir_dest = "../products";
            for ($x = 0; $x < count($moreFiles); $x++) {
                $moreImageName = ReplaceArrayImage($moreFiles[$x]['name']);
                if ($moreImageName != "") {
                    $handle = new Upload($moreFiles[$x]);
                    if ($handle->uploaded) {

                        $moreImageName = "productmore_" . $x . time() . $moreImageName;


                        $handle->image_resize = true;
                        $handle->image_x = 283;
                        $handle->image_y = 269;
                        $handle->file_new_name_body = substr($moreImageName, 0, strrpos($moreImageName, '.'));
                        $handle->Process($dir_dest);
                        $moreImageName = $handle->file_dst_name;

                        $insertMImageQuery = "INSERT INTO " . $tableprefix . "product_view_images
                                                      SET nimage_productid = '" . mysql_real_escape_string($product_insert_id) . "',
                                                          vimage_name= '" . mysql_real_escape_string($moreImageName) . "',
                                                          vproduct_image_delete_status 	='N'";
                        mysql_query($insertMImageQuery) or die(mysql_error());
                    }
                }
            }



            $txtSearch = "";
            $txtProductName = "";
            $txtProductCode = "";
            $txtDescription = "";
            $txtPrice = "";
            $ddlArtist = "";

            //update count
            $routesql = "Select route from " . $tableprefix . "categories where category_id ='$catid'";
            //echo $routesql;
            $result = mysql_query($routesql);
            $row = mysql_fetch_array($result);
            $route = $row["route"];
            $qryupdatecatcount = "update " . $tableprefix . "categories set count=count+1 where category_id in($route)";
            mysql_query($qryupdatecatcount);

            $message .= PRO_ADD_SCC;

            header("Location:catalog.php?message=$message&catid=$catid");
            exit;
        }
    }
} else if ($_POST["btnDeleteSelected"]) {
    if (!isNotNull($productlist)) {
        $message .= DELETE_PRO." <br>";
    } else {
        $cnt = count($productlist);
        $plist = implode(",", $productlist);
        $sql = "DELETE FROM " . $tableprefix . "products WHERE product_id IN ($plist) ";
        $res = mysql_query($sql);

        $select_product_images = "SELECT image_small,image_big
                                                      FROM " . $tableprefix . "product_options WHERE product_id IN(" . $plist . ")";


        $result_product_images = mysql_query($select_product_images) or die(mysql_error());

        if (mysql_num_rows($result_product_images) > 0) {
            while ($product_images_row = mysql_fetch_array($result_product_images)) {
                $imagesmall_thumb = $product_images_row['image_small'];

                $imagelarge_thumb = $product_images_row['image_big'];

                @unlink("../products/" . $imagesmall_thumb);
                @unlink("../products/" . $imagelarge_thumb);
            }
        }
        $sql_options = "DELETE FROM " . $tableprefix . "product_options WHERE product_id IN ($plist) ";
        $del_options = mysql_query($sql_options);

        $select_product_images_more = "SELECT im.vimage_name
                                                   FROM " . $tableprefix . "product_view_images im
                                                   WHERE im.nimage_productid IN(" . $plist . ")";
        $rs_product_image_more = mysql_query($select_product_images_more) or die(mysql_error());

        if (mysql_num_rows($rs_product_image_more) > 0) {

            while ($prows = mysql_fetch_array($rs_product_image_more)) {
                $moreimage = $prows['vimage_name'];

                $moreimage_thumb = "thumb_" . $prows['vimage_name'];

                @unlink("../products/bulk_images/" . $moreimage);

                @unlink("../products/bulk_images/" . $moreimage_thumb);
            }
        }

        $sql_product_images_more = "DELETE FROM " . $tableprefix . "product_view_images WHERE nimage_productid IN ($plist) ";
        $del_product_images_more = mysql_query($sql_product_images_more);
    }
}

$artistlist = makeArtistList();


$sql = " SELECT p.product_id,p.product_name,p.product_code,(p.product_price+po.additional_price) as product_price,p.date_added,s.artist_name
            FROM " . $tableprefix . "products p
            INNER JOIN " . $tableprefix . "product_options po ON po.product_id = p.product_id AND po.default_option = 'YES'
            INNER JOIN " . $tableprefix . "artists s ON p.product_artist_id = s.artist_id
            WHERE product_category = '" . addslashes($catid) . "' " . $qryopt . "   ORDER BY p.product_name  ";


/* $sql = "SELECT cf.vcustom_field_name,cf_value.vfield_name_value
  FROM ".$tableprefix."custom_fields_value cf_value,".$tableprefix."custom_fields cf
  WHERE cf_value.ncustom_field_id = cf.ncustom_id
  AND vcustom_field_form_id = '2'"; */

$sess_back = $_SERVER["PHP_SELF"] . "?begin=" . $begin . "&num=" . $num . "&numBegin=" . $numBegin . "&cmbSearchType=" . $cmbSearchType . "&txtSearch=" . $txtSearch . "&catid=" . $catid;
$_SESSION["gbackurl"] = $sess_back;

$totalrows = mysql_num_rows(mysql_query($sql));
$txtSearch1 = urlencode($txtSearch);
$navigate = pageBrowser($totalrows, 10, 10, "&cmbSearchType=$cmbSearchType&txtSearch=$txtSearch1&catid=$catid", $_GET[numBegin], $_GET[start], $_GET[begin], $_GET[num]);
$sql = $sql . $navigate[0];
$rs = mysql_query($sql);

include "../includes/htmltop.php";
?>
<style>
    /*.tooltip {
        display:none;
        background:transparent url(../images/tooltip/black_arrow.png);
        font-size:12px;
        height:70px;
        width:160px;
        padding:25px;
        color:#fff;
    }*/

    .tooltip {
        background-color: black;;
        filter: alpha(opacity=75)-moz-opacity:0.75;
        opacity: 0.75;
        border: 2px solid #FFFFFF;

        border-radius:5px 5px 5px 5px;
        color: white;
        font-size: 1.2em;
        left: -12px;
        padding: 10px;
        position: relative;
        top: 21px;
        width: 200px;
    }
    .tooltip:after, .tooltip:before
    {
        top: 100%;
        border: solid transparent;
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
    }
    .tooltip:after
    {
        border-color: rgba(10, 13, 15, 0);
        border-top-color: #0a0d0f;
        border-width: 10px;
        left: 50%;
        margin-left: -10px;
    } .tooltip:before

    {
        border-color: rgba(245, 245, 245, 0);
        border-top-color: #f5f5f5;
        border-width: 16px;
        left: 50%;
        margin-left: -16px;
    }

</style>
<script language="JavaScript" type="text/JavaScript" src="../js/jquery.tools.min.js"></script>
<script language="JavaScript" type="text/JavaScript">
    $(document).ready(function(){
        $(".help a[title]").tooltip();

        $('#txtSearch').keypress(function(e){

            if(e.keyCode==13){

                clickSearch();
            }
        });

    })
    function clickSearch()
    {

        document.frmCatalog.submit();

    }
    function confirmDelete(){
        if(confirm("<?php echo ARE_YOU_SURE_DELETE; ?>") ){
            return true;
        }
        return false;
    }
    function checkDigitalProduct()
    {

        if(document.frmCatalog.txtDigitalProduct.checked == true)
        {

            document.getElementById("digitalProductBlock").style.display = "";
            $(".canshipBlock").hide();

        }
        else
        {

            document.getElementById("digitalProductBlock").style.display = "none";
            $(".canshipBlock").show();

        }

    }

    function add_more_upload()
    {
        var new_total = Math.round(document.frmCatalog.TOTAL_FILE.value) + 1;

        if(new_total<=document.frmCatalog.MAX_NO_IMAGES.value)
        {
            document.getElementById('pic'+document.frmCatalog.TOTAL_FILE.value).style.display='';
            document.frmCatalog.TOTAL_FILE.value++;
        }//end if
    }//end funciton

    function checkEnter(e){
        e = e || event;
        return (e.keyCode || event.which || event.charCode || 0) !== 13;
    }

</script>
<body>
    <center>
        <div align="left" id="layout">
            <!--header area start -->
            <?php
            include_once("../includes/topheader_admin.php")
                    ?>
            <!--header area end -->
            <div align="left" class="main_body">
                <!--main body start -->
                <?php
                include_once("../includes/admintopmenu.php")
                        ?>
                <div align="left" class="content_area">
                    <!--content area start -->
                    <div align="left" class="page_title"><?php echo CATALOG; ?></div>
                    <div align="center" class="text_information grey_tr"><?php
                        if ($_GET['message'] != '') {
                            $message = $_GET['message'];
                        }
                        ?><?php echo $message ?></div>
                    <br>
                    <div align="left">
                        <form name="frmCatalog" action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST" enctype="multipart/form-data" onkeypress="return checkEnter(event)">
                            <input type="hidden" name="catid" value="<?php echo htmlentities($catid); ?>" >
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
                                <tr>
                                    <td colspan="3">
                                        <fieldset>
                                            <legend class='page_title2'><?php echo EXISTING_CAT; ?></legend>
                                            <table width="100%" class="category_bx" border=0 align="left">
                                                <tr><td colspan="2" class="sub_hds" align='left' style='word-break:break-all;'><?php echo getCategoryLink("catalog.php", $catid) ?></td></tr>
                                                <tr><td colspan="2">
                                                        <table width="70%" cellpadding="5">

                                                            <?php
                                                            if (!isset($catid) || $catid == "") {
                                                                $catid = 0;
                                                            }
                                                            $sql = "SELECT * FROM " . $tableprefix . "categories WHERE parent_id = '" . addslashes($catid) . "'
												 ORDER BY category_name ";
                                                            $res = mysql_query($sql);
                                                            $odd = true;
                                                            if (mysql_num_rows($res) != 0) {
                                                                ?>
                                                            <tr>
                                                                <th><?php echo NAME; ?></th>
                                                                <th><?php echo ADD_SUBCAT; ?></th>
                                                                <th><?php echo DETAILS; ?></th>
                                                                <th><?php echo EDIT; ?></th>
                                                                <th><?php echo DELETE; ?></th>
                                                            </tr>
                                                                <?php
                                                                while ($row = mysql_fetch_array($res)) {
                                                                    if ($row["gift_certificate_flag"] == 'Y') {
                                                                        $link_go = '#';
                                                                    } else {
                                                                        $link_go = 'catalog.php?catid=' . $row["category_id"] . '&parent=' . $catid . '';
                                                                    }
                                                                    ?>
                                                            <tr>
                                                                <td><?php echo "<a class='links' href='" . $link_go . "'>" . stripslashes($row["category_name"]) . "</a>&nbsp;&nbsp;"; ?></td>
                                                                <td><?php if ($row["gift_certificate_flag"] != 'Y') { ?> <?php echo "<a class='links' href='addcategory.php?catid=" . $row["category_id"] . "&parent=" . $catid . "' >+ ".ADD_SUBCAT."</a>"; ?><?php } ?></td>
                                                                <td><?php echo "<a class='links' href='" . $link_go . "'>".DETAILS."</a>"; ?></td>
                                                                <td><?php if ($row["gift_certificate_flag"] != 'Y') { ?>    <?php echo "<a href='editcategory.php?catid=" . $row["category_id"] . "&parent=" . $catid . "'  class='links' >".EDIT."</a>&nbsp;" ?> <?php } ?></td>
                                                                <td><?php if ($row["gift_certificate_flag"] != 'Y') { ?> <?php echo "<a class='links' href='catalog-delete.php?catid=" . $row["category_id"] . "&parent=" . $catid . "' onClick='return confirm(\"".ARE_U_SURE."\");'>".DELETE."</a>"; ?> <?php } ?></td>
                                                            </tr>
                                                                    <?php
                                                                }
                                                            }
                                                            ?>
                                                        </table>
                                                    </td></tr>
                                                <tr><td colspan="2">&nbsp;</td></tr>

                                                <tr><td colspan="2">&nbsp;</td></tr>
                                            </table>
                                        </fieldset>

                                    </td>
                                </tr>
                                <tr><td colspan="3">
                                        <?php if (!($catid > 0)) {
                                            ?>
                                        <input class="button" type="button" onclick="window.location='addcategory.php'" value="<?php echo ADD_CATEGORY; ?>"/>
                                            <?php } ?>
                                    </td></tr>
                                <?php
                                $checkleafcategory = " select *  FROM  " . $tableprefix . "categories where parent_id='$catid'";
                                $showproductadd = true;
                                if (mysql_num_rows(mysql_query($checkleafcategory)) > 0) {
                                    $showproductadd = false;
                                }
                                if (isset($catid) and $catid != "" and $showproductadd) {
                                    ?>
                                <tr>
                                    <td colspan="3">
                                        <fieldset>
                                            <legend class='page_title2'><?php echo PRO_UNDER; ?></legend>
                                            <table width="100%" border=0 >
                                                <tr>
                                                    <td colspan="2" class=listingtdbgcolor>
                                                        <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
                                                        <table width="100%"  border="0" cellpadding="2" cellspacing="1" class="maintext" >
                                                            <tr>
                                                                <td colspan="3" align="right"><span class="required">*</span><?php echo MANDATORY_FIELDS; ?></td>
                                                            </tr>
                                                            <tr class="whitebg" valign="top">
                                                                <td align="right" colspan="6" valign="top" >
                                                                    <table width="100%" border=0  >
                                                                        <tr>
                                                                            <td width="100%" align=right valign="baseline" class=""><?php echo SEARCH; ?>
                                                                                <select name="cmbSearchType" class="textarea">
                                                                                    <option value="all"><?php echo ALL_PRODUCTS; ?></option>
                                                                                    <option value="productname"  <?php
                                                                                        if ($cmbSearchType == "productname" || $cmbSearchType == "") {
                                                                                            echo("selected");
                                                                                        }
                                                                                                ?>><?php echo PRODUCT_NAME; ?></option>
                                                                                    <option value="artistname"  <?php
                                                                                        if ($cmbSearchType == "artistname") {
                                                                                            echo("selected");
                                                                                        }
                                                                                                ?>><?php echo TEXT_SELLER_NAME; ?></option>
                                                                                    <option value="dateadded"  <?php
                                                                                        if ($cmbSearchType == "dateadded") {
                                                                                            echo("selected");
                                                                                        }
                                                                                                ?>><?php echo DATE_ADDED.TEXT_DATE_FORMAT; ?></option>
                                                                                </select> &nbsp;
                                                                                <input type="text" name="txtSearch" id="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch)); ?>"  class="textarea">
                                                                                &nbsp;
                                                                                <a href="javascript:clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>&nbsp;
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </td>
                                                            </tr>
                                                            <tr><td align="left" colspan="6"><?php echo("Listing $navigate[1] of $totalrows results."); ?></td></tr>
                                                            <tr><td><table border="0" width="100%" class='manage_table' cellpadding="0" cellspacing="0">
                                                                        <tr class="form_header">
                                                                            <td>#</td>
                                                                            <th>&nbsp;</th>
                                                                            <th align='left'><?php echo PRODUCT_NAME; ?></th>
                                                                            <th align='left'><?php echo TEXT_PRODUCT_CODE; ?></th>
                                                                            <th align="left"><?php echo PRICE; ?></th>
                                                                            <th align='left'><?php echo REGISTERED.TEXT_DATE_FORMAT; ?></th>
                                                                            <th align='left'><?php echo SELLER; ?></th>
                                                                        </tr>
                                                                            <?php
                                                                            $i = $begin + 1;
                                                                            while ($row = mysql_fetch_array($rs)) {
                                                                                $link = "<a href='editproduct.php?productid=" . $row["product_id"] . "' class='links'>";
                                                                                $check = "<input class='checkbox' type='checkbox' name='chkproducts[]' value='" . $row["product_id"] . "' class='smalllink'> ";
                                                                                ?>
                                                                        <tr class="whitebg" valign=top>
                                                                            <td><?php echo (($page * $limit) + $i - $limit) ?></td>
                                                                            <td valign=top align='left'><?php echo $check; ?></td>
                                                                            <td valign=top align='left' style='word-break:break-all;' class=boxalternatecolor2><?php echo $link . stripslashes($row["product_name"]) . "</a>"; ?></td>
                                                                            <td valign=top align='left'><?php echo $link . $row["product_code"] . "</a>"; ?></td>
                                                                            <td valign=top align='left'> <?php echo $adminCurrency['currency_symbol']; ?> <?php echo $link . htmlentities(number_format($row["product_price"], 2, '.', '')) . "</a>"; ?></td>
                                                                            <td valign=top align='left'><?php echo $link . dateFormat($row["date_added"], "Y-m-d", "m/d/Y") . "</a>"; ?></td>
                                                                            <td valign=top align='left' class="boxalternatecolor"><?php echo $link . $row["artist_name"] . "</a>"; ?>&nbsp;</td>
                                                                        </tr>
                                                                                <?php
                                                                                $i++;
                                                                            }
                                                                            ?>
                                                                        <tr class="whitebg"><td align="center" colspan="6"><?php echo($navigate[2]); ?></td></tr>
                                                                    </table></td></tr>
                                                                <?php if (isNotNull($check)) { ?>
                                                            <tr class="whitebg">
                                                                <td colspan="6" align="center"><input type="submit" class="button" name="btnDeleteSelected" value="<?php echo TEXT_DELETE_SELECTED; ?>" onClick="return confirmDelete();" ></td>
                                                            </tr>
                                                                    <?php
                                                                }
                                                                ?>
                                                            <tr class="whitebg">
                                                                <td colspan="6">&nbsp;</td>
                                                            </tr>
                                                        </table>
                                                        <!-- products adding area -->
                                                    </td>
                                                </tr>
                                                <script type="text/javascript" src="<?php echo SITE_URL ?>/ckeditor/ckeditor.js"></script>
                                                    <?php
                                                    $options_arr = getOptionsFromCategory($catid);
                                                    $num_of_options = count($options_arr);
                                                    $StrOptions = "";
                                                    if ($num_of_options != "") {
                                                        foreach ($options_arr AS $eachoption) {
                                                            $StrOptions .='<br>' . $eachoption['option_name'];
                                                            $StrOptions .='<input type="text" name="option_' . $eachoption['option_id'] . '[]" value="">';
                                                        }
                                                    }
                                                    ?>
                                                <script type="text/javascript">

                                                    window.onload = function()
                                                    {
                                                        CKEDITOR.replace( 'editor1' );
                                                    };
                                                    //Santhosh thomas Alexander coded this
                                                    //This is to do dynamic options add
                                                    $(document).ready(function(){

                                                        $("#add_more_options").click(function() {

                                                            var block = '<tr class="option_block"><td><table cellpadding="2" cellspacing="2" border="0" bgcolor="#CCCCCC"><tr><th><?php echo DEFAULT_OPT; ?></th><th><?php echo ADD_PRICE; ?> &nbsp;(<?php echo $adminCurrency['currency_code']; ?>)</th><th><?php echo PRODUCT_STOCK; ?>&nbsp;<span class="required">*</span></th><th><?php echo REORDER_LEVEL; ?>&nbsp;</th></tr><tr><td><span class="default_setting_box"><input type="radio" name="default" id="default" onclick="setDefaultOption(this);"><input type="hidden" class="default_option" name="default_option[]"  id="default_option[]" value=""><?php echo $StrOptions ?></span></td><td><input type="text" name="additional_price[]" id="additional_price[]" value=""></td><td><input type="text" name="product_stock[]" id="product_stock[]" value=""></td><td><input type="text" name="reorder_level[]" id="reorder_level[]" value=""></td></tr><tr><th><?php echo SMALL_IMG; ?>&nbsp;</th><th><?php echo BIG_IMG; ?>&nbsp;</th><th><?php echo DISCOUNT_PERC; ?></th><th><?php echo DISCOUNT_DESC; ?></th></tr><tr><td><input type="file" name="image_small[]" id="image_small[]" value=""></td><td><input type="file" name="image_big[]" id="image_big[]" value=""></td><td><input type="text" name="discount[]" id="discount[]" value=""></td><td><input type="text" name="discount_description[]" id="discount_description[]" value=""></td></tr><tr class="canshipBlock"><th><?php echo SW_LBS; ?>&nbsp;<span class="required">*</span></th><th><?php echo LENGTH; ?><span class="required">*</span> (<?php echo CM; ?>) </th><th><?php echo WIDTH; ?> <span class="required">*</span>(<?php echo CM; ?>) </th><th><?php echo HEIGHT; ?> <span class="required">*</span>(<?php echo CM; ?>) </th></tr><tr class="canshipBlock"><td><input type="text" name="product_shipping_weight[]" id="product_shipping_weight[]" value=""></td><td><input type="text" name="product_length[]" id="product_length[]" value=""></td><td><input type="text" name="product_width[]" id="product_width[]" value=""></td><td><input type="text" name="product_height[]" id="product_height[]" value="">&nbsp;<img border="0" title="<?php echo TITLE_DELETE_OPTION; ?>" src="../images/btn_del.gif" alt="<?php echo TITLE_DELETE_OPTION; ?>" class="delete_option" onclick="javascript:delete_option(this);"></td></tr></table></td></tr>';
                                                            $("#options_window").append(block);
                                                            checkDigitalProduct();
                                                        });

                                                    });
                                                    function setDefaultOption(obj) {

                                                        $(".default_option").val("");
                                                        $(obj).parents(".default_setting_box").find(".default_option").val("1");


                                                    }
                                                    function delete_option(obj) {

                                                        $(obj).parents(".option_block").hide("slow");
                                                        $(obj).parents(".option_block").html("");
                                                    }

                                                </script>
                                                <tr><td colspan="2" bgcolor="#FFFFFF">&nbsp;</td></tr>
                                                <tr>
                                                    <td  colspan="2" bgcolor="#FFFFFF">
                                                        <!-- ***************************************************************** -->
                                                        <table cellpadding="2" class="bx_bg" cellspacing="2" width="100%">
                                                            <tr><td colspan="3" align="left" class="page_title2"><b><?php echo ADD_NEW_PRO; ?></b></tr>
                                                            <tr><td colspan="3"></tr>
                                                            <tr><td align="right" width="33%"><?php echo PRODUCT_NAME; ?> &nbsp; <span class="required">*</span></td>
                                                                <td width="3%" align='left'></td>
                                                                <td width="64%" align='left'><input name="txtProductName" type="text" class="textarea" id="txtProductName" value="<?php echo $txtProductName; ?>" size="30" maxlength="100">
                                                                    <span class="help"><a href="#" title="<?php echo PRODNAME_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                </td></tr>
                                                            <tr><td align="right" width="33%"><?php echo TEXT_PRODUCT_CODE; ?> &nbsp; </td>
                                                                <td width="3%" align='left'></td>
                                                                <td  align='left'><input name="txtProductCode"  type="text" class="textarea" id="txtProductCode" value="<?php echo $txtProductCode; ?>" size="20" maxlength="20">
                                                                    <span class="help"><a href="#" title="<?php echo PRODID_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                </td></tr>
                                                            <tr><td align="right" ><?php echo SELLER; ?>&nbsp;<span class="required">*</span></td>
                                                                <td ></td>
                                                                <td align='left'><?php echo makeDropDownList("ddlArtist", $artistlist, $ddlArtist, true, 'textarea', $properties, $behaviors) ?>
                                                                    <span class="help"><a href="#" title="<?php echo PRODSELLER_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                </td></tr>
                                                            <tr><td align="right" valign="top"><?php echo PRODUCT_DESCRIPTION_TEXT; ?>&nbsp;<span class="required">*</span></td>
                                                                <td valign="top" ></td>
                                                                <td align='left'>
                                                                    <textarea id="editor1" name="txtDescription"><?php echo stripslashes($_POST['txtDescription']); ?></textarea>
                                                                </td></tr>
                                                            <tr><td align="right" ><?php echo PRICE; ?>&nbsp;<span class="required">*</span></td>
                                                                <td ></td>
                                                                <td align='left'><input name="txtPrice"  type="text" class="textarea" id="txtPrice" value="<?php echo htmlentities($txtPrice); ?>" size="30" maxlength="100">
                                                                    &nbsp;<b><?php echo $adminCurrency['currency_code']; ?></b>
                                                                    <span class="help"><a href="#" title="<?php echo PROD_PRICE_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                </td>
                                                            </tr>
                                                                <tr>
                                                                <td align="right"><?php echo DIGITAL_PRODUCT; ?>&nbsp;</td>
                                                                <td></td>
                                                                <td align='left'><input type="checkbox" id="txtDigitalProduct"  name="txtDigitalProduct" value="Y" onClick="checkDigitalProduct();" <?php echo $chkDigitalProduct; ?>>
                                                                    <span class="help"><a href="#" title="<?php echo DWNLD_PROD_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                </td>
                                                            </tr>

                                                            <tr>
                                                                <td colspan="3">
                                                                    <div id="digitalProductBlock" style="display:<?php echo $digitalBlock; ?>">
                                                                        <table cellpadding="2" cellspacing="2" width="100%" border="0">
                                                                            <tr>
                                                                                <td width="39%" align="right"><?php echo UPLOAD_DIGPRO; ?>&nbsp;<span class="required">*</span></td>
                                                                                <td width="3%" align="left"></td>
                                                                                <td width="21%" align='left'><input type="file" class="textarea" name="txtProductZip"></td>
                                                                                <td width="37%" class="text_information" align="left"><?php echo TEXT_FILE_TYPE; ?> :&nbsp;.zip,.pdf, .rar , .wma,.psd , .opf, azw,.lit,.pdb, .pkg,.mp3,.mpeg,.mp4,.avi  <br>
                                                                                    <?php echo MAX_ALLOWED_SIZE; ?>   <b><?php echo $maximagesizestringinPHP; ?></b>
                                                                                    <span class="help"><a href="#" title="<?php echo FILE_EXTN_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td width="39%" align="right"><?php echo DWNLD_TRY;?>&nbsp;</td>
                                                                                <td width="3%" align="left"></td>
                                                                                <td colspan="2" align="left">
                                                                                    <select name="txtDownloadTry" class="textarea">
                                                                                            <?php
                                                                                            for ($i = 1; $i <= 10; $i++) {
                                                                                                ?>
                                                                                        <option value="<?php echo $i ?>"><?php echo $i ?></option>
                                                                                                <?php
                                                                                            }
                                                                                            ?>
                                                                                    </select>&nbsp;Time(s)
                                                                                    <span class="help"><a href="#" title="<?php echo NO_DWNLD_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td width="39%" align="right"><?php echo D_EXPIRY; ?>&nbsp;</td>
                                                                                <td width="3%" align="left"></td>
                                                                                <td colspan="2" align="left">
                                                                                    <select name="txtDownloadExpiry" class="textarea">
                                                                                            <?php
                                                                                            for ($i = 1; $i <= 10; $i++) {
                                                                                                ?>
                                                                                        <option value="<?php echo $i ?>"><?php echo $i ?></option>
                                                                                                <?php
                                                                                            }
                                                                                            ?>
                                                                                    </select>&nbsp;Day(s)
                                                                                    <span class="help"><a href="#" title="<?php echo LINK_EXPIRE_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                </td>
                                                                            </tr>
                                                                        </table>
                                                                    </div>
                                                                </td>
                                                            </tr>


                                                            <tr>
                                                                
                                                                <td colspan="3" width="100%">
                                                                    <table cellpadding="2" cellspacing="2" border="0" id="options_window" width="100%">
                                                                        <tr>
                                                                            <td>
                                                                                <table cellpadding="2" cellspacing="2" border="0" bgcolor="#CCCCCC">
                                                                                    <tr>
                                                                                        <th><?php echo DEFAULT_OPT; ?>
                                                                                            <span class="help"><a href="#" title="<?php echo MUL_PROD_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo ADD_PRICE; ?>&nbsp;(<?php echo $adminCurrency['currency_code']; ?>)
                                                                                            <span class="help"><a href="#" title="<?php echo EXTRA_PRICE_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo PRODUCT_STOCK; ?>&nbsp;<span class="required">*</span>
                                                                                            <span class="help"><a href="#" title="<?php echo AVAIL_STK_TOOLTIP; z?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo REORDER_LEVEL; ?>&nbsp;
                                                                                            <span class="help"><a href="#" title="<?php echo STK_LEVEL_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                    </tr>
                                                                                        <?php
                                                                                        $dchecked = "";
                                                                                        $default = 1;
                                                                                        if (count($_POST['default_option']) == 0) {
                                                                                            $dchecked = "checked";
                                                                                        }
                                                                                        if ($_POST['default_option'][0] == "1") {
                                                                                            $dchecked = "checked";
                                                                                            $default = $_POST['default_option'][0];
                                                                                        }
                                                                                        ?>
                                                                                    <tr>
                                                                                        <td>
                                                                                            <span class="default_setting_box">
                                                                                                <input type="radio" name="default" id="default" onclick="setDefaultOption(this);" <?php echo $dchecked; ?>>
                                                                                                <input type="hidden" class="default_option" name="default_option[]"  id="default_option[]" value="<?php echo $default; ?>">

                                                                                                    <?php
                                                                                                    $options_arr = getOptionsFromCategory($catid);
                                                                                                    $num_of_options = count($options_arr);
                                                                                                    if ($num_of_options != "") {
                                                                                                        foreach ($options_arr AS $eachoption) {
                                                                                                            echo "<br>" . $eachoption['option_name'];
                                                                                                            ?>
                                                                                                <input type="text" name="option_<?php echo $eachoption['option_id'] ?>[]" value="<?php echo $_POST['option_' . $eachoption['option_id']][0]; ?>">
                                                                                                            <?php
                                                                                                        }
                                                                                                    }
                                                                                                    ?>
                                                                                            </span>

                                                                                        </td>
                                                                                        <td><input type="text" name="additional_price[]" id="additional_price[]" value="<?php echo $_POST['additional_price'][0]; ?>">

                                                                                        </td>
                                                                                        <td>
                                                                                            <input type="text" name="product_stock[]" id="product_stock[]" value="<?php echo $_POST['product_stock'][0]; ?>">

                                                                                        </td>
                                                                                        <td>
                                                                                            <input type="text" name="reorder_level[]" id="reorder_level[]" value="<?php echo $_POST['reorder_level'][0]; ?>">

                                                                                        </td>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <th><?php echo SMALL_IMG; ?>&nbsp;
                                                                                            <span class="help"><a href="#" title="<?php echo IMGLIST_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo BIG_IMG; ?>&nbsp;
                                                                                            <span class="help"><a href="#" title="<?php echo IMG_DISP_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo DISCOUNT; ?> (%)
                                                                                            <span class="help"><a href="#" title="<?php echo DISC_PERC_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo DISCOUNT_DESC; ?>
                                                                                            <span class="help"><a href="#" title="<?php echo EX_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td><input type="file" name="image_small[]" id="image_small[]" value="<?php echo $_POST['image_small'][0]; ?>"></td>
                                                                                        <td><input type="file" name="image_big[]" id="image_big[]" value="<?php echo $_POST['image_big'][0]; ?>"></td>
                                                                                        <td><input type="text" name="discount[]" id="discount[]" value="<?php echo $_POST['discount'][0]; ?>"></td>
                                                                                        <td><input type="text" name="discount_description[]" id="discount_description[]" value="<?php echo $_POST['discount_description'][0]; ?>"></td>
                                                                                    </tr>
                                                                                    <tr class="canshipBlock" style="display:<?php echo $canshipblock; ?>">
                                                                                        <th><?php echo SW_LBS; ?>&nbsp;<span class="required">*</span>
                                                                                            <span class="help"><a href="#" title="<?php echo PWEIGHT_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo LENGTH; ?> <span class="required">*</span>(cm)
                                                                                            <span class="help"><a href="#" title="<?php echo PLEN_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo WIDTH; ?> <span class="required">*</span>(cm)
                                                                                            <span class="help"><a href="#" title="<?php echo PWIDTH_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                        <th><?php echo HEIGHT; ?> <span class="required">*</span>(cm)
                                                                                            <span class="help"><a href="#" title="<?php echo PHEIGHT_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                                                                        </th>
                                                                                    </tr>
                                                                                    <tr class="canshipBlock" style="display:<?php echo $canshipblock; ?>">
                                                                                        <td><input type="text" name="product_shipping_weight[]" id="product_shipping_weight[]" value="<?php echo $_POST['product_shipping_weight'][0]; ?>"></td>
                                                                                        <td><input type="text" name="product_length[]" id="product_length[]" value="<?php echo $_POST['product_length'][0]; ?>"></td>
                                                                                        <td><input type="text" name="product_width[]" id="product_width[]" value="<?php echo $_POST['product_width'][0]; ?>"></td>
                                                                                        <td><input type="text" name="product_height[]" id="product_height[]" value="<?php echo $_POST['product_height'][0]; ?>">&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                                                    </tr>
                                                                                </table>
                                                                            </td>
                                                                        </tr>




                                                                            <?php
                                                                            for ($i = 1; $i < $options_count; $i++) {
                                                                                ?>
                                                                        <tr class="option_block">
                                                                            <td>
                                                                                <table cellpadding="2" cellspacing="2" border="0" bgcolor="#CCCCCC">
                                                                                    <tr>
                                                                                        <th><?php echo DEFAULT_OPT; ?></th>
                                                                                        <th><?php echo ADD_PRICE; ?>&nbsp;(<?php echo $adminCurrency['currency_code']; ?>)</th>
                                                                                        <th><?php echo PRODUCT_STOCK; ?>&nbsp;<span class="required">*</span></th>
                                                                                        <th><?php echo RECORDER_LEVEL; ?>&nbsp;</th>
                                                                                    </tr>
                                                                                            <?php
                                                                                            $dchecked = "";
                                                                                            if ($_POST['default_option'][$i] == "1") {
                                                                                                $dchecked = "checked";
                                                                                            }
                                                                                            ?>
                                                                                    <tr>
                                                                                        <td>
                                                                                            <span class="default_setting_box">
                                                                                                <input type="radio" name="default" id="default" onclick="setDefaultOption(this);" <?php echo $dchecked ?>>
                                                                                                <input type="hidden" class="default_option" name="default_option[]"  id="default_option[]" value="<?php echo $_POST['default_option'][$i] ?>">
                                                                                                        <?php
                                                                                                        $options_arr = getOptionsFromCategory($catid);
                                                                                                        $num_of_options = count($options_arr);
                                                                                                        if ($num_of_options != "") {
                                                                                                            foreach ($options_arr AS $eachoption) {
                                                                                                                echo "<br>" . $eachoption['option_name'];
                                                                                                                ?>
                                                                                                <input type="text" name="option_<?php echo $eachoption['option_id'] ?>[]" value="<?php echo $_POST['option_' . $eachoption['option_id']][$i]; ?>">
                                                                                                                <?php
                                                                                                            }
                                                                                                        }
                                                                                                        ?>
                                                                                            </span>
                                                                                        </td>
                                                                                        <td><input type="text" name="additional_price[]" id="additional_price[]" value="<?php echo $_POST['additional_price'][$i] ?>"></td>
                                                                                        <td><input type="text" name="product_stock[]" id="product_stock[]" value="<?php echo $_POST['product_stock'][$i] ?>"></td>
                                                                                        <td><input type="text" name="reorder_level[]" id="reorder_level[]" value="<?php echo $_POST['reorder_level'][$i] ?>"></td></tr>
                                                                                        <tr>
                                                                                            <th><?php echo SMALL_IMG?>&nbsp;<span class="required">*</span></th>
                                                                                            <th><?php echo BIG_IMG?>&nbsp;<span class="required">*</span></th>
                                                                                            <th><?php echo DISCOUNT?> (%)</th>
                                                                                            <th><?php echo DISCOUNT_DESC?></th>
                                                                                        </tr>

                                                                                    <tr>
                                                                                        <td><input type="file" name="image_small[]" id="image_small[]" value="<?php echo $_POST['image_small'][$i] ?>"></td>
                                                                                        <td><input type="file" name="image_big[]" id="image_big[]" value="<?php echo $_POST['image_big'][$i] ?>"></td>
                                                                                        <td><input type="text" name="discount[]" id="discount[]" value="<?php echo $_POST['discount'][$i] ?>"></td>
                                                                                        <td><input type="text" name="discount_description[]" id="discount_description[]" value="<?php echo $_POST['discount_description'][$i] ?>"></td>
                                                                                    </tr>
                                                                                    <tr class="canshipBlock" style="display:<?php echo $canshipblock; ?>">
                                                                                        <th><?php echo SW_LBS; ?> (LBS)&nbsp;<span class="required">*</span></th>
                                                                                        <th><?php echo LENGTH; ?> (cm) </th>
                                                                                        <th><?php echo WIDTH; ?> (cm) </th>
                                                                                        <th>Height (cm) </th>
                                                                                    </tr>
                                                                                    <tr class="canshipBlock" style="display:<?php echo $canshipblock; ?>">
                                                                                        <td><input type="text" name="product_shipping_weight[]" id="product_shipping_weight[]" value="<?php echo $_POST['product_shipping_weight'][$i] ?>"></td>
                                                                                        <td><input type="text" name="product_length[]" id="product_length[]" value="<?php echo $_POST['product_length'][$i] ?>"></td>
                                                                                        <td><input type="text" name="product_width[]" id="product_width[]" value="<?php echo $_POST['product_width'][$i] ?>"></td>
                                                                                        <td><input type="text" name="product_height[]" id="product_height[]" value="<?php echo $_POST['product_height'][$i] ?>">&nbsp;<img border="0" title="Delete Option" src="../images/btn_del.gif" alt="Delete Option" class="delete_option" onclick="javascript:delete_option(this);"></td>
                                                                                    </tr>
                                                                                </table>
                                                                            </td>
                                                                        </tr>
                                                                                <?php
                                                                            }
                                                                            ?>
                                                                    </table>
                                                                </td>
                                                            </tr>
                                                                <?php
                                                                $options_arr = getOptionsFromCategory($catid);
                                                                $num_of_options = count($options_arr);
                                                                if ($num_of_options >= 1) {
                                                                    ?>
                                                            <tr>
                                                                <td colspan="3"><input type="button" name="add_more_options" value="<?php echo ADD_OPTIONS; ?>" id="add_more_options"/></td>
                                                            </tr>
                                                                    <?php
                                                                }
                                                                ?>
                                                            <tr>
                                                                <td colspan="3">
                                                                    <div id="canshipBlock" class="canshipBlock" style="display:<?php echo $canshipblock; ?>">
                                                                        <table cellpadding="2" cellspacing="2" border="0" width="100%">
                                                                            <tr>
                                                                                <td colspan="3"><span class="required"><strong>*</strong></span><strong> <?php echo TXT_SMALL_IMG; ?> - 183 X 191 </strong></td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="3"><span class="required"><strong>*</strong></span><strong> <?php echo TXT_BIG_IMG; ?> - 283 X 269 </strong></td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="3"><span class="required"><strong>*</strong></span><strong> <?php echo TXT_CP; ?> </strong></td>
                                                                            </tr>
                                                                        </table>
                                                                    </div>
                                                                </td>
                                                            </tr>

                                                            <tr>
                                                                <td><?php echo MORE_IMG; ?> (jpg/gif/png) </td>
                                                                <td colspan="2">
                                                                        <?php
                                                                        $moreImagesql = "SELECT nproduct_image_id, nimage_productid,
                        vimage_name, vimage_description, vproduct_image_delete_status
                   FROM " . $tableprefix . "product_view_images
                  WHERE nimage_productid= '" . addslashes($productid) . "'";

                                                                        $result = mysql_query($moreImagesql);
                                                                        if (mysql_num_rows($result) != 0) {

                                                                        }

                                                                        $moreImageMaxLImit = getSettingsValue('more_image_limit');
                                                                        if ($moreImageMaxLImit > 0) {
                                                                            for ($iCount = 0; $iCount < $moreImageMaxLImit; $iCount++) {
                                                                                ?>
                                                                    <div class="jqMoreImageContainer" style="padding:5px;border:1px solid #D3D1D1;margin:10px 1px 10px 1px;float:left;width:200px;">
                                                                        <div style="width:180px;float:left;margin-bottom: 4px;"><b><?php echo MORE_IMAGE; ?> <?php echo $iCount + 1; ?></b></div>
                                                                        <input type="file" name="productMoreImage[]" />
                                                                    </div>
                                                                                <?php
                                                                            }
                                                                        }
                                                                        ?>

                                                                </td>
                                                            </tr>

                                                            <tr><td colspan="3">&nbsp;</td></tr>
                                                            <tr><td colspan="3" align="center"><input type="submit" value="<?php echo BTN_ADD_PROD; ?>" name="btnAddProduct" class="button" onClick="return validateProductForm();"></td></tr>
                                                            <tr>
                                                                <td colspan="3">
                                                                    <div align='left' class='linksCopy2'></div>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table>
                                        </fieldset>
                                    </td>
                                </tr>
                                    <?php } else {
                                    ?>
                                <table>
                                    <tr>
                                        <td colspan="3" class=''><?php echo TXT_SUBCAT; ?></td>
                                    </tr>
                                </table>
                                    <?php }
                                ?>
                                <tr><td colspan="3">&nbsp;</td></tr>
                            </table>
                        </form>
                    </div>
                    <!--content area end-->
                </div>
                <div align="left" class="clear_float"><img src="<?php echo $imagefolder; ?>/clear.gif" alt="" width="1" height="1" /></div>
                <!--main body end -->
            </div>
            <?php
            include "../includes/footer.php";
            ?>
            <script type="text/javascript">
                var msgProductName = "<?php echo MSG_PRODUCT_NAME; ?>";
                var msgProductPrice = "<?php echo MSG_PRODUCT_PRICE; ?>";
            </script>
            <script src="../js/product-check.js"  type="text/javascript"></script>