<?php 
//****************MODIFY BY AMIT SINGH ************D&D************************23-11-2012*********************************
// include('Class_PickListEditor.php');
include('Class_PickListEditor.php');
include("ClassRole.php");
include("includes/Class_Branch.php");
include('includes/stockcalculation.php');
$max_size='2000';
$flag=0;
function extension($filename)
{
$name_brk=explode('.',$filename);
return  $name_brk[1];
} 
if($_REQUEST['act']=='delitem')
{
	$DelCond="id='".$_REQUEST['id']."'";
	$s->DeleteRecord('crm_lead_items',$DelCond);
	echo "<script>location.href='index.php?route=CRM/crmleadview&action=Edit&leadid=".$_REQUEST['leadid']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

$catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');
$table="crm_leads";

if($_REQUEST['Save'])
{
	if($_REQUEST['company_name']=='new')
	{
	$ClientID=$_REQUEST['crm_newclient'];
	}
	else
	{
	$ClientID=$_REQUEST['company_name'];
	}
	$data['mst_branchid']=$_REQUEST['mst_branchid'];
	$data['leads_date']=ConvertSQLDate($_REQUEST['leads_date']);
	$data['lead_subject']=$_REQUEST['lead_subject'];
	$data['department']=$_REQUEST['mst_departmentid'];
	$data['designation']=$_REQUEST['designation'];
	$data['groupname']=$_REQUEST['groupname'];
	$data['lead_Category']=$_REQUEST['lead_Category'];
	$data['lead_source']=$_REQUEST['lead_source'];
	$data['lead_Stage']=$_REQUEST['lead_Stage'];
	$data['lead_status']=$_REQUEST['lead_status'];
	$data['sale_executive']=$_REQUEST['sale_executive'];
	$data['lead_Priority']=$_REQUEST['lead_Priority'];
	$data['company_name']=$ClientID;
	$data['contact_name']=$_REQUEST['contact_name'];
	//$data['fname']=$_REQUEST['fname'];
	$data['department1']=$_REQUEST['mst_departmentid1'];
	$data['designation1']=$_REQUEST['designation1'];
	$data['address1']=$_REQUEST['address1'];
	$data['address2']=$_REQUEST['address2'];
	$data['address3']=$_REQUEST['address3'];
	$data['email']=$_REQUEST['email'];
	$data['phone']=$_REQUEST['phone'];
	$data['fax']=$_REQUEST['fax'];
	$data['mobile']=$_REQUEST['mobile'];
	//$data['street']=$_REQUEST['street'];
	$data['zipcode']=$_REQUEST['zipcode'];
	$data['country']=$_REQUEST['country'];
	$data['city']=$_REQUEST['city'];
	$data['state']=$_REQUEST['state'];
	$data['created_date']=date('Y-m-d');
	$data['description']=trim($_REQUEST['description']);
	if($_REQUEST['groupid']!='0'){
	$data['groupid']=trim(implode(',',$_REQUEST['groupid']));
	}
	if($_REQUEST['userid']){
	$data['userid']=trim(implode(',',$_REQUEST['userid']));
	}
	$data['lead_function']=trim($_REQUEST['lead_function']);
	$data['deleted_status']='N';
	$data['created_by']=trim($_SESSION['UserID']);
	
	$leadfile=$_FILES['leadfile']['name'];
	$type=extension($leadfile);
	if($leadfile!=''){
	$tmp_name=$_FILES['leadfile']['tmp_name'];		 
	$new_name='Lead-'.$_REQUEST['leadid'].'.'.$type;
	$path="CRM/Doc/".$new_name;
	$file_copy=copy($tmp_name,$path);
	if($file_copy)
	$data['leadfile']=trim($new_name);
	} 	 
	$cond="leadid='".$_REQUEST['leadid']."'";
	$row= $s->RecordEdit($table, $data,$cond);
	
	$lastid=$_REQUEST['leadid'];	
	
	$del=DB_query("delete from crm_lead_items where mst_leadid='".$_REQUEST['leadid']."'",$db);
	for($i=0;$i<count($_REQUEST['item_code']);$i++)
	{
	$data_arr['item_code']=$_REQUEST['item_code'][$i];
	$data_arr['size']=$_REQUEST['size'][$i];
	$data_arr['grade']=$_REQUEST['grade'][$i];
	$data_arr['stock_qty']=$_REQUEST['stock_qty'][$i];
	$data_arr['stock_price']=$_REQUEST['stock_price'][$i];
	$data_arr['stock_perunit']=$_REQUEST['stock_perunit'][$i];
	$data_arr['stock_value']=$_REQUEST['stock_value'][$i];
	$data_arr['total_stock']=$_REQUEST['total_stock'][$i];
	$data_arr['stock_qty2']=$_REQUEST['secondary_qty'][$i];
	$data_arr['primary_unit']=$_REQUEST['unit_prim'][$i];
	$data_arr['secondary_unit']=$_REQUEST['unit_sec'][$i];
	
	$data_arr['mst_leadid']=$_REQUEST['leadid'];
	$rowinsert= $s->RecordInsert('crm_lead_items', $data_arr);
	}
	
	$s->pageLocation("index.php?route=CRM/View_Leads".$rurl);
}

if($_REQUEST['action']=='Edit' || $_REQUEST['action']=='View')
{
	$getlead=DB_query( "select * from crm_leads where leadid='".$_REQUEST['leadid']."'",$db);
	while($row_lead=DB_fetch_array($getlead)){
	$mst_branchid=$row_lead['mst_branchid'];
	$store=$row_lead['store'];
	$leads_date=$row_lead['leads_date'];
	$leadcode=$row_lead['leadcode'];
	$lead_subject=$row_lead['lead_subject'];
	$company_name=$row_lead['company_name'];
	$contact_name=$row_lead['contact_name'];
	$lead_Stage=$row_lead['lead_Stage'];
	$lead_source=$row_lead['lead_source'];
	$lead_status=$row_lead['lead_status'];
	$lead_Priority=$row_lead['lead_Priority'];
	$sale_executive=$row_lead['sale_executive'];
	$lead_Category=$row_lead['lead_Category'];
	$fname=$row_lead['fname'];
	$mname=$row_lead['mname'];
	$lname=$row_lead['lname'];
	$department=$row_lead['department'];
	$designation=$row_lead['designation'];
	$department1=$row_lead['department1'];
	$designation1=$row_lead['designation1'];
	$groupname=$row_lead['groupname'];
	$address1=$row_lead['address1'];
	$address2=$row_lead['address2'];
	$address3=$row_lead['address3'];
	$email=$row_lead['email'];
	$groupname=$row_lead['groupname'];
	$phone=$row_lead['phone'];
	$fax=$row_lead['fax'];
	$mobile=$row_lead['mobile'];
	$street=$row_lead['street'];
	$zipcode=$row_lead['zipcode'];
	$country=$row_lead['country'];
	$city=$row_lead['city'];
	$state=$row_lead['state'];
	$description=$row_lead['description'];
	$groupid=$row_lead['groupid'];
	$created_by=$row_lead['created_by'];
	 $userid=$row_lead['userid'];
	$lead_function=$row_lead['lead_function'];
	$lead_source=$row_lead['lead_source'];
	$lead_priority=$row_lead['lead_priority'];
	$lead_status=$row_lead['lead_status'];
	$lead_category=$row_lead['lead_category'];
	$lead_executive=$row_lead['lead_executive'];
	$lead_stage=$row_lead['lead_stage'];
	$leadfile	 =$row_lead['leadfile'];
	$created_date=date('Y-m-d');
	$description=trim($row_lead['description']);
	
	$function_filter[]=$row_lead['mst_branchid'];
	$function_filter[]=$row_lead['store'];
	$function_filter[]=ConvertSQLDate($row_lead['leads_date']);
	$function_filter[]=ConvertSQLDate($row_lead['leads_date']); 	
	}
}
?>
<script language="javascript" type="text/javascript">
function open_items()
{
		window.open("leadTelephonicItemsedit.php?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 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;
	}
}


function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var rowCount = table.rows.length;
	if(document.getElementById("hiditemcount").value=='')
	r =rowCount+1;
	else
	{
	var t=document.getElementById("edit_cnt").value;
	var t1=document.getElementById("hiditemcount").value;
	r=parseInt(t1)+parseInt(t)+1;
	}
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=r;
	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;
	//alert(newcell.childNodes[0].type);
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	//alert(nod_id[0]);
	//alert(nod_id[1]);
	//alert(nod_id[2]);
	
	if(!isNaN(nod_id[1]))
	{
	 newcell.childNodes[0].id = nod_id[0]+'_'+r;
	}
	else 
	{
	var id1=nod_id[0]+'_'+nod_id[1];
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	}
	//alert(newcell.childNodes[0].id );
	break;		
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	
	if(!isNaN(nod_id[1]))
	{
	
	 newcell.childNodes[0].id = nod_id[0]+'_'+r;
	}
	else 
	{	
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	}
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	{
	 newcell.childNodes[0].id = nod_id[0]+'_'+r;
	}
	else 
	{
	var id1=nod_id[0]+'_'+nod_id[1];
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	}
	
	
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	{
	 newcell.childNodes[0].id = nod_id[0]+'_'+r;
	}
	else 
	{
	var id1=nod_id[0]+'_'+nod_id[1];
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	}
	
	
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	{
	 newcell.childNodes[0].id = nod_id[0]+'_'+r;
	}
	else 
	{
	var id1=nod_id[0]+'_'+nod_id[1];
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	}
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	document.getElementById("stock_perunit_"+r).value='1';
	//document.getElementById("stock_perunit_"+rowCount).value='1';
}
	
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--;
					}
					
				}
	var t=document.getElementById("edit_cnt").value;
	if(document.getElementById("hiditemcount").value!='')
	var t1=document.getElementById("hiditemcount").value;
	else
	var t1=0;
	r=parseInt(t1)+parseInt(t)-1;
		document.getElementById("hiditemcount").value=r;		
		}
		catch(e) 
		{
			alert(e);
		}

	}	





