/*
* Global Variables
*/
var userModelSelect = 0;
var invalidPrinterInput = false;
var ratingsEnabled;
var availabilityEnabled;
$(function(){
	ratingsEnabled = $('#bvPath').html() != '';
	availabilityEnabled = $('#displayBasicAvailabilityInfo').exists();
	
	$('.printerNameLink, .renameLink', '#userPrinterList').live('click', function(e){
		e.preventDefault();
		
		displayEditPrinter($(this));
	});
	
	$('.removeLink', '#userPrinterList').live('click', function(e){
		e.preventDefault();
		var printerId = $(this).closest('.renameRemove').children('.removePrinterId').html();
		DWRDelegate.deletePrinter(printerId);
		
		$(this).closest('.displayPrinterRow').remove();
		
		if ( !$('.firstRow', '#userPrinterList').exists() )
			$('.displayPrinterRow:first', '#userPrinterList').addClass('firstRow');
		
		if (!$('.displayPrinterRow', '#userPrinterList').exists()) {
			hideAll();
			$('.editPrinters, #savedPrinters').hide();
		}
		
		dhtmlDataSync();
	});
	
	$('input[name=printerName]', '#userPrinterList').live('keyup', function(e){
		if (e.keyCode == 13) 
			$(this).closest('.updatePrinter').find('.saveNameLink').click();
	});
	
	$('.saveNameLink', '#userPrinterList').live('click', function(e){
		e.preventDefault();
		$('.printerNameError').hide();
		var $updatePrinterElement = $(this).closest('.updatePrinter');
		var printerName = $updatePrinterElement.find('input[name=printerName]').val();
		var printerId = $updatePrinterElement.find('input[name=printerId]').val();			

		DWRDelegate.setUserPrinterName(printerId, printerName, function(updated){
			if(updated){
				dhtmlDataSync();
				displayPrinterList();
			}else{
				$updatePrinterElement.closest('.displayPrinterRow').find('.printerNameError').show();
				invalidPrinterInput = true;
			}
		});		
	});
	
	$('#savePrinterLink').click(function(e){
		e.preventDefault();
		hideAll();	
		$('#invalidCharsMsg').hide();
		$('#overPrinterLimitMsg').hide();				
		$('.inBannerSubHeading', '#savePrinterContent').show();
		$('#newPrinterName').show();
		$('#addPrinterSubmit').show();		

		if ($('.accountLogin', '#header').exists()){
			window.location.hash += "savePrinter";
			setCookie("lastViewedPrinter", window.location.hash);
			return;
		}
		showSavePrinterInput();
		DWRDelegate.getUserPrinterCount(function(count){
			if (count >= 10){							
				$('#overPrinterLimitMsg').show();
				$('.inBannerSubHeading', '#savePrinterContent').hide();
				$('#newPrinterName').hide();
				$('#addPrinterSubmit').hide();
			}
		});
	
	});
	
	$('.overlayObj','#savePrinter, #editPrintersLink').bgiframe();
	$('.ajaxWait').bgiframe();
	
	$('a', '#editPrintersLink, .managePrintersTrigger').click(function(e){
		e.preventDefault();
		
		hideAll();
		
		if (invalidPrinterInput == true){
			invalidPrinterInput = false;
			dhtmlDataSync();
			displayPrinterList();				
		}
		
		$manageOverlay = $('.overlayObj', '#editPrintersLink');		
		$manageOverlay.toggle();	
		
		var newScroll = $manageOverlay.offset().top - $(window).height() + $manageOverlay.height();
		if (newScroll > $(window).scrollTop())
		   $(window).scrollTop(newScroll);
	});
	
	$('#newPrinterName').keyup(function(e){
		if (e.keyCode == 13)
			$('#addPrinterSubmit').click();
	})
	
	$('#addPrinterSubmit').click(function(e){
		var printerName = $('#newPrinterName').val();
		var printerSku = $('#selectedPrinterSku').val();		
		var uri = "";

		if (printerName == '') {
			return;
		}
			
		if (window.location.hash.length > 0){
			uri = window.location.hash;
		}else{
			uri = decodeCookieHash();
		}
				
		DWRDelegate.addPrinter(printerName, uri, printerSku, function(added){
			if (added){
				if (!$('.displayPrinterRow', '#userPrinterList').exists()) 
					$('.editPrinters, #savedPrinters').show();
												
				$('.overlayObj', '#savePrinter').hide();
				
				dhtmlDataSync();
			}else{
				$('#invalidCharsMsg').show();
				$('.inBannerSubHeading', '#savePrinter').hide();
				$('#newPrinterName').focus();
			}
						
			$('#newPrinterName').val('');
		});
	});
	
	$('#printersSelect').click(function(){ hideAll(); });
	$('#printersSelect').change(function(){	
		if(!isNonEmptyString($(this).val()))
			return;
		var sku = getSkuFromPrinterVal($(this).val());
		
		//setPrinter(sku, true);
		window.location.hash = getHashFromPrinterVal($(this).val());		
		populateFromHash();
		$('#savePrinterLink').hide();		
	});
	
	
	
	$('#manufSelect').change(function(e){
		e.preventDefault();
		var manufId = $(this).val();
		if(!isNonEmptyString(manufId))
			return;	
			
		ajaxStarted('manufSelect');
		clearResults();			
		addAttrToHash('manufId', manufId);		
		populateTypesModels(manufId, 0, 0);
	});	
	
	$('#typeSelect').change(function(e){
		var typeId = $('#typeSelect').val();	
		if(!isNonEmptyString(typeId))
			return;
		e.preventDefault();
		
		ajaxStarted('typeSelect');
		clearResults();
		var manufId = $('#manufSelect').val();
		addAttrToHash('typeId', typeId);		
		DWRDelegate.getInkModelRefinements(manufId, typeId, function(inkTypeModelInfo){
			populateModels(inkTypeModelInfo.models, 0);
			
			ajaxEnded('typeSelect');
		});
	});

	$('#modelSelect').change(function(e){
		e.preventDefault();
		var modelId = $(this).val();
		
		this.blur();
		addAttrToHash('modelId', modelId);		
		populateInkSkus(modelId);		
		setCookie("lastViewedPrinter", window.location.hash);		
		userModelSelect = 1;
		$('#inkSearchTerm', '#inkTonerManuf').focus();
	});
	
	$('a', '#searchResultsCallout').click(function(){
		$('html, body').animate({ scrollTop: $('#ajaxSkuResults').offset().top - 35}, 500);
	})
	
	$('#goBtn', '#inkTonerManuf').parent().css("opacity",((this.value != '')?'0.5':'1'));			
	$('#inkSearchTerm', '#inkTonerManuf').keyup(function(e){
		$('#goBtn', '#inkTonerManuf').parent().css("opacity",((this.value == '')?'0.5':'1'));			
	});
	
	$('#goBtn', '#inkTonerManuf').click(function(e){
		e.preventDefault();
		
		if ($('#inkSearchTerm', '#inkTonerManuf').val() != '')
			this.form.submit();
	});
	
	var printerVal = $('#printersSelect').val();
	printerSku = getSkuFromPrinterVal(printerVal);	
	if (isNonEmptyString(printerSku)){
		//setPrinter(printerSku, true);
		window.location.hash = getHashFromPrinterVal(printerVal);
	}
		
	if (window.location.hash.length == 0){ 
		setHashFromCookie();
	}
	
	savePrinterIfNeeded();
	
	if ($('.accountLogin', '#header').exists()){
		$('#savePrinterLink').addClass('accountLogin');
	}
		
	populateFromHash();		
	dhtmlDataSync();
	
	$('.col1, .col2', '#inkTonerManuf #content').hide();
	updateSavePrinterLink();
});

