function el(ObjID) { return document.getElementById(ObjID); }
function changePage(url){
	document.location=url;
}
function blink_adauga(){
	if(document.getElementById('adauga_link')){
		if(document.getElementById('adauga_link').style.color=="white")
			document.getElementById('adauga_link').style.color="#FF6600";
		else
			document.getElementById('adauga_link').style.color="white";
	}
}
function changePicture(width,height,url) {
	if((parseFloat(width)>450) || (parseFloat(height)>450)){
		if(parseFloat(width)>parseFloat(height)){
			w=450;
			h=450*(height/width);
		}
		else{
			h=450;
			w=450*(width/height);
		}
		el('big_photo_container').innerHTML='<img id="big_photo" name="big_photo" style="margin-left:0px;" height="'+h+'" width="'+w+'" src="/upload/anunturi/photo/'+url+'" />'
	}
	else{
		el('big_photo_container').innerHTML='<img id="big_photo" name="big_photo" style="margin-left:0px;" src="/upload/anunturi/photo/'+url+'" />'
	}
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function exchange() {
	if(el('exchange_sum').value!="") {
		var from=parseFloat(el('exchange_sum').value.replace(",","."));
		var result=from*el('exchange_from').value/el('exchange_to').value;
		result=result.toFixed(2).replace(".",",");
		el('exchange_result').value=addCommas(result);
	} else
		el('exchange_result').value=0;
}

function changeCategory(targ,selObj,restore){ //v3.0
	eval(targ+".location='/adauga_anunt/"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function changeCategoryAlert(targ,selObj,restore){ //v3.0
	eval(targ+".location='/alarme/"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function limitTextarea(limitNumber,Parent,Child) {
    var parentLength=el(Parent).value.length;
    if(parentLength <= limitNumber) {
        el(Child).value=limitNumber-el(Parent).value.length;    
    } else {
        el(Parent).value=el(Parent).value.substr(0,limitNumber);    
    }
}

function elHeight(ObjId) {
	if(document.getElementById(ObjId).clientHeight) {
		var height=document.getElementById(ObjId).clientHeight;
	} else if(document.getElementById(ObjId).offsetHeight) {
		var height=document.getElementById(ObjId).offsetHeight;
	}
	return height;
}

var h=0;
var ResizeIntervalIdOpen=null;
var ResizeIntervalIdClose=null;
var height=null;
function close_box(ObjId,openit){
	if(h>0) {
		h-=20; // Miscarea in pixeli
		el(ObjId).style.height=h.toString()+"px";
	} else {
		clearInterval(ResizeIntervalIdClose);
		height=null;
		el(ObjId).style.height='100%';
		el(ObjId).style.display='none';
		el('opened_div').value="";
		ResizeIntervalIdOpen=null;
		if(openit!=null && openit!=ObjId)
			open_box(openit);
	}
	if(ResizeIntervalIdClose==null)
		ResizeIntervalIdClose=setInterval("close_box('"+ObjId+"','"+openit+"')",10);
}

function open_box(ObjId){
	if(el('opened_div').value!="") {
		close_box(el('opened_div').value,ObjId);
	}
	else {
		if(height==null) {
			el(ObjId).style.display='block';
			height=elHeight(ObjId);
		}
		if(h<height) {
			h+=20; // Miscarea in pixeli
			el(ObjId).style.height=h.toString()+"px";
		} else {
			clearInterval(ResizeIntervalIdOpen);
			ResizeIntervalIdClose=null;
			el('opened_div').value=ObjId;
		}
		if(ResizeIntervalIdOpen==null)
			ResizeIntervalIdOpen=setInterval("open_box('"+ObjId+"')",10);
	}
}

function close_box_preview(ObjId,openit,pozitie,id_tata){
	if(h>0) {
		h-=20; // Miscarea in pixeli
		el(ObjId).style.height=h.toString()+"px";
	} else {
		clearInterval(ResizeIntervalIdClose);
		height=null;
		el(ObjId).style.height='100%';
		el(ObjId).style.display='none';
		el('opened_div').value="";
		ResizeIntervalIdOpen=null;
		if(openit!=null && openit!=ObjId){
			open_box_preview(openit,pozitie,id_tata);
		}
		else{
			for(i=0;i<9;i++){
				el("preview_"+i).style.display="block";
			}
			Ads('inchise','anunturi');
		}
	}
	if(ResizeIntervalIdClose==null)
		ResizeIntervalIdClose=setInterval("close_box_preview('"+ObjId+"','"+openit+"','"+pozitie+"','"+id_tata+"')",10);
}

function open_box_preview(ObjId,pozitie,id_tata){
	for(i=0;i<9;i++){
		el("preview_"+i).style.display="none";
	}
	if(el('opened_div').value!="") {
		close_box_preview(el('opened_div').value,ObjId,pozitie,id_tata);
	}
	else {
		if(height==null) {
			el(ObjId).style.display='block';
			height=elHeight(ObjId);
		}
		if(h<height) {
			h+=20; // Miscarea in pixeli
			el(ObjId).style.height=h.toString()+"px";
		} else {
			clearInterval(ResizeIntervalIdOpen);
			ResizeIntervalIdClose=null;
			el('opened_div').value=ObjId;
			Ads(pozitie,id_tata);
		}
		if(ResizeIntervalIdOpen==null)
			ResizeIntervalIdOpen=setInterval("open_box_preview('"+ObjId+"','"+pozitie+"','"+id_tata+"')",10);
	}
}

var count1 = 0;
var count2 = 0;

function insertOptionBefore(num)
{
  var elSel = document.getElementById('selectX');
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = 'Insert' + num;
    elOptNew.value = 'insert' + num;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function removeOptionSelected()
{
  var elSel = document.getElementById('selectX');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function appendOptionLast(num)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = 'Append' + num;
  elOptNew.value = 'append' + num;
  var elSel = document.getElementById('selectX');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptionLast()
{
  var elSel = document.getElementById('selectX');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}

function ajaxRequest() {
	var ajaxRequest;
		try {
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					alert("Your browser broke!");
					return false;
				}
			}
		}
	return ajaxRequest;
}

function AddToSelected(id){
	var aR=ajaxRequest();
	aR.onreadystatechange=function() {
		if(aR.readyState==1 || aR.readyState==2 || aR.readyState==3) {
		}
		if(aR.readyState==4) {
			el('SelectedAds').innerHTML=aR.responseText;
		}
	}
	aR.open("GET","/Resources/Php/add_to_selected.php?id="+id+"&rand="+parseInt(Math.random()*99999999),true); 	
	aR.send(null);
}

function DeleteSelected(){
	var aR=ajaxRequest();
	aR.onreadystatechange=function() {
		if(aR.readyState==1 || aR.readyState==2 || aR.readyState==3) {
		}
		if(aR.readyState==4) {
			el('SelectedAds').innerHTML=aR.responseText;
		}
	}
	aR.open("GET","/Resources/Php/delete_selected.php?rand="+parseInt(Math.random()*99999999),true); 	
	aR.send(null);
}

function Ads(pozitie,id_tata){
	var aR=ajaxRequest();
	aR.onreadystatechange=function() {
		if(aR.readyState==1 || aR.readyState==2 || aR.readyState==3) {
		}
		if(aR.readyState==4) {
			ads=aR.responseText.split("-><-");
			for(i=0;i<ads.length-1;i++){
				temp=ads[i].split("|--|");
				divid=temp[0];
				divtext=temp[1];
				el('preview_'+divid).style.display="block";
				el('preview_'+divid).innerHTML=divtext;
			}
		}
	}
	aR.open("GET","/Resources/Php/get_preview.php?pozitie="+pozitie+"&id_tata="+id_tata+"&rand="+parseInt(Math.random()*99999999),true); 	
	aR.send(null);
}