function GetClientDetails(str)
{

	if(str=='new'){
	document.getElementById("hid-newclient").style.display='block';
	}
	else
	{
	document.getElementById("hid-newclient").style.display='none';
	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];
	}
	}
	xmlhttp.open("GET","CRM_leadclient.php?site="+str,true);
	xmlhttp.send();
	}
}

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>Leads Management</h1>
<form action=""	 method="post" enctype="multipart/form-data">	
<h3>General Information</h3>	
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right"><strong>Branch</strong></td>
    <td width="20%">
	<?php echo $ClassBranch->SiteRecord('mst_branchid',$mst_branchid);?> 
	<input type="hidden" name="mst_branchid" id="mst_branchid" value="<?php echo $mst_branchid; ?>" />
	<input type="hidden" name="store"  style="width:150px;" id="store" value="<?php echo $$store; ?>" /></td>
    <td width="13%" align="right"><strong>Lead No.</strong></td>
    <td width="20%"><div id='hid_leads'>
      <input type="text" name="leadcode" style="width:98%;" value="<?php echo  $leadcode?>" readonly="true" />
    </div></td>
    <td width="13%" align="right"><strong>Lead Date</strong></td>
    <td><input type="text" name="leads_date" class="datepicker" style="width:98%;" value="<?php  echo ConvertSQLDate($leads_date);?>" /></td>
  </tr> 
  <tr class="row1">
    <td align="right"><strong>Subject</strong></td>
    <td><input type="text" name="lead_subject" style="width:98%;" value="<?php  echo $lead_subject;?>" /></td>
    <td align="right"><strong>Lead Category</strong></td>
    <td><select name="lead_Category"  style="width:98%;">
      <?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']) { ?> selected="selected" <?php } ?> ><?php echo  $Type['name'];?></option>
      <?php }?>
    </select></td>
    <td align="right"><strong>Lead Status</strong></td>
    <td><?php $lstatus=array('--','Attempted to Contact','Cold','Contact in Future','Hot','Junk Lead','Lost Lead','Qualified','Not Contacted','Pre Qualified','Warm','Active','Techno commercial submitted','Negotiation going on','Proposal submitted','Negotiation done','Proposal to be submitted','Contact in Future','Contacted')?>
      <select name="lead_status"  style="width:98%;">
        <?php for($k=0;$k<count($lstatus);$k++)
	{?>
        <option value="<?php  echo $lstatus[$k];?>" <?php  echo $lead_status==$lstatus[$k]?'selected':''?> >
          <?php  echo $lstatus[$k];?>
          </option>
        <?php 
	}
	?>
      </select></td>
  </tr>
   <tr class="row1">
    <td align="right"><strong>Priority</strong></td>
    <td>
	<?php $Priority=array('--','High','Low','Highest')?>
	<select name="lead_Priority"  style="width:98%;">
	<?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 align="right"><strong>Sale Executive</strong></td>
    <td><input type="text" name=""  style="width:98%;" value="<?php  echo $s->getField('www_users','id',$sale_executive,'realname');?>" />
	<input type="hidden" name="sale_executive" value="<?php echo $sale_executive; ?>" />
	</td>
    <td align="right"><strong>Lead source</strong></td>
    <td><?php $leadsource=array('--','By Mail','By Phone');?>
      <select name="lead_source"  style="width:98%;">
        <?php for($k=0;$k<count($leadsource);$k++)
	{?>
        <option value="<?php  echo $leadsource[$k];?>" <?php  echo $lead_source==$leadsource[$k]?'selected':''?> >
          <?php  echo $leadsource[$k];?>
          </option>
        <?php 
	}
	?>
      </select></td>
  </tr>