function ajaxStarted(c_name) { $('.ajaxWait').addClass(c_name); }
function ajaxEnded(c_name) { $('.ajaxWait').removeClass(c_name); }

function showSavePrinterInput(){
	var printerDesc = $('#printerDesc').html();		
	if (isNonEmptyString(printerDesc)){
		if (printerDesc.length > 30){
			printerDesc = printerDesc.substring(0, 30);
		}
		$('#newPrinterName').val(printerDesc);
	}
	$('.overlayObj', '#savePrinter').show();
	$('#newPrinterName').focus();
}

function updateSavePrinterLink(){
	if (!($('.accountLogin', '#header').exists())){
		$('#savePrinterLink').attr('href', '#');
	}
}

function savePrinterIfNeeded(){
	//check for savePrinter
	var index = window.location.hash.indexOf("savePrinter");
	if (index > 0){
		var hash = 	window.location.hash;
		hash = hash.substring(0, index);
		window.location.hash = hash;
		setCookie("lastViewedPrinter", hash);
		$('#savePrinterLink').click();
	}
}

function getMdImageFromSmall(imageUrl){
	imageUrl = imageUrl.replace('/sm/', '/md/');
	imageUrl = imageUrl.replace('_sm', '_md');
	return imageUrl;
}

function setHashFromCookie(){
	var cookieVal = getCookie('lastViewedPrinter');
	if (cookieVal == null){		
		return;
	}	
	if (window.location.hash.length == 0){
		window.location.hash = decodeCookieHash();	
	}
}
				
