/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, regexp: true, undef: true, white: true, indent: 4 */
/*global window*/
/*global ActiveXObject*/
/*global CartwrightUtil*/

var CartwrightPortal = {
    widgetdrag: {
        isdragged: 0,   		// Monitors if mouse is being dragged or not
        dragenabled: 0, 		// Turn mouse dragging off by setting this to 0: TODO: Try and get dragging to work!!
        startdragX: 0,
        startdragY: 0,
        initdragX: 0,
        initdragY: 0,
        /* get mouse location */

        startdrag: function (event, a) {
            if (this.dragenabled === 0) {
                return;
            }
            var d = document.getElementById(a);
            d.style.cursor = "move";
            this.isdragged = 1;
            this.startdragX = CartwrightUtil.mousePosition.getMouseX(event);
            this.startdragY = CartwrightUtil.mousePosition.getMouseY(event);
            this.initdragX = CartwrightUtil.elePosition.findPosX(d) - CartwrightUtil.elePosition.findPosX(document.getElementById('container'));
            this.initdragY = CartwrightUtil.elePosition.findPosY(d) - CartwrightUtil.elePosition.findPosY(document.getElementById('container'));
            d.style.position = "absolute";
            d.style.left = this.initdragX + "px";
            d.style.top = this.initdragY + "px";
        },
        enddrag: function (event, a) {
            this.isdragged = 0;
            document.getElementById(a).style.cursor = "auto";
        },
        processdrag: function (event, a) {
            if (this.isdragged === 1 && this.dragenabled === 1) {
                document.getElementById(a).style.left = this.initdragX + (CartwrightUtil.mousePosition.getMouseX(event) - this.startdragX) + "px";
                document.getElementById(a).style.top = this.initdragY + (CartwrightUtil.mousePosition.getMouseY(event) - this.startdragY) + "px";
            }
        }
    },


    mainwidgets: {
        hidecontent: function (a) {
            var d;
            d = document.getElementById(a);
            if (d.style.display === 'none') {
                // move offscreen and record length
                var l = d.style.left;
                d.style.left = "-5000px";  // restore off screeen, record height in f                
                d.style.height = 'auto';
                document.getElementById(a).style.display = 'block';
                var f = d.offsetHeight;
                d.style.height = "1px";
                d.style.left = l; // put back on screen
                this.grow(a, f);  // grow
            } else {
                d.style.height = d.offsetHeight + "px";
                var h = parseInt(d.style.height, 10);
                this.shrink(a, h);
            }
        },
        shrink: function (a, h) {
			var f = h;
            setTimeout(function () {
                if (h > 0) {
                    document.getElementById(a).style.height = h + 'px';
                    h = h - f/10;
                    setTimeout(arguments.callee, 1);
                } else {
                    document.getElementById(a).style.display = 'none';
                    var ele = a.replace("content", "footer");
                    document.getElementById(ele).className = "footerdown";
                }
            }, 1);
        },
        grow: function (a, f) {
            setTimeout(function () {
                var h = parseInt(document.getElementById(a).style.height, 10);
                if (h < f) {
                    h = h + f/10;
                    if (h > f) {
                        h = f;
                    }
                    document.getElementById(a).style.height = h + 'px';
                    setTimeout(arguments.callee, 1);
                } else {
                    var ele = a.replace("content", "footer");
                    document.getElementById(a).style.height = 'auto';
                    document.getElementById(ele).className = "footerup";
                }
            }, 1);
        }
    },
    showhiddenvehicles: function () { // Show/hide hidden vehicles on my page
        document.getElementById("vhidden").style.display = 'block';
        document.getElementById("voha").style.display = 'none';
    },
    showsearch: function () {
        if (document.forms['srch'].st.value !== "") {
            var xmlHttp;
            if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
                alert("Sorry, your browser does not support this feature");
                return false; // If we couldn't create the AJAX return a message to the user
            }
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState === 4) {
                    document.getElementById('srchres').innerHTML = xmlHttp.responseText;
                }
            };
            var getURL = "vehiclesearch.php?st=" + document.forms['srch'].st.value;
            document.getElementById('srchres').innerHTML = "Searching please wait";
            xmlHttp.open("GET", getURL, true);
            xmlHttp.send(null);
        }
    },
    repopdoclst: function (ext) {
        if (!ext) {
            ext = "asp";
        }
        var d = document.getElementById("docvehiclelist");
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
            alert("Sorry, your browser does not support this feature");
            return false; // If we couldn't create the AJAX return a message to the user
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState === 4) {
                var docstring = '<ul class="docvehlist">' + xmlHttp.responseText + '</ul>';
                d.innerHTML = docstring;
            }
        };
        var geturl = 'docvehiclelist.' + ext + '?';
        geturl += 'st=' + document.forms['docsrchform'].st.value;
        d.innerHTML = "Retrieving your vehicles currently on hire, please wait...";
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);
    },
    filldiv: function (div, fleetnumber, rentalstartdate, servicecycle, ext) { /* Ajax code for updaing documents window */
        if (!ext) {
            ext = "asp";
        }
        var d = document.getElementById(div);
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
            alert("Sorry, your browser does not support this feature");
            return false; // If we couldn't create the AJAX return a message to the user
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState === 4) {
                var docstring = 'Currently viewing ' + fleetnumber + '<ul class="doclist">' + xmlHttp.responseText + '</ul>';
                d.innerHTML = docstring;
            }
        };
        var geturl = 'docinc.' + ext + '?';
        geturl += 'fleetnumber=' + fleetnumber;
        geturl += '&rentalstartdate=' + rentalstartdate;
        geturl += '&servicecycle=' + servicecycle;
        geturl += '&handlerfunc=CartwrightPortal.opendocwindow';
        //prompt('',geturl)
        d.innerHTML = "Retrieving documents, please wait...";
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);
    },
    opendocwindow: function (doc, demo) { // Function to display document, or message when in demo mode 
        if (demo === 1) {
            document.getElementById("docview").innerHTML = '<iframe height="100%" width="100%" frameborder="0" src="pdf/sample.pdf"></iframe>';
        } else {
            var dochtml = '<iframe height="100%" width="100%" frameborder="0" src="' + doc.replace("\\", "/") + '"></iframe>'; // TODO: Don't use IFRAME?
            document.getElementById("docview").innerHTML = dochtml;
        }
    },
    changepass: function () {/* Display password dialog */
        if (document.getElementById('cp1').style.display === "none") {
            document.getElementById('cp1').style.display = 'block';
            document.getElementById('cp2').style.display = 'block';

        } else {
            document.getElementById('cp1').style.display = 'none';
            document.getElementById('cp2').style.display = 'none';
        }
    },
    subfeedback: function (ext) {
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
            alert("Sorry, your browser does not support this feature");
            return false; // If we couldn't create the AJAX return a message to the user
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState === 4) {
                document.getElementById("fb1").innerHTML = xmlHttp.responseText;
            }
        };
        var geturl = 'subfeedback.php?c=' + document.forms['feedback'].comments.value;
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);


    },
    custacces: function (a, b, c) {
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
            alert("Sorry, your browser does not support this feature");
            return false; // If we couldn't create the AJAX return a message to the user
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState === 4) {
                document.getElementById("fb1").innerHTML = xmlHttp.responseText;
            }
        };
        var geturl = 'custaccess.php?a=' + a + '&b=' + b + '&c=' + c;
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);
    },
    modaccess: function (a, b, c) {
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists
            alert("Sorry, your browser does not support this feature");
            return false; // If we couldn't create the AJAX return a message to the user
        }
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState === 4) {
                document.getElementById("fb1").innerHTML = xmlHttp.responseText;
            }
        };
        var geturl = 'modaccess.php?a=' + a + '&b=' + b + '&c=' + c;
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);
    },
    checkfrm: function () {
        var f = document.forms['inp'];
        var d = f.requireddated.value;
        if (d === "" || !CartwrightUtil.isNumeric(d, 1) || d > 31 || d === 0) {
            alert("Required date - day is not valid");
            return 1;
        }
        d = f.requireddatem.value;
        if (d === "" || !CartwrightUtil.isNumeric(d, 1) || d > 12 || d === 0) {
            alert("Required date - month is not valid");
            return 1;
        }
        d = f.requireddatey.value;
        if (d !== "08" && d !== "09" && d !== "10") {
            alert("Required date - year is not valid");
            return 1;
        }
        d = f.numberoftrailers.value;
        if (d === "" || !CartwrightUtil.isNumeric(d, 1)) {
            alert("Please enter valid number of trailers");
            return 1;
        }
        d = f.height.value;
        if (!CartwrightUtil.isNumeric(d)) {
            alert("Please enter valid height in metres");
            return 1;
        }
        f.submit();
    }
};
