function convertStringForPass(str) {
	str=str.replace(/#/g,"**:1:**");
	str=str.replace(/&/g,"**:2:**");
	
	return str;
}

function update_product_price(product_type_id) {
	http.open('get', 'ajax/onlinestore.ajax.php?update_product_price=1&product_type_id='+product_type_id);
	http.onreadystatechange = handle_update_product_price;
	http.send(null);
}

function handle_update_product_price() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	document.getElementById('product_price').innerHTML=response;
  }
}

function image_small_change(image_id,src) {
	document.getElementById('image_small').src=src;
	
	document.getElementById('image_small_'+document.getElementById('image_small_id').value).style.border='1px solid #e5e5e5';
	document.getElementById('image_small_'+image_id).style.border='1px solid #6eb955';
	
	document.getElementById('image_small_id').value=image_id;
}

function add_to_cart(page,section_id,section_sub_id,product_id) {
	document.getElementById('onlinestore_section_product_addtocart_spinner').innerHTML="<img src='images/spinner.gif' style='position:relative; top:3px; left:5px;'></img>";
	
	var product_type_id = document.getElementById('product_product_type_id').value;
	var qty = document.getElementById('product_qty').value;
	
	document.getElementById('page').value=page;
	document.getElementById('section_id').value=section_id;
	document.getElementById('section_sub_id').value=section_sub_id;
	document.getElementById('product_id').value=product_id;
	document.getElementById('product_type_id').value=product_type_id;
	document.getElementById('qty').value=qty;
	
	setTimeout('add_to_cart_settimeout()',1250);
}
	
function add_to_cart_settimeout() {
	http.open('get', 'ajax/onlinestore.ajax.php?add_to_cart=1&page='+document.getElementById('page').value+'&section_id='+document.getElementById('section_id').value+'&section_sub_id='+document.getElementById('section_sub_id').value+'&product_id='+document.getElementById('product_id').value+'&product_type_id='+document.getElementById('product_type_id').value+'&price='+document.getElementById('price').value+'&qty='+document.getElementById('qty').value);
	http.onreadystatechange = handle_add_to_cart;
	http.send(null);
}

function handle_add_to_cart() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response=="") {
  		var item_count=document.getElementById('onlinestore_shoppingcart_items').innerHTML;
  		document.getElementById('onlinestore_shoppingcart_items').innerHTML=(item_count*1)+1;
  		document.getElementById('onlinestore_add_to_cart_thankyou').innerHTML="Your shoppingcart has been updated. <a href='shoppingcart.php'>View Shopping Cart.</a>";
  		document.getElementById('onlinestore_section_product_addtocart_spinner').innerHTML="";
  	} else {
  		document.getElementById('onlinestore_add_to_cart_thankyou').innerHTML=response;
  		document.getElementById('onlinestore_section_product_addtocart_spinner').innerHTML="";
  	}
  }
}

function update_product_in_cart(shoppingcart_product_id,qty) {
	document.getElementById('shoppingcart_product_id').value=encodeURI(convertStringForPass(shoppingcart_product_id));
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));

	http.open('get', 'ajax/onlinestore.ajax.php?update_product_in_cart=1&shoppingcart_id='+shoppingcart_id+'&shoppingcart_product_id='+shoppingcart_product_id+'&qty='+qty);
	http.onreadystatechange = handle_update_product_in_cart;
	http.send(null);
}

function handle_update_product_in_cart() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	var split = response.indexOf('::');
  	document.getElementById('onlinestore_shoppingcart_items').innerHTML=response.substring(0,split);
  	document.getElementById('shoppingcart').innerHTML=response.substring((split+2));
  }
}

function remove_from_cart(shoppingcart_product_id) {
	document.getElementById('shoppingcart_product_id').value=shoppingcart_product_id;
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));
	
	http.open('get', 'ajax/onlinestore.ajax.php?remove_from_cart=1&shoppingcart_id='+shoppingcart_id+'&shoppingcart_product_id='+shoppingcart_product_id);
	http.onreadystatechange = handle_remove_from_cart;
	http.send(null);
}

