
function Addressbook(el){
	var msg, errorMsg, selectedAddressId;
	this.el = el;
	this.bodyEl = getEl('addressbook-bd');
	
	this.fields = [{"name":"street", "type":"input"},
					{"name":"postalcode", "type":"input"},
					{"name":"city", "type":"input"},
					{"name":"countries_id", "type":"select"}]

	this.requiredMsg = '<b>Vet</b> gedrukte velden zijn verplicht.';
	this.errorMsg = '<b>Rood</b> gedrukte velden zijn onjuist ingevuld.';	

	this.inAccount = (getEl('account') !== null);

	this.init = function(){
	 	if (this.getCount() > 0) {
			this.loadDlg();
			this.addEventListeners();			
		} else {
		 
			msg = this.bodyEl.createChild({cls:'addressbook-msg', tag:'span'});
			msg.setVisibilityMode(YAHOO.ext.Element.DISPLAY);
			msg.dom.innerHTML = this.requiredMsg;

			errorMsg = this.bodyEl.createChild({cls:'addressbook-msg error-msg', tag:'span'});
			errorMsg.setVisibilityMode(YAHOO.ext.Element.DISPLAY);				
			errorMsg.setVisible(false);
			errorMsg.dom.innerHTML = this.errorMsg;
		 
		 	var nextBtn = getEl('next-btn');
		 	if (nextBtn) {
				nextBtn.mon('click', function(e){
					if (!this.saveAddress(getEl('address-form').dom, 0, this.load.createDelegate(this))){
						e.preventDefault();
					};
				}, this);
			}
			getEl('address-form-fields').setStyle('display','block');
			
		}
	}
	
	this.getCount = function(){
	 	return this.el.getChildrenByClassName('address','li').length;	
	}

	this.loadDlg = function(){
			var adlg = new YAHOO.ext.BasicDialog("ab-dlg", { 
			        modal:true,
			        autoCreate:{
						id: 'ab-dlg',
						tag:'form',
						method: 'post',
						onSubmit: 'return false;'	
					},
			        width:425,
			        title: 'Nieuw adres',
			        height:280,
			        shadow:true,
			        minWidth:150,
			        minHeight:100
			});
			adlg.addKeyListener(27, adlg.hide, adlg);
			adlg.addButton('Opslaan', function(){
				this.saveAddress(getEl('ab-dlg').dom, this.adlg.addressId, this.update.createDelegate(this));				
			}, this);
			adlg.addButton('Sluiten', adlg.hide, adlg);
			
			adlg.body.appendChild(getEl('address-form-fields'));
			getEl('address-form-fields').setStyle('display', 'block');

			msg = adlg.footer.createChild({cls:'dlg-msg', tag:'span'});
			msg.setVisibilityMode(YAHOO.ext.Element.DISPLAY);
			msg.dom.innerHTML = this.requiredMsg;

			errorMsg = adlg.footer.createChild({cls:'dlg-msg error-msg', tag:'span'});
			errorMsg.setVisibilityMode(YAHOO.ext.Element.DISPLAY);				
			errorMsg.setVisible(false);
			errorMsg.dom.innerHTML = this.errorMsg;
			this.adlg = adlg;
	}
	
	this.update = function(){
	 	getEl('addressbook-bd').getUpdateManager().update('index2.php', 'action=renderAddresses', function(o,b){
			this.addEventListeners();								
		}.createDelegate(this));
		this.adlg.hide();
	}
	
	this.load = function(){
	 	if (getEl('selectedAddressId')) getEl('selectedAddressId').dom.value = selectedAddressId;
		getEl('address-form').dom.submit();
	}
	
	this.addEventListeners = function(){
	   	var newAddressBtn = getEl('address-add-btn');
		newAddressBtn.mon('click', this.addAddress, this);

		var nextBtn = getEl('next-btn');
		if (nextBtn){
			nextBtn.mon('click', function(e){
			 	if (selectedAddressId) getEl('selectedAddressId').dom.value = selectedAddressId;
			}, this);
		}
		
		var addresses = this.el.getChildrenByClassName('address', 'li');
		for (var i=0;i<addresses.length;i++){
		 
		 	var id = addresses[i].id.substring(8);		 
		 	var bd = getEl('address-bd-' + id);
		 	bd.mon('click', this.selectAddress.createDelegate(this, [id], 0), this);

			if (!this.inAccount){
			 	bd.mon('mouseover', function(id, e){
					getEl('address-btns-' + id).setStyle('visibility', 'visible');
				}.createDelegate(this, [id], 0),this);
			 	bd.mon('mouseout', function(id, e){
					getEl('address-btns-' + id).setStyle('visibility', 'hidden');
				}.createDelegate(this, [id], 0),this);			
			}

		 	getEl('address-radio-' + id).mon('click', this.selectAddress.createDelegate(this,[id],0),this);

			getEl('address-edit-btn-' + id).mon('click', function(id, e){
				YAHOO.util.Connect.asyncRequest('post', 'index2.php?action=editAddress&id=' + id, {success: function(o,b){
					this.editAddress(id, eval("(" + o.responseText + ")"));
				}.createDelegate(this)});
			}.createDelegate(this, [id], 0),this);			
		 	
			getEl('address-del-btn-' + id).mon('click', function(id, e){
			 	YAHOO.ext.MessageBox.confirm('Adres verwijderen', 'Weet u zeker dat u dit adres wilt verwijderen?', function(btn){
					if (btn == 'yes'){
						YAHOO.util.Connect.asyncRequest('post', 'index2.php?action=deleteAddress&id=' + id, {success: function(o,b){
						 	if (o.responseText.lastIndexOf('false') == -1){
							 	selectedAddressId = o.responseText;						 
							 	getEl('addressbook-bd').getUpdateManager().update('index2.php', 'action=renderAddresses', function(o,b){
									this.addEventListeners();								
								}.createDelegate(this));
							} else {
								YAHOO.ext.MessageBox.alert('Adres verwijderen','Dit adres kan niet worden verwijderd.');
							}
						}.createDelegate(this)})
					}
				}, this);
			}.createDelegate(this, [id],0),this);					 	
		}			
	}

	this.resetForm = function(){
	 	for (var u=0;u<this.fields.length;u++){
			if (this.fields[u].type == 'input') getEl(this.fields[u].name).dom.value = '';
			else getEl(this.fields[u].name).dom.selectedIndex = 0;
			getEl('field-' + this.fields[u].name).removeClass('has-error');
		}
	}

	this.selectAddress = function(id, e){
	 	var adr = this.el.getChildrenByClassName('address','li');
	 	for (var a=0;a<adr.length;a++){
			adr[a].removeClass('selected');	
		}
		getEl('address-radio-' + id).dom.checked = true;
	 	getEl('address-' + id).addClass('selected');
	 	selectedAddressId = id;
	}
	
	this.isValidAddress = function(){
	 	var valid = true;
		for (var i=0;i<this.fields.length;i++){
			if ((this.fields[i].type == 'input' && getEl(this.fields[i].name).dom.value == '') || (this.fields[i].type == 'select' && getEl(this.fields[i].name).dom.selectedIndex == 0)) {
				getEl('field-' + this.fields[i].name).addClass('has-error');
				valid = valid && false;					
			} else {
				getEl('field-' + this.fields[i].name).removeClass('has-error');				
			}
		}
		return valid;
	}
	
	this.saveAddress = function(form, id, callback){
	 	if (this.isValidAddress()){
		 	YAHOO.util.Connect.setForm(form);
		 	YAHOO.util.Connect.asyncRequest('POST', 'index2.php?action=saveAddress&id=' + id, {success: function(o,b){
				msg.setVisible(true);
				errorMsg.setVisible(false);
				selectedAddressId = o.responseText;						 
				callback();
			}.createDelegate(this)});
		} else {
			msg.setVisible(false);
			errorMsg.setVisible(true);			
			return false;
		}
	}		

	this.addAddress = function(){
	 	this.resetForm();
	 	this.adlg.setTitle('Nieuw adres');
	 	this.adlg.addressId = 0;
	 	msg.setVisible(true);
	 	errorMsg.setVisible(false);						
 		this.adlg.show();		
	}

	this.editAddress = function(id, fields){
	 	this.resetForm();

		for (var a=0;a<fields.length;a++){
			var el = getEl(fields[a].name);					 
		 	if (fields[a].type == 'input') el.dom.value = fields[a].value;
		 	else {
		 		for (var t=0;t < el.dom.options.length;t++){
					if (el.dom.options[t].value == fields[a].value) break;
				}
				el.dom.selectedIndex = t;							
			}
		}

		this.adlg.addressId = id;
	 	this.adlg.setTitle('Adres wijzigen');
	 	msg.setVisible(true);
	 	errorMsg.setVisible(false);						 					
		this.adlg.show();
	}

	this.init();
	
}	


    