<?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');
$table="crm_leads";

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


	/*$fstatus=true;
	if($_REQUEST['mst_branchid']=='000')
	{
	$msgErr[]="Please Select Branch"	;
	$fstatus=false;
	}
	if($_REQUEST['leads_date']=='')
	{
	$msgErr[]="Please Enter Date"	;
	$fstatus=false;
	}	
	if($_REQUEST['leads_date']!='')
	{
	$_SESSION['crm_leads_date']=$_REQUEST['leads_date'];
	}
	if($_REQUEST['lead_subject']=='')
	{
	$msgErr[]="Please Enter the Subject"	;
	$fstatus=false;
	}
	if($_REQUEST['lead_subject']!='')
	{
	$_SESSION['crm_lead_subject']=$_REQUEST['lead_subject'];
	}
	if($_REQUEST['lead_Priority']=='0')
	{
	$msgErr[]="Please Select Priority"	;
	$fstatus=false;
	}
	if($_REQUEST['lead_Category']=='0')
	{
	$msgErr[]="Please Select the Lead Category"	;
	$fstatus=false;
	}
	if($_REQUEST['lead_status']=='0')
	{
	$msgErr[]="Please Select the Lead Status"	;
	$fstatus=false;
	}
	if($_REQUEST['contact_name']=='')
	{
	$msgErr[]="Please Enter the Contact Person"	;
	$fstatus=false;
	}
	if($_REQUEST['company_name']=='')
	{
	$msgErr[]="Please Enter the Company Name"	;
	$fstatus=false;
	}
	if($_REQUEST['fname']=='')
	{
	$msgErr[]="Please Enter the First Name"	;
	$fstatus=false;
	}*/
	/* for($K=0;$K<$_REQUEST['countItems'];$K++)
	{
	
	if($_REQUEST['Quantity'][$k]=='0')
	{
	$msgErr[]="Please Enter Quantity".$k;
	$fstatus=false;
	}
	if($_REQUEST['Rate'][$k]=='0')
	{
	$msgErr[]="Please Enter Rate".$k;
	$fstatus=false;
	}
	if($_REQUEST['Discount'][$k]=='0')
	{
	$msgErr[]="Please Enter Discount".$k;
	$fstatus=false;
	}
	}*/
}

//************************SAVE RECORDS IN THE TABLES*************************START HERE***********************************
//if($_REQUEST['Save']!='' and $fstatus )
if($_REQUEST['Save']!='')
{
	$PreSql=DB_query("select prefix from locations where loccode='".$_REQUEST['mst_branchid']."'",$db);
	$Prefix=DB_fetch_array($PreSql);
	$chkLC=DB_query("select * from crm_leads where mst_branchid='".$_REQUEST['mst_branchid']."'",$db);
	if(DB_num_rows($chkLC)>0)
	{
	$GetMxSQL=DB_query("select MAX(leadcode) AS MAXLC from crm_leads where mst_branchid='".$_REQUEST['mst_branchid']."'",$db);
	$GetMAXID=DB_fetch_array($GetMxSQL);
	$Explode=explode("/",$GetMAXID['MAXLC']);
	$FinalMaxId=$Explode[2]+1;
	$FinalCode=$Prefix['prefix']."/"."Lead"."/".str_pad($FinalMaxId,5,0,STR_PAD_LEFT);
	}
	else
	{
	$FinalCode=$Prefix['prefix']."/"."Lead"."/"."00001";
	}
	$data['mst_branchid']=$_REQUEST['mst_branchid'];
	$data['store']=$_REQUEST['store'];
	$data['leads_date']=ConvertSQLDate($_REQUEST['leads_date']);
	$data['leadcode']=$FinalCode;
	$data['lead_subject']=$_REQUEST['lead_subject'];
	$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'];
	if($_REQUEST['company_name']=='new')
	{
	 $comapany=$_REQUEST['crm_newclient'];
	}
	else
	{
	$comapany=$_REQUEST['company_name'];
	}
	$data['company_name']=$comapany;
	$data['contact_name']=$_REQUEST['contact_name'];
	$data['fname']=$_REQUEST['fname'];
	$data['department']=$_REQUEST['mst_departmentid'];
	$data['designation']=$_REQUEST['designation'];
	$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['groupname']=$_REQUEST['groupname'];
	$data['created_date']=date('Y-m-d');
	$data['description']=trim($_REQUEST['description']);
	if($_REQUEST['groupid']!='0'){
	$data['groupid']=trim(implode(',',$_REQUEST['groupid']));
	}
	if($_REQUEST['leaduserid']){
	$data['leaduserid']=trim(implode(',',$_REQUEST['leaduserid']));
	}
	$data['lead_function']=trim($_REQUEST['lead_function']);
	$data['deleted_status']='N';
	$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(leadid) from crm_leads",$db);
	$maxiddata=DB_fetch_array($maxid);
	$maxnumber=$maxiddata['Max(leadid)']+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/Doc/";	
	if(move_uploaded_file($_FILES['leadfile']['tmp_name'],$path.$new_name))
	{
	
	echo "<strong>Copy Unsuccessful</strong>";
	
	}
	}
	$data['leadfile']=trim($new_name);
		
	$row= $s->RecordInsert($table, $data);
	$lastid=DB_Last_Insert_ID($db,'crm_leads', 'leadid');
	
	

	
	
	
	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']=$lastid;
	$rowinsert= $s->RecordInsert('crm_lead_items', $data_arr);
	}
	
	if($_REQUEST['leaduserid']!='')
	{
		for($i=0;$i<count($_REQUEST['leaduserid']);$i++)
		{
		$email[]=$s->GetFieldValue('www_users',"id='".$_REQUEST['leaduserid'][$i]."'",'email');
		}
	//$emails=implode(",",$email);
	 $to=$_REQUEST['email'];
	$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***********************************

?>
<!------------------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("lead_date").value;

window.open("leadTelephonicItems.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;
	}
}

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("lead_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("Lead Date Can't Be Less than Current Date");
		document.getElementById("lead_date").value='';
		document.getElementById("lead_date").focus();
        return false;
    }
    else
    {
        return true;
        
    } 




}
</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("lead_date").value=='')
	{
	alert("Lead Date must be filled out");
	return false;
	}
	//if(document.getElementById("lead_subject").value=='')
