
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++ )
      {
        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).selectedIndex != 0 )
                        {
                            countinputs ++;
                        }
                    }
                }

        }
      }
    }
  }
  if ( countinputs > 0 ) {
      return 1;
  }
  else
  {
      return 0;
  }

}

function disabledalluncheck( divid )
{
  var checkignore = 0;
  var objexist = $( divid );

  if ( objexist )
  {

    objexist.title='Du kan ikke velge flere mål, fullfør noen, eller velg vekk noen';
    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 != '' )
        {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {

                if ( obj.item(i).type == 'text' )
                {
                    obj.item(i).disabled = true;
                }
                else if ( obj.item(i).type == 'checkbox' )
                {
                    obj.item(i).disabled = true;
                }
                else if ( obj.item(i).type == 'radio' )
                {
                     obj.item(i).disabled = true;
                }

            }

         }

     }
    }

    obj = objexist.getElementsByTagName("SELECT");
    if ( obj )
    {
      for ( i = 0; i < obj.length; i++ )
      {
        if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
        {
                strobj = new String(obj.item(i).id);
                if ( strobj.substr( 0 ,4) == "inp_" )
                {
                    obj.item(i).disabled = true;
                }
        }
      }
    }
  }
}

function enabledalluncheck( divid )
{
  var checkignore = 0;
  var objexist = $( divid );
  if ( objexist )
  {
    objexist.title='';
    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 != '' )
        {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {

                if ( obj.item(i).type == 'text' )
                {
                            obj.item(i).disabled = false;
                }
                else if ( obj.item(i).type == 'checkbox' )
                {
                    obj.item(i).disabled = false;
                }
                else if ( obj.item(i).type == 'radio' )
                {
                    obj.item(i).disabled = false;
                }
            }
          }
     }
    }

    obj = objexist.getElementsByTagName("SELECT");
    if ( obj )
    {
      for ( i = 0; i < obj.length; i++ )
      {
        if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
        {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {
                 obj.item(i).disabled = false;
            }
        }
      }
    }
  }
}

var GoalArray1 = Array();
var GoalArray2 = Array();
//var GoalArray3 = Array();

function onloadGoalinit()
{
   var obj1188 = document.getElementById('inp_sel_1188_1_0');
   if ( obj1188 ) {
       elementChangedGoalsFirst( obj1188, 0 );
   }

   var obj1193 = document.getElementById('inp_sel_1193_1_0');
   if ( obj1193 ) {
       var spanobj = document.getElementById('option_res_1193');
       if (spanobj) {
           var countOption = spanobj.innerHTML;
           if ( countOption == '1' ) {
               countOptionReal = 0;
           }
           if ( countOption == '2' ) {
               countOptionReal = 2;
           }
           if ( countOption == '3' ) {
               countOptionReal =4 ;
           }
           if ( countOption == '4' ) {
               countOptionReal = 6;
           }
           if ( countOption == '5' ) {
               countOptionReal = 8;
           }
           if ( countOption == '6' ) {
               countOptionReal = 10;
           }
           if ( countOption == '7' ) {
               countOptionReal = 12;
           }

           changedSelectGoals( obj1193, countOptionReal );
       }

   }


   var obj1199 = document.getElementById('inp_sel_1199_1_0');
   if ( obj1199 ) {
       var spanobj = document.getElementById('option_res_1199');
       if (spanobj) {
           var countOption = spanobj.innerHTML;

/*           if ( $('inp_sel_1199_2_0').selectedIndex != 0)
           {
               countOption = Math.floor((parseInt(countOption)-1)/2);
           }
           else
           {
              countOption = parseInt(countOption)-1;
           }*/

           countOption = parseInt(countOption)-1;
           changedSelectGoals( obj1199, countOption );

       }

   }

   var obj1201 = document.getElementById('inp_sel_1201_1_2');
   if ( obj1201 ) {
       var spanobj = document.getElementById('option_res_1201');
       if (spanobj) {
           var countOption = spanobj.innerHTML;
/*            if ( $('inp_sel_1201_2_2').selectedIndex != 0)
           {
               countOption = Math.floor((parseInt(countOption)-1)/2);
           }
           else
           {
              countOption = parseInt(countOption)-1;
           }
*/
           countOption = parseInt(countOption)-1;
           changedSelectGoals( obj1201, countOption );
       }

   }


   CheckCountGoalsLoad();

}

