var myShoppingList = new ShoppingList();
var Departments;

ShoppingList.prototype.addItem = addItem;
ShoppingList.prototype.addComment = addComment;

function ShoppingList(){
	this.items = new Array();
}

function addItem(value){
	this.items[this.items.length] = value;
}

function addComment(departmentId, categoryId, comment){
	for(x = 0; x < this.items.length; x++){
		if(this.items[x].categoryId == categoryId && this.items[x].departmentId == departmentId){
			this.items[x].comment = comment;
			break;
		}
	}
}
function updateComments(){
	var item;
	for(x = 0; x < myShoppingList.items.length; x++){
		item = document.getElementById("txt" + myShoppingList.items[x].departmentId + myShoppingList.items[x].categoryId);
		if(item != null){
			item.value = myShoppingList.items[x].comment;
		}
	}
}

function shoppingItem(){
	this.departmentId = "";
	this.categoryId = "";
	this.departmentName = "";
	this.categoryName = "";
	this.comment = "";
}
function modifyList(source, categoryId, categoryName, departmentId, departmentName)
{
	var TEXT_NODE_TYPE = 3;
	var myItem;
	
	
	if(source.firstChild.nodeType == TEXT_NODE_TYPE){
		if(source.firstChild.data == "Add"){
			myItem = new shoppingItem();
			myItem.categoryId = categoryId;
			myItem.categoryName = categoryName;
			myItem.departmentId = departmentId;
			myItem.departmentName = departmentName;
			
			myShoppingList.addItem(myItem);
			source.firstChild.data = "Remove";
		}
		else{
			for(x = 0; x < myShoppingList.items.length; x++){
				if(myShoppingList.items[x].categoryId == categoryId && myShoppingList.items[x].departmentId == departmentId){
					myShoppingList.items.splice(x, 1);
					break;
				}
			}
			source.firstChild.data = "Add";
		}
	}
	
	buildList();
}
function modifyListTwo(source, categoryName, departmentId, departmentName)
{
	var myItem;
	var category = document.getElementById(categoryName);
	var catName;
	var categoryId;
	var regEx = new RegExp(" ", "g");
	
	if(category.type == "text"){
		catName = category.value;
		categoryId = "cat" + myShoppingList.items.length;
		category.value = "";
	}
	else if(category.type.substring(0, 6) == "select"){
		if(category.selectedIndex > -1){
			categoryId = category.options[category.selectedIndex].value;
			departmentId = categoryId.split("|")[0];
			categoryId = categoryId.split("|")[1];
		}
	}
	
	if(source.firstChild.data == "Add"){
		if(catName.replace(regEx, "") != ""){
			myItem = new shoppingItem();
			myItem.categoryId = categoryId;
			myItem.categoryName = catName;
			myItem.departmentId = departmentId;
			myItem.departmentName = departmentName;
		
			myShoppingList.addItem(myItem);
		}
	}
	else{
		for(x = 0; x < myShoppingList.items.length; x++){
			if(myShoppingList.items[x].categoryId == categoryId && myShoppingList.items[x].departmentId == departmentId){
				myShoppingList.items.splice(x, 1);
				break;
			}
		}
	}
	
	buildList();
}

function serializeList(list){
	var serialList = "";
	for(x = 0; x < list.items.length; x++){
		if(serialList.length > 0){
			serialList += "^";
		}
		serialList += list.items[x].departmentId + "|" + list.items[x].departmentName + "|" + list.items[x].categoryId + "|" + list.items[x].categoryName + "|" + list.items[x].comment;
	}
	
	return serialList;
}
function deSerializeList(list){
	var myList = new ShoppingList();
	var items = list.split("^");
	var itemAttributes;
	
	myList.items = new Array();
	if(items != ""){
		for(x = 0; x < items.length; x++){
			itemAttributes = items[x].split("|");
			myList.items[x] = new shoppingItem();

			myList.items[x].departmentId = itemAttributes[0];
			myList.items[x].departmentName = itemAttributes[1];
			myList.items[x].categoryId = itemAttributes[2];
			myList.items[x].categoryName = itemAttributes[3];
			myList.items[x].comment = itemAttributes[4];
		}
	}
	
	return myList;
}

function myCompare(a,b){
	var returnValue = 0;
	var aValue;
	var bValue;
	
	aValue = a.departmentName + a.categoryName;
	bValue = b.departmentName + b.categoryName;
	
	if(aValue < bValue){
		returnValue = -1;
	}
	else if(aValue > bValue){
		returnValue = 1;
	}
	return returnValue;
}

