
function calculateNext_GOAL( curNum )
{
  return curNum+1;
}
function calculatePrev_GOAL( curNum )
{
  return curNum-1;
}

function nextTab_GOAL( str ){

    if ( validate( str ) )
    {
       var tmp = str.split( '_' );
       hide( $(curQuestion) );
       tmp = calculateNext_GOAL( parseInt(tmp[1]) );
//       updateProgressBar( tmp );
       lastQuestion = curQuestion;
       curQuestion = 'page_'+tmp;
       highlight_GOAL( $(curQuestion) );
       show( $(curQuestion), true );
    }
    else
    {
//       show( $("invalidMsg_"+curQuestion) );
    }
}

function highlight_GOAL ( o )
{
   var categNum = 'categ_'+o.childNodes[0].innerHTML;
   if ( curCategory != categNum )
   {
       $(curCategory).className='goal_li';
//       hide( $(curCategory+'_questions') );
       curCategory = categNum;
       $(curCategory).className='goal_li active';
//       show( $(curCategory+'_questions'), false );
   }
}

function prevTab_GOAL( str ){
    var tmp = str.split( '_' );
    hide( $(curQuestion) );
    tmp = calculatePrev_GOAL( parseInt(tmp[1]) );
//    updateProgressBar( tmp );
    curQuestion = 'page_'+tmp;
    highlight_GOAL( $(curQuestion) );
    show( $(curQuestion), true );

    return true;
}

function setCategoryActive_GOAL( categoryIndex ){
//    curQuestion='page_'+pageIndex;
//
    $("div_categories").style.display='none';
    $("questions_goals").style.display='block';

    nextTab_GOAL ( 'page_'+categoryIndex );
    return true;
}


function setCategoryMain_GOAL( ){
//    curQuestion='page_'+pageIndex;
//
    $("div_categories").style.display='block';
    $("questions_goals").style.display='none';

    location.hash = 'goals';
    sysmessagediv = $('goal-sys-message');
    hide(sysmessagediv);
    //nextTab_GOAL ( 'page_'+categoryIndex );
    return true;
}


function updateProgressBar_GOAL( num )
{
   var total_questions = 48;
   var maxDivWidth = 840;
   var filled = 100*num/total_questions;
   var obj = $('cur_progress');
   obj.style.width = (maxDivWidth/100)*filled+'px';
}

function findPos( obj )
{
   var posX = obj.offsetLeft;
   var posY = obj.offsetTop;
   while ( obj.offsetParent )
   {
      posX = posX + obj.offsetParent.offsetLeft;
      posY=  posY + obj.offsetParent.offsetTop;
      if( obj == document.getElementsByTagName('body')[0]) { break; }
      else
      {
         obj = obj.offsetParent;
      }
   }
   return [ posX , posY ];
}

function goalStatusChange( checkbox, ajax_url )
{
    var id_part_array = checkbox.id.split( '_' );
    if ( id_part_array[1] == 'not' )
    {
        if ( checkbox.checked )
        {
             undoGoal(checkbox.value, ajax_url);
        }
        else
        {
             doGoal( checkbox.value, ajax_url );
        }
    }
    else
    {
        if ( checkbox.checked )
        {
             doGoal( checkbox.value, ajax_url );
        }
        else
        {
             undoGoal(checkbox.value, ajax_url);
        }

    }
    return true;
}

function doGoal( node_id, ajax_url )
{
     var dochekbox_id = "check_not_done_" + node_id;
     $(dochekbox_id).checked = false;
     var undochekbox_id = "check_done_" + node_id;
     $(undochekbox_id).checked = true;
     var XY  = findPos($(undochekbox_id));
var X = XY[0];
var Y = XY[1];

     var messagediv;

      messagediv = $('goal-done-message');
X = parseInt( X ) + 0;
Y = parseInt( Y ) + 0;
hide($('goal-done-message'));
     messagediv.style.top = Y + 'px';
     messagediv.style.left = X + 'px';

     sendDoRequest( ajax_url, node_id, 1);
}
function undoGoal( node_id, ajax_url )
{
     var dochekbox_id = "check_not_done_" + node_id;
     $(dochekbox_id).checked = true;
     var undochekbox_id = "check_done_" + node_id;
     $(undochekbox_id).checked = false;
     sendDoRequest( ajax_url, node_id, 0 );
}


function sendDoRequest( moduleurl, node_id, action )
{

      var method = "GET";
      var url = moduleurl + "?node_id=" + node_id + "&action=" + action;
      var params = '';
      var onload = goalAnsHandler;
      var onerror = errorHandler;
      var contentType = headers = false;
      return setAjaxRequest(method, url, params, onload, onerror, contentType, headers);
}

