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