var PV = null;

/**
 * @param  pc Combo de paises
 * @param  rc Ccombo de regiones
 * @param  lc Ccombo de localidades
 */
var SelRegiones = new Class({
	
	_pc : null,
	_rc : null,
	_lc : null,
	_lang: null,
	_value : null,
	initialize : function(pc, rc, lang) {

		//alert(rc);
		this._pc = pc;
		//this._rc = rc;		
		this._lang = lang;
		this._value = rc.value;
		rc.parentNode.removeChild(rc);
		var selector = new Element('select').setProperties({
			'name' : "provincia",
			'class' : "campoNormal",
		 	'id' : "provincia"
				}).injectInside($('provcontainer'));
		selector.setAttribute('name', 'USUARIO[provincia]');
		//selector.injectBefore(rc);
		//rc.replaceWith(selector);
		
		//rc.remove();		
		this._rc=selector;		
	},
	
	createNode : function(id, name) {		
		var node = new Element('option').setProperties({
			'value' : id
		}).setHTML(name);
		if(this._value == id){
			node.setProperty('selected','selected');
		}		
		return node;
	},
	
	addNode : function(combo, value, text) {
		var opt = this.createNode(value, text);
		combo.appendChild(opt);
		//alert(combo.type);
	},
	
	cleanCombo : function(combo) {
		for(var i = (combo.childNodes.length-1); i >= 0 ; i--) {
			combo.removeChild(combo.childNodes[i]);
		}
	},
	
	fillCombo : function(xml, combo) {	
		var xml = xml.documentElement;
		var elemento = xml.firstChild;
		
		//this.addNode(combo, 0, 'Seleccione una opciÃ³n');
		
		while(elemento) {
			//alert(elemento.firstChild.nodeValue);
			var nombre = elemento.firstChild.nodeValue;
			var id = nombre;
			this.addNode(combo, id, nombre);			
			elemento = elemento.nextSibling;
		}
	},
	setRegiones : function(texto, xml) {
		
		PV.cleanCombo(PV._rc);
		PV.fillCombo(xml, PV._rc);
	},
	
	getRegiones : function(lang) {
		var pais = this._pc.options[this._pc.selectedIndex].value;
		
		var url = "/src/getRegiones.php?pais=" + pais + "&lang=" + lang;

		Ajaxhttp = new Ajax(url, {method: 'get', onComplete: this.setRegiones}).request();
	}

});
var global_lang = "";
//Window.onDomReady(function(){
function initSelRegiones(lang) {
	global_lang = lang;
	//alert($ES('.selectorPais'));
	//$ES('.selectorPais').setStyle('display', 'none');
	if($('pais').options[$('pais').selectedIndex].value=="CA" || $('pais').options[$('pais').selectedIndex].value=="US"){
			PV = new SelRegiones($('pais'), $('provincia'), lang);
			PV.getRegiones(lang);
	}
	$('pais').addEvent('change',function(){
		if($('pais').options[$('pais').selectedIndex].value=="CA" || $('pais').options[$('pais').selectedIndex].value=="US"){
			PV = new SelRegiones($('pais'), $('provincia'), global_lang);
			PV.getRegiones(global_lang);

		}
		else {
			var provSelect = $('provincia');			
			if(provSelect.type!='text'){
				//alert (inputText.type);
				//provSelect.remove();
				for(var i = (provSelect.childNodes.length-1); i >= 0 ; i--) {
					provSelect.removeChild(provSelect.childNodes[i]);
				}
				provSelect.parentNode.removeChild(provSelect);
				var inputText = new Element('input').setProperties({
					'class' : "campoNormal",
		 			'id' : "provincia", 
					'name' : "provincia",
					'value' : "", 
					'maxlength' :"35", 
					'type' : "text"
				}).injectInside($('provcontainer'));
				inputText.setAttribute('name', 'USUARIO[provincia]');
				//alert($('provincia').type;
/*				var provincia = $('provincia');
				var inputText = new Element('input').setProperties({
					'class' : "campoNormal",		 			
					'name' : "USUARIO[provincia]",
					'value' : "", 
					'maxlength' :"35", 
					'type' : "text"
				});
				inputText.injectInside(provincia.parentNode);
				for(var i = (provincia.childNodes.length-1); i >= 0 ; i--) {
					provincia.removeChild(provincia.childNodes[i]);
				}
				provincia.parentNode.removeChild(provincia);
				inputText.setProperties({
					'class' : "campoNormal",
		 			'id' : "provincia", 
					'name' : "USUARIO[provincia]",
					'value' : "", 
					'maxlength' :"35", 
					'type' : "text"
				});*/
			}	
		}
	},false);	
}

