﻿


function getKeyCode(evt) {
	        return (typeof(evt.keyCode) != "undefined" && evt.keyCode != 0) ?
	            evt.keyCode : evt.charCode;
	    }
	
function googlecartBeforeAdd(item, index, newQuantity, opt_node) {
    if(item.getCustomAttribute('sku').match("ENROLL_CUSTOMER") || item.getCustomAttribute('sku').match("ENROLL_WHOLESALE") || item.getCustomAttribute('sku').match("ENROLL_CEO_BUILDER") )
    {
        //Same Item being added more than once
        if(newQuantity>1)
        {
            alert('You only need to Enroll Once, therefore Enrollment Options are limited to One per Cart');
            return false;
        }
        
        //Check for other enrollments
        var items = googlecart.getItems();
        var eCount=0;
        for (var i = 0; i < items.length; i++)
        {
            if(items[i].getCustomAttribute('sku').match("ENROLL_CUSTOMER") && !items[i].isMarkedForRemoval() )
            {
                eCount++;
                if(eCount>1)
                {
                    alert('Please choose One Enrollment option');
                    return false;
                }
            }
            else if(items[i].getCustomAttribute('sku').match("ENROLL_WHOLESALE") && !items[i].isMarkedForRemoval() )
            {
                eCount++;
                if(eCount>1)
                {
                    alert('Please choose One Enrollment option');
                    return false;
                }
            }
            else if(items[i].getCustomAttribute('sku').match("ENROLL_CEO_BUILDER") && !items[i].isMarkedForRemoval() )
            {
                eCount++;
                if(eCount>1)
                {
                    alert('Please choose One Enrollment option');
                    return false;
                }
            }

        }
    }
    
    return true;
}

function Enrollment_In_Cart_Check()
{
        var items = googlecart.getItems();


        for (var i = 0; i < items.length; i++)
        {
	    if(items[i].getCustomAttribute('sku').match("ENROLL_CUSTOMER") && !items[i].isMarkedForRemoval() )
            {
                return true;
            }
            else if(items[i].getCustomAttribute('sku').match("ENROLL_WHOLESALE") && !items[i].isMarkedForRemoval() )
            {
               
                return true;
            }
            else if(items[i].getCustomAttribute('sku').match("ENROLL_CEO_BUILDER") && !items[i].isMarkedForRemoval() )
            {
                
                return true;
            }

        }


	return false;
}



function googlecartOnCheckoutClick() {
   try
{
    googlecart.expungeMarkedForRemoval();
    //googlecartWidget.close();



    var items = googlecart.getItems();    
    var order_bv = Get_Bonus_Volume();
    var used_existing_id = Youngevity_ID_Used();
    var has_temp_id = Check_Temporary_ID();
    var has_enrollment = Enrollment_In_Cart_Check();
    

    var has_info_pack = Check_Info_Pack();
    
    if(has_info_pack && order_bv>0)
    {
        alert("Sorry, you cannot Order Product AND the Information Pack at the same time");
        return false;
    }
    
    //No Volume, no enrollment just books/cds/dvds
    if(order_bv==0 && !has_enrollment)
    {
        Calcuate_Shipping();
        return true;
    }
    
    if(used_existing_id)
    {
        return Calcuate_Shipping();
        //return true;
    }
            


    //Check Order for Required Enrollment if Product is Purchased
    if(order_bv>0 && !used_existing_id && !has_temp_id && !has_enrollment)
    {
        var uR = "popups/enrollment_required.aspx";
        enrollment_notice_popup.SetContentUrl(uR);
        enrollment_notice_popup.Show();
        return false;
    }


   
    //If Some Enrollment Option has been selected, remove Y_ID if it exists
    if(used_existing_id)
    {
        Remove_Youngevity_ID_Item();
    }



 
    var cart_subTotal =  googlecart.getSubtotal();

    
    //Preferred Customer Only
    //if(has_temp_id && cart_subTotal==0)
    //{
        //var pFMsg = "Your Application has been Logged";
        //pFMsg = pFMsg + "\r\n We will process that information as quickly as possible.";
        //pFMsg = pFMsg + "\r\n You can order product at this time during the current web session.";
        //pFMsg = pFMsg + "\r\n If you return at a later date, you will need your Youngevity ID which will be emailed to you once your Youngevity enrollment is complete";
        
        //alert(pFMsg);
        //window.location="default.aspx";
        //return false;    
    //}
    
    var enroll_type = 0;
    //Show Enrollment if no TempID
    if(!has_temp_id)
    {
        for (var i = 0; i < items.length; i++)
        {
            if(items[i].getCustomAttribute('sku').match("ENROLL"))
            {
                if(items[i].getCustomAttribute('sku').match("CUSTOMER"))
                    enroll_type = 1;
                else if (items[i].getCustomAttribute('sku').match("WHOLESALE"))   
                    enroll_type = 2;
		        else if (items[i].getCustomAttribute('sku').match("CEO"))
		            enroll_type = 3;
               break;     
            }
        } 
        
        if(enroll_type==0)
        {
            alert("Error in Enrollment Selection, please try again");
            return false;        
        }

        alert("Enrollment Options require additional information, please fill out the required enrollment information");
        
        
        window.location = "enrollment_info.aspx?aType="+enroll_type;
        
        return false;        
        
    }
    
    var has_customer=0;
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('sku').match("CUSTOMER"))
            has_customer=1;
    }
    
    if(has_customer && order_bv==0)
    {
        alert("Selection of Preferred Customer Enrollment REQUIRES the purchase of at least One (1) Product");
        return false;
    }
    
   
    
    
   
    return Calcuate_Shipping();
  
  
}
catch(err)
{
	alert("Checkout Process had some error " + err.description);
	return false;
}
}

