// JavaScript Document

/*
create selections obj
-------------------------
create category obj
   ----------------------
   create item obj
   add item to category
   ----------------------
add category to selections
--------------------------

*/


function Selections() {
	var mainObj = this;
	mainObj.LIST = new Array(); // list of categories with items in them
	mainObj.getItemByID = getItemByID;
	mainObj.addCategory = addCategory;
	mainObj.getCategoryByName = getCategoryByName;
	mainObj.display_selection_box = display_selection_box;
	mainObj.toString = toString;
	
	function getItemByID(id) {
		for(var i=0; i<mainObj.LIST.length; i++) {
			var CAT = mainObj.LIST[i];
			
			for(var j=0; j<CAT.items.length; j++) {
				var itemObj = CAT.items[j];
				if(itemObj.id == id) {
					
					return itemObj;	
				}
			}
		}
		return false;
	}
	
	
	function toString() {
		var outputString = '';
		var itemObject;
		var CAT;
		for(var i=0; i < mainObj.LIST.length; i++) {
			CAT = mainObj.LIST[i];
			outputString += "\n"+"CATEGORY["+CAT.id+"]: "+CAT.name+"\n";
			
			for(var j=0; j < CAT.items.length; j++) {
				itemObject = CAT.items[j];
				outputString += "   - "+itemObject.id+": "+itemObject.name+"\n";
			}
		}
		
		return outputString;
	}
	
	
	function addCategory(catObj) {
		if(getCategoryByName(catObj.name) == false) {
			mainObj.LIST[mainObj.LIST.length] = catObj;
			return 1;
		} else {
			return 2;	
		}
	}
	
	function getCategoryByName(cat_name) {
		for(var i=0; i < mainObj.LIST.length; i++) {
			if(mainObj.LIST[i].name == cat_name) {
				return mainObj.LIST[i];
			}
		}
		
		return false;
	}
	
	function display_selection_box(divID, catID) {
		var CODE = '<table cellspacing="0" cellpadding="0" class="selection_cart">';
		
		var itemObject;
		var CAT;
		var cat_header = false;
		for(var i=0; i < mainObj.LIST.length; i++) {
			CAT = mainObj.LIST[i];
			cat_header = false;
			
			for(var j=0; j < CAT.items.length; j++) {
				itemObject = CAT.items[j];
				if(!SEL_CART.inCart(itemObject.id)) {
					if(!cat_header) {
						
						if(CAT.id != catID) {
							CODE += '<tr><td class="selection_box_header" onclick="CUSTOM_SELS.display_selection_box(\''+divID+'\',\''+CAT.id+'\');">'+CAT.name+' &raquo;</td></tr>'+"\n";	
						} else {
							CODE += '<tr><td class="selection_box_header" onclick="CUSTOM_SELS.display_selection_box(\''+divID+'\');">'+CAT.name+' ^</td></tr>'+"\n";
							
						}
						cat_header = true;
					}
					if(CAT.id == catID) {
						CODE += '<tr><td class="selection_cart_item" onclick="sel_addItem(\''+itemObject.id+'\');">'+itemObject.name+'</td></tr>'+"\n";
					}
				}
			}
		}
		if(mainObj.LIST.length < 1) {
			//CODE += '<tr><td class="selection_cart_header">There are no items.</td></tr>'+"\n";
		}
		CODE += '</table>';
		
		CURRENTLY_DISPLAYING = catID;
		document.getElementById(divID).innerHTML = CODE;
		
	}
	
}


function Category(id, name) {
	var mainObj = this;
	mainObj.id = id;
	mainObj.name = name;
	mainObj.items = new Array();
	mainObj.addItem = addItem;
	
	function addItem(itemObj) {
		mainObj.items[mainObj.items.length] = itemObj;
		return true;
	}
}


function ItemObj(id, name, categoryName) {
	this.id = id;
	this.name = name;
	this.categoryName = categoryName;
}





