<?php

session_start();
if($_SESSION['dbName']==""){
    header("location:../index.php");
}

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$query1="select grid_color1,grid_color2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
    extract($rows);
}

$selectedCustomerType = $_GET['selectedcustomertype']; if($selectedCustomerType == '') $selectedCustomerType = $_POST['selectedcustomertype'];
$selectedCustomerTypedesc = $_GET['selectedcustomertypedesc']; if($selectedCustomerTypedesc == '') $selectedCustomerTypedesc = $_POST['selectedcustomertypedesc'];
$restaddress = 150 - strlen(rtrim($selectedCustomerTypedesc," "));
$message = $_GET['message'];
$mainmode = $_GET['mainmode']; if($mainmode == '') $mainmode = $_POST['mainmode'];

class tabs {
    var $name;
    var $tabs;
    var $active;
    var $current;
    function start($name){
        if (empty($this->active)){ $this->active = $name; }
        $this->current = $name;
        ob_start();
    }
    function end(){
        $this->tabs[$this->current] = ob_get_contents();
        ob_end_clean();
    }
    function run(){

        if (count($this->tabs) > 0){
            echo "<DIV CLASS='tabs'>\n";
            $jsClear = "";
            foreach($this->tabs as $tabname => $tabcontent){
                $tabid = "tab_".$this->name."_$tabname";
                $contentid = "tabcontent_".$this->name."_$tabname";
                $jsClear .= "\tdocument.getElementById('$tabid').className = 'tab_inactive';\n";
                $jsClear .= "\tdocument.getElementById('$contentid').style.display = 'none';\n";
            }
            echo "<script type=\"text/javascript\">\n";
            echo "function tab_".$this->name."(id,fieldname){\n";
            echo "$jsClear";

            echo "\tdocument.getElementById('tab_".$this->name."_'+id).className = 'tab_active';\n";
            echo "\tdocument.getElementById('tabcontent_".$this->name."_'+id).style.display = '';\n";

            echo"
				//alert(fieldname);
				if(fieldname!='' && fieldname!=undefined)

					document.clientdt[fieldname].focus();
				";
            echo "}\n";
            echo "</script>\n";
            foreach($this->tabs as $tabname => $tabcontent){
                $tabid = "tab_".$this->name."_$tabname";
                $contentid = "tabcontent_".$this->name."_$tabname";
                echo "<DIV CLASS='";
                if ($this->active == $tabname){ echo "tab_active"; }else{ echo "tab_inactive"; }
                echo "' ID='$tabid' ";
                echo "onClick=\"tab_".$this->name."('$tabname');\">$tabname</DIV>\n";
            }
            echo "<DIV STYLE='float: center; clear:both;'></DIV>\n";
            foreach($this->tabs as $tabname => $tabcontent){
                $contentid = "tabcontent_".$this->name."_$tabname";
                echo "<DIV ID = '$contentid' CLASS='tab_content' STYLE='display: ";
                if ($this->active == $tabname){ echo "block"; }else{ echo "none"; }
                echo ";'>$tabcontent</DIV>\n";
            }
            echo "</DIV>\n";
            echo "<DIV STYLE='clear: both;'></DIV>\n";
        }}
}

print "
<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1256\" />
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<LINK REL=\"stylesheet\" TYPE=\"text/css\" MEDIA=\"all\" HREF=\"css/tabs.css\">
<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"css-v2/style.css\">

<script type=\"text/javascript\" src=\"jquery-1.9.1.js\"></script>

<title>Customer Type</title>

<style type=\"text/css\">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: \"Times New Roman\", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type=\"text/css\" rel=\"stylesheet\" href=\"dhtmlgoodies_calendar.css?random=20051112\" media=\"screen\"></LINK>
<SCRIPT type=\"text/javascript\" src=\"dhtmlgoodies_calendar.js?random=20060118\"></script>

