function capWords(inputString) {
	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = inputString.toLowerCase();
	stringLen = tmpStr.length;
	if (stringLen > 0)
	{
	  for (i = 0; i < stringLen; i++)
	  {
	    if (i == 0)
		{
	      tmpChar = tmpStr.substring(0,1).toUpperCase();
	      postString = tmpStr.substring(1,stringLen);
	      tmpStr = tmpChar + postString;
	    }
	    else
		{
	      tmpChar = tmpStr.substring(i,i+1);
	      if (tmpChar == " " && i < (stringLen-1))
		  {
	      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
	      preString = tmpStr.substring(0,i+1);
	      postString = tmpStr.substring(i+2,stringLen);
	      tmpStr = preString + tmpChar + postString;
	      }
	    }
	  }
	}
	return tmpStr;
}

//---------- FILTER OBJECT FOR HANDLING FILTER HISTORY ------------
var SC_Filter = function (){
	this.fId;
	this.fName = "";
	this.fParameters = new Array();
	
	_SC_Filter_prototype_call = true;
	SC_Filter.prototype.generateRemove = generateRemove;
	SC_Filter.prototype.isEndRange = isEndRange;
	SC_Filter.prototype.to_sop = to_sop;
	function generateRemove(){
		var str = "<a href='javascript:removeFilter("+this.fId+");' >";
		if ( this.fName == "SortBy"){
			str +="Sorted by " + this.fParameters[0];
		} else if ( this.fName == "SortByAttribValue" ){
			str +=this.fParameters[1];
		} else if ( this.fName == "SortByAttribRange") {
			if ( this.isEndRange(this.fParameters[1],this.fParameters[2])==false){
				str +="Sorted by " + this.fParameters[0] + " from " + this.fParameters[1] + " to " + this.fParameters[2];
			} else {
				str +="Sorted by " + this.fParameters[0] + " more than "+this.fParameters[1];
			}
		}
		str +="</a>";
		return str;
	}
	
	function to_sop(){
		return this.fId+";"+this.fName+";"+this.fParameters+";"+this.fParameters.length+";"+this.generateRemove();
	}
	
	function isEndRange(num1, num2){
		if ( (num2/num1) >= 100) return true;
		return false;
	}
}