function clearResults(){
	clearInkSkus();
	setPrinter('', false);
	$('#savePrinterLink').hide();
	$('#printersSelect').val('');
	$('#searchTip').show();
	
	$('.col1, .col2', '#inkTonerManuf #content').hide().removeClass('oneColumn').removeClass('threeColumn').removeClass('fourColumn');
	$('#searchResultsCallout').hide();
}

function clearInkSkus(){
	$('.inkSkuClone, .hSeperate', '#manufBrandCont, #prvtBrandCont').remove();
}

function populateInkSkus(modelId){
	clearInkSkus();
	ajaxStarted('populateInkSkus');
	
	$('.col1, .col2', '#inkTonerManuf #content').hide().removeClass('oneColumn').removeClass('threeColumn').removeClass('fourColumn');
	$('#searchResultsCallout').hide();
	
	DWRDelegate.getInkModelResults(modelId, function(inkModelResultsInfo){
		
		var printerSku = inkModelResultsInfo.printer;
		setPrinter(printerSku, true);		
		$('#savePrinterLink').show();
		var inkResultsPrivate = inkModelResultsInfo.inkResultsPrivate;
		var inkResultsManuf = inkModelResultsInfo.inkResultsManuf;

		/*
		* ODB's Side
		*/
		cols = 2;
		if (inkResultsManuf.length == 0) {
			cols = 4;
			$('.col2', '#ajaxSkuResults').addClass('fourColumn');
		} else if (inkResultsManuf.length >= 3 && inkResultsPrivate.length == 1) {
			cols = 1;
			$('.col2', '#ajaxSkuResults').addClass('oneColumn');
			$('.col1', '#ajaxSkuResults').addClass('threeColumn');
		}	
		
		for(i=0; i < inkResultsPrivate.length; i++) {
			$('.col2:hidden', '#inkTonerManuf #content').show();
			
			var $skuDiv = $('.inkSkuTemplate').clone().attr('class', 'inkSkuClone');
			
			populateSkuDiv($skuDiv, inkResultsPrivate[i], i);
						
			$skuDiv.insertBefore('.inkSkuTemplate', '#prvtBrandCont');
			
			if ((i % cols == cols-1 && i != 0) || (inkResultsPrivate.length == 1)) {				
				$('.inkSkuClone:last', '#prvtBrandCont').css('border', 'none');
				
				if (i != inkResultsPrivate.length - 1 && inkResultsPrivate.length > cols) {
					$('<div class="hSeperate"></div>').insertAfter('#prvtBrandCont .inkSkuClone:last');
				}
					
				$('<div class="clear"></div>').insertAfter('#prvtBrandCont .inkSkuClone:last');
			} 
			
			$skuDiv.addClass(inkLayoutClasses(i));
		}
		
		if (inkResultsPrivate.length == 1 && cols != 1 && cols != 4) {			
			$('.inkSkuClone:last', '#prvtBrandCont').addClass('centerSku');
		}

		/*
		* Manufacturer's Side
		*/
		cols = 2;
		if (inkResultsPrivate.length == 0) {
			cols = (inkResultsManuf.length < 4) ? inkResultsManuf.length : 4;
			$('.col1', '#ajaxSkuResults').addClass('fourColumn');
		} else if (inkResultsPrivate.length == 1 && inkResultsManuf.length >= 3) {
			cols = 3;
			$('.col2', '#ajaxSkuResults').addClass('oneColumn');
			$('.col1', '#ajaxSkuResults').addClass('threeColumn');
		}
		
		if ($('#manufSelect').val() == '100686') {
			$('.col2', '#ajaxSkuResults').hide();
			$('.col1', '#ajaxSkuResults').removeClass('threeColumn').removeClass('oneColumn').addClass('fourColumn');
			cols = (inkResultsManuf.length < 4) ? inkResultsManuf.length : 4;
		}
		
		for(i=0; i < inkResultsManuf.length; i++) {
			$('.col1:hidden', '#inkTonerManuf #content').show();
			
			var $skuDiv = $('.inkSkuTemplate').clone().attr('class', 'inkSkuClone');
			
			populateSkuDiv($skuDiv, inkResultsManuf[i], i);			
			
			$skuDiv.insertBefore('#npbdiv');
			
			if ((i % cols == cols-1 && i != 0) || (inkResultsManuf.length == 1)) {				
				$('.inkSkuClone:last', '#manufBrandCont').css('border', 'none');
				
				if (i != inkResultsManuf.length - 1 && inkResultsManuf.length > cols) {
					$('<div class="hSeperate"></div>').insertAfter('#manufBrandCont .inkSkuClone:last');
				}
					
				$('<div class="clear"></div>').insertAfter('#manufBrandCont .inkSkuClone:last');
				
			}
			
			$skuDiv.addClass(inkLayoutClasses(i));
		}
		
		if (inkResultsManuf.length == 1 && cols != 1 && cols != 4) {				
			$('.inkSkuClone:last', '#manufBrandCont').addClass('centerSku');
		}
		
		totalResults = inkResultsManuf.length;
		if ($('#manufSelect').val() != '100686') {
		  totalResults = totalResults + inkResultsPrivate.length;
		}
		
		if (totalResults > 0) {
			$('#searchResultsCallout').show();
			
			if ($("#manufSelect").attr('selectedIndex') != 0)
				$('.printerBrandLabel', '#searchResultsCallout').show().html( $("#manufSelect option:selected").html() + '&nbsp;' );
			else
				$('.printerBrandLabel', '#searchResultsCallout').hide();
			
			if ($("#typeSelect").attr('selectedIndex') != 0)
				$('.printerTypeLabel', '#searchResultsCallout').show().html( $("#typeSelect option:selected").html() + '&nbsp;' );
			else
				$('.printerTypeLabel', '#searchResultsCallout').hide();
							
			if ($("#modelSelect").attr('selectedIndex') != 0)
				$('.printerModelLabel', '#searchResultsCallout').show().html( $("#modelSelect option:selected").html() + '&nbsp;' );
			else 
				$('.printerModelLabel', '#searchResultsCallout').hide()
				
			$('.resultCountLabel', '#searchResultsCallout').html( totalResults );	
			
			if (totalResults == 1)
				$('.resultsPural', '#searchResultsCallout').hide();
			else
				$('.resultsPural', '#searchResultsCallout').show();		
		}
		
		ajaxEnded('populateInkSkus');
		
		$('html, body').animate({ scrollTop: $('#searchResultsCallout').offset().top - 5}, 500);
	});
}



