﻿var adCount = 0;
var timeCount = 0;
var numAds = 0;
var switchDelay = 0;
var commitDelay = 0;
var adverts = new Array();

jQuery(document).ready(function() {
    swfobject.embedSWF("http://flash.locallinkup.com/llu_banner.swf", "recPanel", "178", "261", "9.0.0");
    numAds = parseInt(document.getElementById('hfNumAds').value);
    switchDelay = parseInt(document.getElementById('hfSwitchDelay').value);
    commitDelay = parseInt(document.getElementById('hfCommitDelay').value);
    adverts = document.getElementById('hfAdverts').value.split("^");
    getAd();
    if (document.getElementById('hfPageName').value == "contact") {
        showTab('findUs');
    }
});

function getAd() {
    if (timeCount == commitDelay | timeCount == switchDelay + commitDelay) {
        var d = new Date();
        var time = d.getTime();
        var strURL = "http://" + document.getElementById('hfLLUDomain').value + "/commitServer.ashx?id=" +
                    adverts[adCount].split(';')[0] + "&page=" + window.location + "&d=" + time;
        jQuery.get(strURL, function(data) { });
    }
    if (timeCount % switchDelay == 0) {
        var currCoName = adverts[adCount].split(';')[1];
        var currAdTitle = adverts[adCount].split(';')[2];
        var currAdText = adverts[adCount].split(';')[3];
        var currAdURL = adverts[adCount].split(';')[4];
        document.getElementById('topALink').innerHTML = currAdTitle;
        document.getElementById('topALink').href = currAdURL;
        document.getElementById('topALink').target = "_blank"
        document.getElementById('adText').innerHTML = currAdText;
        document.getElementById('botALink').innerHTML = "Visit Now";
        document.getElementById('botALink').href = currAdURL;
        document.getElementById('botALink').target = "_blank"
        adCount++;
    }
    timeCount += 10000;
    if (adCount == numAds) {
        adCount = 0;
    }
    setTimeout("getAd()", 10000);
}

function showLLU() {
    window.open('http://www.locallinkup.com', '', 'scrollbars=yes,menubar=yes,height=650,width=900,resizable=yes,toolbar=yes,location=yes,status=yes');
}

function showTab(id) {
    var colour = document.getElementById('hfColour').value;
    var whatTab = document.getElementById('whatWeDoTab');
    var whatTabText = document.getElementById('whatWeDoText');
    var findTab = document.getElementById('findUsTab');
    var findTabText = document.getElementById('findUsText');
    var descrPanel = document.getElementById('descrPanel');
    var latestNews = document.getElementById('latestNews');
    var mapPanel = document.getElementById('mapPanel');
    if (id == "whatDo") {
        descrPanel.style.display = "inline";
        latestNews.style.display = "inline";
        mapPanel.style.display = "none";
        if (colour == "Green") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017031.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017021.png)";
            findTabText.style.color = "#8bc53e";
        }
        if (colour == "Blue") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00016982.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00016981.png)";
            findTabText.style.color = "#25a9ff";
        }
        if (colour == "Red") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017047.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017046.png)";
            findTabText.style.color = "#ec1c23";
        }
        if (colour == "Orange") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017059.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017058.png)";
            findTabText.style.color = "#f7921e";
        }
        if (colour == "Emerald") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017152.png)";
            whatTabText.style.color = "#006666";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017151.png)";
            findTabText.style.color = "#33ffcc";
        }
        if (colour == "Pink") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017254.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017255.png)";
            findTabText.style.color = "#ff99ff";
        }
        if (colour == "Purple") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017270.png)";
            whatTabText.style.color = "#ffffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017269.png)";
            findTabText.style.color = "#cc33cc";
        }
        if (colour == "Silver") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017286.png)";
            whatTabText.style.color = "#4d4d4d";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017285.png)";
            findTabText.style.color = "#cccccc";
        }
        if (colour == "SkyBlue") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017307.png)";
            whatTabText.style.color = "#2e3192";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017306.png)";
            findTabText.style.color = "#99ffff";
        }
        if (colour == "Yellow") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017336.png)";
            whatTabText.style.color = "#754c24";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017335.png)";
            findTabText.style.color = "#ffff33";
        }
    }
    if (id == "findUs") {
        descrPanel.style.display = "none";
        latestNews.style.display = "none";
        mapPanel.style.display = "block";
        document.getElementById('gMapFrame').src = "http://maps.locallinkup.co.uk?compID=" + document.getElementById('hfCompanyID').value + "&h=280px&w=440px";
        if (colour == "Green") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017021.png)";
            whatTabText.style.color = "#8bc53e";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017031.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Blue") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00016981.png)";
            whatTabText.style.color = "#25a9ff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00016982.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Red") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017046.png)";
            whatTabText.style.color = "#ec1c23";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017047.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Orange") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017058.png)";
            whatTabText.style.color = "#f7921e";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017059.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Emerald") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017151.png)";
            whatTabText.style.color = "#33ffcc";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017152.png)";
            findTabText.style.color = "#006666";
        }
        if (colour == "Pink") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017255.png)";
            whatTabText.style.color = "#ff99ff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017254.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Purple") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017269.png)";
            whatTabText.style.color = "#cc33cc";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017270.png)";
            findTabText.style.color = "#ffffff";
        }
        if (colour == "Silver") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017285.png)";
            whatTabText.style.color = "#cccccc";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017286.png)";
            findTabText.style.color = "#4d4d4d";
        }
        if (colour == "SkyBlue") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017306.png)";
            whatTabText.style.color = "#99ffff";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017307.png)";
            findTabText.style.color = "#2e3192";
        }
        if (colour == "Yellow") {
            whatTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017335.png)";
            whatTabText.style.color = "#ffff33";
            findTab.style.backgroundImage = "url(http://image.locallinkup.com/PlinkPlus/00017336.png)";
            findTabText.style.color = "#754c24";
        }
    }
}

