/*
  depends on: mootools with Ajax - option included
*/

function msg(msg,id,type){
  //new div element, displaying failure case
  var msgDiv = new Element('div');
  msgDiv.id = 'errordiv_'+id;
  msgDiv.injectInside($E('body'));
  msgDiv.setStyle('position','absolute');
  msgDiv.setStyle('top','10px');
  msgDiv.setStyle('left','10px');
  if(type === 'error'){
   msgDiv.setStyle('background-color','#FF0000');
  } else if(type === 'msg') {
   msgDiv.setStyle('background-color','#0000FF');
  }
  msgDiv.setStyle('color','#fff');
  msgDiv.setStyle('fontWeight','bold');
  var errorStartDuration = 500;
  var errorEndDuration   = 500;
  var myEffects = new Fx.Styles(msgDiv, {
   duration:errorStartDuration,
   transition: Fx.Transitions.linear,
   onComplete: function(){
   }
  }); 
  myEffects.start({
    'height': [0, 40],
    'width': [0, 100]
  }).chain(function(){
         msgDiv.appendText(msg);
         (function(){msgDiv.setText('')}).delay(3000);
         this.start.delay(3000, this, {
		         'width': [100, 0],
		         'height': [40, 0]         
	       });
         (function(){msgDiv.remove()}).delay(3500);
  });
}

function doAjax(url,target){
  var ajax = new Ajax(url, {method: 'get', update: target}).request();
}

function getContentFadeIn(url,id){
   var ajax = new Ajax(url, {
      method:  'get', 
      update:  $(id),
      evalScripts: true,
      onRequest: function() { 
         actImg.setStyle('display','block'); 
         $(id).effect('opacity').start(0);
      },
      onComplete: function(){ 
         $(id).effect('opacity').start(1);
         actImg.setStyle('display','none');  
      }, 
      onFailure: function(){ msg('Toiming ebaõnnestus!',id,'error') }
  });
  ajax.request();
}





window.addEvent('domready', function() {

  if($('heartriskcalculator')){
      
      
// Variable that holds the effects.
var fx = {
	'loading': new Fx.Style( 'loading', 'opacity',{ duration: 200 } ),
	'success': new Fx.Style( 'success', 'opacity',{ duration: 200 } ),
	'fail': new Fx.Style( 'fail', 'opacity',{ duration: 200 } )
};

// Hides the loading div, and shows the el div for
// a period of four seconds.
var showHide = function( el ){
	fx.loading.set(0);
	(fx[ el ]).start(0,1);
	(function(){ (fx[ el ]).start(1,0); }).delay( 4000 );
}


// Listen for click events on the submit button.
$('submit').addEvent('click', function(evt){
	// Stops the submission of the form.
	new Event(evt).stop();

	// Sends the form to the action path,
	// which is ’script.php’.
	$('heartriskform').send({
   update: $('risk'),
		onRequest: function(){
			// Show loading div.
			fx.loading.start( 0,1 );
		},
		onSuccess: function(response){
			// Hide loading and show success for 3 seconds.
			showHide('loading');
			showHide('success');
		},
		onFailure: function(){
			// Hide loading and show fail for 3 seconds.
			showHide('loading');
			showHide('fail');
		}
	});
} );  
   var sum = 0;
   /*
   function calculateRisk(){
      var elems = $('heartriskcalculator').getElements('input[name^=item_]');
      elems.each(function(el){
         var id = el.id.split('_')[1];
         switch(id){
            case 'nyha': 
               break;
            case 'ef':
               break;
            case 'ischemic':
               break;
            case 'sbp':
               break;
            case 'acei':
               break;
            case 'diuretic':
               break;
            case 'na':
               break;
            case 'hemoglobin':
               break;
            case 'lymphs'':
               break;
            case 'acid'':
               break;
            case 'cholesterol':
               break;
         }
      }      
   }
      var elems = $$('.item');
      elems.each(function(el){
         var id = el.id.split('_')[1];
         if(id == 'age'){
            el.addEvent('change', function(e) {
               //getting old age
               sum = parseInt($('age').value) / 10 * ln (1.09)); 
               alert(sum);
            });
         } else if(id === 'genderm'){
         
         } else if(id === 'genderf'){

         } else {
            el.addEvent('keyup', function(e) {
               calculateRisk();
               alert(el.id);
            }
         }      
      }*/
      
  }
 
});



function numbersonly(e, decimal) {
   var key;
   var keychar;

   if (window.event) {
      key = window.event.keyCode;
   }
   else if (e) {
      key = e.which;
   }
   else {
      return true;
   }
   keychar = String.fromCharCode(key);

   if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
      return true;
   }
   else if ((("0123456789").indexOf(keychar) > -1)) {
      return true;
   }
   else if (decimal && (keychar == ".")) {
     return true;
   }
   else
      return false;
}


function calculateTotals(amount){
   //three columns to calculate
   var total_visits=0;
   var total_studies=0;
   var total_sums=0;
   for(var i = 0; i < amount; i++){
   //  alert($('studies_' + i).value*1);
   //   total_visits += $('visits_' + i).value*1;
   //   total_studies += $('studies_' + i).value*1;
      total_sums += $('sum_' + i).value*1;
   }


  // $('total_visits').value = total_visits*1;
  // $('total_studies').value = total_studies*1;
   $('total_sums').value = Math.round(total_sums*100)/100;
}