

function getProfileList(container)
{
	$.getJSON('ajax/profiles.php', function(data){

		if(data.status == 99)
		{
			$('#' + container).html(data.message);
			documentid = 0;
			return 0;
		}

		
		$.each(data, function(index){

			$('#profiles_table').append('<tr><td><a href="http://www.derdubor.no/profileAction.do?id=M' + this.id + '" target="_blank	">' + this.firmanavn + '</a></td>' +
										'<td><button onclick="location.replace(\'public/edit_profile.php?id=' + this.id + '&ddbid=M' + this.id +'\');"><img src="/images/icons/page_white_edit.png" />Endre</button></td>' +
										'</tr>');
		});
	});
	$('#please_wait').css('display', 'none');	
}

var documentid = -1;

function getProfileListSimple(container)
{
	$.getJSON('ajax/profiles.php', function(data){
			
			if(data.status == 99)
			{
				$('#' + container).html(data.message);
				documentid = 0;
				return 0;
			}

			var counter = 1;
			
			$('#' + container). html('');
			$.each(data, function(index){

				if(counter == 1)
				{
					documentid = this.id;
				}
				
				$('#' + container).append('<div ' + (counter > 4 ? 'style="display: none;"' : '') + '>' + counter + '. ' + 
										  '<a href="http://www.derdubor.no/profileAction.do?id=M' + this.id + '" target="_blank	">' + this.firmanavn + '</a>' +
										  '&nbsp;&nbsp; (<a href="public/edit_profile.php?id=' + this.id + '&ddbid=M' + this.id + '">Endre</a>)' +  
										  '</div>');
				
				counter++;
			});
	});
}

function getStatistics(container)
{
	$('#' + container).html('<a href="statistics.php"><img id="stat_img_' + documentid + '" src="ajax/statistics.php?action=profile&format=bargraph&document=M' + documentid + '&width=450&height=200" /></a>');
	$('#stat_img_' + documentid).css('border', '1px solid #c0c0c0');
	//$('#stat_img_' + documentid).css('height', '75px');
}


function getInvoices(container, cid)
{
	$.getJSON('ajax/invoices.php', {'type' : 'cid'}, function(data){
		
		$.each(data, function(){
			if(!this.overdue)
			{
				$('#' + container).append('<tr><td><a href="invoice_details.php?id=' + this.id + '">' + this.firmanavn + '</a>' + 
									  '</td><td>' + this.due_date + 
									  '</td></tr>');	
			}
			else
			{
				$('#' + container + '_overdue').append('<tr><td><a href="invoice_details.php?id=' + this.id + '">' + this.firmanavn + '</a>' + 
									  '</td><td>' + this.due_date + 
									  '</td></tr>');
			}
			
		});
		
	});
	$('#' + container).show();
	$('#please_wait').hide();	
}

function getInquiries(container, no_result_text)
{
    $.getJSON('ajax/inquiries.php', {}, function(data)
    {
        var counter = 1;
        $('#' + container).html('');
        HTMLstr = "<ol>";
    
        $.each(data, function() 
        {
            if (!this.read)
            {
                var css_class = 'unread';
            }
            else
            {
                var css_class = '';
            }
            
            if (counter < 10)
            {
                HTMLstr += "<li><a href='inquiry_read.php?id=" + this.id + "'><span class='" + css_class + "'>" + this.from + "</span></a> (" + this.date + ")</li>";
            }
            else
            {
                HTMLstr += "<li style='list-style-type: none; text-align: right;'><a href='inquiry_list.php'>Se resten av henvendelsene her &raquo;</a></li>";
            }
            
            counter++;
        });

        HTMLstr += "</ol>";
        
        if(counter <= 1)
        {
            $('#' + container).html(no_result_text);
        }
        else
        {
            $('#' + container).html(HTMLstr);
        }
    });
}

function getInvoicesSimple(container, no_result_text)
{
	$.getJSON('ajax/invoices.php', {'type' : 'cid'},  function(data){
		var counter = 1;
		$('#' + container).html('');
		$.each(data, function(){
			if(!this.overdue)
			{
				$('#' + container).append('<div ' + (counter > 4 ? 'style="display: none;"' : '') + '>' + counter + '. ' + 
				'<a href="invoice_details.php?id=' + this.id + '">' + this.dato + '</a>' + 
				'</a></div>');
				counter++;
			}
		});
		
		if(counter <= 1)
		{
			$('#' + container).html(no_result_text);
		}
	});
}

function getInvoiceDetails(id)
{
	$.getJSON('ajax/invoices.php', {'type' : 'oid', 'id' : id }, function(data){
		$('#firm_name').html(data.firmname);
		$('#invoice_address').html(data.invoice_address + '<br/>' + data.invoice_address2);
		$('#contact_person').html(data.contact_person);
		$('#phone').html(data.phone);
		$('#fax').html(data.fax);
		$('#order_date').html(data.date);
		$('#ddb_reference').html(data.ddb_reference);
		$('#orgid').html(data.orgid);
		$('#invoice_info').html(data.about);
		$('#invoice_freetext').html(data.freetext);
		$('#order_id').html(data.id);
		$('#invoice_id').html(data.invoice_id);
		$('#mva').html(data.mva + ',-');
		$('#order_sum').html('<b>' + data.order_sum + ',-</b>');
		
		var products_html = '';
		
		$.each(data.products, function(){
			products_html += '<tr>' +
							 '<td colspan="3">' + this.id + ' ' + this.name + ' ' + this.description+  '</td>' + 
							 '<td align="right">' + this.price + ',-</td>' +
							 '</tr>';
						
		});

		$('#products').after(products_html);
	});
	$('#invoice_details').show();
	$('#please_wait').hide();	
}

function getCustomer(container, minimal)
{
	$.getJSON('ajax/customer.php', {action : 'view'}, function(data){
		
		$('#' + container).html('<div>' + data.firmanavn + '</div>');
		$('#' + container).append('<div>' + data.orgnr + '</div>');
		$('#' + container).append('<div>' + data.telefon + '</div>');
		$('#' + container).append('<div>' + data.kontaktperson + '</div>');
		if(!minimal) //if full display
		{
			$('#' + container).append(data.firmanavn);

			$('#please_wait').css('display', 'none');	
		}
	});
}

function getContactPoint(container, default_text)
{
	$('#' + container).html(default_text);
	
	//TODO get contact point from admin,depends on implementation of contact point in admin
}

$.fn.image = function (src, f)
{
	return this.each(function(){
		var i = new Image();
		i.src = src;
		i.onload = f;
		this.appendChild(i);
	});
}

//setup a derdubor popup
//title is optional
function add_ddb_popup(id, title) {
	if(typeof($('#ddb-popup-background').val()) == 'undefined')
	{
		$('<div id="ddb-popup-background">').addClass('popup_window_border').appendTo('body');
	}
	$('#' + id).addClass('popup_window');
	
		$('<div>' + title + '</div>')
		.addClass('title')
		.prependTo('#' + id);
	
	$('<div>Lukk vindu</div>')
		.addClass('close_popup_window')
		.click(function(){
			toggle_ddb_popup(id);
		})
		.prependTo('#' + id);
}

function toggle_ddb_popup(id){
	$('#ddb-popup-background').toggle();
	$('#' + id).toggle();
}