function selectionCart() {	
	var mainObj = this;
	mainObj.items = new Array();
	mainObj.inCart = inCart;
	mainObj.addItem = addItem;
	mainObj.removeItem = removeItem;
	mainObj.display_cart_code = display_cart_code;
	mainObj.toString = toString;
	mainObj.getItemListAsString = getItemListAsString;
	mainObj.itemListToCart = itemListToCart;
	mainObj.clearCart = clearCart;
	
	function inCart(id) {
		// search to see if this item is already in the list
		for(var i=0; i<mainObj.items.length; i++) {
			if(mainObj.items[i].id == id) {
				return true;
			}
		}
		return false;
	}
	
	function addItem(id) {
		if(!inCart(id)) {
			itemObj = CUSTOM_SELS.getItemByID(id);
			mainObj.items[mainObj.items.length] = itemObj;
			return 1;
		}
		return 2;
	}
	
	function removeItem(id) {
		var newList = new Array();
		var exists = false;
		for(var i=0; i<mainObj.items.length; i++) {
			if(mainObj.items[i].id != id) {
				newList[newList.length] = mainObj.items[i];
			} else {
				exists = true;
			}
		}		
		mainObj.items = newList;
		
		if(exists) {
			return 1;
		} else {
			return 2;	
		}
	}
	
	function getItemListAsString() {
		var result = '-1';
		for(var i=0; i<mainObj.items.length; i++) {
			result += ','+mainObj.items[i].id
		}
		return result;
	}
	
	function clearCart(itemListString) {
	    var removeList = new Array(mainObj.items.length);
		for(var i=0; i<mainObj.items.length; i++) {
		    removeList[i] = mainObj.items[i].id;
		}
		for(var i in removeList) {
		    mainObj.removeItem(removeList[i]);
	    }
    }
	function itemListToCart(itemListString) {
	    mainObj.clearCart();
		var items = itemListString.split(",");
		
		for(var i=0; i<items.length; i++) {
			mainObj.addItem(items[i]);
		}
	}
	
	function display_cart_code(restore_button) {
		if(typeof(restore_button) == 'undefined') {
			restore_button = false;
		}
		
		var CODE = '<table cellspacing="0" cellpadding="0" class="selection_cart">';
		
		var itemObject;
		var CAT;
		var cat_header = false;
		for(var i=0; i < CUSTOM_SELS.LIST.length; i++) {
			CAT = CUSTOM_SELS.LIST[i];
			cat_header = false;
			
			for(var j=0; j < CAT.items.length; j++) {
				itemObject = CAT.items[j];
				if(mainObj.inCart(itemObject.id)) {
					if(!cat_header) {
						CODE += '<tr><td class="selection_cart_header">'+CAT.name+'</td></tr>'+"\n";
						cat_header = true;
					}
					CODE += '<tr><td class="selection_cart_item" onclick="sel_removeItem(\''+itemObject.id+'\');">'+itemObject.name+'</td></tr>'+"\n";
				}
			}
		}
		if(mainObj.items.length < 1) {
			CODE += '<tr><td class="selection_cart_header">There are no items in your box.</td></tr>'+"\n";
		}
		
		CODE += '<tr><td class="main" align="center">';
		
	    
		CODE += '<br /><input class="custom-selection-fields"type="button" name="clearSelections" onclick="clear_selection();" value="Clear your selections" />';
		    
		if(CUSTOMERS_ID > 0) {
		    CODE += '<br /><br />';
		    CODE += '<input class="custom-selection-fields" type="text" id="custom-selection-name" value="Enter a name" maxlength="128" />';
		    CODE += '<br /><input class="custom-selection-fields" type="button" name="saveSelections" onclick="save_selection();" value="Save your selection" />';
		    CODE += '<br /><br /><span id="customer-selection-wrap"></span>';
	    } else {
	        CODE += '<br /><br /><input type="button" onclick="window.location.href=\'login.php\';" value="Login to save your selections" class="login_save_button" />';
        }
        
		CODE += '</td><tr>';
		
		CODE += '</table>';
		
		return CODE;
		
	}
	
	function toString() {
		var CODE = "\n";
		var itemObject;
		var CAT;
		var cat_header = false;
		for(var i=0; i < CUSTOM_SELS.LIST.length; i++) {
			CAT = CUSTOM_SELS.LIST[i];
			cat_header = false;
			
			for(var j=0; j < CAT.items.length; j++) {
				itemObject = CAT.items[j];
				if(mainObj.inCart(itemObject.id)) {
					if(!cat_header) {
						CODE += CAT.name+"\n";
						cat_header = true;
					}
					CODE += '   - '+itemObject.name+"\n";
				}
			}
		}
		
		return CODE;
	}
	
}



////////////
function sel_addItem(id) {
	SEL_CART.addItem(id);
	CUSTOM_SELS.display_selection_box('selection_box', CURRENTLY_DISPLAYING);
	displayCart('selection_cart');
}

function sel_removeItem(id) {
	SEL_CART.removeItem(id);
	CUSTOM_SELS.display_selection_box('selection_box', CURRENTLY_DISPLAYING);
	displayCart('selection_cart');
}


