﻿$(document).ready(docReady);

function docReady()
{
    if ($("#gaScript").length > 0) //if google analytics is enabled
    {
        $("a[href^='/eng/Forms_and_Publications/Application_Form']").click(gaTrack);
        $("a[href^='/eng/Forms_and_Publications/Fatal_Application_Form']").click(gaTrack);
        $("a[href='/eng/Contact_Us/Contact_InjuriesBoard_ie.html']").click(gaTrack);
        $("a[href='mailto:enquiries@injuriesboard.ie']").click(gaTrack);
        $("a[href='https://piab-injuriesboard.ie/index.aspx']").click(gaTrack);
        $("a[href='/eng/Enquiry/']").click(gaTrack);
        
        $("input[type='submit'][id$='btnEnquirySubmit']").click(gaTrack);
    }
}

function gaTrack(eventData)
{
    var eventElem = eventData.currentTarget;

    if (eventElem.tagName.toLowerCase() == "a")
    {
        if (eventElem.href == 'https://piab-injuriesboard.ie/index.aspx')
            _gaq.push(['_trackEvent', 'Online Engagement', 'Proceed to Application form', eventElem.href]);
	else if (eventElem.innerHTML.toLowerCase().indexOf("application form") > -1 || eventElem.innerHTML.toLowerCase().indexOf("fatal application form") > -1)
        {
            var applicationFormName = eventElem.innerHTML.toLowerCase();
            applicationFormName = applicationFormName.replace(' (word)', '');
            applicationFormName = applicationFormName.replace(' (pdf)', '');
            applicationFormName = applicationFormName.replace(' word', '');
            applicationFormName = applicationFormName.replace(' pdf', '');

            applicationFormName = applicationFormName.replace('application form a', 'Application Form A');
            applicationFormName = applicationFormName.replace('application form b', 'Application Form B');
            applicationFormName = applicationFormName.replace('fatal application form', 'Fatal Application Form');

            _gaq.push(['_trackEvent', 'Online Engagement', 'Request for ' + applicationFormName, eventElem.href]);
        }
        else if (eventElem.href == '/eng/Contact_Us/Contact_InjuriesBoard_ie.html' || eventElem.href == 'mailto:enquiries@injuriesboard.ie')
            _gaq.push(['_trackEvent', 'Online Engagement', 'Fulfill Contact Us Form', eventElem.href]);
        else if (eventElem.href == '/eng/Enquiry/')
            _gaq.push(['_trackEvent', 'Online Engagement', 'Register for an information pack', eventElem.href]);
    }
    else if (eventElem.tagName.toLowerCase() == "input")
    {
        if (eventElem.id.indexOf('btnEnquirySubmit') > -1)
            _gaq.push(['_trackEvent', 'Online Engagement', 'Submit Enquiry', $("select[id$=ddlQueryType] option:selected").text()]);
    }
}
