// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Ultimate client-side JavaScript client sniff. Version 3.03
// (C) Netscape Communications 1999-2001.  Permission granted to reuse and distribute.
// http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
var is_opera = (agt.indexOf("opera") != -1);
var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

function showEditWindow(src,wname,w,h,s,st,f,r)
{
	if (src=="#") return
	else
	{
		w = w ? w : 600;
		h = h ? h : 500;
		s = s ? s : "yes";
		st = st ? st : "yes";
		f = f ? f : "no";
		r = r ? r : "yes";
		wname = wname ? wname : "newWin"+(Math.floor(Math.random()*100000));
		var winl = (screen.width - w) / 2;
		var wint = (screen.height / 2) - h;
		winprops = "Fullscreen="+f+",height="+h+",width="+w+",top="+wint+",left="+winl+
		           ",toolbar=no,status="+st+",scrollbars="+s+",location=0,menubar=0,directories=0,resizable="+r;
		window.open(src,wname,winprops);

	}
}

var allchecked;

function checkAll( theForm) {
  if (allchecked) { allchecked = false; }
    else { allchecked = true; }
  for (var i=0;i<theForm.elements.length;i++)
  {
    var e = theForm.elements[i];
      e.checked = allchecked;
  }

}

function setSc(v)
{
	if (v) document.body.scrollTop=v;
}
function writeSc(frm)
{
	obj=eval("document.forms."+frm);
	if (obj) obj.scroll_value.value=document.body.scrollTop;
}

// [k] getElementById wrapper for x-browser comp.
if(document.all && !document.getElementById) {
	document.getElementById = function(id) {
		return document.all[id];
	}
}

var isExplorerBrowser = navigator.appName.indexOf("Microsoft") != -1;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

function objShow(objname) {
	obj = document.getElementById(objname);
	obj.style.visibility = 'visible';
	obj.style.position = '';
	obj.style.left = '';
	obj.style.top = '';
	obj.style.height = '';
	obj.style.width = '';
	obj.style.display = '';
}

function objHide(objname) {
	obj = document.getElementById(objname);
	obj.style.visibility = 'hidden';
	obj.style.position = 'absolute';
	obj.style.left = '0px';
	obj.style.top = '0px';
	obj.style.height = '1px';
	obj.style.width = '1px';
	obj.style.display = 'none';
}

function setClass(objname, clsname) {
	obj = document.getElementById(objname);
	obj.className = clsname;
}

function setStyle(objname, prop, value) {
	obj = document.getElementById(objname);
	eval("obj.style."+prop+" = '"+value+"';");
}

// [k] sets cookie
function setCookie(NameOfCookie, value, expiredays, path, domain, secure) {
	var ExpireDate = new Date();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays*24*3600*1000));
	document.cookie = NameOfCookie +"="+ escape(value) + ((expiredays == null)? "": ";expires="+ ExpireDate.toGMTString()) +((path == null)? "": (";path=" + path)) +((domain == null) ? "" : (";domain=" + domain)) +((secure == true) ?";secure":"");
}

// [k] gets cookie
function getCookie(NameOfCookie) {
	if(document.cookie.length > 0) {
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length + 1;
			end = document.cookie.indexOf(";",begin);
			if(end == -1) end = document.cookie.length;
			//alert('Cookie: '+NameOfCookie+'='+unescape(document.cookie.substring(begin,end)));
			return unescape(document.cookie.substring(begin,end));
		}
	}
	return null;
}

function saveScrollPosition(obj, prefix) {
	xpos = obj.scrollTop;
	ypos = obj.scrollLeft;
	setCookie(prefix+'_xpos', xpos, '365', '/');
	setCookie(prefix+'_ypos', ypos, '365', '/');
}