function inkLayoutClasses(x){
	var tempClass = '';
	
	/*if (x % 2 == 0)	tempClass = 'inkLeft';
	else tempClass = 'inkRight'*/
	
	if (x < 2) tempClass += 'firstInkRow';
	
	return tempClass;
}

function populateTypesModels(manufId, typeId, modelId){
	$('#typeSelect').children('option:not(:first)').remove();
	
	DWRDelegate.getInkModelRefinements(manufId, typeId, function(inkTypeModelInfo){	
		for(i=0; i < inkTypeModelInfo.types.length; i++) {
			var selected = "";
			if (inkTypeModelInfo.types[i].id == typeId){
				selected = " selected='selected'";
			}
			$('#typeSelect').append("<option value='" + inkTypeModelInfo.types[i].id + "'" + selected + ">" + inkTypeModelInfo.types[i].description + "</option>");
		}
		
		if (inkTypeModelInfo.types.length > 0){
			$('#typeSelect').removeAttr("disabled");
		}else{
			$('#typeSelect').attr("disabled", "disabled");
		}	
		populateModels(inkTypeModelInfo.models, modelId);	
		
		ajaxEnded('manufSelect');
	});
}

function populateModels(models, modelId){
	//$('#modelSelect').html('');
	$('#modelSelect').children('option:not(:first)').remove();
	var selIndex = 0;
	for(i=0; i < models.length; i++) {
		var selected = "";
		if (models[i].id == modelId){
			selected = " selected='selected'";
		}
		$('#modelSelect').append("<option value='" + models[i].id + "'" + selected+ ">" + models[i].description + "</option>");
	}	
	if (models.length > 0){
		$('#modelSelect').removeAttr("disabled");
	}else{
		$('#modelSelect').attr("disabled", "disabled");
	}		
}

