/**
 * @author ajie
 */
//make sure YOUR path is correct!!
			Ext.BLANK_IMAGE_URL = 'lib/ext/resources/images/default/s.gif';

			var myAccountPage = {
			
				kirimIDpelanggan:function(){
					if (form.idPelanggan.value && form.email.value) {
						window.alert('Terima kasih.\nAnda akan dihubungi pihak YesTV untuk dikonfirmasi lebih lanjut.');
						window.document.submit;
					}
				},
				
				updatePilihan:function(){
					var t  = Ext.DomQuery.selectValue('input[type=radio]:checked/@value');
					Ext.get('pilihan').dom.value = t;
				},
				
				cekPilihan:function() {
					var ra = Ext.DomQuery.selectValue('input[type=radio]:checked/@value');
					if (ra != null) window.document.submit;
				},
					
				changePassword:function(){
					alert("masup juga");
					
				},
				updatePhoto:function(){
					window.open("/_engine/AGAjaxConnector.php?cmd=Common.prepareChangePhoto","","width=500,height=150");
					
				},
				changePhotoFinish:function(){
					
					var imgUrl = document.getElementById("imageURL").value;
					opener.document.getElementById("profilePhoto").src = "/_userfiles/" + imgUrl;
					window.close();
					
					
				},
				registerAccountData:function(){
						var fullname = Ext.get('fullname').dom.value;
						var dob = Ext.get('dob').dom.value;
						var gender = Ext.get('gender').dom.value;
						
						var password = Ext.get('password').dom.value;
						var password2 = Ext.get('password2').dom.value;
						var country = Ext.get('country').dom.value;
						var city = Ext.get('city').dom.value;
						var province = Ext.get('province').dom.value;
						var address = Ext.get('address').dom.value;
						var phone = Ext.get('phone').dom.value;
						var mobilephone = Ext.get('mobilephone').dom.value;
						var email = Ext.get('email').dom.value;
						
						var err = false;
						var errElements = '';
						
						if (city==-1) city=1000;
						
						if (email == '') {
							err = true;
							errElements = errElements + 'email,';
						}
						
						if (gender == '') {
							err = true;
							errElements = errElements + 'gender,';
						}
						if (password != password2) {
							err = true;
							errElements = errElements + 'password,';
						}
						
						if (err) {
							alert('field * harus diisi');
							resetProgress("Invalid entries", "btn_submit", "notifications", 1);
						}
						else {
						
							startProgress("Please wait while sending your data...", "btn_submit", "notifications");
							var conn = new Ext.data.Connection();
							conn.request({
								url: '/_engine/AGAjaxConnector.php',
								method: 'POST',
								params: {
									cmd: 'myAccountPage.registerAccountData',
									fullname: fullname,
		
									password: password,
									dob: dob,
									gender: gender,
									country: country,
									city: city,
									province: province,
									address: address,
									phone: phone,
									mobilephone: mobilephone,
									email: email
								},
								success: function(responseObject){
									if (responseObject.responseText == '') {
										alert('Registration request completed.\nThank you.')
										document.location = "/index.php";
									}
									else {
										Ext.Msg.alert('Error', responseObject.responseText);
										resetProgress("Failed to send your data. Please try again later" + responseObject.responseText, "btn_submit", "notifications", 1);
									}
								},
								failure: function(){
									Ext.Msg.alert('Error', 'Connection failure. Please try again later');
								}
							});
							
							
						}
					
					
				},
				
				updateAccountData:function(){
					var fullname = Ext.get('fullname').dom.value;
					var dob = Ext.get('dob').dom.value;
					var gender = Ext.get('gender').dom.value;
					var country = Ext.get('country').dom.value;
					var city = Ext.get('city').dom.value;
					var province = Ext.get('province').dom.value;
					var address = Ext.get('address').dom.value;
					var phone = Ext.get('phone').dom.value;
					var mobilephone = Ext.get('mobilephone').dom.value;
					var email = Ext.get('email').dom.value;
					
					var err = false;
					var errElements = '';
					startProgress("Please wait while updating your data...","btn_submit","notifications");
					if(fullname==''){
						err = true;
						errElements = errElements + 'fullname,';
					}
					
					if(gender==''){
						err = true;
						errElements = errElements + 'gender,';
					}
					
					if(err){
						resetProgress("Invalid entries","btn_submit","notifications",1);
					}
					
					
					var conn = new Ext.data.Connection();
						conn.request({
					    url: '/_engine/AGAjaxConnector.php',
					    method: 'POST',
					    params: {cmd: 'myAccountPage.updateAccountData',
										fullname:fullname,
										dob:dob,
										gender:gender,
										country:country,
										city:city,
										province:province,
										address:address,
										phone:phone,
										mobilephone:mobilephone,
										email:email
										},
					    success: function(responseObject) {
							 if(responseObject.responseText == ''){
							 	resetProgress("Your data has been updated" + responseObject.responseText,"btn_submit","notifications",0);
							 }else{
							 	resetProgress("Failed to save your data. Please try again later" + responseObject.responseText,"btn_submit","notifications",1);
							 }
					    },
					     failure: function() {
					         Ext.Msg.alert('Error', 'Connection failure. Please try again later');
					     }
					});
					
					
				},
				
				login:function(){
					var username = Ext.get('username').dom.value;
					var password = Ext.get('password').dom.value;
					var referer = Ext.get('referer').dom.value;
					var kl = Ext.get('keeplogged').dom.value;
					
					if(username =='' || password==''){
						 resetProgress("Please provide username and password","signin","notifications",1);
						 return false;
					}
					startProgress("Verifying your username and password. Please wait...","signin","notifications");
					
					var conn = new Ext.data.Connection();
						conn.request({
					    url: '/_engine/AGAjaxConnector.php',
					    method: 'POST',
					    params: {cmd: 'myAccountPage.login',username:username,password:password},
					    success: function(responseObject) {
							
							 if(responseObject.responseText > 0){
							 	startProgress("Please wait while take you in..","signin","notifications");
								if(referer==''){
									document.location = "/page/myaccount/profile";
								}else{
									document.location = referer;
								}
							 }else{
							 	resetProgress("Invalid username and password","signin","notifications",1);
							 }
					    },
					     failure: function() {
					         Ext.Msg.alert('Error', 'Connection failure. Please try again later');
					     }
					});
					
					
					
				},
				updateCityListByProvince:function(){
					var provID = Ext.get('loc_province').dom.value;
					
					var conn = new Ext.data.Connection();
					conn.request({
					    url: '/_engine/AGAjaxConnector.php',
					    method: 'GET',
					    params: {cmd: 'Common.ajax_getAllCityByProvince', provID:provID},
					    success: function(responseObject) {
							results = responseObject.responseText;
							var arrayResult = results.split("@SPRTOR@");
							
							var defValue = arrayResult[0].split("@FSPRTOR@");
							var strEl = "loc_city";
							var targetSel = document.getElementById(strEl);
							
							for(i=0;i < 40;i++){
									targetSel.options[0] = null;
							}
	
							elCount = 0;
							for(j=0;j < arrayResult.length-1;j++){
								if(arrayResult[j] !=''){
									var comp = arrayResult[j].split("@FSPRTOR@");
									targetSel.options[elCount] = new Option(comp[0], comp[1], false, false);
									elCount++;
								}
							}
	
							//set active selection
							for(i=0;i < arrayResult.length-1;i++){
								var comp = arrayResult[i].split("@FSPRTOR@");
								if(parseInt(comp[1]) == parseInt(intActiveSubTypeID)){
									targetSel.selectedIndex = i+1;
								}
							}
							
							
					    },
					     failure: function() {
					         Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
					     }
					});
					
					
					
				},
				
				updatePackageSelection:function(){
					var t  = Ext.DomQuery.selectValue('input[type=radio]:checked/@value');
					Ext.get('selectedPackageData').dom.value = t;
					
				},
				updateResidentSelection:function(){
					var t  = Ext.DomQuery.selectValue('input[type=radio]:checked/@value');
					Ext.get('selectedResidentTypeData').dom.value = t;
					
				},
				slide:function(elid){
					var status = document.getElementById('c_status_'+elid).value;
			
					var slideMe = Ext.get('p_'+elid);
					
					if (status == 0) {
						slideMe.slideIn('t', {
									easing: 'easeOut',
									duration: .5
								});
						document.getElementById('c_status_'+elid).value = 1;
					}
					else {
						slideMe.slideOut('t', {
							easing: 'easeOut',
							duration: .5,
							remove: false,
							useDisplay: true
						});
						document.getElementById('c_status_'+elid).value = 0;
					}
					
				},
				getElements:function(){
					var el = "selectedPackageData#loc_address01#loc_address02#" +
							 "loc_province#loc_city#loc_postcode#selectedResidentTypeData#" +
							 "subscribe_fullname#subscribe_phone#subscribe_email#subscribe_idnum";
					var elements = el.split("#");
					return elements;
				},
				
				
				nextPage:function(){
				
					//startProgress("Goto next page. Please wait...","btnNext","notification");
					var elements = subscribePage.getElements();
					var currPage = parseInt(document.getElementById('pagenum').value);
					var nextPage = currPage + 1;
					subscribePage.saveElements(elements,nextPage);
					
				},
				
				submit:function(){
				
					//startProgress("Goto next page. Please wait...","btnNext","notification");
					var elements = subscribePage.getElements();
					subscribePage.saveElements(elements,'submit');
					
				},
				
				save:function(){
					var conn = new Ext.data.Connection();
						conn.request({
					    url: '/_engine/AGAjaxConnector.php',
					    method: 'POST',
					    params: {cmd: 'Common.saveSubscribtion'},
					    success: function(responseObject) {
							document.location = "/subscribe-yestv/4";
					    },
					     failure: function() {
					         Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
					     }
					});
					
					
				},
		
				previousPage:function(){
					//startProgress("Goto previous page. Please wait...","btnBack","topStatusMessage");
					var elements = subscribePage.getElements();
					var currPage = parseInt(document.getElementById('pagenum').value);
					var prevPage = currPage - 1;
					subscribePage.saveElements(elements,prevPage);
				},
			
				saveElements:function(els,destPage){
					var elements = '';
					var values = '';
					for(i=0;i<els.length;i++){
						if(document.getElementById(els[i])){
							elements = elements + els[i] + "#spar#";
							//values = values + document.getElementById(els[i]).value + "#spar#";
							values = values + Ext.get(els[i]).dom.value + "#spar#";
						}
					}
			
					
					var conn = new Ext.data.Connection();
					conn.request({
					    url: '/_engine/AGAjaxConnector.php',
					    method: 'POST',
					    params: {cmd: 'Common.saveElements', elements: elements,values:values},
					    success: function(responseObject) {
							if(destPage !='submit'){
								document.location = "/subscribe-yestv/"+destPage;
							}else{
								subscribePage.save();
							}
					    },
					     failure: function() {
					         Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
					     }
					});
					
					
			
				}
				
				
			}
			
		