//-------------------------------------------------------------------
// When a state menu is changed, update the country menu to the corresponding country...
//-------------------------------------------------------------------
function js_update_country_menu(state_id,state_value) {
	var state_value_array = state_value.split('_');
	var country_id = js_str_replace('_state', '_country', state_id);
	if(document.getElementById(country_id).value != null) {
		document.getElementById(country_id).value = state_value_array[1];
	} 
} // end function

//-------------------------------------------------------------------
// Open the ship address editor in a modal window...
// USED BY:
//		f_storefront.php
//-------------------------------------------------------------------
function js_open_ship_address_editor(menu_id, form_id, opener_bn) {
	var sai_value = document.getElementById('ship_address_menu_'+menu_id).value;
	if(sai_value == -2) {
		//This version will open the editor in a thickbox modal...
		//tb_show(null,'ship_address_editor.php?sai='+sai_value+'&ship_address_menu_id='+menu_id+'&form_id='+form_id+'&KeepThis=true&TB_iframe=true&width=750&height=550&modal=true',null);
		//This version will open the editor in a standard popup window...
		window.open('ship_address_editor.php?form_id='+form_id+'&opener_bn='+opener_bn,'',' resizable=yes, scrollbars=yes, menubar=yes, width=550,height=550');
	}	
} // end function


//-------------------------------------------------------------------
// Open the digital delivery player in a modal window...
// USED BY:
//		DIGITAL-FILE-ACCESS-BUTTON
//-------------------------------------------------------------------
function js_digdel_modal_player(digdel_order_item_id) {
	tb_show(null,'digdel_modal.php?digdel_order_item_id='+digdel_order_item_id+'&KeepThis=true&TB_iframe=true&width=760&height=570&modal=true',null);
} // end function


//-------------------------------------------------------------------
// Open the digital delivery player in a modal window...
// USED BY:
//		DIGITAL-FILE-ACCESS-BUTTON
//-------------------------------------------------------------------
function js_digdel_tped_window(digdel_order_item_id) {
	tb_show(null,'digdel_tped.php?digdel_order_item_id='+digdel_order_item_id+'&KeepThis=true&TB_iframe=true&width=700&height=350&modal=true',null);
} // end function

//-------------------------------------------------------------------
// Submit a form...
// USED BY:
//		f_storefront.php
//-------------------------------------------------------------------
function js_submit_form(menu_id, form_id) {
	var sai_value = document.getElementById('ship_address_menu_'+menu_id).value;
	if(sai_value > 0) {
		document.getElementById(form_id).submit();
	}
} // end function


//-------------------------------------------------------------------
// 1. Obtains the shipping rates from ajax/get_shipping_rates.php, 
//		for use in both the estimate and checkout areas.
// 2. Calls the ajax/render_shipping_rates.php to display the rates on-screen.
// USED BY:
//		js_storefront.js (7 times)
//		SHIPPING-ENGINE-BLOCK-END
//		(The ship_country menu calls this via js-update-sail().)
//-------------------------------------------------------------------
function js_get_shipping_rates() { 
	//alert('line 47');
	//disableElement('shipSubmitButton'); 
	//toggleShipButtonClass('SHIPPING-OPTIONS-SUBMIT-BUTTON-DISABLED');  
	var get_shipping_rates_data=new Array();

	if((document.getElementById('ship_est_zip') != null) && (document.getElementById('ship_est_country') != null)) {
		if((document.getElementById('ship_est_zip').value == '') || (document.getElementById('ship_est_country').value == '')) {
			//alert('No zip code and/or country provided. Could not get ship estimate.');
			return;
		} else {
		//	alert('Ship Estimate zip: '+document.getElementById('ship_est_zip').value+' | Ship Estimate country: '+document.getElementById('ship_est_country').value);
		}
		get_shipping_rates_data[0] = 'estimate';
		get_shipping_rates_data[1] = document.getElementById('ship_est_zip').value;
		get_shipping_rates_data[2] = document.getElementById('ship_est_country').value;
	} else {
		get_shipping_rates_data[0] = 'sa_arr';
	}	

	if(document.getElementById('shipping_engine_block_span') != null) {
		document.getElementById('shipping_engine_block_span').innerHTML = '<br /><br /><img src="img/spinner.gif" width="16" height="16" align="absmiddle" />&nbsp;&nbsp;<i>... Processing the request for shipping rates...</i><input name="submit_shipping_options" id="submit_shipping_options" value="" type="hidden"><br /><br />'; 
		var js_get_shipping_rates_target = 'shipping_engine_block_span';
		//alert('line 69');

		if(document.getElementById('go_to_next_page') != null) {
			document.getElementById('go_to_next_page').disabled = true;
		}

	} else {
		var js_get_shipping_rates_target = 'null';
		//alert('line 72');
	}
	//var my_num = 0;
	//alert('line 75');
	js_ajax(get_shipping_rates_data, 'ajax/get_shipping_rates.php', 'null', 'js_ajax(\'null\', \'ajax/render_shipping_rates.php\', \''+js_get_shipping_rates_target+'\')'); //to test, change "null" to js_get_shipping_rates_target and comment out next line below.
	
	//alert(document.getElementById('shipping_input_test').value);
			
	//js_ajax('null', 'ajax/render_shipping_rates.php', js_get_shipping_rates_target); //Now called from the js_ajax call above, to prevent drawing the ship rates before the SESSION var is set.
	//alert('my_num = '+my_num);
		
} // end function 