function clear_selection() {
    SEL_CART.clearCart();
	CUSTOM_SELS.display_selection_box('selection_box', CURRENTLY_DISPLAYING);
	displayCart('selection_cart');
}
function save_selection() {
    var nme = jQuery('#custom-selection-name').val();
    if(typeof(nme) == 'undefined' || nme == "") {
      nme = "No Name";   
    }
    if(selection_name_exists(nme)) {
        if(!confirm("This will overwrite the existing selection: "+nme)) {
           return;
        }
    }
    
	var sel = SEL_CART.getItemListAsString();
    jQuery.getJSON("ajax-customer-selections.php", {exec: "add", customers_id: CUSTOMERS_ID, products_id: PRODUCT_ID,
                                                selection_name: nme, selections: sel},
        function(obj) {
            if(obj.status) {
                alert("Your selection has been successfully saved.");
                get_all_selections();
            }
        }
    );
}
var selectionsList = new Array();
function get_all_selections() {
    if(CUSTOMERS_ID < 1) return;
    
    jQuery.getJSON("ajax-customer-selections.php", {exec: "get-all", customers_id: CUSTOMERS_ID, products_id: PRODUCT_ID},
        function(obj) {
            var count = 0;
            var options_html = '<select class="custom-selection-fields" name="customer-selection" id="customer-selection">';
            for(var i=0; i<obj.length; i++) {
               selectionsList[obj[i].ccs_id] = {'selection_name': obj[i].selection_name, 'selections': obj[i].selections}
               count++;
               options_html += '<option value="'+obj[i].ccs_id+'">'+obj[i].selection_name+'</option>';
            }
            options_html += '</select>';
            options_html += '<br /><input class="custom-selection-fields-half" type="button" name="restoreSelections" onclick="restore_selection();" value="Load" />';
            options_html += '<input class="custom-selection-fields-half" type="button" name="restoreSelections" onclick="delete_selection();" value="Delete" />';
            if(count > 0) {
                jQuery('#customer-selection-wrap').html(options_html);
            }
        }
        
    );
   
}
function selection_name_exists(nme) {
    for(var i in selectionsList) {
       var sel = selectionsList[i];
       if(nme == sel.selection_name) {
           return true;
       }
    }
    return false;
}

jQuery(document).ready(function() { get_all_selections(); });
function restore_selection() {
    var val = jQuery('#customer-selection').val();
    var sel = selectionsList[val]['selections'];
    if(typeof(sel) != 'undefined') {
        SEL_CART.itemListToCart(sel);
    }
	CUSTOM_SELS.display_selection_box('selection_box', CURRENTLY_DISPLAYING);
	displayCart('selection_cart');
    jQuery('#custom-selection-name').val(selectionsList[val]['selection_name']);
}

function delete_selection() {
    if(!confirm("Are you sure you want to delete this selection?")) {
        return;
    }
    var val = jQuery('#customer-selection').val();
    jQuery.getJSON("ajax-customer-selections.php", {exec: "delete", ccs_id: val },
        function(obj) {
            if(obj.status) {
                alert("Your selection has been successful deleted.");
                get_all_selections();
	            displayCart('selection_cart');
            }
        }
    );
}
function displayCart(div, restore_button) {
    var selection_name = jQuery('#custom-selection-name').val();
	if(typeof(restore_button) == 'undefined') {
		restore_button = false;
	}
	
	var divObj = document.getElementById(div);
	var cart_code = SEL_CART.display_cart_code(restore_button);
	divObj.innerHTML = cart_code;
	
	var field_container = document.getElementById(""+FIELD_ID);
	if(field_container != null) {
		field_container.value = SEL_CART.toString();
	}
	get_all_selections();
	jQuery('#custom-selection-name').val(selection_name);
}

function hideCategoriesExcept(category_name, prefix) {
	var itemObject;
	var CAT;
	var show;
	for(var i=0; i < CUSTOM_SELS.LIST.length; i++) {
		CAT = CUSTOM_SELS.LIST[i];
		if(CAT.name == category_name) {
			if(CURRENTLY_DISPLAYING != category_name) {
				show = true;
				CURRENTLY_DISPLAYING = CAT.name;
			} else {
				show = false;
				CURRENTLY_DISPLAYING = '';
			}
		} else {
			show = false;
		}
		for(var j=0; j < CAT.items.length; j++) {
			itemObject = CAT.items[j];
			if(show && !SEL_CART.inCart(itemObject.id)) {
				showDiv(prefix+""+itemObject.id);
			} else{
				hideDiv(prefix+""+itemObject.id);
			}
		}
	}
}


function hideDiv(divId) { 
		if (document.getElementById) { // DOM3 = IE5, NS6 
			document.getElementById(divId).style.visibility = "hidden"; 
			document.getElementById(divId).style.height = "1px"; 
			document.getElementById(divId).style.overflow = "hidden"; 
		} else { 
			if (document.layers) { // Netscape 4 
				var divObj = eval("document."+divId);
				divObj.visibility = "hidden"; 
			} else { // IE 4 
				var divObj = eval("document.all."+divId);
				divObj.style.visibility = "hidden"; 
			} 
		} 
		
	} 
			
function showDiv(divId) { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(divId).style.visibility = "visible"; 
		document.getElementById(divId).style.height = "auto"; 
		document.getElementById(divId).style.overflow = "auto"; 
	} else { 
		if (document.layers) { // Netscape 4 
			var divObj = eval("document."+divId);
			divObj.visibility = "visible"; 
		} else { // IE 4 
			var divObj = eval("document.all."+divId);
			divObj.style.visibility = "visible"; 
		} 
	} 
	
	
}
