var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var agt = navigator.userAgent.toLowerCase();
var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')
    == -1) && (agt.indexOf('webtv') == -1));
var is_nav5up = (is_nav && (is_major >= 5));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav4 = (is_nav && (is_major == 4));
var is_ie = (agt.indexOf("msie") != -1);
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0") == -1) );
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5up = (is_ie && !is_ie3 && !is_ie4);
var is_mac = (agt.indexOf("mac") != -1);
var is_macppc = (is_mac && ((agt.indexOf("ppc") != -1) || (agt.indexOf("powerpc") != -1)));

var winSpecs = new Array();
winSpecs['default'] = "toolbar=no,scrollbars=yes,resizable=yes,location=no,menubar=0,width=895,height=500";
winSpecs['help'] = winSpecs['default'];
winSpecs['bpdns'] = winSpecs['default'];

var locHelpType = 'australia';

function refineSource(theForm, dataSources)
{
    if (!theForm.elements['refineResults'][1].checked || !theForm.elements['searchIndex'][0].checked)
    {
        theForm.elements['yellowpage'].value = '';
        theForm.elements['whitepage'].value = '';
        theForm.elements['localWeb'].value = '';
        theForm.elements['whitepageBG'].value = '';

        for (var i = 0; i < dataSources.length; i++)
        {
            theForm.elements[dataSources[i]].value = 'on';
        }
    }
    else
    {
        theForm.elements['refineResults'][0].checked = true;
        refineSource(theForm, new Array('yellowpage', 'localWeb'))
        enableAdvancedFields(document.forms, new Array('findAnyWords', 'findWithoutWords'), document.forms['searchForm'])
    }
}

function enableField(theForms, elementId, form)
{
    for (var i = 0; i < theForms.length && theForms[i].name == form.name; i++)
    {
        theForms[i].elements[elementId].style.background = '#ffffff';
        if (theForms[i] != form && theForms[i].elements['searchIndex'].length)
        {
            theForms[i].elements['searchIndex'][1].checked = true;
        }
    }
}

function enableAdvancedFields(theForms, elementIds, form)
{
    for (var i = 0; i < elementIds.length; i++)
    {
        enableField(theForms, elementIds[i], form);
    }
}

function useNewWindow(type, url)
{
    if (type == "" || type == null || !winSpecs[type])
    {
        type = 'default';
    }

    var popup = window.open(url, type, winSpecs[type]);
    popup.focus();

    return false;
}

function useNewWindowCTC(type, url, src, id)
{
    if (type == "" || type == null || !winSpecs[type])
    {
        type = 'default';
    }

    var popup = window.open(url, type, winSpecs[type]);
    popup.focus();
    omnitureCTC(src, id)
    return false;
}

function validateSearch(theForm)
{
    var find = theForm.elements['find'];
    if (find.value == '' && theForm.elements['location'].value == '')
    {
        alert('Please enter a search and/or location term.');
        return false;
    }
    recordClickOfSearchButton();
    return true;
}

function setFocus()
{
    document.forms['searchForm'].elements['find'].focus();
}

function validateSyndication(theForm)
{
    var myRegxp = /^[^a-zA-Z]+$/;
    var nameRegxp = /^[a-zA-Z\s\W]+$/;
    var blHasErrors = false;
    var errorMsg = "The following errors occured while validating your input.\nPlease provide the following information:\n"

    if (theForm.contactName.value == "")
    {
        errorMsg += " -  you need to enter your name\n";
        blHasErrors = true;
    }
    if (theForm.emailAddress.value == "")
    {
        errorMsg += " -  you need to enter your e-mail address\n";
        blHasErrors = true;
    }
    if (theForm.emailAddress.value != "")
    {
        var email = theForm.emailAddress.value
        var AtPos = email.indexOf("@")

        email = trimTrailingWhiteSpace(email);
        if (AtPos == -1) {
            errorMsg += " -  You need to enter a valid email address\n"
            blHasErrors = true;
        }
    }
    if (theForm.phoneNumber.value == "")
    {
        errorMsg += " -  you need to enter your phone number\n";
        blHasErrors = true;
    }
    if (theForm.phoneNumber.value != "")
    {
        if (!myRegxp.test(theForm.phoneNumber.value)) {
            errorMsg += " -  you need to enter a valid phone number\n";
            blHasErrors = true;
        }
    }
    if (theForm.contactName.value != "")
    {
        if (!nameRegxp.test(theForm.contactName.value)) {
            errorMsg += " -  you need to enter a valid contact name\n";
            blHasErrors = true;
        }
    }
    if (theForm.pageViews.value != "")
    {
        if (!myRegxp.test(theForm.pageViews.value)) {
            errorMsg += " -  you need to enter a valid number for page views\n";
            blHasErrors = true;
        }
    }
    if (theForm.australianPercent.value != "")
    {
        if (!myRegxp.test(theForm.australianPercent.value)) {
            errorMsg += " -  you need to enter a valid percentage number for pages generated in Australia\n";
            blHasErrors = true;
        }
    }
    if (theForm.subscribers.value != "")
    {
        if (!myRegxp.test(theForm.subscribers.value)) {
            errorMsg += " -  you need to enter a valid percentage number for ISP subsribers\n";
            blHasErrors = true;
        }
    }
    if (blHasErrors)
    {
        alert(errorMsg);
        return false;
    }
    else
    {
        return true;
    }
}

