<!-- Hide script from old browsers
// Make sure file is not hosted in a different frameset
if ( top.location.href != window.location.href )
  { top.location.href=window.location.href }

    function openWindow(url, w, h) {
        var windowprops = "width=" + w + ",height=" + h +",,left=10,top=10,scrollbars=no,status=yes,toolbar=no,location=no,directories=no,resizable=yes";
          popup = window.open(url,'newWin',windowprops);
          popup.focus();
           } 
        
	function openPicture(pic,w,h) {
     
		var windowprops = "left=160,top=100,scrollbars=no,status=yes,toolbar=no,location=no,directories=no,resizable=yes,width=" + w + ",height=" + h;
          
		picture = window.open('','newPicture',windowprops);
		picture.close();
        picture = window.open('','newPicture',windowprops);  
        picture.focus();
        
		picture.document.write("<html><head><title>Picture<\/title><\/head><body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>")
		picture.document.writeln("<img src=" + pic + " width=" + w + " height=" + h + ">")
		picture.document.write("<\/body><\/html>")
	}                
 
function showdate()
{
	
		// Determine the current date and display it
   	var today = new Date();
   	var day = today.getDay();
   	var date = today.getDate();
   	var month = today.getMonth();
   	var year = today.getFullYear();
   	
   		//January,February,March,April,May,June,July,August,September,October,November,December
   	if (month == 0) {month='Jan'};
   	if (month == 1) {month='Feb'};
   	if (month == 2) {month='Mar'};
   	if (month == 3) {month='Apr'};
   	if (month == 4) {month='May'};
   	if (month == 5) {month='Jun'};
   	if (month == 6) {month='Jul'};
   	if (month == 7) {month='Aug'};
   	if (month == 8) {month='Sept'};
   	if (month == 9) {month='Oct'};
   	if (month == 10) {month='Nov'};
   	if (month == 11) {month='Dec'};
   
   	if (day == 0) {day='Sunday'};
   	if (day == 1) {day='Monday'};
   	if (day == 2) {day='Tuesday'};
   	if (day == 3) {day='Wednesday'};
   	if (day == 4) {day='Thursday'};
   	if (day == 5) {day='Friday'};
   	if (day == 6) {day='Saturday'};
   
   if ( document.all ) {
		document.write ('<span class="small">&nbsp;&nbsp;' + day + ', ' + month +  ' ' + date + ', ' + year + '</span>');
	} else {
		document.write ('<span class="smallNC">&nbsp;&nbsp;' + day + ', ' + month +  ' ' + date + ', ' + year +  '</span>');
	}
   
   
}

function switchClass(obj,strClassName) {
		obj.className	= strClassName;
	}
	
function gotoURL(strUrl) {
		location = strUrl;
	}

function copyrightYear(startYear)
	{
		d = new Date();
		
		if (startYear != d.getFullYear())
			{
			return startYear + " - " + d.getFullYear();
			}
		else
			{
			return startYear;
			}
	}
	
	// Show current date in format "Nov 15, 2003".		
function nsDate()
{
		// Get today's date.
	var sDate = new Date();
	var sToday = "";
	
		// Array with months.
	var aMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	
		// Get current month.
	var iMonth = sDate.getMonth();
		
		// Get current hour,minute,second.
	var iHours   = sDate.getHours();
	var iMinutes = sDate.getMinutes();
	var iSeconds = sDate.getSeconds();
			
	sToday = "";
		// Get today's date string.
	sToday += aMonths[iMonth] + " ";
	sToday += sDate.getDate() + ", ";
	sToday += sDate.getFullYear() + " ";

		// Display the date on the screen.
	document.write("<b>"+sToday+"</b>");	
}

	// Get the current date in format "Nov 15, 2003  12:53:23 pm".