var ItemController = function ()
{
  // CONFIGURATIONS
  //Items
  this.itemEntityTagName= "itemEntity"; // TAG
  this.itemDivToShow= "divToShow-"; // DIV
  this.showItemsTable = "showItemsTable"; //TABLE
  this.itemAttributeTagName= "input"; // INPUT
  this.itemAttributeName = "name"; // TAG attribute
  this.itemAttributeValue = "value"; // TAG attribute
  this.itemAttributeUniqueValues = "uniqueValues"; // TAG attribute
  
  //Attribute headers
  this.attributeHeaderName = "SC_AttributeHeader";
  
  //Pagination
  this.itemSortFromIndex = "SC_itemSortFromIndex"; // SPAN
  this.itemSortTillIndex = "SC_itemSortTillIndex"; // SPAN
  this.itemSortTotalCount = "SC_itemSortTotalCount"; // SPAN
  this.itemSortCurrentPage = "SC_itemSortCurrentPage"; // INPUT
  this.itemSortMaxPerPage = "SC_itemSortMaxPerPage"; //INPUT
  this.itemSortMaxPerPageDefault = "SC_itemSortMaxPerPageDefault"; //INPUT
  this.itemSortPaginationLinks = "SC_itemSortPaginationLinks"; // SPAN
  this.itemSortPaginationDiv = "SC_Pagination"; // DIV
  this.itemSortNextPage = "SC_itemSortNextPage"; // A
  this.itemSortPrevPage = "SC_itemSortPrevPage"; // A

  //Pagination limits
  this.useNetSuiteLimit = true;
  this.netSuiteMaxItemLimit = 50;
  this.reportMsgs = (document.URL.indexOf("warnings=true") != -1);
  
  //Filters
  this.sortByAttributeField = "";
  
  //Pagination extra buttons
  this.itemSortViewAll = "SC_viewAll"; // A
  this.itemSortViewPagination = "SC_viewPagination"; // A
  this.itemsByRow = "itemsByRow";

  //Sorting controls
  this.itemSortBy = "SC_itemSortBy";
  this.itemSortBySelect = "SC_itemSortBySelect";
  this.filterHistoryDiv = "SC_filterHistoryDiv";
  
   
  //CLASS VARIABLES FOR INFORMATION CONTAINING
  //-----------------------------------------
  this.items = new Array(); //It contains every item
  this.filteredItems = new Array(); //It contains only the items that are currently showing
  
  this.attributeHeaders = new Array();
  this.currentFilters = new Array();
  this.nextFilterId = 1;
  // Here comes the variables for the HTML controls.
  
  //-----------------------------------------
  
  
  // initialize the member function references 
  // for the class prototype
	_ItemController_prototype_called = true;
	ItemController.prototype.loadData = loadData;
	ItemController.prototype.catchEveryAttributeHeader = catchEveryAttributeHeader;
	ItemController.prototype.catchEveryItem = catchEveryItem;
	ItemController.prototype.addSC_Item = addSC_Item;
	
	ItemController.prototype.addItemIdToShow = addItemIdToShow;
	ItemController.prototype.getItemById = getItemById;
	ItemController.prototype.showFilteredItems = showFilteredItems;
	ItemController.prototype.sortByAttribute = sortByAttribute;
	ItemController.prototype.clearShowTable = clearShowTable;
	ItemController.prototype.sortByAttributeValue = sortByAttributeValue;
	ItemController.prototype.sortByAttributeRange = sortByAttributeRange;
	ItemController.prototype.resetFilters = resetFilters;
	ItemController.prototype.reloadRangesSorts = reloadRangesSorts;
	ItemController.prototype.showFilteredItemsSetCP = showFilteredItemsSetCP;
	ItemController.prototype.makePagination = makePagination;
	ItemController.prototype.applyFilters = applyFilters;
	ItemController.prototype.removeFilter = removeFilter;
	ItemController.prototype.addFilter = addFilter;
	ItemController.prototype.listEveryItem = listEveryItem;
 
	//----------------LOAD-------------------
	function loadData(){
		this.catchEveryItem();
		this.catchEveryAttributeHeader();
		this.showFilteredItems();
	}

  // Getting every item in the this.items array
  function catchEveryItem()
  {
  	var itemTags = document.getElementsByTagName('div');
	var itemDiv = null;
	var item = null;
	var attributesTags = null;
	var attributeInput = null;
	
	for ( var i = 0; i<itemTags.length;i++){
		itemDiv = itemTags[i];
		if ( itemDiv.title == this.itemEntityTagName){ 
			attributesTags = itemDiv.getElementsByTagName(this.itemAttributeTagName);
			item = new SC_Item(itemDiv.id);
			item.toStringAttrib = 'Web Title';
			for (var x=0;x<attributesTags.length;x++){
				attributeInput = attributesTags[x];
				item.addAttribute((item.getId()+x),attributeInput.name, attributeInput.value, attributeInput.getAttribute('uniqueValues'));
			}//End attributes rolling
			this.addSC_Item(item);
			this.addItemIdToShow(item);
		}//End if itemDiv.name == itemEntityTagName
	}//End itemDivs rolling
	if (this.reportMsgs==true){ alert(this.items.length + " Items where found and stored."); }
  }
  
  // Getting every diferent attribute names
  function addSC_Item(scItem)
  {//Checking if the item alredy exists.
	for (var i=0;i<this.items.length;i++){
		var item = this.items[i];
		if (item.getId() == scItem.getId()){
			return 0;
		}
	}
	this.items[this.items.length] = scItem;
	return 1;
  }
  
  function getItemById(itemId){
  	for (var i=0;i<this.items.length;i++){
  		if ( this.items[i].getId() == itemId){
  			return this.items[i];
  		}
  	}
  	return 0;
  }
  
  //---------------------------------------------------------------
  //---------------      ATRIBUTE HEADERS      --------------------
  function catchEveryAttributeHeader(){
  	var tags = document.getElementsByTagName('span');
  	var span = null;
  	var ah = null;
  	for (var i=0;i<tags.length;i++){
  		if ( tags[i].title == this.attributeHeaderName){
  			span = tags[i];
  			ah = new SC_AttributeHeader(span.id.split('-')[0]);
  			ah.scLoad(this.items,span.innerHTML);
  			
  			ah.createTags();
  			if (this.reportMsgs==true) alert("Create tags hecho");
  			this.attributeHeaders[this.attributeHeaders.length] = ah;  			
  		}
  	}
  }
  
  //------------------------ITEMS TO SHOW--------------------------
  function addItemIdToShow(item)
  {//Checking if the item alredy exists.
	this.filteredItems[this.filteredItems.length] = item;
	return 1;
  }
  
  function showFilteredItems(){
  	try{
	  	this.clearShowTable();
	  	var paginationDiv = document.getElementById(this.itemSortPaginationDiv);
	  	var itemsToShow = this.filteredItems;
	  	if (paginationDiv != null){
	  		itemsToShow = this.makePagination(itemsToShow);
	  	}
	  	var tbl = document.getElementById(this.showItemsTable);
	  	tbl.style.display="block";
	  	var byRow = parseInt(document.getElementById('itemsByRow').innerHTML);
	  	var rowCount = 0;
	  	for ( var i=0;i<itemsToShow.length;i++){
	  		
	  		var isPagedItem = false;
	  		var oTd = document.createElement('TD');
	  		var internalId=itemsToShow[i].getId();
	  		var idiv = document.getElementById(this.itemDivToShow+internalId);
	  		
	  		if ( idiv == null){
	  			if ( internalId.indexOf('pItem|') != -1){
	  				if (this.reportMsgs == true) alert("The item that will be added now is a pagination item");
	  				idiv = "<span>" + itemsToShow[i].getAttributeByName('divToShow').getValue().toLowerCase()+"</span>";
	  				isPagedItem = true;
	  			}
	  		}
	  		var x=0;
			if ((i % byRow)==0){
				if ( tbl.rows[rowCount] == null){
					oRow = document.createElement('TR');	
				} else {
					
					oRow = tbl.rows[rowCount];
				}
				rowCount = rowCount +1;
			}
			if ( isPagedItem == false ) {
				oTd.innerHTML = idiv.innerHTML;
			} else {
				oTd.innerHTML = idiv;
				isPagedItem = false;
			}
			oRow.appendChild(oTd);
			tbl.appendChild(oRow);
	  	}
	 	this.reloadRangesSorts();
	 	//ONLY FOR GOLDEN
	 	callCellJS();
	 	//---------------
	 } catch (e) {
	 	if ( this.reportMsgs == true ) alert (e);		
 	}
  }
  
  function showFilteredItemsSetCP(currentPage){
  	document.getElementById(this.itemSortCurrentPage).value = currentPage;
  	this.showFilteredItems();
  }
  
  function makePagination(itemsToShow){
  	var fromIndex = document.getElementById(this.itemSortFromIndex);
  	var fromIndex2 = document.getElementById(this.itemSortFromIndex+"-2");
	var tillIndex = document.getElementById(this.itemSortTillIndex);
	var tillIndex2 = document.getElementById(this.itemSortTillIndex+"-2");
	var totalCount = itemsToShow.length;

    document.getElementById(this.itemSortTotalCount).innerHTML = totalCount;
	document.getElementById(this.itemSortTotalCount+"-2").innerHTML = totalCount;
	
	var currentPage = parseInt(document.getElementById(this.itemSortCurrentPage).value);
	var currentPage2 = parseInt(document.getElementById(this.itemSortCurrentPage+"-2").value);
	var maxPerPage = parseInt(document.getElementById(this.itemSortMaxPerPage).value);
	var maxPerPage2 = parseInt(document.getElementById(this.itemSortMaxPerPage+"-2").value);
	
	var startIndex = ((currentPage * maxPerPage)-maxPerPage);
	fromIndex.innerHTML = startIndex+1;
	fromIndex2.innerHTML = startIndex+1;
	var endIndex = startIndex + maxPerPage;
	var itemsForPage = new Array();
	var i= 0;
	for ( i = startIndex; i<endIndex && i<itemsToShow.length;i++){
		itemsForPage[itemsForPage.length] = itemsToShow[i];
	}
	tillIndex.innerHTML = i;
	tillIndex2.innerHTML = i;
	itemsToShow = itemsForPage; // dejo en los items para mostrar solo el de la pagina
	
	//-------------------------------
	//SC_itemSortPaginationLinks
	//---Making pagination Links-----
	var pagLinksDiv = document.getElementById(this.itemSortPaginationLinks);
	var pagLinksDiv2 = document.getElementById(this.itemSortPaginationLinks+"-2");
	
	
	var maxPages = (totalCount / maxPerPage);
	var decimales = (parseFloat(maxPages) - parseInt(maxPages));
    if ( decimales >0){
        maxPages = maxPages - decimales+1;// porq empieza d 1
    }
	
    pagLinksDiv.innerHTML = "&nbsp;";
    pagLinksDiv2.innerHTML = "&nbsp;";
	for ( var i =1; i<=maxPages;i++){
		pagLinksDiv.innerHTML = pagLinksDiv.innerHTML + '&nbsp;<a href="javascript:showSetCP('+i+')">'+i+'</a>&nbsp; ';
		pagLinksDiv2.innerHTML = pagLinksDiv2.innerHTML + '&nbsp;<a href="javascript:showSetCP('+i+')">'+i+'</a>&nbsp; ';
	}
	return itemsForPage;
  }
    
  function reloadRangesSorts(){
  	for (var i=0;i<this.attributeHeaders.length;i++){
  		this.attributeHeaders[i].scReLoad(this.filteredItems);
  	}
  }
  
  function sortByAttribute(attribName){
	  try{
	  	this.sortByAttributeField = attribName;
	  	var itemsAr = this.filteredItems;
	  	var item = null;
	  	var respAr = new Array();
	  	var toSortAr = null;
	  	for (var i=0;i<itemsAr.length;i++){
	  		item = itemsAr[i];
	  		toSortAr =  new Array();
	  		toSortAr[0] = item.getAttributeByName(attribName).getValue();
	  		toSortAr[1] = item.getId();
	  		respAr[respAr.length]=toSortAr;
	  	}
	  	//Sorting the array.
	  	respAr = respAr.sort();
		this.filteredItems = new Array();
	  	for (var i=0;i<respAr.length;i++){
	  		//Modfying the items
	  		this.filteredItems[i] = this.getItemById(respAr[i][1]);
	  	}
	  } catch (e) {
	  	if ( this.reportMsgs == true ) alert(e);
	  }
  }
  
  //Clear the table that shows the items
  function clearShowTable(){
  	var tbl = document.getElementById(this.showItemsTable);
  	while( tbl.rows.length>0){
  		tbl.deleteRow(0);
  	}
  }
  //---------------------------------------------------------------
  
  //----------------SORT ITEMS BY ATTRIB VALUE---------------------
  function sortByAttributeValue(attribName, attribValue){
  	var newFiltered = new Array();
  	for (var i=0; i<this.filteredItems.length;i++){
  		var attrib = this.filteredItems[i].getAttributeByName(attribName);
  		if ( attrib != null ){
			if ( this.reportMsgs == true ) alert("attrib.getValue().toUpperCase() ="+attrib.getValue().toUpperCase()+" == "+ "attribValue.toUpperCase()="+attribValue.toUpperCase());
  			if (attrib.getValue().toUpperCase() == attribValue.toUpperCase()){
  				newFiltered[newFiltered.length]=this.filteredItems[i];
  			}
  		}
  	}
  	if ( this.reportMsgs == true ) alert("Filtered Items "+newFiltered.length);
  	this.filteredItems = newFiltered;
  }
  //---------------------------------------------------------------
  
  //----------------SORT ITEMS BY ATTRIB VALUE---------------------
  function sortByAttributeRange(attribName, minRange, maxRange){
  	var newFiltered = new Array();
  	for (var i=0; i<this.filteredItems.length;i++){
  		var item = this.filteredItems[i];
  		if ( item != null ){
  			if (item.getNumericAttributeValueByName(attribName) >= minRange && item.getNumericAttributeValueByName(attribName) < maxRange){
  				newFiltered[newFiltered.length]=this.filteredItems[i];
  			}
  		}
  	}
  	this.filteredItems = newFiltered;
  }
  //---------------------------------------------------------------
  //---------------- RESTART FILTERS ------------------------------
  function resetFilters(){
  	this.filteredItems = this.items;
  	this.currentFilters = new Array();
  	this.nextFilterId = 1;
  	this.showFilteredItems();
  }
  //---------------------------------------------------------------
  
  function addFilter(fsita){
   try{	
  	//Parameter checking
	
  	if ( fsita.fName == "SortBy" ){
  		if ( fsita.fParameters.length < 1){
			if ( this.reportMsgs == true ) alert("Filter (SortBy) not added: Wrong number of paramters "+( fsita.fParameters.length > 0)+"["+fsita.fParameters.length+"]");
			return -1;
  		}
  	} else if ( fsita.fName == "sortByAttribValue" ){
  		if ( fsita.fParameters.length < 2) {
  			if ( this.reportMsgs == true ) alert("Filter (sortByAttribValue) not added: Wrong number of paramters "+( fsita.fParameters.length > 1)+"["+fsita.fParameters.length+"]");
  			return -1;
  		}
	} else if ( fsita.fName == "sortByAttribRange") {
		if ( fsita.fParameters.length < 3){
			if ( this.reportMsgs == true ) alert("Filter (SortByAttribRange) not added: Wrong number of paramters "+( fsita.fParameters.length > 1)+"["+fsita.fParameters.length+"]");
			return -1;
		}
	}
	//Uniqueness checking
	var filt;
	for (var i=0; i<this.currentFilters.length;i++){
		filt = this.currentFilters[i];
		if ( filt.fName == fsita.fName){
			//I don't need to check parameters length again
			var eq = true;
			for ( var j = 0;j<filt.fParameters.length;j++){
				if ( filt.fParameters[j] !=  fsita.fParameters[j]) { 
					eq = false;
				}
			}
			if ( eq == true){
				if ( this.reportMsgs == true ) alert("Filter ("+fsita.fName+") not added: Filter already added ")
				return -2;
			}
		}
	}
	//Every check done, adding the filter
  	fsita.fId = this.nextFilterId;
  	this.nextFilterId += 1;
  	this.currentFilters[this.currentFilters.length] = fsita;
  	if ( this.reportMsgs == true ) alert("Filter ("+fsita.fName+") added Succesfully ("+fsita.to_sop()+") ");
  	this.applyFilters();
  	return fsita.fId;
   } catch (e) {
   	if ( this.reportMsgs == true ) alert(e);
   }
  }
  
  function removeFilter(fId){
  	var alterFilters = new Array();
  	for ( var i=0; i<this.currentFilters.length; i++){
  		if ( this.currentFilters[i].fId != fId ){
  			alterFilters[alterFilters.length] = this.currentFilters[i];
  		}
  	}
  	this.currentFilters = alterFilters;
  	this.applyFilters();
  }
  
  function applyFilters(){
		var fsita = null;
		var fhdiv = document.getElementById("SC_filterHistoryDiv");
		
		fhdiv.innerHTML = "";
		//Finding the Sort By 
		this.filteredItems = this.items;
	
	  	for ( var i=0; i<this.currentFilters.length; i++){
	  		fsita = this.currentFilters[i];
			if ( fsita.fName == "SortByAttribValue" ){
				if ( fsita.fParameters[1].indexOf("|") == -1 ){
					this.sortByAttributeValue(fsita.fParameters[0],fsita.fParameters[1]);
				} else {
					this.sortByAttributeValue(fsita.fParameters[0],fsita.fParameters[1].split("|"));
				}
			} else if ( fsita.fName == "SortByAttribRange") {
				this.sortByAttributeRange(fsita.fParameters[0],fsita.fParameters[1],fsita.fParameters[2]);
			}
			fhdiv.innerHTML += fsita.generateRemove();
			fhdiv.innerHTML += '<br/>';
	  	}
	  	
	  	if (this.sortByAttributeField != ""){
	  		this.sortByAttribute(this.sortByAttributeField);
	  	}
	  	
	  	this.showFilteredItems();
	  	this.showFilteredItemsSetCP(1);
	  }	 
	  
	  function listEveryItem(){
	  	var str= "";
	  	for (var i=0;i<this.items.length;i++){
	  		str += this.items[i].getId() + " - " + this.items[i].getAttributeByName('Price').getValue() + " | \n";
	  	}
	  	alert (str);
	  }	
}

