function Set_Cookie( name, value, expires, path, domain, secure )
    {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }

    function Get_Cookie( name ) {

    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
    }

    if (screen.width <= 800)
    {
     Set_Cookie('res','800','3','','','');
    }
    else if (screen.width == 1024)
    {
     Set_Cookie('res','1024','3','','','');
    }
    else if (screen.width == 1152)
    {
     Set_Cookie('res','1152','3','','','');
    }
    else if (screen.width > 1152)
    {
     Set_Cookie('res','1280','3','','','');
    }


    function clear_Element(element, default_text)
    {
      if(element.value.indexOf(default_text) == 0)
      {
         element.value = "";
      }
    }

    var clockID = 0;


    //////news_pop functions/////////////


    var xmlHttp;
    var div_name=''; //pop_news
 //   var div_to_kill='';


    function stateChanged()
    {
     //alert('1 '+div_name);
     if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && div_name!='' && xmlHttp.responseText!='')
     {
         document.getElementById(div_name).style.display="block";
         document.getElementById(div_name).innerHTML = xmlHttp.responseText;
         //alert(xmlHttp.responseText);
     }
    }


    function GetXmlHttpObject()
    {
     var objXMLHttp=null;

     if (window.XMLHttpRequest)
     {
       objXMLHttp=new XMLHttpRequest();
     }
     else if (window.ActiveXObject)
     {
       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     return objXMLHttp;
    }

    function update_redir_frame(div_id, sess_id)
    {
     xmlHttp=GetXmlHttpObject();
     div_name=div_id;
     if (xmlHttp==null)
     {
       //alert ("Browser does not support HTTP Request");
       return;
     }
     var date = new Date();
     var timestamp = date.getTime();

     var url="aj_check_news_pop.php";
     url=url+"?sess_id=" + sess_id +"&time="+timestamp;

     //alert (url + ' ' + div_name);


     xmlHttp.onreadystatechange=stateChanged;
     xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
     //div_name='';
    }

    function update_loader_image(user_id)
    {
     xmlHttp=GetXmlHttpObject();
     div_name="loader";
     if (xmlHttp==null)
     {
       //alert ("Browser does not support HTTP Request");
       return;
     }

     var url="aj_images.php";
     url=url+"?user_id=" + user_id;

     xmlHttp.onreadystatechange=stateChanged;
     xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
    }


    function UpdateClock() {
       if(clockID) {
          clearTimeout(clockID);
          clockID  = 0;
       }

       var tDate = new Date();


       if(tDate.getSeconds()==20 ||
          tDate.getSeconds()==40 ||
          tDate.getSeconds()==59)
       {
         if(navigator.appName == "Netscape")
         {
           update_redir_frame('news_pop','<?=set_get_sessid()?>');
         }
       }

       clockID = setTimeout("UpdateClock()", 1000);
    }
    function start_news_clock() {
       clockID = setTimeout("UpdateClock()", 500);
    }

    function kill_news_clock() {
       if(clockID) {
          clearTimeout(clockID);
          clockID  = 0;
       }
    }


    //////other pop functions/////////

        // slänger in en visa popup //
        function show_div_at_curs2(diven, event)
        {
            var x = 0;
            var y = 0;
            div_obj = document.getElementById(diven);


                x = event.clientX + document.body.scrollLeft;
                y = event.clientY + document.body.scrollTop;

                 changeOpac(0, diven);
                div_obj.style.display="block";
                 div_obj.style.left =  (x -450)+'px';
                 div_obj.style.top = (y-120)+'px';

                opacity(diven, 0, 100, 0);
        }

        function show_div_at_center(diven, event)
        {
                var x = 1;
            var y = 1;
                var doc_h, doc_w, scr_t;

            div_obj = document.getElementById(diven);

            doc_w = document.documentElement.clientWidth;
                doc_h = document.documentElement.clientHeight;
                scr_t = document.documentElement.scrollTop;

              y = ((doc_h / 2)+scr_t)-150;
        x = (doc_w / 2)-50;

                  changeOpac(0, diven);
                 div_obj.style.display="block";
                 div_obj.style.left =  x+'px';
                 div_obj.style.top = y+'px';

                opacity(diven, 0, 100, 0);
        }


   function post_quote(post_id)
    {
                var text_area=document.getElementById("posted_message")
                var text_quote=document.getElementById(post_id);
                text_area.value = text_quote.value ;
    }

    var dragObj = new Object(); //Global object to hold on to drag information.

    function drag_div(id, event)
    {
              var el;
            var x,y;

                   dragObj.elNode = document.getElementById(id);

            x = event.clientX + window.scrollX;
            y = event.clientY + window.scrollY;
            dragObj.cursorStartX = x;
            dragObj.cursorStartY = y;

            dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
                  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

            if (document.addEventListener)
                {
                    document.addEventListener("mousemove", dragGo,   true);
                    document.addEventListener("mouseup",   dragStop, true);
                    event.preventDefault();
                }
                else
                {
                    document.attachEvent("onmousemove", dragGo);
                    document.attachEvent("onmouseup",   dragStop);
                    window.event.cancelBubble = true;
                    window.event.returnValue = false;
            }
    }

    function dragGo(event)
    {
            var x,y;

                //x = event.clientX + window.scrollX;
            //y = event.clientY + window.scrollY;
                x = event.clientX + document.body.scrollLeft;
                        y = event.clientY + document.body.scrollTop;



            dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
                  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

                if (document.addEventListener)
                {
                    event.preventDefault();
                }
                else
                {
                        window.event.cancelBubble = true;
                    window.event.returnValue = false;
                }
    }

    function dragStop(event)
    {
                if (document.addEventListener)
                {
                    document.removeEventListener("mousemove", dragGo,   true);
                    document.removeEventListener("mouseup",   dragStop, true);
                }
                else
                {
                    document.detachEvent("onmousemove", dragGo);
                    document.detachEvent("onmouseup",   dragStop);
                }

            dragObj = new Object();
    }

    function show_div_at_curs(diven,e,user_id)
    {
        var x = 0;
        var y = 0;

        if (!e)
        {
           if(window.event)
           {
             //Internet Explorer
             e = window.event;
           }
        }

        if (typeof(e.pageX)=="number")
        {
           x = e.pageX;
           y = e.pageY;
        }
        else if (typeof(e.clientX)=="number")
        {
           x = e.clientX;
           y = e.clientY;

          var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
            ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
            ( navigator.vendor == 'KDE' )
           if( !badOldBrowser )
           {
             if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
             {
               //IE 4, 5 & 6
               x += document.body.scrollLeft;
               y += document.body.scrollTop;
             }
             else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
             {
               //IE 6
               x += document.documentElement.scrollLeft;
               y += document.documentElement.scrollTop;
             }
           }
        }

        //alert(x+' '+y);

        diven.style.display="block";
        diven.style.left = x + 28+'px';
        diven.style.top = y + -28+'px';

        if(user_id)
        {
          update_loader_image(user_id);
        }
    }

    function hide_div(div)
    {
      div_to_kill=div;
      opacity(div, 100, 0, 1900);
      setTimeout("kill_div()", 1000);
    }

    function hide_div2(div)
    {
      div_to_kill=div;
      opacity(div, 100, 0, 1900);
      setTimeout("kill_div()", 0);
    }

    function show_div(diven)
    {
      document.getElementById(diven).style.display="block";
    }

    function kill_div(div_to_kill)
    {
      document.getElementById(div_to_kill).style.display="none";
    }

    function kill_div2(div_to_kill)
    {
      document.getElementById(div_to_kill).style.display="none";
    }

    function opacity(id, opacStart, opacEnd, millisec)
    {
       //speed for each frame
       var speed = Math.round(millisec / 100);
       var timer = 0;

       //determine the direction for the blending, if start and end are the same nothing happens
       if(opacStart > opacEnd)
       {
        for(i = opacStart; i >= opacEnd; i--)
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
       }
       else if(opacStart < opacEnd)
       {
        for(i = opacStart; i <= opacEnd; i++)
        {
          setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
          timer++;
        }
       }
    }

    //change the opacity for different browsers
    function changeOpac(opacity, id)
    {
        var object = document.getElementById(id).style;
        object.opacity = (opacity / 100);
        object.MozOpacity = (opacity / 100);
        object.KhtmlOpacity = (opacity / 100);
        object.filter = "alpha(opacity=" + opacity + ")";
    }

//funktioner till omröstningeformuläret.
            var vote_arrInput = new Array();
        var vote_arrInputValue = new Array();

                vote_arrInput[0] = 0;
                vote_arrInput[1] = 1;
                vote_arrInputValue[0] = "Alternativ 1";
                  vote_arrInputValue[1] = "Alternativ 2";

        function vote_addInput()
        {
                  vote_arrInput.push(vote_arrInput.length);
                  vote_arrInputValue.push("");
                  vote_display();
        }

        function vote_display()
        {
          document.getElementById('alt').innerHTML="";
          for (intI=0;intI<vote_arrInput.length;intI++) {
            document.getElementById('alt').innerHTML+=vote_createInput(vote_arrInput[intI], vote_arrInputValue[intI]);
          }
          document.form_new_vote.alt_count.value = vote_arrInput.length;
        }

        function vote_saveValue(intId,strValue)
        {
          vote_arrInputValue[intId]=strValue;
        }

        function vote_createInput(id,value)
        {
          return id+1+"<input type='text' name='alt_"+ id +"' id='alt_"+ id +"'  class='vote_alt_input'   onChange='javascript:vote_saveValue("+ id +",this.value)' value='"+ value + "'><br>";
        }

        function vote_deleteInput()
        {
          if (vote_arrInput.length > 0)
          {
             vote_arrInput.pop();
             vote_arrInputValue.pop();
          }
          vote_display();
        }


    function validate_not_empty(field,alerttxt)
        {
                with (field)
                {
                        if (value==null||value=="")
                          {
                                  document.getElementById('err_alt').innerHTML = "<span class='vote_errormsg'>"+alerttxt+"</span>";
                                  return false;
                          }
                        else { return true }
                }
        }

        function validate_count_min(field, min_value, alerttxt)
        {
                with (field)
                {
                        if (value<min_value)
                        {
                                document.getElementById('err_alt').innerHTML = "<span class='vote_errormsg'>"+alerttxt+"</span>";
                                return false;
                        }
                        else { return true;        }
                }
        }

        function validate_count_max(field, max_value, alerttxt)
        {
                with (field)
                {
                        if (value>max_value)
                        {
                                document.getElementById('err_alt').innerHTML = "<span class='vote_errormsg'>"+alerttxt+"</span>";
                                return false;
                        }
                        else { return true;        }
                }
        }



        function vote_validate(thisform)
        {
                with (thisform)
                {
                        if (validate_not_empty(about,"Rubrik saknas")==false)
                        { return false; }
                        if (validate_not_empty(about_post,"Saknar text")==false)
                        { return false; }
                        if (validate_count_min(alt_count,2,"Måste vara minst 2 alternativ")==false)
                        { return false; }
                        if (validate_count_max(alt_count,8,"Får vara max 8 alternativ")==false)
                        { return false; }
                                    for (intI=0;intI<alt_count.value;intI++)
                                    {
                                                   if(validate_not_empty(thisform['alt_'+intI],"Text saknas i alternativ "+(intI+1))==false)
                                                   {return false; }
                                    }
                }
        }
