﻿//Stir.com 2009 please do no use or distribute this code without permission from Stir.com


var genderpref = 2;
var gender = 1;


function asyncRegister(handle, email, password, gender, genderpref, postalcode, birthday, rdp, isContestReg) {
    $.post(window.location.protocol + "//" + window.location.host + "/asyncReg.aspx",
                {
                    action: 'reg',
                    handle: handle,
                    email: email,
                    password: password,
                    gender: gender,
                    genderpref: genderpref,
                    postalcode: postalcode,
                    birthday: birthday,
                    rdp: rdp,
                    iscontestreg: isContestReg
                   
                },
        function(data) {
            //$('.requestMessage').text(data.toString());
            if (data.toString().substring(0, 5) == 'http:') {
                window.location = data.toString();
            }
            else if (data.toString().substring(0, 5) == 'email') {
                validateForm(data.toString());
            }
            else {
                validateForm('That handle has already been used, try ' + data.toString());
                $('#handle').val(data.toString());
            }
        }
              );
}


function updateGender(gender) {

    if (gender == 1) {
        //alert('male seeks female');
        genderpref = 2;
        gender = 1;
    }
    else {
        //alert('female seeks male');
        genderpref = 1;
        gender = 2;
    }
}


function validateForm(errorList) {

    var email = $('#email').val();
    var handle = $('#handle').val();
    var password = $('#password').val();
   
    var postalcode = $('#postalcode').val();
    var birthMonth = $('#birthMonth').val();
    var birthYear = $('#birthYear').val();
    var birthDay = $('#birthDay').val();
    var birthdate = birthMonth + '-' + birthDay + '-' + birthYear;

    var isfacebook = 'false';
    var userid = 0;
    var rdp = $(".hdnRDPClass").val();


    //-- Rules For Registration --

    //--Email--
    if (email.length == 0) {
        errorList = errorList + '<li>Please use a valid email address.</li>';
    }
    else {
        var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (email.match(emailRegEx)) {
            //do nothing
        }
        else {
            errorList = errorList + '<li>Please enter a valid email address.</li>';
        }
    }

    //--Handle--
    if (handle.length == 0) {
        errorList = errorList + '<li>Please provide a user name.</li>';
    }
    else {
        if (handle.length < 5) {
            errorList = errorList + '<li>User name must be at least five characters.</li>';
        }
    }

    //--Password--
    if (password.length == 0) {
        errorList = errorList + '<li>Please use a valid password.</li>';
    }
    else {
        if (password.length < 4) {
            errorList = errorList + '<li>Your password must be at least four characters.</li>';
        }
    }

    //--PostalCode--
    if (postalcode.length == 0) {
        errorList = errorList + '<li>You must enter a U.S. Zip or Canadian Postal code.</li>';

    }
    else if (postalcode.length == 0) {
        var postalRegEx = '^[abceghjklmnprstvxyABCEGHJKLMNPRSTVXY][0-9][abceghjklmnprstvwxyzABCEGHJKLMNPRSTVWXYZ] {0,1}[0-9][abceghjklmnprstvwxyzABCEGHJKLMNPRSTVWXYZ][0-9]$';
        var usPostalRegEx = '/(^\d{5}$)|(^\d{5}-\d{4}$)/';
        if (postalcode.match(postalRegEx)) {
            //do nothing
        }
        else if (postalcode.match(usPostalRegEx)) {
            //do nothing
        }
        else {
            errorList = errorList + '<li>You must enter a U.S. Zip or Canadian Postal code!</li>';
        }
    }



    if (birthMonth == 0 || birthDay == 0 || birthYear == 0) {
        errorList = errorList + '<li>The birth date you provided is not valid. Please re-enter.</li>';
    }

    //--Validation Complete
    if (errorList.length != 0) {
        $('.errorMsg').html('<div class="errorReturn"><div class="error"><ul class="errorList">');
        $('.errorList').html(errorList);
    }
    else
    {
        var isContestReg = false;
        if (window.location.href.toLowerCase().indexOf('/fb/eventdetail.aspx') > -1)
        {
            isContestReg = true; 
        }
        
        asyncRegister(handle, email, password, gender, genderpref, postalcode, birthdate, rdp, isContestReg);
    }
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.toLowerCase()
    hashes = hashes.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function validateEmail() {

    str = document.getElementById('emailAddress').value;
    if (str.match(emailRegEx)) {
        document.mailingListForm.submit();
    } else {
        return false;
    }
}