function today()
{
	var ie4=document.all;
    var ns4=document.layers;
    var ns6=document.getElementById&&!document.all;
    	
		// Get today's date.
	var sDate = new Date();
	var sToday = "";
	
		// Array with months.
	var aMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	
		// Get current month.
	var iMonth = sDate.getMonth();
		
		// Get current hour,minute,second.
	var iHours   = sDate.getHours();
	var iMinutes = sDate.getMinutes();
	var iSeconds = sDate.getSeconds();
	
		// If iHour > 12.
	if(iHours>12)
		iHours = iHours - 12;
	
	if(iMinutes < 10)
		iMinutes = "0" + iMinutes;
		
	if(iSeconds < 10)
		iSeconds = "0" + iSeconds;
	
	sToday = "";
		// Get today's date string.
	sToday += aMonths[iMonth] + " ";
	sToday += sDate.getDate() + ", ";
	sToday += sDate.getFullYear() + " ";
	
	sToday += iHours + ":";
	sToday += iMinutes + ":" ;
	sToday += iSeconds; 

	
		// Set am or pm
	if(sDate.getHours() < 12)
		sToday += " am ";
	else
		sToday += " pm ";
	
		// Display the date on the screen.
	
		// Write the clock to the layer:
	if (ns4) 
	{
		return;
	} 
	else 
		if (ie4) 
		{
			document.getElementById("LiveClock").innerHTML = "<b>"+sToday+"</b>";
		}	 
		else 
			if (ns6)
			{
				document.getElementById("LiveClock").innerHTML = "<b>"+sToday+"</b>";
            }            
	
		setTimeout("today()",1000);
		//window.setInterval(today(),100);
}	


function openWindow2(url,wname,w,h,props) 
{
	var winTop=(screen.height-h)/2;
	var winLeft=(screen.width-w)/2;
	
	var windowprops = "width=" + w + ",height=" + h +",left="+winLeft+",top="+winTop+","+props;
	return window.open(url,wname,windowprops);
}
/*
* change image source
* 
* @param string mainImageId - image id
* @param string imageUrl - url of the image that have to be put as a sourse to original image
*/
function changeMainImage(mainImageId, imageUrl) {
	var mainImage = document.getElementById(mainImageId);
	if (mainImage) {
		// change image source to imageUrl
		mainImage.src = imageUrl;
	}
}


// End hiding script from old browsers -->

function showCalendar()
{
	var el = document.getElementById('availabilityRow');
	
	el.style.display = el.style.display == 'none' ? 'table-row' : 'none';
}

function addMorePhotos()
{
	var tbody = document.getElementById('newPhotosTBody');
	
	var tr = document.createElement('tr');
	
	var td = document.createElement('td');
	td.className = 'tableDataLight';
	td.align = 'left';
	
	var b = document.createElement('b');
	b.innerHTML = 'Add new';
	
	td.appendChild(b);
	tr.appendChild(td);
	
	td = document.createElement('td');
	td.className = 'tableDataDark';
	td.align = 'left';
	
	var inp = document.createElement('input');
	inp.type = 'file';
	inp.name = 'addphotos_new[]';
	
	td.appendChild(inp);
	tr.appendChild(td);
	
	tbody.appendChild(tr);
}

function propertyTypeChanged(value)
{
	document.getElementById('directBookingDetails').style.display = value == 'direct' ? 'table-row' : 'none';
}

function ciChanged(value)
{
	var state = (value == 'mail');
	
	document.getElementById('contactInfoSite').disabled = state;
	document.getElementById('contactInfoMail').disabled = !state;	
}

function checkContactInfoInput()
{
	if(document.getElementById('bookingType').value == 'direct')
	{
		if(true || document.getElementById('ciMail').checked == true)
		{
			var CIM = document.getElementById('contactInfoMail');
			
			if(!isValidEmail(CIM.value))
			{
				alert("Please enter valid contacts e-mail.");
				CIM.focus();
				
				return false;
			}
		}
		else
		{
			var CIS = document.getElementById('contactInfoSite');
			
			var error = '';
			
			if(CIS.value == '' || CIS.value == 'http://')
				error = "Please enter contacts web page.";
			
			//if(!error && !isValidURL(CIS.value))
			//	error = "Please enter valid URL.";
			
			if(error)
			{
				alert(error);
				CIS.focus();
				
				return false;
			}
		}
	}
	
	return true;
}

function isValidEmail(email)
{
	var regEx = /^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/;
	
	if(!email || !regEx.test(email))
		return false;
	
	return true;
}

function isValidURL(URL)
{
	var regEx = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	
	if(!URL || !regEx.test(URL))
		return false;
	
	return true;
}

function copyPrevPrice(id)
{
	document.getElementById('price['+id+']').value = document.getElementById('price['+(id-1)+']').value;
	
	if(document.getElementById('available['+(id-1)+']').checked)
	{
		document.getElementById('available['+id+']').checked = true;
		
		availabilityChanged(document.getElementById('available['+id+']'), false);
	}
	else if(document.getElementById('booked['+(id-1)+']').checked)
	{
		document.getElementById('booked['+id+']').checked = true;
		
		availabilityChanged(document.getElementById('booked['+id+']'), true);
	}
	else if(document.getElementById('unavailable['+(id-1)+']').checked)
	{
		document.getElementById('unavailable['+id+']').checked = true;
		
		availabilityChanged(document.getElementById('unavailable['+id+']'), true);
	}
}