function loadScrollPosition(obj, prefix) {
	xpos = getCookie(prefix+'_xpos');
	ypos = getCookie(prefix+'_ypos');
	if ((xpos!='') && (ypos!='')) {
		obj.scrollTop=xpos;
		obj.scrollLeft=ypos;
//		obj.scrollTo(xpos, ypos);
	}
}

function saveSearchTabPosition(tabid) {
	setCookie('us4searchtab', tabid, '1', '/');
}

function getSearchTabPosition() {
	return getCookie('us4searchtab');
}

function savePath() {
	path = top.location.href;
	setCookie('us4c_pathcache', path, '365', '/');
/*	
	if (saveScroll==true) {
*/
	saveScrollPosition(document.body, 'us4c');
/*
	} else {
		setCookie('us4c_xpos', '0');
		setCookie('us4c_ypos', '0');
	}
*/
//	alert(path+' saved');
}

function goBack() {
	path = getCookie('us4c_pathcache');
	if (path!='') {
//		alert(path);
		setCookie('us4c_pathcache', path, '365', '/');
		xpos = getCookie('us4c_xpos');
		ypos = getCookie('us4c_ypos');
		setCookie('us4c_scrollnowto_xpos', xpos, '365', '/');
		setCookie('us4c_scrollnowto_ypos', ypos, '365', '/');
		top.location.href = path;
	}
}

function checkForScrollSaved() {
	if ((
		(getCookie('us4c_scrollnowto_xpos')!='') && 
		(getCookie('us4c_scrollnowto_xpos')!='0')
		) || (
		(getCookie('us4c_scrollnowto_xpos')!='') && 
		(getCookie('us4c_scrollnowto_xpos')!='0'))) {

		// apply and then reset scroll values
		// but apply only if we are on the right page
		if (top.location.href==getCookie('us4c_pathcache')) {
			loadScrollPosition(document.body, 'us4c_scrollnowto');
			setCookie('us4c_scrollnowto_xpos', '0', '365', '/');
			setCookie('us4c_scrollnowto_ypos', '0', '365', '/');
		}
	}
}

addLoadEvent(checkForScrollSaved);

function modSearchChecks(obj, targname) {
	var cbs=document.getElementsByName('hotelsearch_'+targname+'[]');
	for (var i=0; i<cbs.length; i++) {
		if (obj.checked) {
			cbs[i].checked = true;
		} else {
			cbs[i].checked = false;
		}
	}
}

function applySearchCheckList(targname) {
	listobj = document.getElementById('hotelsearch_'+targname+'list');
	list = listobj.value+',';
	var cbs=document.getElementsByName('hotelsearch_'+targname+'s[]');
	for (var i=0; i<cbs.length; i++) {
		if (targname=='area') {
			thiscb = cbs[i].value.substr(0,cbs[i].value.indexOf("#"));
		} else {
			thiscb = cbs[i].value;
		}
		if (list.indexOf(thiscb+',')!=-1) {
			cbs[i].checked = true;
		} else {
			if (listobj.value=='') {
				cbs[i].checked = true;
			} else {
				cbs[i].checked = false;
			}
		}
	}
}

function createSearchCheckList(targname) {
	list = "";
	var cbs=document.getElementsByName('hotelsearch_'+targname+'s[]');
	for (var i=0; i<cbs.length; i++) {
		if (cbs[i].checked) {
			if (targname=='area') {
				thiscb = cbs[i].value.substr(0,cbs[i].value.indexOf("#"));
			} else {
				thiscb = cbs[i].value;
			}
			list = ( (list=='') ? "" : list + "," ) + thiscb;
		}
	}
	listobj = document.getElementById('hotelsearch_'+targname+'list');
	listobj.value = list;
}