function Check_Info_Pack()
{
    var items = googlecart.getItems();
    var has_info_pack=0;
    
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('sku') != null)
        {
            if(items[i].getCustomAttribute('sku').match("INFO_PACKAGE"))
                has_info_pack=1;
        }
    }
    
    return has_info_pack;
}

function Get_Bonus_Volume()
{
    var items = googlecart.getItems();
    var order_bv = 0;
    
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('bonus_volume') != '')
        {
                if(items[i].getCustomAttribute('bonus_volume')!=null)
                {
                    var i_bv = parseFloat(items[i].getCustomAttribute('bonus_volume').replace("BV: ",""));
                    var i_qty = parseFloat(items[i].getQuantity());
                
                    order_bv += parseFloat(i_bv*i_qty);
                }
        }
    }
    
    return order_bv;
}

function Required_Order_BV_Enrollment_Check()
{
    var items = googlecart.getItems();
    var required_volume=-1;
    // Look for enrollment Items
    // Return required_volume
    // Required volume=-1 means no enrollment
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('sku').match("ENROLL"))
        {
            if(items[i].getCustomAttribute('sku').match("ENROLL_BUILDER"))
                required_volume = 300;
            else
                required_volume = 0;
               
            return required_volume;
        }
    }   
    
    return required_volume;
}

function Youngevity_ID_Used()
{
    var items = googlecart.getItems();
    
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('sku').match("EXISTING_CUSTOMER"))
            return true;
    }   
    
    return false;
}

function Remove_Youngevity_ID_Item()
{
    var items = googlecart.getItems();
    for (var i = 0; i < items.length; i++)
    {
        if(items[i].getCustomAttribute('sku').match("EXISTING_CUSTOMER"))
        {
            items[i].markForRemoval();
            googlecart.expungeMarkedForRemoval();
        }
    }
}

function Check_Temporary_ID()
{
    try
    {
        var items = googlecart.getItems();
    
        for (var i = 0; i < items.length; i++)
        {
            if(items[i].getCustomAttribute('product-attr-temp_id')!=null)
            {
                if(items[i].getCustomAttribute('product-attr-temp_id').match("TNT_ID"))
                {
                    var t_id = parseFloat(items[i].getCustomAttribute('product-attr-temp_id').replace("TNT_ID:",""));

                    if(t_id==0)
                        return false;
                    else if(t_id>0)
                        return true;
                        
                    break;
                }
            }
        }   
    }
    catch(err)
    {
      txt="There was an error on this page (Check_Temporary_ID).\n";
      txt+="Error description: " + err.description + "\n";
      txt+="Please contact us directly to continue your enrollment \n";
      txt+="If possible, please send us a copy of this error message so we can better serve you \n";
      txt = err.description;
      alert(txt);
    }

    
    return false;
}

function Set_Temporary_ID(temp_id)
{
    try
    {
        var items = googlecart.getItems();

        
        for (var i = 0; i < items.length; i++)
        {
            if(items[i].getCustomAttribute('temp_id')!=null)
            {
                if(items[i].getCustomAttribute('temp_id').match("TNT_ID"))
                {
                    var tS = "TNT_ID: " + temp_id; 
                    items[i].setCustomAttribute('temp_id', tS);
                    return "true";
                }
            }
        } 
    }
    
    catch(err)
    {
        error_app_callback.PerformCallback(err);
    }

    
    return "false";
}






function Calcuate_Shipping()
{
	try
	{
    var items = googlecart.getItems();   
    var standardShipping = 6.50;
    var expressShipping = 10.00;
    //var cart_subTotal_text = document.getElementById('googlecart-subtotal').innerHTML;
    //var cart_subTotal =  cart_subTotal_text.replace('$','');
    var cart_subTotal =  googlecart.getSubtotal();
    
if(cart_subTotal ==0)
return false;
    
    
    // loop over each element in the shopping cart
    for (var i = 0; i < items.length; i++)
    {
        var itemQuantity = items[i].getQuantity();
        if(items[i].getCustomAttribute('product-attr-shipping')!=null && items[i].getCustomAttribute('product-attr-shipping').match("Free Shipping"))
        {
            var i_p_unit = parseFloat(items[i].getPricePerUnit());
            var i_num_units = parseFloat(items[i].getQuantity());
                
            cart_subTotal = cart_subTotal-parseFloat(i_p_unit*i_num_units);
        }
    }
  
    //Calculate Custom Shipping
    if(cart_subTotal>81.25)
    {
        standardShipping = cart_subTotal*.08;
        expressShipping = cart_subTotal*.12;
    }
    else if(cart_subTotal==0)
    {
        standardShipping = 0;
        expressShipping = 0;
    }

  document.getElementById('standard-shipping').value = standardShipping;
  document.getElementById('express-shipping').value = expressShipping;

	return true;

}
catch(err)
    {
txt="There was an error on this page (Set_Shipping_Costs).\n";
      txt+="Error description: " + err.description + "\n";
      txt+="Please contact us directly to continue your enrollment \n";
      txt+="If possible, please send us a copy of this error message so we can better serve you \n";
      txt+="The shopping cart is set with a default shipping charge of $9,999 to catch such errors.\n";
      txt = err.description;
      alert(txt);
        error_app_callback.PerformCallback(err);

	return false;
    }


}

