﻿
$(document).ready(function() {
    var pageTracker = _gat._getTracker("UA-182004-4");
    pageTracker._initData();
    var pagesize = 10;

    //$('#searchType').val('ft');
    //callAjaxGrid('ft', '', '', 1, pagesize, '', '', '', '');

    //    $('#paging').css('display', 'block');
    //    $('#cboAgentNo').css('display', 'none');
    //    $('#size').css('display', 'block');
    var ty = getQueryVariable('ty');
    var rmp = getQueryVariable('rmp');

    if (ty == "tm" || ty == "tmv") {
        $('#paging').css('display', 'none');
        $('#cboAgentNo').css('display', 'block');
        $('#divAgentNo').css('display', 'block');
        $('#size').css('display', 'none');

        $('#cboAgentNo').val(rmp)
    }
    else {
        $('#paging').css('display', 'block');
        $('#cboAgentNo').css('display', 'none');
        $('#divAgentNo').css('display', 'none');
        $('#size').css('display', 'block');
    }


    var st = getQueryVariable('st');
    var st1 = getQueryVariable('st1');
    var ct = getQueryVariable('ct');
    var ct1 = getQueryVariable('ct1');
    var ak = getQueryVariable('ak');
    var rk = getQueryVariable('rk');

    if (st != "") {
        $('#searchBoxState').val(st);
        callAjaxLoadCity($('#searchBoxState').val(), ct);
    }

    if (st1 != "") {
        $('#searchBoxState1').val(st1);
        callAjaxLoadCity1($('#searchBoxState1').val(), ct1);
    }

    if (ct != "") {
        $('#searchBoxCity').val(ct);
    }

    if (ct1 != "") {
        $('#searchBoxCity1').val(ct1);
    }

//    if (ak != "") {
//        if (ty != "al") {
//            $('#agentSearchTxt').val(URLDecode(ak));
//        }
//    }

//    if (rk != "") {
//        if (ty != "rl") {
//            $('#realtorSearchTxt').val(URLDecode(rk));
//        }
//    }

    $('#mostListing').click(function() {
        window.location = '/property/findanagent.aspx?ty=tm&ak=&rk=&pg=1&rmp=10&st=&ct=&st1=&ct1=';
    });

    $('#mostViewed').click(function() {
        window.location = '/property/findanagent.aspx?ty=tmv&ak=&rk=&pg=1&rmp=10&st=&ct=&st1=&ct1=';
    });

    $('#agentSubmit').click(function() {
        $('#agentSearch').val($('#agentSearchTxt').val());
        //callAjaxGrid('as', $('#agentSearch').val(), $('#realtorSearch').val(), 1, pagesize, $('#searchBoxState').val(), $('#searchBoxCity').val(), '', '');
        window.location = '/property/findanagent.aspx?ty=as&ak=' + URLEncode($('#agentSearch').val()) + '&rk=&pg=1&rmp=10&st=' + $('#searchBoxState').val() + '&ct=' + $('#searchBoxCity').val() + '&st1=&ct1=';
    });

    $('#showAll').click(function() {
        $('#searchType').val('as');
        //callAjaxGrid('as', '', '', 1, pagesize, '', '', '', '');
        window.location = '/property/findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=10&st=&ct=&st1=&ct1=';
    });

    $('#realtorSubmit').click(function() {
        $('#realtorSearch').val($('#realtorSearchTxt').val());

        //callAjaxGrid('rs', $('#agentSearch').val(), $('#realtorSearch').val(), 1, pagesize, '', '', $('#searchBoxState1').val(), $('#searchBoxCity1').val());
        window.location = '/property/findanagent.aspx?ty=rs&ak=&rk=' + URLEncode($('#realtorSearch').val()) + '&pg=1&rmp=10&st=&ct=&st1=' + $('#searchBoxState1').val() + '&ct1=' + $('#searchBoxCity1').val();
    });

    $('#cboAgentNo').change(function() {
        $('#agentNo').val($('#cboAgentNo').val());
        //callAjaxGrid($('#searchType').val(), '', '', 1, $('#agentNo').val(), '', '', '', '');
        window.location = '/property/findanagent.aspx?ty=' + ty + '&ak=&rk=&pg=1&rmp=' + $('#agentNo').val() + '&st=&ct=&st1=&ct1=';
    });

    $('#searchBoxState').change(function() {
        //if ($('input[@name*=searchBoxOptSearchType]:checked').val() != 'N')
        callAjaxLoadCity($('#searchBoxState').val(), '');
    });

    $('#searchBoxState1').change(function() {
        //if ($('input[@name*=searchBoxOptSearchType]:checked').val() != 'N')
        callAjaxLoadCity1($('#searchBoxState1').val(), '');
    });

    for (var i = 65; i <= 90; i++) {
        getAlphaList(String.fromCharCode(i), 'al', pagesize);
        getAlphaList(String.fromCharCode(i), 'rl', pagesize);
    };
});

