﻿var clearDD = '<option value="">Select</option>';

function init() {
    SetSelected('navNetwork');
    //LoadCountries();
    InitSearch();
    $('#networksearch').keypress(function(e) {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;

        if (keycode == 13) {
            DoSearch();
            return false;
        } else return true;
    });
    /*
    $.address.change(function(){
        var id = $.address.value();
        if(id != '/'){
            Grid1.addFilterCriteria('id', OboutGridFilterCriteria.EqualTo, id); 
            Grid1.executeFilter();
        }
    });
    */
}

function ViewDetails(personID, row) {
    $('.networkdetails').html('loading...').parent().hide(0).slideDown('slow');
    Grid1.edit_record(row);
    
    var objP;
    var json = ob_post.post(null, 'LoadDetails', null, { personID: personID });
    //alert(json);
    eval('objP = ' + json);

    var html = '<table class="gabe"><tr>';

    // Photograph
    html += '<td class="col1" rowspan="2">';
    if (objP.photo) {
        html += '<img src="../ImgProxy.aspx?maxh=110&maxw=115&img=uploads/persons/' + objP.photo + '" alt="' + objP.fn + ' ' + objP.ln + '" />';
    } else html += '<img src="../ImgProxy.aspx?img=images/network/default_man.jpg&maxh=110&maxw=115" alt="" />';
    html += '</td>';

    // Address
    html += '<td rowspan="2">';
    html += objP.addr1;
    if (objP.addr2) html += '<br />' + objP.addr2;
    html += '<br />' + objP.city + ', ';
    if (objP.state == 'IT') html += objP.country;
    else html += objP.state;
    html += ' ' + objP.zip;
    if (objP.state != 'IT') html += '<br />' + objP.country;
    html += '<br />' + objP.phone;
    if (objP.displayEmail) html += '<br /><a href="mailto:' + objP.email + '">' + objP.email + '</a>';
    html += '</td>';


    // Nominations
    if (objP.nominations && objP.nominations.length > 0) {
        html += '<td class="pList">';
        html += 'Nominated By:<ul>';
        for (var i = 0; i < objP.nominations.length; i++) {
            var n = objP.nominations[i];
            html += '<li><a class="idFilter" href="Fellows.aspx?id=' + n.nominatorID + '">';
            html += n.salutation + ' ' + n.fn + ' ' + n.ln;
            if (n.suffix) html += ', ' + n.suffix;
            html += '</a></li>';
        }
        html += '</ul>';
        html += '</td>';
    }
    
    if (objP.practiceAreas && objP.practiceAreas.length > 0) {
        html += '</tr><tr><td class="pList">';
        html += 'Practice Areas:<ul>';
        for (var i = 0; i < objP.practiceAreas.length; i++) {
            var a = objP.practiceAreas[i];
            html += '<li>' + a + '</li>';
        }
        html += '</ul>';
        html += '</td>';
    }
    
    html += '</tr></table>';

    $('.networkdetails').html(html);
}

function InitSearch() {
    $('.dropCountry').change(function(e) {
        LoadStates($(this).val());
    });
    $('.dropState').change(function(e) {
        LoadCities($(this).val());
    });
}

/* Drop Down Loading */
function LoadCountries() {
    var json = ob_post.post(null, 'LoadCountries', null);
    var arrCountries, html =  clearDD;
    eval('arrCountries = ' + json);
    for (var i = 0; i < arrCountries.length; i++) {
        var c = arrCountries[i];
        html += '<option ';
        if (c.country == 'United States') html += 'selected ';
        html += 'value="' + c.country + '">' + c.country + '</option>';
    }
    $('.dropCountry').html(html);
    $('.dropStates').html(clearDD);
    $('.dropCity').html(clearDD);

    LoadStates('United States');
}
function LoadStates(country) {
    var json = ob_post.post(null, 'LoadStates', null, { country: country });
    var arrStates, html = clearDD;
    eval('arrStates = ' + json);
    for (var i = 0; i < arrStates.length; i++) {
        var s = arrStates[i];
        html += '<option value="' + s.stateID + '">' + s.state + '</option>';
    }
    $('.dropCity').html(clearDD);
}
function LoadCities(stateID) {
    var html;
    if (!stateID) {
        html = '<option value="">Select State First</option>';
        $('#dropCity').html(html);
        return;
    }

    var json = ob_post.post(null, 'LoadCities', null, { stateID: stateID });
    //alert(json);
    var arrCities, html = clearDD;
    eval('arrCities = ' + json);
    for (var i = 0; i < arrCities.length; i++) {
        var c = arrCities[i];
        html += '<option value="' + c.city + '">' + c.city + '</option>';
    }
    $('#dropCity').html(html);
}
/* End Drop Down Loading */

function DoSearch() {
    var fName = $('#txtName').val();
    var fState = $('.dropState').val();
    var fCity = $('.dropCity').val();
    var fCountry = $('.dropCountry').val();
    
    //$.address.value('');
    //Grid1.removeFilter();
    Grid1.addFilterCriteria('searchField', OboutGridFilterCriteria.Contains, fName); 
    Grid1.addFilterCriteria('country', OboutGridFilterCriteria.EqualTo, fCountry);
    Grid1.addFilterCriteria('state', OboutGridFilterCriteria.EqualTo, fState); 
    Grid1.addFilterCriteria('city', OboutGridFilterCriteria.EqualTo, fCity); 
    Grid1.executeFilter();
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}