function _AddAddress(addresstype, webarea_id, editaddress) {
	
	if(validateDeliveryAddress(addresstype,'Add'))
	{
		var desc = document.getElementById(addresstype+'AdddescriptionINP');
		((desc==null)?descval='':descval=desc.value);
		
		var contact = document.getElementById(addresstype+'AddcontactINP');
		((contact==null)?contactval='':contactval=contact.value);

		var phone = document.getElementById(addresstype+'AddphoneINP');
		((phone==null)?phoneval='':phoneval=phone.value);

		var fax=document.getElementById(addresstype+'AddfaxINP');
		((fax==null)?faxval='':faxval=fax.value);
		
		var postcode=document.getElementById(addresstype+'AddpostcodeINP');
		((postcode==null)?postcodeval='':postcodeval=postcode.value);
		
		var street=document.getElementById(addresstype+'AddstreetINP');
		((street==null)?streetval='':streetval=street.value);
		
		var suburb= document.getElementById(addresstype+'AddsuburbINP');
		((suburb==null)?suburbval='':suburbval=suburb.value);

		var state= document.getElementById(addresstype+'AddstateINP');
		((state==null)?stateval='':stateval=state.value);
		
		var country= document.getElementById(addresstype+'AddcountrySEL');
		((country==null)?countryval='':countryval=country.value);

		var url = surl+'command=util_account';
		var postdata = 'action=add_delivery_address';
		postdata += '&dbAddDescription=' + descval;
		postdata += '&dbAddStreet=' + streetval;
		postdata += '&dbAddSuburb=' + suburbval;
		postdata += '&dbAddState=' + stateval;
		postdata += '&dbAddPostcode=' + postcodeval;
		postdata += '&dbAddCountry=' + countryval;
		postdata += '&dbAddContact=' + contactval;
		postdata += '&dbAddPhone=' + phoneval;
		postdata += '&dbAddFax=' + faxval;
		postdata += '&rid=' + Math.random();	

		var callback = {success:function(o){
							var result= _GetXmlDocObject(o.responseText);
							if(result.getElementsByTagName('response')[0].getAttribute('success')=="true"){

								var lst = document.getElementById('dbdelvrAddressList');
								var lst2 = document.getElementById('dbbillingAddressList');
								
								var lstsel = document.getElementById('db'+addresstype+'AddressList');
								
								if(lst2!=null){
									lst2.options[lst2.options.length]=new Option(descval);
									lst2.options[lst2.options.length-1].value='{"address_id":"'+result.getElementsByTagName('response')[0].getAttribute('addressid')+'","description":"'+descval+'","street":"'+streetval+'","suburb":"'+suburbval+'","state":"'+stateval+'","postcode":"'+postcodeval+'","country":"'+countryval+'","phone":"'+phoneval+'","fax":"'+faxval+'","contact":"'+contactval+'","mirrored":"N"}';
								}
								if(lst!=null){
									lst.options[lst.options.length]=new Option(descval);
									lst.options[lst.options.length-1].value='{"address_id":"'+result.getElementsByTagName('response')[0].getAttribute('addressid')+'","description":"'+descval+'","street":"'+streetval+'","suburb":"'+suburbval+'","state":"'+stateval+'","postcode":"'+postcodeval+'","country":"'+countryval+'","phone":"'+phoneval+'","fax":"'+faxval+'","contact":"'+contactval+'","mirrored":"N"}';
								}
								lstsel.selectedIndex=(lstsel.options.length-1);
								
								var div= document.getElementById(addresstype+'new_div');
								if(div!=null)
									div.style.display='none';
								
								_LoadAddress(editaddress,'delvr');
								_LoadAddress(editaddress,'billing');
								
								if(desc!=null)desc.value='';
								if(contact!=null)contact.value='';
								if(phone!=null)phone.value='';
								if(fax!=null)fax.value='';
								if(postcode!=null)postcode.value='';
								if(street!=null)street.value='';
								if(suburb!=null)suburb.value='';
								if(state!=null)state.value='';
							}else{
								alert(result.getElementsByTagName('response')[0].getAttribute('message'));
							}
							removeWindow(webarea_id);
						},
						failure:function(o){
							alert('SERVER NOT RESPONDING, PLEASE TRY AGAIN LATER');
							removeWindow(webarea_id);
						}};
			
		drawWindow(webarea_id,loadicon+' Saving Address...');
		YAHOO.util.Connect.asyncRequest("POST", url, callback, postdata);
	}
}

