//Home Finder Global Functions
// Check Browsers
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = (userAgent.indexOf('opera') != -1);
var is_saf = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv = (userAgent.indexOf('webtv') != -1);
var is_ie = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4 = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon = (userAgent.indexOf('konqueror') != -1);
var is_ns = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4 = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_regexp = (window.RegExp) ? true : false;
// DOM functions
var DOMtype = '';
if (document.getElementById) {
    DOMtype = "std";
}
else if (document.all) {
    DOMtype = "ie4";
}
else if (document.layers) {
    DOMtype = "ns4";
}

// make an array to store cached locations of objects called by fetch_object
var Myobjects = new Array();

// function to emulate document.getElementById
function fetch_object(idname, forcefetch) {
    if (forcefetch || typeof (Myobjects[idname]) == "undefined") {
        switch (DOMtype) {
            case "std":
                {
                    Myobjects[idname] = document.getElementById(idname);
                }
                break;

            case "ie4":
                {
                    Myobjects[idname] = document.all[idname];
                }
                break;

            case "ns4":
                {
                    Myobjects[idname] = document.layers[idname];
                }
                break;
        }
    }
    return Myobjects[idname];
}

// function to set a cookie
function set_cookie(name, value, expires) {
    var argv = set_cookie.arguments;
    var argc = set_cookie.arguments.length;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    else {
        expires = 1 * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + "=" + escape(value)
		+ "; expires=" + expires_date.toGMTString()
		+ "; path=" + ((path == null) ? "/" : path)
		+ ((domain == null) ? "" : ("; domain=" + domain))
		+ ((secure == true) ? "; secure" : "");
}

function fetch_cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}			

// function to delete a cookie
function delete_cookie(name) {
    var expireNow = new Date();
    document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}

function setSelectOptions(the_form, the_select, do_check) {
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
}

function setCookie(NameOfCookie, value, expiredays) {

    // Three variables are used to set the new cookie. 
    // The name of the cookie, the value to be stored,
    // and finally the number of days until the cookie expires.
    // The first lines in the function convert 
    // the number of days to a valid date.

    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

    // The next line stores the cookie, simply by assigning 
    // the values to the "document.cookie" object.
    // Note the date is converted to Greenwich Mean time using
    // the "toGMTstring()" function.

    document.cookie = NameOfCookie + "=" + escape(value) +
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function getCookie(NameOfCookie) {

    // First we check to see if there is a cookie stored.
    // Otherwise the length of document.cookie would be zero.

    if (document.cookie.length > 0) {

        // Second we check to see if the cookie's name is stored in the
        // "document.cookie" object for the page.

        // Since more than one cookie can be set on a
        // single page it is possible that our cookie
        // is not present, even though the "document.cookie" object
        // is not just an empty text.
        // If our cookie name is not present the value -1 is stored
        // in the variable called "begin".

        begin = document.cookie.indexOf(NameOfCookie + "=");
        if (begin != -1) // Note: != means "is not equal to"
        {

            // Our cookie was set. 
            // The value stored in the cookie is returned from the function.

            begin += NameOfCookie.length + 1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;

    // Our cookie was not set. 
    // The value "null" is returned from the function.

}

function delCookie(NameOfCookie) {

    // The function simply checks to see if the cookie is set.
    // If so, the expiration date is set to Jan. 1st 1970.

    var d = new Date();
    if (getCookie(NameOfCookie)) {
        document.cookie = NameOfCookie + "=" +
	"; expires=" + d.toLocaleDateString();
    }
} 
