// JavaScript Document
var rating_reset = false;
function getClientWidthx()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getElementPositionx(elemId)
{
    var elem = document.getElementById(elemId);

    var w = elem.offsetWidth;
    var h = elem.offsetHeight;

    var l = 0;
    var t = 0;

    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}


currentTextarea = '';

function getPopupx(obj_id, pop_id, tpoz, lpoz)
{
	popup = document.getElementById(pop_id);
	obj = document.getElementById(obj_id);

	pos = getElementPositionx(obj_id);
	if ((getClientWidthx()-pos.left)<325)
	{
		popup.style.right = 0+'px';
		popup.style.left = 'auto';
		popup.style.display = 'block';
	}
	else popup.style.left = (pos.left-lpoz)+'px';
	popup.style.top = (pos.top-tpoz)+'px';
	popup.style.display = 'block';
}

function getPopupx2(obj_id, pop_id, tpoz, lpoz, cTextarea)
{
	currentTextarea = cTextarea;
	popup = document.getElementById(pop_id);
	obj = document.getElementById(obj_id);

	pos = getElementPositionx(obj_id);
	if ((getClientWidthx()-pos.left)<325)
	{
		popup.style.right = 0+'px';
		popup.style.left = 'auto';
		popup.style.display = 'block';

	}
	else popup.style.left = (pos.left-lpoz)+'px';
	popup.style.top = (pos.top-tpoz)+'px';
	popup.style.display = 'block';
}


function showBlock(showname)
{
	var sblock = document.getElementById(showname);
	sblock.style.display = 'block';
}


function hideBlock(hidename)
{
		var hblock = document.getElementById(hidename);
		hblock.style.display = 'none';
}


function shBlock(shname)
{
		var shblock = document.getElementById(shname);
		if (shblock.style.display == 'none')
		{
			shblock.style.display = 'block';
		}
		else
			shblock.style.display = 'none';
}


function shClass(shname,cname1,cname2)
{
		var shblock = document.getElementById(shname);
		if (shblock.className == cname1)
		{
			shblock.className = cname2;
		}
		else
			shblock.className = cname1;
}


function changeClass (name, clname)
{
	var chblock = document.getElementById(name);
	chblock.className = clname;
	}

function shloginTXT (sh_id,hd_id)
	   {
		var ss = document.getElementById(sh_id);
		ss.style.display = 'block';
		var hh = document.getElementById(hd_id);
		hh.style.display = 'none';
	   }

// EDIT VIEW

function showActiveBg(st,tab,bg,id_radio)
      {
        for(i = 0; i < st.length; i++)
        {
			var one2 = document.getElementById(st[i]);
			var bodybg = document.getElementById('bgbody');
			var radioID = document.getElementById(id_radio);
			if (one2)
			{
				one2.className = (tab == st[i]) ? 'bg_active':'bg_none';
				bodybg.style.background = bg;
				radioID.checked = true;
			}
        }
      }

function Anons (annlink) {
	var ann = document.getElementById(annlink)
	if (ann.className == 'ddoott') {
		ann.innerHTML = 'Ajouter un anonce';
		}
	else {
		ann.innerHTML = 'Réduire';
		}
	}
function Anons2 (lplus) {
	var plus = document.getElementById(lplus);
	if (plus.innerHTML == 'Lire plus') {
		plus.innerHTML = 'Réduire';
		}
	else {
		plus.innerHTML = 'Lire plus';
		}
	}



// -----------------------------

//  Rating


var is_being_rated = new Array();
var rate_fadein_opacity = 0;
var rate_fadeout_opacity = 100;
var post_id = 0;
var post_rating = 0;
var ratings_max = 0;

function getStyle(elem, prop)
{
  if (typeof elem!="object") elem = document.getElementById(elem);

  if (document.defaultView && document.defaultView.getComputedStyle)
  {
    if (prop.match(/[A-Z]/)) prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
    return document.defaultView.getComputedStyle(elem, "").getPropertyValue(prop);
  }

  if (elem.currentStyle)
  {
    var i;
    while ((i=prop.indexOf("-"))!=-1) prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2);
    return elem.currentStyle[prop];
  }

  return "";
}

function View_rate_list(id)
{
	obj = document.getElementById(id);
	div = obj.getElementsByTagName('DIV')[0]
	if (getStyle(div, "display")!='block')
		obj.style.backgroundImage = 'url(images/bsoo_stars1.gif)';
	setTimeout("vrl_next(obj)", 350);
}