//-------------------------------------------------------------------
// Update the value of values of the set of ship address menus...
// USED BY:
//		f_storefront.php
//-------------------------------------------------------------------
function js_update_ship_fields(menu_id) {
	var sai_value = document.getElementById('ship_address_menu_'+menu_id).value;
	
	//alert('js_storefront.php message 91: sai_value = '+sai_value);

	if(sai_value < 1) {
		if(document.getElementById('ship_sai') != null) { document.getElementById('ship_sai').value = 'new'; }
		if(document.getElementById('ship_first_name') != null) { document.getElementById('ship_first_name').value = ''; }
		if(document.getElementById('ship_last_name') != null) { document.getElementById('ship_last_name').value = ''; }
		if(document.getElementById('ship_address_1') != null) { document.getElementById('ship_address_1').value = ''; }
		if(document.getElementById('ship_address_2') != null) { document.getElementById('ship_address_2').value = ''; }
		if(document.getElementById('ship_city') != null) { document.getElementById('ship_city').value = ''; }
		if(document.getElementById('ship_state') != null) { document.getElementById('ship_state').value = ''; }
		if(document.getElementById('ship_zip') != null) { document.getElementById('ship_zip').value = ''; }
		if(document.getElementById('ship_country') != null) { document.getElementById('ship_country').value = ''; }
	} else {
		
		//Are these used any more, since the field id format is now "ship_xxxxx_SAI#" ?
		if(document.getElementById('ship_sai') != null) {
			document.getElementById('ship_sai').value = ship_sai[sai_value];
		}	
		if(document.getElementById('ship_first_name') != null) {
			document.getElementById('ship_first_name').value = ship_first_name[sai_value];
		}		
		if(document.getElementById('ship_last_name') != null) {
			document.getElementById('ship_last_name').value = ship_last_name[sai_value];
		}	
		if(document.getElementById('ship_address_1') != null) {
			document.getElementById('ship_address_1').value = ship_address_1[sai_value];
		}			
		if(document.getElementById('ship_address_2') != null) {
			document.getElementById('ship_address_2').value = ship_address_2[sai_value];
		}			
		if(document.getElementById('ship_city') != null) {
			document.getElementById('ship_city').value = ship_city[sai_value];
		}			
		if(document.getElementById('ship_state') != null) {
			document.getElementById('ship_state').value = ship_state[sai_value];
		}			
		if(document.getElementById('ship_zip') != null) {
			document.getElementById('ship_zip').value = ship_zip[sai_value];
		}					
		if(document.getElementById('ship_country') != null) {
			document.getElementById('ship_country').value = ship_country[sai_value];
		}	
		
	} // end if sai_value < 1... else...	
	
} // end function