function goalAnsHandler()
{
     result = eval( '(' + this.req.responseText + ')' );
     if ( result['save_list'].length == 4 )
     {
         var co2_save = result['save_list'][2]['co2_save'];
         var points = result['save_list'][2]['points_increase'];

         var doUndo = result['save_list'][3];
         if ( doUndo == 'do' )
         {
             if ( co2_save > 0 )
             {
                 $('co2-save-message').style.display = 'inline';
                 $('co2-save').innerHTML = co2_save;
             }
             else
             {
                 $('co2-save-message').style.display = 'none';
             }
             if ( points > 0 )
             {
                 $('points-gain-message').style.display = 'inline';
                 $('points-gain').innerHTML = points;
             }
             else
             {
                 $('points-gain-message').style.display = 'none';
             }
             updateActivityBar( co2_save, points, true );
             show( $('goal-done-message'), true );
	     setTimeout('hide($(\'goal-done-message\')) ',1000*30);
         }
         else
         {
             updateActivityBar( co2_save, points, false );
         }

     }
     return true;
}

function updateActivityBar( co2_add, points_add, plus )
{
   co2_add = parseInt( co2_add );
   points_add = parseFloat( points_add );
   var tmp = getCO2Values();
   var total_save_co2 = parseFloat( tmp[0] );
   var result_co2 = parseFloat( tmp[1] );
   tmp = getPointValues();
   var total_save_points = parseFloat( tmp[0] );
   var result_p = parseFloat( tmp[1] );


   var new_percents_co2 = 0;
   var new_percents_points = 0;
   if ( plus )
   {
        new_percents_co2 = Math.round( ( (total_save_co2 + co2_add )/ result_co2) * 100 );
	setCO2Values( total_save_co2 + co2_add, result_co2 );
    if ( Math.round(total_save_points + points_add - result_p) >= 0 ) {
        new_percents_points =  100;
        setPointValues( Math.round(total_save_points + points_add),Math.round( total_save_points + points_add ));
    }
    else
    {
        new_percents_points =  Math.round( (total_save_points + points_add)*100/result_p);
        setPointValues( Math.round(total_save_points + points_add), result_p );
    }

   }
   else
   {
        new_percents_co2 = Math.round( ( (total_save_co2 - co2_add )/ result_co2) * 100 );
        setCO2Values( Math.round(total_save_co2 - co2_add), result_co2 );

        new_percents_points =  Math.round( (total_save_points - points_add)*100/result_p);
        //new_percents_points =  Math.round( ( result_p + total_save_points - points_add )/10);
       setPointValues( Math.round(total_save_points - points_add), result_p );

   }

   $('percents_co2').innerHTML = new_percents_co2;
   $('percents_points').innerHTML = new_percents_points;
   for( var i = 1; i <= 10; i++ )
   {
       if ( i  > Math.round( new_percents_co2 / 10 ) )
       {
          hide($( 'co2_' + i* 10));
       }
       else
       {
          show( $( 'co2_' + i* 10), false );
       }
       if ( i  > Math.round( new_percents_points / 10 ) )
       {
          hide($( 'point_' + i* 10));
       }
       else
       {
          show( $( 'point_' + i* 10), false );
       }
   }


}


function updateActivityBarWork( points_add, plus )
{
   points_add = parseFloat( points_add );

   tmp = getPointValues();
   var total_save_points = parseFloat( tmp[0] );
   var result_p = parseFloat( tmp[1] );
   var workpointcurrent = parseFloat($('shownworkvaluespoints').innerHTML);


   var new_percents_points = 0;
   if ( plus )
   {
       workpointcurrent =  Math.round(workpointcurrent + points_add);
    if ( Math.round(total_save_points + points_add - result_p) >= 0 ) {
        new_percents_points =  100;
        setPointValues( Math.round(total_save_points + points_add),Math.round( total_save_points + points_add ));

    }
    else
    {
        new_percents_points =  Math.round( (total_save_points + points_add)*100/result_p);
        setPointValues( Math.round(total_save_points + points_add), result_p );
    }

   }
   else
   {
       workpointcurrent =  Math.round(workpointcurrent - points_add);
        new_percents_points =  Math.round( (total_save_points - points_add)*100/result_p);
        //new_percents_points =  Math.round( ( result_p + total_save_points - points_add )/10);
       setPointValues( Math.round(total_save_points - points_add), result_p );

   }
   $('shownworkvaluespoints').innerHTML = " "+workpointcurrent+" ";

   $('percents_points').innerHTML = new_percents_points;
   for( var i = 1; i <= 10; i++ )
   {
       if ( i  > Math.round( new_percents_points / 10 ) )
       {
          hide($( 'point_' + i* 10));
       }
       else
       {
          show( $( 'point_' + i* 10), false );
       }
   }


}


function getCO2Values()
{
	return $('resultvaluesco2').innerHTML.split( '|' );
}

function getPointValues()
{
	return $('resultvaluespoints').innerHTML.split( '|' );
}

function setCO2Values( val1, val2 )
{
   $('resultvaluesco2').innerHTML = val1 + '|' + val2;
   $('shownvaluesco2').innerHTML = val1;
}
function setPointValues( val1, val2 )
{
   $('resultvaluespoints').innerHTML = val1 + '|' + val2;
   $('shownvaluespoints').innerHTML = val1+" ";
   $('showntotalvaluespoints').innerHTML = " "+val2;
   // Math.round( ( val1 + val2) );
}

function sendTipsCheckBoxClicked( obj )
{
  var checked = obj.checked;
  if ( checked )
  {
    show( $('nxc_goal_subscribe_type') );
  }
  else
  {
    hide( $('nxc_goal_subscribe_type') );
  }
}

