Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

$(document).ready(function(){
        
        /**
                Shipping Address page 
        */
        if($("#shippingAddress").length){
                
                otherAddressCount       = 0;
                lastContainerID         = 0;
                lastAddressID           = 0;
                
                shippingAddressSetup();
        }
        
        $('#myaccountFormShippingAddress').validate({
                rules: {
                        name: {
                                required: true
                        },
                        line1: {
                                required: true
                        },
                        state: {
                                required: true
                        },
                        city: {
                                required: true
                        },
                        pincode: {
                                required: true,
                                minlength: 6,
                                maxlength: 6
                        },
                        phone: {
                                required: true,
                                minlength: 10,
                                maxlength: 10
                        }
                }
        });
});

$(function() {
        var zone = 'All';
        $('#store-address-container').find('tr').each(function() {
                if($(this).hasClass('default-address')) {
                        zone = $(this).attr('zone');
                }
        });
        $('#' + zone).attr('selected', true);
        $('#zone-selector').val(zone);
        showSpecificStores();
});

var showSpecificStores = function() {
        //var rows = $(this).parents('#hotspot-address-heading').siblings('#store-address-container').find('tr');
        var index = 0;
        var rows = $('#store-address-container').find('tr');
        if($('#zone-selector').val() == 'All') {
                $(rows).show();
        } else {
                $(rows).hide();
                $(rows).each(function() {
                        if($(this).attr('zone') == $('#zone-selector').val()) {
                                index++;
                                $(this).show();
                        }
                });
        }
        if(index == 0) {
                var count = $(rows).length + " stores";
        } else if(index == 1) {
                var count = index + " store";
        } else {
                var count= index + " stores";
        }
        trackEventWithGA("InStore Pickup Region", $('#zone-selector').val());
        $('#store-address-header-text').html("&nbsp;Select a pickup point (<b>" + count + "</b> available)");
};

$("#zone-selector").change(showSpecificStores);

$(".unselected-tab").live('click', function() {
        var tabToBeSelected = $(this).attr('id');
        var deliveryType = "In-Store Pickup";
        if(tabToBeSelected == 'tab-left') {
                $('#inStoreLocationMsg').addClass('invisibleDiv');
                $("#tabSelector").val("myLocation");
                $('#tabSelectorForAddAddressForm').val('myLocation');
                $('#newAddressFormHeading').text("Enter the address where you want the items to be delivered");
                deliveryType = "Deliver to Address";
        } else {
                $('#inStoreLocationMsg').removeClass('invisibleDiv');
                $("#tabSelector").val("HotSpot");
                $('#tabSelectorForAddAddressForm').val('HotSpot');
                $('#newAddressFormHeading').text("Enter the address which you want printed on the bill");
        }
        trackEventWithGA('DeliveryType', deliveryType);
        $(this).removeClass('unselected-tab').addClass('selected-tab');
        $(this).siblings().removeClass('selected-tab').addClass('unselected-tab');
        var hiddenPane = $(this).parent().siblings('.hidden-div');
        $(this).parent().siblings().addClass('hidden-div');
        $(hiddenPane).removeClass('hidden-div');
});

$('.shipping-page-addresses .button-address-select').click(function(){
        var addressId = $(this).attr('id').split('_')[1];
        $('#formChangeAddressTo_' + addressId).submit();
});

$('.shipping-page-addresses .store-address-selector-button').click(function(){
        var addressId = $(this).attr('id').split('_')[1];
        $('#formChangeStoreAddressTo_' + addressId).submit();
});

$('.shipping-page-addresses .billing-address-selector-button').live('click', function(){
        var addressId = $(this).closest('tr').attr('id').split('_')[1];
        $('#addressid').val(addressId);
        var image = $(this).closest('tr').siblings('.default-address').find('img')[0];
        var previousAddress = $(this).closest('tr').siblings('.default-address').attr('id').split('_')[1];
        $(image).attr('id', 'selectAddress_' + previousAddress);
        $(image).attr('src', '/images/RadioButton_Unselected.png');
        $(image).attr('title', 'Select this Store for Shipment');
        $(image).removeClass('dummy-class');
        $(image).addClass('billing-address-selector-button');
        $(this).closest('tr').siblings('.default-address').removeClass('default-address');
        $('#tr_' + addressId).addClass('default-address');
        $(this).attr('src', '/images/RadioButton_Selected.png');
        $(this).addClass('dummy-class');
        $(this).removeClass('billing-address-selector-button');
});



