/*

$ = function(idd)
{
	obj=$(idd);
	if(obj!=null) return obj
	else return null;
}
*/

function tabCh(who, imgSrc)
{
who.src = imgSrc;
}


//selectMenu(document.getElementById('manageproperties'), menu, 'submenucontainer', 'guestreviews', submenu)
function selectMenu(thisObj, menuvar, smcontainer, submid, menuvar2)
{
	try
	{
	$(menuvar["selected"]).src = menuvar[menuvar["selected"]]["out"];
	$(menuvar["selected"]).onmouseover = function(){tabCh(this, menu[this.id]["ovr"]);};
	$(menuvar["selected"]).onmouseout = function(){tabCh(this, menu[this.id]["out"]);};
	}
	catch(e){};
	menuvar["selected"] = thisObj.id;

	$(menuvar["selected"]).onmouseover = function(){};
	$(menuvar["selected"]).onmouseout = function(){};
	$(smcontainer).innerHTML = menuvar[menuvar["selected"]]["sm"]
	$(menuvar["selected"]).src = menuvar[menuvar["selected"]]["ovr"];
	//alert(submid);
	try{if(submid != undefined && menuvar2 != undefined) selectSubmenu($(submid), menuvar2);}
	catch(e){};
}

function selectSubmenu(thisObj, menuvar)
{
	try
	{
		$(menuvar["selected"]).className = menuvar["unsel"];
	}
	catch(e){};

	menuvar["selected"] = thisObj.id;
	$(menuvar["selected"]).className = menuvar["sel"];
}

submenu={
		"selected":'',
		"unsel":"submenu", 
		"sel":"submenusel"
	}

function gotoPage(addr)
{
	document.location.href = addr;
}

/*<Modal Window functions>*/
function getTop(elem)
{
	if (elem.offsetParent)
		{
			return elem.offsetTop + getTop(elem.offsetParent);
		}
	else
		{
			return elem.offsetTop;
		}
}

function getLeft(elem)
{
	if (elem.offsetParent)
	{
		return elem.offsetLeft + getLeft(elem.offsetParent);
	}
	else
	{
		return elem.offsetLeft;
	}
}

function dispDiv(vis)
{
	if(vis==0) {
		$('opaque').style.display='none';
	} else {
		$('opaque').style.display='block';
	};
	
	$('opaque').style.width = $('pagecontent').offsetWidth + "px";
	$('opaque').style.height = $('pagecontent').offsetHeight + "px";
}

function dispDivWhereSliders(vis)
{
	if (!document.attachEvent) {
		if(vis==0) {
			$('opaque').style.display='none';
		} else {
			$('opaque').style.display='block';
		};
		
		$('opaque').style.width = $('pagecontent').offsetWidth + "px";
		$('opaque').style.height = $('pagecontent').offsetHeight + "px";
	}
}

function posFerModala(targetId, objRef, dX, dY)
{
	try
	{
		$(targetId).style.left = getLeft(objRef) + parseInt(dX) + "px";//objRef.offsetWidth
	}
	catch(e){};
	try
	{
		$(targetId).style.top = getTop(objRef) + parseInt(dY) + "px";//40 
	}
	catch(e){};
}

//LB - Not really sure about this, cant tell what the arguments and element names refer to.
//Also dont know what posFelModala is.  Making a new showWindowAtFront() because the new tooltips
//for the booking makeover overlap divs that are shown with this function
function showWindow(thiss, idd, bgId, dX, dY)
{
	$(idd).style.display="block";
	$(bgId).style.display="block";
	
	try
	{
		$(idd).style.opacity=1.0;
	}
	catch(e){};
	try
	{
		$(idd).filters.alpha.opacity=100;
	} catch(e){};
	$(bgId).style.display="block";
	
	//LB - I added this because the new tooltips are display over the windows no mater what i do.  This should fix them no matter what.
	//RObert - Removed because position of window are wrecked
	/*document.body.insertBefore($(idd), document.body.childNodes[0]);*/
	if (typeof hideTooltips == "function") hideTooltips();
	
	posFerModala(idd, thiss, dX, dY);
}

function showWindowAtFront(element_id)
{
	var el = $(element_id);
	el.setOpacity(1.0);
	el.style.display = "block";
	
	var left = 0-Position.page(document.body)[0] + el.getWidth()/2;
	var top = 0-Position.page(document.body)[1] + el.getHeight()/3;
	
	el.style.left = left + "px";
	el.style.top = top + "px";
	
	document.body.insertBefore(el, document.body.childNodes[0]);
}