</table>
<br>
<h3>Client &amp; Communication Information</h3>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right"><strong>Client name</strong></td>
    <td width="20%"><?php $ZoneValue=$s->getField('clientmaster','id',$company_name,'Zone'); 
		if($ZoneValue=='')
		$selas='selected';
	?>
	<select style="width:98%;"  name="company_name"  onchange="GetClientDetails(this.value)" >
		<option value="0">Select Client</option>
		<option value="new" <?php echo $selas;?>>--New Client--</option>
		<?php
		  $ClientSQL="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site in('".$_SESSION['AssignedBranch']."') 
		and Zone in(".$_SESSION['AssignedSites'].")";
		$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'>
	<?php if($ZoneValue=='') {?>
	<input style="width:98%;" name="crm_newclient" type="text" value="<?php  echo $company_name;?>"/> 
	<?php } ?>
	</div></td>
    <td width="13%" align="right"><strong>Group Name</strong></td>
    <td width="20%"><input type="text" name="groupname2" style="width:98%;" value="<?php  echo $groupname;?>" /  /></td>
    <td width="13%" align="right"><strong>Commodity</strong></td>
    <td><input type="text" name="lead_function" style="width:98%;"  value="<?php echo $lead_function;?>"/></td>
  </tr>
  <tr class="row1">
    <td align="right"><strong>Address I</strong></td>
    <td><input type="text" name="address1" style="width:98%;" value="<?php  echo $address1;?>"/></td>
    <td align="right"><strong>Address II </strong></td>
    <td><input type="text" name="address2" style="width:98%;" value="<?php  echo $address2;?>" /></td>
    <td align="right"><strong>Address III</strong></td>
    <td><input type="text" name="address3" style="width:98%;" value="<?php  echo $address3;?>"/></td>
  </tr>
  <tr class="row1">
    <td align="right"><strong>City </strong></td>
    <td><select name="city" id="city" style="width:98%;" onchange="ShowDiv(this.value,'searchstate','get_state_for_lead.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'] == $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 colspan="4" valign="top" style="padding:0;">
  <div id="amc-tab">
	<div id="searchstate">
	<table width="100%" border="0" cellpadding="3" cellspacing="0">
	<tr>
	<td width="13%" align="right"><strong>State</strong></td>
    <td width="35%">
	<select name="state" id="state" style="width:98%;" onchange="search_city(this.value)">
 	   <?php 
	    $sql = "select * from state_master where 1=1 ";
		$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 width="13%" align="right"><strong>Country</strong></td>
    <td >
	<select name="country" id="country" style="width:98%;"  >
          <option value="">---Select Country---</option>
        <?php 
			$c_sql = "select * from country_master where 1=1";
			$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> 
	</tr>
	</table>
	</div></div></td>
   </tr>
   <tr class="row1">
    <td align="right"><strong>Email Id</strong></td>
    <td><input type="text" name="email" style="width:98%;" value="<?php  echo $email;?>" /></td>
    <td align="right"><strong>Phone No.</strong></td>
    <td><input type="text" name="phone" style="width:98%;"  value="<?php  echo $phone;?>"/></td>
    <td align="right"><strong>Mobile No.</strong></td>
    <td><input type="text" name="mobile" style="width:98%;"value="<?php  echo $mobile;?>" /></td>
  </tr>
  <tr class="row1">
    <td align="right"><strong>Fax No.</strong></td>
    <td><input type="text" name="fax" style="width:98%;" value="<?php  echo $fax?>" /></td>
    <td align="right"><strong>Zip Code</strong></td>
    <td><input type="text" name="zipcode" style="width:98%;"  value="<?php  echo $zipcode?>" /></td>     
     <td colspan="2">&nbsp;</td>
	</tr>
  <tr>
  <td colspan="9" id="ContactPerson" style="padding:0px;"></td>
  </tr>
