function searchDB(callingElement) {
    var title = document.getElementsByName('title')[0].value;
    var category = document.getElementsByName('categories')[0].value;
    var keywords = document.getElementsByName('keywords')[0].value;
    var limit = document.getElementsByName('limit')[0].value;
    var strictSearch = document.getElementsByName('strictSearch')[0].checked;
    var offset = 0;

    // Check if pagnation is making the call
    if (callingElement.id.includes("pag")) {
        var multipl = parseInt(callingElement.innerHTML, 10);
        multipl -= 1;

        // Make sure there is an offset value and properly offset it
        if (limit == "") { offset = 50 * multipl - multipl;
        } else { offset = limit * multipl - multipl; }
    }

    document.getElementById("headTitle").innerHTML = "Search DB";
    // Our query being setup for processing
    var formData = "dbQuery=set&title=" + title +
                   "&category=" + category +
                   "&keywords=" + keywords +
                   "&limit=" + limit +
                   "&offset=" + offset +
                   "&strictSearch=" + strictSearch;

    porocess(formData, 0);
}

function saveORdel(element) {
    var checkedState = element.children[0].checked;
    var link = element.children[1].href;
    var thumb = element.children[1].children[0].src;
    var head = document.getElementById("headTitle");

    if (checkedState == true) {
        var formData = "saveFave=set&link=" + link + "&thumbnail=" + thumb;
    } else {
        var formData = "delFave=set&link=" + link + "&thumbnail=" + thumb;
        if (head.innerHTML == "Favorites") {
            element.parentElement.removeChild(element);
        }
    }
    porocess(formData, 1);
}

function viewFave() {
    document.getElementById("headTitle").innerHTML = "Favorites";
    var formData = "viewFave=set";
    porocess(formData, 0);
}

function porocess(formData, postProcess) {
    var xhttp = new XMLHttpRequest();  // Create the xhttp object
    // This is actually run after open and send are done
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            if (postProcess == 0) {
                updatePage(this);  // Send the returned data to further process
            } else if (postProcess == 1) {
            }
        }
    };
    xhttp.open("POST", "process.php", true);  // Open the connection
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(formData);    // Start the process
}

function updatePage(xml) {
    // Get the echoed data and insert to div of dynDataField
    document.getElementById("dynDataField").innerHTML = xml.responseText;
}

function clearDynDiv() {
    document.getElementById("headTitle").innerHTML = "Search DB";
    document.getElementById("dynDataField").innerHTML = "";
    document.getElementById("viewWindow").src = "";
}

function Backgrounds() {
    var bgs = document.getElementById("bkgrnds");
    var btnBg = document.getElementById("backgroundsChng");

    if (bgs.style.display == "none") {
        bgs.style.display = "inline-block";
        btnBg.innerHTML = "Hide Backgrounds";
    } else {
        bgs.style.display = "none";
        btnBg.innerHTML = "UnHide Backgrounds";
    }
}

function bgSetter(check) {
    var path = 'resources/images/backgrounds/'+ check +'.gif';
    var bg = document.getElementById("bg").src = path;
    document.cookie = "background=" + path; "; path=" + document.URL
}

function loadBg() {
    var cookies = document.cookie.split(";");  // Set as an array to traverse....

    if (cookies[0].includes("background=")) {
        document.getElementById("bg").src = cookies[0].replace("background=", "");
    } else {
        var path = "resources/images/backgrounds/2.gif";
        document.getElementById("bg").src = path;
        document.cookie = "background=" + path; "; path=" + document.URL
    }
}


function itterateDown() {
    var initial = document.getElementById('pag1').innerHTML;
    if (parseInt(initial, 10) > 1) {
        for (var i=1; i<6; i++) {
            var nameVal = "pag" + i;
            var working = document.getElementById(nameVal);
            var newValue = parseInt(working.innerHTML, 10) - 1;
            working.innerHTML = newValue;
        }
    }

}

function itterateUp() {
    for (var i=1; i<6; i++) {
        var nameVal = "pag" + i;
        console.log(nameVal);
        var working = document.getElementById(nameVal);
        var newValue = parseInt(working.innerHTML, 10) + 1;
        working.innerHTML = newValue;
    }
}