function _LoadAddress(editaddress, addresstype){
	
	var lst = document.getElementById('db'+addresstype+'AddressList');
	//var orderDetails = document.getElementById('dbEditAddressLbl');
	var orderDetails=null;

	if(lst !=null && lst.selectedIndex >0)
	{
		var json = eval("("+lst[lst.selectedIndex].value+")");
			
		if(editaddress){
			var sr = document.getElementById('streetINP');
			var sb = document.getElementById('suburbINP');
			var st = document.getElementById('stateINP');
			var pc = document.getElementById('postcodeINP');
			var ph = document.getElementById('phoneINP');
			var fx = document.getElementById('faxINP');
			var cn = document.getElementById('contactINP');
			var ds = document.getElementById('descriptionINP');
			var ctry=document.getElementById('delvrcountrySEL');

			if(sr!=null)
				sr.value = json.street;
			if(sb!=null)
				sb.value = json.suburb;
			if(st!=null)
				st.value = json.state;
			if(pc!=null)
				pc.value = json.postcode;
			if(ph!=null)
				ph.value = json.phone;
			if(fx!=null)
				fx.value = json.fax;
			if(cn!=null)
				cn.value = json.contact;
			if(ds!=null)
				ds.value = json.description;
			if(ctry!=null){
				for(var i=0; i < ctry.length;i++){
					if(ctry.options[i].value==json.country){
						ctry.selectedIndex=i;
						break;
					}
				}
			}
		}else{
			var sr = document.getElementById(addresstype+'streetINP');
			var sb = document.getElementById(addresstype+'suburbINP');
			var st = document.getElementById(addresstype+'stateINP');
			var pc = document.getElementById(addresstype+'postcodeINP');
			var ph = document.getElementById(addresstype+'phoneINP');
			var fx = document.getElementById(addresstype+'faxINP');
			var cn = document.getElementById(addresstype+'contactINP');
			var cy = document.getElementById(addresstype+'countryINP');

			if(sr!=null)
				sr.innerHTML = json.street;
			if(sb!=null)
				sb.innerHTML = json.suburb;
			if(st!=null)
				st.innerHTML = json.state;
			if(pc!=null)
				pc.innerHTML = json.postcode;
			if(ph!=null)
				ph.innerHTML = json.phone;
			if(fx!=null)
				fx.innerHTML = json.fax;
			if(cn!=null)
				cn.innerHTML = json.contact;
			if(cy!=null)
				cy.innerHTML = json.country;
			//toggleSaveDeliverAddress('none');
			//toggleMirrorDeliverAddress('block');
	
			if(orderDetails != null)
			{
				if(json.mirrored=='N')
					orderDetails.style.display = 'inline';
				else
					orderDetails.style.display = 'none';
			}
		}
		//document.getElementById('saveAddressTbl').style.display='block';
	}
}

function toggleSaveDeliverAddress(display){
	
	var fm = document.forms['addressform'];
	for(var i=0; i<fm.length; i++)
	{
		if(fm.elements[i].id.match('dbSave')!=null){
			fm.elements[i].style.display=display;
		}
	}
	document.getElementById('dbSaveButton').style.display=display;
}

function toggleNewAddress(addresstype){
	
	var d = document.getElementById(addresstype+'new_div');
	if(d!=null){
		if(d.style.display=='block'){
			d.style.display='none';
		}else{
			d.style.display='block';
		}
	}
}