function changedSelectGoals(obj, countItem)
{
  for (var i=obj.options.length-1; i >= parseInt(countItem)+1; i--)
  {
      obj.remove(i);
  }

}

function elementChangedGoalsFirst( obj, idobjgoal )
{
  if ( obj )
  {

    var page_id = obj.id.split('_');
    page_id = page_id[page_id.length-1];
    var sel_id = obj.id.split('_');
    sel_id = sel_id[2];

    var currentvalue1 = 0;
    var currentvalue2 = 0;
    var currentvalue3 = 0;
    var selectList1 = $('inp_sel_'+sel_id+'_1'+'_'+(page_id) );
    var selectList2 = $('inp_sel_'+sel_id+'_2'+'_'+(page_id) );
  //  var selectList3 = $('inp_sel_'+sel_id+'_3'+'_'+(page_id) );
 //   if ( selectList1 && selectList2 && selectList3 ) {
   if ( selectList1 && selectList2  ) {


    if ( GoalArray1.length < 1 ) {

        for ( var i = 0; i <  selectList1.options.length; i++ ) {
            GoalArray1[selectList1.options[i].value] = selectList1.options[i].text;
        }
    }
    if ( GoalArray2.length < 1 ) {

        for ( var i = 0; i <  selectList2.options.length; i++ ) {
            GoalArray2[selectList2.options[i].value] = selectList2.options[i].text;
        }
    }
   /* if ( GoalArray3.length < 1 ) {

        for ( var i = 0; i <  selectList3.options.length; i++ ) {
            GoalArray3[selectList3.options[i].value] = selectList3.options[i].text;
        }
    }
*/

            if ( selectList1.selectedIndex != -1)
            {
                currentvalue1 = selectList1.options[selectList1.selectedIndex].text;
            }
            if ( selectList2.selectedIndex != -1)
            {
                currentvalue2 =  selectList2.options[selectList2.selectedIndex].text;
            }
  /*          if ( selectList3.selectedIndex != -1)
            {
                currentvalue3 =  selectList3.options[selectList3.selectedIndex].text;
            }
  */          var ua = navigator.userAgent.toLowerCase();
            if (( ua.indexOf("opera") != -1 ) || ( /MSIE [567]/.test( navigator.appVersion ) ) )
            {
                setTimeout( function(){ addSelectListIeOperaGoal( selectList1, GoalArray1, (Number( currentvalue2 ) + Number( currentvalue3 )) );}, 1);
                setTimeout( function(){ addSelectListIeOperaGoal( selectList2, GoalArray2, (Number( currentvalue1 ) + Number( currentvalue3 )) );}, 1);
    //            setTimeout( function(){ addSelectListIeOperaGoal( selectList3, GoalArray3, (Number( currentvalue1 ) + Number( currentvalue2 )) );}, 1);
            }
            else
            {
                addSelectListGoal( selectList1, GoalArray1, (Number( currentvalue2 ) + Number( currentvalue3 )) );
                addSelectListGoal( selectList2, GoalArray2, (Number( currentvalue1 ) + Number( currentvalue3 )) );
      //          addSelectListGoal( selectList3, GoalArray3, (Number( currentvalue1 ) + Number( currentvalue2 )) );
            }
            if (idobjgoal != 0) {
                 CheckCountGoals(obj, idobjgoal);
            }

    }
  }
}


