var current_file_path;
var lock_image = 0;

function update_site_info() {
	var f = document.account_mgt_form;
	create_xmlhttp_request();
	
	//check FQDN for appropriate number of elements
	var a = f.host_site.value.split('.');
	
	if (a.length < 3){
		alert('Must use a fully qualified domain name for your site. For example: linens.your-domain.com');
		f.host_site.focus();
		return false;
	}
	
	// update affiliate date data
	var save_string = 
		'command='+f.command.value +
		'&site_id='+f.site_id.value +
		'&host_site=' + f.host_site.value +
		'&intro_text=' + escape(f.intro_text.value) +
		'&about_us=' + escape(f.about_us.value) +
		'&featured_product_id=' + f.featured_product_id.value;
		
	xmlhttp2.open("POST",'/cgi-bin/customers/account_data.cgi', true);
	xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp2.setRequestHeader("Content-length", save_string.length);
	xmlhttp2.setRequestHeader("Connection", "close");
	xmlhttp2.onreadystatechange=function () {
		if (xmlhttp2.readyState==4) {
			if (xmlhttp2.status==200) {
				var f = document.account_mgt_form;
				var jsonText 	= xmlhttp2.responseXML.getElementsByTagName('json_str')[0].firstChild.nodeValue;

				var u =  eval('(' + jsonText + ')');
				if (u.error == 0) {
					if (f.logo_path.value != '' || f.site_image.value != '') {
						file_uploader(u.site_id);
					}
					document.getElementById('feedback').innerHTML = "update completed";
					setTimeout("document.getElementById('feedback').innerHTML = ''",5000);
				}
				else {
					document.getElementById('save_button').disabled = false;
					alert('Error processing request. Save error: '+u.error);
					return false;
				}
			}
		}	
	}	
	xmlhttp2.send(save_string);
}

function file_uploader(s_id){
	var f = document.account_mgt_form;
	if (f.logo_path.value != '' || f.site_image.value != '') {
		var good_ext    = new RegExp(/.gif$|.jpg$|.jpeg$|.png$/gi); // bad means any bytes other than these ...
		var good_ext2    = new RegExp(/.gif$|.jpg$|.jpeg$|.png$/gi); // bad means any bytes other than these ...
		if (good_ext.test(f.logo_path.value) == false && f.logo_path.value != '') {
		    alert('Unsupported file type. Please select an image file for your logo.');
			return false;
		}
		
		if (good_ext2.test(f.site_image.value) == false && f.site_image.value != '') {
		    alert('Unsupported file type. Please select an image file for your site image. ' + f.site_image.value);
			return false;
		}
		f.site_id.value = s_id;
		f.command.value = 'update logo_path';
		f.submit();
		f.command.value = 'update site info';
		f.logo_path.value='';
		f.site_image.value='';
	}
}

function show_category_products(c_id) {
	if (!c_id)	{ c_id = 0; }
	
	get_xml('/cgi-bin/customers/account_management.cgi?command=list products&category_id='+c_id,'featured_product_list');
}

function choose_category(c_id) {
	if (!c_id)	{ c_id = 0; }
	
	get_xml('/cgi-bin/customers/account_management.cgi?command=list categories&parent_category_id='+c_id,'product_list');
}

function choose_product(p_id,path,name){
	var f = document.account_mgt_form;
	
	document.getElementById('product_image_name').innerHTML= name;
	
	
	document.getElementById('product_image').src=path;
	document.getElementById('product_image').style.display='inline';
	lock_image = 1;
	
	f.featured_product_id.value = p_id;
	setTimeout('lock_image=0;',2000);
}

function preview_product(f) {

	if (document.getElementById('product_image').style.display == 'none' ) {
		document.getElementById('product_image').style.display='inline';
	}
	else {
		current_file_path = document.getElementById('product_image').src;
	}
	document.getElementById('product_image').src=f;
}

function restore_image() {

	if (lock_image == 0) {
		if (!current_file_path) {
			document.getElementById('product_image').style.display='none';
		}
		else {
			document.getElementById('product_image').src=current_file_path;
		}
	}
}

