﻿/* ------- Geo Targeting ------ */
// GetUserZip takes a parameter, bUseGeotargeting - This will determine if geotargeting should be done on the user or not if the ZIP code cookie is not present.
function GetUserZip(bUseGeotargeting){
    var customerzip = null;
    
    customerzip = GetCookie("customerzip");
    var iptargeted = GetCookie("iptargeted")
    if (customerzip == "null" || customerzip == null) {
        customerzip = '';
    }


    if ( (customerzip == '') && (!iptargeted) && (bUseGeotargeting) ) {       
        $.ajax({
            url:        "/handlers/tools/shopping/getuserzip.ashx",
            timeout:    10000,
            async:      false,
            complete:   function(XMLHttpRequest, textStatus){
                            if (textStatus == "success"){
                                // Set cookie to indicate that IP address was geotargeted so we don't have to call the service over and over if no ZIP was located.
                                SetCookie("iptargeted", "true");
                                customerzip = XMLHttpRequest.responseText;
                                // Only set cookie if a zip was found                                
                                if ( (customerzip != null) && (customerzip != "null") && (customerzip != "") ) {
                                    SetUserZip(customerzip);
                                }
                            }
                        }
        });
    }
    
    return (customerzip == "null" || customerzip == null) ? "" : customerzip;

}

function SetUserZip(sUserZip) { 
    var oDate = new Date();
    oDate.setDate(oDate.getDate() + 30); //30 day duration                                
    SetCookie("customerzip", sUserZip , oDate); 
}