//-------------------------------------------------------------------
// Locates all instances of a ship_address_menu on the page, and then
// rebuilds it to ensure all new/edited ship addresses are present in it.
// 2011-11-08: Changed all references of "parent." to "opener." so ship address menus would reload themselves again.
//-------------------------------------------------------------------
function js_rebuild_ship_address_menus(sai) {
	if(opener && typeof opener.document != 'undefined') {
		var allTags = opener.document.body.getElementsByTagName('*'); //xxxx "parent." will only need to be present in certain circumstances
	} else {
		var allTags = document.body.getElementsByTagName('*');
	}
	var ids = [];
	for (var tg = 0; tg< allTags.length; tg++) {
		var tag = allTags[tg];
		if (tag.id) {
			//alert('js_storefront.js message 143: tag.id = '+tag.id+' | document.URL = '+document.URL);
			if(js_strpos(tag.id, 'ship_address_menu_span_')!==false) {			
				//alert('js_storefront.js message 147: ship_address_menu: tag.id = '+tag.id+' | tag.value = '+tag.value);
				var instance_id = js_str_replace('ship_address_menu_span_', '', tag.id);
				
				if(opener && typeof opener.document != 'undefined' && opener.document.getElementById('ship_address_menu_'+instance_id).value == '-2') {
					
					//alert('js_storefront.js message 147: ship_address_menu value was '+parent.document.getElementById('ship_address_menu_'+instance_id).value+', changed it to '+document.getElementById('ship_sai_'+sai).value);
					
					//parent.document.getElementById('ship_address_menu_'+instance_id).value = sai; //In dev...
					var selected_value = sai;
				} else if(document.getElementById('ship_address_menu_'+instance_id) != null && document.getElementById('ship_address_menu_'+instance_id).value == '-2') {
					var selected_value = sai;
				} else { 		
					if(opener && typeof opener.document != 'undefined') {						
						var selected_value = opener.document.getElementById('ship_address_menu_'+instance_id).value;	
					} else {
						var selected_value = document.getElementById('ship_address_menu_'+instance_id).value;	
					}
				} // end if tag.value == -2... else...
				//alert('js_storefront.js message 147: instance_id = '+instance_id+' | selected_value = '+selected_value);

				js_ajax_build_sail_menu(instance_id, selected_value, 'parent');			
			} // strpos
		} // end if
	} // end for loop
	//alert(ids);		
	
} // end function


//-------------------------------------------------------------------
// Calls ajax/build_sail_menu.php to build the SAIL menu...
// USED BY:
//		ship_address_editor.php
//		session_handler.php
//		FIELD-SHIP-EDITOR-CLOSE-BUTTON
//		template_packages\xxxxxx\ship_address_editor.template.php //xxxx Does this need to be updated/removed? Moved to a QC tag?
// 2011-11-08: Changed all references of "parent." to "opener." so ship address menus would reload themselves again.
//-------------------------------------------------------------------
function js_ajax_build_sail_menu(ship_address_menu_id, sai, window_target) {
	//alert('ship_address_menu_id = '+ship_address_menu_id+' | sai = '+sai+' | document.URL = '+document.URL);
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) {
		alert('Request failed: This browser does not support HTTP Requests.');
		return;
	}
	var AjaxURL='ajax/build_sail_menu.php?checksum='+Math.random();
	    AjaxURL=AjaxURL+'&ship_address_menu_id='+ship_address_menu_id;	
	    AjaxURL=AjaxURL+'&sai='+sai;

	ajax_v_general.onreadystatechange=function() {
		if (ajax_v_general.readyState==4 || ajax_v_general.readyState=='complete') {
			//if(window_target == 'parent') {
			if(opener && typeof opener.document != 'undefined') {	
				opener.document.getElementById('ship_address_menu_span_'+ship_address_menu_id).innerHTML=ajax_v_general.responseText;	
			} else {
				document.getElementById('ship_address_menu_span_'+ship_address_menu_id).innerHTML=ajax_v_general.responseText;	
			}
		}
	}
	ajax_v_general.open('GET',AjaxURL,true);
	ajax_v_general.send(null);
	
} // end function


