/**
 * Clase que devuelve las provincias asociadas a un paíss
 *
 * @author Fernando Magán
 * @date 2009-03
 */

var PV = null;

var Provincias = new Class({
    Implements: [Options],
    
	_pc : null,
	_rc : null,
    path: null,
    
    initialize:function(pc, rc, path){
	
		this._pc = pc;
		this._rc = rc;
		this.path = path;
		
		if(pc) {
			pc.addEvent('change', function(){PV.requestProvincias(this.options[this.selectedIndex].value);}, false);
		}
	
	},
    
    /**
     * Crea la petición de las provincias del país actual
     *
     * @param identificador string de valor del pais
     */
    requestProvincias: function(identificador){
        new Request.XML({
            'url': this.path + '/es/varios/provincias',
        	//'url': this.path + '/es/varios/provincias',
            'method': 'post'
                }).addEvent('success', this.handleRequestRating.bindWithEvent(this)).send('pais=' + identificador);
    },
    
    /**
     * Recoge los valores de la respuesta Ajax y llama a render
     *
     * @param {Object} text
     * @param {Object} xml
     */
    handleRequestRating: function(text, xml){
    	
    	this._rc.getChildren().each(function(el) {
			el.dispose();
		});
                       
        var erres = xml.getElements('p');
                       
        $each(erres, function(key, value) {
        
            var objOption = new Element('option').setProperties({
							'value' : key.getElementsByTagName('value')[0].firstChild.nodeValue.toInt(),
							'selected': ''
						}).appendText(key.getElementsByTagName('text')[0].firstChild.nodeValue);


			this._rc.adopt(objOption);

        
        }, this);
    }
});