//---------------------FOR PAGINATION -----------------------------
	function setPagination(){
		document.getElementById(itemctrl.itemSortMaxPerPage).value = document.getElementById(itemctrl.itemSortMaxPerPageDefault).value;
		document.getElementById(itemctrl.itemSortPaginationDiv).style.display="block";
		itemctrl.showFilteredItemsSetCP(1);
	}
	  
	function unsetPagination(){
		document.getElementById(itemctrl.itemSortMaxPerPage).value = 50;
		document.getElementById(itemctrl.itemSortPaginationDiv).style.display="none";
		itemctrl.showFilteredItemsSetCP(1);
	}
	
	function prevPage(){
		var currentPage = document.getElementById(itemctrl.itemSortCurrentPage);
		if (currentPage.value > 1){
			itemctrl.showFilteredItemsSetCP(parseInt(currentPage.value) -1);
		}
	}
	function nextPage(){
		var currentPageCtrl = document.getElementById(itemctrl.itemSortCurrentPage);
	    var currentPage = currentPageCtrl.value;
	
		var itemCount = parseInt(document.getElementById(itemctrl.itemSortTotalCount).innerHTML);
		var maxPerPage = parseInt(document.getElementById(itemctrl.itemSortMaxPerPage).value);
	
		var maxPages = (itemCount / maxPerPage);
		var decimales = (parseFloat(maxPages) - parseInt(maxPages));
	    if ( decimales >0){
	        maxPages = maxPages - decimales+1;// porq empieza d 1
	    }
		if (currentPage < maxPages){
			itemctrl.showFilteredItemsSetCP(parseInt(currentPage) +1);
		}
	}
	function showSetCP(cp){
		itemctrl.showFilteredItemsSetCP(cp);
	}
	
