﻿function ShowInformation(id, isVisibleID)
{
    $(id).style.display = "block";
    $(id+'.Link').style.display = "none";
    
    if (isVisibleID)
    {
        $(isVisibleID).value = "1";
    }
}

function HideInformation(id, isVisibleID)
{
    $(id).style.display = "none";
    $(id+'.Link').style.display = "block";
    
    if (isVisibleID)
    {
        $(isVisibleID).value = "0";
    }
}

function ShowGettingStarted(visible, updateUser)
{
    if (visible == 'True')
    {
        $('GettingStarted').style.display="block";
        $('RecentQuotes').style.display="none";
        $('OrgStats').style.display="none";
        $('Sales').style.display="none";
    }
    else
    {
        $('GettingStarted').style.display="none";
        $('RecentQuotes').style.display="block";
        $('OrgStats').style.display="block";
        $('Sales').style.display="block";
        
        if (updateUser == 'True')
        {
            Dashboard_Default.HideGettingStarted();
        }
    }    
}

function SaveNote(contactID, documentID)
{
    var noteType = $F('ctl00_PageContent__Notes__NoteType');
    var note = $F('ctl00_PageContent__Notes__Note');
    var rating = 0;
    
    if ($F('ctl00_PageContent__Notes__Rating1'))
    {
        rating = 1;
    }
    else if ($F('ctl00_PageContent__Notes__Rating2'))
    {
        rating = 2;
    }
    else if ($F('ctl00_PageContent__Notes__Rating3'))
    {
        rating = 3;
    }
    else if ($F('ctl00_PageContent__Notes__Rating4'))
    {
        rating = 4;
    }
    else if ($F('ctl00_PageContent__Notes__Rating5'))
    {
        rating = 5;
    }       
    
    if (typeof(Contacts_ViewCompany) != 'undefined')
    {
        Contacts_ViewCompany.SaveNote(contactID, noteType, note, rating, UpdateNote);
    } 
    else if (typeof(Quotes_Quote) != 'undefined')
    {
        Quotes_Quote.SaveNote(contactID, documentID, noteType, note, rating, UpdateNote);
    } 
    else if (typeof(Quotes_ViewQuote) != 'undefined')
    {
        Quotes_ViewQuote.SaveNote(contactID, documentID, noteType, note, rating, UpdateNote);
    } 
    
    HideInformation('AddNote');
}
    
function UpdateNote(res)
{
    if (res.value)
    {
        $('Notes').innerHTML = res.value + $('Notes').innerHTML;
    }
}

function UpdateChart(chartID, chartType, dataUrl, title)
{
    var chart = new FusionCharts("../Charts/" + chartType + ".swf", title, "450", "135", "0", "1");
    	
    chart.setDataURL(dataUrl);	   	   
    chart.render(chartID);
}

function AddLineItem(lineID, description, price)
{
    var lineItemHtml = $('LineItem').innerHTML;
    
    lineItemHtml = lineItemHtml.replace('LineItemID', lineID);    
    lineItemHtml = lineItemHtml.replace('LineItemID', lineID);    
    lineItemHtml = lineItemHtml.replace('LineItemID', lineID);    
    lineItemHtml = lineItemHtml.replace('LineItemID', lineID);    
    
    $('LineItems').innerHTML += lineItemHtml;
    
    $('LineDescription_'+lineID).value = description;
    $('LinePrice_'+lineID).value = price;
}

function ValidateContact(source, clientside_arguments)
{    
    clientside_arguments.IsValid = 
        $F('ctl00_PageContent__Contact') != '00000000-0000-0000-0000-000000000000' ||
        ($F('ctl00_PageContent__CompanyName') != '' && $F('ctl00_PageContent__ContactName') != '');  
}

var contactWindow;

function NewClient()
{
    if (!contactWindow)
    {
        contactWindow = new Window({className: "dialog", title: "New Client", width:500, height:350, recenterAuto:false, resizable: true});            
    }  
    
    contactWindow.getContent().update($('NewClientControls').innerHTML);  
    contactWindow.showCenter();          
}

