/********************************************************************************** 
ScrollText 
* Copyright (C) 2001 Thomas Brattli
* This script was released at DHTMLCentral.com
* Visit for more great scripts!
* This may be used and changed freely as long as this msg is intact!
* We will also appreciate any links you could give us.
*
* Made by Thomas Brattli
*********************************************************************************/

function lib_bwcheck(){ //Browsercheck (needed)
this.ver=navigator.appVersion
this.agent=navigator.userAgent
this.dom=document.getElementById?1:0
this.opera5=this.agent.indexOf("Opera 5")>-1
this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom && !this.opera5)?1:0;
this.ie9=(this.ver.indexOf("MSIE 9")>-1 && this.dom && !this.opera5)?1:0;
this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
this.ie=this.ie4||this.ie5||this.ie6||this.ie7||this.ie8||this.ie9
this.mac=this.agent.indexOf("Mac")>-1
this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
this.ns4=(document.layers && !this.dom)?1:0;
this.bw=(this.ie9 || this.ie8 || this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
return this
}
var bw=new lib_bwcheck()


/*****************

You set the width and height of the divs inside the style tag, you only have to
change the divScrollTextCont, Remember to set the clip the same as the width and height.
You can remove the divUp and divDown layers if you want. 
This script should also work if you make the divScrollTextCont position:relative.
Then you should be able to place this inside a table or something. Just remember
that Netscape crash very easily with relative positioned divs and tables.

Updated with a fix for error if moving over layer before pageload.

****************/


//If you want it to move faster you can set this lower, it's the timeout:
var speed = 30

//Sets variables to keep track of what's happening
var loop, timer