//------------------------------------------
//--------------FILTERS.--------------------
	function resetFilters(){
		itemctrl.resetFilters();
		document.getElementById(itemctrl.filterHistoryDiv).innerHTML = "";
		addFinishLinks();
	}
	
	function addFilter(fName, fParam1, fParam2, fParam3){
		var fsita = new SC_Filter();
		fsita.fName = fName;
		if ( fParam3 != null){
			fsita.fParameters[0] = fParam1;
			fsita.fParameters[1] = fParam2;
			fsita.fParameters[2] = fParam3;
		} else if (fParam2 != null){
			fsita.fParameters[0] = fParam1;
			fsita.fParameters[1] = fParam2;
		} else if (fParam1 != null){
			fsita.fParameters[0] = fParam1;	
		}
		if (fsita.fParameters.length>0){
			return itemctrl.addFilter(fsita);
		}
	}
	
	function sortBy(attribName){
		itemctrl.sortByAttribute(attribName)
		itemctrl.showFilteredItems();
	}
	
	function removeFilter(fId){
		itemctrl.removeFilter(fId);
	}
	
	//Capitalize Words
	function capWords(inputString) {
		var tmpStr, tmpChar, preString, postString, strlen;
		tmpStr = inputString.toLowerCase();
		stringLen = tmpStr.length;
		if (stringLen > 0)
		{
		  for (i = 0; i < stringLen; i++)
		  {
		    if (i == 0)
			{
		      tmpChar = tmpStr.substring(0,1).toUpperCase();
		      postString = tmpStr.substring(1,stringLen);
		      tmpStr = tmpChar + postString;
		    }
		    else
			{
		      tmpChar = tmpStr.substring(i,i+1);
		      if (tmpChar == " " && i < (stringLen-1))
			  {
		      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
		      preString = tmpStr.substring(0,i+1);
		      postString = tmpStr.substring(i+2,stringLen);
		      tmpStr = preString + tmpChar + postString;
		      }
		    }
		  }
		}
		return tmpStr;
	}

	function addAttribLinksLinks(attributeName, spanName){
		function addFinish(pList,str){
		    for ( var i=0;i<pList.length;i++){
		        if (pList[i][0] == str){
		            pList[i][1] = pList[i][1] + 1 
		            return false;
		        }
		      }
		    var nomCount = new Array();
		    nomCount[0] = str;
		    nomCount[1] = parseInt(1);
		    list[list.length]=nomCount;
		    return true;
		}
		var list = new Array();
		for (var i=0;i<itemctrl.filteredItems.length;i++){
		    var item = itemctrl.filteredItems[i];
		    addFinish(list,item.getAttributeByName(attributeName).getValue());
		}
		
		//Creating the tags
		/*
		var finishLinks = document.getElementById(spanName);
		finishLinks.innerHTML = "";
		for (var i=0;i<list.length;i++){
			var tags = document.createElement("A");
			if ( list[i][1] >0 && list[i][0].length > 0){
				tags.href="javascript:addFilter('SortByAttribValue','"+attributeName+"','"+list[i][0]+"');";
				tags.innerHTML=list[i][0]+"<strong>("+list[i][1]+")</strong>"
				finishLinks.appendChild(tags);
				finishLinks.innerHTML +="<br/>";
			}
		}
		*/
		var finishLinks = document.getElementById(spanName);
		finishLinks.innerHTML = "";
		var allOption = document.createElement('option');
		allOption.value=attributeName;
		allOption.innerHTML="Show All";
		finishLinks.appendChild(allOption);
		list.sort();
		for (var i=0;i<list.length;i++){
			tags = document.createElement("option");
			if ( list[i][1] >0 && list[i][0].length > 0){
				tags.value=attributeName;
				tags.innerHTML=capWords(list[i][0]);
				finishLinks.appendChild(tags);
			}
		}
	}