function _SaveDeliveryAddress(addresstype, webarea_id){
	if(validateDeliveryAddress('',''))
	{
		var lst = document.getElementById('db'+addresstype+'AddressList');
		
		if(lst.selectedIndex >0)
		{
			var desc = document.getElementById('descriptionINP');
			((desc==null)?descval='':descval=desc.value);
			
			var contact = document.getElementById('contactINP');
			((contact==null)?contactval='':contactval=contact.value);

			var phone = document.getElementById('phoneINP');
			((phone==null)?phoneval='':phoneval=phone.value);

			var fax=document.getElementById('faxINP');
			((fax==null)?faxval='':faxval=fax.value);
			
			var postcode=document.getElementById('postcodeINP');
			((postcode==null)?postcodeval='':postcodeval=postcode.value);
			
			var street=document.getElementById('streetINP');
			((street==null)?streetval='':streetval=street.value);
			
			var suburb= document.getElementById('suburbINP');
			((suburb==null)?suburbval='':suburbval=suburb.value);

			var state= document.getElementById('stateINP');
			((state==null)?stateval='':stateval=state.value);
			
			var country= document.getElementById('delvrcountrySEL');
			((country==null)?countryval='':countryval=country.value);
			
			
			var json = eval("("+lst[lst.selectedIndex].value+")");
			var fm = document.forms['addressform'];
			var url = surl+'command=util_account';
			var postdata = 'action=save_delivery_address';
			
			postdata += '&dbSaveAddressID=' + json.address_id;
			postdata += '&dbSaveDescription=' + descval;
			postdata += '&dbSaveContact=' + contactval;
			postdata += '&dbSaveStreet=' + streetval;
			postdata += '&dbSaveSuburb=' + suburbval;
			postdata += '&dbSaveState=' + stateval;
			postdata += '&dbSavePostcode=' + postcodeval;
			postdata += '&dbSaveCountry=' + countryval;
			postdata += '&dbSavePhone=' + phoneval;
			postdata += '&dbSaveFax=' + faxval;
			postdata += '&rid=' + Math.random();
			
			var callback = {success:function(o){
							var result= _GetXmlDocObject(o.responseText);
							if(result.getElementsByTagName('response')[0].getAttribute('success')=="true"){
								lst.options[lst.selectedIndex].text=  descval;
								lst.options[lst.selectedIndex].value='{"address_id":"'+json.address_id+'","description":"'+descval+'","street":"'+streetval+'","suburb":"'+suburbval+'","state":"'+stateval+'","postcode":"'+postcodeval+'","country":"'+countryval+'","phone":"'+phoneval+'","fax":"'+faxval+'","contact":"'+contactval+'","mirrored":"N"}';
								_LoadAddress(true, addresstype)
							}else{
								alert(result.getElementsByTagName('response')[0].getAttribute('message'));
							}
							removeWindow(webarea_id);
						},
						failure:function(o){
							alert('SERVER NOT RESPONDING, PLEASE TRY AGAIN LATER');
							removeWindow(webarea_id);
						}};
			
			drawWindow(webarea_id,loadicon+' Saving Address...');
			YAHOO.util.Connect.asyncRequest("POST", url, callback, postdata);
		}else{
			alert('Please select an address from the list.');
		}
	}
}

function _DeleteDeliveryAddress(addresstype,webarea_id) {
	var lst = document.getElementById('dbdelvrAddressList');
	
	if(lst.selectedIndex >0)
	{
		var json = eval("("+lst[lst.selectedIndex].value+")");
		
		if (confirm('You are about to delete '+json.description+' address. Are you sure?') == false) {
			return false;
		}

		var url = surl+'command=util_account';
		var postdata = 'action=delete_delivery_address';
		postdata += '&dbDelAddressID=' + json.address_id;
		postdata += '&rid=' + Math.random();
		
		var callback = {success:function(o){
							var result= _GetXmlDocObject(o.responseText);
							if(result.getElementsByTagName('response')[0].getAttribute('success')=="true"){
								
								var rec= lst.selectedIndex;
								lst.selectedIndex=(rec-1);
								lst.options[rec]= null;
								_LoadAddress(true, addresstype);
							}else{
								alert('SERVER IS UNABLE TO REMOVE ADDRESS, PLEASE TRY AGAIN LATER');
							}
							removeWindow(webarea_id);
						},
						failure:function(o){
							alert('SERVER NOT RESPONDING, PLEASE TRY AGAIN LATER');
							removeWindow(webarea_id);
						}};
		
		drawWindow(webarea_id,loadicon+' Deleting Address...');
		YAHOO.util.Connect.asyncRequest("POST", url, callback, postdata);
	}else{
		alert('Please select an address from the list.');
	}
}