function elementChangedFly( obj, idobjgoal )
{
  if ( obj )
  {

    var page_id = obj.id.split('_');
    page_id = page_id[page_id.length-1];
    var sel_id = obj.id.split('_');
    sel_id = sel_id[2];

    var currentvalue1 = 0;
    var currentvalue2 = 0;
    var selectList1 = $('inp_sel_'+sel_id+'_1'+'_'+(page_id) );
    var selectList2 = $('inp_sel_'+sel_id+'_2'+'_'+(page_id) );

   if ( selectList1 && selectList2  ) {

      var spanobj = document.getElementById('option_res_'+sel_id);
      if (spanobj)
      {

            var countOption = spanobj.innerHTML;
            var cursel = selectList1.selectedIndex;
            if ( selectList2.selectedIndex == 0)
            {
                countOption = parseInt(countOption)-1;
                var ua = navigator.userAgent.toLowerCase();
                if (( ua.indexOf("opera") != -1 ) || ( /MSIE [567]/.test( navigator.appVersion ) ) )
                {
                    setTimeout( function(){ addSelectListIeOperaFly( selectList1, countOption, cursel );}, 1);
                }
                else
                {
                    addSelectListFly( selectList1, countOption, cursel );
                }
            }
            else
            {
                countOption = Math.floor((parseInt(countOption)-1)/2);
                var ua = navigator.userAgent.toLowerCase();
                if (( ua.indexOf("opera") != -1 ) || ( /MSIE [567]/.test( navigator.appVersion ) ) )
                {
                    setTimeout( function(){ addSelectListIeOperaFly( selectList1, countOption, cursel );}, 1);
                }
                else
                {
                    addSelectListFly( selectList1, countOption, cursel );
                }
            }
     }
   }
  }
}


function addSelectListIeOperaGoal( objSelect, SelectArray, sumval )
{
            var cursel =  objSelect.selectedIndex;
            objSelect.options.length = 0;
            for (var key in SelectArray)
            {
                if ( ( Number( sumval ) + Number(SelectArray[key]) ) <= 7 )
                   {
                       addOptionGoal(objSelect, SelectArray[key], key);
                    }
            }
            if (Number(cursel) < Number(objSelect.options.length) ) {
                setTimeout( function(){objSelect.selectedIndex = cursel;}, 1 );
            }
            else
            {
                setTimeout( function(){objSelect.selectedIndex = 0;}, 1 );
            }




}
function addSelectListGoal( objSelect, SelectArray, sumval )
{
            var cursel =  objSelect.selectedIndex;
            objSelect.options.length = 0;
            for (var key in SelectArray)
            {
                if ( ( Number( sumval ) + Number(SelectArray[key]) ) <= 7 )
                   {
                       addOptionGoal(objSelect, SelectArray[key], key);
                    }
            }
            if (Number(cursel) < Number(objSelect.options.length) ) {
                objSelect.selectedIndex = cursel;
            }
            else
            {
                objSelect.selectedIndex = 0;
            }



}


function addSelectListIeOperaFly( objSelect, sumval, cursel )
{
            objSelect.options.length = 1;
            for (var i = 1; i<= sumval; i++)
            {
                addOptionGoal(objSelect, i, i+1);

            }
            if (Number(cursel) < Number(objSelect.options.length) ) {
                setTimeout( function(){objSelect.selectedIndex = cursel;}, 1 );
            }
            else
            {
                setTimeout( function(){objSelect.selectedIndex = Number(objSelect.options.length)-1;}, 1 );
            }
}
function addSelectListFly( objSelect, sumval, cursel )
{
            objSelect.options.length = 1;
            for (var i = 1; i<= sumval; i++)
            {
                    addOptionGoal(objSelect, i, i+1);
            }
            if (Number(cursel) < Number(objSelect.options.length) ) {
                objSelect.selectedIndex = cursel;
            }
            else
            {
                objSelect.selectedIndex = Number(objSelect.options.length)-1;
            }
}

