function get_newsletters(){

 		var token = $('access_token').value;

		if(token == '') {
			$('access_error').update("<br />Please enter Access Token.<br />");
			return false;
		}
		var url = '../../web_services/get_newsletters.php'; 
		new Ajax.Request(url, {	parameters: { token : token},
							onCreate:	function(transport) { 

										},
							onSuccess:	function(transport) {

										},
							onComplete:	function(transport) {
											response = transport.responseText;
											if(response == 1){
												window.location="http://www.aquahydrate.com/investors/newsletter.php?TKN="+token;
											} else {
												$('access_error').update("<br />Please enter a valid Access Token.<br />");
											}									
										},
							onFailure:	function(transport) {
									
										}
						  }
					);

	}

function getstores(zip){

		var ziplength = zip.length;

		if(ziplength > 4) {
			var url = '../../web_services/closest_stores.php'; 
			new Ajax.Request(url, {	parameters: { zip : zip},
								onCreate:	function(transport) { 

											},
								onSuccess:	function(transport) {

											},
								onComplete:	function(transport) {
												response = transport.responseText;
												$('find-result').update(response);
											
											},
								onFailure:	function(transport) {
										
											}
							  }
						);
		} else {
			$('find-result').update(' ');
			return false;
		}

	}

function show_continue(){

	var case_one_qty = $('case_one_qty').value;
	var case_two_qty = $('case_two_qty').value;

	if(case_one_qty != '' || case_two_qty != '') {
		$('continue').show();
	} else {
		$('continue').hide();
	}

	}

function find_sum(case_one_qty)
{
	var sum=0;
	var no=case_one_qty;
	while(no>0)
	{
	sum=sum+no%10;
	no=Math.floor(no/10);
	}
	return sum;    
}

function getshippingoptions(){

	var case_one_qty = $('case_one_qty').value;
	var case_two_qty = $('case_two_qty').value;
	//var case_shipping_type = $('case_shipping_type').value;
	var case_shipping_type = '';
	
	if(case_one_qty == '' && case_two_qty == '') {
		alert('Please enter Quatity of Cases.');
		return false;
	}

	var case_one_qty_sum = find_sum(case_one_qty);
	var case_two_qty_sum = find_sum(case_two_qty);

	if(case_one_qty_sum == 0 && case_two_qty_sum == 0) {
		alert('Please enter a valid Quatity of Cases.');
		return false;
	}

	var url = '../../tab-contents/shipping-options.php'; 
	new Ajax.Request(url, {	parameters: { case_one_qty : case_one_qty, case_two_qty : case_two_qty, case_shipping_type : case_shipping_type},
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {

									},
						onComplete:	function(transport) {
										response = transport.responseText;
										$('purchase_online').update(response);
									},
						onFailure:	function(transport) {
								
									}
					  }
				);

	}

function get_state_from_zip(){

	var case_count = $('case_count').value;
	var zipcode = $('ship_zipcode').value;
	var ziplength = zipcode.length;
	if(ziplength > 4) {
		var url = '../../web_services/state_from_zip.php'; 
		act = "shipping";

		new Ajax.Request(url, {	parameters: { count : case_count, zip : zipcode , act : act},
							onCreate:	function(transport) { 
											$('loader').update("<img src='images/loader.gif'>");
										},
							onSuccess:	function(transport) {

										},
							onComplete:	function(transport) {
											$('loader').update("");
											response = transport.responseText;
											var myObject = eval('(' + response + ')');
											if(myObject['is_valid']) {
												get_shipping_options();
											} else {
												$('ship_state').update(zipcode +" is not a valid US zipcode");
												$('loader').update("");
												$('ship_method').hide();
												$('ship_charge').update("");
												$('ship_total').update("");
											}

										},
							onFailure:	function(transport) {
								
										}
						  }
					);
	}
}

function get_shipping_options(){

	var case_count = $('case_count').value;
	var zipcode = $('ship_zipcode').value;
	if(zipcode) {
		var url = '../../web_services/shipping.php'; 
		new Ajax.Request(url, {	parameters: { count : case_count, zip : zipcode },
							onCreate:	function(transport) { 
											$('loader').update("<img src='images/loader.gif'>");
										},
							onSuccess:	function(transport) {

										},
							onComplete:	function(transport) {
											$('loader').update("");
											response = transport.responseText;
											var myObject = eval('(' + response + ')');
											$('ship_state').update(myObject['state_name']+' - '+myObject['country_name']);
											$('ship_method').show();
											var methods='';
											var cnt = myObject['options'].length;
											for ( i = 0; i < cnt; i++ ) {
												methods += '<option value="'+ roundNumber(myObject['options'][i][1],2) +'">'+ myObject['options'][i][0]+ ' - ' +roundNumber(myObject['options'][i][1],2) +'</option>';
											}
											var shipchrg = roundNumber(myObject['options'][0][1],2);
											$('ship_method').update(methods);
											var subtotal = myObject['count'] * 20;
											$('ship_subtotal').update('$'+subtotal);
											$('ship_charge').update('$'+shipchrg);
											var total = subtotal + parseFloat(shipchrg);
											$('ship_total').update('$'+roundNumber(total,2));
											var state = myObject['state_name']+' - '+myObject['country_name'];
											//var shipchrg = roundNumber(myObject['options'][0][1],2);
											var method_name = myObject['options'][0][0];
											save_shipping_options(zipcode, state, subtotal, shipchrg, total, method_name);

										},
							onFailure:	function(transport) {
								
										}
						  }
					);
	}
}