function validateDeliveryAddress(addressType, savetype){
	
	var desc = document.getElementById(addressType+savetype+'descriptionINP');
	var contact = document.getElementById(addressType+savetype+'contactINP');
	var phone = document.getElementById(addressType+savetype+'phoneINP');
	var fax=document.getElementById(addressType+savetype+'faxINP');
	var postcode=document.getElementById(addressType+savetype+'postcodeINP');
	var street=document.getElementById(addressType+savetype+'streetINP');
	var suburb= document.getElementById(addressType+savetype+'suburbINP');
	var state= document.getElementById(addressType+savetype+'stateINP');
	var country= document.getElementById(addressType+savetype+'countrySEL');
	
	if (desc!=null){
		if ((desc.value).match("{|}") != null || desc.value == '') {
			alert("You must give your 'address' a name");
			return false;
		}else if(!_isvalid("alphanumeric",desc.value)){
			alert("Please enter a valid 'address name'. Make sure the field contains only alphabet and numeric characters.");
			return false;
		}
	}
	
	if (street!=null){
		if ((street.value).match("{|}") != null || street.value == '') {
			alert("You must give a 'street' for this address");
			return false;
		}else if(!_isvalid("alphanumeric",street.value.replace('-','').replace('/',''))){
			alert("Please enter a valid 'street'. Make sure the field contains only alphabet and numeric characters.");
			return false;
		}
	}
	
	if (suburb!=null){
		if ((suburb.value).match("{|}") != null || suburb.value == '') {
			alert("You must give a 'suburb/town' for this address");
			return false;
		}else if(!_isvalid("alphanumeric",suburb.value.replace('-','').replace('/',''))){
			alert("Please enter a valid 'suburb/town'. Make sure the field contains only alphabet and numeric characters.");
			return false;
		}
	}
	
	if (state!=null){
		if ((state.value).match("{|}") != null || state.value == '') {
			alert("You must give a 'state/province' for this address");
			return false;
		}else if(!_isvalid("alphanumeric",state.value)){
			alert("Please enter a valid 'state/provincial'. Make sure the field contains only alphabet and numeric characters.");
			return false;
		}
	}

	if (postcode!=null){
		if ((postcode.value).match("{|}") != null || postcode.value == '') {
			alert("You must give a 'postcode' for this address");
			return false;
		}else if(!_isvalid("numeric",postcode.value)){
			alert("Please enter a valid numeric 'postcode/zipcode'.");
			return false;
		}
	}
	
	if (country!=null){
		if ((country.value).match("{|}") != null || country.value == '') {
			alert("Please Select a 'country'");
			return false;
		}
	}
	
	if (contact!=null){
		if ((contact.value).match("{|}") != null || contact.value==""){
			contact.value = '';
		}else if(!_isvalid("alphanumeric",contact.value)){
			alert("Please enter a valid 'contact'. Make sure the field contains only alphabet and numeric characters.");
			return false;
		}
	}
	
	if (phone!=null){
		if ((phone.value).match("{|}") != null || phone.value=="") {
			phone.value = '';
		}else if(!_isvalid("numeric",phone.value)){
			alert("Please enter a valid numeric 'phone number' with no spaces eg. 0311223344");
			return false;
		}
	}
	
	if(fax!=null){
		if ((fax.value).match("{|}") != null || fax.value=="") {
			fax.value = '';
		}else if(!_isvalid("numeric",fax.value)){
			alert("Please enter a valid numeric 'fax number' eg. 0311223344");
			return false;
		}
	}
	return true;
}