<script type=\"text/javascript\">
function set_customertypedesc(){
    document.customertypedt.customertypedesc.value = document.customertypedt.selectedcustomertypedesc.value;
}
function setCT(){
    document.customertypedt.customertype.value = document.customertypedt.selectedcustomertype.value;
}
function ClearPage(){
    document.getElementsByClassName('message')[0].innerHTML = '';
    document.customertypedt.selectedcustomertype.value = '';
    document.customertypedt.selectedcustomertypedesc.value = '';
    document.customertypedt.remLendesc.value = 150;
    document.customertypedt.sdefault.value = '';
}
function AddCT(){
    document.customertypedt.mainmode.value = 'add';
    document.customertypedt.selectedcustomertype.disabled = false;
    ClearPage();
}
function SubmitRecord(){
    if(document.customertypedt.selectedcustomertype.value == '') alert('Customer type must have a value');
    else{
    setCT();
    set_customertypedesc();
    document.customertypedt.submit();
    }
}
function UpdateDetail(customertype, customertypedesc){
    document.getElementsByClassName('message')[0].innerHTML = '';
    document.customertypedt.selectedcustomertype.disabled = true;
    document.customertypedt.mainmode.value = 'edit';
    document.customertypedt.customertype.value = customertype;
    document.customertypedt.customertypedesc.value = customertypedesc;
    document.customertypedt.selectedcustomertype.value = customertype;
    document.customertypedt.selectedcustomertypedesc.value = customertypedesc;
    textCounter(document.customertypedt.selectedcustomertypedesc, document.customertypedt.remLendesc,150);
    document.customertypedt.sdefault.value = '';
}
function DeleteDetail(record){
    if(confirm('Are you sure you want to delete?')){
        document.customertypedt.mainmode.value = 'delete';
        document.customertypedt.customertype.value = record;
        document.customertypedt.submit();
    }
}
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters center' counter
    else {
        countfield.value = maxlimit - field.value.length;
    }
}
function setDefault(){
    if(document.customertypedt.selectedcustomertype.value != ''){
    if(confirm('Are you sure you want to set this to default? (Please click save after confirming)')){
        document.customertypedt.sdefault.value = 'true';
    }
    }
    else{
        alert('Please select a customer type');
    }
}
function removeDefault(){
    if(confirm('Are you sure you want to remove the default customer type?')){
        document.customertypedt.sdefault.value = 'remove';
        document.customertypedt.submit();
    }
}
</script>

</head>
<body>";
echo "<form name='customertypedt' method='post' action='Form_CustomerType_Actions.php'>
        <input type='hidden' name='mainmode' value='$mainmode'>
        <input type='hidden' name='customertype' value='$selectedCustomerType'>
        <input type='hidden' name='customertypedesc' value='$selectedCustomerTypedesc'>
        <input type='hidden' name='sdefault' value='$customerTypeDefault'>
        <table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>
        <tr><td  valign='top'>";
include_once("sidemenu.php");
echo    "</td></tr>";

echo "<tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Selling Prices</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo "<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>
	            	<button type='button' id='btnClear' name='btnClear' onclick='ClearPage(); return false;' title=' Clear Page '><span><span>Clear Page</span></span></button>
		            <button type='button' id=btnAddLegalType' name='btnAddLegalType' onclick='AddCT(); return false;' title=' Add Customer Type '><span><span>Add</span></span></button>
		            <button type='button' id='btnPost' name='btnPost' onclick='SubmitRecord(); return false;' title=' Save Changes ' $Insmode><span><span>Save</span></span></button>
                </td><td align='left' width='260px'></td><td></td>
            </tr>
	        </table>
        </td>
    </tr>";

echo"<tr><td id='content-body'>";
echo "<div class=\"tabs\">";
echo "<div class=\"tab_active\" id=\"tab__Main Page\" onclick=\"tab_('Main Page');\">Main Page</div>";
$tabs = new tabs("SellingPrice");

$tabs->start("Main Page");

echo"<table border='0'  align='left' valign='top' class='tablecontent'><col width=65%>";