function vrl_next(obj)
{
	obj.className += " act";
	obj.style.backgroundImage = 'url(images/rate_stars_blank.gif)';
	obj.getElementsByTagName('DIV')[0].style.display = 'block';
	id = id - 0;
	if (is_being_rated[id]!= 1)
	{
		is_being_rated[id] = 0;
	}
}

function current_rating(id, rating)
{
	if(is_being_rated[id]!=='1') {
		post_id = id;
		post_rating = rating;
		for(i = 1; i <= rating; i++) {
			document.images['rating_' + post_id + '_' + i].src = 'images/design/rs_full.gif';
			ratings_max = i;
		}
	}
}

function ratings_off(rating_score, insert_half) {
	if(is_being_rated[post_id] !== '1') {
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating_score) {
				document.images['rating_' + post_id + '_' + i].src = 'images/design/rs_full.gif';
			} else {
				document.images['rating_' + post_id + '_' + i].src = 'images/design/rs_blank.gif';
			}
		}
	}
}


function current_rating2(id, rating)
{
	if(is_being_rated[id]!=='1') {
		post_id = id;
		post_rating = rating;
		for(i = 1; i <= rating; i++) {
			document.images['rating_' + post_id + '_' + i].src = 'images/design/rs2_full.gif';
			ratings_max = i;
		}
	}
}

function ratings_off2(rating_score, insert_half) {
	if(is_being_rated[post_id] !== '1') {
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating_score) {
				document.images['rating_' + post_id + '_' + i].src = 'images/design/rs2_full.gif';
			} else {
				document.images['rating_' + post_id + '_' + i].src = 'images/design/rs2_blank.gif';
			}
		}
	}
}

/*function rate_post() {
	if(is_being_rated[post_id]!=='1') {
		is_being_rated[post_id] = '1';
	}

}*/

var ajax_rate = new sack();

function rate_post(uid) {
	uid = uid?uid:rating_user_id;
	//	document.location.href = '.rating-' + post_rating;
	if(is_being_rated[post_id]!=='1') {
		is_being_rated[post_id] = '1';
		ajax_rate.setVar("rating", post_rating);
		ajax_rate.setVar("item_id", post_id);
		ajax_rate.setVar("user_id", uid);
		ajax_rate.requestFile = rating_file;	// Specifying which file to get
		ajax_rate.onCompletion = SetNewRate;	// Specify function that will be executed after file has been found
		ajax_rate.runAJAX();		// Execute AJAX function
	}
}

function View_all_ri(obj1, id1, id2 )
{
	if (obj1.className == 'more')
	{
		obj1.className +=' less';
		obj1.getElementsByTagName('span')[0].innerHTML = 'Less';
		document.getElementById(id2).style.display = 'none';
		document.getElementById(id1).style.display = 'block';
	}
	else
	{
		obj1.className = 'more';
		obj1.getElementsByTagName('span')[0].innerHTML = 'More';
		document.getElementById(id2).style.display = 'block';
		document.getElementById(id1).style.display = 'none';
	}
}

// --------------------------------



// ADD SMILE

function insertSmilie(code) {
//  var myTextArea = document.getElementById(message);
    var myTextArea = document.getElementById(currentTextarea);

    //IE support
    if (document.selection) {
        myTextArea.focus();
        sel = document.selection.createRange();
        sel.text = code;
    }
    //MOZILLA/NETSCAPE support
    else if (myTextArea.selectionStart || myTextArea.selectionStart == "0") {
        var startPos = myTextArea.selectionStart;
        var endPos = myTextArea.selectionEnd;
        var oldText = myTextArea.value;

        myTextArea.value = oldText.substring(0, startPos) + code + oldText.substring(endPos, oldText.length);

    } else {
        myTextArea.value += code;
    }

    myTextArea.focus();
}


function insertPhoto(photo) {
//  var myTextArea = document.getElementById(message);
    var myTextArea = document.getElementById(currentTextarea);
	var inpphoto = document.getElementById(photo);
    //IE support
    if (document.selection) {
        myTextArea.focus();
        sel = document.selection.createRange();
        sel.text = '[img]' + inpphoto.value +'[/img]';
    }
    //MOZILLA/NETSCAPE support
    else if (myTextArea.selectionStart || myTextArea.selectionStart == "0") {
        var startPos = myTextArea.selectionStart;
        var endPos = myTextArea.selectionEnd;
        var oldText = myTextArea.value;

        myTextArea.value = oldText.substring(0, startPos) + '[img]' + inpphoto.value +'[/img]' + oldText.substring(endPos, oldText.length);

    } else {
        myTextArea.value += '[img]' + inpphoto.value +'[/img]';
    }

    myTextArea.focus();
}

