// Toolbar Scripts

	function toggle_search(){
		
		if($('#currency_div').is(":visible")){
			toggle_currencies();
		}
		
		if($('#search_div').is(":visible")){
			$('#search_div').slideToggle(600, function(){});
		} else {
			$('#search_div').slideToggle(600, function(){
				$('#search_div input[type=text]').focus();
			});
		}
		
		/*
		if($('#search_div').css('display') == 'none'){
			$('#search_div').slideToggle(600, function(){
				$('#search_div input[type=text]').focus();
			});
		} else {
			$('#search_div').slideToggle(600, function(){
				//$('#search_div').stop(true, true).hide();
			});
		}
		*/
		
	}
	
	function toggle_currencies(){
		
		if($('#search_div').is(":visible")){
			toggle_search();
		}
		$('#currency_div').slideToggle(600);
				
	}
	
	function choose_ensemble(){
		createPopUp(500, 270, true, false, 'center', this, 'Ensembles', true, '/ensembles');
	}

// Form Scripts

	function form_submit(form, e){
		
		var key_code;
		
		if(window.event) key_code = window.event.keyCode;
		else if (e) key_code = e.which;
		else return true;
		
		if(key_code == 13){
			form.submit();
			return false;
		} else {
			return true;	
		}
		
	}
	
	function smart_field(element, default_text, class_name){
		
		element.onfocus = function(){
			if(this.value == default_text){
				this.value = '';
				this.className = class_name;
				this.focus();
			} else {
				this.select();
			}
		}
		element.onblur = function(){
			if(this.value == ''){
				this.value = default_text;
				this.className = class_name + '_blur';
			}
		}
		
		if (element.value == default_text) element.className = class_name + '_blur';
		element.onblur();
		
	}

// Calendar Appointment Scripts
	
	var selected_time = "";
	function set_time(appointment_time){
		selected_time = appointment_time;		
	}
	
	var selected_date = "";
	function set_date(appointment_date){
		selected_date = appointment_date;		
	}
	
	function available_dates(){
		
		var show_name = document.getElementById('show_name').value;

		var s = document.createElement('SELECT');
			s.className = "registration_field";
			s.name = "appointment_date";
			s.id = "appointment_date";
			s.style.width = "150px";
			s.onchange = function () { available_times() };

		document.getElementById('date_holder').innerHTML = '';
		document.getElementById('date_holder').appendChild(s);
		
		var o = document.createElement('OPTION');
			o.text = 'Choose A Date';
			o.value = '';

		try {
			document.getElementById('appointment_date').add(o, null);
		} catch(e) {
			document.getElementById('appointment_date').add(o);
		}
		
		available_times();
		
		$.getJSON('/com/controller/appointment.available_dates.cfm?show_name='+show_name, {},
			function(response){
				
				var s = document.createElement('SELECT');
					s.className = "registration_field";
					s.name = "appointment_date";
					s.id = "appointment_date";
					s.style.width = "150px";
					s.onchange = function () { available_times() };
				
				document.getElementById('date_holder').innerHTML = '';
				document.getElementById('date_holder').appendChild(s);
				
				var o = document.createElement('OPTION');
					o.text = 'Choose A Date';
					o.value = '';
		
				try {
					document.getElementById('appointment_date').add(o, null);
				} catch(e) {
					document.getElementById('appointment_date').add(o);
				}

				$.each(response.DATA.appointment_date,
					   function(i, appointment_date){
						
						   if(appointment_date == selected_date){
							    $("#appointment_date").append($("<option SELECTED />").val(appointment_date).text(response.DATA.display_date[i]));
						   } else {
						   		$("#appointment_date").append($("<option />").val(appointment_date).text(response.DATA.display_date[i]));
						   }
						
					   }
				);
				available_times();
				
			}
		);
		
	}
	
	function available_times(){
		
		var appointment_date;
		try {
			appointment_date = document.getElementById('appointment_date').value;
		} catch(e) {
			appointment_date = "";	
		}
		
		try {
			show_name = document.getElementById('show_name').value;
		} catch(e) {
			show_name = "";
		}
		
		var s = document.createElement('SELECT');
			s.className = "registration_field";
			s.name = "appointment_time";
			s.id = "appointment_time";
			s.style.width = "130px";

		document.getElementById('time_holder').innerHTML = '';
		document.getElementById('time_holder').appendChild(s);

		var o = document.createElement('OPTION');
			o.text = 'Choose A Time';
			o.value = '';
		
		try {
			document.getElementById('appointment_time').add(o, null);
		} catch(e) {
			document.getElementById('appointment_time').add(o);
		}
		
		$.getJSON('/com/controller/appointment.available_times.cfm?appointment_date='+appointment_date+'&show_name='+show_name, {},
			function(response){
				
				var s = document.createElement('SELECT');
					s.className = "registration_field";
					s.name = "appointment_time";
					s.id = "appointment_time";
					s.style.width = "130px";
				
				document.getElementById('time_holder').innerHTML = '';
				document.getElementById('time_holder').appendChild(s);
				
				$.each(response.DATA.appointment_time,
					   function(i, appointment_time){
						
						   if(appointment_time == selected_time){
							    $("#appointment_time").append($("<option SELECTED />").val(appointment_time).text(appointment_time));
						   } else {
						   		$("#appointment_time").append($("<option />").val(appointment_time).text(appointment_time));
						   }
						
					   }
				);
				
			}
		);
		
	}

