Subversion Repositories SmartDukaan

Rev

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