function DoPostback(_url, _data, _successFunc, _errorFunc, _completeFunc) {
    $.ajax({
        type: "POST",
        url: _url,
        data: _data,
        //contentType: "application/json; charset=utf-8",
        //dataType: "json",
        error: _errorFunc,
        success: _successFunc,
        complete: _completeFunc
    });
    return false;
}

function PostbackSuccess(data, textStatus, XMLHttpRequest) {
    //console.log(data);

    //$('#showData').html(data);
    //alert("Postback Sucess");
	//return false;
}

function PostbackSuccess_ShowBrew(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);

    $('#newsShowBrew').empty(); 
     
    for (itemID in _data) {
        var item = _data[itemID];
        // now item should be the array you're expecting, so...
        if (item.brewery!= null){
            $('#newsShowBrew').show();
            $('#newsShowBrew').append('<a href="#" id="user_' + item.brewery_id + '" >' + item.brewery + "</a><br />");
        }  
    } 
}

function PostbackSuccess_ShowBrewAdd(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);

    $('#newsShowBrew').empty(); 
     
    for (itemID in _data) {
        var item = _data[itemID];
        // now item should be the array you're expecting, so...
        if (item.brewery!= null){
            $('#newsShowBrew').show();
            $('#newsShowBrew').append('<a href="/beer/brewery/' + item.slug +'" target="_parent" title="' + item.brewery +' - ' + item.city +', ' + item.state +'">' + item.brewery + "</a><br />");
        }  
    } 
}

function PostbackSuccess_ShowBMBrew(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);

    $('#adminShowBrew').empty(); 
     
    for (itemID in _data) {
        var item = _data[itemID];
        // now item should be the array you're expecting, so...
        if (item.loc_id!= null){
            $('#adminShowBrew').show();
            $('#adminShowBrew').append('<a href="#" id="user_' + item.loc_id + '" ><strong>' + item.name + '</strong> ' + item.street + ' ' + item.city + ' ' + item.state + ' ' + item.zip + ' ' + item.phone + ' ' + item.url + ' ' + item.twitter + ' ' + item.facebook + "</a><br />");
        }  
    } 
}

function PostbackSuccess_ShowBeer(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);

    $('#newsShowBeer').empty(); 
     
    for (itemID in _data) {
        var item = _data[itemID];
        // now item should be the array you're expecting, so...
        if (item.beer!= null){
            $('#newsShowBeer').show();
            $('#newsShowBeer').append('<a href="#" id="buser_' + item.beer_id + '" >' + item.beer + " by " + item.brewery + "</a> <br />");
        }  
    } 
}

function PostbackSuccess_ShowUser(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);

    $('#blogShowUser').empty(); 
     
    for (itemID in _data) {
        var item = _data[itemID];
        // now item should be the array you're expecting, so...
        if (item.member_name!= null){
            $('#blogShowUser').show();
            $('#blogShowUser').append('<a href="#" id="user_' + item.id_member + '" >' + item.member_name  + "</a> <br />");
        }  
    } 
}

function PostbackSuccess_ShowWGC(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);
    if(_data.wgc=="1"){
        var title="This beer has been added to your MyWants";
        var loc="wants";
    }
    if(_data.wgc=="2"){
        var title="This beer has been added to your MyGots";
        var loc="gots";
    }
    if(_data.wgc=="3"){
        var title="This beer has been added to your MyCellar";
        var loc="cellar";
    }
    
    // set the correct WGC to checked after it has been added to the DB
    $('#'+_data.uid+'_'+_data.wgc+'_'+_data.beer_id).html('<a href="/wgc/'+loc+'/'+_data.uid+'" title="'+title+'"><img src="/images/ok.png" height="10px;" border="0"></a>');
    // re initialize the tooltips so it shows with this new click
    $("a[title]").tooltip({
        track: true, 
        delay: 0, 
        showURL: false, 
        showBody: " - ", 
        fade: 250 
    }); 
}