// Product Selection Scripts
	
	var animation_speed = 200;
	var active_item;

	function load_item(id_item, preload){

		if(active_item != id_item){
			if(active_item){
				init_item(active_item);
			}
			init_styles(id_item);
			load_styles(id_item);
			active_item = id_item;
		} else {
			init_item(id_item);
			active_item = 0;
		}

	}

	function load_styles(id_parent, preload){
		
		
		$("#style_" + id_parent).show(animation_speed);
		$.getJSON('/com/controller/item_styles.cfm?id_parent='+id_parent, {},
			function(response){
				
				// Default Option
				default_style = '';
				if(typeof(preload) != 'undefined') default_style = preload;
			
				$.each(response.DATA.STYLE,
					   function(i, style){
						   if(default_style == style){
							    $("#style_" + id_parent).append($("<option  SELECTED />").val(style).text(style));
						   } else {
								$("#style_" + id_parent).append($("<option />").val(style).text(style));
						   }
					   }
				);
				
				if(default_style){
					document.getElementById('style_'+id_parent).onchange();	
				}
				
			}
		);

	}

	function load_metals(id_parent, preload){

		$("#style_"+id_parent+" option[value='']").remove();
		$("#metal_" + id_parent).show(animation_speed);
		$("#size_" + id_parent).hide(animation_speed);
		$("#similaritem_" + id_parent).hide(animation_speed);
		$("#quantity_" + id_parent).hide(animation_speed);
		
		style = document.getElementById('style_'+id_parent).value;
		
		$.getJSON('/com/controller/item_metals.cfm?id_parent='+id_parent+'&style='+style, {},
			function(response){

				// Clear List of Metals...
				init_metals(id_parent);
				
				// Default Option
				default_metal = $.cookie("FILTER_METAL");
				if(typeof(preload) != 'undefined') default_metal = preload;
				
				// Load New Metals
				$.each(response.DATA.METAL_TYPE,
					   function(i, metal){
							if(default_metal == response.DATA.METAL_ID[i]){
								$("#metal_" + id_parent).append($("<option  SELECTED />").val(response.DATA.METAL_ID[i]).text(metal));
							} else {
								$("#metal_" + id_parent).append($("<option />").val(response.DATA.METAL_ID[i]).text(metal));
							}
					   }
				);
				
				if(default_metal){
					document.getElementById('metal_'+id_parent).onchange();
				}

			}
		);

	}

	function load_sizes(id_parent, preload){

		$("#metal_"+id_parent+" option[value='']").remove();
		$("#size_" + id_parent).show(animation_speed);
		$("#similaritem_" + id_parent).hide(animation_speed);

		
		style = document.getElementById('style_'+id_parent).value;
		id_metal = document.getElementById('metal_'+id_parent).value;

		$.getJSON('/com/controller/item_sizes.cfm?id_parent='+id_parent+'&style='+style+'&id_metal='+id_metal, {},
			function(response){

				// Clear List of Sizes...
				init_sizes(id_parent);
				
				// Default Option
				default_size = $.cookie("FILTER_SIZE");
				if(typeof(preload) != 'undefined') default_size = preload;
				
				// Load New Sizes
				$.each(response.DATA.ITEM_SIZE,
					   function(i, size){

							size == 6 ? s = size + ' (Stock Size)' : s = size; 
							
							if(default_size == response.DATA.ID[i]){
								$("#size_" + id_parent).append($("<option SELECTED />").val(response.DATA.ID[i]).text('Size ' + s));
							} else {
								$("#size_" + id_parent).append($("<option />").val(response.DATA.ID[i]).text('Size ' + s));
							}
					   }
				);
				
				if(default_size){
					document.getElementById('size_'+id_parent).onchange();
				}

			}
		);

	}
	
	function load_similaritems(id_parent){
		
		$("#size_"+id_parent+" option[value='']").remove();
		$("#similaritem_" + id_parent).show(animation_speed);
		$("#quantity_" + id_parent).hide(animation_speed);
		
		id_metal = document.getElementById('metal_'+id_parent).value;
		id_size = document.getElementById('size_'+id_parent).value;
		style = document.getElementById('style_'+id_parent).value;
		
		$.getJSON('/com/controller/item_like.cfm?id_parent='+id_parent+'&id_metal='+id_metal+'&id_size='+id_size+'&style='+style, {},
			function(response){

				// Clear List of Sizes...
				init_similaritems(id_parent);

				// Load New Sizes
				$.each(response.DATA.STYLE,
					   function(i, style){
							$("#similaritem_" + id_parent).append($("<option />").val(response.DATA.ID_SIMILARITEM[i]).text('Yes, Add Style ' + style + ' ' + response.DATA.PRICE_SALE[i]));
					   }
				);
				
				if(response.ROWCOUNT != 0){
					$("#similaritem_" + id_parent).append($("<option />").val(0).text('No'));
				} else {
					$("#similaritem_" + id_parent).append($("<option SELECTED />").val(0).text('No Matching Items'));
					document.getElementById('similaritem_'+id_parent).onchange();
				}

			}
		);
		
	}
	
	function load_quantities(id_parent, preload){
		
		if(typeof(preload) != 'undefined'){
			$("#size_"+id_parent+" option[value='']").remove();
			$("#quantity_" + id_parent).show(animation_speed);
		} else {
		
			$("#similaritem_"+id_parent+" option[value='']").remove();
			$("#quantity_" + id_parent).show(animation_speed);
			
			init_quantities(id_parent);
			
			for(var i=1; i<=5; i++){
				$("#quantity_" + id_parent).append($("<option />").val(i).text(i));
			}
			$("#quantity_" + id_parent).append($("<option />").val(10).text('6+ Adjust At Checkout'));
		
		}
		
	}
	
	function add_item(id_parent, popup){
		
		style = document.getElementById('style_'+id_parent).value;
		id_metal = document.getElementById('metal_'+id_parent).value;
		id_size = document.getElementById('size_'+id_parent).value;
		id_similaritem = document.getElementById('similaritem_'+id_parent).value;
		quantity = document.getElementById('quantity_'+id_parent).value;
		
		if(typeof(popup) == 'undefined'){
			popup = false;
		}
		
		$("#item_"+id_parent).css("opacity", 0);
		$("#item_"+id_parent).parent().css("background", "url(/images/loader.gif) center no-repeat");

		/*
		alert('id_parent : '+id_parent
			 +'style : '+style
			 +'id_metal : '+id_metal
			 +'id_size : '+id_size
			 +'id_similaritem : '+id_similaritem
			 +'quantity : '+quantity);
		*/
		
		$.getJSON('/com/controller/cart_add.cfm',
			{
				id_parent : id_parent,
				style : style,
				id_metal : id_metal,
				id_size : id_size,
				id_similaritem : id_similaritem,
				quantity : quantity
			},
			function(response){
				setTimeout(function(){

					init_item(id_parent);
					$("#item_"+id_parent).parent().css("background", "");
					
					setTimeout(function(){
						
						$("#item_"+id_parent).css("opacity", 1);
						
						if(popup){
							
							$.each(response.DATA.ID_RECORD,
							   function(i, id_record, id_parent){
									parent.load_listing(response.DATA.ID_PARENT[i], response.DATA.ID_RECORD[i]);
							   }
							);
							
							show_seal(id_parent);
							if( parent.$('#quick_cart').length ){
								parent.$('#quick_cart').attr({
									src: parent.$('#quick_cart').attr("src")
								});
							}
							
							if( parent.$('#checkout_information').length ){
								parent.location.reload();
							}
							parent.closePopUp();
							
						} else {
						
							$.each(response.DATA.ID_RECORD,
							   function(i, id_record, id_parent){
									load_listing(response.DATA.ID_PARENT[i], response.DATA.ID_RECORD[i]);
							   }
							);
							
							show_seal(id_parent);
							if( $('#quick_cart').length ){
								$('#quick_cart').attr({
									src: $('#quick_cart').attr("src")
								});
							}
							
						}
						
					}, 500);

					active_item = 0;

				}, 500);
			}
		);

	}
	
	function init_item(id_item){
		$("#style_" + id_item).hide(animation_speed);
		$("#metal_" + id_item).hide(animation_speed);
		$("#size_" + id_item).hide(animation_speed);
		$("#similaritem_" + id_item).hide(animation_speed);
		$("#quantity_" + id_item).hide(animation_speed);
		init_styles(id_item);
	}
	
	function init_styles(id_parent){
		$("#style_" + id_parent).children().remove();
		$("#style_" + id_parent).append($("<option />").val('').text('Step 1 of 5: Carat Total Weight'));
	}

	function init_metals(id_parent){
		$("#metal_" + id_parent).children().remove();
		$("#metal_" + id_parent).append($("<option />").val('').text('Step 2 of 5: Choose A Metal'));
	}

	function init_sizes(id_parent){
		$("#size_" + id_parent).children().remove();
		$("#size_" + id_parent).append($("<option />").val('').text('Step 3 of 5: Finger Size'));
	}
	
	function init_similaritems(id_parent){
		$("#similaritem_" + id_parent).children().remove();
		$("#similaritem_" + id_parent).append($("<option />").val('').text('Step 4 of 5: Add Matching Item?'));
	}
	
	function init_quantities(id_parent){
		$("#quantity_" + id_parent).children().remove();
		$("#quantity_" + id_parent).append($("<option />").val('').text('Step 5 of 5: Quantity'));
	}