function getSkuFromPrinterVal(printerVal){
	if (printerVal == null){
		return null;
	}
	var sub = printerVal.substring(printerVal.indexOf('_')+1);
	return sub;
}

function getHashFromPrinterVal(printerVal){
	var sub = printerVal.substring(0, printerVal.indexOf('_'));
	return sub;
}

function getBVImgSrc(sku){
	if (!ratingsEnabled) return;
	
	var bvPath = $('#bvPath').html();
	var bvContext = $('#bvContext').html();
	var src = bvPath + '/' + bvContext + '/' + sku + '/ratings.gif';
	return src;
}

function setPrinter(printer, printerSelected){
	if (!printer.valid) {
		$('#printerBVImg').attr('src', '');
		$('#printerBVImg').hide();	
		$('#printerDesc').html('');
		$('#selectedPrinterSku').val('');
		$('#selectedPrinter').hide();				
		$('#searchTip').show();			
	}else{
		$('#selectedPrinter').show();	
		$('#searchTip').hide();	
		$('#printerBVImg').show();		
		$('#selectedPrinterBVLink').attr('href', printer.link + '#reviewTab');
		$('#printerBVImg').attr('src', getBVImgSrc(printer.sku));
		$('#printerImage').attr('src', getMdImageFromSmall(printer.imageFileName));			
		$('#printerDesc').html(printer.description);
		$('#selectedPrinterSku').val(printer.sku);

	}
	
	if (!ratingsEnabled) {
		$('#printerBVImg').hide();
		$('#selectedPrinterBVLink').hide();
	}
}

function displayEditPrinter($this){
	displayPrinterList();
	$container = $this.closest('.displayPrinterTable');
	$editRow = $container.next();
	
	$editRow.show();
	$container.hide();
	$editRow.children().find(':input[type=text]').focus().select();
}

