
$(document).ready(function($) {

    YAHOO.namespace('subscriber');
    var userelo = (document.getElementById('signUpReloDialog')) ? true : false;

    if (userelo) {
        YAHOO.subscriber.signUpDialog = new YAHOO.widget.Dialog('signUpReloDialog',
						        { width: '30em',
						            fixedcenter: true,
						            modal: true,
						            visible: false,
						            constraintoviewport: true,
						            close: false,
						            zIndex: 12000
						        });
    }
    else {
        //ordinary listmailer signin attached to document body
        YAHOO.subscriber.signUpDialog = new YAHOO.widget.Dialog('signUpDialog',
						        { width: '30em',
						            fixedcenter: true,
						            modal: true,
						            visible: false,
						            constraintoviewport: true,
						            zIndex: 12000
						        });
    }

    document.body.className += ' yui-skin-sam';
    YAHOO.subscriber.signUpDialog.render();
    $('[rel="signin-alert-message"]').each(function() { $(this).hide(); });

    if (!userelo) {
        getCookie('listmailerlogin') ? toggleVOWInstruct(true) : toggleVOWInstruct(false);
        //check the 'include sold' checkbox attributes.  If this is a registered VOW user, allow 'checked'. If not, open up the
        //pane explaining what VOW is.
        $('[rel="vow-sold-check"]').each(function() {
            if ($(this).attr('vow-user') == 'False') {
                $(this).attr('checked', '');
                $(this).click(function() { $(this).attr('checked', ''); showVOWInfo(); });
            }
            else {
                $(this).attr('checked', 'checked');
            };
        });
    }

    if (userelo) {
        $('#btnOK').click(function() {
            //acknowledgment of a successful sign-in by relo user.  Page reloads in order to invoke relo banner mode
            YAHOO.subscriber.signUpDialog.hide();
            setCookie('ReloPartnerID', parseQS('ReloPartner'), '180', '/', '', '');
            location.href = '/';
        });

        if (!getCookie('ReloPartnerID')) {
            $('#signUpReloDialog').show();
            YAHOO.subscriber.signUpDialog.show();
        }
    }
});

// ---------------------- end document ready ---------------------------------------------------------------------------------

function signIn() { $('#signUpDialog').show(); toggleSignIn('signIn'); YAHOO.subscriber.signUpDialog.show(); }
function signUp() { $('#signUpDialog').show(); toggleSignIn('signUp'); YAHOO.subscriber.signUpDialog.show(); }
function showVOWInfo() { $('#signUpDialog').show(); $('#signUp').hide(); $('#signIn').hide(); $('#vowinfo').show(); YAHOO.subscriber.signUpDialog.show(); }

function toggleVOWInstruct(signedin) {
    if (signedin) {
        $('[rel="vow-instruct-signedin"]').show();
        $('[rel="vow-instruct-notsignedin"]').each(function() { $(this).hide(); });
    }
    else {
        $('[rel="vow-instruct-signedin"]').hide();
        $('[rel="vow-instruct-notsignedin"]').each(function() { $(this).show(); });
    }
}

function toggleSignIn(type) {
    $('#vowinfo').hide();

    if (type == 'signUp') {
        $('#signUp').show();
        $('#signIn').hide();
    }
    else if (type == 'signIn') {
        $('#signUp').hide();
        $('#signIn').show();
    }
}

function validateSignUp(type) {
    $('[rel="signin-alert-message"]').each(function() { $(this).hide(); });
    $('#firstname').css('border', '');
    $('#email').css('border', '');
    $('#pwd').css('border', '');
    $('#pwd2').css('border', '');
    var err = 0;
    
    var fname = $('#firstname').val();
    fname = $.trim(fname);
    if (fname == '') {
        $('#nameRequired').show();
        $('#firstname').css('border', '1px solid red');
        err++;
    }

    if (!validateEmailByValue($('#email').val())) {
        $('#emailRequired').show();
        $('#email').css('border', '1px solid red');
        err++;
    }

    var pwd1 = $('#pwd').val();
    pwd1 = $.trim(pwd1);
    var pwd2 = $('#pwd2').val();
    if (pwd1 == '') {
        $('#pwdRequired').show();
        $('#pwd').css('border', '1px solid red');
        err++;
    }

    if (type == 'relo') {
        //check the password entered.  Does it match what the partner company set up in the admin?
        if ($.ajax({ url: '/listmailer/signin/validateReloPwd_AJAX.asp', data: 'pwd=' + pwd1 + '&ID=' + $('#ReloPartner').val(), async: false }).responseText == 'False') {
            $('#pwdRequired').show();
            $('#pwd').css('border', '1px solid red');
            err++;
        }
    }

    if (type == 'reg') {
        if (pwd1 != pwd2) {
            $('#pwdRequired2').show();
            $('#pwd2').css('border', '1px solid red');
            err++;
        }
    }

    return (err == 0);
}

