function makeButton() {
	$$(".answerContainer").each(function(s, index) {
		var stringLeft = "#"+s.id+" div.answerBgLeft";
		var stringRight = "#"+s.id+" div.answerBgRight";
		var label = "#"+s.id+" label";
		
		s.observe('mouseover', function(p, index2) { 
			$$(stringLeft).each(function(r, index) {
				r.style.background = "url(_img/answer_bg_left2.png)";
			});
			$$(stringRight).each(function(u, index) {
				u.style.background = "url(_img/answer_bg_right2.png)";
			});
			$$(label).each(function(w, index3) {
				w.style.background = "transparent url(_img/answer_bg2.png) repeat-x top left";
			});				
		});
		
		s.observe('mouseout', function(p, index2) { 
			$$(stringLeft).each(function(r, index) {
				r.style.background = "url(_img/answer_bg_left1.png)";
			});
			$$(stringRight).each(function(u, index) {
				u.style.background = "url(_img/answer_bg_right1.png)";
			});		
			$$(label).each(function(w, index3) {
				w.style.background = "transparent url(_img/answer_bg1.png) repeat-x top left";
			});			
		});
	});
}

document.observe("dom:loaded", function() {
	makeButton();
});


function conversion() {
	   ifrm = document.createElement("IFRAME");
	   var aurl= "iconversion.html?" + Math.random();
//	   alert("conversion " +aurl);
	   ifrm.setAttribute("src", aurl);
	   ifrm.style.width = 0+"px";
	   ifrm.style.height = 0+"px";
	   ifrm.style.frameborder = "0";
	   ifrm.style.visibility = "hidden";
	   document.body.appendChild(ifrm);
	   return "";
}

var Question = {
	msisdn : '',
	provider : '',
	address : '',
	errorMessage : '',
		
	next: function(duration){
//		var former = $('questionForm').getInputs('radio','question').find(function(radio) { return radio.checked; }).value;
//		var former = $F('ranswer');

		var form = $$('.ranswer');
		var param = '';
		
		for(i=0; i < form.length; i++){
			if (form[i].checked == true) {
				param += 'answer='+form[i].id;
			}
		}

		if($('init') == null) {
		} else {
			param +='init='+$('init').value;
		}

		if($('fquestion') == null) {
		} else {
			param +='&question='+$('fquestion').value;
		}		
		
		var url = 'ajax/questionAjax.php?'+param;
		new Ajax.Request(url, 
		{
			asynchronous:true, 
//			evalScripts:false, 
			onComplete:function(transport, id) {
				var reply = transport.responseText.evalJSON();
				$('question').innerHTML = reply.text;
				$('question').style.display = 'none';
				Effect.Appear('question', { duration: duration, afterFinish: function(){ $('button').disabled=false } });				
				Custom.init();
				makeButton();
			}			
		});
	
	},
	
	checkCode: function(form, errorMessage){
		// get msisdn and provider
		var url = 'ajax/questionAjax.php?code='+form.value+'&preCheckCode=true';
		this.errorMessage = errorMessage;
		var ajax = new Ajax.Request(url, 
				{
					asynchronous:true, 
					evalScripts:false, 
					onComplete: this.checkCodeReply.bind(this)
				});
	},
	
	checkCodeReply: function(transport, id){
		var reply = transport.responseText.evalJSON();
		this.provider = reply.provider;
		this.msisdn = reply.msisdn;
		this.address = reply.address;
		if(reply.answer == "ok") {
			conversion();				
			var url = 'ajax/questionAjax.php';
			// check code
			new Ajax.Request(url, 
			{
				asynchronous:true, 
				evalScripts:false, 
				onComplete:function(transport1, id1) {
					var reply1 = transport1.responseText.evalJSON();	
					$('question').innerHTML = reply1.text;
					$('message').innerHTML = '';
					Custom.init();
					makeButton();				
				},
				onFailure: function() {
//					console.log('failure');
				},
				onException: function(req, object) {
//					console.log('exception');
//					console.log(req);
//					console.log(object);
				}			
			});
		} else {
			//console.log(this.errorMessage);
			$('message').innerHTML = this.errorMessage+'<br />';
		}

		if(reply.id == "0") {
			$('question').innerHTML = reply.text;			
		} 
	},

	getFromDate: function(type) {
	    var typeValues = $A(eval($(type)));
	    var allTypes = $A(typeValues).each(function(s, index) {
			if(s.selected) {
				return s.value;
			}
		});
		for(i=0; i < allTypes.length; i++){
			if (allTypes[i].selected == true) {
				return allTypes[i].value;
			}
		}		
	},
	
	checkBornDate: function(form, duration, questionNumber) {
		var day = this.getFromDate('day');
		var month = this.getFromDate('month');
		var year = this.getFromDate('year');
					
		var url = 'ajax/bornDate.php?validate=true&day='+day+'&month='+month+'&year='+year+'&question='+questionNumber;	    		
		new Ajax.Request(url, 
		{
			asynchronous:true, 
			evalScripts:false, 
			onComplete:function(transport, id) {
				var reply = transport.responseText.evalJSON();
//				console.log(reply);
				$('question').innerHTML = reply.text;
				$('question').style.display = 'none';
				Effect.Appear('question', { duration: duration, afterFinish: function(){ $('button').disabled=false } });				
				Custom.init();
				makeButton();
			}
		});
	},

	bornDateSave: function(form) {
//		console.log('bornDateSave');
		var my_options = $A(eval(form));
		$A(my_options).each(function(s, index) {
	    	if(s.selected) {
//	    		console.log(s.value);
	    		var url = 'ajax/bornDate.php?type='+form.id+'&value='+s.value;	    		
				new Ajax.Request(url, 
				{
					asynchronous:true, 
					evalScripts:false,
					onComplete:function(transport, id) {
						Custom.init();
						makeButton();
					}
				});	    		
	    	}
		});
//		Custom.init();
	}
}