// Product Listing Scripts

	function load_listing(id_parent, id_record){
		
		$.get('/com/controller/product_listing.cfm',
			{ id_record : id_record },
			function(product_listing){
				
				if( $('#productlisting_' + id_record).length ){
					$('#productlisting_' + id_record).after(product_listing);
					$('#productlisting_' + id_record).remove();
				} else {
					$('#cartstyles_' + id_parent).append(product_listing);
				}
				
			}
		);
		
	}
	
	function remove_listing(id_record, id_parent){
		
		if(confirm("Are you sure you want to remove this item?")){
			
			$.post('/com/controller/cart_remove.cfm',
				{ id_record : id_record },
				function(response){
					
					if( $('#productlisting_' + id_record).length ){
					
						$('#productlisting_' + id_record).remove();
						if( $('#cartstyles_' + id_parent).children().length == 0 ){
							hide_seal(id_parent);
						}
						if( $('#quick_cart').length ){
							$('#quick_cart').attr({
								src: $('#quick_cart').attr("src")
							});
						}
						
					} else {
						location.reload();	
					}
					
				}
			);
			
		}
		
	}

// Product Seal Scripts

	function show_seal(id_parent){
				
		var seal_code = '<img src="/images/seal_cart.png"'+
							 'alt="This style is in your cart."'+
							 'title="This style is in your cart."'+
							 'height="34" width="35"'+
							 'vspace="5" hspace="5" /><br />';
		
		$('#seals_' + id_parent).html(seal_code);
		$('#addmore_' + id_parent).show(animation_speed);
		
	}
	
	function hide_seal(id_parent){
		
		$('#seals_' + id_parent).empty();
		$('#addmore_' + id_parent).hide(animation_speed);
		
	}

