

var checkName = "null";

$(document).ready(function() {

    if (checkName == "null")
    {
    
        $('span.nameCustomizer').each(function() {

         $(this).click(function() { getName(); return false; });

        });
    
    } else {
    
        switchNames(checkName);
        
    }
    
    
});

function getName() {

    // prepare the modal window...this is the window that welcomes users to the 
    // view classes page.
    var popString = "/_personalizeHandlers/_getName.aspx?" + 
        "height=100&width=400&modal=false&KeepThis=true&TB_iframe=true";

    //pop the window
    var modalWindow = tb_show("", 
        popString, 
        "");


}

function setName(guestName) {

    if (guestName.length > 1)
    {
        guestName = capitalizeMe(guestName);

        switchNames(guestName);

        // make call to ajax function
        saveToDB(guestName);
    }
    
    tb_remove();
    

}


function switchNames(checkName) {


    // loop all name holders and replace
    $('span.nameCustomizer').each(function() {
    
     $(this).text(trim(checkName));
     $(this).removeClass("nameCustomizer");
     $(this).addClass("nameCustomized"); 
     $(this).unbind('click');     

    });   
    
    // loop all name holders and replace
    $('input.inputName').each(function() {

     $(this).val(checkName);

    });       
    

}

function saveToDB(guestName) {

    $.ajax({
        type: "POST",
        url:  "/_personalizeHandlers/_saveName.aspx",
        data: "N=" + 
            encodeURI(guestName) + 
            "&P=" + 
            encodeURI(window.location),
        
            success: function(msg){
                // keep this in case we need it
                
            }
        
        });
        
}


function capitalizeMe(val) {

    newVal = '';
    val = val.split(' ');

    for(var c=0; c < val.length; c++) {

            newVal += val[c].substring(0,1).toUpperCase() +
            val[c].substring(1,val[c].length) + ' ';
            
    }

    return newVal;
}
