﻿function getChildCategory(object)
{
	if(!ready) return;
	$get('hidCurrentIndex').value = 0;
	$get('hidSlide').value = 1;
	
	var productStorage = '';
	var categoryStorage = '';
	
	$get('imgLoading').style.display = "block";
	
	setCategoryToBePopulated(object);
	setCategoryTypeForProduct(object);
	doTrackWebtrendsURL('category',object.innerHTML);
	PageMethods.GetChildCategory(object.id,$get('CategoryTypeForProduct').value,getChildCategorySuccess,failed);
	
	PageMethods.GetProductsByCategoryIdWithType(object.id,$get('CategoryTypeForProduct').value,getProductsByCategoryIdSuccess,failed);
	populateBtnImage(object);
}

function populateBtnImage(object)
{
	if($get('CategoryTypeForProduct').value == 'category'){
		$get('imgSecondNavBtn').src = ("TitleImage.aspx?imageName=" + object.innerHTML).replace("&amp;","mingjindion");
	}
	else if($get('CategoryTypeForProduct').value == 'subCategory'){
		$get('imgThirdNavBtn').src = ("TitleImage.aspx?imageName=" + object.innerHTML).replace("&amp;","mingjindion");
	}
	$get('imgFourthNavBtn').src = ("TitleImage.aspx?imageName=" + object.innerHTML).replace("&amp;","mingjindion");
}

function setCategoryTypeForProduct(object)
{
	var ele = object.parentNode;
	while(true)
	{	
		if(ele.id != null)
		{
			if(ele.id == 'categoriesFirstNav')
			{
				$get('CategoryTypeForProduct').value = 'category';
				return;
			}
			else if(ele.id == 'categoriesSecondNav')
			{
				$get('CategoryTypeForProduct').value = 'subCategory';
				return;
			}
			else if(ele.id == 'categoriesThirdNav')
			{
				$get('CategoryTypeForProduct').value = 'subsubCategory';
				return;
			}
		}
		ele = ele.parentNode;
	}
}

function doTrackWebtrendsURL(category,section)
{
	dcsMultiTrack('DCS.dcsuri', '/browse.aspx', 'WT.ti', '+' + category + '+', 'WT.cg_n',section);
}

function setCategoryToBePopulated(object)
{
	var ele = object.parentNode;
	
	while(true)
	{	
		if(ele.id != null)
		{
			if(ele.id == 'categoriesFirstNav')
			{
				$get('CategoryToBePopulated').value = 'categoriesSecondNavList';
				return;
			}
			else if(ele.id == 'categoriesSecondNav')
			{
				$get('CategoryToBePopulated').value = 'categoriesThirdNavList';
				return;
			}
			else if(ele.id == 'categoriesThirdNav')
			{
				$get('CategoryToBePopulated').value = '';
				return;
			}
		}
		ele = ele.parentNode;
	}
}

var test;

function getChildCategorySuccess(response)
{
	var childCategory = response;	
	   
	
	removeChildNodes($get($get('CategoryToBePopulated').value));
	
	if(childCategory.length == 0)	{
		return;
	}
	
	
	var result = '<ul>';
	for(var i = 0;;i++)
	{
		var firstItemStyle = '';
		
		if(i==0)
			firstItemStyle = " class='paddingTop5' ";
			 
		if(childCategory[i] == null)
		{
			break;
		}
		else
		{

			result += "<li "+firstItemStyle+"><a href='javascript:void(0);' onclick='getChildCategory(this);return false;' id='"+childCategory[i]["ID"]+"'>"+childCategory[i]["Name"] + "</a></li>";				
		}
	}
	
	result +="</ul>";
	
	var container = document.createElement('div');
	container.className = 'cateContainer' + childCategory[0]["ParentCategoryID"];
	container.innerHTML = result;
	
	$get('imgLoading').style.display = "none";
	
	if($get('CategoryToBePopulated').value.length != 0)
	{
		$get($get('CategoryToBePopulated').value).appendChild(container);
	}
}

