﻿var iDreamRegister = {
    PostFunction: null,
	ForceHide: false,
    Timer: null,
    ShowWelcome: function() {
        //Dan M - 17/07/09 - Function to show that lovely thanks for registering message.
        $.ajax({
            type: "GET",
            url: "ajax/lightbox/prereg_thanks.html",
            cache: false,
            success: function(msg) {
                $.blockUI({ message: msg, css: { padding: 0, margin: 0, border: 0, height: "517px", width: "636px", top: ($(window).height() - 517) / 2 + 'px', left: ($(window).width() - 636) / 2 + 'px', cursor: 'default'} });
                $('.blockOverlay').click($.unblockUI);
                $('#close_button').click($.unblockUI);
                var liftoffTime = new Date("August 8, 2009 08:00");
                $("#launchcountdown").countdown({until: liftoffTime});
            }
        });
    },
    isValidPhoneNumber: function(ph) {
        if (ph == null) {
            return false;
        }
        var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
        // 10 is the minimum number of numbers required
        return ((/\d{10,}/i).test(stripped));
    },
    Begin: function() {
        $.ajax({
            type: "GET",
            url: "ajax/register/xt_register.asp",
            cache: false,
            success: function(msg) {
                //Test Unblock UI first:
                $.blockUI({ message: msg, css: { padding: 0, margin: 0, border: 0, height: "517px", width: "636px", top: ($(window).height() - 517) / 2 + 'px', left: ($(window).width() - 636) / 2 + 'px', cursor: 'default'} });
                //iDreamHold
                $('.blockOverlay').click($.unblockUI);
                $('#close_button').click($.unblockUI);
                //$('.blockOverlay').click();
                //$('#close_button').click();
                if (!iDreamRegister.PostFunction || iDreamRegister.ForceHide) {

                    //iDreamHold
                    $('#back_button').hide();
                    $('.idreamcountbacklight').toggleClass('idreamcountbacklight').toggleClass('idreamgreenacc');
		} else {
		    $('.idreamcountbacklight').toggleClass('idreamcountbacklight').toggleClass('idreamgreenacc');
		}
                $('#frmReg').submit(function() {
                    iDreamRegister.ShowMessage();
                    if ($('#U_Name').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter your name.');
                        $('#U_Name').focus();
                    }
                    else if ($('#U_DoB').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter your date of birth.');
                        $('#U_DoB').focus();
                    }
                    else if (!iDreamRegister.IsValidDate($('#U_DoB').val())) {
                        // Just clear form and set focus
                        $('#U_DoB').focus();
                    }
                    else if ($('#U_Email').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter your e-mail address.');
                        $('#U_Email').focus();
                    }
                    else if (!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($('#U_Email').val()))) {
                        iDreamRegister.ShowMessage('The e-mail address you entered is invalid.');
                        $('#U_Email').focus();
                    }
                    else if ($('#U_Password').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter a password for your new account');
                        $('#U_Password').focus();
                    }
                    else if ($('#U_Password').val().length < 6) {
                        iDreamRegister.ShowMessage('Your password needs at least 6 characters.');
                        $('#U_Password').focus();
                    }
                    else if ($('#U_Contact').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter your contact telephone number');
                        $('#U_Contact').focus();
                    }
                    else if (!iDreamRegister.isValidPhoneNumber($('#U_Contact').val())) {
                        iDreamRegister.ShowMessage('The telephone number you entered is not valid');
                        $('#U_Contact').focus();
                    }
                    else if ($('#U_Address1').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter the first line of your address');
                        $('#U_Address1').focus();
                    }
                    else if ($('#U_Town').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter the town your address resides in');
                        $('#U_Town').focus();
                    }
                    else if ($('#U_County').val().length == 0) {
                        iDreamRegister.ShowMessage('You didn\'t enter which county/state you are from.');
                        $('#U_County').focus();
                    }
                    else if ($('#U_Postcode').val().length == 0) {
                        iDreamRegister.ShowMessage('You forgot to enter your postal code or zip');
                        $('#U_Postcode').focus();
                    }
                    else if (!$('#termsconfirm').is(":checked")) {
                        iDreamRegister.ShowMessage('Please tick to confirm acceptance of our marketplace guidelines.');
                        $('#termsconfirm').focus();
                    }
                    else {
                        // Woo - through initial validation, now to push through to the xt_register.asp page:
                        //Ajax Request
                        iDreamRegister.ShowAnimated();
                        $.ajax({
                            type: "POST",
                            url: "ajax/register/xt_register.asp",
                            cache: false,
                            data: {
                                U_Name: $('#U_Name').val(),
                                U_DoB: $('#U_DoB').val(),
                                U_Email: $('#U_Email').val(),
                                U_Password: $('#U_Password').val(),
                                U_CountryID: $('#U_CountryID').val(),
                                U_Address1: $('#U_Address1').val(),
                                U_Address2: $('#U_Address2').val(),
                                U_Town: $('#U_Town').val(),
                                U_County: $('#U_County').val(),
                                U_Postcode: $('#U_Postcode').val(),
                                U_Contact: $('#U_Contact').val(),
                                U_Gender: $('#U_Gender').val()
                            },
                            success: function(msg) {
                                iDreamRegister.ShowAnimated(); // Stop Animation
                                switch (parseInt(msg)) {
                                    case -409: //Conflict
                                        iDreamRegister.ShowMessage("This email address has already been registered with us.");
                                        break;
                                    case -406: //Unnacceptable
                                        iDreamRegister.ShowMessage("Please make sure you fill out all of the registration form");
                                        break;
                                    default:
                                        lgUID = parseInt(msg);
                                        iDreamRegister.ClearForm();
                                        if ($(".register").length) {
                                            $(".register").load("ajax/login/xt_header.asp", { a: "0" }, function() {
                                                //Loaded Partial Header
                                                hook_top_actions();
                                            }); //force POST
                                        }
                                        if (iDreamRegister.PostFunction) {
                                            iDreamRegister.PostFunction.call();
                                            iDreamRegister.PostFunction = null;
                                        } else {
                                            $.unblockUI();
                                        }
                                }
                            }
                        });
                        return false;
                    }

                    return false;
                });
            },
            error: function() {
                $.blockUI({ message: "There was an error processing your request", timeout: 3000 });
                $('.blockOverlay').click($.unblockUI);
            }
        });
    },
    IsValidDate: function(dob) {
        if (/^\d{1,2}[\/]\d{1,2}[\/]\d{4}$/.test(dob)) {
            var tString = dob.split("/");
            if (tString[0] > 31 || tString[1] > 12 || tString[0] < 1 || tString[1] < 1 || tString[2] > (new Date().getFullYear() + 0) || tString[2] < (new Date().getFullYear() - 110)) {
                iDreamRegister.ShowMessage("The date you've entered isn't correct please check and try again.");
            } else {
                var tDate = new Date();
                tDate.setFullYear(tDate.getFullYear() - 18);
                if (Date.parse(tString[1] + "/" + tString[0] + "/" + tString[2]) >= Date.parse(tDate)) {
                    iDreamRegister.ShowMessage("I'm sorry, you need to be older then 18 to register with i-Dream");
                    $('#rtMessage').css("background-color", "#950E11");
                } else {
                    return true;
                }
            }
        } else {
            iDreamRegister.ShowMessage('Sorry, the date you entered is not in dd/mm/yyyy format');
        }
    },
    ClearForm: function() {
        $('#U_Name').val('');
        $('#U_DoB').val('');
        $('#U_Email').val('');
        $('#U_Password').val('');
        $('#U_CountryID').attr('value', '199');
    },
    ShowMessage: function(msg) {
        $('#rtMessage').css("background-color", "#466917");
        if ($('#rtMessage').hasClass("Invisible"))
            $('#rtMessage').removeClass("Invisible");
        if (msg) {
            if ($('#rtMessage').html() == "")
                $('#rtMessage').fadeIn("def");
            $('#rtMessage').html(msg);
        } else {
            $('#rtMessage').hide();
            $('#rtMessage').html("");
        }
    },
    ShowAnimated: function() {
        if (iDreamRegister.Timer) {
            clearInterval(iDreamRegister.Timer);
            iDreamRegister.Timer = null;
            iDreamRegister.ShowMessage();
        } else {
            iDreamRegister.ShowMessage("Working");
            iDreamRegister.Timer = setInterval(function() {
                if ($('#rtMessage').html() == "...Working...") {
                    iDreamRegister.ShowMessage("Working");
                } else {
                    iDreamRegister.ShowMessage("." + $('#rtMessage').html() + ".");
                }
            }, 250);
        }
    }
}
