
function mtcRun(fun, retries, timeout) {
	try {
		if(typeof(fun) == "function") {
			if(fun())
				return true;
		}
	}
	catch(e) {}
	if(retries > 0) {
		setTimeout("mtcRun(" + fun + ", " + retries-- + ", " + timeout * 5 + ")", timeout);
	}
	return false;
}

/***********Ajout de gilles*********************/
/* Manage ad
/* old function
 */
function refresh_adtag(divname) {
	setAdProfil();
}

/***************************************************/
/* Manage ad */
function setAdProfil() {
	if(document.getElementById("mtcAdTop"))
		mtcSetAd("mtcAdTop", "profil", "ban", 728, 90);
	if(document.getElementById("mtcMembersPub"))
		mtcSetAd("mtcMembersPub", "profil", "medban", 300, 250);
}

function mtcAd(divId, type, ban, width, height) {
	this.divId = divId;
	this.type = type;
	this.ban = ban;
	this.w = width;
	this.h = height;
	this.ifrId = divId + 'Ifr';
}

mtcAd.prototype.refresh = function () {
	var div = document.getElementById(this.divId);
	if(!div)
		return false;
	var ifr = eval("frames." + this.ifrId);
	if(ifr)
		ifr.location.reload();
	else
		div.innerHTML = '<iframe id="' + this.ifrId + '" name="'+ this.ifrId +'" width="' + this.w + '" height="' + this.h + '" src="/as.php?type=' + this.type + '&ban=' + this.ban + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="no"></iframe>';
	return true;
}

function mtcSetAd(divId, type, ban,  width, height) {
	var ad = new mtcAd(divId, type, ban, width, height);
	ad.refresh();
}

/***************************************************/
/* Tiny tools */
function getHTMLData(d) {
	d = document.getElementById(d);
	return (d? d.innerHTML : '');
}

function setHTMLData(d, html) {
	d = document.getElementById(d);
	if(d) d.innerHTML = html;
}

function delHTMLCommentTags(dat) {
	return(dat.replace(/(\<!--|--\>)/gi,''));
}

function stringCutter(str, maxi) {
	return (str.length > maxi ? str.substr(0, maxi-1) + '.' : str);
}

function maxTextarea(obj, maxi) {
	if(obj.value.length > maxi) obj.value = obj.value.substr(0, maxi);
}

/***************************************************/
/* Alert IsAway */
function mtcAlertIsAway() {
	setHTMLData('mtcIAMessHead', getHTMLData('mtcIAMessHeadIsaway'));
	setHTMLData('mtcIAMessLabel', getHTMLData('mtcIAMessLabelIsaway'));
	if(getObj('mtcIsAway')) {
		slideIsAwayUp();
		return true;
	}
	return false;
}

/***************************************************/
/* Action Callback */

function mtcFlashCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_flash_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('Flash', sentence, ok);
}

function mtcMeeticTestCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_test_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('MeeticTest', sentence, ok);
}
// la fonction mtcMeeticTestConfirmCallback pour le MeeticTest est intégrée directement dans la section de la page body left colum memberprofile

function mtcContactCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_contact_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('Contact', sentence, ok);
}

function mtcContactConfirmCallback(aboid, nickname, ori, posx, posy, act) {
	var func = 'event_contact.event_contact_set("' + aboid + '", "mtcContactCallback", "mtcContactConfirmCallback", true)';
	confirmBubble(ori, posx, posy, act, nickname, func);
}

function mtcContactcommentCallback(sentence, ok, nickname, comment) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_contact_comment_nickname"\>\<\/span\>/, nickname);
	sentence = sentence.replace(/\<span id="contact_comment"\>\<\/span\>/, comment);
	mtcActionCallback('Contact', sentence, ok);
}

function mtcFavoriteCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_favorite_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('Favorite', sentence, ok);
}

function mtcFavoritecommentCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_favorite_comment_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('Favorite', sentence, ok);
}

function mtcBlacklistCallback(sentence, ok, nickname) {
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\<span id="event_blacklist_nickname"\>\<\/span\>/, nickname);
	mtcActionCallback('Blacklist', sentence, ok);
}

function mtcBlacklistConfirmCallback(aboid, nickname, ori, posx, posy, act) {
	var func = 'event_blacklist.event_blacklist_set("' + aboid + '", "mtcBlacklistCallback", "mtcBlacklistConfirmCallback", true)';
	confirmBubble(ori, posx, posy, act, nickname, func);
}

function mtcPhoneCallback(sentence, ok, nickname) {
	mtcContactCallback(sentence, ok, nickname);
}

function mtcPhoneConfirmCallback(aboid, nickname, ori, posx, posy, act) {
	var func = 'event_phone.event_phone_set("' + aboid + '", "mtcPhoneCallback", "mtcPhoneConfirmCallback", true)';
	confirmBubble(ori, posx, posy, act, nickname, func);
}

function mtcContactsRedirect() {
	window.location.href = getObj('mycontacts_href').href;
}
/****/

function mtcActionCallback(act, sentence, ok) {
	var head = getHTMLData('mtcConfMessHead'+act);
	
	setHTMLData('mtcConfMessHead', head);
	setHTMLData('mtcConfMessLabel', sentence);
	
	try {
		slideConfirmUp();
	}
	catch(e) {
		//window.status = "mtcActionCallback: " + ":" + nickname + ":" + sentence + ":" + ok;
	}
}

