﻿var AddressSearcher = null;

function PageFieldLoader()
{
    this.Load = function(page)
    {
        for (var field in page)
        {
            var item = null;
            eval('item = page.' + field);
            eval('page.' + field + '=document.getElementById(\'' + item + '\');');
        }
    }
}

function ObjectCreator()
{
    this.Load = function(data)
    {
        return eval('(' + data + ')');
    }
}

function FormatCurrency(num, needcent) 
{
    var result = "";
    num = num.toString().replace(/\$|\,/g,'');
    
    if (isNaN(num))
    {
        num = "0";
    }
    
    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.5);
    var cents = num % 100;
    num = Math.floor(num / 100).toString();
    
    if(cents<10) 
    {
        cents = "0" + cents;
    }
    
    for (i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    {
        num = num.substring(0,num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }

    if (needcent)
    {
        result = ((sign ? '' : '-') + '$' + num + '.' + cents);
    }
    else
    {
        result = ((sign ? '' : '-') + '$' + num);
    }
    
    return result;
}

function NumberSearch(e, url, number)
{
    var keynum = null;
    
    if(e != null && e.which)
    {
        keynum = e.which;
    }
    else if(e != null && window.event)
    {
        keynum = e.keyCode;
    }
    if (keynum != 13 && keynum != 0 && e != null)
    {
        return true;
    }
    
    var field = document.getElementById(number);
    if (field.value.length == 0)
    {
        ShowMessageBox(field, "Please enter mls number value to search.", 'right');
    }
    else
    {
        window.location.href = url + "?mls=" + field.value;
    }

    return false;
}

function TextFieldFocus(field, watermark)
{
    if (field.value == watermark) 
    {
        field.value = "";
    }
}

function TextFieldBlur(field, watermark)
{
    HideAddressListBox(field);
    field.value = (field.value.length == 0) ? watermark : field.value;
}

function GetTextFieldValue(field, watermark)
{
    return (field.value != watermark) ? field.value : "";
}

function GetBrowserName()
{
    var result = "";
    var agent = navigator.userAgent;

    if ((voff = agent.indexOf("MSIE")) != -1)
    {
        result = "MSIE";
    }
    else if ((voff = agent.indexOf("Opera")) != -1) 
    {
        result = "Opera";
    }
    else if ((voff = agent.indexOf("Firefox")) != -1) 
    {
        result = "Firefox";
    }
    else if ((noff = agent.lastIndexOf(' ') + 1) < (voff = agent.lastIndexOf('/')))
    {
        result = agent.substring(noff,voff);
    }

    if (result.toLowerCase() == result.toUpperCase())
    {
        result = navigator.appName;
    }
    
    return result;
}

function ShowMessageBox(field, info, type)
{
    if (field == null) 
    {
        alert("Application Information:\n" + info);
    }
    else
    {
        field.focus();
        var pos = GetPosition(field);

        var box = document.getElementById('FieldMessageBox');
        box.style.display = "block";

        if (type == 'left')
        {
            box.className = "msgbox_left";
            box.style.left = (pos.x + field.offsetWidth / 2 - 10) + "px";
            box.style.top = (pos.y + field.offsetHeight / 2 - box.offsetHeight) + "px";
        }
        else if (type == 'right')
        {
            box.className = "msgbox_right";
            box.style.top = (pos.y + field.offsetHeight / 2 - box.offsetHeight) + "px";
            box.style.left = (pos.x - box.offsetWidth + field.offsetWidth / 2 + 10) + "px";
        }
        else
        {
            box.className = "msgbox_bottom";
            box.style.left = (pos.x + field.offsetWidth / 2 - 10) + "px";
            box.style.top = (pos.y + field.offsetHeight / 2) + "px";            
        }
        
        box.innerHTML = "<div class='msgbox_content'>" + info + "</div>";
        
        setTimeout("HideMessageBox()", 1000);
    }
}

function HideMessageBox()
{
    document.getElementById('FieldMessageBox').style.display = "none";
}

function GetPosition(field) 
{
    var pos = { x: 0, y: 0 };
    
    while (field) 
    {
        pos.x += field.offsetLeft;
        pos.y += field.offsetTop;
        field = field.offsetParent;
    }
    
    return pos;
}

function IsPostalCode(code)
{
    var result = false;
    
    if (code.length == 3 && code.search(/^[a-zA-Z]\d[a-zA-Z]$/) != -1)
    {
        result = true;
    }
    if (code.length == 6 && code.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) 
    {
        result = true;
    }
    else if (code.length == 7 && code.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) 
    {
        result = true;
    }
    
    return result;
}

function FormatPostalCode(code)
{
    var result = code;
    code = code.replace(' ', '');

    if (result.length == 6)
    {
        result = code.substring(0, 3) + " " + code.substring(3, 6);
    }
    
    return result;
}

function SetCookie(name, value)
{
    document.cookie = name + "=" + escape(value);
    var date = new Date();
    date.setMonth(date.getMonth() + 12);
    document.cookie += ("; expires=" + date.toUTCString());
}

function GetCookie(name)
{
    var cookie = document.cookie.split("; ");
    
    for (var i=0; i < cookie.length; i++)
    {
        var items = cookie[i].split("=");
        if (name == items[0]) return unescape(items[1]);
    }
    
    return null;
}

function Dialog()
{
    this.Show = function(title, content, x, y)
    {
        var ctrl = document.getElementById('FieldMessageBox');

        ctrl.style.top = y + 'px';
        ctrl.style.left = x + 'px';
        ctrl.style.display = 'block';
        ctrl.className = 'dialog_body';

        var value = "<div class='dialog_header'><div class='dialog_title'>" + title + "</div>";
        value = value + "<div class='dialog_close' onclick='HideMessageBox()'>";
        ctrl.innerHTML = value + "</div></div><div class='dialog_content'>" + content + "</div>";
    }
}

function AttachAddressFieldEvent(field)
{
    AddressSearcher = field;
    AddressSearcher.InputKey = null;
    AddressSearcher.ResultSet = null;
    AddressSearcher.AreaSelectedIndex = -1;

    if (AddressSearcher.ListField == null) 
    {
        AddressSearcher.ListField = document.createElement('div');
    }
    AddressSearcher.onkeyup = AddressFieldKeyHandler;
}
    
function AddressFieldKeyHandler(e)
{
    var keynum;

    if (e) e = e;
    else e = window.event;
    if(e.which) keynum = e.which;
    else keynum = e.keyCode;

    if (keynum == 38)
    {
        NavigateAddressList(false);
        return true;
    }
    else if (keynum == 40)
    {
        NavigateAddressList(true);
        return true;
    }
    else if (keynum == 13)
    {
        HideAddressListBox(this);
        return false;
    }
    
    if (this.value.length == 1 && this.InputKey != this.value)
    {
        this.InputKey = this.value;
        RealEstateService.LookupAreas(this.value, AddressResultReceived);
    }
    
    if (this.value.length > 0 && this.ResultSet != null) 
    {
        FilterAddressResult(this);
    }
    else 
    {
        HideAddressListBox(this);
    }
    return false;
}

function NavigateAddressList(advance)
{
    var fields = AddressSearcher.ListField.childNodes;
    var len = (fields != null) ? fields.length : 0;

    var index = AddressSearcher.AreaSelectedIndex;
    if (index >= 0 && index < len)
    {
        fields[index].className = "asearcher_item_off";
    }
    
    if (!advance) 
    {
        index = (index <= 0) ? len - 1 : index - 1;
    }
    else
    {
        index = (index >= len - 1) ? 0 : index + 1;
    }
    AddressSearcher.AreaSelectedIndex = -1;
    
    if (index >= 0 && index < len) 
    {
        AddressSearcher.AreaSelectedIndex = index;
        fields[index].className = "asearcher_item_on";
        AddressSearcher.value = fields[index].innerHTML;
    }
}
   
function AddressResultReceived(result)
{
    AddressSearcher.ResultSet = result;
    FilterAddressResult(AddressSearcher);
}

function FilterAddressResult(field)
{
    var total = 0;
    var maximum = 16;
    ClearAddressListBox(field);
    
    var val = field.value;
    var number = (field.ResultSet != null) ? field.ResultSet.length : 0;
    
    for (var i = 0; i < number; i++)
    {
        var name = field.ResultSet[i];
        if (SameAddressStart(val, name))
        {
            AddSearchAddress(field, name, total++);
        }
        if (total >= maximum) break;
    }
    
    if (total > 0) 
    {
        ShowAddressListBox(field);
    }
    else 
    {
        HideAddressListBox(field);
    }
}

function SameAddressStart(left, right)
{
    var result = (left.length <= right.length);
    return result && (left.toUpperCase() == right.substring(0, left.length).toUpperCase());
}

function ClearAddressListBox(field)
{
    AddressSearcher.AreaSelectedIndex = -1;
    if (field.ListField != null) field.ListField.innerHTML = '';
}

function AddSearchAddress(input, address, index)
{
    var field = document.createElement('div');
    
    field.innerHTML = address;
    field.className = "asearcher_item_off";
    field.InputField = input;
    field.FieldIndex = index;
    
    field.onmousedown = function()
    {
        AddressSearcher.value = this.innerHTML;
        HideAddressListBox(this.InputField);
    }
    field.onmouseover = function()
    {
        var fields = AddressSearcher.ListField.childNodes;
        var len = (fields != null) ? fields.length : 0;

        var index = AddressSearcher.AreaSelectedIndex;
        if (index >= 0 && index < len)
        {
            fields[index].className = "asearcher_item_off";
        }
        
        this.className = "asearcher_item_on";
        AddressSearcher.AreaSelectedIndex = this.FieldIndex;
    }
    
    input.ListField.appendChild(field);
}

function ShowAddressListBox(field)
{
    var pos = GetPosition(field);
    field.ListField.style.display = 'block';
    field.ListField.style.left = pos.x + "px";
    field.ListField.className = 'asearcher_main';
    field.ListField.style.width = (field.offsetWidth - 2) + "px";
    field.ListField.style.top = (pos.y + field.offsetHeight + 1) + "px";
    field.offsetParent.appendChild(field.ListField);
}

function HideAddressListBox(field)
{
    ClearAddressListBox(field);
    if (field.ListField != null) field.ListField.style.display = 'none';
}

function DataSet() 
{
    this.Size = 0;
    this.Capacity = 32;
    this.Data = new Array(this.Capacity);

    this.Add = function(item)
    {
        if (this.Size + 1 > this.Capacity)
        {
            this.Capacity += 32;
            var data = new Array(this.Capacity);
            
            for (var i = 0; i < this.Size; i++)
            {
                data[i] = this.Data[i];
            }
        }
        this.Data[this.Size++] = item;
    }
    
    this.Clear = function()
    {
        for (var i = 0; i < this.Size; i++)
        {
            this.Data[i] = null;
        }
        this.Size = 0;
    }
    
    this.GetCount = function()
    {
        return this.Size;
    }
    
    this.Get = function(index)
    {
        return (index >= 0 && index < this.Size) ? this.Data[index] : null;
    }
}

function PlayBannerMovies(imageField, flashField, path)
{
    var max = 6;
    var index = Math.round(Math.random() * max) + 1;
    var file = "'" + path + index + ".swf'";
    
    var banner = document.getElementById(flashField);
    var content = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='MoviePlayer' ";
    content = content + "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' ";
    content = content + "width='550' height='145'>";
    
    content = content + "<param name='movie' value=" + file + "><param name='quality' value='high'>";
    content = content + "<param name='LOOP' value='false'><param name='wmode' value='opaque' />";
    content = content + "<embed src=" + file + " width='550' height='145' loop='false' quality='high' id='MoviePlayer' ";
    content = content + "pluginspage='http://www.macromedia.com/go/getflashplayer' ";
    banner.innerHTML = content + "type='application/x-shockwave-flash' wmode='opaque'></embed></object>";
    
    var image = document.getElementById(imageField);
    image.style.background = "url(" + path + index + ".jpg)";
    
    setTimeout("ShowFlashImageField('" + flashField + "')", 1000);
}

function ShowFlashImageField(field)
{
    var movie = document.embeds['MoviePlayer'];
    if (movie == null)
    {
        movie = document.getElementById('MoviePlayer');
    }
    if (movie.PercentLoaded() != 100 && (movie.Retries == null || movie.Retries < 3))
    {
        setTimeout("ShowFlashImageField('" + field + "')", 1000);
        movie.Retries = ((movie.Retries != null) ? movie.Retries + 1 : 1);
        return;
    }
    document.getElementById(field).style.visibility = 'visible';
}

function ShowPopupWindow(url, w, h)
{
    var winl;
    var wint;
    
    if(screen.width)
    {
        winl = (screen.width - w) / 2;
        wint = (screen.height - h) / 2;
    }
    else
    {
        winl = 0;
        wint = 0;
    }
    
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0';

    var newwin = window.open(url, 'clslink_window', settings);
    void 0;
    newwin.focus();
}