function SaveNewClient()
{
    contactWindow.close();
    
    var clientID = Quotes_Quote.SaveContact(
        $F('ctl00_PageContent__CompanyName'),
        $F('ctl00_PageContent__ContactName'),
        $F('ctl00_PageContent__Position'),
        $F('ctl00_PageContent__EmailAddress')).value;
        
    $('ctl00_PageContent__Contact').options[0].text = $F('ctl00_PageContent__CompanyName');
    $('ctl00_PageContent__NewContactID').value = clientID;
}

var prices = new Array();

function RegisterPrice(priceID, status)
{
    prices.push('Price'+priceID);
        
    if (status == 'ENTRYSTATUS/ACTIVE')
    {
        $('Price'+priceID).style.display = "block";
    }
    else
    {
        $('Price'+priceID).style.display = "none";
    }
    
    // Regsiter events
    var controlID = 'ctl0' + priceID;      
    Event.observe('ctl00_PageContent__Prices_' + controlID + '__LineTitle', 'keyup', UpdatePriceEvent);
    Event.observe('ctl00_PageContent__Prices_' + controlID + '__Quantity', 'keyup', UpdatePriceEvent);
    Event.observe('ctl00_PageContent__Prices_' + controlID + '__LinePrice', 'keyup', UpdatePriceEvent);
    
    UpdatePrice(controlID);
}

function UpdatePriceEvent(e)
{
    var controlID = e.target.id.substring(26,31);
    UpdatePrice(controlID);
}

function UpdatePrice(controlID)
{
    var priceID = controlID.substring(4,5);    
    var title = $F('ctl00_PageContent__Prices_' + controlID + '__LineTitle');
    var quantity = parseFloat($F('ctl00_PageContent__Prices_' + controlID + '__Quantity'));
    var price = parseFloat($F('ctl00_PageContent__Prices_' + controlID + '__LinePrice'));
    
    if (!quantity) quantity = 1;
    if (!price) price = 0;          
    
    if (title != '')
    {
        $('Price'+priceID+'Title').innerHTML = title + ": $" + (quantity * price);
    }
    else    
    {
        $('Price'+priceID+'Title').innerHTML = "Option " + (parseInt(priceID) + 1);
    }        
}

function AddPrice()
{
    for (i=0;i<prices.length;i++)
    {
        if ($(prices[i]).style.display == "none")
        {
            $(prices[i]).style.display = "block";            
            
            if (i==prices.length-1)
            {
                $('AddPriceButton').style.display = "none";                
            }
            
            break;
        }
    }
}   

function DeletePrice(priceID)
{
    $(priceID).style.display = "none";
}

function DeleteContactField(fieldID)
{
    $(fieldID).style.display = "none";
    $(fieldID).innerHTML = "";
}

function AddContactField()
{
    var html = $('FieldX').innerHTML;
    
    var fieldIndex = 1;    
    while ($('Field_' + fieldIndex))
    {
        fieldIndex++;
    }
    
    while (html.indexOf('FieldX') >= 0)
    {
        html = html.replace('FieldX', 'Field_' + fieldIndex);
    }    
    
    var row = document.createElement("div");
    row.id = 'Field_' + fieldIndex;
    $('ContactFields').appendChild(row);
  
    $(row.id).update(html);
        
    $('Field_' + fieldIndex + '_Name').focus();
    $('ContactFieldInstructions').style.display = "none";  
}        
    
var subMenu = null;
    
function ShowMenu(menu, subMenu)
{
    if (subMenu != null)
    {
        HideMenu();
    }
    
    subMenu = $('subMenu');
    subMenu.style.display = "block";
    
    Event.observe(document, 'click', HideMenu);
}

function HideMenu(event)
{
    if (subMenu != null)
    {
        subMenu.style.display = "none";
    }
    
    Event.stopObserving(document, 'click');
}