//////// 
//////// MAIN

function af_check(_id) {
	_status = document.getElementById("checked-" + _id).checked;
	_i = 0;
	if(_status == true) {
		document.getElementById("active-" + _id).style.border = "1px solid #FF9900";
		document.getElementById("img-favorites").src = "/book/?s=t&Property_Id=" + _id;
		_i = 1;
	}
	if(_status == false) {
		document.getElementById("active-" + _id).style.border = "1px solid #FFFFFF";
		document.getElementById("img-favorites").src = "/book/?s=f&Property_Id=" + _id;
		_i = -1;
	}
	
	document.getElementById("booking-num").value = parseInt(document.getElementById("booking-num").value) + _i;
}
function scroll_pic(_id, _status) {

	_position = parseInt(document.getElementById("gpic-" + _id + "-cnt-s").value);	
	_position_total = parseInt(document.getElementById("gpic-" + _id + "-cnt-total").value);

	if(_status == "next") {		
		_position_new = _position + 1;
		if(!document.getElementById("gpic-" + _id + "-cnt-" +_position_new)) {
			_position_new = 1;
		}
		_next_img = document.getElementById("gpic-" + _id + "-cnt-" + _position_new).value;
	}
	if(_status == "prev") {		
		_position_new = _position - 1;
		if(!document.getElementById("gpic-" + _id + "-cnt-" +_position_new)) {
			_position_new = _position_total;
		}
		_next_img = document.getElementById("gpic-" + _id + "-cnt-" + _position_new).value;
	}
	if(_next_img) {
		document.getElementById("img-main-" + _id).style.background = "url(/img/pics/ico_loading_150x113.gif)";	
		document.getElementById("gpic-" + _id + "-cnt-s").value = _position_new;
	}	
	var image = new Image();
	image.onload = function() {
	    document.getElementById("img-main-" + _id).style.background = "url(" + _next_img + ") no-repeat";	
	};
	image.src = _next_img;	
}	

//////// 
//////// PROPERTY

		function changeObjectBlock(s_val) {
			_p1 = document.getElementById("href-object-description");
			if(_p1) {	_p1.style.color = ""; document.getElementById("object-description").style.display = "none";	}
			
			_p2 = document.getElementById("href-object-price");
			if(_p2) {	_p2.style.color = ""; document.getElementById("object-price").style.display = "none";	}
			
			_p3 = document.getElementById("href-object-photos");
			if(_p3) {	_p3.style.color = ""; document.getElementById("object-photos").style.display = "none";	}
			
			_p4 = document.getElementById("href-object-contact");
			if(_p4) {	_p4.style.color = ""; document.getElementById("object-contact").style.display = "none";	}
			
			_p5 = document.getElementById("href-object-calculate");
			if(_p5) {	_p5.style.color = ""; document.getElementById("object-calculate").style.display = "none";	}
			
			_p6 = document.getElementById("href-object-calendar");
			if(_p6) {	_p6.style.color = ""; document.getElementById("object-calendar").style.display = "none";	}
			
			_p7 = document.getElementById("href-object-response");
			if(_p7) {	_p7.style.color = ""; document.getElementById("object-response").style.display = "none";	}	
			
			document.getElementById(s_val).style.display = "";
			document.getElementById("href-" + s_val).style.color = "red";
			return true;
		}
		
		function add_favorites(_href) {
			window.open(_href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no');
			return false;
		}

		function PropertyBook() {
			book_window = window.open("/book/","book_window","scrollbars=yes,resizable=yes,width=700,height=500,left=150,top=100");
			document.book_form.target = "book_window";
			document.book_form.submit();
			return true;
		}


		function getCurrSymbol(lscurr)  {
			var lsOut;
			switch(lscurr)  {
				case "INR": lsOut = "INR"; break;
				case "RUB": lsOut = "РУБ"; break;
				case "AUD": lsOut = "AU$"; break;
				case "USD": lsOut = "$"; break;
				case "EUR": lsOut = "&euro;"; break;
				case "GBP": lsOut = "&pound;"; break;
				case "ZAR": lsOut = "R";break;
				case "CAD": lsOut = "CA$";break;
				case "CHF": lsOut = "CHF";break;
			}
			return lsOut;
		}

		function roundprice(rprice, rbpence) { 
			//rprice [the full price to be rounded]
			//rbpence flag to specify to round to pence(3.99) or to pound(3)[  1=pence 0=pounds ]
			//rprice=222.5; rbpence=1;
			
			//alert('roundpence() rprice='+rprice + ' rbpence='+rbpence);
			var mystr = rprice.toString();
			var mypos=mystr.indexOf(".");
			//alert('roundpence() mypos='+mypos);
			if (mypos==-1)  {
				retval=mystr;
				//Put comma in to represent thousand separator - what a pain!!
				//alert('leaving here');
				
				
				if (rbpence==1)   {
					//No decimal place, so add the .00
					retval=mystr+'.00'
				}
			}
			else  {	
			
				if (rbpence==1)  {
					//Add a ZERO if only one number after the decimal place
					//Get the bit before the decimal place
					var after = mystr.slice(mypos+1);
					//alert('a) after='+after);
					if (after.length==1)  {  //dp+1digit==2
						after=after+'0';
						//alert('b) after='+after);
					}
					else  {
						//Get 3 digits (if they exist) so we can round properly
						//alert('after=' + after);
						
						if (after.length > 2 )  {
							after=after.slice(0,2) + '.' + after.slice(2,3);
							//Rounds to 2 dp
							after=Math.round(after);
						}
						//alert('after slice=' + after);
					}
					retval=mystr.slice(0,mypos) + '.' + after;
				}
				else  {
					//Just return the bit before the decimal place
					//retval=mystr.slice(0,mypos);
					retval=Math.round(mystr)
				}
				
				
			}
			//alert('roundpence() retval='+retval);
			return addCommas(retval);
		}

		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 update_ccy(from_ccy, to_ccy){
				
			for (var i = 1; i <= 30; i ++)  {						
				
					if(document.getElementById('RUB_'+i) == null) {
						break;	
					}
					
					var VAL_IN    =	document.getElementById('RUB_'+i);
					var VAL_USER  =	document.getElementById('USER_'+i);
					var VAL_CUR   =	document.getElementById('VAL_'+i);
					
					var User_Rate = Currency_Rates[to_ccy];
					if(to_ccy == "RUB") User_Rate = 1;
					
					VAL_USER.innerHTML = roundprice(VAL_IN.innerHTML * User_Rate, 0);
					VAL_CUR.innerHTML  = getCurrSymbol(to_ccy);
			}
			
			//Save their currency preference
			save_preferred_ccy(to_ccy);
			return true;
		}

		function save_preferred_ccy(saveccy)  {
			url_iframe = "/db/save_ccy.php?CCY=" + saveccy;
			document.getElementById("save_ccy_frame").src = url_iframe;
		}	
