function mouseOver(e) {
    if (e && e.className.search('-over') == -1) {
		e.oldClassName=e.className;
		e.className+='-over';
	}
}
function mouseOut(e) {
    if (e && e.oldClassName) {
		e.className=e.oldClassName;
		e.oldClassName = null;
	}
}

var mouseClick_CLICKED = false;

function mouseClick(e, event) {
	var i;
	if (mouseClick_CLICKED) return;
	mouseClick_CLICKED = true;
	setTimeout('mouseClick_CLICKED = false;', 300);

	if (e && e.hasChildNodes) {
		for (i=0; i<e.childNodes.length; i++) {
			if (e.childNodes[i].tagName == 'A') {
				document.location = e.childNodes[i].getAttribute('href');
				return;
			}
		}
	}
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) return window.document[movieName];
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		return (document.embeds && document.embeds[movieName]) ? document.embeds[movieName] : document.getElementById(movieName);
	}
}

function setFlashVar(id,varName,varValue) {
	var flashMovie=getFlashMovieObject(id);
	if (flashMovie) {
		flashMovie.SetVariable("/:"+varName, varValue);
	}
}

function getFlashVar(id,varName) {
	var flashMovie=getFlashMovieObject(id);
	if (flashMovie) {
		var varValue=flashMovie.GetVariable("/:"+varName);
		return varValue;
	}
}

function stepBack(e) {
	if (!e || !e.form) return;
	flowControl.init();
	flowControl.go(setURLVariable(e.form.action,'action','back'));
	return false;
}

function submitForm(obj, form_element_id, value) {
	var form;
	if (form_element = document.getElementById(form_element_id)) {
		form_element.value = value;
		form = form_element.form;
	} else form = getAncestorByName(obj, 'FORM');
	if (form) {
		if (form.onsubmit && !form.onsubmit()) {
			return false;
		}
		form.submit();
	} else return false;
}

function showLayer(id) {
	var obj = document.getElementById(id);
	if (!obj) return;
	obj.style.visibility = "visible";
	
	if (document.all) {
		obj.style.display = "block";
		return;
	}
	
	switch(obj.tagName) {
		case "TABLE":
			obj.style.display = "table";
		break;
		case "TR":
			obj.style.display = "table-row";
		break;
		case "TD":
			obj.style.display = "table-cell";
		break;
		default:
			obj.style.display = "block";
		break;
	}
}
	
function hideLayer(id) {
	var obj = document.getElementById(id);
	if (!obj) return;
	obj.style.visibility = "hidden";
	obj.style.display = "none";
}

function getAncestorByName(element, ancestorName) {
	if (element.parentNode.tagName) {
		return (element.parentNode.nodeName == ancestorName) ? element.parentNode : getAncestorByName(element.parentNode, ancestorName);
	}
}

function getChild(e,tag) {
	return (e.getElementsByTagName(tag).length > 0) ? e.getElementsByTagName(tag)[0] : false;
}

function setAttribute(obj,attrName,attrValue) {
	if (!obj) return;
	if (document.all) obj[attrName] = attrValue;
	obj.setAttribute(attrName,attrValue);
}

function inArray(needle,haystack) {
	for (var i=0; i< haystack.length; i++) {
		if (haystack[i] == needle) return true;
	}
	return false;
}

function addDistinct(v, a) {
	if (!inArray(v,a)) {
		a.push(v);
	}
}

function addArrayIndex(array,index,value,reset) {
	if (!array[index] || reset) array[index] = new Array();
	array[index].push(value);
}

