﻿
//========================================================
//   standard TRIM() function using Regular Expressions
//========================================================
function trim(str) 
{
    if (str==null) { return ""; }
    if (GetType(str) == "object") { return ""; }
	return str.replace(/^\s*|\s*$/g,"");
}


//========================================================
//   shorthand for document.getElementById with null check
//========================================================
function gE(objId)
{
    if (document.getElementById(objId))
    {
        return document.getElementById(objId);
    }
    return null;
}

//========================================================
//   changes the search state of the top tabs
//========================================================

function SearchState(state)
{
    var L = gE("sListings");
    var C = gE("sCommunity");
    switch(state)
    {
        case "sListings":
            L.className = "active";
            C.className = "";
            break;
        case "sCommunity":
            L.className = "";
            C.className = "active";
            break;
    }
    
}

//========================================================
//   gets a string describing the current search type
//========================================================
function GetSearchType()
{
    var L = gE("sListings");
    var C = gE("sCommunity");
    
    if (C.className == "active")
    {
        return "sCommunity";
    }
    else
    {
        return "sListings";
    }
}

//========================================================
//   performs search through Ajax
//   See "DXAJAXObject.js" for source
//========================================================

function DoSearch()
{
    var theForm = document.frmSearch;
    var txtSearch = trim(theForm.txtSearch.value);
    var SearchType = GetSearchType();
    if (txtSearch == "")
    {
        window.alert("Please enter a value for city, state or zip");
        return;
    }
    
    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch;
    var ResponseDelegate = function(AjaxResponse)
    {
        if (trim(AjaxResponse)=="0")
        {
            ZeroResults();
        }
        else
        {
            switch(SearchType)
            {
                case "sCommunity":
                    window.location.href = WebRoot + "Community.aspx?N=" + AjaxResponse + "&txtSearch=" + txtSearch;
                    break;
                default:
                    window.location.href = WebRoot + "results.aspx?N=" + AjaxResponse + "&txtSearch=" + txtSearch;
                    break;
            }
            
        }
    }
    //-----------------
    $DXAJAX.GetForDelegate(ResponseDelegate, strURL);
    //-----------------
}

//========================================================
//   displays message if no regions are returned from Ajax
//========================================================

function ZeroResults()
{
    var DIV = gE("zero_results");
        DIV.innerHTML = "Your search returned zero results";

        DIV.style.left = RecurseOffset(gE("txtSearch")).offsetLeft + "px";
        DIV.style.top = (RecurseOffset(gE("txtSearch")).offsetTop + RecurseOffset(gE("txtSearch")).offsetHeight) + "px";
        DIV.style.display = "block";
        window.setTimeout(function(){DIV.style.display = "none";},1500);
   
}

//==========================================
//  Captures Enter Key for all pages
//  Performs search if enter key is pressed
//==========================================

var isnav = window.Event ? true : false;
if (isnav) 
{
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} 
else 
{
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) 
{
  	if (e.which == 13) 
  	{ 
  		DoSearch();
		return false;
	} 
	return true;
}

function MicrosoftEventHandler_KeyDown() 
{
  	if (event.keyCode == 13) 
  	{ 
  		DoSearch();
		return false;
	} 
    return true;
}

//========================================================
//  Capture MouseMove event X, Y, stores it in the MousePosition object
//  Mouse position is globally available in the  object
//========================================================


// Global variables
var MousePosition = new Object();
    MousePosition.X = 0;
    MousePosition.Y = 0;

function captureMousePosition(e) 
{
    if (document.layers) 
    {
        MousePosition.X = e.pageX + 25;
        MousePosition.Y = e.pageY - 25;
    } 
    else if (document.all) 
    {
        MousePosition.X = window.event.x + document.body.scrollLeft + 25;
        MousePosition.Y = window.event.y + document.body.scrollTop - 25;
    } 
    else if (document.getElementById) 
    {
        MousePosition.X = e.pageX + 25;
        MousePosition.Y = e.pageY - 25;
    }
}

function CaptureMouseXY() 
{
	// Set Netscape up to run the "captureMousePosition" function whenever
	// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
	// the movement a little easier.
	if (document.layers) { // Netscape -6
	    document.captureEvents(Event.MOUSEMOVE);
	    document.onmousemove = captureMousePosition;
	} else if (document.all) { // Internet Explorer
	    document.onmousemove = captureMousePosition;
	} else if (document.getElementById) { // Mozilla
	    document.onmousemove = captureMousePosition;
	}
}


//========================================================
//  RecurseOffset(object)
//  returns true cross-browser offsetLeft and offsetTop of an object.
//  offsetWidth and offsetHeight are included for 
//  ease of use.
//--------------------------------------------------------
//  ex. var Left = RecurseOffset(obj).offsetLeft;
//========================================================

function RecurseOffset(obj)
{
    if (GetType(obj)=="string")
    {
        if (gE(obj)==null)
        {
            alert("RecurseOffset requires a valid DOM object");
        }
        else
        {
            obj = gE(obj);
        }
    }
   var ROO = new RecurseOffsetObject(obj);
   var Offsets = new Object();
       Offsets.offsetLeft   = ROO.GetOffsetLeft();
       Offsets.offsetTop    = ROO.GetOffsetTop();
       Offsets.offsetWidth  = ROO.GetOffsetWidth();
       Offsets.offsetHeight = ROO.GetOffsetHeight();
   
   return Offsets;
}