function checkAvailabilityPirces()
{
	var price = null;
	
	for(i = 1; i < 104; i++)
	{
		if(!document.getElementById('available['+i+']').checked)
			continue;
		
		price = document.getElementById('price['+i+']').value;
		
		if(!is_number(price) || price <= 0 || price > 9999)
		{
			alert('Please enter valid price between 0.01 and 9999.00');
			document.getElementById('price['+i+']').focus();
			
			return false;
		}
	}
	
	return true;
}

function is_number(num)
{
	if(isNaN(num) || num.indexOf(',') != -1)
		return false;
	
	return true;
}

function availabilityChanged(el, ch)
{
	el.parentNode.className = ch ? 'booked' : 'available';
}

function setWeekPrice(price)
{
	if(!price)
		price = '&nbsp;';
	else if(price != SELECT_WEEK)
	{
		var old = document.getElementById('weekPrice').innerHTML;
		var prc = '';
		
		if(old != SELECT_WEEK)
		{
			var currency = old.substr(0, 1);
			
			old = parseFloat(old.substr(1, old.length-1));
			price = parseFloat(price.substr(2, price.length-1));
			
			old = old+price;
			
			prc = currency+old.toFixed(2);
		}
		else
			prc = price.substr(1, price.length-1);
		
		price = prc;
	}		
	
	document.getElementById('weekPrice').innerHTML = price;
}

var firstDateSelected = false;
var invalidPeriod = false;

var actualDates = null;