function getSelectIndex(select_id,value) {
	var select_element = (typeof(select_id)=='string') ? document.getElementById(select_id) : select_id;
	if (!select_element) return;
	for (var i=0; i<select_element.options.length; i++) {
//		alert('select_element.options[i].value'+select_element.options[i].value+'=='+value);
		if (select_element.options[i].value == value) return i;
	}
	return -1;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function language(lang) {
  langRE = /\/bg\/|\/en\/|\\bg\\|\\en\\/;
  document.location.replace(document.location.href.replace(langRE,'/'+lang+'/'));
}

function nav_toggle(id){
	var obj;
	if (obj = MM_findObj(id)) {
		if (obj.style) {
			if (obj.style.visibility == 'visible') {
				obj.style.visibility = 'hidden';
				obj.style.display = 'none';
			} else {
				obj.style.visibility = 'visible';
				obj.style.display = 'block';
			}
		}
	}
}

function nav_hide(id) {
	if (!id) return;
	if (obj = MM_findObj(id)) {
		if (obj.style) {
			obj.style.visibility = 'hidden';
			obj.style.display = 'none';
		}
	}
}

var nav = [];
var nav_out = [];

function show_obj(id, display) {
	if (typeof(display)=='undefined') display='block';
	var obj = document.getElementById(id);
	if (obj && obj.style) {
		obj.style.display = display;
		obj.style.visibility = 'visible';
	}
}

function hide_obj(id) {
	var obj = document.getElementById(id);
	if (obj && obj.style) {
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
	}
}

function nav_show(id) {
	var obj, old;
	
	if (id) {
		var level = id.split('_').length -1;
		hide_obj(nav[level]);
		show_obj(id);
		nav[level] = id;
		if (nav_out[level] == id) nav_out[level]='';
	} else {
		var i;
		for (i=0; i<nav.length; i++) {
			hide_obj(nav[i]);
		}
	}
}

function nav_auto_hide(id) {
	/*var level = id.split('_').length;
	nav_out[level] = id;
	
	setTimeout(
		function () {
			if (nav_out[level] == id) hide_obj(id);
		},
		3000
	);*/
}

function correctPNG()  {
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   if ((version >= 5.5) && (document.body.filters)) {
      for(var i=0; i<document.images.length; i++) {
         var img = document.images[i];
         var imgName = img.src.toUpperCase();
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
		 	onClick = '';
            var imgID = (img.id) ? "id='" + img.id + "' " : "";
            var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			if (img.onclick != null) onClick = " onclick = \""+img.onclick.toString().substring(23,img.onclick.toString().length-2)+"\"";
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
            var imgStyle = "display:inline-block;" + img.style.cssText ;
            if (img.align == "left") imgStyle = "float:left;" + imgStyle;
            if (img.align == "right") imgStyle = "float:right;" + imgStyle;
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
            var strNewHTML = "<span " + onClick + imgID + imgClass + imgTitle;
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
			
            img.outerHTML = strNewHTML;
            i = i-1;
         }
      }
   }    
}