//-------------------------------------------------------------------
// Update the ship menus that currently have a value of "-1"
//-------------------------------------------------------------------
function js_update_unselected_ship_address_menus(sai) {
	var allTags = document.body.getElementsByTagName('*');
	var ids = [];
	for (var tg = 0; tg< allTags.length; tg++) {
		var tag = allTags[tg];
		if (tag.id) {
			if((js_strpos(tag.id, 'ship_address_menu_')!==false) && (js_strpos(tag.id, '_span_')===false)) {			
				//ids.push(tag.id);
				//alert('ship_address_menu value = '+tag.value);
				if(tag.value == '-1') {
					document.getElementById(tag.id).value = sai;
				} // end if tag.value == -1				
			} // strpos
		} // end if
	} // end for loop
	//alert(ids);	
} // end function



var ajax_sail_throttled_count = 0; //Set the global "ajax counter" var. AJAX will only connect to the .php file when ajax_counter == 0
var ajax_sail_throttled_delay = 750; //Set the AJAX delay. 1000 = one second

//Prep the AJAX data and forward it along to the trigger function...
function js_update_sail_throttled(sai,field_id,field_value) {
	//alert('js_update_sail_throttled(): sai = '+sai+' | field_id = '+field_id+' | field_value = '+field_value);
	ajax_sail_throttled_count = ajax_sail_throttled_count+1;
	setTimeout("js_ajax_sail_throttled_trigger("+ajax_sail_throttled_count+", '"+sai+"', '"+field_id+"', '"+field_value+"')", ajax_sail_throttled_delay);
} // end function

//Forward the AJAX request along only when ajax_counter == 0 (ie: When the user is done typing.)
function js_ajax_sail_throttled_trigger(currCount, sai, field_id, field_value) {
   //alert('js_ajax_sail_throttled_trigger(): sai = '+sai+' | field_id = '+field_id+' | field_value = '+field_value);
   if(currCount == ajax_sail_throttled_count) {
      ajax_sail_throttled_count = 0;
      //alert('Line 216: Calling js_ajax() function!');
      js_update_sail(sai,field_id,field_value);
   }
}




