var customer_obj = null ;
var m_contactidx = 0 ;

$(document).ready(function() 
{ 
	$('#btnCustomerClear').button().bind('click',clearCustomer) ;
	$('#btnCustomerUpdate').button().bind('click',updateCustomer) ;
	$('#btnCustomerAdd').button().bind('click',addCustomer) ;
	$('#btnCustomerPrint').button().bind('click',printCustomer) ;
	$('#customer-tabs').tabs() ;
	$('#txtCustomerName').focus() ;
	$(window).resize(resizeCustomerGrid) ;
	resizeCustomerGrid() ;
	clearCustomer();
}) ;
function resizeCustomerGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-customer-entry").outerHeight() - 55;
	$("div#sbg-customer-data").css("height", h +'px') ;		
}
function clearCustomer() {
	$('#txtCustomerId').val("Auto") ;
	$('#txtCustomerCode').val("") ;
	$('#txtCustomerName').val("") ;
	$('#txtCustomerWeb').val("") ;
	$('#txtCustomerEmail').val("") ;
	$('#txtCustomerAddressLine1').val("") ;
	$('#txtCustomerAddressLine2').val("") ;
	$('#txtCustomerAddressLine3').val("") ;
	$('#txtCustomerAddressLine4').val("") ;
	$('#txtCustomerAddressLine5').val("") ;
	$('#txtCustomerTelephone').val("") ;
	$('#txtCustomerFax').val("") ;
    $('#tblCustomerContact').empty() ;
	$('#customer-tabs').tabs("option", "active", 0) ;
	$('#txtCustomerName').focus() ;
	$('#btnCustomerUpdate').prop('disabled','disabled') ;
	$('#customer_err_mesg').text('') ;
	$('#customer_err_name').hide() ;
	if (customer_obj != null && customer_obj != undefined)
	{
		$($(customer_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	customer_obj = null;
}

function updateCustomer() {
	saveCustomer(C_UPDATE) ;
}
function addCustomer() {
	saveCustomer(C_ADD) ;
}
function saveCustomer(type) {
	if (!validateCustomer()) {
		return false;
	}
	var data = { "type": type, "id": $('#txtCustomerId').val(), 
                                "code": $('#txtCustomerCode').val(), 
                                "name": $('#txtCustomerName').val(), 
                                "web": $('#txtCustomerWeb').val(), 
                                "email": $('#txtCustomerEmail').val(), 
                                "address_line_1": $('#txtCustomerAddressLine1').val(), 
                                "address_line_2": $('#txtCustomerAddressLine2').val(), 
                                "address_line_3": $('#txtCustomerAddressLine3').val(), 
                                "address_line_4": $('#txtCustomerAddressLine4').val(), 
                                "address_line_5": $('#txtCustomerAddressLine5').val(), 
                                "telephone": $('#txtCustomerTelephone').val(), 
                                "fax": $('#txtCustomerFax').val(),
                            	"customer_contact": getCustomerContact(),};
	var url = "request.pzx?c=" + customer_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onCustomerResponse,customer_obj) ;
}

function editCustomer(id,obj) {	
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + customer_url + "&d=" + new Date().getTime();		
	clearCustomer();
	customer_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showCustomer,obj) ;
}

function deleteCustomer(id,obj) {
	if (confirm("Confirm you want to delete Customer id : " + id + "?") == false) {
		return false;
	}
	
	var data = {"type": C_DELETE,"id": id} ;
	var url = "request.pzx?c=" + customer_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onCustomerResponse,obj) ;
}
function showCustomer(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtCustomerId').val(resp.data.id) ;
	$('#txtCustomerCode').val(resp.data.code) ;
	$('#txtCustomerName').val(resp.data.name) ;
	$('#txtCustomerWeb').val(resp.data.web) ;
	$('#txtCustomerEmail').val(resp.data.email) ;
	$('#txtCustomerAddressLine1').val(resp.data.address_line_1) ;
	$('#txtCustomerAddressLine2').val(resp.data.address_line_2) ;
	$('#txtCustomerAddressLine3').val(resp.data.address_line_3) ;
	$('#txtCustomerAddressLine4').val(resp.data.address_line_4) ;
	$('#txtCustomerAddressLine5').val(resp.data.address_line_5) ;
	$('#txtCustomerTelephone').val(resp.data.telephone) ;
	$('#txtCustomerFax').val(resp.data.fax) ;
    showCustomerContact(resp.data.customer_contact);
	$('#btnCustomerUpdate').prop('disabled','') ;
	$('#txtCustomerName').focus() ;
}
function printCustomer() {
	var url = "report.pzx?c=" + customer_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function addCustomerContact() {
	m_contactidx++;
	$('#tblCustomerContact').append("<tr id='trCustomerContact" + m_contactidx + "'><td style='width:30px;display:none'>" +m_contactidx+ "</td>" +
		"<td style='width:120px;'>" + $('#txtCustomerContactName').val() + "</td>" +
		"<td style='width:120px;'>" + $('#txtCustomerContactEmail').val() + "</td>" +
		"<td style='width:120px;'>" + $('#txtCustomerContactTelephone').val() + "</td>" +
		"<td style='width:120px;'>" + $('#txtCustomerContactFax').val() + "</td>" +
		"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeCustomerContact(" + m_contactidx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
	clearCustomerContact();
}
function clearCustomerContact() {
	$('#txtCustomerContactName').val('') ;
	$('#txtCustomerContactEmail').val('') ;
	$('#txtCustomerContactTelephone').val('') ;
	$('#txtCustomerContactFax').val('') ;
}
function removeCustomerContact(idx) {
	$('#trCustomerContact'+idx).remove() ;
}
function getCustomerContact(){
	var lines = "" ;
	var sep = "" ;
	$('#tblCustomerContact tr').each(function() { 
		name = $(this).find("td").eq(1).html()  ;
		email = $(this).find("td").eq(2).html() ;
		telephone = $(this).find("td").eq(3).html() ;
		fax = $(this).find("td").eq(4).html() ;
        
        lines = lines + sep + name  + "~" + email + "~" + telephone + "~" + fax;
        sep = "|" ;
	}); 
	return lines ;
}
function showCustomerContact(lines) {
	if (lines == undefined || lines == null || lines == "") return ;
	
	var line = lines.split('|') ;
	var fld = "" ;
	m_contactidx = 0 ;
	for (var i = 0 ; i < line.length ; i++) {
		if (line[i] != "") {
			fld = line[i].split('~') ;
			m_contactidx++;
			$('#tblCustomerContact').append("<tr id='trCustomerContact" + m_contactidx + "'><td style='width:30px;display:none'>" +m_contactidx+ "</td>" +
                                    "<td style='width:120px;'>" + fld[0] + "</td>" +
                                    "<td style='width:120px;'>" + fld[1] + "</td>" +
                                    "<td style='width:120px;'>" + fld[2] + "</td>" +
                                    "<td style='width:120px;'>" + fld[3] + "</td>" +
                                    "<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeCustomerContact(" + m_contactidx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
			
		}
	}
}
function onCustomerResponse(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("Error",resp.mesg) ;
		return;
	}
	if (resp.type == C_ADD) {
		$('#sbg-customer-table tr:first').after("<tr><td>" + resp.data + "</td>" + 
			"<td>" + $('#txtCustomerCode').val() + "</td>" + 
			"<td>" + $('#txtCustomerName').val() + "</td>" + 
			"<td style='text-align:center'><a href='javascript:' onclick='editCustomer(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='deleteCustomer(" + resp.data + ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" +
			"</tr>"); 
	} else if (resp.type == C_DELETE) {
		$($(obj).closest("tr")).remove() ;
	} else if (resp.type == C_UPDATE) {
		$($(obj).closest('tr')).children('td:eq(1)').html($('#txtCustomerCode').val()) ;
		$($(obj).closest('tr')).children('td:eq(2)').html($('#txtCustomerName').val()) ;
	}
	$('#sbg-popup-box').html(resp.mesg) ;
	$('#sbg-popup-box').show() ;
	$('#sbg-popup-box').fadeOut(5000) ;
	clearCustomer() ;
}
function validateCustomer() {
	$('#customer_err_mesg').text('') ;
	if ($('#txtCustomerName').blank())
	{
		$('#customer_err_name').show() ;
		$('#txtCustomerName').focus() ;
		$('#customer_err_mesg').text("Customer name can not be blank.") ;
		return false ;
	}
	else 
		$('#customer_err_name').hide() ;
			
	return true ;
}