function trimTrailingWhiteSpace(myString) {
    return myString.replace(/\s*$/, '');
}

function recordClick(src)
{
    <!-- omniture tracking for links -->
    if (src != '') {
        var s = s_gi(s_account);
        s.linkTrackVars = 'eVar24,events';
        s.linkTrackEvents = 'event6';
        s.events = 'event6';
        s.eVar24 = src;
        void(s.tl(true, 'o', src));
        s.eVar24 = '';
        s.events = '';
    }
}

function recordClickOfSearchButton()
{
    <!-- omniture tracking for click of SEarch Button -->
        var s = s_gi(s_account);
        s.events = 'event8';
        s.linkTrackVars = 'events';
        s.linkTrackEvents = s.events;
        void(s.tl(true, 'o', 'searchSubmit'));
        s.events = '';
}

function recordClickRelated(src)
{
    <!-- omniture tracking for realted links -->
    if (src != '') {
        var s = s_gi(s_account);
        s.linkTrackVars = 'eVar24,events';
        s.linkTrackEvents = 'event7';
        s.events = 'event7';
        s.eVar24 = src;
        void(s.tl(true, 'o', src));
        s.eVar24 = '';
        s.events = '';
    }
}

function recordClickPopularSearch(src)
{
    <!-- omniture tracking for popular search links -->
    if (src != '') {
        var s = s_gi(s_account);
        s.linkTrackVars = 'eVar24,events';
        s.linkTrackEvents = 'event8';
        s.events = 'event8';
        s.eVar24 = src;
        void(s.tl(true, 'o', src));
        s.eVar24 = '';
        s.events = '';
    }
}


function omnitureCTR(src, position)
{
    //omniture tracking for Click through positions
    if (src != '' && position != '') {
        var s = s_gi(s_account);
        s.linkTrackVars = 'prop37,eVar24,eVar39,events';
        s.linkTrackEvents = 'event6';
        s.events = 'event6';
        s.eVar24 = src;
        s.prop37 = position;
        s.eVar39 = position;
        s.tl(true, 'o', src);
        s.eVar24 = '';
        s.events = '';
        s.prop37 = '';
        s.eVar39 = '';
    }
}

function omnitureCTC(src, id)
{
    //omniture tracking for c2c and listing ID
    if (src != '' && id != '') {
        var s = s_gi(s_account);
        s.events = 'event6';
        s.eVar24 = src;
        s.eVar40 = id;
        s.linkTrackVars = 'eVar24,eVar40,events';
        s.linkTrackEvents = 'event6';
        s.tl(true, 'o', src);
        s.eVar24 = '';
        s.events = '';
        s.eVar40 = '';
    }
}

var newWin;
function recordClickAndOpenURL(url, src, position)
{
    location.href = url;
    omnitureCTR(src, position);
    return false;
}