//-------------------------------------------------------------------
// Updates the [sail] SESSION var, either:
//		1. If the sai < 1, the sai is set to time() and ADDED,
//		2. If the sai > 0, the sai is UPDATED.
// USED BY:
//		f_storefront.php (for ship_state and ship_country menus)
//		FIELD-SHIP-ZIP
//-------------------------------------------------------------------
function js_update_sail(sai,field_id,field_value) {
	//alert('js_update_sail(): sai = '+sai+' | field_id = '+field_id+' | field_value = '+field_value);
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) {
		alert('Request failed: This browser does not support HTTP Requests.');
		return;
	}

	//alert('sai = '+sai+' | field_id = '+field_id+' | field_value = '+field_value);

	/*
	//Not every country has zip codes.
	if((field_id == 'ship_zip') && (field_value.length < 4)) {
		return;	
	}
	*/

	var feedback = '';

	// ajax/update_sail.php updates the $sai in various session arrays...
	var AjaxURL='ajax/update_sail.php?checksum='+Math.random();
	    AjaxURL=AjaxURL+'&sai='+sai;
	    AjaxURL=AjaxURL+'&id='+sai;

	//make an array of all the element IDs below...
	var form_fields = new Array('ship_sai', 'ship_first_name', 'ship_last_name', 'ship_address_1', 'ship_address_2', 'ship_city', 'ship_state', 'ship_zip', 'ship_country');
	
	//loop through the array to create the AjaxURL...
	for(var i=0,len=form_fields.length; field_name=form_fields[i], i<len; i++) {
		if(document.getElementById(field_name+'_'+sai) != null) { 
			AjaxURL=AjaxURL+'&'+field_name+'='+js_urlencode(document.getElementById(field_name+'_'+sai).value);
		} else if(document.getElementById(field_name+'_-2') != null) {
			//The -2 is needed because when adding a new ship address from the ship addr editor modal
			//the sai in the session will switch from -2 to time() after the first call to ja_update_sail().
			//However, all of the form fields on the page will have the ID number -2. 
			AjaxURL=AjaxURL+'&'+field_name+'='+js_urlencode(document.getElementById(field_name+'_-2').value);
		} else { 
			feedback = feedback+field_name.value+' not found. '; 
		}
	} // end for loop
	
	//if(feedback == '') { feedback = 'All shipping fields were found.'; } alert('Line 230:\nAjaxURL = '+AjaxURL+'\n\nfeedback = '+feedback);
	
	if(document.getElementById('ship_sai_'+sai)!=null) {
		var tmp_sai = sai;
	} else if(document.getElementById('ship_sai_-2')!=null) {
		var tmp_sai = '-2';
	} else {
		alert('js_storefront.js message 243: tmp_sai could not be determined.');	
	}
	
	//alert('line 246 | sai = '+sai+' | tmp_sai = '+tmp_sai);
	
	if((document.getElementById('ship_sai'+'_'+tmp_sai)!=null) && (ajax_v_general!=null)) { 
		ajax_v_general.onreadystatechange=function() {
			
			//alert('line 256 | sai = '+sai+' | tmp_sai = '+tmp_sai);

			if (ajax_v_general.readyState==4) {
				// 4 = "loaded"
				if (ajax_v_general.status==200) {
					// 200 = OK
					//alert('js_trim(ajax_v_general.responseText) = '+js_trim(ajax_v_general.responseText));
					document.getElementById('ship_sai'+'_'+tmp_sai).value = js_trim(ajax_v_general.responseText);	
					
					//alert('js_storefront.js line 242: field_id = '+field_id);
					
					if((js_strpos(field_id, 'ship_state_')!==false) || (js_strpos(field_id, 'ship_zip_')!==false) || (js_strpos(field_id, 'ship_country_')!==false) || (js_strpos(field_id, 'ship_address_menu_')!==false)) {
											
						//alert('line 264 | sai = '+sai+' | tmp_sai = '+tmp_sai);
						
						//Update the ship menus that currently have a value of "-1"
						js_update_unselected_ship_address_menus(sai);
						
						//Calculate and render shipping rates...
						js_get_shipping_rates();		
					
						js_update_tax_spans(); //Update the tax SESSION data and span tags.
						//js_update_order_grand_total_span();	//Update the grand_total SESSION data and span tags.					
					
					
					} // end if editing ship state or country or zip
					
		    } else {
		    	//alert('js_storefront.js message 300: Problem retrieving XML data: ajax_v_general.status = '+ajax_v_general.status);
		    }
		    
		  } // end if readystate==4
		
		} // end ajax_v_general.onreadystatechange=function()
		
		ajax_v_general.open('GET',AjaxURL,true);
		ajax_v_general.send(null);		

	} //else { alert('document.getElementById(ship_sai) == null'); }

	//alert('document.getElementById(ship_sai).value = '+document.getElementById('ship_sai').value+' | js_trim(ajax_v_general.responseText) = '+js_trim(ajax_v_general.responseText));	
	
	//update all shipping address menus on the page with the latest SAIL data...
	js_rebuild_ship_address_menus(sai);
	
} // end function