var idforcomment=0;

function CheckCountGoals(objclick, idgoal)
{
    var divid = 'questions_goals';
    var countinputs = 0;
    var res;
    var divsarray = Array();
    var checkfordis;
    var obj = $( divid );
    if ( obj )
    {


        objdivs = obj.getElementsByTagName("div");
        if ( objdivs )
        {
            for ( k = 0; k < objdivs.length; k++ )
            {
                if( typeof objdivs.item(k).id != undefined && objdivs.item(k).id != '' )
                {
                    if ( objdivs.item(k).id.substr( 0, 15) == "question_block_" )
                    {
                        res = getCountAllInputs( objdivs.item(k).id );
                        if (res == 0 ) {
                           divsarray[divsarray.length] = objdivs.item(k).id;
                        }
                        else
                        {
                           countinputs += res;
                        }
                    }
                }
            }
        }
        $('count_goals').innerHTML = countinputs;

        if( parseInt(countinputs) > 7 ) /// max ammount of goals
        {


            if (idforcomment != 0)
            {
              $(idforcomment).innerHTML = '';
            }


            $('comment_for_'+idgoal).innerHTML = '';
            for ( m = 0; m < divsarray.length; m++ )
            {
                 disabledalluncheck( divsarray[m] );
            }
            //alert('Du har nå valgt 8 mål. For å velge flere, må du fullføre noen');
        }
        else
        {



            if (idforcomment == 0)
            {
              idforcomment = 'comment_for_'+idgoal;
              kol = 8 - parseInt(countinputs);
              $(idforcomment).innerHTML = 'Veldig bra! Du kan setle deg '+kol+' mal til';
            }
            else
            {
              if( parseInt(countinputs) > 0 )
             {
               $(idforcomment).innerHTML = '';
               idforcomment = 'comment_for_'+idgoal;
               kol = 8 - parseInt(countinputs);
               $(idforcomment).innerHTML = 'Veldig bra! Du kan setle deg '+kol+' mal til';
             }
              else
              {
                 $(idforcomment).innerHTML = '';
                 idforcomment = '0';
              }

            }


            for ( m = 0; m < objdivs.length; m++ )
            {
                enabledalluncheck( divsarray[m] );
            }

       }
    }
}


function CheckCountGoalsLoad(){
    var divid = 'questions_goals';
    var countinputs = 0;
    var res;
    var divsarray = Array();
    var checkfordis;
    var obj = $( divid );
    if ( obj ) {

        objdivs = obj.getElementsByTagName("div");
        if ( objdivs ) {
            for ( k = 0; k < objdivs.length; k++ )
            {
                if( typeof objdivs.item(k).id != undefined && objdivs.item(k).id != '' )
                {
                    if ( objdivs.item(k).id.substr( 0, 15) == "question_block_" )
                    {
                        res = getCountAllInputs( objdivs.item(k).id );
                        if (res == 0 ) {
                           divsarray[divsarray.length] = objdivs.item(k).id;
                        }
                        else
                        {
                           countinputs += res;
                        }
                    }
                }
            }
        }
        $('count_goals').innerHTML = countinputs;
        if( parseInt(countinputs) > 7 ) /// max ammount of goals
        {
            for ( m = 0; m < divsarray.length; m++ )
            {
                 disabledalluncheck( divsarray[m] );
            }
        }
        else
        {
            for ( m = 0; m < objdivs.length; m++ )
            {
                enabledalluncheck( divsarray[m] );
            }
        }
    }

}



function getCountAllInputs( objid ){

  var countinputs = 0;
  var checkignore = 0;
  var objexist = $( objid );
  var ignorequestions = Array();
  if ( objexist )
  {
    var obj = objexist.getElementsByTagName("input");
    if ( obj )
    {
      for ( i = 0; i < obj.length; i++ )
      {
         if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
         {
             if ( obj.item(i).id == "ignore" )
             {
                ignorequestions[ignorequestions.length] = obj.item(i).value;
             }
         }
      }
    }
    var obj = objexist.getElementsByTagName("input");
    if ( obj )
    {
     for ( i = 0; i < obj.length; i++ )
     {
        if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
        {
            checkignore = 0;
            for( j = 0; j < ignorequestions.length; j++)
            {
              if (  obj.item(i).id == ignorequestions[j])
              {
                    checkignore = 1;
              }
            }
          if ( checkignore == 0 )
          {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {

                if ( obj.item(i).type == 'text' )
                    {
                        if ( obj.item(i).value != "" )
                        {
                            countinputs ++;
                        }

                    }
                    else if ( obj.item(i).type == 'checkbox' )
                    {
                        if ( obj.item(i).checked )
                        {
                            countinputs ++;
                        }

                    }
                    else if ( obj.item(i).type == 'radio' )
                    {
                        if ( obj.item(i).checked )
                        {
                           countinputs ++;
                        }

                    }

            }
          }

        }
     }
    }

    obj = objexist.getElementsByTagName("SELECT");
    if ( obj )
    {
      for ( i = 0; i < obj.length; i++ )
 