function JQPSaveValidator() {
	var valid = ($("#template_name").val().length>0);
	if (valid) {
		valid = $("#quote").val().length>0;
	}
	if (valid) {
		$("#btn_save_template").removeAttr( "disabled" );
	} else {
		$("#btn_save_template").attr( "disabled" , "disabled" );
	}
}

function JQPLoadValidator() {
	var num = Math.floor( $("#templates").val() );
	if (num) {
		$("#btn_load_template").removeAttr( "disabled" );
	} else {
		$("#btn_load_template").attr( "disabled" , "disabled" );
	}
}

function JQPDeleteValidator() {
	var num = Math.floor( $("#templates").val() );
	if (num) {
		$("#btn_delete_template").removeAttr( "disabled" );
	} else {
		$("#btn_delete_template").attr( "disabled" , "disabled" );
	}
}

function JQPTemplatesChange() {
	JQPLoadValidator();
	JQPDeleteValidator();
}

function JQPRefreshTemplatesCallback( templates ) {
	var  o = $("#templates");
	o.data( "templates" , templates );
	o.html( "" );
	for (i in templates) {
		var template = templates[i];
		o.append( $("<option></option").val(template.member_template_id).html(template.template_title) );
	}
	JQPTemplatesChange();
}

function JQPRefreshTemplates() {
	$.ajax(
		{
			type: "POST",
			cache: false,
			data : (
				{
					inajax : 1,
					ajaxevent : "refresh_templates"
				}
			),
			success: function(ret) { JQPRefreshTemplatesCallback(ret); }
		}
	);
}

function JQPSaveTemplateCallback( ret ) {
	ClearFormDirty( document.frm_job_quote );
	JQPRefreshTemplates();
}

function JQPSaveTemplate() {
	var datas = new Object();
	var templates = $("#templates").data( "templates" );
	var i;
	var ttl = $("#template_name").val();
	var found = false;
	var member_template_id = 0;
	for (i in templates) {
		if (templates[i].template_title==ttl) {
			member_template_id = templates[i].member_template_id;
			found=true;
		}
	}
	if (found) {
		if (!window.confirm(aJSLangDefs.quotepage_Are_you_sure_to_overwrite_template)) return;
	}
	datas.inajax = 1;
	datas.ajaxevent = "save_template";
	if (found) datas.update = 1;
	if (found) datas.member_template_id = member_template_id;
	datas.template_title = $("#template_name").val();
	datas.template_price = $("#quote_pricing").val();
	datas.template_text = $("#quote").val();
	$.ajax(
		{
			type: "POST",
			cache: false,
			data : datas,
			success: function(ret) { JQPSaveTemplateCallback(ret); }
		}
	);
}

function JQPLoadTemplateCallback( datas ) {
	$("#quote_pricing").val( datas.template_price );
	$("#quote").val( datas.template_text );
	$("#template_name").val( datas.template_title );
	if (datas.template_text.length) {
		$("#quote").removeClass( "invalid" );
	} else {
		$("#quote").addClass( "invalid" );
	}
	if (datas.template_price.length) {
		$("#quote_pricing").removeClass( "invalid" );
	} else {
		$("#quote_pricing").addClass( "invalid" );
	}
	ClearFormDirty( document.frm_job_quote );
	JQPSaveValidator();
}

function JQPLoadTemplate() {
	if (IsFormDirty( document.frm_job_quote )) {
		if (!window.confirm( aJSLangDefs.quotepage_Are_you_sure_to_load_template )) return;
	}
	var datas = new Object();
	// TODO check if template existing and ask for overwrite
	datas.inajax = 1;
	datas.ajaxevent = "load_template";
	datas.member_template_id = $("#templates").val();
	$.ajax(
		{
			type: "POST",
			cache: false,
			data : datas,
			success: function(ret) { JQPLoadTemplateCallback(ret); }
		}
	);
}

function JQPDeleteTemplateCallback( datas ) {
	JQPRefreshTemplates();
}

function JQPDeleteTemplate() {
	var num = Math.floor( $("#templates").val() );
	if (!num) return;
	if (!window.confirm( aJSLangDefs.quotepage_Are_you_sure_to_delete_template )) return;
	var datas = new Object();
	datas.inajax = 1;
	datas.ajaxevent = "delete_template";
	datas.member_template_id = $("#templates").val();
	$.ajax(
		{
			type: "POST",
			cache: false,
			data : datas,
			success: function(ret) { JQPDeleteTemplateCallback(ret); }
		}
	);
}

function JQPToggleJobDetails() {
	var o = $("#job_details");
	if (o.css("display")=="none") {
		o.css("display","block");
	} else {
		o.css("display","none" );
	}
}


function ValidateJobQuoteForm( oF ) {
	if (!oF.quote_pricing.value.length) {
		alert( aJSLangDefs.quotepage_No_quote_pricing_filled );
		oF.quote_pricing.focus();
		return false;
	}
	if (!oF.quote.value.length) {
		alert( aJSLangDefs.quotepage_No_quote_filled );
		oF.quote.focus();
		return false;	
	}
	ClearFormDirty( oF );
	return true;
}

function InitJobQuotePane( id , datas ) {
	$("#templates").data( "templates" , datas.templates );
	$("#quote").keyup( function() { JQPSaveValidator(); } );
	$("#template_name").keyup( function() { JQPSaveValidator(); } );
	$("#btn_load_template").click( function() { JQPLoadTemplate(); } );
	$("#btn_save_template").click( function() { JQPSaveTemplate(); } );
	$("#btn_delete_template").click( function() { JQPDeleteTemplate(); } );
	$("#templates").click( function() { JQPTemplatesChange(); } );
	$("#templates").change( function() { JQPTemplatesChange(); } );

	JQPRefreshTemplatesCallback( datas.templates );
	FormChangeWarning( document.frm_job_quote );
}