function matchAreasToRegions(regobj) {
	if (regobj=='all') {
		var regioncbs=document.getElementsByName('hotelsearch_regions[]');
		for (k=0; k<regioncbs.length; k++) {
			var areacbs=document.getElementsByName('hotelsearch_areas[]');
			for (var j=0; j<areacbs.length; j++) {
				thisreg = areacbs[j].value.substr(areacbs[j].value.indexOf("#")+1);
				if (thisreg==regioncbs[k].value) {
					areacbs[j].checked = regioncbs[k].checked;
				}
			}
		}
	} else {
		var areacbs=document.getElementsByName('hotelsearch_areas[]');
		for (var j=0; j<areacbs.length; j++) {
			thisreg = areacbs[j].value.substr(areacbs[j].value.indexOf("#")+1);
			if (thisreg==regobj.value) {
				areacbs[j].checked = regobj.checked;
			}
		}
	}
}

function matchRegionsToAreas(obj) {
	var areacbs=document.getElementsByName('hotelsearch_areas[]');
	var regioncbs=document.getElementsByName('hotelsearch_regions[]');
	if (obj=='all') {
		for (var k=0; k<areacbs.length; k++) {
			areg = areacbs[k].value.substr(areacbs[k].value.indexOf("#")+1);
			for (var j=0; j<regioncbs.length; j++) {
				if (areg==regioncbs[j].value) {
					thisregstatus = "";
					for (var i=0; i<areacbs.length; i++) {
						thisareg = areacbs[i].value.substr(areacbs[i].value.indexOf("#")+1);
						if (thisareg==areg) {
							if (areacbs[i].checked) {
								thisregstatus += "+";
							} else {
								thisregstatus += "-";
							}
						}
					}
					if (thisregstatus.indexOf("-")==-1) {
						// all checked
						regioncbs[j].checked = true;
					} else if (thisregstatus.indexOf("+")==-1) {
						// all unchecked
						regioncbs[j].checked = false;
					} else {
						// mixed
						regioncbs[j].checked = true;
					}
				}
			}
		}
	} else {
		areg = obj.value.substr(obj.value.indexOf("#")+1);
		for (var j=0; j<regioncbs.length; j++) {
			if (areg==regioncbs[j].value) {
				thisregstatus = "";
				for (var i=0; i<areacbs.length; i++) {
					thisareg = areacbs[i].value.substr(areacbs[i].value.indexOf("#")+1);
					if (thisareg==areg) {
						if (areacbs[i].checked) {
							thisregstatus += "+";
						} else {
							thisregstatus += "-";
						}
					}
				}
				if (thisregstatus.indexOf("-")==-1) {
					// all checked
					regioncbs[j].checked = true;
				} else if (thisregstatus.indexOf("+")==-1) {
					// all unchecked
					regioncbs[j].checked = false;
				} else {
					// mixed
					regioncbs[j].checked = true;
				}
			}
		}
	}
}

function enableAllFields(theForm)
{
    for(i=0; i<theForm.elements.length; i++)
    {
        theForm.elements[i].disabled = false;
    }
    return true;
}

var o = Object;

function btnModalWin(id, width, height)
{
  o = FTab(id,false,false,width,height,'modal:1;cookie:0;center:1;status:1;resize:0;minmax:0;close:1;move:1;');
  o.show(0);
}



function addnewdiv(divparent, divbefore, divtoclone, removetaglike, divcount)
{
	var newdiv = divtoclone.cloneNode(true);
	newdiv.id = "clonediv"+Math.random();
	if (divparent)
	{
		if (divbefore)
		{
			if (divbefore.nextSibling) {
				divparent.insertBefore(newdiv, divbefore.nextSibling);
			} else {
				divparent.appendChild(newdiv);
			}	
		}
		else {
			divparent.appendChild(newdiv);
		}	
		if (newdiv)
		{//clear node childs
			for (i=0; i<newdiv.childNodes.length; i++)
			{
				if (newdiv.childNodes[i].tagName == removetaglike)
				{
					newdiv.removeChild(newdiv.childNodes[i]);
					i--;
				}
			}
		}//divparent.insertBefore(newdiv, divbefore);
		divcount++;
	}
	return divcount;
}

