/**
 * jquery_osd_notepad.js - JavaScript Functions for Notepad Implementation.
 * @package jquery_osd_notepad.js
 * @copyright 2005 - 2008 OSD
 * Functions for jQuery interface for Notepad Implementation.
*/

//$(document).ready(function() { 
//);
var statusSuccess = "success";
var statusFailed = "failed";
var statusSeperator = "|";
var data_helper = "/osd_helpers/osd_ajax_data.php";

//alert("included");

function check_success(data_string) { 
    //alert("check_success : " + data_string);
    if ((data_string.search(/success/i)) != -1) { 
        //alert("Yes "+data_string);
        return true;
    }
    else { 
        //alert("No "+data_string);
        return false;
    }
}

function notepad_add_item(p_id) { 
    //alert("Selected Product ID : "+p_id);
    // AJAX POST call to add to notepad
    $.ajax({ 
        type: "POST",
        url: data_helper,
        cache: false,
        data: { 
            object: 'notepad', 
            operation: 'add',
            p_id: p_id 
        },
        success: function(return_data) { 
            //alert(return_data);
            if (check_success(return_data)) { 
                //alert("Yes ");
                var note_new = return_data.replace(statusSuccess+statusSeperator, "");
                //alert("note_new : "+note_new);
                //var note_items = $("#note_items").html();
                //$("#note_items").html(note_items + note_new);
                //$("#note_items:first-child").prepend(note_new);
                //$("#note_items li:last-child").append(note_new);
                $("#note_items").append(note_new);
                //alert($("#note_none").css("display"));
                if ($("#note_none").css("display") != "none") { 
                    $("#note_none").css("display", "none");
                }
                //alert($("#note_none").css("display"));
           }
            else { 
                var status = return_data.replace(statusFailed+statusSeperator, "");
                //alert("Status : "+status);
            }
        }
    }); // ajax
}

function notepad_delete_item(n_id) { 
    //alert("Selected Note ID : "+n_id);
    // AJAX POST call to delete from notepad
    $.ajax({ 
        type: "POST",
        url: data_helper,
        cache: false,
        data: { 
            object: 'notepad', 
            operation: 'delete',
            n_id: n_id 
        },
        success: function(return_data) { 
            //alert("Return data : "+return_data);
            if (check_success(return_data)) { 
                //alert("Note : note_"+n_id);
                $("#note_"+n_id).remove();
                //alert($("#note_items:empty").attr("id"));
                if (n_id == 1) { 
                    // Show default message
                    $("#note_none").css("display", "block");
                }
                var test = $("#note_items:has(li)").attr("id");
                //alert(test);
            }
            else { 
                //alert("Status : Failed");
            }
        }
    }); // ajax
}


