Subversion Repositories SmartDukaan

Rev

Rev 1035 | Rev 1919 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 rajveer 1
$(document).ready(function(){
2
 
3
	/**
4
		Shipping Address page 
5
	*/
6
	if($("#shippingAddress").length){
7
 
8
		otherAddressCount 	= 0;
9
		lastContainerID 	= 0;
10
		lastAddressID		= 0;
11
 
12
		shippingAddressSetup();
13
	}
14
 
15
});
16
 
17
/**
18
	Shipping Address page setup
19
*/
20
function shippingAddressSetup(){
21
 
22
	// Count no of other addresses for delete operation
23
	countOtherAddress();
24
 
25
	// Add Address button
26
	$("#shippingAddress input[name=addAddress1]").click(function(){
27
		addNewAddress();
28
	});
29
 
30
	$("#shippingAddress input[name=addAddress2]").click(function(){
31
		addNewAddress();
32
	});
33
 
34
 
35
	// Save and make primary address button
36
	$("#shippingAddress input[name=saveMakePriAddress]").click(function(){
37
		saveAddress('makePrimary');
38
	});
39
 
40
	// Save address button
41
	$("#shippingAddress input[name=saveAddress]").click(function(){
42
		saveAddress('save');
43
	});
44
 
45
	// Cancel address button
46
	$("#shippingAddress input[name=cancelAddress]").click(function(){
47
		resetShipingAddress();
48
	});
49
}
50
 
51
/**
52
	Count no. of other addresses
53
*/
54
function countOtherAddress(){
55
	var i 			= 0;
56
	var count 		= 2;
57
	var addressID 	= 0;
1048 chandransh 58
	var arrAddressID = [];
789 vikas 59
	var otherAddressCount = 0;
595 rajveer 60
 
789 vikas 61
	if ($("#shippingAddress #A1 div").attr("id") != undefined) {
62
		addressID = parseInt($("#shippingAddress #A1 div").attr("id").substr(7));
63
		arrAddressID.push(addressID);
64
	}
595 rajveer 65
 
1048 chandransh 66
	while(i === 0){
595 rajveer 67
		if($("#shippingAddress").find("#A" + count + ":visible").length == 1){
68
			addressID = parseInt($("#shippingAddress").find("#A" + count + " div").attr("id").substr(7));
69
			arrAddressID.push(addressID);
70
 
71
			otherAddressCount++;	// Count of other addresses
72
		}else{
73
			i = 1;
74
		}
75
 
76
		count++;
77
	}
78
 
79
	// sort address ids so that find the lastest address id
80
	for(i=0; i<arrAddressID.length; i++){
81
		for(j=i+1; j<arrAddressID.length; j++){
82
			if(arrAddressID[i] < arrAddressID[j]){
83
				temp = arrAddressID[i];
84
				arrAddressID[i] = arrAddressID[j];
85
				arrAddressID[j] = temp;
86
			}
87
		}
88
	}
89
 
90
	lastContainerID	= otherAddressCount + 1;	// last address container id
91
	lastAddressID 	= arrAddressID[0];			// last address id
92
}
93
 
