﻿$(document).ready(function () {
    $("#loginbox").submit_ajax_form({ sendPath: "/SubmitTo/Login.aspx" });

    $('.terms a').click(function () {
        $('<div id="blkoverlay" style="height:' + $(document).height() + 'px;opacity:0.8;"></div>').insertBefore($('#centrewrap'));
        var termleft = ($(document).width() - 883) / 2;
        var termheight = $(document).height() - 142;
        $.ajax({
            type: "POST",
            url: "/Terms-and-Conditions.aspx",
            success: function (response) {
                $('<div id="termsandcons" style="left:' + termleft + 'px;height:' + termheight + 'px;top:50px;">' + response + '</div>').insertBefore($('#centrewrap'));
                $('#termscontent').css('height', (termheight - 50) + 'px');
            }
        });

        return false;
    });

    $('#blkoverlay, #closewindow a').live('click', function () {
        $('#blkoverlay, #termsandcons').remove();

        return false;
    });

//    $('.agree-button').live('click', function () {
//        $('#termscontent').ajax({
//            type: "POST",
//            url: "/SubmitTo/AcceptTerms.aspx",
//            success: function (response) {
//                if (response == "") {
//                    window.location = "/";
//                } else {
//                    window.location = "/Logout.aspx";
//                }
//            }
//        });
//    });
});

// * == Ajax form validation and submit == * //
(function ($) {
    $.fn.submit_ajax_form = function (options) {

        var defaults =
		{
		    sendPath: '',
		    responseContainer: 'ajaxresponse',
		    jTable: false,
		    sendAjaxForm: ''
		};

        var options = $.extend(defaults, options);

        return this.each(function () {
            var form = $(this),
				send =
				{
				    formElements: form.find('textarea, select, input:text, input[type=hidden], input[type=file], input[type=password]'),
				    validationError: false,
				    button: form.find('input:image'),
				    datastring: ''
				};

            send.button.bind('click', checkElements);

            function send_ajax_form() {

                $.ajax({
                    type: "POST",
                    url: options.sendPath,
                    data: send.datastring,
                    success: function (response) {
                        response = response.split(",");
                        
                        switch (response[0]) {
                            case "":
                                window.location = "/";
                            break;

                            case "1":
                                $('<div id="blkoverlay" style="height:' + $(document).height() + 'px;opacity:0.8;"></div>').insertBefore($('#centrewrap'));
                                
                                var termleft = ($(document).width() - 883) / 2;
                                var termheight = $(document).height() - 142;
                                $.ajax({
                                    type: "POST",
                                    url: "/Terms-and-Conditions.aspx?id=" + response[1] + "&uname="+ response[2] + "&pwrd="+ response[3],
                                    success: function (result) {
                                        $('<div id="termsandcons" style="left:' + termleft + 'px;height:' + termheight + 'px;top:50px;"><input type="hidden" class="hiddenfield" name="userid" class="userid" value="'+ response +'" />' + result + '</div>').insertBefore($('#centrewrap'));
                                        $('#termscontent').css('height', (termheight - 50) + 'px');
                                    }
                                });
                            break;

                            default:
                                $('#loginbox').after('<div class="loginErr">'+ response +'</div>');
                        }
                    }
                });

            }

            function checkElements() {
                // reset validation var and send data
                send.validationError = false;
                send.datastring = 'ajax=true';

                send.formElements.each(function (i) {
                    var currentElement = $(this),
						surroundingElement = currentElement.parent(),
						value = currentElement.val(),
						name = currentElement.attr('name'),
						classes = currentElement.attr('class'),
						nomatch = true;

                    send.datastring += "&" + encodeURIComponent(name) + "=" + encodeURIComponent(value);

                    if (classes.match(/required/)) {
                        if (value == '') {
                            $(this).addClass("error");
                            send.validationError = true;
                        }
                        else {
                            $(this).removeClass("error");
                        }
                        nomatch = false;
                    }

                    if (classes.match(/email/)) {
                        if (!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)) {
                            $(this).addClass("error");
                            send.validationError = true;
                        }
                        else {
                            $(this).removeClass("error");
                        }
                        nomatch = false;
                    }

                    if (classes.match(/inputfile/)) {
                        var aFileName = value.split('.');
                        var sDoctype = aFileName[aFileName.length - 1];

                        if (!sDoctype.match(/[doc,docx]/gi)) {
                            $('#inputfiletype strong').css("color", "#ff0000");
                        }
                        else {
                            $('#inputfiletype strong').css("color", "#000");
                        }
                        nomatch = false;
                    }
                });

                if (send.validationError == false) {
                    if (options.sendAjaxForm == '') {
                        send_ajax_form();
                    }
                    else {
                        $(options.sendAjaxForm).submit();
                    }
                }
                return false;
            }
        });
    }
})(jQuery);