//-------------------------------------------------------------------
// 1. Calls ajax/refresh-ship-values.php
//	2. Updates order_shipping_value_span_X based on data returned by ajax file.
//	3. Updates order_shipping_name_span_X based on data returned by ajax file.
// 4. Calls js-update-tax-spans() to update tax display in basket.
// 5. Updates order_grand_total_span based on data returned by ajax file.
// USED BY:
//		ajax\render_shipping_rates.php
//-------------------------------------------------------------------
function js_update_ship_spans(ship_selection_value) {
	//alert('ship_selection_value = '+ship_selection_value);
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) { alert('Request failed: This browser does not support HTTP Requests.'); return; }
	var AjaxURL='ajax/refresh_ship_values.php?ship_selection_value='+ship_selection_value;
	ajax_v_general.onreadystatechange=function() {
		//alert('js-handle-shipping-selection() line 1');
		if (ajax_v_general.readyState==4 || ajax_v_general.readyState=='complete') {
			//alert('js-handle-shipping-selection() line 2');
			var xmlDoc = js_load_xml_string(ajax_v_general.responseText);	
			var x=xmlDoc.getElementsByTagName("ship_item");
			for (i=0;i<x.length;i++) {
			 	sai = x[i].getElementsByTagName("ship_sai")[0].childNodes[0].nodeValue;
			 	ship_name = js_urldecode(x[i].getElementsByTagName("ship_name")[0].childNodes[0].nodeValue);
			 	ship_value = x[i].getElementsByTagName("ship_value")[0].childNodes[0].nodeValue;
			 	m_sign = js_urldecode(x[i].getElementsByTagName("m_sign")[0].childNodes[0].nodeValue);
			 
				if(document.getElementById('order_shipping_value_span_'+js_trim(sai)) != null) { 
					document.getElementById('order_shipping_value_span_'+js_trim(sai)).innerHTML=js_trim(m_sign)+Number(js_trim(ship_value)).toFixed(2);	
				} else if(document.getElementById('order_shipping_value_span_0') != null) { 
					document.getElementById('order_shipping_value_span_0').innerHTML=js_trim(m_sign)+Number(js_trim(ship_value)).toFixed(2);	
				} else { 
					//Useful for debugging...
					//alert('order_shipping_value_span_0 is null! (hardcoded zero)'); 
				}	 	
			 
			 	if(document.getElementById('order_shipping_name_span_'+js_trim(sai)) != null) { 
					document.getElementById('order_shipping_name_span_'+js_trim(sai)).innerHTML=ship_name;	
				} else if(document.getElementById('order_shipping_name_span_0') != null) { 
					document.getElementById('order_shipping_name_span_0').innerHTML=ship_name;	
				} else {
					//Useful for debugging...
					//alert('order_shipping_name_span_0 is null! (hardcoded zero)'); 
				}
		 
		  } // end for loop
		  
			js_update_tax_spans(); //Update the tax SESSION data and span tags.
			//js_update_order_grand_total_span();	//Update the grand_total SESSION data and span tags.	
			
					  
		} // end if readystate==4
	} // end ajax_v_general.onreadystatechange=function
	ajax_v_general.open('GET',AjaxURL,true);
	ajax_v_general.send(null);
} // end function


//-------------------------------------------------------------------
// Update all the order_tax_value_span_X tags on the page...
//		<td colspan="3" class="JUSTIFY-RIGHT DASH-TOP"><span id="order_tax_names_span_0"><i>No taxes yet applied to this address.</i></span></td>
//		<td class="JUSTIFY-RIGHT DASH-TOP"><span id="order_tax_value_span_0">&#36;0.00</span></td>
//-------------------------------------------------------------------
function js_update_tax_spans() {
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) { alert('Request failed: This browser does not support HTTP Requests.'); return; }
	var AjaxURL='ajax/refresh_tax_values.php';
	ajax_v_general.onreadystatechange=function() {
		//alert('js-update-tax-spans() line 1');
		if (ajax_v_general.readyState==4 || ajax_v_general.readyState=='complete') {
			//alert('js-update-tax-spans() line 2');
			//alert('ajax_v_general.responseText = '+ajax_v_general.responseText);
			var xmlDoc = js_load_xml_string(ajax_v_general.responseText);	
			//alert('xmlDoc = '+xmlDoc);
			var x=xmlDoc.getElementsByTagName("tax_item");
			//alert('x = '+x);
			for (i=0;i<x.length;i++) {
			 	sai = x[i].getElementsByTagName("tax_sai")[0].childNodes[0].nodeValue;
			 	tax_name = js_urldecode(x[i].getElementsByTagName("tax_name")[0].childNodes[0].nodeValue);
			 	tax_value = x[i].getElementsByTagName("tax_value")[0].childNodes[0].nodeValue;
			 	m_sign = js_urldecode(x[i].getElementsByTagName("m_sign")[0].childNodes[0].nodeValue);
			 	//alert('sai = '+sai+' | tax_name = '+tax_name+'+ | tax value = '+tax_value);		 
				if(document.getElementById('order_tax_value_span_'+js_trim(sai)) != null) { 
					document.getElementById('order_tax_value_span_'+js_trim(sai)).innerHTML=js_trim(m_sign)+Number(js_trim(tax_value)).toFixed(2);	
				} else if(document.getElementById('order_tax_value_span_0') != null) { 
					document.getElementById('order_tax_value_span_0').innerHTML=js_trim(m_sign)+Number(js_trim(tax_value)).toFixed(2);	
				} else { 
					//Some pages, like the customer account area, will not have the tax span.
					//alert('order_tax_value_span_0 is null! (hardcoded zero)'); 
				}	 	
			 
			 	if(document.getElementById('order_tax_names_span_'+js_trim(sai)) != null) { 
					document.getElementById('order_tax_names_span_'+js_trim(sai)).innerHTML=tax_name;	
				} else if(document.getElementById('order_tax_names_span_0') != null) { 
					document.getElementById('order_tax_names_span_0').innerHTML=tax_name;	
				} else { 
					//Some pages, like the customer account area, will not have the tax span.
					//alert('order_tax_names_span_0 is null! (hardcoded zero)'); 
				}
		 
		  } // end for loop
		  js_update_order_grand_total_span();	//Update the grand_total SESSION data and span tags.	
		
		} // end if readystate==4
	} // end ajax_v_general.onreadystatechange=function
	ajax_v_general.open('GET',AjaxURL,true);
	ajax_v_general.send(null);
} // end function

