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