﻿var tbSellerId;
var start = 0;
var finish = 10;
var totalCount = 0;
var totalPages = 1;

function getSeller(elementId)
{
	start = 0;
	finish = 10;
	tbSellerId = elementId;
	sellerNames = document.getElementById(tbSellerId);
	var divElement = document.getElementById('showResultSeller');
	if (sellerNames.value == "")
	{
		divElement.style.display = "none";
	}
	else
	{
		element = document.getElementById(tbSellerId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbSellerId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		
		WebService.GetSellerNamesWithRowCount(sellerNames.value,start, finish, OnCompleteSeller,OnErrorSeller, OnTimeOutSeller);
	}
}

function nextStepSeller()
{
	if (finish <= totalCount)
	{
		element = document.getElementById(tbSellerId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbSellerId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		var divElement = document.getElementById('showResultSeller');
		divElement.style.display = "none";
		start += 10;
		finish += 10;
		sellerNames = document.getElementById(tbSellerId);
		WebService.GetSellerNamesWithRowCount(sellerNames.value,start, finish, OnCompleteSeller,OnErrorSeller, OnTimeOutSeller);
	}
}

function previousStepSeller()
{
	if (start >= 10)
	{
		element = document.getElementById(tbSellerId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbSellerId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		var divElement = document.getElementById('showResultSeller');
		divElement.style.display = "none";
		start -= 10;
		finish -= 10;
		sellerNames = document.getElementById(tbSellerId);
		WebService.GetSellerNamesWithRowCount(sellerNames.value,start, finish, OnCompleteSeller,OnErrorSeller, OnTimeOutSeller);
	}
}

function PreviousPageClick()
{
}


function CalculateTotalPages()
{
	x = parseInt(totalCount/10); 
	pageCount = totalCount / 10;
	if (pageCount - x == 0)
		totalPages = x;
	else
		totalPages = x + 1;
}

function OnCompleteSeller(arg)
{
	element = document.getElementById(tbSellerId);
	if (arg != null)
		{
			if (arg.length != 0)
			{
				var divElement = document.getElementById('showResultSeller');
				divElement.innerHTML = "";
				totalCount = arg[arg.length-1];
				CalculateTotalPages();
				if (arg.length > 1)
				{
					var divList = document.createElement('div');
					var ulElement = document.createElement("ul");
					for(i=0;i<arg.length-1;i++)
					{
						var login = arg[i].split("#")[0];
						var login_id = arg[i].split("#")[1];
					
						var newText = document.createTextNode(login);
						var newNode = document.createElement("li");
						var newLink=document.createElement('a');
						newLink.setAttribute('href', 'javascript:setResultSeller("'+login.replace("'","#")+'",' + login_id + ')');
						newLink.appendChild(newText);
						newNode.appendChild(newLink);
						ulElement.appendChild(newNode);
					}
				divList.appendChild(ulElement);

				var newBr = document.createElement("br");
				newBr.setAttribute('clear', 'all');
				
				var divNavigation = document.createElement('div');
				divNavigation.style.width = "190px";
				
				var previousLink=document.createElement('a');
				previousLink.setAttribute('href', 'javascript:previousStepSeller()');
				previousLink.setAttribute('id', 'previousLink');
				var previousImage = document.createElement('img');
				previousImage.setAttribute('name','imgPreviousPage');
				previousImage.setAttribute('src','../../../assets/images/template/arrow_left.gif');
				previousImage.style.styleFloat = "left";
				previousLink.appendChild(previousImage);
				divNavigation.appendChild(previousLink);
				
				var	textNode = document.createElement('span');
				textNode.innerHTML = ' '+(finish/10) + ' of ' +  totalPages + ' Pages';
				textNode.style.styleFloat = "left";
				divNavigation.appendChild(textNode);
		
				var nextLink=document.createElement('a');
				nextLink.setAttribute('href', 'javascript:nextStepSeller()');
				nextLink.setAttribute('id', 'nextLink');
				var nextImage = document.createElement('img');
				nextImage.setAttribute('name','imgPreviousPage');
				nextImage.setAttribute('src','../../../assets/images/template/arrow_right.gif');
				nextLink.appendChild(nextImage);
				divNavigation.appendChild(nextLink);
				
				divElement.appendChild(divList);
				divElement.appendChild(newBr);
				divElement.appendChild(divNavigation);
				
				if (element.value == "")
				{
					divElement.style.display = "none";
				}
				else
				{
					divElement.style.display = "block";
				}
				
				
				coordinates = getAnchorPosition(tbSellerId);
				divElement.style.top=coordinates.y+element.offsetHeight+"px";
				divElement.style.left=coordinates.x+"px";
				divElement.style.position="absolute";
				element.focus();
			}
		}
		
	}
	var wheelElement = document.getElementById('wheelDiv');
	wheelElement.style.display = "none";
}

function OnErrorSeller(arg)
{
}

function OnTimeOutSeller(arg)
{
}

function setResultSeller(selectedValue, loginId)
{
	var divElement = document.getElementById('showResultSeller');
	divElement.style.display = "none";
	element = document.getElementById(tbSellerId);
	element.value = selectedValue.replace("#","'");
}

function onFocusSellerOutControl()
{	
	document.getElementById('showResultSeller').style.display = 'none';
}

