/*--------------------------------
 *  	   OBJETO WAPPUSH
 *-------------------------------*/
var wappush = {
	version: '1.2.0',
	
	/*------------------------*/
	/* VALIDACIONES DEL INPUT */
	dataInicialCampo: '',
	dataInicialCampoFlag: false,
	
	focus: function(el) {
		if (!this.dataInicialCampoFlag) {
			this.dataInicialCampo = el.value;
			this.dataInicialCampoFlag = true;
			el.value = "";
		}
		
		if(el.value == this.dataInicialCampo) {
			el.value = "";
		}
		
		el.onblur = function(){
			if (this.value == "") {
				this.value = wappush.dataInicialCampo;
				this.dataInicialCampoFlag = false;
			}
		};
	},
	
	soloNumeros: function(evt, el){
		var key = evt.keyCode ? evt.keyCode : evt.which;
		this.ocultarMensajes();
		
		if(key == 13) {
			this.validaNumero(el);
			return false;
		}
		return (key <= 31 || (key >= 48 && key <= 57));
	},
	
	/*-------------*/
	/* DESPLIEGUES */
	mostrarPrincipal: function(el) {
		this.ocultarMensajes();
		var padre = this.fn.findParentNode('wp_main', el);
		padre.style.display = 'block';
	},
	
	mostrarError: function(error) {
		this.ocultarMensajes();
		var msg = this.fn.getElementsByClassName('wp_alerta', this.tempForm);
		for(var i in msg) {
			msg[i].innerHTML = error;
			msg[i].style.display = 'block';
		}
	},
	
	mostrarExito: function(message) {
		this.ocultarMensajes();
		var msg = this.fn.getElementsByClassName('wp_exito', this.tempForm);
		for(var i in msg) {
			msg[i].innerHTML = message;
			msg[i].style.display = 'block';
		}
	},
	
	ocultarMensajes: function() {
		//Ocultamos div de error
		var divError = this.fn.getElementsByClassName('wp_alerta', this.tempForm);
		for(var i in divError) {
			divError[i].style.display = 'none';
		}
		
		//Mostramos div de exito
		var divExito = wappush.fn.getElementsByClassName('wp_exito', this.tempForm);
		for (var i in divExito) {
			divExito[i].style.display = 'none';
		}
	},
	
	volver: function(el) {
		this.mostrarPrincipal(el);
		return false;
	},
	
	tempForm: '',
	validaNumero: function(el) {
		if (this.fn.findParentNode('validarFomularioForm', el)) {
			this.tempForm = this.fn.findParentNode('validarFomularioForm', el);
			
			var cadena = this.tempForm.msisdn.value;
			
			if (cadena == "" || cadena.length < 8) {
				this.mostrarError('Te equivocaste al ingresar el n&uacute;mero, reint&eacute;ntalo.');
				
			}
			else {
				this.writeSWFObject();
				return false;
			}
		}
		
		return false;
	},
	
	/*---------------------*/
	/* INYECCION DEL PROXY */
	writeSWFObject: function() {
		if(document.getElementById('proxy')) {
			var proxy = document.getElementById('proxy');
			proxy.parentNode.removeChild(proxy);
		}
		
		var div = document.createElement('div');
		div.id = 'proxyContent';
		document.body.appendChild(div);
		
		var flashvars = {
			serviceUrl: this.tempForm.action,
			msisdn: this.tempForm.msisdn.value,
			idCampana: this.tempForm.idCampana.value
		};
		var params = { 
			allowScriptAccess: "always" 
		};
		var attributes = {
			id: "proxy",
			allowScriptAccess: "always"
		};	
		swfobject.embedSWF("http://wappush.entelpcs.cl/proxy.swf", "proxyContent", "1", "1", "8.0.0","http://www.entelpcs.cl/services/wap_push/expressInstall.swf", flashvars, params, attributes);
	},
	
	getFlashMovie: function(movieName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	},
	
	/*---------*/
	/* HELPERS */
	fn: {
		findParentNode: function(parentName, childObj) {
			var testObj = childObj.parentNode;
			while (testObj.getAttribute('name') != parentName) {
				testObj = testObj.parentNode;
			}
			return testObj;
		},
		
		getElementsByClassName: function(className, el) {
			if(!el) el = document.body;
			var results = [];
			this.walkTheDOM(el, function(node) {
				var a, c = node.className, i;
				if(c) {
					a = c.split(' ');
					for(i=0; i<a.length; i++) {
						if(a[i] === className) {
							results.push(node);
							break;
						}
					}
				}
			});
			return results;	
		},
		
		walkTheDOM: function(node, func) {
			func(node);
			node = node.firstChild;
			while(node) {
				this.walkTheDOM(node, func);
				node = node.nextSibling;
			}
		}
	}
};

/*--------------------------------
 *  	Respuestas del Proxy
 *-------------------------------*/
function handleSuccesMessage(message) {	
	wappush.mostrarExito(message);
}

function handleErrorMessage(message) {
	wappush.mostrarError(message);
}

function sendFlashVariable() {
	var text = wappush.tempForm.msisdn.value;
   	wappush.getFlashMovie("proxy").doHTTPRequest(text);
}