var BrowserIsIE = true;

function articlePrintCurrentPage(totalPages, intPage)
{		
	var currPage = 1;
	
	for (var i = 1; i <= totalPages; i++)
	{
		try
		{
			var Elem = document.getElementById('tbArticlesPage' + i);
			
			if (Elem.style.display == "block")
				currPage = i;
		}
		catch(ex)
		{
		}
	}
	
	if (currPage != intPage)
	{
		var pastElem = document.getElementById('tbArticlesPage' + currPage);
		var Elem = document.getElementById('tbArticlesPage' + intPage);
		
		var pastElemImg = document.getElementById('imgCurrPage' + currPage);
		var ElemImg = document.getElementById('imgCurrPage' + intPage);
		var pastElemImg2 = document.getElementById('imgCurrPage2' + currPage);
		var ElemImg2 = document.getElementById('imgCurrPage2' + intPage);

		pastElem.style.display = "none";
		Elem.style.display = "block";
		
		pastElemImg.style.display = "none";
		ElemImg.style.display = "block";
		
		pastElemImg2.style.display = "none";
		ElemImg2.style.display = "block";
	}
}

function articlesSelectOrderBy(orderby, sortby)
{
	BrowserIsIE = isBrowserIE();

	var direction
		direction = changeSelection(orderby);
	
	var arrArticles = new Array();
	var arrArticlesSorted = new Array();
	var arrArticlesSortedIndexes = new Array();

	arrArticles = getArticles(sortby);
	arrArticlesSorted = copyArray(arrArticles);
	sortArticles(arrArticlesSortedIndexes, arrArticlesSorted, direction, sortby);

	generateArticles(arrArticles, arrArticlesSorted, arrArticlesSortedIndexes);
}

function changeSelection(orderby)
{
	direction = 0;

	try
	{
		var elemTitle = document.getElementById('orderByTitle');
		var elemStock = document.getElementById('orderByStock');
		var elemPrice = document.getElementById('orderByPrice');
		
		var elemSelected = document.getElementById(orderby);
		var elemPicture = document.getElementById('IMG' + orderby);

		if (elemSelected.className == "SearchSelectedOrder")
		{
			if (elemPicture.src == shopURL + "/Images/" + shopPREFIX + "_ArrowAscending.gif")
			{
				direction = 0;
				elemPicture.src = shopURL + "/Images/" + shopPREFIX + "_ArrowDescending.gif"
			}
			else
			{
				direction = 1;
				elemPicture.src = shopURL + "/Images/" + shopPREFIX + "_ArrowAscending.gif"
			}	
		}
		else
		{
			if (elemPicture.src == shopURL + "/Images/" + shopPREFIX + "_ArrowAscending.gif")
			{
				direction = 1;
			}
			else
			{
				direction = 0;
			}
		}
		
		if (orderby == 'orderByTitle')
		{
			if (direction == 0) direction = 1;
			else direction = 0;
		}
		
		elemTitle.className = 'none';
		elemStock.className = 'none';
		elemPrice.className = 'none';

		elemSelected.className = 'SearchSelectedOrder';
	}
	catch(ex)
	{
		return 0;
	}
	
	return direction;
}

function getArticles(orderby)
{
	var i = 1;	
	var arrArticles = new Array();


/*
	var elemPage = document.getElementById('tbArticlesPage1' + i)
	while (elem != null)
	{
		try
		{
			for(var j = 0; j < elemPage.childNodes.length; j++)
			{
				with(elemPage.childNodes[j])
				{
					var elem = getPosition(dragConts[i].childNodes[j]);
					
					var	elem = elemPage.getElementById('article' + orderby + i);
			
						if (orderby == "Price")
						{
							arrArticles[i - 1] = parseFloat(strReplace(elem.value, ",", "."));
						}
						else if (orderby == "Stock")
						{
							arrArticles[i - 1] = parseFloat(elem.value);
						}
						else
						{
							arrArticles[i - 1] = elem.value;
						}
				}
			}
		}
		catch(ex)
		{
		}
	}
*/

	var elem = document.getElementById('article' + orderby + i)

	while (elem != null)
	{
		try
		{
			elem = document.getElementById('article' + orderby + i);
		
			if (orderby == "Price")
			{
				arrArticles[i - 1] = parseFloat(strReplace(elem.value, ",", "."));
			}
			else if (orderby == "Stock")
			{
				arrArticles[i - 1] = parseFloat(elem.value);
			}
			else
			{
				arrArticles[i - 1] = elem.value;
			}
		}
		catch(ex)
		{
			break;
		}
		i++;
	}

	return arrArticles;
}