function addOptionGoal (oListbox, text, value)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);
  oListbox.appendChild(oOption);
}

function saveGoalajax(objcur, url)
{



    var params = '';
    var objdiv = objcur.parentNode;
    if( objdiv.tagName != "DIV")
    {
       objdiv = objcur.parentNode.parentNode;
       if( objdiv.tagName != "DIV")
       {
          objdiv = objcur.parentNode.parentNode.parentNode;
          if ( objdiv.tagName != "DIV" ) {
           alert('Can not find object');
            return;
           }
       }

    }

    if ( objdiv ) {


    var obj = objdiv.getElementsByTagName("input");
    if ( obj )
    {
     for ( i = 0; i < obj.length; i++ )
     {

        if (obj.item(i).name == "QuestionObjectID[]")
        {
            if ($('subscribed_exist').value == -1) {
                $('subscribed_exist').value = 1;
                params = 'SetGoalsGiveFIRST=1'+'&QuestionObjectID='+obj.item(i).value+params;
            }
            else
            {
                params = 'QuestionObjectID='+obj.item(i).value+params;
            }
        }
        if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
        {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {
                if ( obj.item(i).type == 'text' )
                {

                    if ( obj.item(i).value.replace(/(^\s+)|(\s+$)/g, "").length > 0 ) {
                        params += "&";
                        params += obj.item(i).name +'='+ obj.item(i).value;
                      }

                }
                else if ( obj.item(i).type == 'checkbox' )
                {
                    if ( obj.item(i).checked ) {
                        params += "&";
                        params += obj.item(i).name+ '=' + obj.item(i).value;
                    }
                }
                else if ( obj.item(i).type == 'radio' )
                {
                   if (obj.item(i).checked)
                   {
                     params += "&";
                     params += obj.item(i).name +'='+obj.item(i).value;
                   }
                }
            }
            if ( obj.item(i).id == "ignore" )
            {
                params += "&";
                params += obj.item(i).name +'='+ obj.item(i).value;
            }

          }
     }
    }

    obj = objdiv.getElementsByTagName("SELECT");
    if ( obj )
    {
      for ( i = 0; i < obj.length; i++ )
      {
        if( typeof obj.item(i).id != undefined && obj.item(i).id != '' )
        {
            strobj = new String(obj.item(i).id);
            if ( strobj.substr( 0 ,4) == "inp_" )
            {

                    params += "&";
                    //params += obj.item(i).name + '=' + obj.item(i).text;
                    params += obj.item(i).name + '=' + obj.item(i).value;

            }
        }
      }
    }
      var method = "POST";
      var onload = goalSave;
      var onerror = errorHandler;
      var contentType = headers = false;
      return setAjaxRequest(method, url, params, onload, onerror, contentType, headers);
    }
}

function goalSave()
{
     result = eval( '(' + this.req.responseText + ')' );
     if ( result.error_code!="0" )
     {
          alert('Error: Goal do not save!!!');
     }
    // else
    // {
    //     show( $('goal-save-message') );
    //     setTimeout('hide($(\'goal-save-message\')) ', 1000);
    // }



     return true;
}


function savesendGoalajax(objcur ,url)
{

    var params = '';
    params = 'SetGoalsGive=1';
    obj = $('nxc_goal_subscribe_type_select');
    if ( obj )
    {
        params += "&";
        params += obj.name + '=' + obj.value;
    }

    objsms = $('sms');
    if ( objsms )
    {
        if (objsms.checked)
        {
            params += "&";
            params += objsms.name + '=' + objsms.value;
        }
    }

      var method = "POST";
      var onload = goalSave;
      var onerror = errorHandler;
      var contentType = headers = false;
      return setAjaxRequest(method, url, params, onload, onerror, contentType, headers);

}

function showSysMessage(obj)
{
    var XY  = findPos(obj);
    var X = XY[0];
    var Y = XY[1];

    var sysmessagediv;

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

    show(sysmessagediv);
}