//	{
//	alert("Subject must be filled out");
//	return false;
//	}
	if(document.getElementById("lead_Priority").value=='0')
	{
	alert("Lead Priority  must be filled out");
	return false;
	}
	//if(document.getElementById("lead_Category").value=='0')
//	{
//	alert("Lead Category  must be filled out");
//	return false;
//	}
	//if(document.getElementById("lead_status").value=='0')
//	{
//	alert("Lead Status must be filled out");
//	return false;
//	}
	if(document.getElementById("contact_name").value=='')
	{
	alert("Contact Name must be filled out");
	return false;
	}
	if(document.getElementById("company_name").value=='')
	{
	alert("Company Name  must be filled out");
	return false;
	}
	
}

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

function getOther(int)
{
	if(int=="other")
	{
	document.getElementById("otherReason").style.display="inline-block";
	}
	else
	{
	document.getElementById("otherReason").style.display="none";
	}
}
</script>

<link rel="stylesheet" href="file://///192.168.1.55/mxerp-m/CRM/css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Leads Management</h1>
<form action="" name="frm"	 method="post" enctype="multipart/form-data">				
<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']."') ",$db);?> 
		<select name="mst_branchid" id="mst_branchid" style="width:150px;" onchange="showstorecode(this.value)">
			<option value="000">Select Branch</option>
			<?php while($SiteName=DB_fetch_array($sql_state)) {?>]
			<option value="<?php echo $SiteName['loccode']; ?>"><?php echo $SiteName['locationname']; ?></option>
			<?php }?>
		</select><div id="hid-Godown-div" style="display:none"><select name="store"  style="width:150px;" id="store">
		<?php $store_sql = "SELECT id, StoreName,StoreCode FROM storetype where Store_Site = '".$_GET['site']."'";
		
		$store_result = DB_query($store_sql,$db);
			while($store_row = DB_fetch_array($store_result))
			{
				;
				$p='';
				if($store_row['id'] == $_POST['store'])
				$p="selected";
				?>
				<option value="<?php echo $store_row['id']; ?>"<?php echo $p; ?>><?php echo $store_row['StoreName']; ?></option>
	<?php } ?>
		</select>
		
		</div>	</td>
	<td><span class="tabledata1">Lead No.</span></td>
	<td><div id='hid_leads'>
	  <input type="text" name="leadcode" style="width:150px;" value="Auto Generated" 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" class="datepicker" 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" >Subject</td>
    <td class="tabledata1" ><input type="text" name="lead_subject" style="width:150px;" value="<?php  echo $lead_subject;?>" id="lead_subject" /></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 echo  $Type['name'];?></option>
      <?php }?>
    </select></td>
    <td class="tabledata1" >Lead Status</td>
    <td colspan="3"><?php $lstatus=array('Active','Cold','Hot','Warm','Attempted to Contact','Contact in Future','Contacted','Not Contacted','Junk Lead','Lost Lead','Qualified','Pre Qualified'	,'Techno commercial submitted','Proposal submitted','Proposal to be submitted','Negotiation done','Negotiation going on')?>
      <select name="lead_status"  style="width:150px;" id="lead_status" >
        <?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 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="other">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($_REQUEST['lead_source']==$Lead['id']) { echo 'selected'; } else { echo ''; } ?>><?php echo  $Lead['name'];?></option>
      <?php }?>
    </select></td>
  </tr>

