// OK so this function doesn't fit in a js file named sideSearch.
// we do need to move this and all 4 main js files into a single bit js file.
function loadRecentPets(containerDivId, h2text, petUrlBase, photoUrlBase) {
	
	// get & parse the recent pets cookie info (set in the pet notes cgi)
	var recentPetString = getCookie('pf_recent_pets');
	if(!recentPetString) return 0;
	var recentPetArray  = recentPetString.split('|');
	
	// only create the recent pets content box if there are pets to put inside it.
	if (recentPetArray.length > 0) {
    	var containerDiv = document.getElementById(containerDivId);
		
		// create the recent pets content box
		var innerDiv = document.createElement('div');
		innerDiv.setAttribute('id','recentpets');
		innerDiv.className = 'content_box';
		
		// create the recent pets h2 & br tag
        var h2 = document.createElement('h2');
		h2.innerHTML=h2text;
		innerDiv.appendChild(h2);
		innerDiv.appendChild(document.createElement('br'));
		
		// go through the recent pets and add each one to the content box
    	for (recentPet in recentPetArray) {
        	var pet     = recentPetArray[recentPet].split(',');
	    	var id      = pet[0];
		    var name    = pet[1];
			var shelter = pet[2];
			
			// create a div for the pet
 		    var petDiv  = document.createElement('div');
            petDiv.className = 'recentpet';
			
			// make a linked image of the pet & add it to the pet div
			var petImage  = document.createElement('img');
			var petImageA = document.createElement('a');
			var petA      = document.createElement('a');
			petImage.setAttribute('src', photoUrlBase + shelter + '/' + shelter + '.' + id + '-1-fpm.jpg');
			petImage.setAttribute('alt', name);
            petImageA.setAttribute('href', petUrlBase + id);
            petImageA.appendChild(petImage);
			petDiv.appendChild(petImageA);
			petDiv.appendChild(document.createElement('br'));

            // make a linked pet name & add it to the pet div
			var petP = document.createElement('p');
            petA.setAttribute('href', petUrlBase + id);
			petA.innerHTML=name;
			petP.appendChild(petA);
			petDiv.appendChild(petP);
            
			// add the pet div to the recent pets content box
			innerDiv.appendChild(petDiv);
			
			// make a new line every 4 pets (pets start at 0)
			if ( ((recentPet + 1) % 4) == 0) {
				var rEnd = document.createElement('div');
				rEnd.className = 'recentpetsend';
				rEnd.innerHTML = ' ';
				innerDiv.appendChild(rEnd);
			}
    	}
		
		// put the recent pets content box in the DOM, inside the container div
		containerDiv.appendChild(innerDiv);
	}
}


function showBreedList(breedUrl) {
    var search = document.getElementById("search");

    if(search.elements[1].selectedIndex > 0) {
        setCookie("pet.Animal",search.elements[1].selectedIndex,exp);
        if(search.elements[1].selectedIndex == 9) {
            breedUrl += 'smallFurry';
        }
        else {
            breedUrl += search.elements[1].value;
        }
    }
    document.location = breedUrl;
}

function clearSearchLocation() {
    var search = document.getElementById("search");

    if(search.location.value == "Zip or City, State/Prov") {
        search.location.value = "";
    }
}

function checkInfo() {
    for (i=0;i<0;i++) {
        if(document.forms[2].elements[i].value=="") {
            alert('Required fields on this Form must be completed!');
            document.forms[2].elements[i].focus();
            return false;
        }
    }
    submitInfo();
    return true;
}

function getSelectValue (element) {
    var value = getCookie(element.name);
    if (value != null && value != "") element.selectedIndex = value;
}

function getCheckValue (element) {
    var value=getCookie(element.name);
    if (value>0) {
        element.checked=true;
    }
    else {
        element.checked=false;
    }
}

function setSearchForm() {
    var search = document.getElementById("search");
    var locValue = getCookie('location');

    var animals = document.getElementById("qs-animal");
    var breed = document.getElementById("qs-breed");
    var size = document.getElementById("qs-size");
    var age = document.getElementById("qs-age");
    var gender = document.getElementById("qs-gender");

    if(locValue == null) {
        search.location.value = "Zip or City, State/Prov";
    }
    else {
        search.location.value = locValue;
        if ( document.searchad ) document.searchad.location.value = locValue;
    }
    getSelectValue(animals);
    getValue(breed);
    getSelectValue(size);
    getSelectValue(age);
    getSelectValue(gender);
}

function submitInfo() {
		document.forms[2].submit();
		return true;
}

function checkSearch() {
    var animals = document.getElementById("qs-animal");
    var breed = document.getElementById("qs-breed");
    var size = document.getElementById("qs-size");
    var age = document.getElementById("qs-age");
    var gender = document.getElementById("qs-gender");
    if (animals.selectedIndex == 0 && (breed.value == "" || breed.value == null)) {

        alert("Please select an animal and/or a breed to search!");
        animals.focus();
        return false;
    }
    // SET COOKIES!!!!

    setCookie("pet.Animal",animals.selectedIndex,exp);
    setCookie("pet.Breed",breed.value,exp);
    setCookie("pet.Age",age.selectedIndex,exp);
    setCookie("pet.Size",size.selectedIndex,exp);
    setCookie("pet.Sex",gender.selectedIndex,exp);

    return true;
}

function getValue (element) {
    var value=getCookie(element.name);
    if (value != null) element.value=value;
}

function setValue(element) {
		setCookie(element.name,element.value, exp);
}

// The following is a hack to allow us to call setSearchForm() on
// page load (more or less) without having to resort to window.onload
if ( document.addEventListener ) {
    document.addEventListener( "DOMContentLoaded", setSearchForm, false );
}
else if ( document.all && ! window.opera ) {
    document.write(
        '<script type="text/javascript" id="setSearchForm" ' +
        'defer="defer" src="javascript:void(0)"><\/script>'
    );
    var contentLoader = document.getElementById("setSearchForm");
    contentLoader.onreadystatechange = function () {
        if ( this.readyState == "complete" ) setSearchForm();
    }
}
