Rev 1035 | Rev 4148 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
$(document).ready(function(){/**Shipping Address page*/if($("#shippingAddress").length){otherAddressCount = 0;lastContainerID = 0;lastAddressID = 0;shippingAddressSetup();}});/**Shipping Address page setup*/function shippingAddressSetup(){// Count no of other addresses for delete operationcountOtherAddress();// 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 idfor(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 idlastAddressID = arrAddressID[0]; // last address id}/**Invoked on "Make Primary Address" btn click*/function makePriAddress(sourceId){//alert("ajax to make primary "+ sourceId.substring(7,sourceId.length));ajaxMakePrimary(sourceId.substring(7,sourceId.length));var destId="";var destAddress="";// if primary address is present then take backup of primary id and addressif($(".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 addressif($(".noAddress1:visible").length != 1){//alert("ids before swaping\n primary address id '" + destId + "' & \n other address id '" + sourceId + "'");// 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 idvar 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 idvar 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 idvar delBtn = "<input type='button' class='button' value='Delete' onClick=delAddress('" + destId + "') />";$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html("");$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html(delBtn);//alert("'" + sourceId + "' id is now primary & \n'" + destId + "' id become other address");}else{// if no primary address is present then add new primary addressaddNewPriAddress(sourceId, sourceAddress);// Delete clicked swapping other addressremoveOtherAddress(sourceIdParentId);}}/**Invoked on "Delete" btn click of both primary and other address*/function delAddress(sourceId){//alert("ajax to DELETE "+ sourceId.substring(7,sourceId.length));ajaxDeleteAddress(sourceId.substring(7,sourceId.length));var sourceIdParentId = $("#" + sourceId).parent().attr("id");// on delete of primary addressif(sourceIdParentId == "A1"){$("#" + sourceIdParentId + " #" + sourceId + " .address").html("");alert("Ajax call to delete" + sourceId);// make first other address as primary address if availableif(otherAddressCount === 0){$("#" + sourceIdParentId).html("");$(".noAddress1").slideDown('slow');}else{// make first other address to primary and delete thatvar 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 idvar 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);//alert("Ajax call make primary" + sourceId.substring(7,sourceId.length));ajaxMakePrimary(sourceId.substring(7,sourceId.length));// Remove first other addressremoveOtherAddress(sourceIdParentId);}}else{removeOtherAddress(sourceIdParentId);//alert("Ajax call to delete" + sourceId);}}/**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 validationvar 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 FormresetShipingAddress();}/**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 addressif($(".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 formvar 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 addressif($(".noAddress1:visible").length != 1){$("#A1 #" + priAddressID).attr("id", newPriAddressID);$("#A1 #" + newPriAddressID + " .address").html(newPriAddress);// Replace primary address delete button parameter with new primary idvar 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);//alert("Ajax call to add new primary address" + newPriAddressID);ajaxAddAddress("true", priName, priAdd1, priAdd2, priCity, priState, priPinCode, priPhone, priEmail);// shift old primary address to make first other addressaddOtherAddress("", priAddressID, priAddress);}else{// if no primary address is present then add new primary addressaddNewPriAddress(newPriAddressID, newPriAddress);}}/**Invoke from saveAddress(), addPriAddress() functionsparams:> condition = save // for saving other address only> id // for old primary id> address // for old primary address*/function addOtherAddress(condition, id, address){//alert(condition +"id"+ id + "add"+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;//alert("before ajax");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 />" : "";}// Addresscontainer += '<div class="address">';container += otherAddress;container += '</div>';// Buttonscontainer += '<div class="addressButton">';// Delete buttoncontainer += '<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 buttoncontainer += '<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++;//alert("ajax to add in other address"+ newAddressID);}/**Inovke from makePriAddress(), addPriAddress() function*/function addNewPriAddress(id, address){if($(".noAddress1:visible").length == 1){$(".noAddress1").hide();}var container = "";container = '<div id="' + id + '">';// Addresscontainer += '<div class="address">';container += address;container += '</div>';// Buttonscontainer += '<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);//alert("AJAX to id is now primary" + id);}/**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();//scrollWindow("addNewAddress", 2000);$("#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();//scrollWindow("shippingAddress", 1000);$("#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){//alert("in ajax" +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){// alert( "Data Saved: " + msg );// $("#addressid").val(msg);}});}function ajaxMakePrimary(addressid){jQuery.ajax({type: "POST",url: "./address",data: "action=setdefault&addressid="+addressid,success: function(msg){// alert( "Data Saved: " + msg );// $("#addressid").val(msg);}});}function ajaxDeleteAddress(addressid){jQuery.ajax({type: "POST",url: "./address",data: "action=delete&addressid="+addressid,success: function(msg){//alert( "Data Saved: " + msg );// $("#addressid").val(msg);}});}