function fixPng(id) {
	var obj = document.getElementById(id);
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	var ext = obj.src.substring(obj.src.length-3, obj.src.length);
	if (version >=7 || version < 5.5 || !document.body.filters) return;
	if (ext.toUpperCase() != "PNG") return;

	if (obj.width < 1) {
		setTimeout("fixPng('"+obj.id+"')",20);
		return;
	}
	var imgID = (obj.id) ? "id='" + obj.id + "' " : "";
	var imgClass = (obj.className) ? "class='" + obj.className + "' " : "";
	var imgTitle = (obj.title) ? "title='" + obj.title + "' " : "title='" + obj.alt + "' ";
	var imgStyle = "display:inline-block;" + obj.style.cssText;
	if (obj.align == "left") imgStyle = "float:left;" + imgStyle;
	if (obj.align == "right") imgStyle = "float:right;" + imgStyle;
	if (obj.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	var strNewHTML = "<span " + imgID + imgClass + imgTitle
	+ " style=\"" + "width:" + obj.width + "px; height:" + obj.height + "px;" + imgStyle + ";"
	+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	+ "(src=\'" + obj.src + "\', sizingMethod='scale');\"></span>" ;
	obj.outerHTML = strNewHTML;
}

/*Top Navigation*/

function topNavOver(e) {
	if (!e) return;
	
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	var imgElement;
	
	if (version >= 5.5 && version < 7 && (document.body.filters)) imgElement = getChild(e,'span');
	else imgElement = getChild(e,'img');
	
	if (imgElement) imgElement.style.background ='#FF6E00';
	//nav_show();
}

function topNavOut(e) {
	if (!e) return;
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	var imgElement;
	
	if ((version >= 5.5) && version < 7 && (document.body.filters)) imgElement = getChild(e,'span');
	else imgElement = getChild(e,'img');
	
	if (imgElement) imgElement.style.background ='#001A95';
}

function hotels_worldwide_locator(href) {
	if (href && href != '') {
		hide_obj('site_content');
		show_obj('hotels_worldwide_locator');
		var frame = document.getElementById('goglobal');
		if (frame) {
			frame.src = href;
		}
	}
}

//window.attachEvent("onload", correctPNG);

function drawCalendar(chosenDay, chosenMonth, chosenYear, availableDates) {
	var chosenDate, style, i,year,month,day,date,datesLength;
	var output='';
	chosenMonth--;
	
	var	firstDay = new Date(chosenYear,chosenMonth,1);
    var	startDay = firstDay.getDay();
		startDay = (startDay == 0 ? 7 : startDay)-1;

	output += "<table width='100%' id='calendar-large' class='calendar-large' border='0' year='"+parseInt(chosenYear,10)+"' month='"+parseInt(chosenMonth+1,10)+"' day='"+parseInt(chosenDay,10)+"'  cellspacing='0' cellpadding='0'>"+
			  "<tr><td align='center' class='month-change' onmouseover='mouseOver(this)' onmouseout='mouseOut(this)' onclick='mouseClick(this)'>"+
			  "<a href='javascript:prevMonth(\"calendar-large\")'></a>&lt;</td><td colspan='5' style='padding:5px' align='center'><h2>"+months[chosenMonth]+' '+chosenYear+"</h2></td>"+
			  "<td align='center' class='month-change' onmouseover='mouseOver(this)' onmouseout='mouseOut(this)' onclick='mouseClick(this)'><a href='javascript:nextMonth(\"calendar-large\")'/></a>&gt;</td></tr><tr>";
    for(i=0; i<7; i++) {
		style = (i == 6) ? 'border-right:1px solid #FF6E00;' : 'border-right:none;';
		output += "<td align='center'  style='"+style+"background:#FFE9D9; padding-top:7px; border-bottom:1px solid #FF6E00;' width='14%' height='28'><p>" + dayNames[i] + "</p><\/td>";
	}
    output += "<\/tr><tr align='center' valign='middle'>";
    if(((chosenYear % 4 == 0) && (chosenYear % 100 != 0)) || (chosenYear % 400 == 0))
		days[1] = 29;
    else
        days[1] = 28;
    var column = 0;
    var lastMonth = chosenMonth - 1;
    if(lastMonth == -1) lastMonth = 11;
    for(i=0; i<startDay; i++){
    	output += "<td height='28'>&nbsp;<\/TD>";
        column++;
      }
    var currentMonth = chosenMonth ;
    currentMonth++ ;
    for(i=1; i<=days[chosenMonth]; i++) {
       	output += "<td height='28'>";
		output += "<p><b id='calendar_day_"+i+"'>"+i+"</b></p><\/TD>";
        column++;
        if(column == 7) {
        	output += "<\/tr><tr align='center' valign='middle'>";
            column = 0;
        	}
      	}
    if(column > 0)
    	for (i=1; column<7; i++) {
      		output += "<td height='28'>&nbsp;<\/TD>";
          	column++;
       		}
    output += "<\/tr></table>";
	return output;
}

function fillAvailableDates(availableDates, stop) {
	if (availableDates.length <= 0) return;
	var i,year,month,day,date,inputStr,obj;
	var calendar = document.getElementById('calendar-large');
	
	var minimal = document.minPriceDate.split('-');
	var departure = document.departureDate.split('-');

	for (i=0; i< availableDates.length; i++) {
		date = availableDates[i].split('-');
		year = parseInt(date[2],10);
		month = parseInt(date[1],10);
		day = parseInt(date[0],10);
		if (calendar.getAttribute('year') == year && calendar.getAttribute('month') == month) {
			obj = document.getElementById('calendar_day_'+parseInt(day,10));
			if (!obj) {
				alert('Cant find day: '+day+' ('+parseInt(day,10)+')');
				continue;
			}
			
			if (stop) {
				obj.innerHTML = '<span class="stop-date">'+document.transport_stop_sales+'</span>';
			} else {
				inputStr = "<input type='radio' ";
				if (day == parseInt(departure[0],10) && month == parseInt(departure[1],10) && year == parseInt(departure[2],10)) {
					inputStr += 'checked=1 ';
				}
				inputStr += "name='departureDate' value='"+availableDates[i]+"' onclick='refresh_tour_prices(this.value, document.duration, this.form)' />";
				obj.innerHTML += inputStr;
			}
			
			getAncestorByName(obj, "TD").className = 
				stop ? 'stop-date' : 
					(
						(day == parseInt(minimal[0],10) && month == parseInt(minimal[1],10) && year == parseInt(minimal[2],10)) ? 'active-date' : 'possible-date'
					);
		}
	}
}

function prevMonth(calendarID) {
var calendar = document.getElementById(calendarID);
	if (calendar) {
		var	year  = parseInt(calendar.getAttribute('year'),10);
		var	month = parseInt(calendar.getAttribute('month'),10);
			month--;
		
		month = (month <= 0) ? 12 : month;
		if (month == 12) year--;
		
		document.getElementById('tour-calendar').innerHTML = drawCalendar(1, month, year);
		fillAvailableDates(availableDates);
		fillAvailableDates(stopDates, true);
	}
}

function nextMonth(calendarID) {
var calendar = document.getElementById(calendarID);
	if (calendar) {
		var	year  = parseInt(calendar.getAttribute('year'),10);
		var	month = parseInt(calendar.getAttribute('month'),10);
			month++;
		
		month = (month > 12) ? 1 : month;
		if (month == 1) year++;
		
		document.getElementById('tour-calendar').innerHTML = drawCalendar(1, month, year);
		fillAvailableDates(availableDates);
		fillAvailableDates(stopDates, true);
	}
}

function dateValidate(obj, format) {
var date;
	if ((date = isDate(format, obj.value)) || obj.value == '') {
		obj.defaultValue = obj.value = date[0];
	} else obj.value = obj.defaultValue;
}