//------------------------------------------
	
  function getElementsLikeId(likeid, tagName, container){
	var retObjs = new Array();
	if ( typeof(container)=="string" ){
		container = document.getElementById(container);
	}
  	try{
  		var tags = container.getElementsByTagName(tagName);
  		for( var i=0;i<tags.length;i++){
  			if ( tags[i].id.toLowerCase().indexOf(likeid.toLowerCase()) != -1 ){
  				retObjs[retObjs.length] = tags[i];
  			}
  		}
  	}catch (e) {}
  	return retObjs;
  }
//------------------------------------------
	var facceptedChars = "01234356789.";
	function extractFloat(str){
		var strNum = "";
		var len = str.length;
		for(var i=0;i<len;i++){
			ch = str.charAt(i);
			if(facceptedChars.indexOf(ch) != -1) strNum += ch;
		}
		if (strNum == "") return 0;
		return parseFloat(strNum);
	}
	
	function dosDecimales(num){
		var str = num +"";
		var dotIndex = str.indexOf(".");
		var strLastIndex = str.length-1;
		if ( dotIndex == -1 ){
			return str+".00";
		} else if( dotIndex+2 > strLastIndex ){
			return str +"0";
		} else {
			return str.substring(0,dotIndex +3);
		}
	}
	
	function callCellJS(){
		//SHOW HIDE UNITTYPES
		var unittypes = getElementsLikeId('unittype-','span','showItemsTable');
		var unittypesDiv = getElementsLikeId('unittype-','div','showItemsTable');
		//SHOW HIDE SALEUNITS
		var saleunits = getElementsLikeId('saleunit-','span','showItemsTable');
		var saleunitsDiv = getElementsLikeId('saleunit-','div','showItemsTable');
		//SHOW VENDORNAME OR SUPPLIERCODE
		var vendorName = getElementsLikeId('vendorname-','span','showItemsTable');
		var supplierCode = getElementsLikeId('suppliercode-','span','showItemsTable');
		var rlinks = getElementsLikeId('rlink-','a','showItemsTable');
		//--------
		var scontents = getElementsLikeId('sc_content_narrow-','span','showItemsTable');
		var sqtytofix = getElementsLikeId('sc_qty_tofix-','div','showItemsTable');
		//---------
		var uoms = getElementsLikeId('unittype-','div','showItemsTable');
		var pricesX = getElementsLikeId('priceqtytable-','div','showItemsTable');
	
		
		
		var i = unittypes.length;
		while(i--){
			scontents[i].innerHTML = capWords(scontents[i].innerHTML);
			sqtytofix[i].innerHTML = "Qty";
			//SHOW HIDE UNITTYPES
			unittype = unittypes[i].innerHTML;
			if (unittype == "[Error processing dynamic tag getCurrentAttribute('item', 'unitstype') : attribute 'unitstype' not found]") {
				unittypesDiv[i].style.display="none";
			}else {
				unittypesDiv[i].style.display="block";
			}
		
			//SHOW HIDE SALEUNITS
			saleunit = saleunits[i].innerHTML;
			if (saleunit == "[Error processing dynamic tag getCurrentAttribute('item', 'saleunit') : attribute 'saleunit' not found]") {
				saleunitsDiv[i].style.display="none";
			}else {
				saleunitsDiv[i].style.display="block";
			}
			
			//SHOW VENDORNAME OR SUPPLIERCODE
			var vendorname = vendorName[i].innerHTML;
			if (vendorname.length < 10 && vendorname != "[Error processing dynamic tag getCurrentAttribute('item', 'vendorname') : attribute 'vendorname' not found]" && vendorname != "" ) {
				rlinks[i].innerHTML= capWords(vendorname);
			}else if( supplierCode[i].innerHTML != "[Error processing dynamic tag getCurrentAttribute('item', 'custitemsupplier_code') : attribute 'custitemsupplier_code' not found]" && supplierCode[i].innerHTML != ""){
				rlinks[i].innerHTML= capWords(supplierCode[i].innerHTML);
			}else{
				rlinks[i].innerHTML= "#";
			}
			
			if ( uoms[i].innerHTML == "per Unit") uoms[i].innerHTML = "Unit";
			//--
			tbl = pricesX[i].getElementsByTagName('tbody')[0];
			if ( tbl != null){
				trows = tbl.rows;
				otd = document.createElement('td');
				otd.innerHTML ="Price/Unit";
				otd.className="listheadernosort, smalltext";
				otd.style.background="#E8E7DD none repeat scroll 0 0";
				otd.style.fontSize="11px";
				otd.style.fontWeight="bold";
				otd.style.fontFamily="Verdana";
				tbl.rows[0].appendChild(otd);
				rlen = trows.length
				var x=1;
				rowQty = extractFloat(uoms[i].innerHTML);
				while ( x<rlen){
					rowPrice = extractFloat(trows[x].cells[1].innerHTML);
					otd = document.createElement('td');
					otd.className="texttable";
					otd.innerHTML = "($"+dosDecimales(rowPrice/rowQty)+"/Unit).";
					tbl.rows[x].appendChild(otd);
					x = x + 1;
				}
			} else if ( uoms[i].innerHTML != "" ){
				qty = extractFloat(uoms[i].innerHTML);
				if ( qty == 0) continue;
				price = extractFloat(pricesX[i].innerHTML)
				if ( price == null || price == "") continue;
				
				pricesX[i].innerHTML = "$"+price+" ($"+dosDecimales(price/qty)+"/Unit).";
			}
		}
	}
	