echo"   <tr bgcolor='#F7F7F7' height='25' >
        <td valign='top'>
            <table border='0' style='table-layout:fixed' align='left' valign='top'>
	        	<col width=120>
		        <col width=150>
		        <col width=120>
		        <col width=120>
		        <col width=120>
		        <col width=80>
		        <col width=80>
            <tr bgcolor='#F7F7F7' height='25'>
                <td valign='center' align='left' ><span class='style1'>Customer Type:&nbsp;</span></td>
                <td valign='center' align='left'><span class='style1'><input type='text' name='selectedcustomertype' value='$selectedCustomerType' disabled></span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr bgcolor='#F7F7F7' height='25'>
                <td valign='center' align='left' ><span class='style1'>Description:&nbsp;</span></td>
                <td valign='center' align='left' colspan=5>
                    <span class='style1'>
                    <textarea name='selectedcustomertypedesc'  size=100% maxlength='150' cols=70 rows=3
                        onKeyDown='textCounter(this.form.selectedcustomertypedesc,this.form.remLendesc,150);'
                        onKeyUp='textCounter(this.form.selectedcustomertypedesc,this.form.remLendesc,150);'>" . $selectedCustomerTypedesc . "
                    </textarea>
                    </span></td></tr>
                    <tr bgcolor='#F7F7F7' height='25'>
                        <td colspan=6 align=center>
                            <span class='style12'>Remaining Chars</span>&nbsp;
                            <input readonly type=text name=remLendesc size=1 maxlength=3 value='$restaddress' style='font-size:11px; width:25px;  border:none; background-color:#F7F7F7; '></td>

                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
                        <tr bgcolor='#F7F7F7' height='25'>
                <td valign='center' align='left' ><span>
                    <button type='button' onclick='removeDefault();' style='vertical-align: middle;'><span><span style='color:#cd0a0a'>Remove default</span></span></button>
                    </span></td>
                <td valign='center' align='left'>
                    <span>
                    <button type='button' onclick='setDefault();' style='vertical-align: middle;'><span><span style='color:#cd0a0a'>Set to default</span></span></button>
                    </span></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            </table>
        <td valign=\"top\" align=\"left\">
            <table style=\"width:380px;\" border=\"0\" cellspacing=\"0\">
                <colgroup>
                    <col width=\"100\">
                    <col width=\"150\">
	            </colgroup><tbody>
            <tr><td id=\"search-top\" style=\"width:380px;height:15px;\" colspan=\"4\"></td></tr>
            <tr><td valign=\"top\" colspan='3' id='search-content' style=\"width:380px;\">
                <table style=\"width:380px;\" border=\"0\" cellspacing=\"0\">
                    <colgroup>
                        <col width=\"30\">
                        <col width=\"30\">
                        <col width=\"80\">
                        <col width=\"140\">
                        <col width=\"130\">
                    </colgroup>
                    <tbody>
                    <tr height=\"30px\"><td>&nbsp;</td>
	                    <td align=\"center\">
	                        <span class=\"style1\"><b>Customer Type</b></span>
                        </td>
                        <td align=\"center\">
                            <span class=\"style1\"><b>Description</b></span>
                        </td>
                    </tr>
                    <tr><td colspan=\"3\">
                        <div style=\"overflow-y:scroll;height:320px;width:370px;border:0px solid #000000;padding-left:5px;\">
                            <table border=\"0\" width=\"352px\">
                            <colgroup>
                                <col width=\"30\">
                                <col width=\"30\">
                                <col width=\"160\">
                                <col width=\"170\">
                            </colgroup>
                            <tbody>";
$querydt = "select * from clientscustomertype ORDER BY clientscustomertype_code ASC";
$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
while($rowsdt=mysql_fetch_row($resultdt)) {

    $ct += 1;

    if ($ct % 2 == 0)
        $Color = "style='background-color:" . $grid_color1 . "'";
    else
        $Color = "style='background-color:" . $grid_color2 . "'";


    print "
            <tr " . $Color . " height='20px'>
                <td align='center' valign='center'><span class='style15'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"UpdateDetail('" . $rowsdt[1] . "','" . $rowsdt[2] . "')\" title=' Edit Record '  ></span></td>
    	        <td align='center' valign='center'>
    	            <span class='style15'>
    	            <img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteDetail('" . $rowsdt[1] . "') title=' Delete Record '  >
    	            </span>
                </td>
    	        <td align='left'><span class='style15'>" . $rowsdt[1] . "</span>";
    if($rowsdt[3] == '1'){
        echo "<span class='style15' color='red' align='right'>&nbsp;&nbsp;&nbsp; (Default)</span>";
    }
    print "
    	        </td>
   	            <td align='left'><span class='style15'>" . $rowsdt[2] . "</span></td>
            </tr>";


}
echo"</tbody>
                            </table>
                        </div>
					</tr>
					</tbody>
				</table>
				</td>
			</tr>
			<tr align='left'><td id=\"search-content\" colspan=\"4\" style='width:380px;height:20px;'>&nbsp;</td></tr>
            <tr><td id=\"search-bottom\" style=\"width:380px;height:20px;\" colspan=\"6\"></td></tr>
			</table>
		</td>
		</tr>
	</table>
    </div>
	</div>
	</td>
	</tr>";

echo"<tr height=\"30\"><td align=\"center\" id=\"content-bottom\"></td></tr>
</form>
</body>
</html>";

//clientscustomertype_id, clientscustomertype_code, clientscustomertype_description, clientscustomertype_default