function getProductsByCategoryIdSuccess(response)
{
 
	var products = response;
	var resultProduct = '';
	var currentIndex = parseInt($get('hidCurrentIndex').value);
	
	var productContainer = document.createElement('div');
	
	if(products.length > 0)
		productContainer.className = 'proContainer' + products[0]["CategoryID"];	
	
	var i = currentIndex * 10;
	
	removeChildNodes($get('divProductList'));
	
	for(;;i++)
	{		
		if(products[i] == null)
		{			
			break;		
		}
		else
		{				
			var templateProduct = $get('templateProduct').cloneNode(true);			
			
			templateProduct.style.display = 'block';
			templateProduct.id = "";
			var productName = '';
			
			if(products[i]['Brand']['Name'] != null && products[i]['Brand']['Name'] != "")
			{
				productName += products[i]['Brand']['Name'] + '&nbsp;';
				if(products[i]['PSymbol']['Character'] != null && products[i]['PSymbol']['Character'] != "")
				{				
					productName += products[i]['PSymbol']['Character'] + '&nbsp;';
				}
			}
			
			productName += products[i]['Name'];
			
			if(products[i]['Unit'] != null && products[i]['Unit'] != "")
			{
				productName += '&nbsp;' + products[i]['Unit'];
			}
			
			getElementsByClassName('productName','span',templateProduct)[0].innerHTML = productName;
			getElementsByClassName('productPrice','span',templateProduct)[0].innerHTML = formatCurrency(products[i]['TotalPrice']);
			if(products[i]['Quantity'] != null){
				getElementsByClassName('Quantity','span',templateProduct)[0].innerHTML = products[i]['Quantity'];
			}
			if(products[i]['OldPrice'] != 0){
				getElementsByClassName('OldPrice','span',templateProduct)[0].innerHTML = 'was '+ formatCurrency(products[i]['OldPrice']);
			}
			if(products[i]['UnitPrice'] != null && products[i]['UnitPrice'] != ""){
				getElementsByClassName('UnitPrice','span',templateProduct)[0].innerHTML = "(" + (products[i]['UnitPrice']) + ")";
			}
			
			getElementsByClassName('price','input',templateProduct)[0].value = (products[i]['TotalPrice']);
			getElementsByClassName('productNumber','input',templateProduct)[0].value = products[i]['Number'];			
			
			if($get('divProductList').childNodes.length < 10)
			{
				$get('divProductList').appendChild(templateProduct);						
			}				
			else
			{
				break;
			}
		}
	}
	
	if(products.length > 10)
	{	
		$get('Paging').style.display = 'block';		
		if($get('hidSlide').value == '1')
			GenePaging(products[0]["CategoryID"],products.length);
	}
	else
	{
		$get('Paging').style.display = 'none';
	}
	
	if($get('hidSlide').value == '1')
		categoriesSlide();	
	
	$get('imgLoading').style.display = "none";
	return;
}

function GenePaging(cateId,length)
{
	var maxPage = parseInt((length-1) / 10)+1;
	var pagingHTML = '';
	for(var i = 0; i < maxPage; i++)
	{
		if(i == 0)		
			pagingHTML += '<a href="javascript:void(0);" class="selected" onclick="Paging('+cateId+','+i+',this);return false;">'+ (i+1) +'</a>&nbsp;';	
		else
			pagingHTML += '<a href="javascript:void(0);" class="paging" onclick="Paging('+cateId+','+i+',this);return false;">'+ (i+1) +'</a>&nbsp;';	
	}
	

	$get('Paging').innerHTML = 'page ' + pagingHTML;
	
}

function Paging(cateId,currentPage,object)
{
	var alinks = object.parentNode.getElementsByTagName('a');
	for(var i = 0;i < alinks.length; i++)
	{
		alinks[i].className = "paging";
	}
	object.className = "selected";
	$get('hidSlide').value = 0;
	$get('hidCurrentIndex').value = currentPage;
	PageMethods.GetProductsByCategoryIdWithType(cateId,$get('CategoryTypeForProduct').value,getProductsByCategoryIdSuccess,failed);
}

function failed(response)
{
	alert('failed:'+ response._message);
}