function validate_create_account (section,show_checkout) {
	// initialize error messages
	document.getElementById('company_error').innerHTML 			= '';
	document.getElementById('zip_error').innerHTML 				= '';
	document.getElementById('state_error').innerHTML 			= '';
	document.getElementById('city_error').innerHTML 			= '';
	document.getElementById('address_error').innerHTML 			= '';
	document.getElementById('phone_error').innerHTML 			= '';
	document.getElementById('last_name_error').innerHTML 		= '';
	document.getElementById('first_name_error').innerHTML 		= '';
	document.getElementById('signup_email_error').innerHTML 	= '';
	document.getElementById('fax_error').innerHTML 	= '';
	document.getElementById('toll_free_fax_error').innerHTML 	= '';
	document.getElementById('toll_free_phone_error').innerHTML 	= '';

	var f = document.create_account;
	
	if (f.password) {
		// initialize password error msg if we're updating an existing user. 
		document.getElementById('password_error').innerHTML 		= '';
		
		if (f.password.value != '' && f.password.value.length < 6) {
			document.getElementById('password_error').innerHTML = 'Passwords need to be at least 6 characters in length.';
			f.password.focus();
			return false;
		}
		else if (f.password.value != f.confirm_password.value) {
			document.getElementById('password_error').innerHTML = 'Passwords do not match.';
			f.password.focus();
			return false;
		}
	}
	
	if (f.signup_email.value=='' || !validate_email(f.signup_email)) {
		document.getElementById('signup_email_error').innerHTML = 'Please enter a valid email.';
		f.signup_email.focus();
		return false;
	}
	else if (f.signup_email.value != f.signup_email_confirm.value) {
		document.getElementById('signup_email_error').innerHTML = 'E-mails do not match.';
		f.signup_email.focus();
		return false;
	}
	else if (f.first_name.value == '') {
		document.getElementById('first_name_error').innerHTML = 'Please enter a first name.';
		f.first_name.focus();
		return false;
	}
	else if (f.last_name.value == '') {
		document.getElementById('last_name_error').innerHTML = 'Please enter a last name.';
		f.last_name.focus();
		return false;
	}
	else if (f.phone.value == '') {
		document.getElementById('phone_error').innerHTML = 'Please enter a phone number.';
		f.phone.focus();
		return false;
	}
	else if (f.address1.value == '') {
		document.getElementById('address_error').innerHTML = 'Please enter an address.';
		f.address1.focus();
		return false;
	}
	else if (f.city.value == '') {
		document.getElementById('city_error').innerHTML = 'Please enter a city.';
		f.city.focus();
		return false;
	}
	else if (f.state.value == '') {
		document.getElementById('state_error').innerHTML = 'Please enter a state.';
		return false;
	}
	else if (f.zip.value == '') {
		document.getElementById('zip_error').innerHTML = 'Please enter a zip code.';
		f.zip.focus();
		return false;
	}
	
	if (f.user_type) {
		var u = getCheckedValue(f.user_type);
		
		if (u != 'general') {
			if (f.company_name.value == '') {
				document.getElementById('company_error').innerHTML = 'Please enter a company name.';
				f.company_name.focus();
				return false;
			}
		}
	}
	
	create_account(section,show_checkout);
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function create_account(section,show_checkout) {
	var f = document.create_account;
	
	document.getElementById('save_button').disabled = true;
	
	create_xmlhttp_request();

	var save_string = 
		'command='+ f.command.value +
		'&first_name='+ f.first_name.value +
		'&last_name='+ f.last_name.value +
		'&phone='+ f.phone.value +
		'&address1='+ f.address1.value +
		'&address2='+ f.address2.value +
		'&city='+ f.city.value +
		'&state='+ f.state.value +
		'&country='+ f.country.value +
		'&zip='+ f.zip.value +
		'&fax='+ f.fax.value +
		'&toll_free_fax='+ f.toll_free_fax.value +
		'&toll_free_phone='+ f.toll_free_phone.value +
		'&company_name='+ f.company_name.value +
		'&show_checkout=' + show_checkout;
	
	if (f.newsletter.checked) 	{ save_string += '&newsletter=t'; }
	else 						{ save_string += '&newsletter=f'; }
	
	if (f.requested_user_type) {
		save_string = save_string + '&requested_user_type='+f.requested_user_type.value;
	}
		
	if (f.signup_email.value!='' || f.signup_email.value == f.signup_email_confirm.value) {
		save_string = save_string +  '&signup_email='+ f.signup_email.value;
	}
	if (f.password) {
		if (f.password.value!='' || f.password.value == f.confirm_password.value) {
			save_string = save_string + '&password='+ f.password.value+ '&confirm_password='+ f.confirm_password.value;
		}
	}

	save_string = save_string.replace(fix_pound,'%23');
	
	xmlhttp.open("GET",'/cgi-bin/customers/account_data.cgi?'+save_string);
	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
				var jsonText 	= xmlhttp.responseXML.getElementsByTagName('json_str')[0].firstChild.data;
				
				var u =  eval('(' + jsonText + ')');
			
				if (u.ignore_checkout == 1) {
					document.getElementById('save_button').disabled = false;
				}
				else {
					var sc 			= xmlhttp.responseXML.getElementsByTagName('show_checkout')[0].firstChild.nodeValue;
					
					if (xmlhttp.responseXML.getElementsByTagName('login_email')[0].firstChild) {
						var l 			= '\''+ xmlhttp.responseXML.getElementsByTagName('login_email')[0].firstChild.nodeValue + '\'';
						var p 			= '\''+ xmlhttp.responseXML.getElementsByTagName('login_password')[0].firstChild.nodeValue + '\'';
					}
				}	
				
				if (u.error == 0) {
					
					
					if (sc == 1) {
						document.getElementById('account_area').innerHTML='<a href="javascript:checkout('+l+','+p+');">Proceed to checkout</a>';
					} else {
						document.getElementById('account_area').innerHTML = u.message;
					}
				}
				else {
					document.getElementById('save_button').disabled = false;
					alert('Error processing request. Save error: '+u.error);
					return false;
				}
				
			}
		}	
	}

	xmlhttp.send(null);

}