$(document).ready(function(){
        $('.default-address').focus();
});

/**
        Shipping Address page setup
*/
function shippingAddressSetup(){
        
        // Count no of other addresses for delete operation
        countOtherAddress();
        
        // Add Address button
        $("#shippingAddress input[name=addAddress1]").click(function(){
                addNewAddress();
        });

        $("#shippingAddress input[name=addAddress2]").click(function(){
                addNewAddress();
        });


        // Save and make primary address button
        $("#shippingAddress input[name=saveMakePriAddress]").click(function(){
                saveAddress('makePrimary');
        });

        // Save address button
        $("#shippingAddress input[name=saveAddress]").click(function(){
                saveAddress('save');
        });

        // Cancel address button
        $("#shippingAddress input[name=cancelAddress]").click(function(){
                resetShipingAddress();
        });
}

/**
        Count no. of other addresses
*/
function countOtherAddress(){
        var i                   = 0;
        var count               = 2;
        var addressID   = 0;
        var arrAddressID = [];
        var otherAddressCount = 0;
        
        if ($("#shippingAddress #A1 div").attr("id") != undefined) {
                addressID = parseInt($("#shippingAddress #A1 div").attr("id").substr(7));
                arrAddressID.push(addressID);
        }
        
        while(i === 0){
                if($("#shippingAddress").find("#A" + count + ":visible").length == 1){
                        addressID = parseInt($("#shippingAddress").find("#A" + count + " div").attr("id").substr(7));
                        arrAddressID.push(addressID);
                        
                        otherAddressCount++;    // Count of other addresses
                }else{
                        i = 1;
                }
                
                count++;
        }
        
        // sort address ids so that find the lastest address id
        for(i=0; i<arrAddressID.length; i++){
                for(j=i+1; j<arrAddressID.length; j++){
                        if(arrAddressID[i] < arrAddressID[j]){
                                temp = arrAddressID[i];
                                arrAddressID[i] = arrAddressID[j];
                                arrAddressID[j] = temp;
                        }
                }
        }
        
        lastContainerID = otherAddressCount + 1;        // last address container id
        lastAddressID   = arrAddressID[0];                      // last address id
}

