function addBookmark()
{
	/* Javascript "Add to Bookmarks" support in Firefox 2 is too poor to bother with
	 * - The best you can get is a bookmark that opens in the thin side panel. Roll on FF3
	if (window.sidebar)
	{
		window.sidebar.addPanel(document.title, window.location,"");
	}
	else */if (window.external)
	{
		window.external.AddFavorite( window.location, document.title);
	}
	else if(window.opera && window.print)
	{
		var elem = document.createElement('a');
		elem.setAttribute('href', window.location);
		elem.setAttribute('title', document.title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}
function addRoom()
{
    var cost = document.getElementById('room1cost');
    if (!cost) return false;
    var ol = jQuery.browser.mozilla ? cost.parentNode.parentNode.parentNode : cost.parentNode.parentNode;
    var lis = ol.getElementsByTagName('li');
    if (!lis) return false;
    var i;
    for (i = 1; i < lis.length; i++)
    {
        if (lis.item(i).style['display'] == 'none')
        {
            lis.item(i).style['display'] = 'block';
            lis.item(i + 1).style['display'] = 'block';
            lis.item(i + 2).style['display'] = 'block';
            break;
        }
    }
    if (i + 4 == lis.length)
    {
        var link = document.getElementById('addroomjslink');
        link.style['display'] = 'none';
    }
    
    return false;
}
function enableAddBookmark()
{
	if (!window.external && !(window.opera && window.print))return false;
	var bookmark = document.getElementById('bookmark');
	if (!bookmark) return false;
	var a = bookmark.getElementsByTagName('a').item(0);
	a.onclick = function () {addBookmark(); return false;};
	jQuery(bookmark).css('display', 'block');
}
function enableAddRoom()
{
    var i;
    for (i = 10; i > 1; i--)
    {
        var cost = document.getElementById('room' + i + 'cost');
        var quantity = document.getElementById('room' + i + 'quantity');
        var description = document.getElementById('room' + i + 'description');
        var costli = jQuery.browser.mozilla ? cost.parentNode.parentNode : cost.parentNode;
        var quantityli = jQuery.browser.mozilla ? quantity.parentNode.parentNode : quantity.parentNode;
        var descriptionli = jQuery.browser.mozilla ? description.parentNode.parentNode : description.parentNode;
        if (costli && quantityli && descriptionli)
        {
            if ((!cost.value || cost.value == '') && (!quantity.value || quantity.value == '' || quantity.value == 0) && (!description.value || description == ''))
            {
                costli.style['display'] = 'none';
                quantityli.style['display'] = 'none';
                descriptionli.style['display'] = 'none';
            }
            else
            {
                break;
            }
        }
    }
    var link = document.createElement('a');
    link.setAttribute('href', '#');
    link.onclick = addRoom;
    li = document.createElement('li');
    li.appendChild(link);
    link.id = 'addroomjslink';
    var text = document.createTextNode('Add details for another room');
    link.appendChild(text);
    if (costli && i != 10)
    {
        var ol = costli.parentNode;
        ol.appendChild(li);
    }
}
function enableAgreeTerms()
{
    var check = document.getElementById('agreeterms');
    if (check)
    {
        //var form = document.getElementById('ppform');
        var form = check.form;
        form.onsubmit = function() {
                var check = document.getElementById('agreeterms');
                if (check.checked) return true;
                else
                {
                    alert('You must agree to the terms and conditions to continue');
                    return false;
                }
            };
    }
}
function enableHelpPopUp()
{
    var link = document.getElementById('helplink');
    if (!link) return null;
    link.onclick = function () {popUp(this); return false;}
}
function enableImageSelector()
{
    var imageselector = document.getElementById('imageselector');
            if (imageselector)
            {
                var images = imageselector.getElementsByTagName('img');
                var image = new Image();
                for (var i = 0; i < images.length; i++)
                {       
                    image.src = FILES_URL + '/' + images.item(i).src.match(/([^\\\/]+)$/)[1];
                }
            }
            jQuery('#imageselector img').click(
                                        function(){
                                            
                                            var largeimgsrc = FILES_URL + '/' + this.src.match(/([^\\\/]+)$/)[1];
                                            jQuery('#imageviewer img').hide('slow', function(){
                                                                        var imageviewer = document.getElementById('imageviewer');
                                                                        var largeimage = imageviewer.getElementsByTagName('img').item(0);
                                                                        largeimage.src = largeimgsrc; 
                                                                     });
                                            jQuery('#imageviewer img').show('slow');
                                         }
                                    );
}
function fixOldIePngLogo()
{
	var img = document.getElementById('logo');
	var strNewHTML = '<span title="' + img.alt + '" style="width:' + img.width + 'px;height:' + img.height + 'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + img.src + '\', sizingMethod=\'scale\');"></span>'; 
	img.outerHTML = strNewHTML;
}
function popUp(link)
{
    window.open(link.getAttribute('href'), 'gehelp', "menubar=no,toolbar=no,width=620,height=450,status=no,resizable=yes,directories=no,scrollbars=yes");
}
function submissionCheck()
{
    var message = 'Please fill in this box';
    this.fieldsValid = true;
    jQuery('form input.warning, form textarea.warning, form select.warning')
            .removeClass('warning')
            .focus(null)
        ;
    jQuery('li.required input:not([type="password"]):not([type="file"])[value="'+message+'"], li.required textarea[value="'+message+'"]')
            .attr('value', '')
            .focus(function(){
                if (this.value == message) this.value = '';
                this.className = '';
              })
            .each(function(){
                this.form.fieldsValid = false;
              })
        ;
    if (jQuery('body.properties.add, body.properties.edit').length > 0)
    {
        if (!checkCheckBoxGroupSelection('accommodationtypes'))
        {
            this.fieldsValid = false;
            jQuery('form input[name="accommodationtypes[]"]').addClass('warning');
            alert('Please select at least one Accommodation Type');
        }
        if (!checkCheckBoxGroupSelection('holidaytypes'))
        {
            this.fieldsValid = false;
            jQuery('form input[name="holidaytypes[]"]').addClass('warning');
            alert('Please select at least one Holiday Type');
        }
    }
    jQuery('li.required input[value=""], li.required textarea[value=""], li.required textarea[value=" "], li.required select[value=""]')
        .addClass('warning')
        .focus(function(){
            this.className = '';
           })
        .click(function(){
            this.className = '';
           })
        ;
    jQuery('li.required input:not([type="password"]):not([type="file"])[value=""], li.required textarea[value=""], li.required textarea[value=" "], li.required select[value=""]')
            .attr('value', message)
            .focus(function(){
                if (this.value == message) this.value = '';
                this.className = '';
               })
            .each(function(){this.form.fieldsValid = false;})
        ;
    return (jQuery('.warning').length == 0);
}
function checkCheckBoxGroupSelection(baseElementId)
{
    if (!baseElementId) return null;
    form = document.forms[0];
    if (!form) return null;
    var haveCheck = false;
    var checks = new Array();
    var allChecks = document.getElementsByTagName('input');
    if (!allChecks) return null;
    for (var i = 0; i < allChecks.length; i++)
    {
        if (allChecks.item(i).name && allChecks.item(i).name == baseElementId + '[]')
        {
            checks[checks.length] = allChecks.item(i);
        }
    }
    var lowestIdNum = 0;
    var lastIdNum;
    if (!checks) return null;
    for (var i = 0; i < checks.length; i++)
    {
        var matches = checks[i].id.match(/([0-9]+)/);
        lastIdNum = matches[0];
        if (lowestIdNum === 0) 
        {
            lowestIdNum = parseInt(lastIdNum);
        }
        else if(lastIdNum < lowestIdNum)
        {
            lowestIdNum = parseInt(lastIdNum);
        }
    }
    var i = lowestIdNum;
    while (i > 0)
    {
        var check = document.getElementById(baseElementId + i);
        if (check)
        {
            if (check.checked)
            {
                haveCheck = true;
                break;
            }
            else
            {
                i++;
            }
        }
        else
        {
            i = -1;
        }
    }
    return haveCheck;
}
jQuery(document).ready(function(){
		if (jQuery.browser.msie && jQuery.browser.version < 7) fixOldIePngLogo();
        if (jQuery.browser.mozilla) mozFormFix();
        if (jQuery.browser.msie || jQuery.browser.opera) enableAddBookmark();
        enableImageSelector();
        jQuery('form').submit(function (){return submissionCheck();});
        jQuery('body.pages p:last-child').addClass('last-child');
        jQuery('body.properties div#mainleft p').slice(-1).css('border', 'none');
        jQuery('div#main div.property').slice(-1).css('border', 'none');
		var slashUls = jQuery('div#main ul.slash');
		for (var i = 0; i < slashUls.length; i++)
		{
			jQuery('li', slashUls.get(i)).slice(-1).css('background', 'none').css('padding-right', '0').css('margin-right', '0');
		}
		jQuery('div#imageselector img + img').css('margin-left', '19px');
        if(jQuery.browser.msie) jQuery('div#main input[type=checkbox]').css('width', '12px');
        enableHelpPopUp();
        enableAgreeTerms();
   }
);