var form = {
	init: function(){
		this.validation = {
			nombres: ['name',true],
			apellidos: ['name',true],
			telefono: ['phone',true],
			direccion: ['content',true],
			direccion2: ['content',false],
			pais_id: ['select',true],
			usa_state_id: ['select',false],
			ciudad: ['content',true],
			codigoPostal: ['zipcode',true],
			sexo: ['select',true],
			fechaNacimiento: ['date',false],
			email: ['email',true],
			clave: ['password',true],
			clave2: ['password',true]
		};

		this.inputs = $('form').getElements('input,select').associate(['nombres','apellidos','telefono','direccion','direccion2','ciudad','codigoPostal','fechaNacimiento','email','clave','clave2','boletin','recordar','pais_id','usa_state_id','sexo']);
		this.buttons = $$('#buttons span').associate(['register']);

		this.dp = new datePicker(this.inputs.fechaNacimiento,dp_options);
		this.inputs.fechaNacimiento.addEvents({
			'focus': this.dp.show.bind(this.dp),
			'keyup': function(e){
				var e = new Event(e);
				if(e.key=='delete'){
					this.dp.deselect().update().hide();
				}
			}.bind(this)
		}).readOnly = true;

		this.inputs.pais_id.addEvent('change',function(){
			if(this.inputs.pais_id.value=='223'){
				this.validation.usa_state_id[1] = true;
				this.inputs.usa_state_id.getParent().getParent().removeClass('hidden');
			}else{
				this.inputs.usa_state_id.getParent().getParent().addClass('hidden');
				this.inputs.usa_state_id.selectedIndex = 0;
				this.validation.usa_state_id[1] = false;
			}
		}.bind(this)).fireEvent('change');

		this.inputs.email.addEvent('change',this.checkUser.bind(this));
		this.buttons.register.addEvent('click',this.register.bind(this));

		this.request = new Request({url:BASE.ajax+'usuario.ajax',onSuccess:function(r){this['on'+this.request.action](r);}.bind(this),onFailure:onError});
	},

	checkUser: function(){
		if(this.checkInputs(['email'])){
			Loading.show();
			this.request.action = 'CheckUser';
			this.request.send({data:'action='+this.request.action+'&email='+this.inputs.email.value});
		}
	},

	onCheckUser: function(response){
		switch(response){
			case 'true':
				alert('Ya existe una cuenta con el email: '+this.inputs.email.value);
				this.inputs.email.set('value','').focus();
				Loading.hide();
				break;
			case 'false':
				Loading.hide();
				break;
			default:
				alert(response);
		}
	},

	register: function(){
		if(this.checkForm()){
			Loading.show();
			data = {};
			['nombres','apellidos','telefono','direccion','direccion2','ciudad','codigoPostal','pais_id','usa_state_id','sexo','fechaNacimiento','email','clave','boletin'].each(function(l){data[l]=this.inputs[l].value;},this);
			['boletin','recordar'].each(function(l){if(this.inputs[l].checked){data[l]=1;}},this);
			this.request.action = 'UserRegister';
			this.request.send({data:'lng='+lng.code+'&action='+this.request.action+'&data='+encodeURIComponent(JSON.encode(data))});
		}
	},

	onUserRegister: function(response){
		switch(response){
			case 'true':
				redirect(micuenta);
				break;
			default:
				alert(response);
		}
	},

	checkForm: function(){
		if(this.checkInputs(['nombres','apellidos','telefono','direccion','direccion2','pais_id','usa_state_id','ciudad','codigoPostal','fechaNacimiento','email','clave','clave2'],true)){
			if(this.inputs.clave.value==this.inputs.clave2.value){
				return true;
			}else{
				this.showTip('clave2',_lng.clavemal);
			}
		}
		return false;
	},

	checkInputs: function(ls,tip){
		this.hideTip();
		for(var i=0;i<ls.length;i++){
			var value = this.inputs[ls[i]].value = this.inputs[ls[i]].value.trim();
			if((value=='' && this.validation[ls[i]][1]) || (value!='' && !value.test(iRules[this.validation[ls[i]][0]].regx))){
				if(tip){
					this.showTip(ls[i],(value==''?_lng.required:false));
				}
				return false;
			}
		}
		return true;
	},

	showTip: function(l,msg){
		$clear(this._hideTip);
		if(!this.tip){
			this.tip = {cont:new Element('div',{'class':'iVtip hidden'}).inject(document.body)};
			this.tip.title = new Element('h3').set('html','Error!').inject(this.tip.cont);
			this.tip.text = new Element('p').inject(this.tip.cont);
		}
		this.tipl = l;
		var pos = this.inputs[l].addClass('fail').getPosition();
		this.tip.cont.setStyles({'left':(pos.x+310)+'px','top':(pos.y-12)+'px'});
		this.tip.text.innerHTML = msg ? msg : iRules[this.validation[l][0]].msg;
		this.tip.cont.removeClass('hidden');
		this._hideTip = this.hideTip.delay(5000,this);
		window.scroll(0,pos.y-50);
		this.inputs[l].focus();
	},

	hideTip: function(){
		if(this.tip){
			this.inputs[this.tipl].removeClass('fail');
			this.tip.cont.addClass('hidden');
		}
	}
};

window.addEvent('domready',form.init.bind(form));
