String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}


load_areas = function() {

	select = document.getElementById('loc_area');

	while (select.firstChild) select.removeChild(select.firstChild); // remove all options
			
	option = document.createElement('option');
	option.value = 0;
	option.appendChild(document.createTextNode('Loading...'));
	
	select.appendChild(option);
	select.className = 'loading';

	callback = {
		success: function(o) {
			select = document.getElementById('loc_area');
			select.className = '';
			
			while (select.firstChild) select.removeChild(select.firstChild); // remove all options

			if (o.responseText == '') {
				option = document.createElement('option');
				option.value = 0;
				
				if (document.getElementById('loc_country').value == 0) {
					option.appendChild(document.createTextNode('Select first a country'));
				} else {
					option.appendChild(document.createTextNode('No areas defined yet'));
				}
				
				select.appendChild(option);
			} else {
				option = document.createElement('option');
				option.value = 0;
				
				option.appendChild(document.createTextNode('Select an area'));
								
				select.appendChild(option);
			
				a = o.responseText.split('|');
				for (i=0; i<a.length; i++) {
					b = a[i].split('~');
	
					option = document.createElement('option');
					option.value = b[0];
					option.appendChild(document.createTextNode(b[1]));
					
					select.appendChild(option);
				}
			}

		},

		failure: function(o) {alert(o.statusText+' '+o.status+' '+o.responseText)}
	}

	data = 'action=options&country='+document.getElementById('loc_country').value;

	ajax = YAHOO.util.Connect.asyncRequest('POST', '/manager-area.php', callback, data);
	if (!ajax) {
		alert('Unable to load data.\n Please check your browser\'s security settings or your firewall.');
	}
}

load_location = function() {
	location.href = '/area.php?area='+document.getElementById('loc_area').value;
}

YAHOO.util.Event.onDOMReady(function() {
	YAHOO.util.Event.addListener('loc_country', 'change', load_areas);
	YAHOO.util.Event.addListener('loc_area', 'change', load_location);
});



////////////////////////////////////////////////////////
// :hover and :active simulation for buttons
////////////////////////////////////////////////////////

if (YAHOO.env.ua.ie) {
	YAHOO.util.Event.onDOMReady(function() {
		var buttons = document.getElementsByTagName('button');
		
		attach_button_states(buttons);
		
	});
}


attach_button_states = function(buttonarray) {
	for (i=0; i<buttonarray.length; i++) {
		button = buttonarray[i];
		
		YAHOO.util.Event.addListener(button, 'mouseover', function() {
			YAHOO.util.Dom.addClass(this, 'hover');
			YAHOO.util.Dom.removeClass(this, 'active');
		});
		
		YAHOO.util.Event.addListener(button, 'mouseout', function() {
			YAHOO.util.Dom.removeClass(this, 'active');
			YAHOO.util.Dom.removeClass(this, 'hover');
		});
		
		YAHOO.util.Event.addListener(button, 'mousedown', function() {
			YAHOO.util.Dom.addClass(this, 'active');
			YAHOO.util.Dom.removeClass(this, 'hover');
		});
		
		YAHOO.util.Event.addListener(button, 'mouseup', function() {
			YAHOO.util.Dom.addClass(this, 'hover');
			YAHOO.util.Dom.removeClass(this, 'active');
		});
		
		YAHOO.util.Event.addListener(button, 'blur', function() {
			YAHOO.util.Dom.removeClass(this, 'active');
			YAHOO.util.Dom.removeClass(this, 'hover');
		});
		
		YAHOO.util.Event.addListener(button, 'focus', function() {
			YAHOO.util.Dom.addClass(this, 'active');
		});
	}
}



////////////////////////////////////////////////////////
// form validation
////////////////////////////////////////////////////////

fading_config = {fadein: {}, fadeout: {}};

fading_config.fadein.div = {
	backgroundColor: { from: 'ffffff', to: 'ffc88c' }, 
	borderColor: { from: 'ffffff', to: 'ff8604' }
};

fading_config.fadeout.div = {
	backgroundColor: { from: 'ffc88c', to: 'ffffff' }, 
	borderColor: { from: 'ff8604', to: 'ffffff' }
};

fading_config.fadein.input = {
	backgroundColor: { from: 'eeeeee', to: 'ffc88c' }, 
	borderColor: { from: '999999', to: 'ff8604' }
};

fading_config.fadeout.input = {
	backgroundColor: { from: 'ffc88c', to: 'eeeeee' }, 
	borderColor: { from: 'ff8604', to: '999999' }
};

/*	red style
fading_config.fadein.div = {
	backgroundColor: { from: 'ffffff', to: 'ffbbbb' }, 
	borderColor: { from: 'ffffff', to: 'ff0000' }
};

fading_config.fadeout.div = {
	backgroundColor: { from: 'ffbbbb', to: 'ffffff' }, 
	borderColor: { from: 'ff0000', to: 'ffffff' }
};

fading_config.fadein.input = {
	backgroundColor: { from: 'eeeeee', to: 'ffbbbb' }, 
	borderColor: { from: '999999', to: 'ff0000' }
};

fading_config.fadeout.input = {
	backgroundColor: { from: 'ffbbbb', to: 'eeeeee' }, 
	borderColor: { from: 'ff0000', to: '999999' }
};
*/
////////////////////////////////////////////////////////
// customized alerts
////////////////////////////////////////////////////////

make_alert = function(name, header, body, func) {
	var alertobj = new YAHOO.widget.Dialog(name, {
		fixedcenter:true, 
		visible:false,  
		zIndex:1000, 
		modal:true,
		width:"300px", 
		buttons : [ { text:"OK", handler:function() {this.hide(); this.destroy(); if (func) func(); }, isDefault:true } ] ,
		effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} 
	} );
	
	alertobj.setHeader(header); 
	alertobj.setBody(body); 
	
	// listener for [x], [esc] and [enter]
	var listener = new YAHOO.util.KeyListener(document, { keys: [13, 27, 88] }, {
		fn: function() {this.hide(); this.destroy(); if (func) func(); }, 
      scope: alertobj, 
      correctScope: true
	}, 'keyup'); 
	
	alertobj.cfg.queueProperty("keylisteners", listener);
	
	
	alertobj.render(document.body);
	
	if (YAHOO.env.ua.ie) {
		attach_button_states(alertobj.getButtons());
	}

	return alertobj;
}

