//step to use 
//1.OnListCli


document.write('<div style="position:absolute;visibility:hidden;" id="ListCN"><select name="ListCustomerName" id="ListCustomerName" multiple onClick="OnListClicked()"></select></div>');
document.write('<input type ="hidden" name="textTyped" id="textTyped"/>');

var mCaller  = "";
var prevText = "";
var http = getHTTPObject(); // We create the XMLHTTPRequest Object
function CustomerNamesResponse()
{
	document.getElementById("loading").style.visibility="hidden";
	try
	{
		if (http.readyState == 4) 
		{
			if (http.status == 200) 
			{
				results = http.responseText.split("~");
				var it;
				var listBox=document.getElementById('ListCustomerName');
				while(listBox.options.length>0)
				{
					listBox.options[0]=null;
				}
				for(it=0;it<results.length;it++)
				{
					listBox.options[it]=new Option(results[it],results[it]);
				}
				ListCN.style.visibility	=	"visible";
				ListCN.style.position 	=	"absolute";
				ListCN.style.top  		=	mCaller.offsetParent.offsetTop+mCaller.offsetParent.offsetHeight;
				ListCN.style.left 		= 	mCaller.offsetParent.offsetLeft;
				ListCN.style.Width		= 	mCaller.offsetParent.offsetWidth;
			} 
			else 				
			{
				alert ( "Not able to retrieve names" );
			}
		}	
	}catch(e){}
}

function getCustomerNames(callerId) {
    mCaller = document.getElementById(callerId);
	var len = mCaller.value.length;
	if(len>0 && prevText!=mCaller.value)
	{
		document.getElementById("loading").style.visibility="visible";
		http.open("POST","getCustomerNames.jsp?textTyped="+mCaller.value+"~", true);
		http.onreadystatechange = CustomerNamesResponse;
		http.send(null);
	}
	if(len==0)
	{
		document.getElementById('ListCustomerName').style.visibility="hidden";
	}
	else
	{
		document.getElementById('ListCustomerName').style.visibility="visible";
	}
}

function getHTTPObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
 	}
	return xmlHttp;
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
function EmpAllotState()//getresponse
{
	var arr = new Array(1000);
	try
	{
		if (http.readyState == 4) 
		{
			if (http.status == 200) 
			{
				results = http.responseText.split("~");
				alert("Employee already Alloted "+results[0]+" calls");
				if(parseInt(results[0])>=5)
				{
					//mCaller.
					alert("You Can not Allot calls to this engineer");
					mCaller.disabled=false;
					mCaller.options[0].selected=true;
				}
				mCaller.disabled=false;
			}
		}
	}catch(e){}
}
function getEmployeeAllotment(callerId,tBranch) //sendrequest
{
	
	mCaller=document.getElementById(callerId);
	var index	=	mCaller.selectedIndex;
	var empName	=	mCaller.options[index].text;
	mCaller.disabled=true;
    if(mCaller.value.length<1)
	{
		return;
	}
	try{
		//alert("Ajax_EmpAllotState.jsp?empName="+empName+"&tBranch="+tBranch);
		http.open("POST","Ajax_EmpAllotState.jsp?empName="+empName+"&tBranch="+tBranch, true);
		http.onreadystatechange = EmpAllotState;
		http.send(null);
	}catch(e){}
}
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
function Ajax_ReciveContact()
{
	try
	{
		if (http.readyState == 4) 
		{
			if (http.status == 200) 
			{
				results = http.responseText.split("~");
				document.getElementById("CalledBy").value=results[0];
				document.getElementById("ContactNo").value=results[1];
			}
		}
	}catch(e){}
}
function Ajax_FillContact(callerId,empId,customer)
{
	if(empId=null)
	{
		empId="";
	}
	if(customer.length>2)
	{
		customer=customer.replace("&","~");
	}
	var branch='main';
	try{
		branch=document.getElementById(callerId).value;
	}catch(e){}
		try{
		http.open("POST","Ajax_GetContact.jsp?custBranch="+branch+"&tCustId="+empId+"&customerName="+customer, true);		
		http.onreadystatechange = Ajax_ReciveContact;
		http.send(null);
	}catch(e){alert(e);}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function closeConnection() //sendrequest
{
	try{
		http.open("POST","Ajax_CloseConnection.jsp", true);
		http.send(null);
	}catch(e){alert("EXE");}
}
