<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<head>
<script>
function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
 if(yy.indexOf('"')>-1) 
  {
    alert("\" is not allowed");
    abc.value='';
  }
 if(yy.indexOf('&')>-1) 
  {
    alert("\& is not allowed");
    abc.value='';
  }
}
</script>
</head>
<?
if(isset($_POST['add']))
{
        $oldcus=$_REQUEST['cus'];
        $custnm=$_REQUEST['cusnwnm'];
        $custnm=strtoupper($custnm);
        //$cuscod=$_REQUEST['reg'];
       
 	if(strlen($custnm)<=0)
 	{	
            print "In sufficient entry";

 	}
	else
	{
	$custname=$custnm;
	//$custname=$custnm.'('.$cuscod.')';
	/*
        $res=mysqli_query($db,"select cname from customer where cname='$custname'");
        if(mysqli_num_rows($res)>0)
            {
                print "<script> alert('Duplicate A/c Head'); history.go(-1) </script>";

            }
            $chk=mysqli_query($db,"select * from ledger where led='$custname'");
            if(mysqli_num_rows($chk)>0)
                {
                    // print "<script> alert('Duplicate Ledger'); history.back(1); </script>";
                    //return;
                }
				*/
	if(strlen($custnm)>0)
 	{
 	    $oldop=mysqli_query($db,"select op from ledger where led='$oldcus' and brcode='$brcode'");
        $cusop=mysqli_query($db,"select op from ledger where led='$custname' and brcode='$brcode'");
        $rowcusop=mysqli_fetch_array($cusop);
            
        $rowoldop=mysqli_fetch_array($oldop);
        $custtop=$rowoldop[0]+$rowcusop[0];
               
                
                
            //$query1op="update ledger set op='$custtop' where led='$custname' and brcode='$brcode'";
            //echo $query1;
            //mysqli_query($db,$query1op);
			
			 $query1="delete from ledger where led='$oldcus' and led!='$custnm' and brcode='$brcode'";
            //echo $query1;
            mysqli_query($db,$query1);
            $query2="update journal2 set partic='$custname' where partic='$oldcus' and brcode='$brcode'";
			//echo $query2;
            mysqli_query($db,$query2);
            //echo $query2;
            $query3="update customer set cname='$custname',acname='$custname' where acname='$oldcus' and brcode='$brcode'";
            mysqli_query($db,$query3);
			
            //echo $query3;
			/*
            $query4="update prod_invoice set customer='$custname',vehno='$cuscod' where right(customer,8)='($cuscod)' and brcode='$brcode'";
            mysqli_query($db,$query4);
            //echo $query4;
            $query5="update salesretn set customer='$custname',vehno='$cuscod' where right(customer,8)='($cuscod)' and brcode='$brcode'";
            mysqli_query($db,$query5);
            //echo $query5;
			*/
           // print "<script> alert('Data has been updated'); </script>";
            
  		
 	}
	}
}

?>
<title>Demo</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form.css" rel="stylesheet" />
<script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<script>
function valid()
{
}
</script>

<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;

}
</style>
<style>
th{background-color:steelblue;color:white}
</style>
<body background="back_ground.jpg">
<?php //include('logo2.php'); ?>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()" method='post'>

<br>
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px;">  &nbsp;&nbsp;Utility >> Ledger Name Change</font>
<br>

<center>
<table class="form" ><tr><th colspan='2'>
<h3><font color='black'>Ledger Name Change</font></h3></td></tr>
<form action='<?print $PHP_SELF?>' method='POST'>
    <?
    $sql="SELECT * FROM ledger where brcode='$brcode' order by led asc";
$result = mysqli_query($db,$sql);
$result1=mysqli_query($db,$sql);
?>

<tr><td>Ledger name </td>
    <td  id="to_new_customerTD">
        <select name='cus' id='cus'  style='width:260px;border:1px solid grey; border-style:dotted;' >
            <option value="">--select--</option>
            <?
            while($row=mysqli_fetch_array($result))
{
?>
<option value="<? echo $row['led'] ?>"><? echo $row['led'] ?></option>
<?
}
?>
        </select>
        </td></tr>
  
<tr> <td >Merge To </td><td id="to_new_registerTD">
<select name='cusnwnm' id='cusnwnm'  style='width:260px;border:1px solid grey; border-style:dotted;' >
            <option value="">--select--</option>
            <?
            while($row1=mysqli_fetch_array($result1))
{
?>
<option value="<? echo $row1['led'] ?>"><? echo $row1['led'] ?></option>
<?
}
?>
        </select>


</td></tr>
<tr> <th colspan='2' align='center'><input type=submit value='Merge' name='add'></th></tr>
</table>
<br>
<br>
<p style='color:red'><b>Note: Please Update the opening balance by manually</b><p>
<br>
<p style='color:red'><b>Note: Please take care while merging. Once merged data cannot be retrieved!</b><p>
</form>
	<script type="text/javascript">
        function showregister(str)
{
//var str="hello";
//alert (str);
if (str=="")
  {
  document.getElementById("to_new_registerTD").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("to_new_registerTD").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","showreg2.php?cname="+str,true);
xmlhttp.send();

}
</script>