</table>
<br>  
<h3 style="color:#003399">Product Information</h3>
<p align="left">
<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="deleteRowOrder('dataTable')" value="Delete" />
</p>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 solid 1px;"> 
  <tr>
    <td width="3%">S. No.</td>
    <td width="1"></td>
    <td width="10%">Item Code</td>
    <td>Item Name</td>
	<td width="13%">Size</td>
    <td colspan="5" style="text-align:center;">Quantity</td>
    <td width="8%">Price</td>
    <td width="8%">Per Unit</td>
    <td width="8%">Value</td>
    <td width="8%">Total Stock Qty</td>
	<td width="1">&nbsp;</td>
  </tr>
 <tbody id="dataTable">

<?php $i=0; 
$getitem=DB_query("select * from crm_lead_items where mst_leadid='".$_REQUEST['leadid']."' ",$db);
$n= DB_num_rows($getitem);
while($row=DB_fetch_array($getitem)){
$ItemCode=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'StockID');
$ItemName=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'item_name');
$ItemName=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'item_name');
$conv1=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'conv_factor');

if($row['size']==0)
{
$conv=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'conv_factor');
}
else
{
$catid=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'SubCategoryID');
				$door=$s->GetFieldValue('size_master',"type_product='".$catid."' 
				and id='".$row['size']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",'area');

				$frame=$s->GetFieldValue('size_master',"frame_category='".$catid."'
				 and id='".$row['size']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",'frame');

				$shutter=$s->GetFieldValue('size_master',"shutter_category='".$catid."'
				 and id='".$row['size']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",'sutter');
				
					if(!empty($door)) { $conv=$door; }
					else if(!empty($frame)) {$conv=$frame;	}
					else { 	$conv=$shutter;	}
					
							$op=openingBalance($function_filter,$row['item_code']);
							$in_1=receivedMRN($function_filter,$row['item_code']);
							$out_1=issueSlip($function_filter,$row['item_code']);
							$out_2=issueSiteToSite($function_filter,$row['item_code']);		
							$in_2=receivedSiteToSite($function_filter,$row['item_code']);
							$out_3=issueMatarialReturn($function_filter,$row['item_code']);
							$in_3=receivedReturn($function_filter,$myrow['id']);		 
							$invalue=($op+$in_1[0]+$in_2+$in_3);							
							$outvalue=($out_1+$out_2+$out_3);
							$tot=trim($invalue)-trim($outvalue);							
							$total_current=$tot;
}
?>
	<tr bgcolor="#fff">
		<td><input name="delitem[]"  type="checkbox" value="" /></td>
	    <td><input type="hidden" name="item_code[<?php echo $i;?>]" id="itemid_<?php echo $i;?>" value="<?php echo $row['item_code'];?>" /></td>
		<td><input type="text" name="" id="itemcode_<?php echo $i; ?>" size="9" style="width:100%;" value="<?php echo $ItemCode; ?>" /></td>
	    <td><input type="text" name="" id="itemname_<?php echo $i; ?>" style="width:100%;" value="<?php echo $ItemName; ?>" /></td>
	    <td><select style="width:100%;" name="size[]" id="size_<?php echo $i; ?>" onchange="showconvac(this.value,this.id)"><option value="0" >--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($row['size']==$sizeid['id']) { ?> selected="selected" <?php } ?> ><?php echo $sizeid['size']; ?></option>
			<?php } ?>
			</select></td>
	    <td width="5%"><input type="text" name="stock_qty[<?php echo $i;?>]" id="stock_qty_<?php echo $i;?>" style="width:100%;" value="<?php echo $row['stock_qty'];?>" onkeyup="calcValue1(this.value,this.id)" onblur="calcValue1(this.value,this.id)" /></td>
	    <td width="5%"><select name="unit_prim[]" id="unit_prim_<?php echo $i; ?>" style="width:100%;" >
				<?php $unit=DB_query("select * from  unitsofmeasure where unitid ='".$row['primary_unit']."'   ",$db);
				while($unit_row=DB_fetch_array($unit)){ ?>
				<option value="<?php echo $unit_row['unitid']; ?>"><?php echo $unit_row['unitname']; ?></option>
				<?php } ?>
	        </select>
	    </td>
		<td width="5%"><input type="hidden" name="conv_fac" id="conv_fac_<?php echo $i; ?>" value="<?php echo $conv; ?>" /></td>
		<td width="5%"><input type="text" name="secondary_qty[]"  id="secondary_qty_<?php echo $i; ?>" style="width:100%;" value="<?php echo $row['stock_qty2']; ?>"/></td>
		<td width="5%"><select name="unit_sec[]" id="unit_sec_<?php echo $i; ?>" style="width:100%;" >
				<?php $unit=DB_query("select * from  unitsofmeasure where unitid ='".$row['secondary_unit']."'   ",$db);
				while($unit_row=DB_fetch_array($unit)) { ?>
			    <option value="<?php echo $unit_row['unitid']; ?>"><?php echo $unit_row['unitname']; ?></option>
			    <?php } ?>
	        </select></td>	
	    <td><input type="text" name="stock_price[<?php echo $i;?>]" id="stock_price_<?php echo $i;?>" style="width:100%;" value="<?php echo $row['stock_price'];?>" /></td>
	    <td><input type="text" name="stock_perunit[<?php echo $i;?>]" id="stock_perunit_<?php echo $i;?>" style="width:100%;" value="<?php echo $row['stock_perunit'];?>"/></td>
	    <td><input type="text" name="stock_value[<?php echo $i;?>]" id="stock_value_<?php echo $i;?>" style="width:100%;" value="<?php echo $row['stock_value'];?>" onfocus="CalculateTotalLeadAmt(this.value,this.id)"/></td>
	    <td><input type="text" name="total_stock[<?php echo $i;?>]" id="total_stock_<?php echo $i;?>" style="width:100%;" value="<?php echo $total_current;?>" /></td>
	    <td><input type="hidden" name="conf[]" id="conf_<?php echo $i; ?>" style="width:100%;" readonly="true" value="<?php  echo $conv1; ?>" /></td>  
    </tr>
  <?php $i++;} ?>