function get_shipping_options_method(method){

	var case_count = $('case_count').value;
	var zipcode = $('ship_zipcode').value;
	if(zipcode) {
		var url = '../../web_services/shipping.php'; 
		new Ajax.Request(url, {	parameters: { count : case_count, zip : zipcode },
							onCreate:	function(transport) { 
											$('loader').update("<img src='images/loader.gif'>");
										},
							onSuccess:	function(transport) {

										},
							onComplete:	function(transport) {
											$('loader').update("");
											response = transport.responseText;
											var myObject = eval('(' + response + ')');
											$('ship_state').update(myObject['state_name']+' - '+myObject['country_name']);
											$('ship_method').show();
											var methods='';
											var cnt = myObject['options'].length;
											var selected = '';
											var shipcharge = method;
											for ( i = 0; i < cnt; i++ ) {
												if(myObject['options'][i][1] == method){
													selected = 'selected';
												}
												if(selected == 'selected'){
													shipcharge = myObject['options'][i][1];
												}
												methods += '<option value="'+ myObject['options'][i][1] +'" '+selected+'>'+ myObject['options'][i][0]+ ' - ' +myObject['options'][i][1] +'</option>';
													selected = '';
											}
											$('ship_method').update(methods);

											var subtotal = myObject['count'] * 20;
											$('ship_subtotal').update('$'+subtotal);
											$('ship_charge').update('$'+shipcharge);
											var total = subtotal + shipcharge;
											$('ship_total').update('$'+total);

											var state = myObject['state_name']+' - '+myObject['country_name'];
											save_shipping_options(zipcode, state, subtotal, shipcharge, total);


										},
							onFailure:	function(transport) {
								
										}
						  }
					);
	}
}

function get_shipping(val,count){

	var elt = $('ship_method');
	var method_name = elt.options[elt.selectedIndex].text;
	var ship_method = method_name.split('-');
	$('ship_charge').update('$'+val);
	var subtotal = count * 20;
	var total = subtotal + parseFloat(val);
	$('ship_total').update('$'+roundNumber(total,2));
	var rsubtotal = roundNumber(subtotal,2);

	save_shipping_options(0,'', rsubtotal, val, total, ship_method[0]);

}

function save_shipping_options(ship_zipcode, ship_state, ship_subtotal, ship_charge, ship_total, method_name){

		var url = '../../tab-contents/save-shipping-options.php'; 
		new Ajax.Request(url, {	parameters: { ship_zipcode : ship_zipcode, ship_state : ship_state, ship_subtotal : ship_subtotal, ship_charge : ship_charge, ship_total : ship_total, method_name : method_name},
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {
									},
						onComplete:	function(transport) {
									},
						onFailure:	function(transport) {
						
									}
					  }
				);
}

function save_shipping_adress(val){

	var ship_name = $('ship_name').value;
	var ship_phone = $('ship_phone').value;
	var ship_email = $('ship_email').value;
	var ship_address1 = $('ship_address1').value;
	var ship_address2 = $('ship_address2').value;
	var ship_city = $('ship_city').value;
	var ship_zipcode = $('ship_zipcode').innerHTML;
	var ship_state = $('ship_state').innerHTML;

	var url = '../../tab-contents/save-shipping-address.php'; 
	new Ajax.Request(url, {	parameters: { ship_name : ship_name, ship_phone : ship_phone, ship_email : ship_email, ship_address1 : ship_address1, ship_address2 : ship_address2, ship_city : ship_city, ship_zipcode : ship_zipcode, ship_state : ship_state, val : val },
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {

									},
						onComplete:	function(transport) {

										if(val == '1') {

											var url = '../../tab-contents/shipping-options.php'; 
											new Ajax.Request(url, {	parameters: { },
																onCreate:	function(transport) { 
																			},
																onSuccess:	function(transport) {

																			},
																onComplete:	function(transport) {
																				response = transport.responseText;
																				$('home-content-rightinner').update(response);
																			},
																onFailure:	function(transport) {
								
																			}
															  }
														);


										} else {
											if(ship_name == '' || ship_phone == '' || ship_email == '' || ship_address1 == '' || ship_city == '') {
												alert("Required information incomplete.");
												return false;
											}

											var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
											var test = emailPattern.test(ship_email);  
											if(test == false){
												alert("Please enter a valid Email.");
												return false;
											}

											ajaxpage('tab-contents/credit-address.php', 'home-content-rightinner');
										}
									},
						onFailure:	function(transport) {
								
									}
					  }
				);

	}