// Product Filter Scripts

	function filter_metal(metal_type){
		
		$.post('/com/controller/preferences_set.cfm',	
			{filter_metal : metal_type},function(response){}
		);
		
	}
	
	function filter_size(item_size){
		
		$.post('/com/controller/preferences_set.cfm',	
			{filter_size : item_size},function(response){}
		);
		
	}
	
// Search Scripts
	
	function toggle_quickcart(quick_cart){
		
		$.post('/com/controller/preferences_set.cfm',	
			{quick_cart : quick_cart},function(response){
				location.reload();	
			}
		);
		
	}
	
	function search_sort(sort_value){
		
		if(sort_value == 'set'){
			$('#sort_by').val('set');
			$('#sort_order').val('ASC');
		} else if (sort_value == 'style'){
			$('#sort_by').val('style');
			$('#sort_order').val('ASC');
		} else if (sort_value == 'cart'){
			$('#sort_by').val('cart');
			$('#sort_order').val('ASC');
		} else {
			$('#sort_by').val('name');
			$('#sort_order').val('ASC');
		}
		document.getElementById('class_form').submit();
		
	}
	
// Quick-Cart Scripts

	function toggle_checkbox(id){
		$('#record_' + id).attr('checked', !$('#record_' + id).attr('checked'));
	}

	function remove_items(){
			
		if(confirm("Are you sure you want to remove all checked items?")){
			
			var id_records = 0;
			$('input[type=checkbox]').each(function () { 
				if (this.checked) { 
					id_records += ',' + $(this).attr('name');
				}
			});
			
			$.post('/com/controller/cart_remove.cfm',
				{ id_record : id_records },
				function(response){
					parent.location.reload();
				}
			);
			
		}
	
	}

