var dValue = {};
var valid = false;
var eventName = "Moorilla Hobart International";
var eventQueryName = "HobartInternational";
var apiLoc = "/newsletter/subscribeHI.asp";

window.addEvent('domready',function() {

	//Livescores
    if ($$('a.livescores-btn')) {
        $$('a.livescores-btn').each(function(el) {
            el.removeProperty('target');
            var livescoresbtn = el.set({
                'href': '#',
                'events': {
                    'click': function(e) {
                        var clickEvent = new Event(e).stop();
                        if (openLSWindowTandR && pageTracker) {
                            var tracklivescores = '/outbound/livescores';
                            pageTracker._trackPageview(tracklivescores);
                            openLSWindowTandR(2009, '2', 'en', 3, '5505', 'www.hobartinternational.com.au');
                        }
                    }
                }
            });
        });
    }
	
    if ($('frmEmailUpdates')) {
        $$('input[type=text]').each(function(e, i) {
            e = e.getProperty('ID');
            dValue[i] = $(e).value;
            $(e).set({
                'events': {
                    'focus': function(e) {
                        this.setStyle('color', '#666');
                        this.value = (this.value == dValue[i] && this.value != '') ? (this.value = '') : (this.value);
                    },
                    'blur': function(e) {
                        this.value = (this.value == '' || this.value.length < 2) ? (dValue[i]) : (this.value.trim().stripScripts());
                    }
                }
            });
        });
        var FormFx = new Fx.Morph($('frmEmailUpdates'), {
            duration: 800,
            transition: Fx.Transitions.Sine.easeOut,
            link: 'chain'
        });
        $('subscribeButton').set({
            'events': {
                'click': function(e) {
                    e.stop();
                    if (FormValidation(valid, dValue)) {
                        $('frmEmailUpdates').fireEvent('submit');
                    }
                }
            }
        });
        $('frmEmailUpdates').set({
            'events': {
                'submit': function(e) {
                    if (FormValidation(valid, dValue)) {
                        $('frmEmailUpdates').set('send', {
                            url: apiLoc,
                            method: 'get',
                            autoCancel: true,
                            onRequest: function() {
                                var ajaxloader = new Element('div', {
                                    'id': 'ajaxloader'
                                }).inject('frmEmailUpdates', 'before');
                                FormFx.start({
                                    'opacity': 0.2
                                });
                            },
                            onSuccess: function() {
                                $('ajaxloader').dispose();
                                if ($('pErrorMessage')) {
                                    $('pErrorMessage').dispose();
                                }
                                FormFx.start({
                                    'opacity': 0,
                                    'height': 0,
                                    'complete': function() {
                                        var emailstatus = new Element('div', {
                                            'id': 'divEmailStatus',
                                            'opacity': 0,
                                            'styles': {
                                                'margin': '15px 0 10px 0',
                                                'width': '100%',
                                                'color': '#009bdf'
                                            },
                                            'html': '<p>Thank you ' + $('firstname').value + ' for your subscribing to TennisWorld.</p>'
                                        }).inject('frmEmailUpdates', 'before').fade(1);
                                    }
                                });
                                if (pageTracker) {
                                    pageTracker._trackPageview('/ajax/email-updates-form-submitted');
                                }
                            },
                            onFailure: function(e) {
                                $('ajaxloader').dispose();
                                FormFx.start({
                                    'opacity': 1
                                });
                                if (! ($('pErrorMessage'))) {
                                    var errormessage = new Element('p', {
                                        'id': 'pErrorMessage',
                                        'opacity': 0,
                                        'styles': {
                                            'clear': 'both',
                                            'color': '#ff0000',
                                            'margin-top': '10px',
                                            'width': '100%'
                                        },
                                        'html': 'There was a problem subscribing you to the newsletter. Please try again later.'
                                    }).inject('frmEmailUpdates', 'top').fade(1);
                                }
                            }
                        });
                        $('frmEmailUpdates').send(apiLoc + "?" + eventQueryName + "=true");
                    }
                }
            }
        });
    }
	
});