function setTextArea(type) {
    if (type == "clear") {
        if (document.getElementById('txtQuest').value == "Enter your question here, e.g. Do you stock drill bits?") {
            document.getElementById('txtQuest').value = "";
        }
    }
    else {
        if (document.getElementById('txtQuest').value == "") {
            document.getElementById('txtQuest').value = "Enter your question here, e.g. Do you stock drill bits?";
        }
    }
}

function setTextBox(element, type) {
    if (type == "clear") {
        if (element.value == "Please give your name" || element.value == "Please give your number") {
            element.value = "";
        }
    }
    else {
        if (element.value == "") {
            element.value = type;
        }
    }
}

function setEmail() {
    parent.location = 'mailto:' + document.getElementById('hfMailAddress').value;
}

function showWebsite() {
    window.open(document.getElementById('hfWebUrl').value, '', 'scrollbars=yes,menubar=yes,height=650,width=900,resizable=yes,toolbar=yes,location=yes,status=yes');
}

var radioClicked = "";

function setRadiosUnClicked() {
    document.getElementById('radioUrgent').style.backgroundImage = "url('http://image.locallinkup.com/PlinkPlus/00016958.png')";
    document.getElementById('radio0-5').style.backgroundImage = "url('http://image.locallinkup.com/PlinkPlus/00016958.png')";
    document.getElementById('radioToday').style.backgroundImage = "url('http://image.locallinkup.com/PlinkPlus/00016958.png')";
    document.getElementById('radioASAP').style.backgroundImage = "url('http://image.locallinkup.com/PlinkPlus/00016958.png')";
}

function setRadioClicked(id) {
    radioClicked = id;
    setRadiosUnClicked();
    document.getElementById(id).style.backgroundImage = "url('http://image.locallinkup.com/PlinkPlus/00016959.png')";
}

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function sendRequest() {
    if (document.getElementById('txtQuest').value != "" && document.getElementById('txtQuest').value != "Enter your question here, e.g. Do you stock drill bits?") {
        if (document.getElementById('txtQuestName').value != "" && document.getElementById('txtQuestName').value != "Please give your name") {
            if (document.getElementById('txtQuestMobile').value != "" && document.getElementById('txtQuestMobile').value != "Please give your number") {
                if (IsNumeric(document.getElementById('txtQuestMobile').value.replace(" ", ""))){
                    if (radioClicked != "") {
                        var strURL = "http://" + document.getElementById('hfLLUDomain').value + "/sendQuestionMessage.ashx";
                        if (radioClicked == "radioUrgent") {
                            radioClicked = "urgently";
                        }
                        if (radioClicked == "radio0-5") {
                            radioClicked = "less than 5 hours";
                        }
                        if (radioClicked == "radioToday") {
                            radioClicked = "within 24 hours";
                        }
                        if (radioClicked == "radioASAP") {
                            radioClicked = "as soon as possible";
                        }
                        jQuery.post(strURL, {
                            Name: document.getElementById('txtQuestName').value,
                            Number: document.getElementById('txtQuestMobile').value,
                            NumberOfCompany: document.getElementById('hfCompMobile').value,
                            Status: radioClicked,
                            Message: document.getElementById('txtQuest').value,
                            CompanyID: document.getElementById('hfCompanyID').value
                        }, function(data) {
                            if (data == "success") {
                                showMessage("quest_submitted", 3000);
                                radioClicked = "";
                                setRadiosUnClicked();
                                document.getElementById('txtQuest').value = "Enter your question here, e.g. Do you stock drill bits?";
                                document.getElementById('txtQuestName').value = "Please give your name";
                                document.getElementById('txtQuestMobile').value = "Please give your number";
                            }
                        });
                    }
                    else {
                        showMessage("select_response", 3000);
                    }
                }
                else {
                    showMessage("invalid_phone", 3000);
                }
            }
            else {
                showMessage("enter_mobile", 3000);
            }
        }
        else {
            showMessage("enter_name", 3000);
        }
    }
    else {
        showMessage("quest_to_submit", 3000);
    }
}

