function InitJobServices( id , datas ) {
	var js = $("#"+id);
	js.data( "datas" , datas );
	var checks = $("input[id^="+id+"]");
	for (i=0;i<checks.length;i++) {
		var ochk = $(checks[i]);
		if (ochk.attr("type")!="checkbox") continue;
		// go through checkboxes
		ochk.click( function() { JobServiceTypeChanged( $(this) , id ); } );
	}
	RegenerateJobServices( id );
}

function RefreshJobServices( id ) {
	var js = $("#"+id);
	var d = new Object();
	for (i in aServiceTypes) {
		if (!($("#"+id+"_"+i).attr("checked"))) continue;
		var q = new Object();
		q.unit = $("#"+id+"_unit_"+i).val();
		q.quantity = $("#"+id+"_q_"+i).val();
		q.loc = $("#"+id+"_loc_"+i).val();
		d[i]=q;
	}
	js.data( "datas" , d );
}

function JobServiceTypeChanged( o , id ) {
	RefreshJobServices( id );
	RegenerateJobServices( id );
}

function GenerateSelectOnServiceType( id , servicetype_id ) {
	var sid = id + "_unit_"+servicetype_id;
	var sel = $("<select></select>" ).attr( "id" , sid ).attr("name",sid);
	sel.append( $("<option></option>").html( aJSLangDefs.Choose ).val("") );
	var avail = aXrefServiceTypeUnits[servicetype_id];
	for (i=0;i<avail.length;i++) 
		sel.append( $("<option></option>").html( aServiceTypeUnits[avail[i]] ).val( avail[i] ) );
	return sel;
}

function GenerateInputOnServiceType( id , servicetype_id ) {
	var sid = id + "_q_"+servicetype_id;
	var inp = $("<input></input>" ).attr( "id" , sid ).attr("name",sid);
	inp.attr( "class" , "FrontendTextbox" );
	return inp;
}

function GenerateLocationInputOnServiceType( id , servicetype_id ) {
	var sid = id + "_loc_"+servicetype_id;
	var inp = $("<input></input>" ).attr( "id" , sid ).attr("name",sid);
	inp.attr( "class" , "FrontendTextbox" );
	return inp;
}

function RegenerateJobServices( id ) {
	var o = $("#"+id);
	o.html( "" );
	datas = o.data( "datas" );
	for (servicetype_id in datas) {
		// title
		o.append( $("<div></div>").addClass( "ttl" ).html( aServiceTypes[servicetype_id] + ":" ) );
		// input
		var inp = GenerateInputOnServiceType( id , servicetype_id );
		inp.val( datas[servicetype_id].quantity );
		o.append( $("<div></div>").addClass( "unit").html( "").append( inp ) );
		// select
		var sel = GenerateSelectOnServiceType( id , servicetype_id );
		sel.val( datas[servicetype_id].unit );
		o.append( $("<div></div>").addClass( "sel" ).html( "" ).append( sel ) );
		// cleardiv
		o.append( $("<div></div>").addClass( "clr" ) );
		if (servicetype_id==2) {
			// title
			o.append( $("<div></div>").addClass( "ttl ttlloc" ).html( " - " + aJSLangDefs.interpret_location + ":" ) );
			// input
			var inp = GenerateLocationInputOnServiceType( id , servicetype_id );
			o.append( $("<div></div>").addClass( "sel").html( "").append( inp ) );
			
			o.append( $("<div></div>").addClass( "clr" ) );
		}
	}
}

function ValidateJobServices( id ) {
	RefreshJobServices( id );
	var datas = $("#"+id).data( "datas" );
	var num = 0;
	for (i in datas) {
		num++;
		var u = datas[i].unit;
		if (i == 2 ) {
			if (datas[i].loc.length==0) {
				alert( aJSLangDefs.Location_empty );
				$("#loc").focus();
				return false;
			}
		}
		var min = Math.floor( aServiceTypeMinLengths[u] );
		if (!datas[i].unit.length && min>0) {
			alert( aJSLangDefs.quotepage_error_No_service_unit_filled );
			$("#"+id+"_unit_"+i).focus();
			return false;
		} else {
			// ki van toltve, hogy miben, csak hogy mennyi az a nem mindegy
			var curr = (datas[i].quantity.length) ? Math.floor( datas[i].quantity ) : 0;
			if (curr<min) {
				var al = aJSLangDefs.quotepage_error_Service_size_too_small + "\n- ";
				al+= aJSLangDefs.Minimal_service_size_needed;
				al+=aServiceTypeMinLengths[1] + " " + aServiceTypeUnits[1] + ", ";
				al+=aServiceTypeMinLengths[2] + " " + aServiceTypeUnits[2] + ", ";
				al+=aServiceTypeMinLengths[3] + " " + aServiceTypeUnits[3] + "\n\n";
				al+=aJSLangDefs.quotepage_error_Service_size_too_small2;
				alert( al );
				$("#"+id+"_q_"+i).focus();
				return false;
			}
		}
	}
	if (!num) {
		alert( aJSLangDefs.quotepage_error_No_service_filled );
		return false;
	}
	return true;
}