function pickDate(el, date, dateTo, dontReset, skipLast)
{
	hideTooltip();
	
	date = dateToEn(date);
	dateTo = dateToEn(dateTo);
	
	if(el.className.indexOf('selected') != -1)
	{
		resetSelected();
		document.getElementById('dateFrom').value = PLEASE_SELECT;
		document.getElementById('dateTo').value = PLEASE_SELECT;
		
		return false;
	}
	
	if(false && window.invalidPeriod)
		return false;
	
	if(dateTo)
	{
		var df = document.getElementById('dateFrom').value;
		var dt = document.getElementById('dateTo').value;
		
		df = dateToEn(df);
		dt = dateToEn(dt);
		
		if(df != PLEASE_SELECT && dt != PLEASE_SELECT)
		{
			date = dateToEn(date);
			
			var startD = new Date(fixStartDate(date));
			
			startD = startD.getDate()+' '+getMonthName(startD.getMonth())+' '+startD.getFullYear();
			
			startD = dateToEn(startD);
			
			dateTo = dateToEn(dateTo);
			
			var endD = new Date(dateTo);
			
			endD = endD.getDate()+' '+getMonthName(endD.getMonth())+' '+endD.getFullYear();
			
			endD = dateToEn(endD);
			
			if(dt == startD || df == endD)
			{
				dontReset = true;
				
				if(dt == startD)
				{
					window.actualDates = [df, endD];
				}
				else
				{
					window.actualDates = [startD, dt];
				}
			}
		}
		
		if(window.oldClasses && !dontReset)
		{
			resetSelected();
		}
		
		tooltip('', '', COMPLETE_BOOKING);
		
		setWeekPrice(el.id);
		
		var marked = 0;
		
		date = dateToEn(date);
		
		document.getElementById('dateFrom').value = fixStartDate(date);
				
		dateTo = dateToEn(dateTo);
		
		var dt = new Date(dateTo);
				
		document.getElementById('dateTo').value = dateToEn(dt.getDate()+' '+getMonthName(dt.getMonth())+' '+dt.getFullYear());
		
		firstDateSelected = false;
		
		var tr = el.parentNode;
		
		for(i in tr.childNodes)
		{
			if(tr.childNodes[i].innerHTML && is_number(tr.childNodes[i].innerHTML))
			{
				setSelectedDate(tr.childNodes[i]);
				
				marked++;
			}
		}
		
		var negative = date.indexOf('-');
		var zero = date.substr(0, 1);
		
		if(negative != -1 || zero == 0)
		{
			var times = 0;
			
			if(negative != -1)
			{
				var tmp = date.substr(0, 2);
				
				times = parseInt(tmp) * -1 + 1;
			}
			else
				times = 1;
			
			var tbl = tr.parentNode.parentNode;
			
			var prevSbl = getPrevSibling(tbl);
			
			while(!prevSbl.innerHTML)
			{
				prevSbl = getPrevSibling(prevSbl);
			}
			
			for(i in prevSbl.childNodes)
			{
				if(prevSbl.childNodes[i].innerHTML)
				{
					var prevTbl = prevSbl.childNodes[i];
					
					for(j in prevTbl.childNodes)
					{
						if(prevTbl.childNodes[j].id == 'ctr[]')
						{
							var lastRow = prevTbl.childNodes[j];
						}
					}
					
					var rv = null;
					for(k in lastRow.childNodes)
					{
						if(lastRow.childNodes[k].innerHTML)
						{
							setSelectedDate(lastRow.childNodes[k]);
							
							rv = lastRow.childNodes[k];
						}
					}
				}
			}
		}
		
		if(window.actualDates)
		{
			document.getElementById('dateFrom').value = window.actualDates[0];
			document.getElementById('dateTo').value = window.actualDates[1];
			
			window.actualDates = null;
		}
		
		var nextRow = null;
		if(nextRow = getNextSibling(tr))
		{
			if(!skipLast)
				setSelectedDate(nextRow.firstChild, true);
			
			return nextRow.firstChild;
		}
		else
		{
			var tbl = tr.parentNode.parentNode;
			
			var nextSbl = getNextSibling(tbl);
			
			while(!nextSbl.innerHTML)
			{
				nextSbl = getNextSibling(nextSbl);
			}
			
			for(i in nextSbl.childNodes)
			{
				if(nextSbl.childNodes[i].innerHTML)
				{
					var nextTbl = nextSbl.childNodes[i];
					
					for(j in nextTbl.childNodes)
					{
						if(nextTbl.childNodes[j].id == 'ctr[]')
						{
							var nextTblRow = nextTbl.childNodes[j];
							
							for(k in nextTblRow.childNodes)
							{
								if(nextTblRow.childNodes[k].innerHTML)
								{
									if(!skipLast || marked != 7)
										setSelectedDate(nextTblRow.childNodes[k]);
									
									if(marked++ == 7)
										return nextTblRow.childNodes[k];
								}
							}
						}
					}
				}
			}
		}
	}
	else
	{
		if(!firstDateSelected)
		{
			document.getElementById('dateFrom').value = date;
			firstDateSelected = true;
			
			resetSelected();
			setSelectedDate(el);
			
			window.firstMarked = el;
		}
		else
		{
			resetSelected();
			
			document.getElementById('dateTo').value = date;
			firstDateSelected = false;
			
			if(!validateChosenDates())
				return false;
			
			var markFrom = markTo = strMarkFrom = strMarkTo = null;
			var day = 86400000;
			
			var dateFrom = document.getElementById('dateFrom').value
			var dateTo = document.getElementById('dateTo').value
			
			dateFrom = dateToEn(dateFrom);
			var df = new Date(dateFrom);
			
			dateTo = dateToEn(dateTo);			
			var dt = new Date(dateTo);
			
			var lastEl = window.firstMarked;
			
			var times = Math.round(((dt.getTime()-df.getTime())/day) / 7);
			
			var justOne = times == 1;
			
			var c = 0;
			while(times--)
			{
				markFrom = new Date(dt.getTime() + (7 * day)*c);
				markTo = new Date(markFrom.getTime() + 7 * day);
				
				strMarkFrom = markFrom.getDate()+' '+getMonthName(markFrom.getMonth())+' '+markFrom.getFullYear();
				strMarkTo = markTo.getDate()+' '+getMonthName(markTo.getMonth())+' '+markTo.getFullYear();
				
				skipLast = (c == 0 || times != 0) && !justOne ? true : false;
				
				lastEl = pickDate(getNextSibling(lastEl), strMarkFrom, strMarkTo, true, skipLast);
				
				c++;
			}
			
			document.getElementById('dateFrom').value = dateFrom;
			document.getElementById('dateTo').value = dateTo;
			
			
			if(window.invalidPeriod)
			{
				alert(DATES_NOT_AVAILABLE);
				
				document.getElementById('dateFrom').value = PLEASE_SELECT;
				document.getElementById('dateTo').value = PLEASE_SELECT;
				resetSelected();
				
				window.invalidPeriod = false;
			}
		}
	}
	
	return rv;
}

function getNextSibling(el) 
{
    do
    {
        el = el.nextSibling;
    }
    while(el && el.nodeType != 1);
    
    return el;
}

function getPrevSibling(el) 
{
    do
    {
        el = el.previousSibling;
    }
    while(el && el.nodeType != 1);
    
    return el;
}