function sortArticles(arrItemsIndex, arrItems, direction, sortby)
{
	for (var i = 0; i < arrItems.length; i++)
	{
		arrItemsIndex[i] = i;
	}


	if (direction == 0)
	{
		for (var i = 0; i < arrItems.length; i++)
		{
			for (var j = i; j < arrItems.length; j++)
			{
				if (arrItems[j] > arrItems[i])
				{
					var tmp = arrItems[i];
					var tmpIndex = arrItemsIndex[i];
					
					arrItems[i] = arrItems[j];
					arrItems[j] = tmp;
					
					arrItemsIndex[i] = arrItemsIndex[j];
					arrItemsIndex[j] = tmpIndex;
				}
			}
		}
	}
	else
	{
		for (var i = 0; i < arrItems.length; i++)
		{
			for (var j = i; j < arrItems.length; j++)
			{
				if (arrItems[j] < arrItems[i])
				{
					var tmp = arrItems[i];
					var tmpIndex = arrItemsIndex[i];
					
					arrItems[i] = arrItems[j];
					arrItems[j] = tmp;
					
					arrItemsIndex[i] = arrItemsIndex[j];
					arrItemsIndex[j] = tmpIndex;
				}
			}
		}
	}
}

function copyArray(arrItems)
{
	var arrTmp = new Array();
	
	for (var i = 0; i < arrItems.length; i++)
	{
		arrTmp[i] = arrItems[i];
	}
	
	return arrTmp;
}

function generateArticles(arrItems, arrItemsSorted, arrItemsSortedIndexes)
{
	var arrNewArticles = new Array();
	var arrNewArticlesTitle = new Array();
	var arrNewArticlesStock = new Array();
	var arrNewArticlesPrice = new Array();
	
	
	for (var i = 0; i < arrItems.length; i++)
	{
		var replaceElemIndex = arrItemsSortedIndexes[i] + 1;
		var replaceElem = document.getElementById("divArticle" + replaceElemIndex);	
		var replaceElemHTML = replaceElem.innerHTML;
		
		arrNewArticlesTitle[i] = getArticleAttributeValue(replaceElemHTML, "articleTitle");
		arrNewArticlesStock[i] = getArticleAttributeValue(replaceElemHTML, "articleStock");
		arrNewArticlesPrice[i] = getArticleAttributeValue(replaceElemHTML, "articlePrice");

		arrNewArticles[i] = replaceElemHTML;
	}
	
	for (var i = 0; i < arrItems.length; i++)
	{
		var elemIndex = i + 1;
		var elem = document.getElementById("divArticle" + elemIndex);	
		
			arrNewArticles[i] = setArticleAttributeValue(arrNewArticles[i], "articleTitle", arrNewArticlesTitle[i]);		
			arrNewArticles[i] = renameArticleAttribute(arrNewArticles[i], "articleTitle", "articleTitle" + elemIndex);
			arrNewArticles[i] = setArticleNameValue(arrNewArticles[i], "articleTitle", "articleTitle" + elemIndex)

			arrNewArticles[i] = setArticleAttributeValue(arrNewArticles[i], "articleStock", arrNewArticlesStock[i]);
			arrNewArticles[i] = renameArticleAttribute(arrNewArticles[i], "articleStock", "articleStock" + elemIndex);
			arrNewArticles[i] = setArticleNameValue(arrNewArticles[i], "articleStock", "articleStock" + elemIndex)
			
			arrNewArticles[i] = setArticleAttributeValue(arrNewArticles[i], "articlePrice", arrNewArticlesPrice[i]);
			arrNewArticles[i] = renameArticleAttribute(arrNewArticles[i], "articlePrice", "articlePrice" + elemIndex);
			arrNewArticles[i] = setArticleNameValue(arrNewArticles[i], "articlePrice", "articlePrice" + elemIndex)

			elem.innerHTML = arrNewArticles[i];
			elem.id = "divArticle" + elemIndex;
	}
}