//Get the alpha list from A-Z
function getAlphaList(fstChar, type, pagesize){
  if(type == 'rl')
      $('<div class="alphaList"></div>').html(fstChar).click(function() {
      $('#searchType').val('rl');
      $('#agentSearch').val(fstChar);
      $('#realtorSearch').val(fstChar);
      //callAjaxGrid(type, fstChar, fstChar, 1, pagesize, '', '', '', '');
      window.location = '/property/findanagent.aspx?ty=rl&ak=&rk=' + fstChar + '&pg=1&rmp=10&st=&ct=&st1=&ct1='; 
    }).appendTo('#realtorAlphaList');
  else
      $('<div class="alphaList"></div>').html(fstChar).click(function() {
      $('#searchType').val('al');
      $('#agentSearch').val(fstChar);
      $('#realtorSearch').val(fstChar);
      //callAjaxGrid(type, fstChar, fstChar, 1, pagesize, '', '', '', '');
      window.location = '/property/findanagent.aspx?ty=al&ak=' + fstChar + '&rk=&pg=1&rmp=10&st=&ct=&st1=&ct1='; 
    }).appendTo('#agentAlphaList');  
}

function getQueryVariable(variable) {

    var query = window.location.search.substring(1);
    
    if (query != "") {
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        } 
    }
    else {
        return "";
    }
}


function callAjaxLoadCity(st, ct) {
    //$('#result').html('<div class="loader" title="Loading....."></div>');
    $.ajax({
        url: '/ajax_server/property/svr_searchboxtab_city.aspx',
        data:
        'st=' + st,
        type: 'GET',
        datatype: 'application/xml',
        timeout: 100000,
        cache: false,
        error: function(err) {
            //$('#result').empty();
            //alert('There is something wrong with the server, or your session is expired, please relogin or try again...','Error');
        },
        success: function(xml) {
            document.getElementById('searchBoxCity').length = 0;
            AddItem('All Areas', '');
            $(xml).find('record').each(function() {
                var city = $(this).text();
                AddItem(city, city, ct);
            });
        }
    });
}

function callAjaxLoadCity1(st, ct1) {
    //$('#result').html('<div class="loader" title="Loading....."></div>');
    $.ajax({
        url: '/ajax_server/property/svr_searchboxtab_city.aspx',
        data:
        'st=' + st,
        type: 'GET',
        datatype: 'application/xml',
        timeout: 100000,
        cache: false,
        error: function(err) {
            //$('#result').empty();
            //alert('There is something wrong with the server, or your session is expired, please relogin or try again...','Error');
        },
        success: function(xml) {
            document.getElementById('searchBoxCity1').length = 0;
            AddItem1('All Areas', '');
            $(xml).find('record').each(function() {
                var city = $(this).text();
                AddItem1(city, city, ct1);
            });
        }
    });
}

function AddItem(Text, Value, ct) {
    // Create an Option object
    var opt = document.createElement("option");

    // Add an Option object to Drop Down/List Box
    document.getElementById('searchBoxCity').options.add(opt);
    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;

    if (ct == Value) {
        opt.selected = true;
    }
}

function AddItem1(Text, Value, ct1) {
    // Create an Option object        
    var opt = document.createElement("option");

    // Add an Option object to Drop Down/List Box
    document.getElementById('searchBoxCity1').options.add(opt);
    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;

    if (ct1 == Value) {
        opt.selected = true;
    }
}

function setImageSize(myImage, obj, size) {
    var img1 = new Image()
    var val = myImage
    img1.src = val
    var iwidth = img1.width
    var iheight = img1.height
    var z

    if (iwidth > iheight) {
        if (iwidth > size) {
            z = iwidth / size;
            iwidth = size;
            iheight = iheight / z;
        }
    } else {
        if (iheight > size) {
            z = iheight / size;
            iheight = size;
            iwidth = iwidth / z;
        }
    }
    obj.height = iheight;
    obj.width = iwidth;
}