var oldClasses = [];
function setSelectedDate(el, last)
{
	var currClass = el.className;
	
	if((currClass.indexOf('rented') != -1 || currClass.indexOf('unknown') != -1) && 
		(currClass.indexOf('availableTO') == -1 && currClass.indexOf('TOavailable') == -1 
		&& currClass.indexOf('startDate') == -1 && currClass != 'availableTOrented'))
	{
		window.invalidPeriod = true;
		
		return false;
	}
	
	if(currClass.indexOf('selected') == -1)
		window.oldClasses[window.oldClasses.length] = [el, currClass];
	
	var toPos = currClass.indexOf('TO');
	var newClass = null;
	
	if(toPos != -1)
	{
		if(currClass.indexOf('available') != -1)
			newClass = currClass.replace('available', 'selected');
		else
		{
			if(last)
				newClass = 'selected'+currClass.substr(toPos, currClass.length-toPos);
			else
				newClass = currClass.substr(0, toPos+2)+'selected';
		}
	}
	else
		newClass = 'selected';
	
	el.className = newClass;
}

function resetSelected()
{
	setWeekPrice(SELECT_WEEK);
	
	for(i in window.oldClasses)
	{
		window.oldClasses[i][0].className = window.oldClasses[i][1];
	}
	
	window.oldClasses.length = 0;
}

function fixStartDate(date)
{
	if(date.indexOf('-') != -1 || date.substr(0, 1) == 0)
	{
		tmp = date.split(' ');
		
		if(tmp[1] == 'Jan')
		{
			tmp[2] -= 1;
			tmp[1] = 'Dec';
			tmp[0] = 31 + parseInt(tmp[0]);
		}
		else
		{
			tmp[1] = getPrevMonth(tmp[1]);
			
			var dim = getDaysInMonth(tmp[1], !(tmp[2]%4));
			tmp[0] = dim + parseInt(tmp[0]);
		}
		
		date = tmp[0]+' '+tmp[1]+' '+tmp[2];
	}
	
	return date;
}

function getDaysInMonth(month, leapYear)
{
	if(month == 'Jan' || month == 'Mar' || month == 'May' || month == 'Jul' || month == 'Aug' || month == 'Oct' || month == 'Dec')
		return 31;
	else if(month == 'Apr' || month == 'Jun' || month == 'Sep' || month == 'Nov')
		return 30;
	else if(leapYear)
		return 29;
	else
		return 28;
}



function getPrevMonth(month)
{
	var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
	
	for(i in months)
	{
		if(month == months[i])
			return months[i-1];
	}
}

function validateChosenDates()
{
	/*
	if(document.getElementById('dateFrom').value == PLEASE_SELECT || document.getElementById('weekPrice').innerHTML == SELECT_WEEK)
	{
		alert(SELECT_DATES);
		
		return false;
	}
	
	if(document.getElementById('dateTo').value == PLEASE_SELECT)
	{
		alert(SELECT_END_DATE);
		
		return false;
	}
	*/
	
	var dFrom = document.getElementById('dateFrom').value;
	var dTo = document.getElementById('dateTo').value;
	
	dFrom = dateToEn(dFrom);
	dTo = dateToEn(dTo);
	
	var from = new Date(dFrom);
	var to = new Date(dTo);
	
	if(from >= to)
	{
		alert(DATE_INCORRECT);
		document.getElementById('dateFrom').value = PLEASE_SELECT;
		document.getElementById('dateTo').value = PLEASE_SELECT;
		resetSelected();
		
		return false;
	}
	
	return true;
}


if(document.layers)
{ 
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = captureMousePosition;
} 
else if(document.all)
	document.onmousemove = captureMousePosition;
else if(document.getElementById)
	document.onmousemove = captureMousePosition;

var xMousePos = 0; 
var yMousePos = 0; 

function captureMousePosition(e) 
{
	if(document.layers) 
	{
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	} 
	else if(document.all) 
	{
		xMousePos = window.event.x+(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		yMousePos = window.event.y+(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} 
	else if(document.getElementById) 
	{
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	}
}


function tooltip(value, className, msg)
{
	var val = '';
	
	if(className.indexOf('selected') != -1)
		val = CURR_SELECTION;
	else
		val = PRICE+': '+value+'<br />'+CLICK_TO_SELECT;
	
	if(msg)
		val = msg;	
	
	var tooltip = document.getElementById('tooltip');
	
	tooltip.style.display = 'block';
	tooltip.style.left = parseInt(xMousePos+10)+'px';
	tooltip.style.top = parseInt(yMousePos+10)+'px';
	tooltip.innerHTML = val;
}

function hideTooltip()
{
	document.getElementById('tooltip').style.display = 'none';
}

function setLang(lang)
{
	document.getElementById('language').value = lang;
	
	return true;
}