function closeLightbox(){
	$('loading').hide();
	$('loading_opaque').hide();

}


function showWindowAtFrontAjax(url, width, height, afterFunct)
{
  new Ajax.Request(url, {onSuccess: function(response){_showAjaxWindow(response, afterFunct);}});

}

function _showAjaxWindow(response, afterFunct)
{
  if (!$('rbo_opaque')) {
  	var opaque = document.createElement('div');
  	opaque.id = 'rbo_opaque';
	document.body.appendChild(opaque);
  }
  
  var box = document.createElement('div');
  box.id = "ajax_div";

  
  Position.absolutize(box);
  box.style.zIndex = '10000';
  box.style.width = '700px';

    $('rbo_opaque').style.display = "block";
  $('rbo_opaque').style.height = document.documentElement.scrollHeight + "px";
	  box.innerHTML = response.responseText;
  document.body.appendChild(box);
  document.body.insertBefore(box, document.body.childNodes[0]);
  
  
	// retrieve required dimensions
	var eltDims = box.getDimensions();
	var browserDims = document.body.getDimensions();

	
	// calculate the center of the page using the browser and element dimensions
	var y = (browserDims.height - eltDims.height) / 2;
	var x = (browserDims.width - eltDims.width) / 2;
	
	// set the style of the element so it is centered
	var styles = { 
		position : 'absolute',
		top :  (Position.page(document.body)[1]*-1 + 100)  + 'px',
		left : x + 'px',
		display: 'none'
		};
	
	box.setStyle(styles);
	if (afterFunct != null)
		Effect.Appear(box, {afterFinish: afterFunct});
	else
		Effect.Appear(box);
}

function showAjax(url)
{
  new Ajax.Request(url, {onSuccess: function(response){showLoadingWithoutBR(response.responseText);}});
}

function showWindowWhereSliders(thiss, idd, bgId, dX, dY)
{
	if (!document.attachEvent) {
		$(idd).style.display="block";
		$(bgId).style.display="block";
		try
		{
			$(idd).style.opacity=1.0;
		}
		catch(e){};
		try
		{
			$(idd).filters.alpha.opacity=100;
		} catch(e){};
		$(bgId).style.display="block";
	} else {
//		disableSliders();
		$(idd).style.display="block";
	}
	posFerModala(idd, thiss, dX, dY);
	
}
//showWindow(this, 'window', 'opaque');

function hideWindow(idd, bgId)
{
	$(idd).style.display="none";
	$(bgId).style.display="none";
}


/*Fade In, Fade out*/
var itv;
function fadeOut(cine, lmt, tmp, fps) {
	try{clearInterval(itv)}catch(e){};
	if(isNaN(parseFloat(document.getElementById(cine).style.opacity))) {
		document.getElementById(cine).style.opacity=1.0;
	}
	if(parseFloat(document.getElementById(cine).style.opacity)>lmt) {
		try{clearInterval(itvl);clearInterval(itv);}
		catch(e){};
		var nrstp=(tmp/1000)*fps;
		var stp=(document.getElementById(cine).style.opacity-lmt)/nrstp;
		stp=parseInt(stp*100)/100;
		itv=setInterval('fo("'+cine+'", '+lmt+', '+stp+')', 1000/fps);
	}
}

function fadeOutWhereSliders(cine, lmt, tmp, fps) {

	if (!document.attachEvent) {
		try{clearInterval(itv)}catch(e){};
		if(isNaN(parseFloat(document.getElementById(cine).style.opacity))) {
			document.getElementById(cine).style.opacity=1.0;
		}
		if(parseFloat(document.getElementById(cine).style.opacity)>lmt) {
			try{clearInterval(itvl);clearInterval(itv);}
			catch(e){};
			var nrstp=(tmp/1000)*fps;
			var stp=(document.getElementById(cine).style.opacity-lmt)/nrstp;
			stp=parseInt(stp*100)/100;
			itv=setInterval('fo("'+cine+'", '+lmt+', '+stp+')', 1000/fps);
		}
	} else {
		document.getElementById(cine).style.display = 'none';
		enableSliders();
		hideWindow(cine,'opaque');
	}

}