<tr class="row1">
 <td colspan="8" style="padding:0px; border:none;">
 <table cellpadding="0" cellspacing="0" id="otherReason" style="display:none;">
 <tr>
    <td class="tabledata1">Reference Name</td>
    <td class="tabledata1"><input type="text" name="ref_name" style="width:150px;" id="ref_name" /></td>
	
	<td class="tabledata1">Address</td>
    <td class="tabledata1" ><input type="text" name="ref_addres" style="width:150px;" id="ref_addres" /></td>
	
	<td class="tabledata1" >Mobile No.</td>
    <td class="tabledata1" ><input type="text" name="ref_phone" style="width:150px;" id="ref_phone" /></td>
	
	<td class="tabledata1" >Email ID</td>
    <td class="tabledata1" ><input type="text" name="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">Cleint Name  <font color="#FF0000">*</font></td>
    <td style="width:200px;">
		<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 Name</td>
    <td  class="tabledata1" ><input type="text" name="groupname" style="width:150px;" value="<?php  echo $groupname;?>" /  /></td>
    <td  class="tabledata1">Commodity</td>
    <td colspan="3"><input type="text" 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">Address II <font color="#FF0000">&nbsp;</font></td>
    <td  class="tabledata1"  id="Add2" ><input type="text" name="address2" id="address2" style="width:150px;" value="" /></td>
    
    <td  class="tabledata1">Address III </td>
    <td  id="Add3" colspan="3"><input type="text" name="address3" style="width:150px;" value="<?php  echo $groupname;?>"   /></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 ($pre_city==$city_row['id']?"selected":"") ?>><?php echo $city_row['city']; ?></option>
	   <?php }?>
      </select>	</td>
	<td colspan="4">
	<div id="searchstate">
	<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	  <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)">
 	   <?php 
	    $sql = "select * from state_master where id = '".$pre_state."' ";
		$result=DB_query($sql,$db);
		while($myrow=DB_fetch_array($result))
		{ 
			$p='';
			if($myrow['id'] == $pre_state)
			$p="selected";
		?>
		<option value="<?php echo $myrow['id'];?>" <?php echo ($row['pre_state']==$myrow['id']?"selected":"") ?>><?php echo $myrow['state']; ?></option>
		<?php }
	   ?>
      </select></td>
     <td width="138" align="left">Country</td>
    <td align="left" id="COUNTRY" colspan="3"><select name="country" id="country" style="width:150px;"  >
          <option value="">---Select Country---</option>
        <?php 
			$c_sql = "select * from country_master where id='".$pre_country."'";
			$c_result = DB_query($c_sql,$db);
			while($c_row=DB_fetch_array($c_result))
			{
				$p='';
				if($c_row['id']==$pre_country)
				$p= "selected";
			?>
				<option value="<?php echo $c_row['id']; ?>"<?php echo ($pre_country==$c_row['id']?"selected":"") ?>><?php echo $c_row['country']; ?></option>
	  <?php }
		?>
		</select></td>
	</tr>
	</table>
	</div></td></tr>
      
 <tr class="row1">
 
  <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">Phone No.</span></td>
    <td class="tabledata1" id="Phone" ><input type="text" name="phone" id="phone" style="width:150px;"  value="<?php  echo $phone;?>"/></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" >Fax No.</td>
    <td id="Fax"><input type="text" name="fax" id="fax" style="width:150px;" value="<?php  echo $fax?>" /></td>
    <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 class="tabledata1" ></td>
	<td colspan="3">&nbsp;</td>
 </tr> 
  