function FormValidation(valid, dValue) {
    if (firstname()) {
        if (lastname()) {
            if (emailValidator()) {
                if (privacypolicy()) {
                    if (! ($('tennisworld').checked)) {
                        $('tennisworld').value = false;
                    }
                    return true;
                } else {
                    $('spanPrivacy').highlight('#ff0000');
                }
            }
        }
    }
}

function emailValidator() {
    var emailFilter = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;
    if ((! (emailFilter.test($('email').value))) || $('email').value == 'Email address') {
        $('email').setStyle('color', 'red');
        $('email').highlight('#ececec');
        return false;
    } else {
        $('email').setStyle('color', '#666');
        return true;
    }
}

function firstname() {
    var alphaExp = /^[a-zA-Z]+(([\s|\-\'])[a-zA-Z]+)?$/;
    if ((! (alphaExp.test($('firstname').value))) || $('firstname').value == 'First name') {
        $('firstname').setStyle('color', 'red');
        $('firstname').highlight('#ececec');
        return false;
    } else {
        $('firstname').setStyle('color', '#666');
        return true;
    }
}

function lastname() {
    var alphaExp = /^[a-zA-Z]+(([\s|\-\'])[a-zA-Z]+)?$/;
    if ((! (alphaExp.test($('lastname').value))) || $('lastname').value == 'Last name') {
        $('lastname').setStyle('color', 'red');
        $('lastname').highlight('#ececec');
        return false;
    } else {
        $('lastname').setStyle('color', '#666');
        return true;
    }
}

function privacypolicy() {
    if (! ($('privacy').checked)) {
        $('privacy').highlight('#ececec');
        return false;
    } else {
        return true;
    }
}

//Live scores new window function 
// Open a window of the desired size in the centre of the screen.
function openLSWindowTandR(year, wkno, lang, tabno, eventid, ref_file, width, height, hasScrollBars) { // ADD NAME FIELD and make sure it get's focus!!!
    var theWidth = width;
    var theHeight = height;
    var scrollBars = "scrollbars";
    if (hasScrollBars == false) scrollBars = "scrollbars=0";
    if ((theWidth == "") || (theWidth == null)) theWidth = 500;
    if ((theHeight == "") || (theHeight == null)) theHeight = 668;
    var theLeft = (screen.availWidth - theWidth) / 2;
    var theTop = (screen.availHeight - theHeight) / 2;
    var strCheckRef = escape(ref_file);
    var lsURL = "http://www.protennislive.com/frameset.asp?year=" + year + "&wkno=" + wkno + "&lang=" + lang + "&tabno=" + tabno + "&eventid=" + eventid + "&ref=" + strCheckRef;
    var popupWin = window.open(lsURL, '_' + Math.round(Math.random() * 1000000), 'top=' + theTop + ',left=' + theLeft + ',menubar=0,toolbar=0,location=0,directories=0,status=0,' + scrollBars + ',width=' + theWidth + ', height=' + theHeight);
}

window.addEvent('load',function() {
    
	if (pageTracker) {
        $$('a.outbound', 'area.outbound').each(function(el) {
            el.addEvent('click',function() {
                var trackCurrentPage = (document.URL.replace(document.domain + '/', '').replace('http://', ''));
                trackCurrentPage = (trackCurrentPage == "") ? (document.URL.replace('http://www.', '')).replace('/', '') : trackCurrentPage;
                var trackOutboundURL = (el.get('href').replace('http://www.', '')).replace('http://', '');
                var tracklink = '/outbound/<FROM>' + trackCurrentPage + '<TO>' + trackOutboundURL;
                pageTracker._trackPageview(tracklink);
            });
        });
    }
	
//    $('atpBar').inject($('atpWrapper'), 'top');
 //   var atpBarFx = new Fx.Morph($('atpBar'), {
   //     duration: 800,
     //   transition: Fx.Transitions.Sine.easeOut
   // });
    //atpBarFx.set({
    //    'opacity': 0
    //});
    //atpBarFx.start({
      //  'opacity': 1,
       // 'display': 'block',
       // 'height': '29px'
    //});
	
});// JavaScript Document