function recordClickAndOpenWIN(url, target, src, category)
{
    if (target == 'sensis_window') {
        if (!newWin || !newWin.location) {
            newWin = window.open(url, target);
        } else {
            newWin.location.href = url;
        }
        newWin.focus();
    } else if (target == "_parent") {
        parent.location.href = url;
    } else if (target == "_blank") {
        newWin = window.open(url, target);
        newWin.focus();
        newWin = null;
    } else {
        location.href = url;
    }
    if (category == "related") {
        recordClickRelated(src);
    } else if (category == "popularsearch") {
        recordClickPopularSearch(src);
    } else {
        recordClick(src);
    }
    return false;
}


function homePageTheme(src) {
    // omniture tracking for HomePage Themes
    if (src != '') {
        var s = s_gi(s_account);
        s.linkTrackVars = 'prop15,eVar25';
        s.prop15 = src;
        s.eVar25 = src;
        void(s.tl(true, 'e', src));
    }
}

function loadInMain(url) {
    var mainWin;
    var thisWin;
    if (window.opener) {
        mainWin = window.opener;
        thisWin = window;
    } else if (top.window.opener) {
        mainWin = top.window.opener;
        thisWin = top.window;
    }

    if (mainWin) {
        mainWin.location.href = url;
        mainWin.focus();
        thisWin.close()
    }
}

/* code for the network nav bar */
var scaCore = {

    /**
     * event listener.
     *
     * @param obj name of the object to be listened for.
     * @param evType the event type.
     * @param fn the function.
     * @param useCapture boolean to enable event capture.
     */
    addEvent: function(obj, evType, fn, useCapture) {
        if (useCapture == null)
            useCapture = true;

        if (obj.addEventListener) {
            obj.addEventListener(evType, fn, useCapture);
            return true;
        } else if (obj.attachEvent) {
            return obj.attachEvent("on" + evType, fn);
        } else {
            obj['on' + eventType] = fn;
        }
    },

    /**
     * check for alpha numeric chars.
     *
     * @param string to test.
     * @return boolean true if special chars exist.
     */
    checkForSpecialCharacters: function(value) {
        var regex = /[^A-Za-z0-9_ ]/;
        return regex.test(value);
    },

    cloneObject: function(obj) {
        var clone = new Object();
        for (var property in obj) {
            clone[property] = obj[property];
        }
        return clone;
    },

    /**
     * trims whitespace.
     *
     * @param string to trim.
     * @return the trimmed string.
     */
    trim: function(value) {
        value = value.replace(/^\s+/, '');
        value = value.replace(/\s+$/, '');
        return value;
    }
}

function bpDnsHelp(type, url, src) {
    var popup = window.open(url, type, winSpecs[type]);
    popup.focus();
    recordClick(src);
    return false;
}

/* WhitePage Search Service scripts */
var nameSuggestionText = 'e.g. Telstra';
var wpResSuggestText = 'e.g. Smith, J';
var locSuggestText = 'e.g. South Bank, VIC';


function cleanSelfSuggestingFields(findId, locId) {
    clearSuggested(findId, 'clickedField');
    clearSuggested(locId, 'clickedField');
}

function resetSelfSuggestingFields(findId, optionId, locId) {

document.forms['searchForm'].elements['searchButton'].focus();

    if (findId != null) {
        var findField = document.getElementById(findId);

        var initText;
        if (optionId == 'yp') {
            initText = nameSuggestionText;
        } else {
            initText = document.getElementById(optionId).checked ? nameSuggestionText : wpResSuggestText;
        }

        if (findField.value == "" || initText == findField.value) {
            initSuggested(findId, "initField", initText);
        } else {
            initSuggested(findId, "clickedField");
        }

    }

    if (locId != null) {
        var locField = document.getElementById(locId);
        if (locField.value == "" || locSuggestText == locField.value) {
            initSuggested(locId, "initField", locSuggestText);
        } else {
            initSuggested(locId, "clickedField");
        }
    }
}

function initSuggested(fieldId, initClass, initText) {
    var field = document.getElementById(fieldId);
    field.className = initClass;
    if (initText != null)
        field.value = initText;
}

function changeInitText(fieldId, newText) {
	var field = document.getElementById(fieldId);
	var currentClass = field.className;
	if (currentClass == 'initField') {
		field.value = newText;
	}
}

function clearSuggested(fieldId, clickedClass) {
    var field = document.getElementById(fieldId);
    var currentClass = field.className;
    if (currentClass != clickedClass) {
        field.className = clickedClass;
        field.value = '';
    }
}