<input type="hidden" name="" value="<?php echo $i;?>" id="edit_cnt" /> 
</tbody>
 
</table>

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

</div><!-- form 4 close -->
<div style="clear:both;"></div>


<div align="left" style="padding-left:0px;">

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

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

  <tr>
  	<?php 
  	if($created_by==$_SESSION['UserID'])
	{
            $groupArray=explode(',', $groupid);
           //print_r($groupArray);
	?>
  	<td class="tabledata1">Groups Name</th>
    <td>
	<?php $sql_state=DB_query("SELECT secroleid , secrolename FROM  securityroles where rolestatus ='Y' and  companyid='".$_SESSION['ACGCompanyId']."'",$db); 
	$i=0;
	while($row=DB_fetch_array($sql_state))
					{
           if (in_array($row['secroleid'], $groupArray))
            {
            $s='checked';
            }
          else
            {
            $s='';
            }
            
            ?>
					<input type='checkbox' name='groupid[]' id='groupid_<?php echo $i; ?>' onclick='getUsers(this.value,<?php echo $i; ?>);' <?php echo $s; ?>  value='<?php echo $row['secroleid']; ?>'><?php echo $row['secrolename'].'<br/>';
					$i++;}?>	
					<input type="hidden" name="countGroup" id="countGroup" value="<?php echo $i; ?>"/>
	
	 </td>
    <td class="tabledata1">Users Name</th>
    <td width="28%">
	<div id='hid-Roleid'>
	<?php
	$user=explode(',',$userid);
       // print_r($user);
	//echo "SELECT id , realname FROM  www_users where rolepid ='".$groupid."' ";
	$sql_state=DB_query("SELECT id , realname FROM  www_users where companyid ='".trim($_SESSION['ACGCompanyId'])."' ",$db);		
     if(!$sql_state)
{
die('Error.'.DB_error_msg($db));
}
	
	
?>
<div style='width:auto;' >
<?php 
while($row=DB_fetch_array($sql_state))
{

$p='';
 	 if (in_array($row['id'], $user))
	 $p='checked';
	  ?>
 <input name="leaduserid[]" type="checkbox" value="<?php  echo $row['id'];?>" <?php echo $p;?> /><?php  echo $row['realname'];?><br />
 <?php 
 }

?>
</div>
 </div>	
 </td>
 </tr>
 <?php
 }
	
	else {
 	?>
 	<tr class="row1">
 	<td class="tabledata1">Groups Name</th>
    <td width="29%">
    <?php $groupSql= "SELECT secrolename FROM  securityroles where secroleid='".$groupid."' and  rolestatus ='Y' ";
    $groupRes=DB_query($groupSql,$db);
	$groupRow=DB_fetch_array($groupRes);
    ?>
    <input type="hidden" name='groupid[]'  value="<?php echo $groupid; ?>">
    <input type="hidden" name='groupid[]'  value="<?php echo $groupid; ?>">
    <input type="text" style="width:120px" name="group"  readonly="true" id="group" value="<?php echo $groupRow['secrolename'];?>"></td>
    <td class="tabledata1">Users Name</th>
     <td width="28%"><?php $usqrSql= "SELECT id , realname FROM  www_users where rolepid ='".$groupid."' ";
     	$userRes=DB_query($usqrSql,$db);
		$userRow=DB_fetch_array($userRes);
         ?>
         <input type="text" style="width:120px" name="user" readonly="true" id="user" 
         value="<?php echo $userRow['realname'];?>"></td>
 	</tr>
 	<?php
		
	}
	
	?>