function addLink() {
//  var myTextArea = document.getElementById(message);
    var myTextArea = document.getElementById(currentTextarea);
    var name = document.getElementById('linkname').value;
    var url = document.getElementById('linkurl').value;
    if (!url || url == "http://" || url == "https://" )
  	{
  		alert("Indiquez un lien");
  		return false;
  	}
  	else
  	{
		//validate

  		if (!name || name=='')
  		name = url;

    //IE support
    if (document.selection) {
        myTextArea.focus();
        sel = document.selection.createRange();
        sel.text = '[url="' + url +'"]'+name+'[/url]';
    }
    //MOZILLA/NETSCAPE support
    else if (myTextArea.selectionStart || myTextArea.selectionStart == "0") {
        var startPos = myTextArea.selectionStart;
        var endPos = myTextArea.selectionEnd;
        var oldText = myTextArea.value;

        myTextArea.value = oldText.substring(0, startPos) + '[url="' + url +'"]'+name+'[/url]' + oldText.substring(endPos, oldText.length);

    } else {
        myTextArea.value += '[url="' + url +'"]'+name+'[/url]';
    }
	}
    myTextArea.focus();

}

function showSM(sell_panel, sell_tab, smblock, smlink)
      {
        for(i = 0; i < sell_panel.length; i++)
        {
			var one = document.getElementById(sell_panel[i]);
			if (one)
			{
				one.style.display = (smblock == sell_panel[i]) ? 'block':'none';
			}
        }
		for(i = 0; i < sell_tab.length; i++)
        {
			var one2 = document.getElementById(sell_tab[i]);
			if (one2)
			{
				one2.className = (smlink == sell_tab[i]) ? 'sma_active':'sma_deff';
			}
        }
        return false;
      }


// -------------------------------------


// SELECT OTHER REGIONS

function showOther()
		{
		 var sell = document.getElementById('sell_region');
		 for ( var i = 0; i<sell.length; ++i)
		 {
		 if (sell.options[i].selected)
		 	{
				if (sell.options[i].value == 'other')
				{
				getPopupx('sell_region', 'block_region', -30, 318);
				}
		  	}
		  }
		}
		function addOtherRegion()
		{
			var sellRegion = document.getElementById('regionObj');
			var sellOther = document.getElementById('cityObj');
			var sellCountry = document.getElementById('countryObj');
			var sellSearch = document.getElementById('sell_region');
			if (sellCountry.selectedIndex>=0)
			{
				if (sellRegion.selectedIndex>=1)
				{
					if (sellOther.selectedIndex>=1)
					{
						var tvalue;
						var newValue = (sellCountry.options[sellCountry.options.selectedIndex].value+'-'+sellRegion.options[sellRegion.options.selectedIndex].value+'-'+sellOther.options[sellOther.options.selectedIndex].value);
						for (j=0; j<sellSearch.length; ++j)
						{
							if (sellSearch.options[j].value == newValue)
							{
								tvalue = true;
								break;
								}
							else {
								tvalue = false;
								}
							}
						if (tvalue == true)
						{
							sellSearch.options[j].selected = true;
							hideBlock('block_region');
							}
						else {
							var newOpt = document.createElement("option");
							newOpt.text = sellOther.options[sellOther.options.selectedIndex].text + ', ' + sellCountry.options[sellCountry.options.selectedIndex].text;
							var browser_name = navigator.appName;
							if (browser_name == "Microsoft Internet Explorer")
							{
								sellSearch.add(newOpt, sellSearch.length-1);
								}
							else {
								sellSearch.add(newOpt, sellSearch.options[sellSearch.length-1]);
								}
							sellSearch.options[sellSearch.length-2].selected = true;
							sellSearch.options[sellSearch.length-2].value = newValue;
							hideBlock('block_region');
						    }

						}
					else {
						alert('Sélectionner une ville')
					}
				}
				else {
					alert('Sélectionner une région');
				}
			}
			else {
				  alert('Sélectionner un pays');
			}
		}

// ---------------------------------