function handle_remove_from_cart() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	var split = response.indexOf('::');
  	document.getElementById('onlinestore_shoppingcart_items').innerHTML=response.substring(0,split);
  	document.getElementById('shoppingcart').innerHTML=response.substring((split+2));
  }
}

function change_shipping_method(shoppingcart_id,shipping_method_id) {
	http.open('get', 'ajax/onlinestore.ajax.php?change_shipping_method=1&shoppingcart_id='+shoppingcart_id+'&shipping_method_id='+shipping_method_id);
	http.onreadystatechange = handle_change_shipping_method;
	http.send(null);
}

function handle_change_shipping_method() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response!="") {
  		document.getElementById('shoppingcart_totals').innerHTML=response;
  	}
  }
}

function promocode_link() { 
	document.getElementById('promocode_link').style.display='none';
	document.getElementById('promocode_apply').style.display='';
}

function promocode_apply() { 
	document.getElementById('promocode_apply_spinner').innerHTML="<img src='images/spinner.gif' style='position:relative; top:2px;'></img> <i style='position:relative; top:-1px; font-size:10px; color:#999;'>validating promo code</i>";
	
	setTimeout('promocode_apply_settimeout()',1250);
}

function promocode_apply_settimeout() {
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));
	var promocode = encodeURI(convertStringForPass(document.getElementById('promocode').value));
	
	http.open('get','ajax/onlinestore.ajax.php?promocode_apply=1&shoppingcart_id='+shoppingcart_id+'&promocode='+promocode);
	http.onreadystatechange = handle_promocode_apply;
	http.send(null);
}

function handle_promocode_apply() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response[0]!="E" && response[1]!="r") {
  		if (response!="") {
  			document.getElementById('shoppingcart').innerHTML=response;
  			document.getElementById('promocode_apply_spinner').innerHTML="<i style='position:relative; top:3px; font-size:10px;'>promo code applied!</i>";
  		} else {
  			document.getElementById('promocode_apply_spinner').innerHTML="<i style='position:relative; top:3px; font-size:10px; color:#f00;'>invalid promo code</i>";
  		}
  	} else {
  		document.getElementById('promocode_apply_spinner').innerHTML="<i style='position:relative; top:3px; font-size:10px; color:#f00;'>invalid promo code</i>";
  	}
  }
}

function checkout_signin() { 
	document.getElementById('signin_spinner').innerHTML="&nbsp;&nbsp;<img src='images/spinner.gif'> <i style='position:relative; top:-4px; font-size:10px; color:#999;'>checking login</i>";
	
	setTimeout('checkout_signin_settimeout()',1250);
}

function checkout_signin_settimeout() {
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));
	var email = encodeURI(convertStringForPass(document.getElementById('signin_email').value));
	var password = encodeURI(convertStringForPass(document.getElementById('signin_password').value));
	
	http.open('get','ajax/onlinestore.ajax.php?checkout_signin=1&shoppingcart_id='+shoppingcart_id+'&email='+email+'&password='+password);
	http.onreadystatechange = handle_checkout_signin;
	http.send(null);
}

function handle_checkout_signin() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response[0]!="E" && response[1]!="r") {
  		document.getElementById('checkout').innerHTML=response;
  	} else {
  		document.getElementById('signin_spinner').innerHTML="&nbsp;&nbsp;<i style='position:relative; top:0; font-size:10px; color:#f00;'>invalid login</i>";
  	}
  }
}

function checkout_joinnow() {
	document.getElementById('joinnow_spinner').innerHTML="&nbsp;&nbsp;<img src='images/spinner.gif'> <i style='position:relative; top:-4px; font-size:10px; color:#999;'>one moment</i>";
	
	setTimeout('checkout_joinnow_settimeout()',1250);
}