// Account Scripts

	function hide_orders(action){
		
		if(confirm("Are you sure you want to " + action + " all checked orders?")){
			
			var id_records = 0;
			$('input[type=checkbox]').each(function () { 
				if (this.checked) { 
					id_records += ',' + $(this).attr('name');
				}
			});

			$.post('/com/controller/account_order.hide.cfm',
				{ id_record : id_records },
				function(response){
					parent.location.reload();
				}
			);
			
		}
	
	}
	
	function convert_quote(id_order){
		
		var message = "A copy of this sales quote will be transferred to your shopping cart so you can place a Purchase Order.";
		message += "\nAny items currently in your shopping cart will be removed.";
		message += "\n\n";
		message += "Please keep in mind that prices will be updated based on current Imagine Bridal product prices.";
		
		if(confirm(message)){
			
			$.post('/com/controller/account_quote.convert.cfm',
				{ id_order : id_order },
				function(response){
					window.location = '/checkout';
				}
			);
			
		}
	
	}

// Checkout Scripts
	
	function item_notes(id_record){
		$.post('/com/controller/cart_notes.cfm',	
			{
				id : id_record,
				item_notes : $('#notes_' + id_record).val()
			},function(response){}
		);
	}
	
	function peghead_shape(id_record){
		
		$.post('/com/controller/cart_peghead.cfm',	
			{
				id : id_record,
				peghead_shape : $('#pegheadshape_' + id_record).val()
			},function(response){}
		);
		
	}
	
	function peghead_size(id_record){
		
		$.post('/com/controller/cart_peghead.cfm',	
			{
				id : id_record,
				peghead_size : $('#pegheadsize_' + id_record).val()
			},function(response){}
		);
		
	}
	
	function toggle_creditcard(){
		
		if( $('#payment_method').val() == 'creditcard' ){
			$('#creditcard_info').show(animation_speed);
		} else {
			$('#creditcard_info').hide(animation_speed);
		}
		
	}


// Item Modification/Addition Scripts

	function item_modify(id_record){
		createPopUp(500, 270, true, false, 'center', this, 'modify_' + id_record, true, '/item/modify/?id_record=' + id_record);
	}
	
	function item_variation(id_record){
		createPopUp(500, 270, true, false, 'center', this, 'modify_' + id_record, true, '/item/add/?id_record=' + id_record);
	}