//========================================================
//  RecurseOffsetObject(object) used in RecurseOffset(object)
//  This can be called directly
//========================================================
function RecurseOffsetObject(obj)
{
	this.ParentObj = null;
	this.CurrentObj = obj;
    this.offsetLeft = obj.offsetLeft;
    this.offsetTop = obj.offsetTop;
    this.offsetWidth = obj.offsetWidth;
    this.offsetHeight = obj.offsetHeight;
    
	RecurseOffsetObject.prototype.Init = function()
	{
	    if (this.CurrentObj.offsetParent != null)
	    {
	        do
		    {
                this.ParentObj = this.CurrentObj.offsetParent;
                this.offsetLeft += this.ParentObj.offsetLeft;
                this.offsetTop += this.ParentObj.offsetTop;
                this.CurrentObj = this.ParentObj;
		    }
		    while (this.CurrentObj.offsetParent != null);
	    }
	}
	RecurseOffsetObject.prototype.GetOffsetLeft = function(){ return this.offsetLeft; };
	RecurseOffsetObject.prototype.GetOffsetTop = function(){ return this.offsetTop; };
	RecurseOffsetObject.prototype.GetOffsetWidth = function(){ return this.offsetWidth; };
	RecurseOffsetObject.prototype.GetOffsetHeight = function(){ return this.offsetHeight; };
	this.Init();
}


//========================================================
//   String.format gives you C# style string formatting
//========================================================
String.format = function()
{
	if (arguments.length==0) 
	{ 
		throw("String.format requires arguments"); 
	}
	var str = " " + arguments[0];
	for(var i=1;i<arguments.length;i++)
    {
		var re = new RegExp('([^\\{]{1})(\\{' + (i-1) + '\\}(?!\\}))','gm');
        str = str.replace(re,'\$1' + arguments[i]);
    }
	str = str.replace(new RegExp('\\{\\{','gm'),"{");
	str = str.replace(new RegExp('\\}\\}','gm'),"}");
    return str.substring(1);
};

//===============================================
//	System Types are caught with GetType()
//	Or, any custom type built with a constructor
//===============================================

function GetType(Element)
{
	if (Element==null)
	{
		return "null";
	}
	if (Element.constructor == null)
	{
		return "object";
	}
	else
	{
		var Catches = Element.constructor.toString().toLowerCase().match(/([a-z0-9]+)(\(\))/i);
		if (Catches != null)
		{
			return Catches[1];
		}
		else
		{
			return "unknown";
		}
	}
}

// Object to hold Window Boundary values 
var WindowBounds = new Object();
    WindowBounds.Width  = 0;
    WindowBounds.Height = 0;
    WindowBounds.MaxX = 0;
    WindowBounds.MaxY = 0;
    WindowBounds.Top  = 0;
    WindowBounds.Left = 0;
        
// function to calculate window boundary object values
function GetWindowBounds()
{
    
    if (document.documentElement && document.documentElement.scrollTop) //IE6 +
    {
        WindowBounds.Width    = document.documentElement.clientWidth;
        WindowBounds.Height   = document.documentElement.clientHeight;
        WindowBounds.Top      = document.documentElement.scrollTop;
        WindowBounds.Left     = document.documentElement.scrollLeft;
    } 
    else if (document.body && document.body.scrollTop) // < IE6
    {
        WindowBounds.Width    = document.body.clientWidth;
        WindowBounds.Height   = document.body.clientHeight;
        WindowBounds.Top      = document.body.scrollTop;
        WindowBounds.Left     = document.body.scrollLeft;
    } 
    else // Mozilla +
    {
        WindowBounds.Width    = window.innerWidth;
        WindowBounds.Height   = window.innerHeight;
        WindowBounds.Top      = window.pageYOffset;
        WindowBounds.Left     = window.pageXOffset;
    }
    WindowBounds.MaxX     = WindowBounds.Width + WindowBounds.Left;
    WindowBounds.MaxY     = WindowBounds.Height + WindowBounds.Top;
    
    /*
    alert(String.format('\
    WindowBounds.Width: {0}\n\
    WindowBounds.Height: {1}\n\
    WindowBounds.Top: {2}\n\
    WindowBounds.Left: {3}\n\
    WindowBounds.MaxX: {4}\n\
    WindowBounds.MaxY: {5}',
    WindowBounds.Width,
    WindowBounds.Height,
    WindowBounds.Top,
    WindowBounds.Left,
    WindowBounds.MaxX,
    WindowBounds.MaxY));
    */
}

function FormatNumber(NumberString)
{
	NumberString += '';
	Parts = NumberString.split('.');
	Number1 = Parts[0];
	Number2 = Parts.length > 1 ? '.' + Parts[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(Number1)) 
	{
		Number1 = Number1.replace(rgx, '$1' + ',' + '$2');
	}
	return Number1 + Number2;
}