function PostbackSuccess_ShowCellar(_data, textStatus, XMLHttpRequest) {
    //console.log(_data);
    if(_data.update=="ok"){
        if(_data.current==0){
            if(_data.value=="hidden"){
                var title = " - Click to show hidden beer";
                var image = "active.png";
            }
            if(_data.value=="trade"){
                var title = "Currently Not Marked Tradable - Click to mark as tradable";
                var image = "inactive.png";
            }
            if(_data.value=="email"){
                var title = " - Click to turn on PM notification";
                var image = "inactive.png";
            }
            
        }else{
            if(_data.value=="hidden"){
                var title = " - Click to hide beer";
                var image = "inactive.png";
            }
            if(_data.value=="trade"){
                var title = "Currently Marked Tradable - Click to mark as actively trying to trade";
                var image = "active.png";
            }
            if(_data.value=="email"){
                var title = " - Click to turn off PM notification";
                var image = "active.png";
            }
            
        }
        
        if(_data.current==2){
            if(_data.value=="trade"){
                var title = "Currently Marked Actively Trying to Trade - Click to mark as non-tradable";
                var image = "active_alt.png";
            }  
        }   
        
        // set the correct WGC to checked after it has been added to the DB
        $('#'+_data.uid+'_'+_data.value+'_'+_data.beer_id).html('<a href="javascript:flipCellar('+_data.uid+','+_data.beer_id+', \''+_data.value+'\','+_data.current+')" title="'+ title +'"><img src="/images/'+ image +'" border="0"></a>');
        // re initialize the tooltips so it shows with this new click
        $("a[title]").tooltip({
            track: true, 
            delay: 0, 
            showURL: false, 
            showBody: " - ", 
            fade: 250 
        });         
    }
}

function PostbackError(XMLHttpRequest, textStatus, errorThrown) {
    alert("Error!\n\n" + errorThrown + "\n\n" + textStatus + "");
    return false;
}

function PostbackComplete(XMLHttpRequest, textStatus) {
    //CloseModal();
	//alert("complete");
    return false;
}

function flipCellar(uid,beer_id,value,current){
	
		var data = '{ "_uid":"' + uid + '", "_beer_id":"' + beer_id + '", "_value":"' + value + '", "_current":"' + current + '" }';
		var url = "/ajax/cellar_flip.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowCellar, PostbackError, PostbackComplete);

}

function addWgc(uid,wgc,beer_id,brew_id,style_id){
	
		var data = '{ "_uid":"' + uid + '", "_beer_id":"' + beer_id + '", "_wgc":"' + wgc + '", "_brew_id":"' + brew_id + '", "_style_id":"' + style_id + '" }';
		var url = "/ajax/wgc_add.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowWGC, PostbackError, PostbackComplete);

}

function searchBreweries(term) {
	if (term != null) {
		var data = '{ "_term":"' + term + '" }';
		var url = "/ajax/brewery_find.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowBrew, PostbackError, PostbackComplete);
	}else {
		alert("Missing variables. Please refresh the page and try again.");
	}
}

function searchBreweriesAdd(term) {
	if (term != null) {
		var data = '{ "_term":"' + term + '" }';
		var url = "/ajax/brewery_find.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowBrewAdd, PostbackError, PostbackComplete);
	}else {
		alert("Missing variables. Please refresh the page and try again.");
	}
}

function searchBMBreweries(term) {
	if (term != null) {
		var data = '{ "_term":"' + term + '" }';
		var url = "/ajax/bm_find.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowBMBrew, PostbackError, PostbackComplete);
	}else {
		alert("Missing variables. Please refresh the page and try again.");
	}
}

function searchBeers(term) {
	if (term != null) {
		var data = '{ "_term":"' + term + '" }';
		var url = "/ajax/beer_find.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowBeer, PostbackError, PostbackComplete);
	}else {
		alert("Missing variables. Please refresh the page and try again.");
	}
}

function searchUsers(term) {
	if (term != null) {
		var data = '{ "_term":"' + term + '" }';
		var url = "/ajax/blog_users_find.php";
		//LoadModal("<center><br /><br />Loading scenario, please wait...<br /><br /><img src=\"../../images/ajax-loader.gif\" alt=\"Loading...\" /></center>");
		DoPostback(url, data, PostbackSuccess_ShowUser, PostbackError, PostbackComplete);
	}else {
		alert("Missing variables. Please refresh the page and try again.");
	}
}