var currMessage;

function showMessage(messageID, time) {
    currMessage = messageID;
    setMessageStartPosition();
    var messageDiv = document.getElementById(currMessage);
    var newPos = ((GetHeight() - messageDiv.offsetHeight) / 2).toString() + "px";
    var bounce1 = (((GetHeight() - messageDiv.offsetHeight) / 2) - 20).toString() + "px";
    var bounce2 = (((GetHeight() - messageDiv.offsetHeight) / 2) - 10).toString() + "px";
    var timer = 0;
    jQuery("#" + currMessage).each(function(i) {
        timer = (timer * .8 + 800);
        jQuery(this).animate({ top: newPos }, timer);
    });
    timer = 0;
    jQuery("#" + currMessage).each(function(i) {
        timer = (timer * .8 + 400);
        jQuery(this).animate({ top: bounce1 }, timer);
        jQuery(this).animate({ top: newPos }, timer);
    });
    timer = 0;
    jQuery("#" + currMessage).each(function(i) {
        timer = (timer * .8 + 400);
        jQuery(this).animate({ top: bounce2 }, timer);
        jQuery(this).animate({ top: newPos }, timer);
    });
    setTimeout("hideMessageDiv()", time);
}

function setMessageStartPosition() {
    var messageDiv = document.getElementById(currMessage);
    document.getElementById(currMessage).style.left = ((GetWidth() - messageDiv.offsetWidth) / 2).toString() + "px";
}

function hideMessageDiv() {
    var timer = 0;
    jQuery("#" + currMessage).each(function(i) {
        timer = (timer * .8 + 1200);
        jQuery(this).animate({ top: "-400px" }, timer);
    });
}

function GetHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}

function GetWidth() {
    var x = 0;
    if (self.clientWidth) {
        x = self.clientWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}

function clearInput(input) {
    if (input.value == "e.g. Blocked Drain" || input.value == "e.g. Lancaster") {
        input.value = "";
    }
}

var locID = "";

function getKeyPressedLocation(event) {
    var text = document.getElementById('locsInput').value;
    if (text != "" && text != "e.g. Lancaster") {
        if (text.length > 2) {
            var d = new Date();
            var time = d.getTime();
            var strURL = "http://" + document.getElementById('hfLLUDomain').value + "/getLocationsHandler.ashx?search=" + text + "&d=" + time;
            //var strURL = "http://localhost:51291/Plink/getLocationsHandler.ashx?search=" + text + "&d=" + time;
            jQuery.get(strURL, function(data) {
                if (data == "locNotFound") {
                    showMessage('location_not_found', 2000);
                }
                else {
                    document.getElementById('locations').innerHTML = data;
                    locID = "";
                    showLocations();
                }
            });
        }
    }
}

function showLocations() {
    document.getElementById('locSearchPanel').style.display = "inline";
    document.getElementById('locationClose').style.display = "inline";
}

function hideLocations() {
    document.getElementById('locSearchPanel').style.display = "none";
    if (document.getElementById('locsInput').value == "") {
        document.getElementById('locsInput').value = "e.g. Lancaster";
    }
    document.getElementById('locationClose').style.display = "none";
}

function selectLocation(element) {
    locID = element.id;
    document.getElementById('locsInput').value = element.innerHTML;
    hideLocations();
}

function search() {

    showMessage("not_yet_active", 3000);

    //            var tooShort = false;
    //            var text = document.getElementById('keywordInput').value;
    //            if (locID != "") {
    //                if (text != "e.g. Blocked Drain" && text != "") {
    //                    var strings = text.split(' ');
    //                    for (var i = 0; i < strings.length; i++) {
    //                        if (strings[i].length < 4) {
    //                            tooShort = true;
    //                        }
    //                    }
    //                    if (tooShort) {
    //                        showMessage("short_words", 3000);
    //                    }
    //                    else {
    //                        
    //                    }
    //                }
    //            }
    //            else {
    //                showMessage('select_town_first', 2000);
    //            }
}