function renameArticleAttribute(body, attribute, newVal)
{
	if (BrowserIsIE)
	{
		var from = body.indexOf(attribute, 0);
		var to =   body.indexOf(' ', from - 1);
		var attribute = body.substr(from, (to - from));
			
			body = strReplace(body, attribute, newVal);
	}
	else
	{
		var from = body.indexOf(attribute, 0);
		var to =   body.indexOf('"', from - 1);
		var attribute = body.substr(from, (to - from));
			
			body = strReplace(body, attribute, newVal);
	}

	return body;
}

function setArticleNameValue(body, attribute, newVal)
{
	if (BrowserIsIE)
	{
		var from = body.indexOf(attribute, 0);
			from = body.indexOf("name=", from) + 5;
		var to =   body.indexOf('>', from);
		var attribute = body.substr(from, (to - from));

			body = strReplace(body, attribute, newVal);
	}
	else
	{
		var from = body.indexOf(attribute, 0);
			from = body.indexOf("name=", from) + 6;
		var to =   body.indexOf('"', from);
		var attribute = body.substr(from, (to - from));

			body = strReplace(body, attribute, newVal);
	}
	
	return body;
}

function setArticleAttributeValue(body, attribute, newVal)
{
	var tmp_attribute = getArticleAttributeValue(body, attribute);
		
		body = strReplace(body, tmp_attribute, newVal);

	return body;
}

function getArticleAttributeValue(body, attribute)
{
	var body = body;

	if (BrowserIsIE)
	{
		if (attribute == "articleTitle")
		{
			var from = body.indexOf(attribute, 0);
				from = body.indexOf("value=", from) + 7;
			var to =   body.indexOf('"', from);
			var attribute = body.substr(from, (to - from));
		}
		else
		{
			var from = body.indexOf(attribute, 0);
				from = body.indexOf("value=", from) + 6;
			var to =   body.indexOf(" ", from);
			var attribute = body.substr(from, (to - from));
		}
	}
	else
	{
		if (attribute == "articleTitle")
		{
			var from = body.indexOf(attribute, 0);
				from = body.indexOf("value=", from) + 7;
			var to =   body.indexOf('"', from);
			var attribute = body.substr(from, (to - from));
		}
		else
		{
			var from = body.indexOf(attribute, 0);
				from = body.indexOf("value=", from) + 7;
			var to =   body.indexOf('"', from);
			var attribute = body.substr(from, (to - from));
		}
	}

	return attribute;
}


function strReplace(sString, sReplaceThis, sWithThis) {
	if (sReplaceThis != "" && sReplaceThis != sWithThis) {
		var counter = 0;
		var start = 0;
		var before = "";
		var after = "";
		
		while (counter<sString.length) {
			start = sString.indexOf(sReplaceThis, counter);
			
			if (start == -1) {
				break;
			} else {
				before = sString.substr(0, start);
				after = sString.substr(start + sReplaceThis.length, sString.length);
				sString = before + sWithThis + after;
				counter = before.length + sWithThis.length;
			}
		}
	}
	
	return sString;
}

function isBrowserIE()
{
	var ua = window.navigator.userAgent
    var msie = ua.indexOf ( "MSIE " )

    if ( msie > 0 )
        return true;
    else
        return false;
}