function buildList(){
	var list = document.getElementById("ddlShopList");
	var test = myShoppingList.items;
	var counter = 0;
	
	test.sort(myCompare);
	myShoppingList.items = test;
	
	list.options.length = 0;
	
	for(i = 0; i < Departments.length; i++){
		list.options.length++;
		list.options[counter] = Departments[i];
		list.options[counter].className = "BoldText";
		counter++;
		for(x = 0; x < myShoppingList.items.length; x++){
			if(myShoppingList.items[x].departmentId == Departments[i].value){
				list.options.length++;
				list.options[counter].value = myShoppingList.items[x].departmentId + "|" + myShoppingList.items[x].categoryId;
				list.options[counter].text = myShoppingList.items[x].categoryName;
				counter++;
			}
		}
	}
}
function InitializeList(){
	var list = document.getElementById("ddlShopList");
	Departments = new Array();
	for(x = 0; x < list.options.length; x++){
		Departments[x] = list.options[x];
	}
}

function snsSubmitList(){
	document.getElementById("snsListData").value = serializeList(myShoppingList);
	return true;
}

function snsChangeStep(page){
	var form = document.forms[0];
	form.action = "createList" + page + ".asp";
	snsSubmitList();
	form.target = "_self";
	form.submit();
}
function updateCategoryStatus(){
	var category;
	for(x = 0; x < myShoppingList.items.length; x++){
		category = document.getElementById("category" + myShoppingList.items[x].departmentId + myShoppingList.items[x].categoryId);
		if(category != null){
			category.firstChild.data = "Remove";
		}
	}
}
function updateLinkStatus(source)
{
	var category;
	var categoryId;
	var departmentId;
	var value;
	var element;
	
	element = document.getElementById(source);
	
	if (element != null)
	{		
		value = element.options[element.selectedIndex].value.split("|");
		categoryId = value[1];
		departmentId = value[0];		
	}
	
	category = document.getElementById("category" + departmentId + categoryId);
	if(category != null){
		category.firstChild.data = "Add";
	}	
}
function displayCategory(row, tdId)
{ 		
	if (document.getElementById(row).style.display == "none")
	{
		document.getElementById(row).style.display = "block";
		document.getElementById(tdId).className = "snsArrowDown";
	}
	else if (document.getElementById(row).style.display == "block")
	{
		document.getElementById(row).style.display = "none";
		document.getElementById(tdId).className = "snsArrow";
	}
}

function addSnsStore(store, name)
{ 
	//window.opener.document.getElementById("tdSelectStore").style.display = "none";	
	window.opener.document.getElementById("tdShowStore").style.display = "block";
	window.opener.document.getElementById("txtStore").value = store;
	window.opener.document.getElementById("txtStoreName").value = name;
	window.opener.document.getElementById("tdShowStore").innerHTML = "<br>Shop 'N' Scan Store: " + name;
	self.close();
}

function printSNSList(page, formName)
{		
	var frm = eval("document." + formName + "");
	
	if (page == "sendToShopScan.asp" || page == "printListStore.asp")
	{		
		if (document.getElementById("txtStore").value.length == 0)
		{
			alert("Please Select A Store");
			return false;
		}
		else if (page == "sendToShopScan.asp" && document.getElementById("txtPSCNumber").value.length == 0)
		{
			alert("Please Enter Your PSC Number");
			return false;
		}
		else if (page == "sendToShopScan.asp" && (document.getElementById("txtAreaCode").value.length == 0	|| document.getElementById("txtPhone3").value.length == 0 || document.getElementById("txtPhone4").value.length == 0))
		{
			alert("Please Enter Your Phone Number");
			return false;
		}
		else
		{
			frm.action = page;
			frm.target = "_Blank";
			frm.submit();
									
			return true;			
		}		
	}
	else
	{	
		frm.action = page;
		frm.target = "_blank";		
		frm.submit();
			
		return true;
	}
}
function snsSteps(page, formName)
{		
	var frm = eval("document." + formName + "");
			
	if (formName == "createList1" || formName == "createList2")
	{
		snsSelectList('ddlList');
	}
	
	frm.action = page;
	frm.target = "_self";
	frm.submit();	
}
function printIt()
{
	window.print();
}
function snsSignUp(action)
{	
	var obj = document.getElementById("txtPSCNumber");
		
	if (action == 'show')
	{
		obj.readOnly = false;
	}
	else
	{	
		obj.readOnly = true;
	}	
}