<tr class="row1">
    <td colspan="8"><h3 style="color:#003399">Contact Person  Information </h3></td>
  </tr>
   
    <tr class="row1">
    <td class="tabledata1" >Contact Person I <font color="#FF0000">*</font></td>
    <td  class="tabledata1" id="ContactPerson"><input type="text" name="contact_name" id="contact_name" style="width:150px;" value="<?php  echo $contact_name;?>" /></td>
    <td class="tabledata1" >Dept.</td>
    <td id="middlename"><select name="mst_departmentid" style="width:150px;"  onchange="ShowDiv(this.value,'hid_POrder','disp_department.php')">
      <option value="0">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 $department==$dept_row['id']?'selected':''?>><?php echo $dept_row['deptname']; ?></option>
      <?php }	?>
    </select>    </td>
    <td  class="tabledata1" >Designation</td>
    <td id="LastName" colspan="3"><div id='hid_POrder'>
      <select name="designation"  id="designation" style="width:150px;" >
        <option value="0">Select</option>
        <?php
    $sql_po= "select * from desigmaster Where mst_departmentid ='".$department."'";
	 $result=DB_query($sql_po,$db);
    while($myrow=DB_fetch_array($result))
	{
		
	?>
        <option value="<?php echo $myrow['id'];?>"<?php echo $designation==$myrow['id']?'selected':'';?>><?php echo $myrow['designame'];?></option>
        <?php }?>
      </select>
    </div></td>
  </tr>


  <tr class="row1">
    <td class="tabledata1" >Contact Person II </td>
    <td id="firstname"><input type="text" name="fname" id="fname" style="width:150px;" value="<?php  echo $fname;?>" /></td>
    <td class="tabledata1" >Dept.</td>
    <td id="middlename"><select name="mst_departmentid1" style="width:150px;"  onchange="ShowDiv(this.value,'hid_POrder1','disp_department.php')">
      <option value="0">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 $department==$dept_row['id']?'selected':''?>><?php echo $dept_row['deptname']; ?></option>
      <?php }	?>
    </select>    </td>
    <td  class="tabledata1" >Designation</td>
    <td id="LastName" colspan="3"><div id='hid_POrder1'>
      <select name="designation1"  id="designation1" style="width:150px;" >
        <option value="0">Select</option>
        <?php
    $sql_po= "select * from desigmaster Where mst_departmentid ='".$department."'";
	 $result=DB_query($sql_po,$db);
    while($myrow=DB_fetch_array($result))
	{
		
	?>
        <option value="<?php echo $myrow['id'];?>"<?php echo $designation==$myrow['id']?'selected':'';?>><?php echo $myrow['designame'];?></option>
        <?php }?>
      </select>
    </div></td>
  </tr>
 
<tr class="row1">
    <td colspan="8"><h3 style="color:#003399"> Product Information</h3></td>
  </tr>
  <tr class="row1">
<td colspan="11" style="background-color:#e7e7e7"><p align="left" style="padding-top:5px; padding-bottom:5px;">
  <input name="button" type="button" class="search" accesskey="A" onclick="addRow('dataTable')" value="Add Rows" />
  &nbsp;
  <input type="button" name="profile_popup" value="Pick Items" id="profile_popup"  onclick="open_items()" accesskey="I" class="search" />
  <input type="button" class="search"  onclick="deleteRowOrder('dataTable')" value="Delete" />
</p></td></tr>
</table> 
 
<div style="overflow: auto; width:100%;">
<div id="showdata">
    <table align="center" width="100%;" border="1"  cellpadding="2" cellspacing="0">

<tr class="row1">
	<td class="tabledata" width="4%">Sr. No.</td>
	<td  class="tabledata" width="1%">&nbsp;</td>
    <td class="tabledata" width="10%">Item Code</td>
    <td  class="tabledata" width="10%">Item Name</td>
	<td class="tabledata" width="6%">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>
	<td class="tabledata">&nbsp;</td>
</tr>
<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" /></td>
<td class="tabledata"><input type="text" name="" value="" id="itemcode" size="9" style="width:100%;" /></td>
<td class="tabledata" style="white-space:nowrap"><input type="text" name="" value="" id="itemname" style="width:100%;" /></td>
<td class="tabledata" style="white-space:nowrap"><select style="width:100%;" name="size[]" id="size" 
   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 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)" /></td>
