var _Debug = true;
var topHandle = null;
var findTOPTries = 0;

function findTOP(win) {
   while ((win.em_top_frame_merlin == null) && (win.parent != null) && (win.parent != win)) {
      findTOPTries++;
      if (findTOPTries > 9) {
			if (_Debug)
      		alert("Error finding API -- too deeply nested.");
      	return null;
      }
      win = win.parent;
   }
   if (win.em_top_frame_merlin != null)
   	return win;
   else
   	return null;
}

function getTOP() {
   var theTOP = findTOP(window);
   if ((theTOP == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
      if (window.opener!=null)
      	theTOP = findTOP(window.opener);
   }
   return theTOP;
}

function getTOPHandle() {
   if (topHandle == null)
      topHandle = getTOP();
   return topHandle;
}

function reload_merlin_data(frameset) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {	
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				top_frame.frames[2].merlin_API.ReloadMerlinData(frameset);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function reload_merlin_data_function(function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {	
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				top_frame.frames[2].merlin_API.ReloadMerlinDataFunction(function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_LastDataUpdate() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetLastDataUpdate();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_idcli() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetIdcli();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_main() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetMain();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_session_id() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetSessionId();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_session_name() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetSessionName();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_app_actual() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetAppActual();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function set_app_actual(value) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				top_frame.frames[2].merlin_API.SetAppActual(value);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_user_actual() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetUserActual();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_group_actual() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetGroupActual();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function set_group_actual(value) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				top_frame.frames[2].merlin_API.SetGroupActual(value);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function add_url_actual(url_actual,group,app) {
	return url_actual+'&group_act='+group+'&app_act='+app;
}

function set_cookie_user(cookiename,cookievalue,expire) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.SetCookieUser(cookiename,cookievalue,expire);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_cookie_user() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return unescape(top_frame.frames[2].merlin_API.GetCookieUser());
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_lang() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {	
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetLang();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_login(idcli,lang) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {	
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetLogin(idcli,lang);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_auto(idcli) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetAuto(idcli);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_remember(idcli) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetRemember(idcli);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}
}

function get_menu() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetMenu();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_app_entorn() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetAppEntorn();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_app_user() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetAppUser();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_app_sis_group() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null)
				return top_frame.frames[2].merlin_API.GetAppSisGroup();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}	
	
function get_app_sis_app() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetAppSisApp();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}		

function get_enviroment() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetEnviroment();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_is_auto() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetIsAuto();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}		
}

function get_is_remember() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetIsRemember();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_username() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetUsername();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_calendar() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetCalendar();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_logout() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetLogout();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_version() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetVersion();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_groups() {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetGroups();
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function check_user_group(user,group,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null)
				return top_frame.frames[2].merlin_API.CheckUserGroup(user,group,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function add_user_group(user,group,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.AddUserGroup(user,group,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_user_atribute(atribute_name,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetUserAtribute(atribute_name,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_user_atributes(function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetUserAtributes(function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function set_user_atributes(atribute_array,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) {
				var array_string = '';
				for (i in atribute_array) {
					if (array_string=='')
						array_string += i+'@@'+atribute_array[i];
					else
						array_string += '*'+i+'@@'+atribute_array[i];	
				}
				return top_frame.frames[2].merlin_API.SetUserAtributes(array_string,function_name);
			}
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_general_user_atributes(function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetGeneralUserAtributes(function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_discussion_type(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetDiscussionType(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_discussion_dates(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetDiscussionDates(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_array_discussion_dates(array_discussions,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) {
				var discussions = array_discussions.join("#");
				return top_frame.frames[2].merlin_API.GetArrayDiscussionDates(discussions,function_name);
			}
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_discussions(function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetDiscussions(function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function get_historic_discussions(function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetHistoricDiscussions(function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function DiscussionCheck(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.DiscussionCheck(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function VoteCheck(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.VoteCheck(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function VoteInsert(discussion_id,result,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) {
				if (result==true)
					return top_frame.frames[2].merlin_API.VoteInsert(discussion_id,"1",function_name);
				else if (result==false)
					return top_frame.frames[2].merlin_API.VoteInsert(discussion_id,"0",function_name);
				else
					return top_frame.frames[2].merlin_API.VoteInsert(discussion_id,"-1",function_name);
				//return top_frame.frames[2].merlin_API.VoteInsert(discussion_id,result,function_name);
			}
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function VoteShow(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.VoteShow(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function VoteResults(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.VoteResults(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function VoteHistoricResults(discussion_id,function_name) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.VoteHistoricResults(discussion_id,function_name);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}

function GetHistoricUrl(type_app,group_id) {
	var top_frame = getTOPHandle();
	if (top_frame!=null) {
		if (typeof(top_frame.frames[2].merlin_API)!='undefined') {
			if (top_frame.frames[2].merlin_API != null) 
				return top_frame.frames[2].merlin_API.GetHistoricUrl(type_app,group_id);
		}
	}
	else {
		if (_Debug)
			alert("No TOP found");	
		return '';	
	}	
}