/******************/
/* Available */

function redirectSerenity() {
	window.location.href = getObj("offers_index_href").href + "?typ_off=12&option_premium=SERENITY";
}

/* confirm available status */
function confirmAvailableStatus(sel) {
	if(sel) {
		confirmBubble('mtcAvailable', -52, 15, (mtcState ? 'unavailable' : 'available'), '', 'available.available_toggle("mtcAvailableCallback")');
	}
	else {
		confirmBubble('mtcAvailable', -52, 15, 'statusnosel', '', 'redirectSerenity()');
	}
}

function setAvailableStatus(state) {
	if(mtcState) messenger_userStatusAvailable();
	else messenger_userStatusUnavailable();
}

function changeAvailableStatus(state) {
	mtcState = eval(state);
	var obj = document.getElementById("mtcAvailable");
	if(obj) {
		obj.className = (mtcState)? "mtcDispAvailable" : "mtcDispUnAvailable";
	}
	setAvailableStatus(mtcState);
}

/* confirm available status slideup */
function mtcAvailableCallback(sentence, state, nickname) {
	
	changeAvailableStatus(state);
	
	var headhtml = getHTMLData("mtcConfMessHeadAvailable");
	var MessLabel = "<strong>" + nickname + "</strong>,&nbsp;" + sentence;
	
	setHTMLData("mtcConfMessLabel", MessLabel);
	setHTMLData("mtcConfMessHead", headhtml);
	
	try {
		slideConfirmUp();
	}
	catch(e) {
		//window.status = "mtcActionCallback: " + ":" + nickname + ":" + sentence + ":" + ok;
	}
}

/***************************************************/
/* Confirm Bubble */
var confbub_timer = 0;

function closeConfirmBubble() {
	if(confbub_timer) clearTimeout(confbub_timer);
	dispLayer('mtcConfirmBubble', 0);
}

function confirmBubble(ori, posx, posy, act, nickname, func) {
	if(confbub_timer) clearTimeout(confbub_timer);
	
	var obj = new getObj(ori);
	var conf = new getObj('mtcConfirmBubble');
	var sentence = getHTMLData('mtcConfBubble_'+act);
	
	nickname = '<strong>' + nickname + '</strong>';
	sentence = sentence.replace(/\[-pseudo-\]/gi, nickname);
	
	setHTMLData('mtcConfBubbleMsg', sentence);
		
	//if(document.all) posx -= 25;
	posx = findPosX(obj) + eval(posx);
	posy = findPosY(obj) + eval(posy);
	moveLayer('mtcConfirmBubble', posx, posy);
	
	getObj('mtcConfBubbleNo').onclick = function() {
		closeConfirmBubble();
	};
	
	getObj('mtcConfBubbleYes').onclick = function() {
		var doIt = (document.all)? setTimeout('eval('+func+')', 10) : eval(func);
		closeConfirmBubble();
	};
	
	dispLayer('mtcConfirmBubble', 1);
	
	//rollover display
	obj.onmouseout = function() {
		confbub_timer = setTimeout('closeConfirmBubble()', 1000);
	};
	obj.onmouseover = function() {
		if(confbub_timer) clearTimeout(confbub_timer);
	};
	conf.onmouseout = function() {
		confbub_timer = setTimeout('closeConfirmBubble()', 1000);
	};
	conf.onmouseover = function() {
		if(confbub_timer) clearTimeout(confbub_timer);
	};
}

/*************************************/
/* quicksearch */
/*************************************/
var qs_search = {};

qs_search.town = function(){
	var area = document.getElementById('search_area');
	var town = document.getElementById('search_town');
	var dist = document.getElementById('search_distance');
	if(area && town)
		town.disabled = (area.options[area.selectedIndex].value != -1) ? false : true;
	if(dist) {
		dist.disabled = true;
		dist.options[0].selected = true;
	}
};

qs_search.distance = function(){
	var town = document.getElementById('search_town');
	var dist = document.getElementById('search_distance');
	if(town && dist)
		dist.disabled = (town.options[town.selectedIndex].value != -1) ? false : true;
};

/*
 * new homelog zip quicksearch
 */

var qs_zip = {};
qs_zip.w = '';

qs_zip.focusZip = function(obj){
	if(qs_zip.w == '') qs_zip.w = obj.value; //init qs_zip.w
	if(obj.value == qs_zip.w) obj.value = '';
};

qs_zip.blurZip = function(obj){
	if(obj.value == '') obj.value = qs_zip.w;
};

qs_zip.initDist = function(flag){
	var dist = document.getElementById('search_distance');
	if(!dist) return;
	dist.disabled = !flag;
	dist.selectedIndex = 0;
}

qs_zip.disableDist = function(){
	qs_zip.initDist(false);
}

qs_zip.enableDist = function(){
	qs_zip.initDist(true);
}

qs_zip.checkZip = function(obj, maxi){
	if(obj.value.length == maxi) geo_quicksearch_distance.geo_quickSearch_distanceSet(obj.value,'div_search_distance', 'qs_zip.enableDist');
	else qs_zip.disableDist();
};

/* EOF */