var AJAX_BASE_DIR = GetSiteLocation(); //window.location.href + "/ajax/"; //"http://localhost/titanicwebdesign.com/ajax/";

// Admin site control panel
var ADDNEWS    = "controlpanel/addnews.php";
var EDITNEWS   = "controlpanel/editnews.php";
var UPDATENEWS = "controlpanel/updatenews.php";
var DELETENEWS = "controlpanel/deletenews.php";
var ADDFILETODATABASE = "controlpanel/addfiletodatabase.php";
var DELETEFILES = "controlpanel/deletefiles.php";

// Submit Design Quote
var SUBMITQUOTE = "execquoterequest.php";

// Javascript, javascript...
function setCharAt(str,index,chr)
{
    if (index > str.length-1)
        return str;

    return str.substr(0,index) + chr + str.substr(index+1);
}

function getBaseURL()
{
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    if (baseURL.indexOf('http://localhost') != -1) {
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
    }
    else
        return baseURL + "/";
}

function GetSiteLocation()
{
    AJAX_BASE_DIR = getBaseURL(); //window.location.href;
    AJAX_BASE_DIR += "ajax/";
    return AJAX_BASE_DIR;
}

// Interface update code
// Success action events for each of the comands above

// Shows a just added news story
function SuccessEvent_AddNews(html)
{
    var e = gbi('news_feed_div');
    e.innerHTML = html + '\r\n' + e.innerHTML;
}

// Hides the news from UI
function SuccessEvent_DeleteNews(id)
{
    var e = gbi('main_news_' + parseInt(id) + '_container');
    e.style.display = 'none';
}

function SuccessEvent_UpdateNews(batch)
{
    var params = batch.split("№", 2); // № is a special splitter character (Russian)

    var id = params[0];

    switchdiv('news_id_' + parseInt(id) + '_solid', 'news_id_' + parseInt(id) + '_editable');

    var e = gbi('news_id_' + parseInt(id) + '_solid');

    if (e) e.innerHTML = params[1];
}

// Submit Design Quote
function SuccessEvent_QuoteReceived(html)
{
    //alert("SuccessEvent_QuoteReceived - " + html);

    // external function
    hide_costs_calculator();
}

var message_received = 0;

function SuccessEvent_ProposalReceived(html)
{
    //alert("SuccessEvent_ProposalReceived");
    //$('#proposal_request_sent').css("display", 'block');
    //$('#sending_proposal_request').css("display", 'none');
    $("#SubmitButton").css("background-image", "url('http://www.titanicwebdesign.com/Images/MessageReceived.png')");
    message_received = 1;
}

function SuccessEvent_AddFileToDatabase(data)
{
    //alert("SuccessEvent_AddFileToDatabase," + data);
}

function SuccessEvent_DeleteFilesFromDatabase(number_of_files)
{
    // Visually erase files from selected checkboxes
    for (var idx = 0; idx < number_of_files; idx++)
        if ($('input[id=chf' + idx + ']').attr('checked'))
            $('#chfcontainer' + idx).css('display', 'none');
    // Remove loading animation
}