94
/**
95
	Invoked on "Make Primary Address" btn click
96
*/
97
function makePriAddress(sourceId){
929 rajveer 98
	//alert("ajax to make primary "+ sourceId.substring(7,sourceId.length));
595 rajveer 99
	ajaxMakePrimary(sourceId.substring(7,sourceId.length));
1048 chandransh 100
    var destId="";
101
    var destAddress="";
595 rajveer 102
	// if primary address is present then take backup of primary id and address
103
	if($(".noAddress1:visible").length != 1){
1048 chandransh 104
		destId 		= $("#A1 div").attr("id");
105
		destAddress = $("#" + destId + " .address").html();
595 rajveer 106
	}
107
 
108
	var sourceIdParentId 	= $("#" + sourceId).parent().attr("id");
109
	var sourceAddress 		= $("#" + sourceId + " .address").html();
110
 
111
	// if primary address is present then swap address
112
	if($(".noAddress1:visible").length != 1){
113
 
929 rajveer 114
		//alert("ids before swaping\n primary address id '" + destId + "' & \n other address id '" + sourceId + "'");
595 rajveer 115
 
116
		// Replace destination Id and its content with source
117
		$("#A1 #" + destId).attr("id", sourceId);
118
		$("#A1 #" + sourceId + " .address").html(sourceAddress);
119
 
120
		// Replace primary address delete button parameter with source id
121
		var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
858 vikas 122
		$("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html("");
123
		$("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html(priDelBtn);
595 rajveer 124
 
125
 
126
		// Replace source Id and its content with destination
127
		$("#" + sourceIdParentId + " #" + sourceId).attr("id", destId);
128
		$("#" + sourceIdParentId + " #" + destId + " .address").html(destAddress);
129
 
130
		// Replace other address primary button parameter with destination id
131
		var priBtn = "<input type='button' class='button' value='Make Primary Address' onClick=makePriAddress('" + destId + "') />";
858 vikas 132
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html("");
133
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html(priBtn);
595 rajveer 134
 
135
		// Replace other address delete button parameter with destination id
136
		var delBtn = "<input type='button' class='button' value='Delete' onClick=delAddress('" + destId + "') />";
858 vikas 137
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html("");
138
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html(delBtn);
595 rajveer 139
 
929 rajveer 140
		//alert("'" + sourceId + "' id is now primary & \n'" + destId + "' id become other address");
595 rajveer 141
	}else{
142
		// if no primary address is present then add new primary address
143
		addNewPriAddress(sourceId, sourceAddress);
144
 
145
		// Delete clicked swapping other address
146
		removeOtherAddress(sourceIdParentId);
147
	}
148
}
149
 
150
/**
151
	Invoked on "Delete" btn click of both primary and other address
152
*/
153
function delAddress(sourceId){
929 rajveer 154
	//alert("ajax to DELETE "+ sourceId.substring(7,sourceId.length));
595 rajveer 155
	ajaxDeleteAddress(sourceId.substring(7,sourceId.length));
156
 
157
 
158
	var sourceIdParentId = $("#" + sourceId).parent().attr("id");
159
 
160
	// on delete of primary address
161
	if(sourceIdParentId == "A1"){
162
 
163
		$("#" + sourceIdParentId + " #" + sourceId + " .address").html("");
164
 
165
 
166
		alert("Ajax call to delete" + sourceId);
167
 
168
		// make first other address as primary address if available
1048 chandransh 169
		if(otherAddressCount === 0){
595 rajveer 170
			$("#" + sourceIdParentId).html("");
171
			$(".noAddress1").slideDown('slow');
172
		}else{
173
 
174
			// make first other address to primary and delete that
175
			var destId = $("#A1 div").attr("id");
176
 
1048 chandransh 177
			sourceIdParentId = $("#addressContainer div").attr("id");
595 rajveer 178
			sourceId = $("#" + sourceIdParentId + " div").attr("id");		
179
 
180
			var sourceAddress = $("#" + sourceId + " .address").html();
181
 
182
			// Replace destination Id and its content with source
183
			$("#A1 #" + destId).attr("id", sourceId);
184
			$("#A1 #" + sourceId + " .address").html(sourceAddress);
185
 
186
			// Replace primary address delete button parameter with source id
187
			var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
188
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html("");
189
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
190
 
191
 
929 rajveer 192
			//alert("Ajax call make primary" + sourceId.substring(7,sourceId.length));
595 rajveer 193
			ajaxMakePrimary(sourceId.substring(7,sourceId.length));
194
			// Remove first other address
195
			removeOtherAddress(sourceIdParentId);
196
		}
197
	}else{
198
		removeOtherAddress(sourceIdParentId);
199
 
929 rajveer 200
		//alert("Ajax call to delete" + sourceId);
595 rajveer 201
	}
202
}
203
 
204
/**
205
	Invoked from delAddress() function
206
*/
207
function removeOtherAddress(sourceIdParentId){
208
 
209
	$("#" + sourceIdParentId).slideUp('slow', function(){
210
		$(this).remove();
211
		otherAddressCount--;
212
 
1048 chandransh 213
		if(otherAddressCount === 0){
595 rajveer 214
			$(".noAddress2").slideDown('slow');
215
		}
216
	});
217
}
218
 
219
/**
220
	Invoked on "Save and make primary Address " & "Save" btn
221
*/
222
function saveAddress(str){
223
 
224
	// form validation
225
	var nameVal 	= jQuery.trim($("#shippingAddress #txtName").val());
226
	var add1Val 	= jQuery.trim($("#shippingAddress #txtAddress").val());
227
	var stateVal 	= $("#shippingAddress #state option:selected").val();
228
	var cityVal 	= $("#shippingAddress #txtCity").val();
229
	var pinCodeVal  = jQuery.trim($("#shippingAddress #txtPinCode").val());
230
	var phoneVal	= jQuery.trim($("#shippingAddress #txtPhone").val());
231
 
232
 
1048 chandransh 233
	if(nameVal.length === 0){
595 rajveer 234
		alert("Please enter name.");
235
 
236
		$("#shippingAddress #txtName").focus();
237
		return false;	
1048 chandransh 238
	}else if(add1Val.length === 0){
595 rajveer 239
		alert("Please enter address.");
240
 
241
		$("#shippingAddress #txtAddress").focus();
242
		return false;	
243
	}else if(stateVal == "0"){
244
		alert("Please select state.");
245
 
246
		$("#shippingAddress #state").focus();
247
		return false;	
1048 chandransh 248
	}else if(phoneVal.length === 0){
595 rajveer 249
		alert("Please enter phone no.");
250
 
251
		$("#shippingAddress #txtPhone").focus();
252
		return false;		
253
	}else if(pinCodeVal.length == 0){
254
		alert("Please enter pin code.");
255
 
256
		$("#shippingAddress #txtPinCode").focus();
257
		return false;	
258
	}else if(cityVal.length == 0){
259
		alert("Please enter city.");
260
 
261
		$("#shippingAddress #txtCity").focus();
262
		return false;	
263
	}else{
264
		if(str == "makePrimary"){
265
			addPriAddress();
266
		}else if(str == "save"){
267
			addOtherAddress("save", 0 , "");
268
		}
269
	}
270
 
271
	// Reset Form
272
	resetShipingAddress();
273
}
274
 
275
/**
276
	Invoke from saveAddress() function
277
*/
278
function addPriAddress(){
1048 chandransh 279
    var priAddressID = "";
280
    var priAddress = "";
595 rajveer 281
	// if primary address is present then take backup of primary id and address to make as first other id and address
282
	if($(".noAddress1:visible").length != 1){
1048 chandransh 283
		priAddressID 	= $("#A1 div").attr("id");
284
		priAddress	= $("#" + priAddressID + " .address").html();
595 rajveer 285
	}
286
 
287
	var priName 	= $("#shippingAddress #txtName").val();
288
	var priAdd1 	= $("#shippingAddress #txtAddress").val();
289
	var priAdd2 	= $("#shippingAddress #txtAddress2").val();
290
	var priCity 	= $("#shippingAddress #txtCity").val();
291
	var priPinCode 	= $("#shippingAddress #txtPinCode").val();
292
	var priState 	= $("#shippingAddress #state option:selected").val();
293
	var priPhone 	= $("#shippingAddress #txtPhone").val();
294
	var priEmail = "";
295
 
296
	var newPriAddressID	= "address" + (++lastAddressID);	// create new primary id to get newly inserted data from form	
297
 
1048 chandransh 298
	var newPriAddress = priName + "<br />" + priAdd1 + "<br />" + priAdd2 + "<br />" + priCity + "<br />" + priPinCode + "<br />" + priState + "<br /> Phone: " + priPhone ;
595 rajveer 299
 
300
	// if primary address is present then add new primary address and shift old primary address as first other address
301
	if($(".noAddress1:visible").length != 1){
302
		$("#A1 #" + priAddressID).attr("id", newPriAddressID);
303
		$("#A1 #" + newPriAddressID + " .address").html(newPriAddress);
304
 
305
		// Replace primary address delete button parameter with new primary id
306
		var priDelBtn = "<input onclick=delAddress('" + newPriAddressID + "') value='Delete' class='button' type='button' />";
307
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html("");
308
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
309
 
929 rajveer 310
		//alert("Ajax call to add new primary address" + newPriAddressID);
595 rajveer 311
 
312
 
313
		ajaxAddAddress("true", priName, priAdd1, priAdd2, priCity, priState, priPinCode, priPhone, priEmail);
314
 
315
		// shift old primary address to make first other address
316
		addOtherAddress("", priAddressID, priAddress);
317
	}else{
318
 
319
		// if no primary address is present then add new primary address
320
		addNewPriAddress(newPriAddressID, newPriAddress);
321
	}
322
}
323
 
324
/**
325
	Invoke from saveAddress(), addPriAddress() functions
326
 
327
	params:
328
		> condition = save	// for saving other address only
329
		> id 				// for old primary id
330
		> address 			// for old primary address
331
*/
332
function addOtherAddress(condition, id, address){
929 rajveer 333
	//alert(condition +"id"+ id +  "add"+address);
595 rajveer 334
	var newContainerID 	= "A" + (++lastContainerID);
335
	var newAddressID	= (condition == "save") ? "address" + (++lastAddressID) : id;
336
	var container 		= "";
337
 
338
	if(condition == "save"){
339
		var otherName		= $("#shippingAddress #txtName").val();
340
		var otherAdd1 		= $("#shippingAddress #txtAddress").val();
341
		var otherAdd2 		= $("#shippingAddress #txtAddress2").val();
342
		var otherCity 		= $("#shippingAddress #txtCity").val();
343
		var otherPinCode 	= $("#shippingAddress #txtPinCode").val();
344
		var otherState 		= $("#shippingAddress #state option:selected").val();
345
		var otherPhone 	= $("#shippingAddress #txtPhone").val();
346
		var otherEmail = "";
347
		var otherAddress = otherName + "<br />" + otherAdd1 + "<br />" + otherAdd2 + "<br />" + otherCity + "<br />" + otherPinCode + "<br />" + otherState + "<br /> Phone: " + otherPhone;
348
 
929 rajveer 349
		//alert("before ajax");
595 rajveer 350
		ajaxAddAddress("false", otherName, otherAdd1, otherAdd2, otherCity, otherState, otherPinCode, otherPhone, otherEmail);
351
 
352
	}else{
353
		otherAddress = address;
354
	}
355
 
356
	container = '<div id="' + newContainerID + '">';
357
		container += '	<div id="' + newAddressID + '">';
358
 
359
			if(condition == "save"){
360
				container += (otherAddressCount != 0) ? "<br /><br />" : "";
361
			}
362
 
363
			// Address
364
			container += '<div class="address">';
365
			container += otherAddress;
366
			container += '</div>';
367
 
368
			// Buttons
369
			container += '<div class="addressButton">';
370
 
371
				// Delete button
372
				container += '<div class="imgDeleteButton deleteWidth">';
373
				container += '	<div class="left">';
374
				container += '	<div class="right">';
375
				container += '		<input type="button" onclick="delAddress(\'' + newAddressID + '\');" value="Delete" class="button" />';
376
				container += '	</div>';
377
				container += '	</div>';
378
				container += '</div>';
379
 
380
				// Make primary address button
381
				container += '<div class="imgEnableButton priAddressWidth">';
382
				container += '		<div class="left">';
383
				container += '		<div class="right">';
384
				container += '			 <input type="button" onclick="makePriAddress(\'' + newAddressID + '\');" value="Make Primary Address" class="button" />';
385
				container += '		</div>';
386
				container += '		</div>';
387
				container += '</div>';
388
				container += '<div class="clearBoth"></div>';
389
 
390
			container += '</div>';
391
 
392
			if(condition != "save"){
393
				container += (otherAddressCount != 0)? "<br /><br />" : "";
394
			}
395
		container += '</div>';
396
	container += '</div>';
397
 
398
	if(condition == "save"){
399
		$("#addressContainer").append(container);	// to save other address
400
	}else{	
401
		$("#addressContainer").prepend(container);	// to save old primary address as first other address
402
	}
403
 
404
	if(otherAddressCount == 0){
405
		$(".noAddress2").hide();
406
	}
407
 
408
	otherAddressCount++;
409
 
929 rajveer 410
	//alert("ajax to add in other address"+ newAddressID);
595 rajveer 411
 
412
 
413
}
414
 
415
 
416
/**
417
	Inovke from makePriAddress(), addPriAddress() function
418
*/
419
function addNewPriAddress(id, address){
420
 
421
	if($(".noAddress1:visible").length == 1){
422
		$(".noAddress1").hide();
423
	}
424
 
425
	var container = "";
426
	container = '<div id="' + id + '">';
427
 
428
		// Address
429
		container += '<div class="address">';
430
		container += address;
431
		container += '</div>';
432
 
433
		// Buttons
434
		container += '<div class="addressButton">';
435
		container += '	<div class="imgDeleteButton deleteWidth">';
436
		container += '		<div class="left"><div class="right">';
437
		container += '			<input type="button" class="button" value="Delete" onclick="delAddress(\'' + id + '\');">';
438
		container += '		</div></div>';
439
		container += '	</div>';
440
		container += '</div>';
441
 
442
	container += '</div>';
443
 
444
	$("#A1").append(container);
445
 
929 rajveer 446
	//alert("AJAX to  id is now primary" + id);
595 rajveer 447
 
448
}
449
 
450
/**
451
	Add address for Shipping Address page
452
*/
453
function addNewAddress(){
454
	// Disable first addAddress button
455
	$("#addAddress1").removeClass('imgEnableButton').addClass('imgDisableButton');
456
	$("#addAddress1 input[name='addAddress1']").attr('disabled','disabled');
457
 
458
	// Hide second addAddress button
459
	$("#addAddress2").hide();
460
 
461
	// Show new address form
462
	$("#addNewAddress").show();
463
	//scrollWindow("addNewAddress", 2000);
464
 
465
	$("#addNewAddress #txtName").focus();
466
}
467
 
468
/**
469
 * 
470
	Reset Shiping address for Shipping Address page
471
*/
472
function resetShipingAddress(){
473
	// Enable first addAddress button
474
	$("#addAddress1").removeClass('imgDisableButton').addClass('imgEnableButton');
475
	$("#addAddress1 input[disabled='']").removeAttr('disabled');
476
 
477
	// Show second addAddress button
478
	$("#addAddress2").show();
479
 
480
	// Hide add new address form
481
	$("#addNewAddress").hide();
482
	//scrollWindow("shippingAddress", 1000);
483
 
484
	$("#shippingAddress #txtName").val("");
485
	$("#shippingAddress #txtAddress").val("");
486
	$("#shippingAddress #txtAddress2").val("");
487
	$("#shippingAddress #txtCity").val("");
488
	$("#shippingAddress #state option[value='0']").attr('selected', 'selected');
489
	$("#shippingAddress #txtPinCode").val("");
490
	$("#shippingAddress #txtPhone").val("");
491
}
492
 
493
 
494
function ajaxAddAddress(isprimary, name, add1, add2, city, state, pin, phone, email){
929 rajveer 495
	//alert("in ajax" +isprimary + name + add1 + add2 + city + state + pin + phone + email);
595 rajveer 496
	jQuery.ajax({
497
		  type: "POST",
498
		  url: "./address",
499
		  data: "action=add&default="+isprimary+"&name="+name+"&line1="+add1+"&line2="+add2+"&city="+city
500
		  +"&state="+state+"&pincode="+pin+"&phone="+phone+"&country=India",
501
		  success: function(msg){
929 rajveer 502
			//  alert( "Data Saved: " + msg );
595 rajveer 503
			  // 	$("#addressid").val(msg);
504
		  }
505
	});
506
}
507
 
508
function ajaxMakePrimary(addressid){
509
	jQuery.ajax({
510
		  type: "POST",
511
		  url: "./address",
512
		  data: "action=setdefault&addressid="+addressid,
513
		  success: function(msg){
929 rajveer 514
			 // alert( "Data Saved: " + msg );
595 rajveer 515
			   //	$("#addressid").val(msg);
516
		  }
517
	});
518
}
519
 
520
function ajaxDeleteAddress(addressid){
521
	jQuery.ajax({
522
		  type: "POST",
523
		  url: "./address",
524
		  data: "action=delete&addressid="+addressid,
525
		  success: function(msg){
929 rajveer 526
			  //alert( "Data Saved: " + msg );
595 rajveer 527
			   //	$("#addressid").val(msg);
528
		  }
529
	});
1048 chandransh 530
}