function displayPrinterList(){
	$('.updatePrinterTable').hide();
	$('.displayPrinterTable').show();
}


function populateSkuDiv($skuDiv, ajaxSkuInfo, index){
	var descLink = appendJSessionIDToAUrl(ajaxSkuInfo.link);
	
	$skuDiv.children().find('.skuImg').attr('src', ajaxSkuInfo.imageFileName);
	$skuDiv.find('.skuDescription').html(ajaxSkuInfo.description);
	$skuDiv.find('.itemNumber span').html(ajaxSkuInfo.sku);
	$skuDiv.find('.main_price').html(ajaxSkuInfo.sellPrice);
	$skuDiv.find('.taxed_price').html(ajaxSkuInfo.sellPriceWithTax);
	
	$skuDiv.find('.inkSku').val(ajaxSkuInfo.sku);
	$skuDiv.find('.inkSku').attr('name', 'entryFormList[' + index + '].sku');	
	$skuDiv.find('.inkInput').attr('name', 'entryFormList[' + index + '].qty');
	$skuDiv.find('.inkSubmit').attr('name', 'cmd_addCart.button.INDEX[' + index + ']');
	$skuDiv.find('.skuDescriptionLink').attr('href', descLink);
	
	if (!ratingsEnabled) {
		$skuDiv.find('.printerBVLink').hide();
	} else {
		var BVLink = descLink + '#reviewTab';	
		$skuDiv.find('.printerBVLink').attr('href', BVLink);
		$skuDiv.find('.printerBVImg').attr('src', getBVImgSrc(ajaxSkuInfo.sku));
	}
	
	if ( !availabilityEnabled ) {
		$skuDiv.children('.availibilityIcons').hide()
	} else {
		var $availIconsDiv = $skuDiv.children('.availibilityIcons');	
		var availText = '<table>';
		var imgSrc = $('#imgPath').html();
		for(j=0; j < ajaxSkuInfo.availabilityIconList.length; j++) {
			availText += "<tr><td><img src='" + imgSrc + ajaxSkuInfo.availabilityIconList[j].iconSrc + "'/></td>";
			availText += "<td>"+ ajaxSkuInfo.availabilityIconList[j].msg + "</td></tr>";
		}
		
		$availIconsDiv.append(availText + '</table>');
	}
	
	
}

function populateRebateData($skuDiv, rebateData){
	if(!rebateData.rebate){
		return;
	}	
	$skuDiv.find('.inkRebateTable').show();	
	$skuDiv.find('.rebateJspText').html(rebateData.jspText);
	$skuDiv.find('.rebateRegPrice').html(rebateData.regPrice);
	
		
	if(rebateData.numOfInstantRebates > 0){	
		$skuDiv.find('.instantRebates').show();
		$skuDiv.find('.amtOfAllInstantRebates').html(rebateData.amtOfAllInstantRebates);
	}	

	if (rebateData.numOfMailInRebates > 0){
		$skuDiv.find('.mailInRebates').show();
		$skuDiv.find('.amtOfAllMailInRebates').html('&nbsp;-' + rebateData.amtOfAllMailInRebates);
		$skuDiv.find('.numOfMailInRebates').prepend('&nbsp;' + rebateData.numOfMailInRebates);		
		$skuDiv.find('.priceAfterRebates').prepend('&nbsp;' + rebateData.priceAfterRebates);
				
		if(rebateData.numOfInstantRebates > 0){	
			$skuDiv.find('.instantRebateMessage').show();		
		}
	}	
}

function populateAllDropDowns(manufId, typeId, modelId){
	if (!isNonEmptyString(manufId)){
		return;
	}
	if (!isNonEmptyString(typeId)){
		typeId = 0;
	}
		
	setSelected('#manufSelect', manufId);	
	populateTypesModels(manufId, typeId, modelId);	
}

