<? 
session_start();
$brcode = $_SESSION['brcode'];
$user   = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out11'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");

//newly added
$update = $_REQUEST['update'];
$cust= $_REQUEST['cust'];
$sapid= $_REQUEST['sapid'];
if($update)
{
    $sql = "update cust_master set corporate = '$sapid' where acname= '$cust' ";
    mysqli_query($db,$sql);
    print "<script> alert('Updated Successfully.....');</script>";
}

?>
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="../jquery/jquery.jqEasyCharCounter.min.js"></script>
<style>
select,input,body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
  <script type="text/javascript">
    var cur=1;
	 var prevcur=0;
	 var gst='';
	 var marginpr='';
	 var custstt='';
	 var shipdistvl='';
	 var shippinvl='';
$(function(){
    
$(".orderto").live("keydown.autocomplete",function(){

$orderto = $(this);

$row = $orderto.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {

		$.ajax({
			url: "searchcust.php",
			dataType: "json",
			data: "key="+$orderto.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.orderto+' | '+  item.partname +' | '+  item.price  +' | '+  item.supqty  ,
						value: item.orderto,
						code:item.partname,
							adr:item.price,
							gst:item.supqty,
							marginpr:item.mrgnprc,
						    custstt:item.custstt,
						    shipdistvl:item.shidist,
						    shippinvl:item.shipin,

					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {

	$row.find('.orderto').val(ui.item.orderto);
	var cusstr=ui.item.code+','+ui.item.adr+','+ui.item.gst+','+marginpr;
	var sapid=ui.item.custstt;
    cusstr=cusstr.trim();
    sapid=sapid.trim();
	document.f1.addr.value=cusstr;
    document.f1.sapid.value=sapid;
	
	//document.f1.msno.focus();
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	});

					
});
});
</script>    
    
<script>
var adr=[];
var ar1=[]; 
var config=[];
var config2=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var em_code=[];
var cess_amt=[];
var hsncode=[];
var tax_perc=[];
function fun1()
{
    var x=document.f1.da.selectedIndex; 
    if(x<=0)
    {
        document.f1.mcode.value='';
        document.f1.config.value='';
    }
    else
    { 
        document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
        document.f1.config.value=config[document.f1.da.selectedIndex];
        document.f1.color.value=color[document.f1.da.selectedIndex];
        document.f1.engno.value=engno[document.f1.da.selectedIndex];
        document.f1.chasis.value=chasis[document.f1.da.selectedIndex];
        document.f1.emcode.value=em_code[document.f1.da.selectedIndex];
        document.f1.cessp.value=cess_amt[document.f1.da.selectedIndex];
        document.f1.hsncode.value=hsncode[document.f1.da.selectedIndex];
        document.f1.taxp.value=tax_perc[document.f1.da.selectedIndex];
    }
    //if(document.f1.model.value != document.f1.mcode.value)
    //{
      //  alert('Model code doesn\'t match with order form');
        //document.f1.mcode.value='';
        //document.f1.config.value='';
        //document.f1.emcode.value= '';  
    //}
}
function chmcode()
{ 
    document.f1.config.value=config2[document.f1.mcode.selectedIndex];
if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
      document.f1.emcode.value= '';  
   }
}
function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.cust.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Customer');
 }
 if(document.f1.fin.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Financer');
 }

 if(document.f1.formno.seletedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
 if(document.f1.segment.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Segment No.');
 }



var day1=document.f1.d.value;
var mon1=document.f1.m.value;
var year1=document.f1.y.value;
var chdt=day1+"/"+mon1+"/"+year1;

var tempdt=year1+"-"+mon1+"-"+day1;
var tempact=document.f1.act.value;
var tempacf=document.f1.acf.value;


function ValidateDate() 
{
 
    var msg = '';
 
    this.isDate = function(date) 
     {
 
        var explodeData = date.explode("/");
        var day = explodeData[0];
        var month = explodeData[1];
        var year = explodeData[2];
 
        var thisMonth = new Date(year, month, 0);
        var daysInMonth = thisMonth.getDate();
        var pattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
 
        if (!pattern.test(date)) {
            msg = "invalid date format";
        }
        else if (day > daysInMonth) {
            msg = "invalid day";
        }
        else if (month > 12) {
            msg = "invalid month";
        }
 
        if (msg) {
            return false;
        } else {
            return true;
        }
    }
 
    this.getMsg = function() 
    {
 
        return msg;
    }
}

 
 vDate = new ValidateDate();
 
if (vDate.isDate(chdt))

 {
if(tempdt>'2016-05-31')
{

//submitOK="false";
//document.f1.m.focus();

//alert('date should be on 31-05-2016');


}

if(tempdt>=tempacf&&tempdt<=tempact)
{

//alert('hii');

}
else
{
submitOK="false";

document.f1.m.focus();

alert('date should be in between current Financial Year');

}

 //alert("Date is valid");
 }
else 

{
 submitOK="false";

document.f1.d.focus();
alert("Date is not valid: " + vDate.getMsg()); 
}




if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}
  if(submitOK=="true")
   return true;
  else
   return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}

function setadr()
{
  var x= document.f1.cust.selectedIndex ; 
  if(x>0)
    document.f1.addr.value=adr[document.f1.cust.selectedIndex];
  else
    document.f1.addr.value='';
}
function setModel()
{
  var x= document.f1.formno.selectedIndex ; 
  if(x>0)
    {
     document.f1.model.value=model[document.f1.formno.selectedIndex];
     document.f1.rep.value=rep[document.f1.formno.selectedIndex];
     document.f1.area.value=area[document.f1.formno.selectedIndex];

   }
  else
    {
      document.f1.model.value='';
     document.f1.rep.value='';
     document.f1.area.value='';
   }

}
 
function setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=28;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

 }
 else
 {
  document.f1.cessp.value=1;
    document.f1.taxp.value=28;
 }

 calc();
}
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>


<form name='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">
    <center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>CUSTOMER DETAILS</TD></TR>
<TR><td align=center bgcolor='ivory'>
<table>
<tr><td><font color=red>Customer</td><td colspan=4> <input type='text' name='cust' id='orderto' placeholder='Type Customer Name Here' class='orderto' size=100>
</td></tr>
<tr>
<td>Address </td><td><textarea name='addr' cols=40></textarea></td><td>Customer SAP ID<input type='text' name='sapid' id='sapid'></td>
<tr><td colspan ='3' align='center'><input type=submit  value='Update' name='update'></td></tr>


</table>
</center>
</form>
</td></tr>
</table>
</center>
<br>
<script>

</script>