//-------------------------------------------------------------------
// Update all the order_grand_total_span tag on the page...
//-------------------------------------------------------------------
function js_update_order_grand_total_span() {
	//alert('js_storefront.php message 467: starting js_update_order_grand_total_span()');
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) { alert('Request failed: This browser does not support HTTP Requests.'); return; }
	var AjaxURL='ajax/refresh_order_grand_total.php';
	ajax_v_general.onreadystatechange=function() {
		//alert('js-update-order-grand-total-span() line 1');
		if (ajax_v_general.readyState==4 || ajax_v_general.readyState=='complete') {
			//alert('js-update-order-grand-total-span() ajax_v_general.responseText:\n\n'+ajax_v_general.responseText);
			var xmlDoc = js_load_xml_string(ajax_v_general.responseText);	
			var x=xmlDoc.getElementsByTagName("grand_total_data");
			 	
		 	if(x[0] != undefined) {
		 		grand_total = x[0].getElementsByTagName("grand_total")[0].childNodes[0].nodeValue;
		 		m_sign = js_urldecode(x[0].getElementsByTagName("m_sign")[0].childNodes[0].nodeValue);
		 		adjustment = js_urldecode(x[0].getElementsByTagName("adjustment")[0].childNodes[0].nodeValue);
		 		
		 		if(document.getElementById('coupon_value_span') != null) {
		 			document.getElementById('coupon_value_span').innerHTML=js_trim(m_sign)+js_number_format(js_trim(adjustment),2,'.',',');
		 		}
		 		
			 	//alert('js-update-order-grand-total-span(): \n\ngrand_total = '+grand_total+'\n\nformatted grand_total = '+js_trim(m_sign)+Number(js_trim(grand_total)).toFixed(2)+'\n\ndocument.getElementById(order_grand_total_span).innerHTML = '+document.getElementById('order_grand_total_span').innerHTML);
				
				//Old way. Would only update one instance of the grand total span on the page. The new way updates ALL instances.
				//if(document.getElementById('order_grand_total_span') != null) { 
					//document.getElementById('order_grand_total_span').innerHTML=js_trim(m_sign)+Number(js_trim(grand_total)).toFixed(2);

				var allTags = document.getElementsByTagName('*');
				var ids = [];
				for (var tg = 0; tg< allTags.length; tg++) {
					var tag = allTags[tg];
					if((tag.id) && (js_strpos(tag.id, 'order_grand_total_span')!==false)) {		
						//alert('js-update-order-grand-total-span(): tag.id = '+tag.id);	
						document.getElementById(tag.id).innerHTML=js_trim(m_sign)+js_number_format(js_trim(grand_total),2,'.',',');
						//before js_number_format() was used, number was formatted by: Number(js_trim(grand_total)).toFixed(2);
					} // strpos
				} // end for loop		
				
			} // end if(typeof(x[0]) != undefined) //Added 2011-11-08	

				//} // end if !null
		} // end if readystate==4
	} // end ajax_v_general.onreadystatechange=function
	ajax_v_general.open('GET',AjaxURL,true);
	ajax_v_general.send(null);
} // end function