function getdivposinparentlist(thisdiv)
{
	var pos = 0;
	tempdiv = thisdiv;
	if (tempdiv)
	{
		while (tempdiv.previousSibling != null)
		{
			if (tempdiv.previousSibling)
			{
				if (tempdiv.previousSibling.nodeType == 1)
				{//elementnode
					if (tempdiv.previousSibling.tagName == thisdiv.tagName) 
					{
						pos++;
					}
				}
				tempdiv = tempdiv.nextSibling;
			}
			else return pos;
		}
	}
	else return pos;
}

function appendnewdiv(divparentname, divbefore, removetaglike, divcount)
{
	//var divbefore = document.getElementById(divbeforename);
	if (divparentname)
	{
		var divparent = document.getElementById(divparentname);
	}
	else
	{
		var divparent = divbefore.parentNode;
	}
	var newdiv = divbefore.cloneNode(true);
	if (divbefore && divparent)
	{
		//search next sibling of inserted item type
		nextSibling = findSiblingByTag(divbefore, divbefore.tagName);
		//alert(nextSibling);
		//alert(newdiv);
		if (nextSibling) {
			divparent.insertBefore(newdiv, nextSibling);
		} else {
			divparent.appendChild(newdiv);
		}	
		if (newdiv)
		{//clear node childs
			for (i=0; i<newdiv.childNodes.length; i++)
			{
				if (newdiv.childNodes[i].tagName == removetaglike)
				{
					newdiv.removeChild(newdiv.childNodes[i]);
					i--;
				}
			}
		}//divparent.insertBefore(newdiv, divbefore);
		divcount++;
	}
	return divcount;
}

function removediv(divparentname, divid, divcount, maxcount)
{
	if (divparentname)
	{
		var divparent = document.getElementById(divparentname);
	}
	else
	{
		var divparent = divid.parentNode;
	}
	
	if (divid && divparent)
	{
		if (divcount >= maxcount)
		{
			divparent.removeChild(divid);
			divcount--;
		}
	}
	return divcount;
}

function recursiveRename(parentelement, tagtypes, nameprefix)
{
    var children = parentelement.childNodes;                
    for(var i=0; i < children.length; i++) {    
    	for (tagtype in tagtypes)
		{
			if (children[i].tagName == tagtypes[tagtype])
			{
				children[i].setAttribute('name', nameprefix+children[i].getAttribute('name'))
			}
		}
        recursiveRename(children[i], tagtypes, nameprefix);
    }
    return;
}

function alertRecursiveTree(parentelement, tagtypes, attrtype)
{
    var children = parentelement.childNodes;                
    for(var i=0; i < children.length; i++) {    
    	for (tagtype in tagtypes)
		{
			if (children[i].tagName == tagtypes[tagtype])
			{
				alert(children[i]);
				alert(children[i].getAttribute(attrtype));
			}
		}
        alertRecursiveTree(children[i], tagtypes, attrtype);
    }
    return;
}

function divsetfieldvalue(parentelement, addfield, pos, currentpos, resource)
{
	if (parentelement)
	{
		var children = parentelement.childNodes; 
		for(var i=0; i < children.length; i++) {
			if (children[i].nodeType == 1)
			{
				if (children[i].getAttribute("name") == addfield)
				{
					if (pos == currentpos)
					{//set the value
						children[i].value = resource;
					}
					currentpos++;
				}
			}
			currentpos = divsetfieldvalue(children[i], addfield, pos, currentpos, resource);
		}
	}
	return currentpos;
}