function checkout_joinnow_settimeout() {
	http.open('get','ajax/onlinestore.ajax.php?checkout_joinnow=1');
	http.onreadystatechange = handle_checkout_joinnow;
	http.send(null);
}

function handle_checkout_joinnow() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response!="") {
  		document.getElementById('checkout').innerHTML=response;
  	} 
  }
}

function checkout_joinnow_back() {
	document.getElementById('centrebuttons_spinner').innerHTML="&nbsp;&nbsp;<img src='images/spinner.gif'>";
	
	setTimeout('checkout_joinnow_back_settimeout()',1250);
}

function checkout_joinnow_back_settimeout() {
	http.open('get','ajax/onlinestore.ajax.php?checkout_joinnow_back=1');
	http.onreadystatechange = handle_checkout_joinnow_back;
	http.send(null);
}

function handle_checkout_joinnow_back() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response!="") {
  		document.getElementById('checkout').innerHTML=response;
  	} 
  }
}

function checkout_joinnow_continue() { 
	document.getElementById('centrebuttons_spinner').innerHTML="&nbsp;&nbsp;<img src='images/spinner.gif'> <i style='position:relative; top:-4px; font-size:10px; color:#999;'>one moment</i>";
	
	setTimeout('checkout_joinnow_continue_settimeout()',1250);
}

function checkout_joinnow_continue_settimeout() {
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));
	var email = encodeURI(convertStringForPass(document.getElementById('joinnow_email').value));
	var confirm_email = encodeURI(convertStringForPass(document.getElementById('joinnow_confirm_email').value));
	var password = encodeURI(convertStringForPass(document.getElementById('joinnow_password').value));
	var confirm_password = encodeURI(convertStringForPass(document.getElementById('joinnow_confirm_password').value));
	var fname = encodeURI(convertStringForPass(document.getElementById('joinnow_fname').value)); 
	var lname = encodeURI(convertStringForPass(document.getElementById('joinnow_lname').value));
	var s1 = encodeURI(convertStringForPass(document.getElementById('joinnow_s1').value));
	var city = encodeURI(convertStringForPass(document.getElementById('joinnow_city').value));
	var province = encodeURI(convertStringForPass(document.getElementById('joinnow_province').value));
	var country = encodeURI(convertStringForPass(document.getElementById('joinnow_country').value));
	var postalcode = encodeURI(convertStringForPass(document.getElementById('joinnow_postalcode').value));
	
	http.open('get','ajax/onlinestore.ajax.php?checkout_joinnow_continue=1&shoppingcart_id='+shoppingcart_id+'&email='+email+'&confirm_email='+confirm_email+'&password='+password+'&confirm_password='+confirm_password+'&fname='+fname+'&lname='+lname+'&s1='+s1+'&city='+city+'&province='+province+'&country='+country+'&postalcode='+postalcode);
	http.onreadystatechange = handle_checkout_joinnow_continue;
	http.send(null);
}

function handle_checkout_joinnow_continue() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response[0]!="E" && response[1]!="r") {
  		document.getElementById('checkout').innerHTML=response;
  	} else {
  		document.getElementById('centrebuttons_spinner').innerHTML="<div class='onlinestore_errors'>"+response+"</div>";
  	}
  }
}

function checkout_step2_back() {
	http.open('get','ajax/onlinestore.ajax.php?checkout_step2_back=1');
	http.onreadystatechange = handle_checkout_step2_back;
	http.send(null);
}

function handle_checkout_step2_back() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response!="") {
  		document.getElementById('checkout').innerHTML=response;
  	} 
  }
}

function checkout_step2_continue() {
	document.getElementById('centrebuttons_spinner').innerHTML="&nbsp;&nbsp;<img src='images/spinner.gif'> <i style='position:relative; top:-4px; font-size:10px; color:#999;'>one moment</i>";
	
	setTimeout('checkout_step2_continue_settimeout()',1250);
}