<td class="tabledata"><select name="unit_prim[]" id="unit_prim" style="width:70px;" >
    <option value="0">Select</option>
  </select></td>
<td class="tabledata"><input type="hidden" name="conv_fac" id="conv_fac" /></td>
<td class="tabledata"><input type="text" name="secondary_qty[]"  id="secondary_qty" style="width:100%;"/></td>
<td class="tabledata"><select name="unit_sec[]" id="unit_sec" style="width:70px;" >
            <option value="0">Select</option>
                </select></td>
				
<td class="tabledata"><input type="text" name="stock_perunit[]" id="stock_perunit" style="width:100%;" value="1" onkeyup="calcValue3(this.value,this.id)"/></td>
<td class="tabledata"><input type="text" name="stock_price[]" value="" id="stock_price" style="width:100%;" onkeyup="calcValue2(this.value,this.id)"/></td>
<td class="tabledata"><input type="text" name="stock_value[]" value="" id="stock_value" style="width:100%;"/></td>
<td class="tabledata"><input type="text" name="total_stock[]" id="total_stock" style="width:100%;"  readonly="true" /></td>
<td class="tabledata"><input type="hidden" name="conf[]" id="conf" style="width:100%;"  readonly="true" /></td>

</tr>
</tbody>
 </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><font color="#FF0000"></font></label></td>
    <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))
					{
					$i++;?>
					<input type='checkbox' name='groupid[]' id='groupid_<?php echo $i; ?>' onclick='getUsers(this.value,<?php echo $i; ?>);' value='<?php echo $row['secroleid']; ?>'><?php echo $row['secrolename'].'<br/>';
					}?>	 </td>
					
    <td class="tabledata1" >Users Name <label><font color="#FF0000"></font></label></td>
    <td>
	<div id='hid-Roleid'>
	<input type="text" name="countGroup" id="countGroup" value="<?php echo $i.'-----'; ?>" />
	
	</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">Document Attachments</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="description"> <?php  echo $description;?></textarea></td>
	
	</td>
  </tr>

  <tr class="row1">
    <td width="28%" colspan="2"><p align="center"><input type="submit" accesskey="S" 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 -->
<div id="hid-users-div">ii</div>

<script>
function getUsers(opt)
{
alert(opt);

	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;
		if(document.getElementById("groupid_"+i).checked==true)
		{sub_cat_array[i]=item_sub_category;}
	}
	alert(sub_cat_array);
	
}

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("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
	{
	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];
	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 checksize(str,str1)
{

if(document.getElementById("hiditemcount").value!='')
var count=document.getElementById("hiditemcount").value;
else
var count=0;

var divstr=str1.split('_');



if(divstr.length>=2)
{
 

for(var m=1;m<=count;m++)
{
	
	if(document.getElementById("itemid").value==document.getElementById("itemid_"+m).value)
	{
		
		
		
		//alert(document.getElementById("size").value);
		//alert(document.getElementById("size_"+m).value);
		
			if(document.getElementById("size").value==document.getElementById("size_"+m).value)
			{	
			var val=document.getElementById("size_"+m).value;		
				alert('Duplicate size choosen --2');
				//select.options[val].selected=false
				document.getElementById("size_"+m).focus();
				
			
			}
		  
	}
	
	
} 

for(var t=1;t<=count;t++)
{

for(var i=1;i<=count;i++)
{
	var j=i+1;
	
	//alert(document.getElementById("size_"+t).value);
		//alert(document.getElementById("size_"+j).value);
	
	if(document.getElementById("itemid_"+t).value==document.getElementById("itemid_"+j).value)
	{
		
			if(document.getElementById("size_"+t).value==document.getElementById("size_"+j).value)
			{
			
				alert('Duplicate size choosen');
				document.getElementById("size_"+j).value='';
				//select.options[str1].selected=false
				document.getElementById("size_"+j).focus();	
					
			}
		  
	}
	
	
}	}

}
else {

for(var m=1;m<=count;m++)
{
	
	if(document.getElementById("itemid").value==document.getElementById("itemid_"+m).value)
	{
		
		
			if(document.getElementById("size").value==document.getElementById("size_"+m).value)
			{
			    alert('Duplicate size choosen');
				//select.options[str1].selected=false
				document.getElementById("size_"+m).focus();	
					
							
			}
		  
	}
	
	
}	
			

}



 

}


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();

}




}



</script>


