﻿//Static variables
var cssFilePath = 'http://www.teeofftimes.co.uk/whitelabel/{0}.css';
//var cssFilePath = 'http://localhost:1030/whitelabel/{0}.css';
var requestUrl = 'http://www.teeofftimes.co.uk/searchresults.aspx?postcode={0}&distance={1}&date={2}&cid={3}';
var headerText = 'Tee time search';


function ShowSearchBox(campaignId) {
    document.write('<div class="totSearchBox">');

    //Header
    document.write('<div class="header">');
    document.write('<h3>' + headerText + '</h3>');
    document.write('<p class="pb">Powered by <a href="http://www.teeofftimes.co.uk?cid=' + campaignId + '" target="_blank">Teeofftimes.co.uk</a></p>');
    document.write('<div class="clearFix"></div>');
    document.write('</div>');
    
    //Postcode
    document.write('<div class="r pcode"><div class="f">Postcode</div><input type="text" id="postcode" /></div>');

    //Distance
    document.write('<div class="r dst"><div class="f">Distance</div><select id="distance"></select></div>');
    var distanceDD = document.getElementById('distance');
    AddToDropdown(distanceDD, '5', '5 Miles', false);
    AddToDropdown(distanceDD, '10', '10 Miles', false);
    AddToDropdown(distanceDD, '15', '15 Miles', false);
    AddToDropdown(distanceDD, '20', '20 Miles', false);
    AddToDropdown(distanceDD, '25', '25 Miles', true);
    AddToDropdown(distanceDD, '50', '50 Miles', false);
    AddToDropdown(distanceDD, '75', '75 Miles', false);
    AddToDropdown(distanceDD, '100', '100 Miles', false);

    //Date
    document.write('<div class="r dte"><div class="f">Date</div><select id="searchDay"></select><select id="searchMonth"></select><select id="searchYear"></select></div>');
    CreateDateValues();

    //Validation
    document.write('<div class="v" id="validation"></div>');

    //Submit button
    document.write('<input type="hidden" id="campaignId" value="' + campaignId + '" />');
    document.write('<input type="button" value="Search" class="button" onclick="PerformRedirect();" />')

    document.write('<div class="clearFix"></div>');
    document.write('</div>');
}

//PerformRedirect
//Redirects to the correct page of the TOT website
function PerformRedirect() {

    var postcode = document.getElementById('postcode').value;
    var distance = document.getElementById('distance')[document.getElementById('distance').selectedIndex].value;
    var day = document.getElementById('searchDay')[document.getElementById('searchDay').selectedIndex].value;
    var month = document.getElementById('searchMonth')[document.getElementById('searchMonth').selectedIndex].value;
    var year = document.getElementById('searchYear')[document.getElementById('searchYear').selectedIndex].value;
    var campaignId = document.getElementById('campaignId').value;

    //Validate
    if (postcode == '' || postcode == null) {
        document.getElementById('validation').innerText = "You need to enter your postcode";
        return;
    }

    var url = String.format(requestUrl, postcode, distance, day + '-' + month + '-' + year, campaignId);
    //document.location.href = url;
    window.open(url, "TeeoffTimes");
}

//CreateDateValues
//Creates the values for the date picker
function CreateDateValues() {
    var monthtext = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var today = new Date()

    var dayElement = document.getElementById("searchDay");
    var monthElement = document.getElementById("searchMonth");
    var yearElement = document.getElementById("searchYear");

    for (var i = 1; i <= 31; i++) {
        dayElement.options[i] = new Option(i, i)
        dayElement.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true);
    }

    for (var m = 0; m < 12; m++) {
        monthElement.options[m] = new Option(monthtext[m], monthtext[m])
        monthElement.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true)
    }

    var thisyear = today.getFullYear()
    for (var y = 0; y < 2; y++) {
        yearElement.options[y] = new Option((thisyear + "").substring(2, 4), thisyear)
        thisyear += 1
    }
    yearElement.options[0] = new Option((today.getFullYear() + "").substring(2, 4), today.getFullYear(), true, true)
}

//AddToDropdown
//Adds the specified text to a dropdown list
function AddToDropdown(obj, value, text, isDefault) {
    obj.options[obj.options.length] = new Option(text, value, isDefault, isDefault);
}

//LoadJsCss
//Loads the css file into the page head
function LoadJsCss(campaignId) {
    var fileref = document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")

    if (campaignId == 0 || campaignId == null) {
        fileref.setAttribute("href", String.format(cssFilePath, "TeeofftimesSearchBox"))
    } else {
        fileref.setAttribute("href", String.format(cssFilePath, campaignId))
    }
    

    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}

//String.format
//Functionality similar to the ASP.net functionality
String.format = function(text) {
    if (arguments.length <= 1) {
        return text;
    }
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }

    return text;
};