function populateFromHash(){
	if (window.location.hash.length == 0){
		return;
	}
	var manufId = getAttrFromHash('manufId', '');	
	var typeId = getAttrFromHash('typeId', '');	
	var modelId = getAttrFromHash('modelId', '');	
	
	populateAllDropDowns(manufId, typeId, modelId);
		
	if (isNonEmptyString(modelId)){
		populateInkSkus(modelId);
	}
}

function setSelectedValues(manufId, typeId, modelId){
	setSelected('#manufSelect', manufId);
	if (typeId != 0)
		setSelected('#typeSelect', typeId);

	if (modelId.length != 0)	
		setSelected('#modelSelect', modelId);
}

function setSelected(selectId, navId){
	$(selectId).children('option[value=' + navId + ']').attr('selected', 'true');
}

function dhtmlDataSync(){
	DWRDelegate.getPrinterList(function(userPrinterList){
		var selectData = '';
		var printName = '';
		
		$tempHeading = $('li:first', '#userPrinterList');
		$('li:first', '#userPrinterList').remove();
		$('ul', '#userPrinterList').html('');
		
		if (userPrinterList.length > 0) {
			$('#printersSelect').children('option:not(:first)').remove();
			for(i=0; i < userPrinterList.length ; i++) {		
				printName = (userPrinterList[i].printerName.length < 20) ? userPrinterList[i].printerName : userPrinterList[i].printerName.substring(0,15) + '...';
				
				selectData += '<option value=' + userPrinterList[i].uri + "_" + userPrinterList[i].printerSku + '>' + userPrinterList[i].printerName + '</option>';
							
				$tempLi = $('.displayPrinterRowTemplate li').clone();
				
				$('ul', '#userPrinterList').append($tempLi);
				
				$('.displayPrinterRow:last .removePrinterId, .displayPrinterRow:last .removePrinterId', '#userPrinterList').html(userPrinterList[i].userPrinterId);
				$('.displayPrinterRow:last input[name=printerId]', '#userPrinterList').val(userPrinterList[i].userPrinterId);
				
				$('.displayPrinterRow:last .printerNameLink', '#userPrinterList').html(printName);
				$('.displayPrinterRow:last input[name=printerName]', '#userPrinterList').val(userPrinterList[i].printerName);
			}
			
			$('.editPrinters, #savedPrinters').show();
		} else {
			$('.editPrinters, #savedPrinters').hide();
		}
		
		$('ul', '#userPrinterList').prepend($tempHeading);
		$('.displayPrinterRow:first', '#userPrinterList').addClass('firstRow');
		
		$('#printersSelect').append(selectData);		
	});
}

function getAttrFromHash(attrStr, hash){
	var encoded = false;
	if (!isNonEmptyString(hash)){
		var hash = window.location.hash;
	}
	else{
		if (hash.indexOf('%23') > -1){//encoded
			encoded = true;
		}
	}	
	var attrIndex = hash.indexOf(attrStr);
	if (attrIndex == -1){
		return;
	}
	var sub = hash.substr(attrIndex);
	var i = sub.indexOf(':') + 1;	
	var j = sub.indexOf('/');
	var len = j - i;
	return sub.substr(i,len);
}

function decodeCookieHash(){
	var cookieVal = getCookie('lastViewedPrinter');
	cookieVal = cookieVal.replace('%23', '#');	
	return cookieVal;
}

function addAttrToHash(attrName, attrVal){
	var attrStr = attrName + ':' + attrVal +'/';
	
	if (attrName == 'manufId'){
		window.location.hash = '#' + attrStr;
		return;
	}	
	var hash = window.location.hash;	
	if (hash.indexOf(attrName) > -1){
		hash = hash.replace(new RegExp(attrName + ':[0-9]*/'),attrStr);
		if (attrName == 'typeId'){ //clear modelId
			hash = hash.replace(new RegExp('modelId:[0-9]*/'),''); 
		}
	}else{
		hash = hash + attrStr;
	}
	window.location.hash = hash;
}