function fo(c, l, s)
{
  if((parseFloat(document.getElementById(c).style.opacity)-s)>l)
   {
    try{document.getElementById(c).style.opacity = parseFloat(document.getElementById(c).style.opacity) - parseFloat(s);} catch(e){};
	try{document.getElementById(c).filters.alpha.opacity = parseFloat(document.getElementById(c).style.opacity)*100;} catch(e){};
   }
  else
   {
    try{document.getElementById(c).style.opacity=l;} catch(e){};
	try{document.getElementById().filters.alpha.opacity=l*100} catch(e){};
    try{clearInterval(itv);} catch(e){alert(e+" Eroare la clearInterval")}

	if(l==0)
	{
	hideWindow(c, 'opaque');
	}
   }
}
/*</Modal Window functions>*/
function toggledisplay(idd)
{
	if(document.getElementById(idd).className=="displaynone")
	{
		document.getElementById(idd).className="displayblock";
	}
	else
	{
		document.getElementById(idd).className="displaynone";
	}
}
//fadeOut('window', 0.0, 1000, 12)

function selectById(a, idd, currentClass, exClass)
{
	try
	 {
	  document.getElementById(idd).className=exClass;
	  document.getElementById(idd).id='';
	 }
	catch(e){};
	
	a.id=idd;
	document.getElementById(idd).className=currentClass;
}

photo1='<table width="100%" border="0" cellspacing="0" cellpadding="0">\
                            <tr>\
                              <td class="txtbold">Photo ';

photo2='</td>\
<td width="217">&nbsp;</td>\
</tr>\
<tr>\
<td><input type="file" name="photo[';
								   
photo3=']" ></td>\
<td>Optional description:</td>\
</tr>\
<tr>\
<td>&nbsp;</td>\
<td><textarea name="textfield2" class="propertydescription"></textarea></td>\
</tr>\
<tr>\
<td colspan="2"><hr size="1" noshade="noshade" color="#D07828" class="hr1" /></td>\
</tr>\
</table>';

/*------------------*/
photocount=1;
function insertPhoto(contId)
{
	photocount++;
	document.getElementById(contId).innerHTML += photo1 + photocount + photo2 + photocount + photo3;
}
/*------------------*/


function USSpecific(thiss)
{
	if(thiss.value=="US")
	{
		for(var i=1; i<USSpecific.arguments.length; i++)
		{
			try{document.getElementById(USSpecific.arguments[i]).style.display="table-row";}
			catch(e){try{document.getElementById(USSpecific.arguments[i]).style.display="block";}catch(e){}};
		}
	}
	else
	{
		for(var i=1; i<USSpecific.arguments.length; i++)
		{
			document.getElementById(USSpecific.arguments[i]).style.display="none";
		}

	}
}
function showObj(obj)
{
	obj.style.display='block';
}

function hideObj(obj)
{
	obj.style.display='none';
}

function ajaxReq(fileAddr, dest)
{
 window.status=fileAddr;
 var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 req.open('POST', fileAddr, true);
 req.onreadystatechange = function()
 {
  if(req.readyState == 4)// && req.status == 200 -> this thing works (and only have sense) only on server
   {
    if(req.responseText.length>0)
     {
      dest.innerHTML=req.responseText
     }
    else
     {
      alert('Eroare: Nu se poate stabili conexiunea.');
     }
   }
 }
 req.send(null);
}

function getXMLHTTP() { //fuction to return the xml http object 
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
	 	
	return xmlhttp;
}

function getState(countryId, targetElement , webRoot) {		
	
	var strURL=webRoot+"properties/getState/"+countryId+"/";
	var req = getXMLHTTP();
	
	if (req) {
		
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					eval(req.responseText);
					document.getElementById(targetElement).innerHTML = '';
					for (i in xArray) {
						var oOption = document.createElement("OPTION");
						oOption.text=xArray[i];
						oOption.value=i;
						if (navigator.userAgent.toLowerCase().indexOf("msie")!=-1) {
							document.getElementById(targetElement).add(oOption);
						} else {
							document.getElementById(targetElement).appendChild(oOption);
						}

						
					}

					if (req.responseText.length == 28) {
						$(targetElement).disabled = 'disabled';
					} else {
						$(targetElement).disabled = false;
					}
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			

		req.open("GET", strURL, true);
		req.send(null);
	}		
}
