								jQuery(window).ready(function(){
					  
					  

					  
					  var prodNameArray = [];
					  var prodAmountArray = [];
					  var counter;
					  var results = "";
            var taxPercent = 1.095;
            var checkoutTotalCost;
            var tax;
					  var total;
					  var shippingCost = 5.00;
					  
					  var counter = jQuery('.product_row .firstcol a').size();
					  
            for(var i=0; i<counter; i++)
            {
                prodNameArray[i] = jQuery(".product_row td a").eq(i).text(); 
                prodAmountArray[i] = jQuery("input:text[name=quantity]").eq(i).val();                
                results += "Product: " + prodNameArray[i] + "  Number of item: " + prodAmountArray[i] + "  "; 
            }

					  checkoutTotalCost = jQuery('#checkout_total').text();
					  checkoutTotalCost = Number(checkoutTotalCost.replace(/[^0-9\.]+/g,""));

					  tax = Number((taxPercent * checkoutTotalCost) - checkoutTotalCost);
            tax = Math.round(tax*100)/100;
  				  
					  total = checkoutTotalCost + tax;
            total = Math.round(total*100)/100;
            total = total + shippingCost;
                
            checkoutTotalCost = checkoutTotalCost + shippingCost;
            
            checkoutTotalCost = checkoutTotalCost.toFixed(2);
            tax = tax.toFixed(2);
            total = total.toFixed(2);
            
            jQuery(".checkout-total").text("$" + total);
            jQuery(".checkout-tax").text("$" + tax);
            jQuery("tr.total_tax").after("<tr class='total_price'><td colspan='3'>Shipping</td><td colspan='2'><span class='pricedisplay'>$5.00</span></td></tr>");
                        
            //var injectHTML = "<label>Promo code:&nbsp;&nbsp;&nbsp;</label><input id='promoCodeText' type='text' />&nbsp;&nbsp;<input id='promoCodeGo' type='button' value='submit promo code' />&nbsp;&nbsp;<label id='promoCodeMessage'></label><br /><br /><form action='https://www.myvirtualmerchant.com/VirtualMerchant/process.do?referrer=http://www.littlemissgrippy.com/' method='POST'><input type='hidden' name='ssl_salestax' value='" + tax + "'><input type='hidden' name='ssl_amount' value='" + total + "'><input type='hidden' name='ssl_account_totalamount' value='" + checkoutTotalCost + "'><input type='hidden' name='ssl_description' value='" + results + "'><input type='hidden' name='ssl_transaction_type' value='ccsale'><input type='hidden' name='ssl_show_form' value='true'><input type='hidden' name='ssl_merchant_id' value='541245'><input type='hidden' name='ssl_pin' value='T2XY75'><input type='hidden' name='ssl_error_url' value='http://www.littlemissgrippy.com/error_page.php'><input type='submit' value='Click To Order'></form>";
                        
        

            function updatePrices(str){  
               total = total - 30.00;
               total = total.toFixed(2);
               checkoutTotalCost = checkoutTotalCost - 30.00;
               checkoutTotalCost = checkoutTotalCost.toFixed(2);
               jQuery(".checkout-total").text("$" + total);
               var injectHTML = "<label>Promo code:&nbsp;&nbsp;&nbsp;</label><input id='promoCodeText' type='text' />&nbsp;&nbsp;<input id='promoCodeGo' type='button' value='submit promo code' />&nbsp;&nbsp;<label id='promoCodeMessage'></label><br /><br /><form action='https://www.myvirtualmerchant.com/VirtualMerchant/process.do?referrer=http://www.littlemissgrippy.com/' method='POST'><input type='hidden' name='ssl_salestax' value='" + tax + "'><input type='hidden' name='ssl_amount' value='" + total + "'><input type='hidden' name='ssl_account_totalamount' value='" + checkoutTotalCost + "'><input type='hidden' name='ssl_description' value='" + results + "'><input type='hidden' name='ssl_transaction_type' value='ccsale'><input type='hidden' name='ssl_show_form' value='true'><input type='hidden' name='ssl_merchant_id' value='541245'><input type='hidden' name='ssl_pin' value='T2XY75'><input type='hidden' name='ssl_error_url' value='http://www.littlemissgrippy.com/error_page.php'><input type='submit' value='Click To Order'></form>";
               jQuery("#checkoutInjectionButton").html("");
               jQuery("#checkoutInjectionButton").append(injectHTML); 
               jQuery('#promoCodeMessage').html('valid code!').css({'color':'green'}); 
               increment(str);
            }

            if ( checkoutTotalCost < 15)
            {
              jQuery("#checkoutInjectionButton").append("");
            }
            else if(checkoutTotalCost > 29){
                jQuery("#checkoutInjectionButton").append(injectHTML);
                updateCodeEvent();
            }
            else{
                jQuery("#checkoutInjectionButton").append("<form action='https://www.myvirtualmerchant.com/VirtualMerchant/process.do?referrer=http://www.littlemissgrippy.com/' method='POST'><input type='hidden' name='ssl_salestax' value='" + tax + "'><input type='hidden' name='ssl_amount' value='" + total + "'><input type='hidden' name='ssl_account_totalamount' value='" + checkoutTotalCost + "'><input type='hidden' name='ssl_description' value='" + results + "'><input type='hidden' name='ssl_transaction_type' value='ccsale'><input type='hidden' name='ssl_show_form' value='true'><input type='hidden' name='ssl_merchant_id' value='541245'><input type='hidden' name='ssl_pin' value='T2XY75'><input type='hidden' name='ssl_error_url' value='http://www.littlemissgrippy.com/error_page.php'><input type='submit' value='Click To Order'></form>");
            } 
            
 
            function updateCodeEvent(){
            	jQuery('#promoCodeGo').click(function() {
            		var str = jQuery('#promoCodeText').val().toLowerCase();
            				
            		jQuery.ajax({
            		  type: 'POST',
            		  url: 'http://www.littlemissgrippy.com/promoCode/checkCode.php',
            		  data: '&sendValue='+str,
            		  success: function(data){
            			   if(data.returnValue == "true"){
                        updatePrices(str);
                     }
                     else{
                        jQuery('#promoCodeMessage').html('invalid code').css({'color':'red'});
                     }
            		  },
            		  dataType: 'json'
            		});
            	});	
            }
            
            function increment(str){
              		jQuery.ajax({
            		  type: 'POST',
            		  url: 'http://www.littlemissgrippy.com/promoCode/increment.php',
            		  data: '&sendValue='+str,
            		  success: function(data){
            		  //
            		  },
            		  dataType: 'json'
            		});
            }
         

            


				});
