<?php 
error_reporting(0);  
//****************MODIFY BY AMIT SINGH ************D&D************************23-11-2012*********************************
include('Class_PickListEditor.php');
include("Class_Branch.php");
include("ClassRole.php");
//echo  $_REQUEST['action'];
$catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');

$catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');
$table="crm_accounts_new";
$max_size='2000';
$flag=0;
function extension($filename)
{
$name_brk=explode('.',$filename);
return  $name_brk[1];
}
  


//************************SAVE RECORDS IN THE TABLES*************************START HERE***********************************
if($_REQUEST['Save']!='')
{
    $dataUpdate['Convert_IntoAccountStatus']=trim('Y');
	$condUpdate="leadid='".$_REQUEST['id']."'";
	//echo "<pre>";
	//print_r($data);exit();
	$row= $s->RecordEdit('crm_leads',$dataUpdate,$condUpdate);
        
$cilentcode=DB_query("select * from clientmaster",$db);
if(DB_num_rows($cilentcode)>0)
{
	if($_SESSION['dealerid']){
        $ClCOdeSQL=DB_query("select client_code from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid='".$_SESSION['dealerid']."' order by id desc limit 1",$db);
        }
 else {
     $ClCOdeSQL=DB_query("select client_code from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid='0' order by id desc limit 1",$db);
 }
	$CliCode=DB_fetch_array($ClCOdeSQL);
	$CliCode['client_code'];
	$Exp=explode("-",$CliCode['client_code']);
	$finid=$Exp[1]+1;
	$finalAccCode=$Exp[0].'-'.$finid;
}
else
{
	$finalAccCode='Account'.'-'.'1';
}
		
                // Check Client 
                $chkSql="select * from clientmaster where c_name='".$_REQUEST['clientname']."'";
                $chkSQLExec=DB_query($chkSql,$db);
                $numrows = DB_num_rows($chkSQLExec);
                if($numrows==0)
                {
                
                $datax['c_name']=$_REQUEST['clientname'];
		$datax['client_code']=$finalAccCode;
		$datax['Site']=$_REQUEST['mst_branchid'];
		$datax['pre_address1']=$_REQUEST['address'];
		$datax['pre_phone1']=$_REQUEST['phone'];
		$datax['fax']=$_REQUEST['fax'];
		$datax['pre_mobile']=$_REQUEST['mobile'];
		$datax['per_email']=$_REQUEST['email'];
		$datax['pre_state']=$_REQUEST['state'];
		$datax['pre_city']=$_REQUEST['city'];
		$datax['pre_country']=$_REQUEST['country'];
		$datax['pre_pincode']=$_REQUEST['zipcode'];
		//$datax['client_code']="AC-".$_REQUEST['AccountName'];
		$datax['Zone']=$_REQUEST['zone'];
                $datax['dealerid']=trim($_SESSION['dealerid']);
                
		$s->RecordInsert('clientmaster', $datax);//NEW CLIENT INTO CLIENTMASTER
		$lastid=DB_Last_Insert_ID($db,'clientmaster', 'id');
                } 
                else 
                {
                    $clientRow = DB_fetch_array($chkSQLExec);
                    $lastid=$clientRow['id'];
                }
		$data23['ledgertype']='client';
		$data23['UnderGroup']=$s->GetFieldValue('accountgroupmaster',"GroupName='Sundry Creditor'  ",'GroupId');
		$data23['AccountName']=$_REQUEST['company_name'];
		$data23['openingdate']=date('Y-m-d');
		$data23['CompanyId']=$_SESSION['ACGCompanyId'];
		//$data['vender_client']=$_REQUEST['leadid'];
		$s->RecordInsert('accountledger', $data23);//CREATE NEW ACCOUNT IN ACCOUNTLEDGER
		
		
		$data3['master_code']=$lastid;
		$data3['master_prefix']='C';
		$data3['person']=$_REQUEST['contact_name'];
		$data3['department']=$_REQUEST['department'];
		$data3['designation']=$_REQUEST['designation'];
		$data3['phone']=$_REQUEST['phone'];
		$data3['email']=$_REQUEST['email'];
		$s->RecordInsert('contact_information_master', $data3);
		
				
		
	
	
	
	$data['branch']=$_REQUEST['mst_branchid'];
	$data['store']=$_REQUEST['store'];
	$data['accdate']=ConvertSQLDate($_REQUEST['Acc_date']);
	$data['account_code']=$finalAccCode;
	$data['client']=$lastid;
	$data['contact_person']=$_REQUEST['contact_name'];
	$data['group_name']=$_REQUEST['groupname'];
	$data['first_name']=$_REQUEST['fname'];
	$data['middle_name']=$_REQUEST['mname'];
	$data['last_name']=$_REQUEST['lname'];
	$data['department']=$_REQUEST['mst_departmentid'];
	$data['destination']=$_REQUEST['designation'];
	$data['address']=$_REQUEST['address'];
	$data['email_id']=$_REQUEST['email'];
	$data['phonno']=$_REQUEST['phone'];
	$data['mobile']=$_REQUEST['mobile'];
	$data['faxno']=$_REQUEST['fax'];
	$data['zipcode']=$_REQUEST['zipcode'];
	$data['country']=$_REQUEST['country'];
	$data['city']=$_REQUEST['city'];
	$data['state']=$_REQUEST['state'];
	$data['remark']=$_REQUEST['remark'];
        $data['dealerid']=trim($_SESSION['dealerid']);
	if($_REQUEST['groupid']!='0'){
	$data['group_id']=trim(implode(',',$_REQUEST['groupid']));
	}
	if($_REQUEST['leaduserid']){
	$data['asignto']=trim(implode(',',$_REQUEST['leaduserid']));
	}
	$data['created_by']=trim($_SESSION['UserID']);
	$data['CompanyId']=trim($_SESSION['ACGCompanyId']);
	
	$leadfile=$_FILES['leadfile']['name'];
	if($leadfile!=''){
	$type=extension($leadfile);
	
	$maxid=DB_query("select Max(accid) from crm_accounts_new",$db);
	$maxiddata=DB_fetch_array($maxid);
	$maxnumber=$maxiddata['Max(accid)']+1;
	

	 $tmp_name=$_FILES['leadfile']['tmp_name'];
	
	if($_FILES['leadfile']['size']>$max_size*1024)
	{
	echo "<strong>Sorry! Size Limit Exceeded</strong>";
	}
	 else
	 $new_name=$maxnumber.'.'.$type;
	 $path="CRM/AccDoc/";	
	if(move_uploaded_file($_FILES['leadfile']['tmp_name'],$path.$new_name))
	{
	
	echo "<strong>Copy Unsuccessful</strong>";
	
	}
	}
        
        
	$data['attach_file']=trim($new_name);
	$row= $s->RecordInsert($table, $data);
	$lastid=DB_Last_Insert_ID($db,'crm_accounts_new', 'accid');
	
	

	
	
	
	for($i=0;$i<count($_REQUEST['item_code']);$i++)
	{
	if($_REQUEST['stock_qty'][$i]!=''){
	$data_arr['item_id']=$_REQUEST['item_code'][$i];
	$data_arr['qty']=$_REQUEST['stock_qty'][$i];
	$data_arr['per_unit']=$_REQUEST['stock_perunit'][$i];
	$data_arr['price']=$_REQUEST['stock_price'][$i];
	$data_arr['value']=$_REQUEST['stock_value'][$i];
	$data_arr['total_stock_qty']=$_REQUEST['total_stock'][$i];
	$data_arr['parant_id']=$lastid;
	$rowinsert= $s->RecordInsert('crm_accounts_new_details', $data_arr);
	}
	}
	
	if($_REQUEST['userid']!='')
	{
		for($i=0;$i<count($_REQUEST['userid']);$i++)
		{
		$email[]=$s->GetFieldValue('www_users',"id='".$_REQUEST['userid'][$i]."'",'email');
		}
	$emails=implode(",",$email);
	 $to=$emails;
	$subject="Hi This A New Enquiry For You";
	$message="Hi This is New Enquiry For You";
	$from="someonelse@example.com";
	$headers = "From:" . $from;
	mail($to,$subject,$message,$headers);
	echo 'mail successfully send';
	}
	
	
	$s->pageLocation("index.php?route=CRM/View_Leads".$rurl);
}
//************************SAVE RECORDS IN THE TABLES*************************END HERE***********************************
$displaySql="select * from crm_leads where leadid='".$_REQUEST['leadid']."'";
$getlead=DB_query($displaySql, $db);
$row_lead=DB_fetch_array($getlead);
extract($row_lead);
?>
<!------------------JS FILES---------------START HERE------------------------------------------------------------------->
<script type="text/javascript" language="javascript">
function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	document.getElementById("stock_perunit_"+rowCount).value='1';
}


