﻿$(document).ready(function() {

    /* Adds live functionality for non supported events e.g. focus */
    (function() { var special = jQuery.event.special, uid1 = 'D' + (+new Date()), uid2 = 'D' + (+new Date() + 1); jQuery.event.special.focus = { setup: function() { var _self = this, handler = function(e) { e = jQuery.event.fix(e); e.type = 'focus'; if (_self === document) { jQuery.event.handle.call(_self, e); } }; jQuery(this).data(uid1, handler); if (_self === document) {                /* Must be live() */if (_self.addEventListener) { _self.addEventListener('focus', handler, true); } else { _self.attachEvent('onfocusin', handler); } } else { return false; } }, teardown: function() { var handler = jQuery(this).data(uid1); if (this === document) { if (this.removeEventListener) { this.removeEventListener('focus', handler, true); } else { this.detachEvent('onfocusin', handler); } } } }; jQuery.event.special.blur = { setup: function() { var _self = this, handler = function(e) { e = jQuery.event.fix(e); e.type = 'blur'; if (_self === document) { jQuery.event.handle.call(_self, e); } }; jQuery(this).data(uid2, handler); if (_self === document) {                /* Must be live() */if (_self.addEventListener) { _self.addEventListener('blur', handler, true); } else { _self.attachEvent('onfocusout', handler); } } else { return false; } }, teardown: function() { var handler = jQuery(this).data(uid2); if (this === document) { if (this.removeEventListener) { this.removeEventListener('blur', handler, true); } else { this.detachEvent('onfocusout', handler); } } } }; })();

    /* Textbox watermarks */
    $(".watermark").live("focus", function() {
        if ($(this).val() == this.defaultValue) {
            $(this).val("");
        }
    }).live("blur", function() {
        if ($.trim($(this).val()) == "") {
            $(this).val(this.defaultValue);
            $(this).css({
                'color': '#CCC'
            });
        }
    });

    /* Change text colour to black once a user types into a textbox */
    $(".watermark").live("keypress", function() {

        if ($(this).val() != this.defaultValue) {
            $(this).css({
                'color': '#000'
            });
        }
    });

    /* Replace target="_blank" so page validates */
    $("a.new-window").click(function(event) {
        event.preventDefault();
        window.open($(this).attr('href'));
    });

    /* Make a div clickable - uses the first anchor it finds as the destination */
    $(".div-link").click(function(event) {
        event.preventDefault();
        var link = $(this).find('a:first');
        if (link.length > 0) {
            window.location = link.attr("href");
        }
    });

    /* Browser back link */
    $("a.back").click(function(event) {
        event.preventDefault();
        history.go(-1);
        return false;
    });

    /* Browser print link */
    $("a.print").click(function(event) {
        event.preventDefault();
        window.print();
    });

    /* Collapsable sections */
    $("a.toggle").click(function(e) {
        e.preventDefault();
        $(this).parent().toggleClass("minimize");
        $(this).parent().toggleClass("maximize");

        $(this).parent().parent().find('.collapsible').slideToggle("fast");
    }
    );

    /* Message Panel Effects */
    $(".msg-panel").hide();

    $(".msg-panel").fadeIn("slow");

    /* Are you sure confirmation popup */
    $(".delete").click(function(event) {
        return confirm('Are you sure you want to delete this row?');
    });

    /* Login pop-up modal */
    $('.login-popup1').live('click', function(evt) {
        evt.preventDefault();

        //$("#selected-id input").val("test");
        
        var message = {
            username: "username",
	        password: "passsword"
        };
        
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/webservices/loginservicews.asmx/GetLandlord",
            data: JSON.stringify(message),
            dataType: "json",
            success: function(msg) {
            alert(msg.d.LandlordName);
            },
            error: function(error) {
            alert(error.responseText)
	        }

        });

        //        $.nyroModalManual({
        //            url: '#login-popup'
        //            , blocker: '#aspnetForm'
        //            , css: {
        //                bg: {
        //                    position: 'fixed'
        //                }
        //            }

        //        });

        // scrollToElement('#');

    });


    /* Adds missing length check to a textarea */
    $('textarea[maxlength]').keyup(function() {
        //get the limit from maxlength attribute   
        var limit = parseInt($(this).attr('maxlength'));
        //get the current text inside the textarea   
        var text = $(this).val();
        //count the number of characters in the text   
        var chars = text.length;

        //check if there are more characters then allowed   
        if (chars > limit) {
            //and if there are use substr to get the text before the limit   
            var new_text = text.substr(0, limit);

            //and change the current text with the new text   
            $(this).val(new_text);
        }
    });
});

/* Make block elements across a page the same height */
function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

/* Hide the contact form button when submit pressed */
function contactFormWait(button) {
    button.style.display = 'none';
    document.getElementById('msg-wait').className = '';
}

/* Scroll to top filter bar on list pages */
function scrollToTopFilterBar() {
    scrollToElement('top-filter-bar');
}

/* Scroll to element by id */
function scrollToElement(elementId) {

    var obj = document.getElementById(elementId);

    var selectedPosX = 0;
    var selectedPosY = 0;

    while (obj != null) {
        selectedPosX += obj.offsetLeft;
        selectedPosY += obj.offsetTop;
        obj = obj.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);
}

/* Set radio button group (to fix bug in repeaters) */
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

/* Custom validator for T&C - checkbox container must have accept-terms class */
function ValidateTermsCheckBox(source, args) {

    var checkbox = $(".accept-terms input:checked");

    args.IsValid = checkbox.val() != null;
}
