/////// Put REO specific in top part

function getDetail(propertyId, btnName, propIdName)
{
    if (!btnName)
    {
        btnName = "btnGetDetail";
    }
    if (!propIdName)
    {
        propIdName = "detailId";
    }
    
    var btn = $(btnName);
    var hdn = $(propIdName);
    
    if (btn && hdn)
    {
        hdn.value = propertyId;
        btn.click();
    }
    
    window.scroll(0, 0);
}

function doStateSearch(stateCode, btnName, propIdName)
{
    if (!btnName)
    {
        btnName = "btnDoStateSearch";
    }
    if (!propIdName)
    {
        propIdName = "stateCode";
    }
    
    var btn = $(btnName);
    var hdn = $(propIdName);
    
    if (btn && hdn)
    {
        hdn.value = stateCode;
        btn.click();
    }
}

function footerStateSearch(stateCode)
{
    var state = document.getElementById("_footer1_stateCode");
    state.value = stateCode;
    var btn = document.getElementById("_footer1_btnDoStateSearch");
    btn.click();
}

/////// Put non-REO specific below here

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
		{
			if (document.getElementsByName(element).length>1)
				return	document.getElementsByName(element);
			element = document.getElementById(element);
		}
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function $V(obj)
{
	var x=$(obj);
	if (x)
	{
		if (x.length>0)
		{
			for(var i=0; i<x.length; i++)
				if (x[i].checked)
				{
					return x[i].value;
				}
		}
		return	x.value;
	}
	else
		return	false;
}

function safeString(s, def)
{
    if (def == null)
        def = '';
        
    if (!s && s != "")
        s = def;
        
    return s;
}
function trim(str)
{
    var trimmed = safeString(str).replace(/^\s+|\s+$/g, '') ;
    return trimmed;
}
function ltrim(str)
{
    var trimmed = safeString(str).replace(/^\s+/g, '') ;
    return trimmed;
}
function rtrim(str)
{
    var trimmed = safeString(str).replace(/\s+$/g, '') ;
    return trimmed;
}

// Client side validation function that sets label to red if value is missing.
// This is dependent on the target label to be named based off the validator
// name. Validator name must start with two-characters before important stuff
// For example, cvFoo could be the validator for some control (tbFoo or tbName, whatever)
// and the target label must be lblFoo.
function checkMissing(source, arguments)
{
    var ctl = $(source.controltovalidate);
    var lbl = $("lbl" + source.id.substring(2));
    
    if (arguments && arguments.IsValid != undefined)
    {
        arguments.IsValid = true;
        
        if (lbl && ctl)
        {
            if (trim($V(ctl)) == "")
            {
                lbl.className = "missing";
                arguments.IsValid = false;
            }
            else
            {
                lbl.className = "";
            }
        }
    }
}

// Client side validation function that sets label to red if value is missing.
// This is dependent on the target label to be named based off the validator
// name. Validator name must start with two-characters before important stuff
// For example, cvFoo could be the validator for some control (tbFoo or tbName, whatever)
// and the target label must be lblFoo.
function checkEmail(source, arguments)
{
    var ctl = $(source.controltovalidate);
    var lbl = $("lbl" + source.id.substring(2));
    
    if (arguments && arguments.IsValid != undefined)
    {
        arguments.IsValid = true;
        
        if (lbl && ctl)
        {
            var val = $V(ctl); 
            if (trim(val) == "")
            {
                lbl.className = "missing";
                arguments.IsValid = false;
            }
            
            else
            {
                if (!isValidEmailAddress(val))
                {
                    lbl.className = "missing";
                    arguments.IsValid = false;
                    alert("Email address is not in the correct format")
                }
                else
                {
                    lbl.className = "";
                }
            }
        }
    }
}

function isValidEmailAddress(val)
{
    var rx = new RegExp("^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(.+))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$");
    
    if (val === null || typeof(val) == "undefined")
    {
        val = "";
    }
    
    return val.match(rx);
}

function Show(el)
{
    try
    {
        if (el && typeof(el) == "string")
        {
            el = $(el);
        }
        
        if (el)
        {
            el.style.display = "block";
        }
    }
    catch (ex)
    {}
}

function Hide(el)
{
    try
    {
        if (el && typeof(el) == "string")
        {
            el = $(el);
        }
        
        if (el)
        {
            el.style.display = "none";
        }
    }
    catch (ex)
    {}
}

function hasClass(ele,cls) {
    if (ele)
    {
    	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
    }
    else
        return false;
}
function addClass(ele,cls) {
	if (ele && cls && !this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (ele && cls && hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

