
var ajaxObj;
var last_englarged  = null;

function performCLDirectorySearchByAddressId( listing_id ) {
    url = '/events/admin/cld_search.php?listing_id='+encodeURI(listing_id);
    return doSearch( url );
}

function performCLDirectorySearch(form,selectedVal) {
    var what    = document.getElementById('bd_what').value;
    var where   = document.getElementById('bd_where').value;

    if( where == '' ) {
        alert('You must specify a location for your directory search. The location can be a city and state/provice.');
        return false;
    }
    
    url = '/events/admin/cld_search.php?query='+encodeURI(what)+'&location='+encodeURI(where);

    return doSearch( url );
}

function doSearch( url ) {
    try {
        // Firefox, Opera 8.0+, Safari
        ajaxObj = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
        ajaxObj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                ajaxObj = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                alert('Your browser does not support AJAX!');
                return false;
            }
        }
    }

    ajaxObj.onreadystatechange  = function () { 
		if( ajaxObj.readyState != 4 ) {
            return false;
        }

		renderCLDirectoryResults();

        if( 'undefined' != typeof( selectedVal ) && selectedVal > 0  ) {
        	selectCLDRadio( 'listing_id_' + selectedVal );
        }
    }

    var area    = document.getElementById('ajaxArea');
	area.innerHTML = 'Searching... Please stand by for a moment!';   

    ajaxObj.open('GET',url,true);
    ajaxObj.send(null);
    
    return false;
}

function renderCLDirectoryResults() {   
    var results     = ajaxObj.responseText.split("\n");
    var area        = document.getElementById('ajaxArea');
    var count       = results.length - 1;

    area.innerHTML  = '';
    row = results[0];
    if( "***Valid Results Found" != row ) {
    	count = 0;
	}
    
    for( i=1; i < count && i < 20; i++ ) {
        row = results[i].split("||");
        area.innerHTML  += '<div class="autoFormFloat"><label></label><span class="autoFormInputs"><input onChange="CLDSelected(\'listing_id_'+row[0]+'\',\''+row[1]+'\')" name="event_venue_id" id="listing_id_'+row[0]+'"  type="radio" value="'+row[0]+'"><a href="javascript:selectCLDRadio(\'listing_id_'+row[0]+'\', \''+row[1]+'\');" id="row_listing_id_'+row[0]+'">'+row[1]+'</a></span></div>';
    }
    
    if( count < 1 ) {
        area.innerHTML  = '<em>Sorry, no results were found for your search query.</em>';
    } else if( 21 == count ) {
    	area.innerHTML = '<em>More than 20 results found. Please refine your search.</em>';
    }
}

function selectCLDRadio(id,label) {
    var elem    = document.getElementById(id);
    
    if( elem ) {
        elem.checked    = 'checked';
    }
    CLDSelected( id, label );
}

function CLDSelected(id, label ) {
	$('#bd_what').attr( 'value', $( '#row_' + id + ' b' ).text() ); 
}

function onCampaignChange() {
    var elem            = document.getElementById('campaign_id');
    var div             = document.getElementById('campaign_form');
    div.style.display   = ( elem.value > 0 ) ? 'none':'';
}

function zoomAdImage( elem ) {
    // are we enalrging or minimizing?
    if( elem.style.maxWidth != '' ) {
        // shut down the previously enlarged picture first
        if( last_englarged != null && 
            last_englarged.src != elem.src &&
            last_englarged.style.maxWidth == '' ) {
            zoomPhoto(last_englarged);
        }
        elem.style.position     = 'absolute';
        elem.style.marginTop    = '-20px';
        elem.style.marginLeft   = '-125px';
        elem.style.maxWidth     = '';
        elem.style.maxHeight    = '';
    } else {
        elem.style.position     = '';
        elem.style.marginTop    = '';
        elem.style.marginLeft   = '';
        elem.style.maxWidth     = '150px';
        elem.style.maxHeight    = '50px';
    }
    last_englarged = elem;
    
    return false;
}