function IsLanguagePairChecked( id , servicetype_id , source_language_id , target_language_id , datas ) {
	var lm = datas.language_matrix;
	if (typeof lm[servicetype_id] =="undefined") return false;
	var sl = lm[servicetype_id];
	if (typeof sl[source_language_id] == "undefined") return false;
	var tl = sl[source_language_id];
	if (typeof tl[target_language_id] == "undefined") return false;
	return true;
}

function GetExpanderSelector( id , source_language_id ) {
	var prefix = id + "_target_" + source_language_id + "_";
	var s = "#"+id + " tr[id^="+prefix+"]";
	return $(s);
}

function ExpandSourceLanguage( id , source_language_id ) {
	var o = GetExpanderSelector( id , source_language_id );
	o.removeClass( "hide" );
	GenerateSourceLanguageCollapserString( $("#"+id+"_source_"+source_language_id) , id , source_language_id , false );
}

function CollapseSourceLanguage( id , source_language_id ) {
	var o = GetExpanderSelector( id , source_language_id );
	o.addClass( "hide" );
	GenerateSourceLanguageCollapserString( $("#"+id+"_source_"+source_language_id) , id , source_language_id , true );
}

function SelectServiceType( id , o ) {
	var servicetype_id = o.data( "servicetype_id" );
	var selected = o.attr( "checked" );
	var prefix = id + "_" + servicetype_id + "_";
	var s = "#"+id + " input[id^="+prefix+"]";
	if (selected) {
		$(s).attr( "checked" , "checked" );
	} else {
		$(s).removeAttr( "checked" );
	}
}

function ExpandAllLanguages( id ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var sellangs = datas.selected_languages;
	for (var i in sellangs) ExpandSourceLanguage( id , sellangs[i] );
}

function CollapseAllLanguages( id ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var sellangs = datas.selected_languages;
	for (var i in sellangs) CollapseSourceLanguage( id , sellangs[i] );
}

function GenerateSourceLanguageCollapserString( td , id , lang_id , collapsed ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var languages = datas.languages;
	td.html( languages[lang_id] + " " );

	img = $("<img></img>");
	img.addClass( "collapser" );
		
	if (collapsed) {
	if (collapsed)
		img.attr( "src" , "/images/down.png" );
		img.click( function() { ExpandSourceLanguage( id , lang_id ); } );
	} else {
		img.attr( "src" , "/images/up.png" );
		img.click( function() { CollapseSourceLanguage( id , lang_id ); } );
	}
	td.append( img );
	return td;
}

function GenerateSourceLanguageCollapser( id , lang_id , collapsed ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var languages = datas.languages;

	var td = $("<td></td>");
	td.attr( "id" , id + "_source_" + lang_id );
	GenerateSourceLanguageCollapserString( td , id , lang_id , collapsed );
	return td;
}

function RenderLanguagePairs( id ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );

	var servicetypes = datas.servicetypes;
	var sellangs = datas.selected_languages;
	var languages = datas.languages;

	// generate table
	var tbl = $("<table></table>");
	tbl.addClass( "LanguagePairs" );
	// generate header row
	var hdr =$("<tr></tr>" );
	var td = $("<td></td>" );
	td.append( aJSLangDefs.myprofilepage_Expand_all_languages + " " );
	var img = $("<img></img>");
	img.attr( "src" , "/images/down.png" );
	img.click( function() { ExpandAllLanguages( id ); } );
	td.append( img );
	td.append( " " );

	var img = $("<img></img>");
	img.attr( "src" , "/images/up.png" );
	img.click( function() { CollapseAllLanguages( id ); } );
	td.append( img );

	hdr.append( td );
	// generate header
	var servicetype_id;
	for (servicetype_id in servicetypes) {
		var td = $("<td></td>");
		td.addClass( "inp" );
		td.attr( "id" , id + "_hdr_"+servicetype_id );
		td.html( servicetypes[servicetype_id] );
			td.addClass( "inp" );
			if (!datas.readonly) {
				var inp = $("<input></input>" );
				inp.attr( "type" , "checkbox" );
				inp.data( "servicetype_id" , servicetype_id );
				inp.click( function( ) { SelectServiceType( id , $(this) ); } );
				td.append( inp );
			}
		hdr.append( td );
	}
	var thead = $("<thead></thead>");
	thead.append( hdr );
	tbl.append( thead );
	// generate source language header
	var i,j,k;
	var source_language_id;
	var target_language_id;
	var img;

	for (i in sellangs) {
		source_language_id = sellangs[i];
		var tr = $("<tr></tr>");
		tr.addClass( "lgreen" );
		tr.attr( "id" , id + "_" + source_language_id );
		// text
		var td = GenerateSourceLanguageCollapser( id , source_language_id , true );
		tr.append( td );
		for (servicetype_id in servicetypes) {
			td = $("<td></td>");
			tr.append( td );
		}
		tbl.append( tr );
		// now language matrix
		tr = "";
		for (j in sellangs) {
			target_language_id = sellangs[j];
			if (source_language_id==target_language_id) continue;
			tr+="<tr id=\""+id+"_target_"+source_language_id+"_" + target_language_id + "\" class=\"hide dgreen\">";
			tr+="<td class=\"padder\">" + ( languages[target_language_id] ) + "</td>";

			for (servicetype_id in servicetypes) {
				tr+="<td class=\"inp\">";
				var chkd =IsLanguagePairChecked( id , servicetype_id , source_language_id , target_language_id , datas );
				if (datas.readonly) {
					if (chkd) {
						tr+=( "&bull;" ); 
					} else {
						tr+=( "" );
					}
				} else {
					var sid = id + "_" + servicetype_id + "_" + source_language_id + "_" + target_language_id;
					var inp = "<input type=\"checkbox\" id=\""+sid+"\" name=\""+sid+"\"";
					if (chkd)
						inp+=" checked=\"checked\"";
					inp+=" />";
					tr+=inp;
				}
			}
		}
		tbl.append( tr );
	}
	o.append( tbl );
}

function InitLanguagePairs( id , datas ) {
	var o = $("#"+id);
	o.data( "datas" , datas );
	RenderLanguagePairs( id );
	if (typeof ClearFormDirty == "function" ) ClearFormDirty( document.frm_my_language_pairs );
}