</table>
</div><!-- form 5 close -->
<div align="left" style="padding-left:0px;">
<table width="100%" border="0" cellspacing="2" cellpadding="3">
<tr class="row1">
    <td class="tabledata1"> Attatch File</th>
    <td width="90%"> <input type="file" name="leadfile" id="leadfile" value="" /> 
	<?php if(!empty($leadfile)) { ?>
	<a href="CRM/Doc/<?php  echo $leadfile;?>" target="_blank"><img src="img/download1.png"  /></a>
	<?php } ?></td>
<tr class="row1">
    <td colspan="4"><h3 style="color:#003399">Remarks</h3></td>
  </tr>

  <tr class="row1">
    <td colspan="2" class="tabledata1">
      <textarea style="width:950px; height:50px;" name="description"> <?php  echo $description;?>
      </textarea>
    
    </td>
  </tr>

  <tr class="row1">
    <td class="tabledata1" colspan="2"><p align="center"><input type="submit" value="Save"  name="Save" class="search" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Reset" class="search" /></p></td>
	</td>
  </tr>
</table>
</div><!-- form 6 close -->
</form>
	</div> <!-- form1 div close -->
</div>
<script>
function showconvac(str,id)
{
var brk=id.split('_');

if(str=='0')
{

if(brk[1]==undefined)
{
var conf=document.getElementById('conf').value;
	document.getElementById("conv_fac").value=conf;
	}
	else
	{
	var conf=document.getElementById('conf_'+brk[1]).value;
	document.getElementById("conv_fac_"+brk[1]).value=conf;
	}
}
else
{
var brk=id.split('_');

 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)
	{
	if(brk[1]==undefined)
	document.getElementById("conv_fac").value=xmlhttp.responseText;
	else
	document.getElementById("conv_fac_"+brk[1]).value=xmlhttp.responseText;
	//document.getElementById("empdta").innerHTML=data[1];
	
	}
	}
	if(brk[1]==undefined)
	{
	var str2=document.getElementById("itemid").value;
	}
	else
	{
	var str2=document.getElementById("itemid_"+brk[1]).value;
	}
	xmlhttp.open("GET","convactor.php?site="+str+"&itemid="+str2,true);
	xmlhttp.send();

}




}


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
	   {*/	
	   if(str!='')
	   {
		var price=document.getElementById("conv_fac").value;
		
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondary_qty").value=tot.toFixed(3);
				/*}*/
	}
	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;
	   }*/
	     if(str!='')
	   {
	   var price=document.getElementById("conv_fac_"+brk[2]).value;
	  
		var tot=parseFloat(str)*parseFloat(price);
		}
		else
		var tot=0;
		document.getElementById("secondary_qty_"+brk[2]).value=tot.toFixed(3);
	   
		
	}
	
}

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;}
	}
}



</script>
