﻿
$(document).ready(function() {
    setDatePicker($(".datepicker"));
});

function setDatePicker(sel) {
    sel.datepicker(
    { dateFormat: 'dd/mm/yy',
        changeYear: true,
        showMonthAfterYear: true,
        yearRange: '-70:+10'
    });
}


function clearTextField(field,val) {
    var currentValue = $(field).val();
    if (currentValue == val) {
        $(field).val("");
    }
}

function setTextField(field,val) {
    var currentValue = $(field).val();
    if (currentValue == "") {
        $(field).val(val);
    }
}

function addOption(sel, name, value) {
    sel.options[sel.options.length] = new Option(name, value);
}

function deleteOptions(sel) {
    for (var i = sel.options.length - 1; i >= 0; i--) {
        sel.remove(i);
    }
}

function addOptionSelected(sel, name, value, isSelected) {
    var opt = new Option(name, value);
    opt.selected = isSelected;
    sel.options[sel.options.length] = opt;
}
/*
function displayLookup(results, sel, unselText) {
    sel.options.length = 0;
    for (i = 0; i < results.length; i++) {
        if (results[i] == "")
            addOption(sel, unselText, "");
        else
            addOption(sel, results[i], results[i]);
    }
}
*/
function displayKeyValue(results, sel, unselText) {
    sel.options.length = 0;
    for (i = 0; i < results.length; i++) {
        if (results[i] == "")
            addOption(sel, unselText, "");
        else
            addOption(sel, results[i].value, results[i].key);
    }
}

function showHide(theid) {
    if (document.getElementById) {
        var switch_id = document.getElementById(theid);
        switch_id.style.display = (switch_id.style.display == "none") ? "block" : "none";
    }
}

function show(theid) {
    theid.style.display = "block";
}

function hide(theid) {
    theid.style.display = "none";
}

function onFailed(a, b, c) {
    var divWait = $('img[src*=/images/loading3.gif]').parent();
    if (divWait.length > 0)
        hide(divWait[0]);

    //just in case
    var dov = $(".overlay")
    if (dov.length > 0)
        dov.hide();

    if (a.get_statusCode() == 401) {
        document.location = document.URL;
    }
    else {
        if (a._exceptionType == "Autolease.Global.Exceptions.DBTimeoutException" ||
            a._exceptionType == "System.ServiceModel.CommunicationException" ||
            a._exceptionType == "System.TimeoutException")
            alert("Our server is busy, please try again");
        else if (a._exceptionType == "Autolease.Global.Exceptions.InvalidSessionException")
            alert("Please log out - your session seem to have expired");
        else if (a._exceptionType == "Autolease.Global.Exceptions.ValidationException")
            alert(a.get_message());
        else if (a._exceptionType == "Autolease.Global.Exceptions.ServiceException") {
            if (confirm("Sorry, there was a problem. Would you like to report it to the service desk?"))
                document.location = "ErrorPage.aspx";
        }
        else
            alert(a.get_message());
    }
}

function StringBuffer() {
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string) {
    this.buffer[this.buffer.length] = string;
    return this;
};

StringBuffer.prototype.toString = function toString() {
    return this.buffer.join("");
};

StringBuffer.prototype.length = function length() {
    return this.buffer.length;
};


function showWaiting(div) {
    show(div);
    showSpinning(div);
    div.innerHTML += "<br />Please wait...";
}

function showSpinning(div) {
    show(div);
    div.innerHTML = "<img src='/images/loading3.gif'>";
}

/*
function KeyEvent(event) {
    // press key "Enter"
    if (event.keyCode == 13 || event.charCode == 13) {
        event.cancelBubble = true;
        if (event.stopPropagation)
            event.stopPropagation();
        if (event.preventDefault)
            event.preventDefault();

        doKeyPress(event);

        return false;
    }
    else
        return true;
}

function doKeyPress(event) {
    // please leave empty!!
}

function doPageLoad() {
    // please leave empty!!
}

function moveToMouse(el, e) {
    e = e || window.event;

    var x, y;
    if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        x = e.clientX - (de.clientLeft || 0);
        y = e.clientY - (de.clientTop || 0);
    }

    el.style.left = (x - 150) + "px";
    el.style.top = (y + 15) + "px";
}
*/
function isValidEmail(address) {
    var myRegExp = /([a-zA-Z]|[0-9])@(?=[a-zA-Z])/;
    var matchPos = address.search(myRegExp);
    if (matchPos == -1)
        return false;
    else
        return true;
}

function convertToValidDate(dateStr) {
    var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
    if (re.test(dateStr)) {
        var dArr = dateStr.split("/");

        var d = new Date(dArr[2], parseInt(dArr[1], 10) - 1, dArr[0]);
        if (d.getDate() == dArr[0] && d.getMonth() + 1 == dArr[1] && d.getFullYear() == dArr[2]) {
            {
                d.setUTCDate(dArr[0]);
                d.setUTCMonth(parseInt(dArr[1], 10) - 1);
                d.setUTCFullYear(dArr[2]);
                d.setUTCHours(0);
                d.setUTCMinutes(0);
                d.setUTCSeconds(0);
                return d;
            }
        }
    }
    else
        return null;
}

function convertNullDate(dateStr) {
    var dt;
    dt = dateStr == null ? convertToValidDate("01/01/1900") : dateStr;
    return dt;
}

function isDate(dateStr) {
    return convertToValidDate(dateStr) != null;
}

var DateFormat = {
    getMonthName: function(i) {
        var m = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var mnthName = '';
        if (isNaN(i))
            return m[0];
        else
            return m[i];
    }
}



function clearForm() {
    $("input").val("");
    $("textarea").val("");
    $("input:checkbox").each(function() {
        this.checked = false;
    });
    $("select").each(function() {
        this.selectedIndex = 0;
    });
}

/* functions for underlining of links on div panels */

function underlineAllOurOffers() {
    $('#allOurOffers a').css({ 'text-decoration': 'underline' });
}
function clearAllOurOffers() {
    $('#allOurOffers a').css({ 'text-decoration': 'none' });
}

function underlineContactAndLocations() {
    $('#ContactsAndLocationsLink').css({ 'text-decoration': 'underline' });
}
function clearContactAndLocations() {
    $('#ContactsAndLocationsLink').css({ 'text-decoration': 'none' });
}

function underlineCustomerLogin() {
    $('#customerLoginLink a').css({ 'text-decoration': 'underline' });
}
function clearCustomerLogin() {
    $('#customerLoginLink a').css({ 'text-decoration': 'none' });
}

function underlineWhatCar() {
    $('#roadLibrary a').css({ 'text-decoration': 'underline' });
}
function clearWhatCar() {
    $('#roadLibrary a').css({ 'text-decoration': 'none' });
}