function arrayRecursiveTree(parentelement, tagtypes, attrtype, hiddennameindex)
{
	if (parentelement)
	{
		if (parentelement.childNodes)
		{
			var children = parentelement.childNodes; 

			for(var i=0; i < children.length; i++) {    
				for (tagtype in tagtypes)
				{
					if (children[i])
					{
						if (children[i].tagName == tagtypes[tagtype])
						{
							attr = children[i].getAttribute(attrtype);
							hiddenvaluename = document.getElementById(attr+hiddennameindex);
							if (hiddenvaluename)
							{
								hiddenvaluename.value = hiddenvaluename.value+children[i].value+"|";
							}
						}
					}
				}
				arrayRecursiveTree(children[i], tagtypes, attrtype, hiddennameindex);
			}
		}
	}
}

function findParentByTag(formelement, tagName)
{
	var control = 0;
	if (formelement)
	{
		while (formelement.parentNode != null)
		{
			control++;
			if (formelement.parentNode)
			{
				if (formelement.parentNode.tagName == tagName) return formelement.parentNode;
				formelement = formelement.parentNode;
			}
			else return null;
			if (control > 30) return null;
		}
	}
	else return null;
}

function findParentNextChildOfSameType(parentelement, childelement)
{
	start = 0;
	if (parentelement.childNodes)
	{
		for (i=0; i<parentelement.childNodes.length; i++)
		{
			nextchild = parentelement.childNodes.item(i);
			if (nextchild == childelement) start = 1;
			if ((nextchild.tagName == childelement.tagName) && (start == 1))
			{
				return nextchild;
			}
		}
	}
	return null;
}

function findSiblingByTag(formelement, tagName)
{
	var control = 0;
	if (formelement)
	{
		while (formelement.nextSibling != null)
		{
			control++;
			if (formelement.nextSibling)
			{
				//alert("formelement.nextSibling");
				//alert(formelement.nextSibling);
				//alert("formelement.nextSibling.tagName");
				//alert(formelement.nextSibling.tagName);
				//alert("formelement.nextSibling.nodeName");
				//alert(formelement.nextSibling.nodeName);
				if (formelement.nextSibling.nodeType == 1)
				{//elementnode
					if (formelement.nextSibling.tagName == tagName) return formelement.nextSibling;
				}
				formelement = formelement.nextSibling;
			}
			else return null;
			if (control > 30) return null;
		}
	}
	else return null;
}

function regularizeform(divid, divname, nameprefix)
{// make php readable room/price list or other list
	//params ex: (pricemaindiv, hoteldata)
	var nameindex=0;
	var inputtypes = new Array();
	inputtypes[0] = 'SELECT';
	inputtypes[1] = 'INPUT';
	divname = divid ? divid : document.getElementById(divname);
	for (i=0; i<divname.childNodes.length; i++)
	{
		if (divname.childNodes[i].nodeType == 1)
		{
			itemsdiv = divname.childNodes[i];
			//alert(itemdiv.getAttribute('name'));
			for (j=0; j<itemsdiv.childNodes.length; j++)
			{
				if (itemsdiv.childNodes[j].nodeType == 1)
				{
					recursiveRename(itemsdiv.childNodes[j], inputtypes, nameprefix+'_'+nameindex);
				}
			}
			//alert(divname.childNodes[i].getAttribute('name'));
			//newdiv.removeChild(divname.childNodes[i]);
			//i--;
			nameindex++;
		}
	}
	return true;
}

var SubmitFormAjaxOn = false;  

function ajax_submitForm(url, resulttarget, plist, onerror, oncomplete, onsuccess)
{
    //Element.show('working');
    if (plist)
    {
        url = url+"&"+plist
    }
    var ocAjax = new Ajax.Request(
        url, 
        {
            method: 'get', 
            sourceitem: resulttarget, 
            onComplete: onsuccess,
            onSuccess: function(transport) {SubmitFormAjaxOn = false; onsuccess(transport.responseText)},  
            onFailure: onerror
        });

    SubmitFormAjaxOn = true;  
    new PeriodicalExecuter(function(CheckAjax)  
        {  
            CheckAjax.stop();  
            if(SubmitFormAjaxOn) 
            {  
                ocAjax.transport.abort();
            }  
        }, 10000);  
}