//Object constructor
function makeObj(obj,nest){
nest=(!nest) ? "":'document.'+nest+'.'
this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight
this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight
this.up=goUp;this.down=goDown;
this.moveIt=moveIt; this.x=0; this.y=0;
this.obj = obj + "Object"
eval(this.obj + "=this")
return this
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function moveIt(x,y){
this.x = x
this.y = y
this.css.left = this.x-1
this.css.top = this.y
}

//Makes the object go up
function goDown(move){
if (this.y>-this.scrollHeight+oCont.clipHeight){
this.moveIt(0,this.y-move)
if (loop) setTimeout(this.obj+".down("+move+")",speed)
}
}
//Makes the object go down
function goUp(move){
if (this.y<0){
this.moveIt(0,this.y-move)
if (loop) setTimeout(this.obj+".up("+move+")",speed)
}
}

//Calls the scrolling functions. Also checks whether the page is loaded or not.
function scroll(speed){
if (scrolltextLoaded){
loop = true;
if (speed>0) oScroll.down(speed)
else oScroll.up(speed)
}
}

//Stops the scrolling (called on mouseout)
function noScroll(){
loop = false
if (timer) clearTimeout(timer)
}
//Makes the object
var scrolltextLoaded = false
function scrolltextInit(){
oCont = new makeObj('divScrollTextCont')
oScroll = new makeObj('divText','divScrollTextCont')
//oScroll.moveIt(0,0)
oCont.css.visibility = "visible"
scrolltextLoaded = true
}
//Call the init on page load if the browser is ok...
//if (bw.bw) onload = scrolltextInit

/***************
Multiple Scripts
If you have two or more scripts that use the onload event, probably only one will run (the last one).
Here is a solution for starting multiple scripts onload:
1. Delete or comment out all the onload assignments, onload=initScroll and things like that.
2. Put the onload assignments in the body tag like in this example, note that they must have braces ().
Example: <body onload="initScroll(); initTooltips(); initMenu();">
**************/


var fRunning = 0;
var bigPicPos = 0;

function changeBigPicPos(pn)
{
	bigPicPos = pn;
	if(bigPicPos >= MaxPos)
		bigPicPos = 0;
	applyFilter();
	document.getElementById('bigPic').src = ImgDir+ImagesPath[bigPicPos];
	document.getElementById('bigPicTtl').innerHTML = ImagesTitle[bigPicPos];
	if (bint) {
		window.clearInterval(bint); // clear old interval 
		bint = window.setInterval(changeBigPic, 5000); // start new
	}
}

function applyFilter()
{
	if(!fRunning && document.all) // Apply only 4 IE
	{
		fRunning = 1;
		document.getElementById('bigPic').filters[0].Apply()
		document.getElementById('bigPic').filters[0].Play()
		document.getElementById('bigPicTtl').filters[0].Apply()
		document.getElementById('bigPicTtl').filters[0].Play()
	}
	fRunning = 0;
}

function changeBigPic()
{
	if (!MaxPos)
		return;
		
	bigPicPos++;
	if(bigPicPos >= MaxPos)
		bigPicPos = 0;
	applyFilter();
	document.getElementById('bigPic').src = ImgDir+ImagesPath[bigPicPos];
	document.getElementById('bigPicTtl').innerHTML = ImagesTitle[bigPicPos];
}

function showMap(val)
{
	var map = document.getElementById('map_div');
	map.style.display = (val) ? 'block' : 'none';
}

function datePickerClosed(obj)
{
	var param = {'sid':gBuff.siteID, 'act':'npd', 'date':obj.value};
	JsHttpRequest.query('/js_order.php',param,nightsFinish,false);
}

function nightsFinish(res, txt)
{
	if (res.status == undefined || parseInt(res.status) > 0)
		return alert('שגיאה '+res.status+': '+txt);
	var s = document.getElementById('nsel'), i, lim, pr = parseInt(s.value);
	
	if (lim = parseInt(res.nights)){
		if (lim + 1 < s.options.length)
			s.options.length = lim + 1;
		else
			for(i=s.options.length; i<=lim; i++)
				s.options[i] = new Option(i,i);
		s.disabled = false;
	} else {
		s.options.length = 1;
		s.disabled = true;
	}

	gBuff.rooms = res.rooms;
	gBuff.date = res.date;
	
	if (!parseInt(res.froom)) {
		document.getElementById('cDiv').innerHTML = ' אין חדרים פנויים לתאריך.';
		document.getElementById('rDiv').style.display = 'none';
	} else
		(pr && pr <= lim) ? nightChange(s.value = pr) : nightChange(0);
}

function nightChange(n)
{
	if (!gBuff || !gBuff.rooms)
		return;

	var tmp = [], i, n = parseInt(n), txt = '';

	if (n) {
		for(i in gBuff.rooms)
			if (parseInt(gBuff.rooms[i].mn) >= n)
				tmp[tmp.length] = parseInt(i);
		txt = ' '+tmp.length+' חדרים פנויים.';
	}
	
	document.getElementById('cDiv').innerHTML = txt;

	gBuff.rpd = tmp;
	gBuff.nights = n;

	setRooms();
	document.getElementById('rDiv').style.display = n ? 'block' : 'none';
}

function rScan()
{
	var res = {}, room, i;
	for(i=0; i<gBuff.ind.length; i++){
		room = document.getElementById('rooms_'+gBuff.ind[i]);
		if (room.value > 0)
			res[room.value] = true;
	}
	return res;
}

function setRooms()
{
	if (!gBuff || !gBuff.ind)
		return;

	var rooms = gBuff.rpd, taken = rScan(), i, j, k;

	for(i=0; i<gBuff.ind.length; i++){
		var sel = document.getElementById('rooms_'+gBuff.ind[i]), tval = parseInt(sel.value), real = false, kids = document.getElementById('kids_'+gBuff.ind[i]);
		sel.options.length = 1;
		
		for(j=0; j<rooms.length; j++){
			if (!taken[rooms[j]] || rooms[j] == tval){
				sel.options[sel.options.length] = new Option(gBuff.rooms[rooms[j]].name,rooms[j]);
				if (rooms[j] == tval)
					real = true;
			}
		}
		(real) ? sel.value = tval : taken[tval] = false;
		
		if (kids)
			if (!gBuff.rooms[sel.value] || parseInt(gBuff.rooms[sel.value].mk) <= 0)
				kids.options.length = 1;
			else if (kids.options.length > gBuff.rooms[sel.value].mk + 1)
				kids.options.length = gBuff.rooms[sel.value].mk + 1;
			else
				for(k=kids.options.length; k<=gBuff.rooms[sel.value].mk; k++)
					kids.options[k] = new Option(k,k);
		
		setRoomPrice(sel,gBuff.ind[i],0);
	}
	
	document.getElementById('aDiv').style.display = (gBuff.ind.length >= gBuff.rpd.length) ? 'none' : 'block';

	priceTotal();
}

function setRoomPrice(room, ind, sum)
{
	if (!gBuff)
		return;
	var r = room ? room : document.getElementById('room_'+ind), param;
	
	if (!gBuff.date || !gBuff.date.length || !gBuff.nights || !r || !parseInt(r.value))
		document.getElementById('price_'+ind).value = 0;
	else {
		param = {'sid':gBuff.siteID, 'act':'ppr', 'date':gBuff.date, 'nights':gBuff.nights, 'room':r.value, 'ind':ind, 'sum':sum};
		JsHttpRequest.query('/js_order.php',param,priceFinish,false);
	}
}

function priceFinish(res,err)
{
	if (res.status == undefined || parseInt(res.status) > 0)
		return alert('שגיאה '+res.status+': '+err);

	var s = document.getElementById('extra_'+res.index), i, e = {};
		
	if (res.extras && res.extras.length){
		s.options.length = 1;
		for(i=0; i<res.extras.length; i++){
			s.options[s.options.length] = new Option(res.extras[i].extraTitle,res.extras[i].extraID);
			e[res.extras[i].extraID] = parseInt(res.extras[i].extraPrice);
		}
		s.style.display = 'block';
	} else {
		s.style.display = 'none';
	}
		
	gBuff.rooms[res.roomID].price = {'base':parseInt(res.price.base), 'kids':parseInt(res.price.kid), 'extras':e};
	updRoomPrice(res.index,res.sum);
}

function updRoomPrice(index, total)
{
	var k = document.getElementById('kids_'+index), r = document.getElementById('rooms_'+index), p = document.getElementById('price_'+index), e = document.getElementById('extra_'+index), price = gBuff.rooms[r.value].price;
	p.value = (price) ? price.base + (k ? parseInt(k.value) : 0) * (price.kids >= 0 ? price.kids : 0) + (parseInt(e.value) ? price.extras[e.value] : 0) : 0;
	if (total)
		priceTotal();
}

function priceTotal()
{
	if (!gBuff || !gBuff.ind)
		return;
	
	var t = 0, p, f = document.getElementById('price_all');
	
	for(i=0; i<gBuff.ind.length; i++)
		t += parseInt(document.getElementById('price_'+gBuff.ind[i]).value);
	f.value = t;
}

function addRoom()
{
	var tbl = document.getElementById('roomtbl'), c = tbl.rows.length-1, row = tbl.insertRow(c+1), td = document.createElement("TD"), i, fam = gBuff.family_zimer ? 6 : 2;

	var inp = create_select('rooms['+c+']','rooms_'+c,'- - - - - - - - - - -','sbp_select4');
	inp.onchange = setRooms;
	td.appendChild(inp);
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);
	
	td = document.createElement("TD");
	inp = create_select('adults['+c+']','adults_'+c,' - ','sbp_select2');
	for(i=1; i<=fam; i++)
		inp.options[inp.options.length] = new Option(i,i);
	inp.options[2].selected = true;
	td.appendChild(inp);
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);

	td = document.createElement("TD");
	if (gBuff.family_zimer){
		inp = create_select('kids['+c+']','kids_'+c,' - ','sbp_select2');
		inp.onchange = function(){updRoomPrice(c, true)}
		td.appendChild(inp);
	} else
		td.innerHTML = '&nbsp;&nbsp;-';
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);
	
	td = document.createElement("TD");
	if (gBuff.family_zimer){
		inp = create_select('infants['+c+']','infants_'+c,' - ','sbp_select2');
		for(i=1; i<=4; i++)
			inp.options[inp.options.length] = new Option(i,i);
		td.appendChild(inp);
	} else
		td.innerHTML = '&nbsp;&nbsp;-';
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);

	td = document.createElement("TD");
	td.className = 'extra_td';
	inp = create_select('extra['+c+']','extra_'+c,'- - - - - - - - - - - -','extra_ord');
	inp.style.display = 'none';
	inp.onchange = function(){updRoomPrice(c, true)}
	td.appendChild(inp);
	row.appendChild(td);
	td = document.createElement("TD");
	row.appendChild(td);

	td = document.createElement("TD");
	inp = create_input(null,'price_'+c,'text','0','sbp_price');
	td.appendChild(inp);
	row.appendChild(td);
	
	gBuff.ind[gBuff.ind.length] = c;
	if (gBuff.ind.length >= gBuff.rpd.length)
		document.getElementById('aDiv').style.display = 'none';
	setRooms();
}

function create_input(name, id, type, value, css)
{
	var cell = document.createElement('INPUT');
	if (name)
		cell.name = name;
	cell.id = id;
	cell.type = type;
	cell.value = value;
	cell.className = css;
	return cell;
}

function create_select(name, id, def_txt, css)
{
	var cell = document.createElement('SELECT');
	cell.name = name;
	cell.id = id;
	cell.className = css;
	cell.options[0] = new Option(def_txt,0);
	return cell;
}

function makeOrder()
{
	var param = document.getElementById('rForm');
	JsHttpRequest.query('/js_order.php',param,orderFinish,true);
}

function orderFinish(res,txt)
{
	if (res.status == undefined || parseInt(res.status) > 0)
		return alert('שגיאה '+res.status+': '+txt);
	location.href = res.link;
}

function completeOrder()
{
	var param = document.getElementById('cForm');
	JsHttpRequest.query('/js_order.php',param,orderFinish,true);
}