function request_user_type(t) {
	var f = document.create_account;
	
	f.requested_user_type.value = t.value;
	
	if (t.value == 'general') {
		document.getElementById('company_row').style.display 		= 'none';
		document.getElementById('fax_row').style.display 			= 'none';
		document.getElementById('toll_free_fax_row').style.display 	= 'none';
		document.getElementById('toll_free_phone_row').style.display 	= 'none';
	}
	else {
		document.getElementById('company_row').style.display 		= '';
		document.getElementById('fax_row').style.display 			= '';
		document.getElementById('toll_free_fax_row').style.display 	= '';
		document.getElementById('toll_free_phone_row').style.display 	= '';
	}
}



function checkout(l,p) {
	var f = document.login_form;
	
	f.login_email.value = l;
	f.login_password.value = p;
	f.action = '/cgi-bin/customers/checkout.cgi';
	f.submit();
}

function load_manage_site() {
	document.getElementById('EMDtpc1_3').innerHTML = 'Loading site manager...';
	
	create_xmlhttp_request();
	xmlhttp.open("GET",'/cgi-bin/customers/account_management.cgi?command=get site info');
	xmlhttp.onreadystatechange=function (){
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
			
				document.getElementById('EMDtpc1_3').innerHTML = xmlhttp.responseXML.getElementsByTagName('content')[0].firstChild.data;
				choose_category(0);
			}
		}
	}
	xmlhttp.send(null);
	
}

function load_order_history(ut) {
	document.getElementById('EMDtpc1_2').innerHTML = 'Loading order history...';
	
	get_xml('/cgi-bin/customers/account_management.cgi?command=load order history','EMDtpc1_2');
}


function load_account_mgt_info() {
	var f = document.create_account;
	f.state_list.length=0;
	
	f.state_list.options[0] = new Option('Loading list of states/provinces...','');
	
	create_xmlhttp_request(1);

	xmlhttp2.open("GET",'/cgi-bin/customers/account_data.cgi?command=list states&country='+f.country.value);
	xmlhttp2.onreadystatechange=function (){
		if (xmlhttp2.readyState==4) {
			if (xmlhttp2.status==200) {
				var e = xmlhttp2.responseXML.getElementsByTagName('error')[0].firstChild.nodeValue;
				
				if (e == 0) {
					var jsonText = xmlhttp2.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='';
					}
					
					load_category_list();
				}
				else {
					alert('Error processing AJAX request: '+e);
				}
			
			}
		}
	}
	xmlhttp2.send(null);
	
}