/**
        Invoked on "Make Primary Address" btn click
*/
function makePriAddress(sourceId){
        ajaxMakePrimary(sourceId.substring(7,sourceId.length));
    var destId="";
    var destAddress="";
        // if primary address is present then take backup of primary id and address
        if($(".noAddress1:visible").length != 1){
                destId          = $("#A1 div").attr("id");
                destAddress = $("#" + destId + " .address").html();
        }
        
        var sourceIdParentId    = $("#" + sourceId).parent().attr("id");
        var sourceAddress               = $("#" + sourceId + " .address").html();
        
        // if primary address is present then swap address
        if($(".noAddress1:visible").length != 1){
                // Replace destination Id and its content with source
                $("#A1 #" + destId).attr("id", sourceId);
                $("#A1 #" + sourceId + " .address").html(sourceAddress);
                
                // Replace primary address delete button parameter with source id
                var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
                $("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html("");
                $("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html(priDelBtn);

                
                // Replace source Id and its content with destination
                $("#" + sourceIdParentId + " #" + sourceId).attr("id", destId);
                $("#" + sourceIdParentId + " #" + destId + " .address").html(destAddress);

                // Replace other address primary button parameter with destination id
                var priBtn = "<input type='button' class='button' value='Make Primary Address' onClick=makePriAddress('" + destId + "') />";
                $("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html("");
                $("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html(priBtn);
                
                // Replace other address delete button parameter with destination id
                var delBtn = "<input type='button' class='button' value='Delete' onClick=delAddress('" + destId + "') />";
                $("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html("");
                $("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html(delBtn);
        }else{
                // if no primary address is present then add new primary address
                addNewPriAddress(sourceId, sourceAddress);

                // Delete clicked swapping other address
                removeOtherAddress(sourceIdParentId);
        }
}

/**
        Invoked on "Delete" btn click of both primary and other address
*/
function delAddress(sourceId){
        ajaxDeleteAddress(sourceId.substring(7,sourceId.length));

        
        var sourceIdParentId = $("#" + sourceId).parent().attr("id");
        
        // on delete of primary address
        if(sourceIdParentId == "A1"){
        
                $("#" + sourceIdParentId + " #" + sourceId + " .address").html("");
                
                // make first other address as primary address if available
                if(otherAddressCount === 0){
                        $("#" + sourceIdParentId).html("");
                        $(".noAddress1").slideDown('slow');
                }else{
                        
                        // make first other address to primary and delete that
                        var destId = $("#A1 div").attr("id");
                        
                        sourceIdParentId = $("#addressContainer div").attr("id");
                        sourceId = $("#" + sourceIdParentId + " div").attr("id");               

                        var sourceAddress = $("#" + sourceId + " .address").html();
                        
                        // Replace destination Id and its content with source
                        $("#A1 #" + destId).attr("id", sourceId);
                        $("#A1 #" + sourceId + " .address").html(sourceAddress);
                        
                        // Replace primary address delete button parameter with source id
                        var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
                        $("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html("");
                        $("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
                        
                        ajaxMakePrimary(sourceId.substring(7,sourceId.length));
                        // Remove first other address
                        removeOtherAddress(sourceIdParentId);
                }
        }else{
                removeOtherAddress(sourceIdParentId);
        }
}

/**
        Invoked from delAddress() function
*/
function removeOtherAddress(sourceIdParentId){
        
        $("#" + sourceIdParentId).slideUp('slow', function(){
                $(this).remove();
                otherAddressCount--;
                
                if(otherAddressCount === 0){
                        $(".noAddress2").slideDown('slow');
                }
        });
}

/**
        Invoked on "Save and make primary Address " & "Save" btn
*/
function saveAddress(str){
        
        // form validation
        var nameVal     = jQuery.trim($("#shippingAddress #txtName").val());
        var add1Val     = jQuery.trim($("#shippingAddress #txtAddress").val());
        var stateVal    = $("#shippingAddress #state option:selected").val();
        var cityVal     = $("#shippingAddress #txtCity").val();
        var pinCodeVal  = jQuery.trim($("#shippingAddress #txtPinCode").val());
        var phoneVal    = jQuery.trim($("#shippingAddress #txtPhone").val());

                
        if(nameVal.length === 0){
                alert("Please enter name.");
                
                $("#shippingAddress #txtName").focus();
                return false;   
        }else if(add1Val.length === 0){
                alert("Please enter address.");
                
                $("#shippingAddress #txtAddress").focus();
                return false;   
        }else if(stateVal == "0"){
                alert("Please select state.");
                
                $("#shippingAddress #state").focus();
                return false;   
        }else if(phoneVal.length === 0){
                alert("Please enter phone no.");
                
                $("#shippingAddress #txtPhone").focus();
                return false;           
        }else if(pinCodeVal.length == 0){
                alert("Please enter pin code.");
                
                $("#shippingAddress #txtPinCode").focus();
                return false;   
        }else if(cityVal.length == 0){
                alert("Please enter city.");
                
                $("#shippingAddress #txtCity").focus();
                return false;   
        }else{
                if(str == "makePrimary"){
                        addPriAddress();
                }else if(str == "save"){
                        addOtherAddress("save", 0 , "");
                }
        }
        
        // Reset Form
        resetShipingAddress();
}

/**
        Invoke from saveAddress() function
*/
function addPriAddress(){
    var priAddressID = "";
    var priAddress = "";
        // if primary address is present then take backup of primary id and address to make as first other id and address
        if($(".noAddress1:visible").length != 1){
                priAddressID    = $("#A1 div").attr("id");
                priAddress      = $("#" + priAddressID + " .address").html();
        }

        var priName     = $("#shippingAddress #txtName").val();
        var priAdd1     = $("#shippingAddress #txtAddress").val();
        var priAdd2     = $("#shippingAddress #txtAddress2").val();
        var priCity     = $("#shippingAddress #txtCity").val();
        var priPinCode  = $("#shippingAddress #txtPinCode").val();
        var priState    = $("#shippingAddress #state option:selected").val();
        var priPhone    = $("#shippingAddress #txtPhone").val();
        var priEmail = "";
        
        var newPriAddressID     = "address" + (++lastAddressID);        // create new primary id to get newly inserted data from form   

        var newPriAddress = priName + "<br />" + priAdd1 + "<br />" + priAdd2 + "<br />" + priCity + "<br />" + priPinCode + "<br />" + priState + "<br /> Phone: " + priPhone ;
        
        // if primary address is present then add new primary address and shift old primary address as first other address
        if($(".noAddress1:visible").length != 1){
                $("#A1 #" + priAddressID).attr("id", newPriAddressID);
                $("#A1 #" + newPriAddressID + " .address").html(newPriAddress);
                
                // Replace primary address delete button parameter with new primary id
                var priDelBtn = "<input onclick=delAddress('" + newPriAddressID + "') value='Delete' class='button' type='button' />";
                $("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html("");
                $("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
                
                ajaxAddAddress("true", priName, priAdd1, priAdd2, priCity, priState, priPinCode, priPhone, priEmail);
                
                // shift old primary address to make first other address
                addOtherAddress("", priAddressID, priAddress);
        }else{
                
                // if no primary address is present then add new primary address
                addNewPriAddress(newPriAddressID, newPriAddress);
        }
}

/**
        Invoke from saveAddress(), addPriAddress() functions
        
        params:
                > condition = save      // for saving other address only
                > id                            // for old primary id
                > address                       // for old primary address
*/
function addOtherAddress(condition, id, address){
        var newContainerID      = "A" + (++lastContainerID);
        var newAddressID        = (condition == "save") ? "address" + (++lastAddressID) : id;
        var container           = "";
        
        if(condition == "save"){
                var otherName           = $("#shippingAddress #txtName").val();
                var otherAdd1           = $("#shippingAddress #txtAddress").val();
                var otherAdd2           = $("#shippingAddress #txtAddress2").val();
                var otherCity           = $("#shippingAddress #txtCity").val();
                var otherPinCode        = $("#shippingAddress #txtPinCode").val();
                var otherState          = $("#shippingAddress #state option:selected").val();
                var otherPhone  = $("#shippingAddress #txtPhone").val();
                var otherEmail = "";
                var otherAddress = otherName + "<br />" + otherAdd1 + "<br />" + otherAdd2 + "<br />" + otherCity + "<br />" + otherPinCode + "<br />" + otherState + "<br /> Phone: " + otherPhone;
        
                ajaxAddAddress("false", otherName, otherAdd1, otherAdd2, otherCity, otherState, otherPinCode, otherPhone, otherEmail);
                
        }else{
                otherAddress = address;
        }

        container = '<div id="' + newContainerID + '">';
                container += '  <div id="' + newAddressID + '">';
                
                        if(condition == "save"){
                                container += (otherAddressCount != 0) ? "<br /><br />" : "";
                        }
                
                        // Address
                        container += '<div class="address">';
                        container += otherAddress;
                        container += '</div>';
                        
                        // Buttons
                        container += '<div class="addressButton">';
                        
                                // Delete button
                                container += '<div class="imgDeleteButton deleteWidth">';
                                container += '  <div class="left">';
                                container += '  <div class="right">';
                                container += '          <input type="button" onclick="delAddress(\'' + newAddressID + '\');" value="Delete" class="button" />';
                                container += '  </div>';
                                container += '  </div>';
                                container += '</div>';
                        
                                // Make primary address button
                                container += '<div class="imgEnableButton priAddressWidth">';
                                container += '          <div class="left">';
                                container += '          <div class="right">';
                                container += '                   <input type="button" onclick="makePriAddress(\'' + newAddressID + '\');" value="Make Primary Address" class="button" />';
                                container += '          </div>';
                                container += '          </div>';
                                container += '</div>';
                                container += '<div class="clearBoth"></div>';
                                
                        container += '</div>';
                        
                        if(condition != "save"){
                                container += (otherAddressCount != 0)? "<br /><br />" : "";
                        }
                container += '</div>';
        container += '</div>';
        
        if(condition == "save"){
                $("#addressContainer").append(container);       // to save other address
        }else{  
                $("#addressContainer").prepend(container);      // to save old primary address as first other address
        }
        
        if(otherAddressCount == 0){
                $(".noAddress2").hide();
        }
        
        otherAddressCount++;
        
        
}


/**
        Inovke from makePriAddress(), addPriAddress() function
*/
function addNewPriAddress(id, address){

        if($(".noAddress1:visible").length == 1){
                $(".noAddress1").hide();
        }
        
        var container = "";
        container = '<div id="' + id + '">';
        
                // Address
                container += '<div class="address">';
                container += address;
                container += '</div>';
                
                // Buttons
                container += '<div class="addressButton">';
                container += '  <div class="imgDeleteButton deleteWidth">';
                container += '          <div class="left"><div class="right">';
                container += '                  <input type="button" class="button" value="Delete" onclick="delAddress(\'' + id + '\');">';
                container += '          </div></div>';
                container += '  </div>';
                container += '</div>';
                
        container += '</div>';
        
        $("#A1").append(container);
        
}

/**
        Add address for Shipping Address page
*/
function addNewAddress(){
        // Disable first addAddress button
        $("#addAddress1").removeClass('imgEnableButton').addClass('imgDisableButton');
        $("#addAddress1 input[name='addAddress1']").attr('disabled','disabled');
        
        // Hide second addAddress button
        $("#addAddress2").hide();
        
        // Show new address form
        $("#addNewAddress").show();
        
        $("#addNewAddress #txtName").focus();
}

/**
 * 
        Reset Shiping address for Shipping Address page
*/
function resetShipingAddress(){
        // Enable first addAddress button
        $("#addAddress1").removeClass('imgDisableButton').addClass('imgEnableButton');
        $("#addAddress1 input[disabled='']").removeAttr('disabled');
        
        // Show second addAddress button
        $("#addAddress2").show();
        
        // Hide add new address form
        $("#addNewAddress").hide();
        
        $("#shippingAddress #txtName").val("");
        $("#shippingAddress #txtAddress").val("");
        $("#shippingAddress #txtAddress2").val("");
        $("#shippingAddress #txtCity").val("");
        $("#shippingAddress #state option[value='0']").attr('selected', 'selected');
        $("#shippingAddress #txtPinCode").val("");
        $("#shippingAddress #txtPhone").val("");
}


function ajaxAddAddress(isprimary, name, add1, add2, city, state, pin, phone, email){
        jQuery.ajax({
                  type: "POST",
                  url: "/address",
                  data: "action=add&default="+isprimary+"&name="+name+"&line1="+add1+"&line2="+add2+"&city="+city
                  +"&state="+state+"&pincode="+pin+"&phone="+phone+"&country=India",
                  success: function(msg){
                  }
        });
}

function ajaxMakePrimary(addressid){
        jQuery.ajax({
                  type: "POST",
                  url: "/address",
                  data: "action=setdefault&addressid="+addressid,
                  success: function(msg){
                  }
        });
}

function ajaxDeleteAddress(addressid){
        jQuery.ajax({
                  type: "POST",
                  url: "/address",
                  data: "action=delete&addressid="+addressid,
                  success: function(msg){
                  }
        });
}