function checkout_step2_continue_settimeout() {
	var shoppingcart_id = encodeURI(convertStringForPass(document.getElementById('shoppingcart_id').value));
	var b_s1 = encodeURI(convertStringForPass(document.getElementById('step2_b_s1').value));
	var b_city = encodeURI(convertStringForPass(document.getElementById('step2_b_city').value));
	var b_province = encodeURI(convertStringForPass(document.getElementById('step2_b_province').value));
	var b_country = encodeURI(convertStringForPass(document.getElementById('step2_b_country').value));
	var b_postalcode = encodeURI(convertStringForPass(document.getElementById('step2_b_postalcode').value));
	var cc_nameoncard = encodeURI(convertStringForPass(document.getElementById('step2_cc_nameoncard').value));
	var cc_creditcardnumber = encodeURI(convertStringForPass(document.getElementById('step2_cc_creditcardnumber').value));
	var cc_expiry_month = encodeURI(convertStringForPass(document.getElementById('step2_cc_expiry_month').value));
	var cc_expiry_year = encodeURI(convertStringForPass(document.getElementById('step2_cc_expiry_year').value));
	var cc_verification = encodeURI(convertStringForPass(document.getElementById('step2_cc_verification').value));
	var s_sameasbilling; if (document.getElementById('step2_s_sameasbilling').checked) { s_sameasbilling=1; } else { s_sameasbilling=0; }
	var s_shipto = encodeURI(convertStringForPass(document.getElementById('step2_s_shipto').value));
	var s_s1 = encodeURI(convertStringForPass(document.getElementById('step2_s_s1').value));
	var s_city = encodeURI(convertStringForPass(document.getElementById('step2_s_city').value));
	var s_province = encodeURI(convertStringForPass(document.getElementById('step2_s_province').value));
	var s_country = encodeURI(convertStringForPass(document.getElementById('step2_s_country').value));
	var s_postalcode = encodeURI(convertStringForPass(document.getElementById('step2_s_postalcode').value));
	var save_information; if (document.getElementById('step2_save_information').checked) { save_information=1; } else { save_information=0; }
	
	http.open('get','ajax/onlinestore.ajax.php?checkout_step2_continue=1&shoppingcart_id='+shoppingcart_id+'&b_s1='+b_s1+'&b_city='+b_city+'&b_province='+b_province+'&b_country='+b_country+'&b_postalcode='+b_postalcode+'&cc_nameoncard='+cc_nameoncard+'&cc_creditcardnumber='+cc_creditcardnumber+'&cc_expiry_month='+cc_expiry_month+'&cc_expiry_year='+cc_expiry_year+'&cc_verification='+cc_verification+'&s_sameasbilling='+s_sameasbilling+'&s_shipto='+s_shipto+'&s_s1='+s_s1+'&s_city='+s_city+'&s_province='+s_province+'&s_country='+s_country+'&s_postalcode='+s_postalcode+'&save_information='+save_information);
	http.onreadystatechange = handle_checkout_step2_continue;
	http.send(null);
}

function handle_checkout_step2_continue() {
  if(http.readyState == 4) {
  	var response = http.responseText;
  	
  	if (response[0]!="E" && response[1]!="r") {
  		document.getElementById('checkout').innerHTML=response;
  	} else {
  		document.getElementById('centrebuttons_spinner').innerHTML="<div class='onlinestore_errors'>"+response+"</div>";
  	}
  }
}

function checkout_onfocus(e,value) {
	if (e.value==value) { e.value=''; }
}

function checkout_password_onfocus(e,value) {
	if (e.value==value) { e.value=''; }
	e.type='password';
}

function checkout_onblur(e,value) {
	if (e.value=='') { e.value=value; }
}

function checkout_password_onblur(e,value) {
	if (e.value=='') { e.value=value; e.type=''; }
}