//-------------------------------------------------------------------
// 1. Calls ajax/update_cmr_arr.php to update the cmr data...
// 2. Parses the XML returned from the ajax file...
// 3. Calls js-update-tax-spans() to update the display of the taxes...
// 4. Updates values of all billing-related form fields...
// 5. Updates display of order_grand_total_span span tag...
// USED BY:
//		f_storefront.php (for bill_state and bill_country menus)
//		FIELD-BILL-ZIP
//-------------------------------------------------------------------
function js_update_cmr_arr(field_id, field_value) {
	var ajax_v_general;
	ajax_v_general=js_get_xml_http_object();
	if (ajax_v_general==null) {
		alert('Request failed: This browser does not support HTTP Requests.');
		return;
	}
	
	/*
	//Not every country has zip codes.
	if((field_id == 'bill_zip') && (field_value.length < 4)) {
		return;	
	}
	*/

	// ajax/update_cmr_arr.php rebuilds the SESSION data, then echoes XML. 
	var AjaxURL='ajax/update_cmr_arr.php?'+field_id+'='+field_value+'&checksum='+Math.random();
	
	//make an array of all the element IDs below...
	var form_fields = new Array('bill_sai', 'bill_first_name', 'bill_last_name', 'bill_address_1', 'bill_address_2', 'bill_city', 'bill_state', 'bill_zip', 'bill_country');
	var feedback = '';
	
	//loop through the array to create the AjaxURL...
	for(var i=0,len=form_fields.length; field_name=form_fields[i], i<len; i++) {
		if(document.getElementById(field_name) != null) { 
			AjaxURL=AjaxURL+'&'+field_name+'='+document.getElementById(field_name).value;
		} else { 
			feedback = feedback+field_name.value+' not found. '; 
		}
	} // end for loop

	ajax_v_general.onreadystatechange=function() {
		//alert('js-update-cmr-arr() line 1');
		if (ajax_v_general.readyState==4 || ajax_v_general.readyState=='complete') {
			//alert('js-update-cmr-arr() line 2');	

			if((field_id=='bill_state') || (field_id=='bill_zip') || (field_id=='bill_country')) {
				js_update_tax_spans(); //Update the tax SESSION data and span tags.
				//js_update_order_grand_total_span();	//Update the grand_total SESSION data and span tags.					
			} // end if editing bill state or country or zip			
		} // end if readystate==4
	} // end ajax_v_general.onreadystatechange=function()
	ajax_v_general.open('GET',AjaxURL,true);
	ajax_v_general.send(null);
	
} // end function
//-------------------------------------------------------------------

//-------------------------------------------------------------------	
// toggle the product review details when clicked.
function js_toggle_review(review_id) {
	if(document.getElementById('review_'+review_id+'_detail_span').style.display != '') {
		document.getElementById('review_'+review_id+'_summary_span').style.display = 'none';
		document.getElementById('review_'+review_id+'_detail_span').style.display = '';
		document.getElementById('review_'+review_id+'_toggle_span').innerHTML = '<a href="#" onclick="js_toggle_review(\''+review_id+'\'); return false;" />Show less.</a>';
	} else {
		document.getElementById('review_'+review_id+'_summary_span').style.display = '';
		document.getElementById('review_'+review_id+'_detail_span').style.display = 'none';	
		document.getElementById('review_'+review_id+'_toggle_span').innerHTML = '<a href="#" onclick="js_toggle_review(\''+review_id+'\'); return false;" />Show more.</a>';
	}
} // end function
//-------------------------------------------------------------------