function shipping_options_continue(){

	var ship_zipcode = $('ship_zipcode').value;
	var ship_state = $('ship_state').value;
	var ship_method = $('ship_method').value;

	if(ship_zipcode == '' || ship_state == '' || ship_method == '') {
		alert("Please enter Zipcode.");
		return false;
	}
	ajaxpage('tab-contents/shipping-address.php', 'home-content-rightinner');

}

function save_credit_adress(val){

	var credit_name = $('credit_name').value;
	var credit_phone = $('credit_phone').value;
	var credit_email = $('credit_email').value;
	var credit_address1 = $('credit_address1').value;
	var credit_address2 = $('credit_address2').value;
	var credit_city = $('credit_city').value;
	var credit_zipcode = $('credit_zipcode').value;
	var credit_state = $('credit_state').innerHTML;

	var url = '../../tab-contents/save-credit-address.php'; 
	new Ajax.Request(url, {	parameters: { credit_name : credit_name, credit_phone : credit_phone, credit_email : credit_email, credit_address1 : credit_address1, credit_address2 : credit_address2, credit_city : credit_city, credit_zipcode : credit_zipcode, credit_state : credit_state },
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {

									},
						onComplete:	function(transport) {
										if(val == '1') {
											ajaxpage('tab-contents/shipping-address.php', 'home-content-rightinner');
										} else {
											if(credit_name == '' || credit_phone == '' || credit_email == '' || credit_address1 == '' || credit_city == '' || credit_zipcode == '') {
												alert("Required information incomplete.");
												return false;
											}

											var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
											var test = emailPattern.test(credit_email);  
											if(test == false){
												alert("Please enter a valid Email.");
												return false;
											}

											ajaxpage('tab-contents/credit-card-details.php', 'home-content-rightinner');
										}
									},
						onFailure:	function(transport) {
								
									}
					  }
				);

}

function get_credit_state_from_zip(){

	var zipcode = $('credit_zipcode').value;
	var ziplength = zipcode.length;

	if(ziplength > 4) {
		var url = '../../web_services/state_from_zip.php';
		act = "billing"; 
		new Ajax.Request(url, {	parameters: { zip : zipcode , act : act},
							onCreate:	function(transport) { 
										},
							onSuccess:	function(transport) {

										},
							onComplete:	function(transport) {
											response = transport.responseText;
											var myObject = eval('(' + response + ')');
											if(myObject['is_valid']) {
												$('credit_state').update(myObject['state_name']+' - '+myObject['country_name']);
											} else {
												$('credit_state').update(zipcode +" is not a valid US zipcode");
											}

										},
							onFailure:	function(transport) {
								
										}
						  }
					);
	}
}

function save_credit_card_details(val){

	var cardnumber = $('cardnumber').value;
	var ex_month = $('ex_month').value;
	var ex_year = $('ex_year').value;
	var amount = $('amount').innerHTML;

	var url = '../../tab-contents/save-credit-card-details.php'; 
	new Ajax.Request(url, {	parameters: { cardnumber : cardnumber, ex_month : ex_month, ex_year : ex_year, amount : amount },
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {

									},
						onComplete:	function(transport) {
										if(val == '1') {
											ajaxpage('tab-contents/credit-address.php', 'home-content-rightinner');
										} else {
											if(cardnumber == '' || ex_month == '' || ex_year == '') {
												alert("Required information incomplete.");
												return false;
											}
											credit_card_pay_now();
										}
									},
						onFailure:	function(transport) {
								
									}
					  }
				);

}

function credit_card_pay_now(){

	var url = '../../web_services/order.php'; 
	new Ajax.Request(url, {	parameters: { },
						onCreate:	function(transport) { 
									},
						onSuccess:	function(transport) {

									},
						onComplete:	function(transport) {

									ajaxpage('tab-contents/thankyou.php', 'home-content-rightinner');

									},
						onFailure:	function(transport) {
							
									}
					  }
				);

}

function check_case_qty(e) { 

                var key; 
                var keychar; 
                if (window.event) { 
                        key = window.event.keyCode; 
                } 
                else if (e) {
                 key = e.which; 
                } 
                else { 
                        return true;
                 } 
                keychar = String.fromCharCode(key);                 
                if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (("0123456789").indexOf(keychar) > -1)) { 
                        return true; 
                } else {                 
                return false; 
                }
}

 function roundNumber(number,decimal_points) {
        if(!decimal_points) return Math.round(number);
        if(number == 0) {
                var decimals = "";
                for(var i=0;i<decimal_points;i++) decimals += "0";
                return "0."+decimals;
        }

        var exponent = Math.pow(10,decimal_points);
        var num = Math.round((number * exponent)).toString();
        return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}

