function load_states(form_name) {
	var f = eval('document.'+form_name);
	
	f.state_list.length=0;
	
	f.state_list.options[0] = new Option('Loading list of states/provinces...','');
	
	create_xmlhttp_request();
	xmlhttp.open("GET",'/cgi-bin/customers/account_data.cgi?command=list states&country='+f.country.value);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
				var e = xmlhttp.responseXML.getElementsByTagName('error')[0].firstChild.nodeValue;
				
				if (e == 0) {
					var jsonText = xmlhttp.responseXML.getElementsByTagName('content')[0].firstChild.data;
					var s = eval('(' + jsonText + ')');
			
					if (s.num_results > 0) {
						f.state_list.style.display	='';
						f.state.style.display		='none';
						
						f.state_list.options[0] = new Option('Please select a state/provice','');
						for (var x=0; x<s.data.length; x++){
							f.state_list.options[f.state_list.options.length] = new Option (s.data[x].state,s.data[x].abv);
						}
						if (f.state.value != '') {
							f.state_list.value = f.state.value;
						}
					}
					else {
						f.state_list.style.display='none';
						f.state.style.display='';
					}
				}
				else {
					alert('Error processing AJAX request: '+e);
				}
			}
		}
	}
	xmlhttp.send(null);
}

function set_state(form_name) {
	var f = eval('document.'+form_name);
	f.state.value = f.state_list.value;
	
	if (f.state_list.value == '') {
		alert('Please select a valid state.');
	}
}

function load_search_results(s,offset){
	create_xmlhttp_request(1);
	
	if (!offset) { offset = 0; }
	
	var o_name;
	
	if (offset == 'ALL') 	{ o_name = 'showall'; }
	else					{ o_name = 'offset'; } 

	xmlhttp2.open("GET",'/cgi-bin/customers/products_results.cgi?command=search&search=' + s + '&'+ o_name +'='+offset);
	xmlhttp2.onreadystatechange=function() {
		if (xmlhttp2.readyState==4) {
			if (xmlhttp2.status==200) {
			
				var o = document.getElementById('product_results_div');
				o.innerHTML=xmlhttp2.responseXML.getElementsByTagName('content')[0].firstChild.data;
				
				var c = xmlhttp2.responseXML.getElementsByTagName('command')[0].firstChild.nodeValue;
				
				document.getElementById('pagenav').innerHTML=xmlhttp2.responseXML.getElementsByTagName('pagenav')[0].firstChild.data;
				
				// create drag and drop objects
				var p =eval('(' + xmlhttp2.responseXML.getElementsByTagName('products')[0].firstChild.data + ')');
				
				if (p.parent_category_id > 0 && c != 'search') {
					document.getElementById('sub_cat_nav').innerHTML='&gt; '+ p.category_name;
				}
	
			}
		}

	}
	xmlhttp2.send(null);
}

function login(){
	var f = document.login_form;
	
	if (f.login_email.value == '') {
		alert('You email address is required to login!');
		f.login_email.focus();
		return false;
	}
	else if (f.login_password.value.length < 6) {
		alert('Password must be at least 6 characters long.');
		f.login_password.focus();
		return false;
	}
	return true;
}

function load_category_list () {
			create_xmlhttp_request(1);
	
			xmlhttp2.open("GET",'/cgi-bin/customers/products.cgi?command=list categories');
			xmlhttp2.onreadystatechange=confirm_load_category_list;
			xmlhttp2.send(null);
}

function confirm_load_category_list () {
	if (xmlhttp2.readyState==4) {
		if (xmlhttp2.status==200) {
			var o = document.getElementById('categories_list');
			o.innerHTML=xmlhttp2.responseXML.getElementsByTagName('content')[0].firstChild.data;
		}
	}
}

function load_product_list (cid,offset) {
	if (!offset) { offset = 0; }

	var o_name;

	if (offset == 'ALL') 	{ o_name = 'showall'; }
	else					{ o_name = 'offset'; } 
	
	create_xmlhttp_request();
	xmlhttp.open("GET",'/cgi-bin/customers/products_results.cgi?command=list products&category_id=' + cid + '&'+ o_name + '='+offset);
	
	xmlhttp.onreadystatechange=confirm_load_product_list;
	xmlhttp.send(null);
}

function confirm_load_product_list () {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
		
			var o = document.getElementById('product_results_div');
			o.innerHTML=xmlhttp.responseXML.getElementsByTagName('content')[0].firstChild.data;
			
			var c = xmlhttp.responseXML.getElementsByTagName('command')[0].firstChild.nodeValue;
			
			document.getElementById('pagenav').innerHTML=xmlhttp.responseXML.getElementsByTagName('pagenav')[0].firstChild.data;
			
			var p =eval('(' + xmlhttp.responseXML.getElementsByTagName('products')[0].firstChild.data + ')');
			
			if (p.parent_category_id > 0 && c != 'search') {
				document.getElementById('sub_cat_nav').innerHTML='&gt; '+ p.category_name;
			}
		}
	}
}



function load_product_list_category (cid, offset) {
	if (!offset) { offset = 0; }
	var o_name;

	if (offset == 'ALL') 	{ 
		o_name = 'showall'; 
	}
	else					{ o_name = 'offset'; } 
	
	create_xmlhttp_request();

	xmlhttp.open("GET",'/cgi-bin/customers/products_results.cgi?command=list products category&category_id=' + cid + '&'+ o_name + '='+offset);
	xmlhttp.onreadystatechange=confirm_load_product_list;
	xmlhttp.send(null);

}

function initScrollLayer_cart() {
	// arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
	// if horizontal scrolling, id of element containing scrolling content (table?)   , 'carttable'
	var wndo = new dw_scrollObj('cartwindow', 'cartlayer');
	
	// pass id's of any wndo's that scroll inside tables
	// i.e., if you have 3 (with id's wn1, wn1, cartwindow): dw_scrollObj.GeckoTableBugFix('wn1', 'wn1', 'cartwindow');
	dw_scrollObj.GeckoTableBugFix('cartwindow'); 
}



function load_product (pid) {
	create_xmlhttp_request();

	xmlhttp.open("GET",'/cgi-bin/customers/products.cgi?command=get product&product_id=' + pid);
	xmlhttp.onreadystatechange=confirm_load_product;
	xmlhttp.send(null);
}

function confirm_load_product () {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var o = document.getElementById('product_results_div');
			o.innerHTML=xmlhttp.responseXML.getElementsByTagName('content')[0].firstChild.data;
		}
	}
}