function open_items()
{
var site=document.getElementById("mst_branchid").value;
var store=document.getElementById("store").value;
var date=document.getElementById("Acc_date").value;

window.open("TelephonicItems.php?site="+site+"&store="+store+"&date="+date+"&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}


function deleteRowOrder(tableID) 
{
	try 
	{
	var table = document.getElementById(tableID);
	//alert(table);
	var rowCount = table.rows.length;
	//alert(rowCount);
	for(var i=0; i<rowCount; i++) 
	{
	var row = table.rows[i];
	var chkbox = row.cells[0].childNodes[0];
	if(null != chkbox && true == chkbox.checked) 
	{
	if(rowCount <= 1) 
	{
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
	alert(e);
	}
}


function CalculateTotalLeadAmt(val,id)
{
	var id_brk=id.split('_');
	var i=id_brk[2];
	// var stock_perunit = document.getElementById("stock_perunit_+"+i).value;
	if(id_brk.length>2)
	{
	var stock_perunit = document.getElementById("stock_perunit_"+i).value;
	var stock_price = document.getElementById("stock_price_"+i).value;
	var stock_qty = document.getElementById("stock_qty_"+i).value;
	document.getElementById("stock_value_"+i).value=(stock_price*stock_qty )/stock_perunit;
	document.getElementById("total_stock_"+i).value=document.getElementById("stock_qty_"+i).value;
	}
	else
	{
	var stock_perunit = document.getElementById("stock_perunit").value;
	var stock_price = document.getElementById("stock_price").value;
	var stock_qty = document.getElementById("stock_qty").value;
	document.getElementById("stock_value").value=(stock_price*stock_qty )/stock_perunit;
	document.getElementById("total_stock").value=document.getElementById("stock_qty").value;
	}
}


function filldefaultAmt(val,id)
{
	var id_brk=id.split('_');
	var i=id_brk[2];
	if(id_brk.length>2)
	{
	document.getElementById("stock_perunit_"+i).value=1;
	}
	else
	{
	document.getElementById("stock_perunit").value=1;
	}
}
</script>
<!------------------JS FILES---------------END HERE------------------------------------------------------------------->

<script type="text/javascript" language="javascript">
function validate()
{
	if(document.getElementById("mst_branchid").value=='000')
	{
	alert("First Branch must be filled out");
	return false;
	}
	if(document.getElementById("Acc_date").value=='')
	{
	alert("Date must be filled out");
	return false;
	}
	if(document.getElementById("clientname").value=='')
	{
	alert("Company Name  must be filled out");
	return false;
	}
	if(document.getElementById("zone").value=='0')
	{
	alert("Zone must be filled out");
	return false;
	}
	if(document.getElementById("contact_name").value=='')
	{
	alert("Contact Name must be filled out");
	return false;
	}
	
	
}


function checkReqDate()
{

var curr_date2='';	
var curr_month2='';
var d = new Date();

//alert(d.format("m/dd/yy"));
var curr_date = d.getDate();


var curr_month1 = d.getMonth();

var curr_year = d.getFullYear();
var curr_month=curr_month1+1;
var t=curr_month;
var d=curr_date;
if(t!=10 || t!=11 ||t!=12 )
{
 curr_month2="0" + curr_month;	
}
else
{
 curr_month2=curr_month;	
}
if(d==1 || d==2 || d==3 || d==4 || d==5 || d==6 || d==7 || d==8 || d==9 )
{
 curr_date2="0" + curr_date;		
}
else
{
 curr_date2=curr_date;		
}


var datedat=(curr_date2 + "/" + curr_month2 
+ "/" + curr_year);

var date1=document.getElementById("Acc_date").value;
var str1 = date1;
var str2 = datedat;
    var dt1  = parseInt(str1.substring(0,2),10);
    var mon1 = parseInt(str1.substring(3,5),10);
    var yr1  = parseInt(str1.substring(6,10),10);
    var dt2  = parseInt(str2.substring(0,2),10);
    var mon2 = parseInt(str2.substring(3,5),10);
    var yr2  = parseInt(str2.substring(6,10),10);
    var date1 = new Date(yr1, mon1, dt1);
    var date2 = new Date(yr2, mon2, dt2);
    if(date2 > date1)
    {
        alert("Date Can't Be Less than Current Date");
		document.getElementById("Acc_date").value='';
		document.getElementById("Acc_date").focus();
        return false;
    }
    else
    {
        return true;
        
    } 




}


function calcValue1(str,id)
{
	var brk=id.split('_');
	if(brk.length==2)
	{
	   if(parseFloat(str)>parseFloat(document.getElementById("total_stock").value))
	   {
	   alert("Qty Should be less or equal then Stock Qty");
	   document.getElementById("stock_qty").value='';
	   return false;
	   }
       else
	   {	
		var price=document.getElementById("stock_price").value;
		if(price!=''){
		var perunit=document.getElementById("stock_perunit").value;
		var tot=str*(price/perunit);
		document.getElementById("stock_value").value=tot;}
		}
	}
	else
	{
	  if(parseFloat(str)>parseFloat(document.getElementById("total_stock_"+brk[2]).value))
	   {
	   alert("Qty Should be less or equal then Stock Qty");
	   document.getElementById("stock_qty_"+brk[2]).value='';
	   return false;
	   }
		var price=document.getElementById("stock_price_"+brk[2]).value;
		if(price!=''){
		var perunit=document.getElementById("stock_perunit_"+brk[2]).value;
		var tot=str*(price/perunit);
		document.getElementById("stock_value_"+brk[2]).value=tot;}
	}
}


function calcValue2(str,id)
{
	var brk=id.split('_');
	if(brk.length==2)
	{
		var qty=document.getElementById("stock_qty").value;
		if(qty!=''){
		var perunit=document.getElementById("stock_perunit").value;
		var tot=qty*(str/perunit);
		document.getElementById("stock_value").value=tot;}
	}
	else
	{
		var qty=document.getElementById("stock_qty_"+brk[2]).value;
		if(qty!=''){
		var perunit=document.getElementById("stock_perunit_"+brk[2]).value;
		var tot=qty*(str/perunit);
		document.getElementById("stock_value_"+brk[2]).value=tot;}
	}
}

function calcValue3(str,id)
{
	var brk=id.split('_');
	if(str==''){str=1;}
	if(brk.length==2)
	{
		var qty=document.getElementById("stock_qty").value;
		if(qty!=''){
		var price=document.getElementById("stock_price").value;
		var tot=qty*(price/str);
		document.getElementById("stock_value").value=tot;}
	}
	else
	{
		var qty=document.getElementById("stock_qty_"+brk[2]).value;
		if(qty!=''){
		var price=document.getElementById("stock_price_"+brk[2]).value;
		var tot=qty*(price/str);
		document.getElementById("stock_value_"+brk[2]).value=tot;}
	}
}

function getUsers(str)
{
	var count=document.getElementById("countGroup").value;
	var sub_cat_array=Array();
	for(var i=0;i<count;i++)
	{
		var item_sub_category=document.getElementById("groupid_"+i).value;
		//alert(item_sub_category);
		if(document.getElementById("groupid_"+i).checked==true)
		{
			sub_cat_array[i]=item_sub_category;
		}
	}
	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("hid-Roleid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_get_users.php?q="+sub_cat_array,true);
xmlhttp.send();
}
</script>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Account Management</h1>
<form action=""	 method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo $_REQUEST['leadid'] ?>" />
<table width="100%" border="0" cellspacing="0" cellpadding="3">
			<?php
	if(count($msgErr)>0)
	{?>
		<tr class="row1">
	<td colspan="4">
		<?php 
		for($i=0;$i<count($msgErr);$i++)
		{
		echo "<div class='error'>".$msgErr[$i]."</div>";	
		}?>
		</td>
		</tr>
		<?php
	}
	
	?>
</table> 
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td colspan="4"><h3 style="color:#003399">General Information</h3></td>
    <td><span class="tabledata1">
      <input type="checkbox" name="sendmail" />
      Send Email</span></td>
    <td colspan="3"><span class="tabledata1">
      <input type="checkbox" name="sendsms" />
      Send SMS </span></td>
  </tr>
   <tr class="row1">
    <td class="tabledata1" >Branch<font color="#FF0000">*</font></td>
    <td>
	<?php  	 //echo $ClassBranch->SiteRecord('mst_branchid',$mst_branchid,'leads');

		$sql_state=DB_query("SELECT loccode,locationname FROM  locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."') and loccode='".$mst_branchid."'",$db);?> 
		<select name="mst_branchid" id="mst_branchid" style="width:150px;" onchange="showstorecode(this.value)">
			
			<?php while($SiteName=DB_fetch_array($sql_state)) {?>]
			<option value="<?php echo $SiteName['loccode']; ?>"><?php echo $SiteName['locationname']; ?></option>
			<?php }?>
		</select>	</td>
	<td><span class="tabledata1">Lead No.</span></td>
	<td><div id='hid_leads'>
	  <input type="text" name="leadcode" style="width:150px;" value="<?php echo $leadcode; ?>" readonly="true" />
	  </div></td>
	<td class="tabledata1" >Lead Date<font color="#FF0000">*</font></td>
	<td class="tabledata1" colspan="3"><span style="width:200px;">
	  <input type="text" name="leads_date" readonly style="width:150px;" 
	value="<?php  echo date('d/m/Y');?>" id="lead_date" onchange="return checkReqDate()" />
	</span></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" >Type</td>
    <td class="tabledata1" ><select  name="lead_type" style="width:150px;" >
            <option value="m" <?php if($lead_type=='m') echo 'selected'; ?>>Material</option>
            <option value="s" <?php if($lead_type=='s') echo 'selected'; ?>>Supply</option>
            <option value="c" <?php if($lead_type=='c') echo 'selected'; ?>>Composite</option>
        </select></td>
    <td class="tabledata1" >Lead Category</td>
    <td style="width:200px;"><select name="lead_Category"  style="width:150px;" id="lead_Category" >
      <?php $TypetSQL=DB_query("SELECT * FROM type_master",$db); 
		while($Type=DB_fetch_array($TypetSQL)){?>
      <option value="<?php echo  $Type['id'];?>" <?php if($lead_Category==$Type['id']) { echo 'selected'; } else { echo ''; } ?>><?php echo  $Type['name'];?></option>
      <?php }?>
    </select></td>
    <td class="tabledata1" >Lead Status</td>
    <td colspan="3"><?php $LeadStatusSQL=DB_query("SELECT id, name FROM lead_status where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); ?>
      <select name="lead_status"  style="width:150px;" id="lead_status" >
         <?php while($statusRow=DB_fetch_array($LeadStatusSQL)){?>
        <option value="<?php  echo $statusRow['id'];?>" <?php  echo $lead_status==$_REQUEST['lead_status']?'selected':''?>><?php echo $statusRow['name'];?></option>
        <?php } ?>
      </select></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" >Priority <font color="#FF0000">*</font></td>
    <td >
	<?php $Priority=array('Highest','High','Medium','Low')?>
	<select name="lead_Priority" id="lead_Priority"  style="width:150px;" >
	<?php for($k=0;$k<count($Priority);$k++)
	{?>
	<option value="<?php  echo $Priority[$k];?>" <?php  echo $lead_Priority==$Priority[$k]?'selected':''?> ><?php  echo $Priority[$k];?></option>
	<?php 
	}
	?>
	</select>	 	</td>
    <td class="tabledata1" >Sale Executive</td>
    <td ><input type="text" name="Input"  style="width:150px;" value="<?php echo  $s->getField('www_users','id',$_SESSION['UserID'],'realname'); ?>"  readonly="readonly"/>
      <input type="hidden" name="sale_executive"  style="width:150px;" value="<?php  echo $_SESSION['UserID'];?>" /></td>
    <td class="tabledata1" >Lead source</td>
    <td colspan="3">
	  <select  name="lead_source"  style="width:150px;" id="lead_source" onChange="getOther(this.value)">
	  <option value="">--Select--</option>
	  <option value="oth"<?php if($lead_source=='oth') { echo 'selected'; } else { echo ''; } ?>>other</option>
      <?php $LeadSQL=DB_query("SELECT * FROM lead_source where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
		while($Lead=DB_fetch_array($LeadSQL)){?>
      <option value="<?php echo $Lead['id'];?>"<?php if($lead_source==$Lead['id']) { echo 'selected'; } else { echo ''; } ?>><?php echo  $Lead['name'];?></option>
      <?php }?>
    </select></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" >Subject</td>
    <td class="tabledata1" colspan="7" ><input type="text" name="lead_subject" style="width:100%;" value="<?php  echo $lead_subject;?>" id="lead_subject" /></td></tr>
  
  <tr class="row1">
 <td colspan="8" style="padding:0px; border:none;">
 <table cellpadding="0" cellspacing="0" id="otherReason"  <?php if($lead_source=='oth') { echo "style='display:block;'"; } else { echo "style='display:none;'"; } ?>>
 <tr>
    <td class="tabledata1">Reference Name</td>
    <td class="tabledata1"><input type="text" name="ref_name" value="<?php echo $ref_name; ?>" style="width:150px;" id="ref_name" /></td>
	
	<td class="tabledata1">Address</td>
    <td class="tabledata1" ><input type="text" name="ref_addres" value="<?php echo $ref_addres; ?>" style="width:150px;" id="ref_addres" /></td>
	
	<td class="tabledata1" >Mobile No.</td>
    <td class="tabledata1" ><input type="text" name="ref_phone" value="<?php echo $ref_phone; ?>" style="width:150px;" id="ref_phone" /></td>
	
	<td class="tabledata1" >Email ID</td>
    <td class="tabledata1" ><input type="text" name="ref_email" value="<?php echo $ref_email; ?>" style="width:150px;" id="ref_email" /></td>
</tr>
 </table>
 </td>
</tr>
 <tr class="row1">
    <td colspan="8"><h3 style="color:#003399">Client &amp; Commnunication Information</h3></td>
  </tr>
   <tr class="row1">
   <td  class="tabledata1">Client  <font color="#FF0000">*</font></td>
    <td style="width:200px;">
        <input type="text" name="clientname" value="<?php echo $s->getField('clientmaster','id',$company_name,'c_name'); ?>" />
		<select style="width:150px;"  name="company_name" id="company_name"  onchange="GetClientDetails(this.value)" >
		<option value="0">Select Client</option>
		<option value="new" <?php echo  $_SESSION['crm_company_name']=='new' ?'selected':''?>>--New Client--</option>
		<?php
		  $ClientSQL="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."'  order by c_name";
		$ClientRes=DB_query($ClientSQL,$db);
		while($ClientRow=DB_fetch_array($ClientRes))
		{
 		?>
		<option value="<?php echo $ClientRow['id']?>" <?php echo ($company_name==$ClientRow['id']?'selected':'');?>><?php echo $ClientRow['c_name']?></option>
		<?php  } ?>
 	</select>
	<div id='hid-newclient' style="display:none;">
	<input type="text" style="width:150px;" name="crm_newclient" id="crm_newclient" />
	</div>	</td>
    <td  class="tabledata1">Group</td>
    <td  class="tabledata1" ><input type="text" id="groupname" name="groupname" style="width:150px;" value="<?php  echo $groupname;?>" /  /></td>
    <td  class="tabledata1">Commodity</td>
    <td colspan="3"><input type="text" id="commodity" name="lead_function" style="width:150px;"  value="<?php echo $lead_function;?>"/></td>
  </tr>
  <tr class="row1">
    <td  class="tabledata1" >Address I</td>
      <td style="width:200px;" id="Add1"><input type="text" name="address1" id="address1" style="width:150px;"   value="<?php  echo $address1;?>"/></td>
    <td class="tabledata1" >Phone No.</td>
    <td class="tabledata1" id="Phone" ><input type="text" name="phone" id="phone" style="width:150px;"  value="<?php  echo $phone;?>"/></td>
    
    <td class="tabledata1" >Contact Person</td>
    <td  class="tabledata1" id="ContactPerson" colspan="3"><input type="text" name="contact_name" id="contact_name" style="width:150px;" value="<?php  echo $contact_name;?>" /></td>
</tr>

<tr class="row1">
    <td class="tabledata1" >City</td>
    <td id="show_city">
    	<select name="city" id="city" style="width:150px;" onchange="ShowDiv(this.value,'searchstate','get_state_for_lead1.php?type=client')" >
       <option value="">---Select City---</option>
	   <?php 
	   $sql = "select * from city_master where 1=1";
	   $result = DB_query($sql,$db);
	   while($city_row = DB_fetch_array($result))
	   {
	   		$p='';
			if($city_row['id'] == $pre_city)
			$p="selected";
	   ?>
	  <option value="<?php echo $city_row['id']; ?>" <?php echo ($city==$city_row['id']?"selected":"") ?>><?php echo $city_row['city']; ?></option>
	   <?php }?>
      </select>	</td>	
	
	  <td class="tabledata1" >Fax No.</td>
    <td id="Fax"><input type="text" name="fax" id="fax" style="width:150px;" value="<?php  echo $fax?>" /></td>
     <td class="tabledata1" >Department</td>
     <td align="left" colspan="3"><select  name="department" style="width:150px;" id="department">
             <option value="">Select</option>
        <?php $deptsql=DB_query("select * from deptmaster",$db); while($deptres=DB_fetch_assoc($deptsql)){ ?>
             <option value="<?php echo $deptres['id'] ?>" <?php if($department==$deptres['id']) { echo 'selected'; } else { echo ''; } ?>><?php echo $deptres['deptname'] ?></option>
        <?php } ?>
         </select></td>
	</tr>
        
        <tr class="row1">
 
  <td width="155" align="left">State</td>
    <td width="218" align="left" id="show_state">
	<select name="state" id="state" style="width:150px;" onchange="search_city(this.value)">
            <option value="">---Select State---</option>
 	   <?php 
	    $sql = "select * from state_master";
		$result=DB_query($sql,$db);
		while($myrow=DB_fetch_array($result))
		{ 
			$p='';
			if($myrow['id'] == $state)
			$p="selected";
		?>
		<option value="<?php echo $myrow['id'];?>" <?php echo ($state==$myrow['id']?"selected":"") ?>><?php echo $myrow['state']; ?></option>
		<?php }
	   ?>
      </select></td>
    <td><span class="tabledata1">Email Id</span></td>
	 <td class="tabledata1" id="Email" ><input type="text" name="email" id="email" style="width:150px;" value="<?php  echo $email;?>" /></td>
    <td ><span class="tabledata1">Designation</span></td>
    <td class="tabledata1" id="mobl" colspan="3"><select  name="designation" style="width:150px;" id="designation">
             <option value="">Select</option>
      <?php $deptsql=DB_query("select * from desigmaster",$db); while($deptres=DB_fetch_assoc($deptsql)){ ?>
             <option value="<?php echo $deptres['id'] ?>" <?php if($designation==$deptres['id']) { echo 'selected'; } else { echo ''; } ?>><?php echo $deptres['designame'] ?></option>
        <?php } ?></select></td>
	</tr>
        
        <tr class="row1">
            <td width="138" align="left">Country</td>
    <td align="left" id="COUNTRY" ><select name="country" id="country" style="width:150px;"  >
          <option value="">---Select Country---</option>
        <?php 
			$c_sql = "select * from country_master ";
			$c_result = DB_query($c_sql,$db);
			while($c_row=DB_fetch_array($c_result))
			{
				$p='';
				if($c_row['id']==$country)
				$p= "selected";
			?>
				<option value="<?php echo $c_row['id']; ?>"<?php echo ($country==$c_row['id']?"selected":"") ?>><?php echo $c_row['country']; ?></option>
	  <?php }
		?>
		</select></td>
	 <td class="tabledata1" >Website</td>
	<td id="ZIPC"><input type="text" name="website" id="website" style="width:150px;"  value="<?php  echo $website;?>" /></td>
    <td ><span class="tabledata1">Mobile No.</span></td>
    <td class="tabledata1" id="mobl" colspan="3"><input type="text" name="mobile" id="mobile" style="width:150px;"value="<?php  echo $mobile;?>" /></td>
 </tr> 
<tr class="row1">
     <td class="tabledata1" >Zip Code</td>
	<td id="ZIPC"><input type="text" name="zipcode" id="zipcode" style="width:150px;"  value="<?php  echo $zipcode?>" /></td>
       
        <td colspan="2"></td>
        <td><span class="tabledata1">Email Id</span></td>
	 <td class="tabledata1" colspan="3"><input type="text" name="email1" id="email1" style="width:150px;" value="<?php  echo $cemail;?>" /></td>
 </tr>
<tr class="row1">
 <td colspan="8" style="padding:0px; border:none;">
 <table cellpadding="0" cellspacing="0" id="contacDetails" style="display:none;">
 
 </table>
 </td>
</tr>


<tr class="row1">
    <td colspan="8"><h3 style="color:#003399">Contact Person  Information </h3></td>
  </tr>

  <tr class="row1" id="contact2">
    <td class="tabledata1" >Contact Person II </td>
    <td id="firstname"><input type="text" name="another_contact" id="another_contact" style="width:150px;" value="<?php  echo $another_contact;?>" /></td>
    <td class="tabledata1" >Dept.</td>
    <td id="middlename"><select name="mst_departmentid1" id="mst_departmentid1" style="width:150px;"  onchange="ShowDiv(this.value,'hid_POrder1','disp_department.php')" >
      <option value="">Select</option>
      <?php 
	$sqldept=DB_query("select * from deptmaster",$db);
	while ($dept_row=DB_fetch_array($sqldept)) {
			?>
      <option value="<?php echo $dept_row['id'];?>" <?php echo $department1==$dept_row['id']?'selected':''?>><?php echo $dept_row['deptname']; ?></option>
      <?php }	?>
    </select>    </td>
    <td  class="tabledata1" >Designation</td>
    <td id="LastName" ><div id='hid_POrder1'>
      <select name="designation1"  id="designation1" style="width:150px;" >
        <option value="">Select</option>
        <?php
    $sql_po= "select * from desigmaster ";
	 $result=DB_query($sql_po,$db);
    while($myrow=DB_fetch_array($result))
	{
		
	?>
        <option value="<?php echo $myrow['id'];?>"<?php echo $designation1==$myrow['id']?'selected':'';?>><?php echo $myrow['designame'];?></option>
        <?php }?>
      </select>
    </div></td>
    <td>Mobile <input type="text" id="cmobile2" name="cmobile2" value="<?php echo $cmobile2; ?>"></td>;
    <td>Email <input type="text" id="cemail2" name="cemail2" value="<?php echo $cemail2; ?>"> </td>
  </tr>
  
  
  

<tr class="row1">
    <td colspan="6"><h3 style="color:#003399">Product Information</h3></td>
  </tr>
  <tr class="row1">
<td colspan="8" style="background-color:#e7e7e7"><p align="left" style="padding-top:5px; padding-bottom:5px;">
<input type="button" onClick="addRow('dataTable')" value="Add More" class="search" />&nbsp;
<input type="button" name="profile_popup" value="Pick Items" id="profile_popup"  onclick="open_items()" class="search" />
<input type="button" class="search"  onclick="deleteRow('dataTable')" value="Delete" />
</p></td></tr>
</table> 
 
<div style="overflow: auto; width:956px;">
<div id="showdata">
<table align="center" width="100%;" border="1" cellpadding="3" cellspacing="0">
<tr class="row1"><td class="tabledata" style="text-align:center;" colspan="14">Item Details</td></tr>
<tr class="row1">
	<td width="4%" class="tabledata">Sr. No.</td>
	<td width="1%" class="tabledata">&nbsp;</td>
    <td width="10%" class="tabledata">Item Code</td>
    <td class="tabledata">Item Name</td>
	<td width="13%" class="tabledata">Size</td>
	<td colspan="5" class="tabledata" style="text-align:center;" width="120">Quantity</td>
    <td  class="tabledata">Per Unit</td>
	<td class="tabledata">Price</td>
    <td  class="tabledata">Value</td>
    <td  class="tabledata">Total stock Qty</td>
</tr>
<?php
$itemSql="select * from crm_lead_items where mst_leadid='".$_REQUEST['leadid']."'";
$result = DB_query($itemSql,$db);
	   while($itemrow = DB_fetch_array($result))
	   { 
               $itemName=$s->GetFieldValue('item_master',"id='".$itemrow['item_code']."'",'item_name');
               $itemcodes=$s->GetFieldValue('item_master',"id='".$itemrow['item_code']."'",'StockID');
               $primUnitId=$s->GetFieldValue('item_master',"id='".$itemrow['item_code']."'",'mst_unitid');
               $SecUnitId=$s->GetFieldValue('item_master',"id='".$itemrow['item_code']."'",'conv_unit');
               
               $unitName=$s->GetFieldValue('unitsofmeasure',"unitid='".$primUnitId."'",'unitname');
               $SecunitName=$s->GetFieldValue('unitsofmeasure',"unitid='".$SecUnitId."'",'unitname');
?>
  <tbody id="dataTable">
  	<tr class="row1">
<td style="white-space:nowrap;" class="tabledata"><input name="delitem[]" type="checkbox" value="" />
</td>	
<td class="tabledata"><input type="hidden" name="item_code[]" id="itemid" value="<?php echo $itemrow['item_code']; ?>"/></td>
<td class="tabledata"><input type="text" name="" readonly  id="itemcode" size="9" style="width:100%;"  value="<?php echo $itemcodes; ?>"/></td>
<td class="tabledata" style="white-space:nowrap"><input type="text" name="" readonly value="<?php echo $itemName; ?>" id="itemname" style="width:100%;"/></td>
<td class="tabledata" style="white-space:nowrap">
<select style="width:100%;" name="size[]" id="size">
													<option>--Select--</option>
<?php $SQLSize=DB_query("SELECT * FROM size_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
												while($sizeid=DB_fetch_array($SQLSize)){?>
                                                                                <option value="<?php echo $sizeid['id']; ?>" <?php if($sizeid['id']==$itemrow['size']) { echo 'selected'; } else { echo ''; } ?>><?php echo $sizeid['size']; ?></option>
												<?php } ?>
												</select>
</td>

<td class="tabledata"><input type="text"  name="stock_qty[]"  id="stock_qty" style="width:100%;" onkeyup="calcValue1(this.value,this.id)" onblur="calcValue1(this.value,this.id)" value="<?php echo $itemrow['stock_qty']; ?>"/></td>
<td class="tabledata">
    <input type="text" readonly name="unit_prim[]"  id="unit_prim" style="width:100%;" value="<?php echo $unitName; ?>"/>
    </td>
<td class="tabledata"><input type="hidden" name="conv_fac" id="conv_fac" /></td>
<td class="tabledata"><input type="text" readonly name="secondary_qty[]"  id="secondary_qty" style="width:100%;" value="<?php echo $itemrow['stock_qty2']; ?>"/></td>
<td class="tabledata">
    <input type="text" readonly name="unit_sec[]"  id="unit_sec" style="width:100%;" value="<?php echo $SecunitName; ?>"/></td>
				
<td class="tabledata"><input type="text" name="stock_perunit[]" id="stock_perunit" style="width:100%;" value="<?php echo $itemrow['stock_perunit']; ?>" onkeyup="calcValue3(this.value,this.id)"/></td>
<td class="tabledata"><input type="text" name="stock_price[]" value="<?php echo $itemrow['stock_price']; ?>" id="stock_price" style="width:100%;" onkeyup="calcValue2(this.value,this.id)"/></td>
<td class="tabledata"><input type="text" name="stock_value[]" value="<?php echo $itemrow['stock_value']; ?>" id="stock_value" style="width:100%;"/></td>
<td class="tabledata"><input type="text" name="total_stock[]" value="<?php echo $itemrow['total_stock']; ?>" id="total_stock" style="width:100%;"  readonly="true" /></td>

</tr>
</tbody>
 <?php } ?>
 </table>
 </div>
 </div>

<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
 <input type="hidden" name="" value="" id="edit_cnt" />

<div >

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr class="row1">
    <td class="tabledata1" colspan="4"><h3 style="color:#003399">Groups & Users Assignment :-</h3></td>
  </tr>

  <tr class="row1">
    <td class="tabledata1" >Groups Name <label></label></td>
    <td>
	<?php 
        $groupArray=explode(',', $groupid);
    foreach($groupArray as $groupidInd)
    {
    echo $s->getField('securityroles','secroleid',$groupidInd,'role_name').'<br/>';
    }
    ?>	
	 </td>
    <td class="tabledata1" >Users Name <label></label></td>
    <td width="28%">
	<div id='hid-Roleid'>
	<?php
	$userArray=explode(',',$userid);
        foreach($userArray as $user)
    {
    echo $s->getField('www_users','id',$user,'realname').'<br/>';
    }
    ?>

 </div>	
 </td>
  </tr>
  
</table>



</div><!-- form 5 close -->

<div >

<table width="100%" border="0" cellspacing="2" cellpadding="3">
<tr class="row1">
    <td colspan="4"><h3 style="color:#003399">Add Doc/PDF :-</h3></td>
  </tr>

<tr class="row1">
    <td class="tabledata1" > Attach File</td>
    <td width="28%"> <input type="file" name="leadfile" id="leadfile" value="" /></td>
	
	</td>
<tr class="row1">
    <td colspan="4"><h3 style="color:#003399">Others :-</h3></td>
  </tr>

  <tr class="row1">
    <td class="tabledata1" >Remarks</td>
    <td width="28%"><textarea style="width:700px; height:50px;" name="remark"> <?php  echo $description;?></textarea></td>
	
	</td>
  </tr>

  <tr class="row1">
    <td width="28%" colspan="2"><p align="center"><input type="submit" value="Save"  name="Save" onClick="return validate();" class="search"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Reset" class="search" /></p></td>
	
	</td>
  </tr>
</table>
</div><!-- form 6 close -->
	</form>
</div>
	</div> <!-- form1 div close -->


<script>
function showstorecode(str)
{
var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");	
	
	document.getElementById("hid-Godown-div").innerHTML=data[0];
    //document.getElementById("empdta").innerHTML=data[1];
	
    }
  }
  xmlhttp.open("GET","lead_storecode.php?site="+str,true);
	xmlhttp.send();
}



function GetClientDetails(str)
{
	if(str=='new'){
	document.getElementById("hid-newclient").style.display='block';
	document.getElementById("contact_name").value='';
	document.getElementById("contact_name2").value='';
	document.getElementById("zone").value='0';
	document.getElementById("designation").value='';
	document.getElementById("fname").value='';
	document.getElementById("mname").value='';
	document.getElementById("lname").value='';
	document.getElementById("address1").value='';
	document.getElementById("address2").value='';
	document.getElementById("address3").value='';
	document.getElementById("email").value='';
	document.getElementById("phone").value='';
	document.getElementById("mobile").value='';
	document.getElementById("fax").value='';
	document.getElementById("zipcode").value='';
	document.getElementById("country").value='0';
	document.getElementById("state").value='0';
	document.getElementById("city").value='0';
	}
	else
	{
	var xmlhttp;
	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)
	{
	var data=xmlhttp.responseText.split("__");	
	document.getElementById("ContactPerson").innerHTML=data[0];
	document.getElementById("firstname").innerHTML=data[1];
	document.getElementById("middlename").innerHTML=data[2];
	document.getElementById("LastName").innerHTML=data[3];
	document.getElementById("Add1").innerHTML=data[4];
	document.getElementById("Add2").innerHTML=data[5];
	document.getElementById("Add3").innerHTML=data[6];
	document.getElementById("Email").innerHTML=data[7];
	document.getElementById("Phone").innerHTML=data[8];
	document.getElementById("mobl").innerHTML=data[9];
	document.getElementById("Fax").innerHTML=data[10];
	document.getElementById("ZIPC").innerHTML=data[11];
	document.getElementById("COUNTRY").innerHTML=data[12];
	document.getElementById("show_state").innerHTML=data[13];
	document.getElementById("show_city").innerHTML=data[14];
	document.getElementById("hid_POrder").innerHTML=data[15];
	}
	}
	xmlhttp.open("GET","CRM_leadclient.php?site="+str,true);
	xmlhttp.send();
	}
}



function CheckClietName(str)
{
	var xmlhttp;
	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("chkclient").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","CRM_CheckClientName.php?name="+str,true);
	xmlhttp.send();
}

</script>


