//Include Opera stylesheet if needed
if (navigator.appName == "Opera") {
	document.write("<link href='WEB-INF/includes/css/opera.css' rel='stylesheet' type='text/css' />"); }


var Site = {
    start: function() {
        ExternalLinks.start();
        Navigation.start();
        Language.start();
    }
}

var Language = {
	start: function(){
		var nav = $('langnav');
        var langLink = document.getElements("#langnav li");
        var ul = langLink.getElements('ul')[0];
        var hyper = langLink.getElements('a.choose')[0];

		langLink.addEvent('mouseover',function(){
				ul.addClass('langNavVisible');
			});
		langLink.addEvent('mouseout',function(){
				ul.removeClass('langNavVisible');
			});
		}
};


var ExternalLinks = {
    start: function() {
        if (!document.getElementsByTagName) return;
        var anchors = document.getElementsByTagName('a');
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
                anchor.target = '_blank';
        }
    }
};

var Navigation = {
    start: function() {
        var nav = $('navigation');
        
        // safari hacks
        if(window.webkit) {
            var topLinks = document.getElements('#navigation a[class]');
            topLinks.each(function(topLink) {
                topLink.addEvent('mouseover', function() {
                    var topLinks = document.getElements('#navigation a[class]');
                    topLinks.each(function(topLink) {
                        topLink.setStyle('background-position', '0px -25px');
                    });
                });
            });
            
            $('mast_image_holder').addEvent('mousemove', function() {
                var topLinks = document.getElements('#navigation a[class]');
                topLinks.each(function(topLink) {
                    topLink.setStyle('background-position', '0px -25px');
                });
            });
            
            var bottomUls = nav.getElements('li ul li ul ');
            bottomUls.each(function(bottomUl) {
                var y = bottomUl.parentNode.getCoordinates()['top'] - 35;
                bottomUl.setStyle('top', y + 'px');
            });
        }
        
        // normal browsers
        for(var i=0; i<nav.childNodes.length; i++) {
            if(nav.childNodes[i].tagName == 'LI') {
                var li = $(nav.childNodes[i]);
                var ul2 = li.getElements('ul')[0];
                if(ul2) {
                    var ul2s = nav.getElements('ul');
                    ul2s.each(function(ul2) {
                        ul2.link = $(ul2.parentNode).getElements('a')[0];
                        
                        ul2.link.addEvent('mouseover', function() {
                            this.setStyle('background-position', '0px 0px');
                            this.setStyle('background-color', 'white');
                        });
                        ul2.link.addEvent('mouseout', function() {
                            this.setStyle('background-position', '0px -25px');
                            this.setStyle('background-color', '');
                        });
                        ul2.addEvent('mouseover', function() {
                            this.link.setStyle('background-position', '0px 0px');
                        });
                        ul2.addEvent('mouseout', function() {
                            this.link.setStyle('background-position', '0px -25px');
                        });
                   });
                }
            }
        }
        
        // ie7 hacks - this is required for sealine's version of ie7
        // for some reason the standard test doesn't work on their browser as it
        // indentifies itself as ie6... this is to prevent the ie6 JS running on their browser
        var isIe7 = (navigator.userAgent.indexOf('MSIE 7.0') > 0);
        
        // ie6 hacks
        if(window.ie6 && !isIe7) {
            var ul2s = nav.getElements('ul');
            ul2s.each(function(ul2) {
                ul2.link.addEvent('mouseover', function() {
                    var parentUl = this.parentNode.parentNode;
                    var subUls = parentUl.getElements('ul');
                    subUls.each(function(subUl) {
                        subUl.setStyle('left', '-999px');
                    });
                    
                    var ul = $(this.parentNode).getElements("ul")[0];
                    var left = this.getPosition()['x'] - $('navigation').getPosition()['x'];
                    ul.setStyle('left', left + 'px');
                });
                
                ul2.addEvent('mouseleave', function() {
                    this.setStyle("left", "-999px");
                });
            });
            
            nav.addEvent('mouseleave', function() {
                var uls = this.getElements('ul')
                uls.each(function(ul2) {
                    ul2.setStyle('left', '-999px');
                });
            });
            
            var links = nav.getElements('a');
            links.each(function(link) {
                var uls = $(link.parentNode).getElements('ul');
                if(uls.length == 0) {
                    link.addEvent('mouseover', function() {
                        var ul = link.parentNode.parentNode;
                        var childUls = ul.getElements('ul');
                        childUls.each(function(childUl) {
                            childUl.setStyle('left', '-999px');
                        });    
                    });
                    
                }
            });
        }
    },
    
    ie6HideAll: function() {
        var ul2s = $('navigation').getElements('ul');
        ul2s.each(function(ul2) {
            ul2.setStyle('left', '-999px');
        });
    }
};

window.addEvent('domready', function(){Site.start();});

//Register page validation
function checkForm() {
	var f = document.Form1;
	var errors = 0;
	var message = "";
			    
	if( f.r_name.value == "" )
	{
		errors++;
		message += "- No name was entered.\n";
	}	
	if(f.r_email.value == "" )
	{
		errors++;
		message += "- No email was entered.\n";
	}
	if(f.r_address1.value == "" )
	{
		errors++;
		message += "- No address line 1 was entered.\n";
	}
	if(f.r_address2.value == "" )
	{
		errors++;
		message += "- No address line 2 was entered.\n";
	}
	if(f.r_town.value == "" )
	{
		errors++;
		message += "- No town was entered.\n";
	}
	if(f.r_country.value == "" )
	{
		errors++;
		message += "- No country was entered.\n";
	}
	if(f.r_postcode.value == "" )
	{
		errors++;
		message += "- No postcode was entered.\n";
	}
	if(f.r_make.value == "" )
	{
		errors++;
		message += "- No boat make was entered.\n";
	}
	if(f.r_model.value == "" )
	{
		errors++;
		message += "- No boat model was entered.\n";
	}
	if(f.r_location.value == "" )
	{
		errors++;
		message += "- No boat location was entered.\n";
	}
	if(f.r_yearreg.value == "" )
	{
		errors++;
		message += "- No boat year of registration was entered.\n";
	}
	
	if( errors > 0 )
	{
		alert("Please complete the following details:\n" + message);
	}
	else
	{
		f.submit();
	}
}

//Popup windows
function OpenShipping() {
	var features = "scrollbars=1";
	var myWidth = 455;
	var myHeight = 500;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("shipping_popup.aspx","shipping",features);
}
function OpenReturns() {
	var features = "";
	var myWidth = 355;
	var myHeight = 450;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("returns_popup.aspx","returns",features);
}

function OpenDemoRegister() {
	var features = "scrollbars=1";
	var myWidth = 480;
	var myHeight = 450;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("demo_register.aspx","demoregister",features);
}

function OpenDemoMoreInformation() {
	var features = "scrollbars=1";
	var myWidth = 480;
	var myHeight = 450;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("demo_moreinformation.aspx","demomoreinformation",features);
	}

function OpenShipping2() {
	var features = "scrollbars=1";
	var myWidth = 455;
	var myHeight = 500;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("../shipping_popup.aspx","shipping",features);
}
function OpenReturns2() {
	var features = "";
	var myWidth = 355;
	var myHeight = 450;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	features+=',width='+myWidth+',height='+myHeight;
	window.open("../returns_popup.aspx","returns",features);
}