function Qty(object,mode)
{
	var parentNode = object.parentNode;

	while(parentNode.tagName != 'TR' )
	{
		parentNode = parentNode.parentNode;
	}

	var txtQty = getElementsByClassName('txtQty','input',parentNode)[0];
	var hidPrice = getElementsByClassName('price','input',parentNode)[0];
	var lblTotalPrice = getElementsByClassName('totalPrice','span',parentNode)[0];
	var qty = 1;
	
	if(isNaN(txtQty.value) && txtQty.value.length != 0){
		txtQty.value = 1;
		alert('Please enter a number in qty field.');
		txtQty.focus();
		return;
	}
	
	if(txtQty.value.length == 0)
	{
		qty = 1;
		return;
	}
	
	
	if(mode == 'add')
	{
		txtQty.value = parseInt(txtQty.value) + 1;
	}
	else if(mode == 'reduce' && parseInt(txtQty.value) > 1)
	{
		txtQty.value = parseInt(txtQty.value) - 1;
	}
	else if(mode == 'update' && parseInt(txtQty.value) > 0)
	{
		txtQty.value = parseInt(txtQty.value);
	}
	
	if(!lblTotalPrice)
		return;
	
	qty = parseInt(txtQty.value);
	
	lblTotalPrice.innerHTML = (qty * parseFloat(hidPrice.value)).toFixed(2);
}

function addProduct(object)
{
	var parentNode = object.parentNode;
	
	while(parentNode.tagName != 'TR' )
	{
		parentNode = parentNode.parentNode;
	}
	
	var txtQty = getElementsByClassName('txtQty','input',parentNode)[0];
	var productNumber = getElementsByClassName('productNumber','input',parentNode)[0];
	var hidPrice = getElementsByClassName('price','input',parentNode)[0];
	
	if(txtQty.value == 0 || txtQty.value.length == 0)
	{
		alert('Please put at least 1 in quantity field');
		txtQty.focus();
		return;
	}
	
	hidTotalItem.value = parseInt(hidTotalItem.value) + parseInt(txtQty.value);
	var TwoDecimalsOnly = parseInt(Math.round(parseInt(txtQty.value) * parseFloat(hidPrice.value)* 100))/100;
	//alert(Math.round(parseInt(txtQty.value) * parseFloat(hidPrice.value) * 100));
	//alert('TwoDecimalsOnly'+TwoDecimalsOnly);
	hidMyTotalPrice.value = parseInt(Math.round((parseFloat(hidMyTotalPrice.value) + TwoDecimalsOnly)*100))/100;
	
		   
	$get('totalItems').innerHTML = parseInt(hidTotalItem.value) == 1? hidTotalItem.value + " item":hidTotalItem.value+" items";
	if(hidMyTotalPrice.value.indexOf('.') > -1)
		$get('TotalPrice').innerHTML = hidMyTotalPrice.value.substring(0,hidMyTotalPrice.value.indexOf('.'));
	else
		$get('TotalPrice').innerHTML = hidMyTotalPrice.value;
	var hidMyTotalPrice_value = formatCurrency(hidMyTotalPrice.value);
	$get('TotalPriceDecimal').innerHTML = hidMyTotalPrice_value.substring(hidMyTotalPrice_value.indexOf('.')+1);
	var hidName = getElementsByClassName('productName','span',parentNode)[0];
	
	doTrackWebtrendsURL('category',hidName.innerHTML);
	PageMethods.AddProduct(productNumber.value,txtQty.value,addProductSuccess,failed);
}

function addProductSuccess(response)
{
	test = response;
}

function categoriesSlide()
{
	if($get('CategoryToBePopulated').value == 'categoriesSecondNavList')
	{
		categoryOnClick();
	}
	else if($get('CategoryToBePopulated').value == 'categoriesThirdNavList')
	{
		subcategoryOnClick();
	}
	else if($get('CategoryToBePopulated').value == '')
	{
		subsubcategoryOnClick();
	}
}


function removeChildNodes(ctrl)
{
	while (ctrl && ctrl.childNodes.length > 0 && ctrl.childNodes[0])
	{
		ctrl.removeChild(ctrl.childNodes[0]);
	}
}

