﻿//Include the opera CSS file if required
if (navigator.appName == "Opera") {
	document.write('<link href="/Includes/css/opera.css" rel="stylesheet" type="text/css" media="all" />'); }

//Include the MAC stylesheet if required
if ((navigator.appVersion.indexOf("Mac") != -1)) {
	document.write('<link href="/Includes/CSS/mac.css" rel="stylesheet" type="text/css" media="all" />'); }
	
	
if ((navigator.appVersion.indexOf("Windows") != -1) && (navigator.appVersion.indexOf("Safari") != -1)) {
	document.write('<link href="/Includes/CSS/winsafari.css" rel="stylesheet" type="text/css" media="all" />'); }

	
var Site = {
	start: function() {
	    ExternalLinks.start();
	    DynamicContactForm.start();
	    ClearDefault.start();
	    ContactAccordion.start();
	    Profiles.start();
	    
    }
};

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];
            try {
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
                anchor.target = '_blank';
            }catch(err) {}
        }
    }
};

var DynamicContactForm = {
    start: function() {
        var formElement = $('enquiry');
        var linkElement = $$('.enquirylink')[0];
        
        if (!formElement) {
            return;
        }
        
        var fx = new Fx.Styles(formElement, {duration:800, wait:false});
        
        linkElement.href = "javascript:void(0);";
        
	    linkElement.addEvent('click', function(){
	        if (formElement.getStyle('height') == "0px") {
                linkElement.setStyle('background-image', 'url(../../images/uparrow.gif)');
                formElement.setStyle('border', 'solid 1px #DAE1EB');
                fx.start({
                    'height' : '465px',
                    'padding' : '10px',
                    'width' : '197px',
                    'opacity' : '100'
                });
            }
            else {
                linkElement.setStyle('background-image', 'url(../../images/downarrow.gif)');
                fx.start({
                    'height' : '0',
                    'padding-top' : '0',
                    'margin-top' : '0',
                    'opacity' : '0'
                });
                formElement.setStyle('border-top', '0');
            }
        });
        
        var closeLink = $('enquiryclose');
        closeLink.href = "javascript:void(0);";
        
        closeLink.addEvent('click', function() {
            linkElement.setStyle('background-image', 'url(../../images/downarrow.gif)');
            fx.start({
                'height' : '0',
                'padding-top' : '0',
                'margin-top' : '0',
                'opacity' : '0'
            });
            formElement.setStyle('border-top', '0');
        });
    }	               
}

var ClearDefault = {
    start: function() {
        var els = $$('.clearDefault');
        els.each(function(el) {
            el.addEvent('focus', function() {
                if (el.defaultValue==el.value) {
                    el.value = ""
                 }
            });
        });
    }
}

window.addEvent('domready', Site.start);

function validateEnquiryForm(culture) {
    var message = "";
    var errorCount = 0;
    
    if($$('.formTitle').getValue() == "") 
    {
        if(culture == "en-GB") message += "Please give your title\n";
        else message += "Por favor, dejar su título\n";
        errorCount++;
    }
    
    if($$('.formFirstName').getValue() == "") 
    {
        if(culture == "en-GB") message += "Please give your first name\n";
        else message += "Por favor, dejar su nombre\n";
        errorCount++;
    }
    
    if($$('.formLastName').getValue() == "") 
    {
        if(culture == "en-GB") message += "Please give your last name\n";
        else message += "Por favor, dejar su apellido\n";
        errorCount++;
    }
    
    if(!/^[0-9\s-()+]+$/.test($$('.formTelephone').getValue())) 
    {
        if(culture == "en-GB") message += "Please give a valid telephone number\n";
        else message += "Por favor, dejar un número de teléfono\n";
        errorCount++;
    }
    
    var emailAddress = $$('.formEmail').getValue();
    if (emailAddress = '.') 
        emailAddress = '';
    if(emailAddress != '')
    {
        var emailValidate = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
        if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)) 
        {
            if(culture == "en-GB") message += "You must give a valid email address\n";
            else message += "Debe introducir una dirección válida\n";
            errorCount++;
        }
    }
    
    var enquiryValue = $$('.formEnquiry').getValue();
    var maxEnquiryLength = 950;
    if(enquiryValue == "")
    {
        if(culture == "en-GB") message += "Please enter an enquiry\n";
        else message += "Debe mandarnos su petición\n";
        errorCount++;
    }
    else if(enquiryValue.length > maxEnquiryLength)
    {
        if(culture == "en-GB") message += "The enquiry can be no more than "+maxEnquiryLength+" characters long. The entered enquiry is "+enquiryValue.length+" characters long.\n";
        else message += "Su petición no puede ser más de "+maxEnquiryLength+" caracteres. Su petición consiste de "+enquiryValue.length+" caracteres.\n";
        errorCount++;
    }

    if (!($$('.formcheckbox input')[0].checked))
    {
        if(culture == "en-GB") 
        {
            message += "Please read our terms and conditions then tick the box\n";
        }
        else 
        {
            message += "Por favor lea los términos y condiciones; a continuación marque el recuadro.\n";
        }
        
        errorCount++;
    }
    
    if (errorCount == 0) 
    {
        return true;
    }
    else 
    {
        alert(message);
        return false;
    }
}

// fix rad editor width
function OnClientModeChange(editor) {
    var htmlArea = document.getElementById("RadEContentTextarea" + editor.Id); 
    htmlArea.style.width = (editor.GetWidth() - 24) + "px";
}

var ContactAccordion = {
    start: function() {
        var togglers = $$('.toggler');
        togglers.each(function(item,index){
            item.addEvent('mouseover', function(){
                document.body.style.cursor = 'hand';
            });
        });
    
        var accordion = new Accordion('h3.atStart', 'div.atStart', {
            opacity: false,
            alwaysHide: true,
            show: -1
        }, $('accordion')); 
    }
}

var Profiles = {
    start: function() {
        // Get profile links
        var profileLinks = $$('.profileLink');
        
        // Get more about links
        var moreAboutLinks = $$('.moreAboutLink');

        // Add click event to profiles
        profileLinks.each(function(profileLink) {
            profileLink.addEvent('click', function() {
            
                // Get the id of the link
                var fullId = profileLink.getAttribute('id');
                $(fullId).addClass('selected');
                var fullIdEdited = fullId.replace('profile', 'moreAbout');
                $(fullIdEdited).removeClass('selected');
                
                // Remove the string Link from the id
                var editedId = fullId.replace('Link', '');
                
                // Find the div that needs hiding
                $(editedId).addClass('hidden');
                
                // Find the div with this id and show it
                var otherID = editedId.replace('profile', 'moreAbout');
                $(otherID).removeClass('hidden');
            });
        });
        
        // Add click event to more about
        moreAboutLinks.each(function(moreAboutLink) {
            moreAboutLink.addEvent('click', function() {
                // Get the id of the link
                var fullId = moreAboutLink.getAttribute('id');
                $(fullId).addClass('selected');
                var fullIdEdited = fullId.replace('moreAbout', 'profile');
                $(fullIdEdited).removeClass('selected');
                
                // Remove the string Link from the id
                var editedId = fullId.replace('Link', '');
                
                // Find the div with this id and show it
                $(editedId).addClass('hidden');
                
                // Find the div that needs hiding
                var otherID = editedId.replace('moreAbout', 'profile');
                $(otherID).removeClass('hidden');
            });
        });
        
    }
}