function trySignIn() {
    
    $('[rel="signin-alert-message"]').each(function() { $(this).hide(); });
    $('#emailSignIn').css('border', '');
    $('#emailPassword').css('border', '');

    var eml = $('#existemail').val();
    var pwd = $('#existpwd').val();
    
    if (!validateEmailByValue(eml)) {
        $('#existEmailRequired').show();
        $('#existemail').css('border', '1px solid red');
        return false;
    }

    var subscriberID = '0';
    var acctSubType;
    var firstName = '';
    var prefAgentCode = '';
    
    //get subscriber information
    $.ajax({ type: 'GET', cache: false, url: '/_include/listmailercom/AJAX_GetSubscriber.asp?email=' + eml, async: false, dataType: 'xml',
        success: function(xml) {
            subscriberID = $(xml).find('ID').text();
            acctSubType = $(xml).find('AccountSubType').text();
            firstName = $(xml).find('FirstName').text();
            prefAgentCode = $(xml).find('PreferredAgentCode').text();
            if (prefAgentCode != '') {
                setCookie('BannerCode', prefAgentCode, '', '/', '', '');
                setCookie('BannerType', 'agent', '', '/', '', '');
            }
        }
    });

    if (subscriberID == '0') {
        $('#existEmailRequired').show();
        $('#existEmailRequired').html('Sorry, we have no record of this e-mail.  Please try again or create a new account.');
        $('#existemail').css('border', '1px solid red');
        return false;
    }

    if ($.ajax({ url: '/_include/listmailercom/AJAX_ValidatePassword.asp?Subscriber=' + eml + '&Pwd=' + pwd, async: false }).responseText == 'False') {
        $('#existPwdRequired').show();
        $('#existpwd').css('border', '1px solid red');
        return false;
    }

    //successful sign in
    setListmailerCookies(eml, firstName);
    $.post('/_include/listmailercom/AJAX_UpdateLastLogin.asp?Subscriber=' + eml);
    YAHOO.subscriber.signUpDialog.cancel();
    saveSearchOrListing();
    
    //navigate to listmailer if user asked to
    if ($('#chkToMyAcct:checked').val() == 'on') {
        location.href = '/listmailer/';
        return;
    }
    //banner mode tripwire.  Have to reload the page to invoke agent/office/team banner.
    if (prefAgentCode != '') {
        location.reload();
    }        

    $.showWelcome();
    $('#mysearch').load('/_include/modules/mysearch/navlinks.asp');
    toggleVOWInstruct(true);
    if (acctSubType == '1') {
        //checking subtype.  If '1', this is a VOW account so allow sold listings in searches.    
        $('[rel="vow-sold-check"]').each(function() { $(this).unbind('click'); });
    }        
        
    return true;
}

function onSignUp() {

    var eml = $('#email').val();
    var pwd = $('#pwd').val();
    var firstName = $('#firstname').val();
    var bcode = getCookie('BannerCode');
    var btype = getCookie('BannerType');
    
    var qs = 'email=' + eml + '&firstName=' + firstName + '&pwd=' + pwd + '&lifespan=14';
    if (btype == 'agent') { qs += '&preferredagent=' + bcode; }

    $.post('/_include/listmailercom/AJAX_CreateSubscriber_Min.asp?' + qs);

    setListmailerCookies(eml, firstName);
    YAHOO.subscriber.signUpDialog.cancel();
    saveSearchOrListing();
    
    //navigate to listmailer if user asked to
    if ($('#chkToNewAcct:checked').val() == 'on') {
        location.href = '/listmailer/';
        return;
    }
    
    //banner mode tripwire.  Have to reload the page to invoke agent/office/team banner.
    if (bcode != '') { location.reload() };
    
    $.showWelcome();
    $('#mysearch').load('/_include/modules/mysearch/navlinks.asp');
    toggleVOWInstruct(true);
}

function setListmailerCookies(eml, firstName) {
    setCookie('listmailerlogin', eml, '', '/', '', '');
    setCookie('listmaileremail', eml, '', '/', '', '');
    setCookie('listmailerfirstname', firstName, '', '/', '', '');
}

function saveSearchOrListing() {
    if (getCookie('saveProperty')) {     /* Save Property */
        var propertyID = getCookie('saveProperty').split('|');
        addSavedProperty(propertyID[0], propertyID[1]);
        setCookie('saveProperty', '', '', '/', '', '');
    }
    else if (getCookie('saveSearch')) {   /* Save Search */
        saveSearch(getCookie('saveSearch'));
        setCookie('saveSearch', '', '', '/', '', '');
    }
}
