require("luamods/layout")
require("luamods/cgi")
require("luamods/command")
require("config/software_conf")

if (pkgName == nil) then pkgName = "" end

header = layout.getHeader()
footer = layout.getFooter()

content = [[
                    <script>

                        var xmlhttp;
                        var clicked = 0;
                        var xmlHttpTimeout;

                        function loadXMLDoc(url,cfunc)
                        {
                            if (window.XMLHttpRequest)
                            {// code for IE7+, Firefox, Chrome, Opera, Safari
                                xmlhttp=new XMLHttpRequest();
                            }
                            else
                            {// code for IE6, IE5
                                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            xmlhttp.onreadystatechange=cfunc;
                            xmlhttp.open("POST",url,true);
                            xmlhttp.send();
                            // Timeout to abort in 3 seconds
                            xmlHttpTimeout=setTimeout(ajaxTimeout, 15000);
                            document.getElementById("myDiv").innerHTML = "<br><b>Searching...</b> <img src=\"images/spinner.gif\" width=20 height=20 />";
                        }

                        function ajaxTimeout()
                        {
                            xmlhttp.abort();
                            document.getElementById("myDiv").innerHTML = "<br><b>Request timed out...</b>";
                            clicked = 0;
                        }

                        function myFunction()
                        {
                            if (clicked == 1)
                            {
                                return;
                            }
                            else
                            {
                                clicked = 1;
                            }
                            loadXMLDoc("software_search_ajax.lua?pkgName=" + document.getElementById('pkgName').value, function()
                            {
                                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                {
                                    clearInterval(xmlHttpTimeout);
                                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                                    clicked = 0;
                                }
                                if (xmlhttp.readyState==4 && xmlhttp.status==404)
                                {
                                    clearInterval(xmlHttpTimeout);
                                    document.getElementById("myDiv").innerHTML="<BR><B>Error 404</B>";
                                    clicked = 0;
                                }

                            });
                        }

                    </script>

                    <div id="info">
                        <div style="background:url(images/software.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>Software - Search Packages</h1></center>
                        </div>
                    </div>

                    <div class="formLayoutfull">
                        <center>
                            <b>Enter the package name to search for.</b><br><br>
                            <img src="images/magnify.png"/> <input type="text" id="pkgName" name="pkgName" value="]]..pkgName..[[" size="40"/><input type="button" name="searchButton" value="Search" onclick="myFunction();"/>
                        </center>
                    </div>
                    
                    <DIV id="myDiv">
                    </DIV>    
]]

print(header .. content .. footer)
