﻿    var currx = 0;
    var curry;
    var isPressed = false;

    var mousex = 0;
    var mousey = 0;
    var grabx = 0;
    var graby = 0;
    var orix = 40;
    var oriy = 40;
    var elex = 0;
    var eley = 0;
    var algor = 0;

    var dragobj = null;

    var hiddenObj = null;

    var totalPoints = 0;
  
    function falsefunc() { return false; } // used to block cascading events

    function init() {
        document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
        update();
    }

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }


    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }

    function CheckURLForPoints(slider, hiddenObj, isPostBack, currentPointValue) {
        if (document.URL.indexOf("pointv", 0) > 1 && !isPostBack) {
            var endPos = document.URL.lastIndexOf("&");
            var pointInfo;
            if (endPos > document.URL.indexOf("pointv"))
                pointInfo = document.URL.substring(document.URL.indexOf("pointv", 0) + 7, endPos);
            else
                pointInfo = document.URL.substring(document.URL.indexOf("pointv", 0) + 7, document.URL.length);

            if (pointInfo != null && pointInfo.length > 0) {
                if (pointInfo >= 100000) {
                    pointInfo = 100000;
                }
                var currpos = pointInfo / 592;
                var slider = document.getElementById("myslider");
                var greenline = document.getElementById("greenline");
                var greyline = document.getElementById("greyline");
                var coordsTxt = document.getElementById("realpoints");

                
                coordsTxt.innerHTML = pointInfo;
                slider.style.position = "absolute";
                slider.style.left = currpos + "px";  // - 1065 + document.body.scrollLeft + document.documentElement.scrollLeft;
                greenline.style.width = currpos + "px";

                greyline.style.width = 170 - currpos + "px";
                greyline.style.left = currpos + "px";
                var hiddenField = document.getElementById(hiddenObj);
                hiddenField.value = pointInfo;
               }
        }
        else {

            currentPointValue = readCookie("slider");
          
            if (currentPointValue == null || currentPointValue > 100000) {
                currentPointValue = 100000;
            }
            var currpos = currentPointValue / 592;
            var slider = document.getElementById("myslider");
            var greenline = document.getElementById("greenline");
            var greyline = document.getElementById("greyline");
            var coordsTxt = document.getElementById("realpoints");
            if (currentPointValue >= 100000) {
                currentPointValue = 100000;
            }
            coordsTxt.innerHTML = currentPointValue;
            slider.style.position = "absolute";
            slider.style.left = currpos + "px";  // - 1065 + document.body.scrollLeft + document.documentElement.scrollLeft;
            greenline.style.width = currpos + "px";

            greyline.style.width = 170 - currpos + "px";
            greyline.style.left = currpos + "px";
            var hiddenField = document.getElementById(hiddenObj);
            hiddenField.value = currentPointValue;
        
        }
    }

    function getMouseXY(e) // works on IE6,FF,Moz,Opera7
    {
        if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

        if (e) {
            if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)            
                mousex = e.pageX;
                mousey = e.pageY;
                algor = '[e.pageX]';
                if (e.clientX || e.clientY) algor += ' [e.clientX] '
            //    var coordsTxt = document.getElementById("realpoints");
              //  coordsTxt.innerHTML = "#" + elex + "," + mousex + "," + grabx + "," + orix;
            }
            
            else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
            mousex = e.clientX + document.body.scrollLeft;  // + document.documentElement.scrollLeft;   //+ document.documentElement.scrollLeft;
           // mousex = e.clientX - 1065 + document.body.scrollLeft;  // + document.documentElement.scrollLeft;   //+ document.documentElement.scrollLeft;
              //  mousey = e.clientY -40 + document.body.scrollTop;
            algor = '[e.clientX]';
          //  var coordsTxt = document.getElementById("realpoints");
          //  coordsTxt.innerHTML = "" + elex + "," + mousex + "," + grabx + "," + orix;  
            if (e.pageX || e.pageY) algor += ' [e.pageX] '
                
            }
        }       
    }

    function update(e) {
        getMouseXY(e); // NS is passing (event), while IE is passing (null)
     
    }

    function grab(context,hidObject) {        
        document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
        dragobj = context;
        // alert("test");
        dragobj.style.zIndex = 11; // move it to the top
        
        document.onmousemove = drag;
        document.onmouseup = drop;
                
      
        elex = orix = dragobj.offsetLeft;
        eley = oriy = dragobj.offsetTop;

        hiddenObj = hidObject;
        
        update();
        grabx = mousex;
        graby = mousey;
        
    }

    function drag(e) // parameter passing is important for NS family
    {
        if (dragobj) {
            elex = orix + (mousex - grabx);
            //     eley = oriy + (mousey - graby);
            //            dragobj.style.position = "absolute";
            //            dragobj.style.left = (elex).toString(10) + 'px';
            //            dragobj.style.top = (eley).toString(10) + 'px';

            /*my code */
            var slider = document.getElementById("myslider");
            var greenline = document.getElementById("greenline");
            var greyline = document.getElementById("greyline");
            var coordsTxt = document.getElementById("realpoints");
           // coordsTxt.innerHTML = "" + elex + "," + mousex + "," + grabx + "," + orix;
            //            var hiddenTemp = document.getElementById("sliderscript");
            //            var urlString = unescape(hiddenTemp.src);
            //            var mytemp = urlString.substr(urlString.indexOf("hiddenFieldID="), urlString.length);
            var hiddenField = document.getElementById(hiddenObj);

            var offset = elex;  //- 1065 + document.body.scrollLeft + document.documentElement.scrollLeft;
            // coordsTxt.innerHTML = "X=" + offset+ ","+hiddenField.value   //+ ", elex=" + elex + ",hid=" + hiddenField;     
            //if (offset > -1 && offset < 175) {
            if (offset > -1 && offset < 170) {
                totalPoints = offset * 592;                
                      
                slider.style.position = "absolute";
                slider.style.left = offset + "px";  // - 1065 + document.body.scrollLeft + document.documentElement.scrollLeft;
                greenline.style.width = offset + "px";

                greyline.style.width = 170 - offset + "px";
                greyline.style.left = offset + "px";
                // greyline.style.left = 10 + elex;
                
                if (totalPoints > 100000) {
                    coordsTxt.innerHTML = 100000;
                }
                else {
                    coordsTxt.innerHTML = totalPoints;
                }                
               // alert("3Hidden value = " + hiddenField.value);
            }
        }
        else {
            var coordsTxt = document.getElementById("realpoints");
           // coordsTxt.innerHTML = "null";
        }
        update(e);
        return false; // in IE this prevents cascading of events, thus text selection is disabled
    }

    function drop() {
        if (dragobj) {
            dragobj.style.zIndex = 11;
            dragobj = null;
        }
        update();
        document.onmousemove = update;
        document.onmouseup = null;
        document.onmousedown = null;   // re-enables text selection on NS

//        var slider = document.getElementById("myslider");
//        alert("position is: " + slider.style.left + "," + document.URL.toString());
//        var urlStr = document.URL.toString();
//        if (urlStr.indexOf("?") > -1) {
//            // slider-key not added:
//            if (urlStr.indexOf("slider") == -1) {
//                if (urlStr.indexOf("&") > -1) {
//                    document.URL = urlStr + "&slider=" + totalPoints;
//                }
//                else {
//                    document.URL = urlStr + "?slider=" + totalPoints;
//                }
//            }
//            else {
//                document.URL = document.URL.substr(0, document.URL.indexOf("slider", 0)) + "&slider=" + totalPoints;
//            }
//        }
//        else {
//            document.URL = urlStr + "?slider=" + totalPoints;
//        }
        var hiddenField = document.getElementById(hiddenObj);
        hiddenField.value = totalPoints;

        createCookie("slider", totalPoints, 1);
    }


    function ShowProgress(element,dis1,dis2,dis3,currentHost) {
        var progressElement = document.getElementById(element);
        var disableElem1 = document.getElementById(dis1);
        var disableElem2 = document.getElementById(dis2);
        var disableElem3 = document.getElementById(dis3);
        if (disableElem1 != null) {
            //disableElem1.disabled = "disabled";
            disableElem1.style.visibility = "hidden";
        }
        if (disableElem2 != null) {
            // disableElem2.disabled = "disabled";
            disableElem2.style.visibility = "hidden";
        }
        if (disableElem3 != null) {
            // disableElem3.disabled = "disabled";
            disableElem3.style.visibility = "hidden";
        }
        if (progressElement != null) {
            if (currentHost.indexOf("http") == -1) {
                currentHost = "http://" + currentHost;
            }
          //  alert("value=" + currentHost);
            progressElement.style.visibility = "visible";
            

          /*  var newElem = document.createElement("IMG");
            newElem.id = "loadingimage";
            newElem.name = "loadingimage";
            newElem.width = "40";
            newElem.height = "40";
            newElem.src = currentHost + "/FinnairImages/loading.gif";
            progressElement.appendChild(newElem);
            progressElement.style.visibility = "visible";
            newElem.style.visibility = "visible";
            */
            var loadingImg = document.getElementById("loadingimage");
            if (loadingImg != null) {
                loadingImg.style.visibility = "visible";
                loadingImg.src = currentHost + "/FinnairImages/loading.gif";
            }
            // alert("URL="+ currentHost + "/FinnairImages/loading.gif");
            //progressElement.innerHTML = "<img align='middle' id='loadingimage' name='loadingimage' width='40' height='40' src='" + currentHost + "/FinnairImages/loading.gif'/>";
            //progressElement.childNodes[0].nodeValue = "<img align='middle'  width='40' height='40' src='" + currentHost + "/FinnairImages/loading.gif'/>";
            

        }
        else {
            //alert("test" + element.toString());
        }

    }