﻿/* Dogs and Puppies For Sale scripting
--------------------------  */
var boto = null;
function togglebreedoptions(ttype) {
    if (ttype == "on") {
        if (boto != null) {
            retainbreedoptions();
        }
        else {
            document.getElementById("breedoptions").className = "";
        }
    }
    else {
        boto = window.setTimeout('document.getElementById("breedoptions").className = "hidden";boto = null;', 500);
    }
}

function hidebreedoptions() {
    window.clearTimeout(boto);
    boto = null;
}

function retainbreedoptions() {
    window.clearTimeout(boto);
    boto = null;
}

function checkfocus(e) {
    alert(window.event.srcElement.tagName);
}

var breedstring = "";

function togglebreedgroup(o, bgid) {
    var cbxarr
    if (document.getElementById("breedgroup" + bgid)) {
        cbxarr = document.getElementById("breedgroup" + bgid).getElementsByTagName("INPUT");
        var i;
        for (i = 0; i < cbxarr.length; i++) {
            if (cbxarr[i].checked != o.checked) {
                cbxarr[i].checked = o.checked;
                cbxarr[i].onchange();
            }
        }
    }
    else {
    // If no breeds then set text to group name
        if (o.checked) {
            if (breedstring == "") {
                breedstring = o.parentNode.lastChild.nodeValue.trim();
            }
            else {
                breedstring += ", " + o.parentNode.lastChild.nodeValue.trim();
            }
        }
        else if (breedstring.indexOf(", " + o.parentNode.lastChild.nodeValue.trim()) > -1) {
            breedstring = breedstring.replace(", " + o.parentNode.lastChild.nodeValue.trim(), "");
        }
        else if (breedstring.indexOf(o.parentNode.lastChild.nodeValue.trim() + ", ") > -1) {
            breedstring = breedstring.replace(o.parentNode.lastChild.nodeValue.trim() + ", ", "");
        }
        else if (breedstring.indexOf(o.parentNode.lastChild.nodeValue.trim()) > -1) {
            breedstring = breedstring.replace(o.parentNode.lastChild.nodeValue.trim(), "");
        }
        if (breedstring.length >= 40) {
            document.getElementById("breeds").value = breedstring.substring(0, 40) + "...";
        }
        else {
            document.getElementById("breeds").value = breedstring;
        }
    }
}

function togglebreed(o) {
    if (o.checked) {
        if (breedstring == "") {
            breedstring = o.parentNode.lastChild.nodeValue;
        }
        else {
            breedstring += ", " + o.parentNode.lastChild.nodeValue;
        }
    }
    else if (breedstring.indexOf(", " + o.parentNode.lastChild.nodeValue) > -1) {
        breedstring = breedstring.replace(", " + o.parentNode.lastChild.nodeValue, "");
    }
    else if (breedstring.indexOf(o.parentNode.lastChild.nodeValue + ", ") > -1) {
        breedstring = breedstring.replace(o.parentNode.lastChild.nodeValue + ", ", "");
    }
    else if (breedstring.indexOf(o.parentNode.lastChild.nodeValue) > -1) {
        breedstring = breedstring.replace(o.parentNode.lastChild.nodeValue, "");
    }
    if (breedstring.length >= 40) {
        document.getElementById("breeds").value = breedstring.substring(0, 40) + "...";
    }
    else {
        document.getElementById("breeds").value = breedstring;
    }
}
var dspage;
function searchdogspuppies(page, nitems) {
    // Disable the form and show loading animation
    disableform(document.getElementById("searchforsale"));
    document.getElementById("bsajaxloader").className = "";
    // Build and array of breed values
    var barray = new Array;
    var cbxarr = document.getElementById("breedoptions").getElementsByTagName("INPUT");
    var i;
    for (i = 0; i < cbxarr.length; i++) {
        if (cbxarr[i].checked && cbxarr[i].name != 'breedgroups') {
            barray[barray.length] = cbxarr[i].value;
        }
        else if (cbxarr[i].checked && cbxarr[i].name == 'breedgroups') {
            barray[barray.length] = 'g' + cbxarr[i].value;
        }
    }
    // Greyout
    greyOutList(true, "dogpuppyresults")
    // Call ajax function
    dspage = page
    TheSniffa.Search_Dogs_Sale(barray, "searchdogs", page, nitems, searchdogspuppiescallback);
}

function searchdogspuppiescallback (response)
{
    // Check for errors
    if(response.error != null) {
    	alert("An error occurred and your results could not be displayed.");
        // Enable button and hide animation
        enableform(document.getElementById("searchforsale"));
        document.getElementById("bsajaxloader").className = "hidden";
        greyOutList(false, "dogpuppyresults")
		return;
	}

    // Get the response value
    document.getElementById("dogpuppyresults").innerHTML = response.value;
    // Enable button and hide animation
    enableform(document.getElementById("searchforsale"));
    document.getElementById("bsajaxloader").className = "hidden";
    greyOutList(false, "dogpuppyresults")
    // Analytics
    if (window.pageTracker != undefined) {
        pageTracker._trackPageview("Dogs and Puppies For Sale Search Page " + dspage); 
    }
}

/*
    Paging
*/
var ldpage;
function pagelatestdogs(page, nitems) {
    // Greyout
    greyOutList(true, "dogresults")
    // Call ajax function
    ldpage = page
    TheSniffa.Search_Dogs_Sale(new Array, "latestdogs", page, nitems, pagelatestdogscallback);
}

function pagelatestdogscallback (response)
{
    // Check for errors
    if(response.error != null) {
    	alert("An error occurred and your results could not be displayed.");
        // Enable button and hide animation
        greyOutList(false, "dogresults")
		return;
	}
    // Get the response value
    document.getElementById("dogresults").innerHTML = response.value;
    // Enable button and hide animation
    greyOutList(false, "dogresults");
    // Analytics
    if (window.pageTracker != undefined) {
        pageTracker._trackPageview("Latest Dogs For Sale Page " + ldpage); 
    }
}
var lppage;
function pagelatestpuppies(page, nitems) {
    // Greyout
    greyOutList(true, "puppyresults");
    // Call ajax function
    lppage = page
    TheSniffa.Search_Dogs_Sale(new Array, "latestpuppies", page, nitems, pagelatestpuppiescallback);
}

function pagelatestpuppiescallback (response)
{
    // Check for errors
    if(response.error != null) {
    	alert("An error occurred and your results could not be displayed.");
        // Enable button and hide animation
        greyOutList(false, "puppyresults");
		return;
	}
    // Get the response value
    document.getElementById("puppyresults").innerHTML = response.value;
    // Enable button and hide animation
    greyOutList(false, "puppyresults");
    // Analytics
    if (window.pageTracker != undefined) {
        pageTracker._trackPageview("Latest Puppies For Sale Page " + lppage); 
    }
}

