﻿// ----------------------------------------------------------------------------------------------
//Soft Join Pop UP FORM

YAHOO.namespace('softJoinPopUp');
YAHOO.softJoinPopUp.init = function() {

    // Build overlay based on markup
    //YAHOO.softJoinPopUp.overlay = new YAHOO.widget.Overlay('softJoinPopUp', { 'fixedCenter': true, 'width': 350, 'visible': false, 'zIndex': 1100, 'effect': { 'effect': YAHOO.widget.ContainerEffect.FADE, duration: 0.25} });
    //YAHOO.softJoinPopUp.overlay.render();
    YUE.addListener(YUD.getElementsByClassName('softJoinPopUpTrigger'), 'click', function() {
        document.getElementById("player").style.display = "none";
        modalBackdrop.show();
        setTimeout('YAHOO.softJoinPopUp.overlay.show()', 400);
        setTimeout('document.LandingSignUpForm.landingFirstName.focus();', 1000);
        charInit();
        clearForm();
        hideAlerts();
        var profileCountry = ((document.getElementById('profileCountry')) ? document.getElementById('profileCountry').value : "");
        if (profileCountry != "" && profileCountry != "US") {
            toggleAddress("LandingSignUpForm", "international");
            document.LandingSignUpForm.landingCountry.value = profileCountry;
        } else {
            toggleAddress("LandingSignUpForm", "domestic");
            if (document.getElementById('profileState')) document.LandingSignUpForm.landingState.value = document.getElementById('profileState').value;
        }
        //populateTellForm();
    }, YAHOO.softJoinPopUp.overlay, true);

    // cancel/close
    YUE.addListener(YUD.getElementsByClassName('softJoinPopUpClose'), 'click', function() {
        document.getElementById("player").style.display = "block";
        YAHOO.softJoinPopUp.overlay.hide();
        setTimeout('modalBackdrop.hide()', 800);
        setTimeout('document.LandingSignUpForm.reset();', 1000);
        setTimeout('YUD.getElementsByClassName("softJoinPopUpThankYou","div")[0].style.display = "none";', 1000);
        setTimeout('YUD.getElementsByClassName("softJoinPopUpContent","div")[0].style.display = "block";', 1000);
        resetCharCount();

    }, YAHOO.softJoinPopUp.overlay, true);
}

YUE.addListener(window, 'load', YAHOO.softJoinPopUp.init);


function submitSoftJoinReferral() {
    //alert("here in submitSoftJoinReferral");
    // get selected communities
    var selectedCommList = new Array();
    var comm = 0;
    // get personal info
    var isInternational = false;
    var intl = YUD.getElementsByClassName("international", "div", "softJoinPopUp")[0];
    if (!YUD.hasClass(intl, "hide")) {
        isInternational = true;
    }
    var fName = document.LandingSignUpForm.landingFirstName.value;
    var lName = document.LandingSignUpForm.landingLastName.value;
    var email = document.LandingSignUpForm.landingEmail.value;
    var state = document.LandingSignUpForm.landingState.value;
    var communityID = document.LandingSignUpForm.landingCommunity.value;
    var friendsName = document.LandingSignUpForm.landingDearFirstName.value;
    var captcha = Recaptcha.get_response();
    //alert(state);
    //alert(communityID);
    if (fName == " (your first name)") {
        fName = "";
    }
    if (lName == " (your last name)") {
        lName = "";
    }
    if (friendsName == " (your friend's name)") {
        friendsName = "";
    }
        
    var errorConsole = YUD.getElementsByClassName('alertBar', '', document.LandingSignUpForm)[0];
    //alert("errorConsole:" + errorConsole);
    //fake communityID to pass validity
    if (communityID > 0) {
        selectedCommList[0] = communityID;
    }
    //alert("captcha:" + captcha);
    var validity = SoftJoinControl.validateReferral(fName, lName, email, friendsName, selectedCommList, state, captcha);
    
    var error_string = document.getElementById("landingFormError").innerHTML
    var error_string1 = "";
    //alert(validity);
    if (!validity) {
        YUD.replaceClass(errorConsole, 'hide', 'show');
        //alert("here1");
        if (error_string == "") {
            displayError('landingFormError', error_string1);
        } 
    }
    
    if (validity == false) {
        var target = YUD.getElementsByClassName('alertBar');
        for (var i = 0; i < target.length; i++) {
            if (YUD.getAncestorByClassName(target[i], 'softJoinPopUpContent')) target[i].style.display = 'block';
        }
    }
    if (validity) {
        ajaxLoading('submitReferralButton', 'submit', 'start');
        var siteID = productSearchBrandIds[getCurrSite()];
        var eventContext = "LANDING_PAGE";
        var isLoggedIn = NotebookUtils.getSignInStatus();
        var topicID = 10;   // contactUs.Subject = "Pulte Landing Page Enquiry";
        Pulte08.AjaxWebServices.PredefinedLandingService.ValidateRecaptcha(Recaptcha.get_challenge(), Recaptcha.get_response(), callBackRecaptcha);
    }
}

function submitSignUpForm2() {
    var selectedCommList = new Array();
    var fName = document.LandingSignUpForm2.landingFirstName.value;
    var lName = document.LandingSignUpForm2.landingLastName.value;
    var email = document.LandingSignUpForm2.landingEmail.value;
    var communityID = document.getElementById('communityID').value;
    var state = document.LandingSignUpForm2.landingState.value;
    var country = "US";
    var isInternational = false;

    //alert(communityID);
    if (communityID > 0) {
        selectedCommList[0] = communityID;
    }
    if (fName == "first name") {
        fName = "";
    }
    if (lName == "last name") {
        lName = "";
    }
    var validity = SoftJoinControl.validateReferral2(fName, lName, email, "noName", selectedCommList, state, "noCaptcha");
    //alert("validity:" + validity);
    
    if (validity == false) {
        var target = YUD.getElementsByClassName('alertBar');
        for (var i = 0; i < target.length; i++) {
            //alert(i);
            if (YUD.getAncestorByClassName(target[i], 'softJoinReferral')) target[i].style.display = 'block';
        }
    }
    selectedCommList[0] = communityID;
    if (validity) {
        var siteID = productSearchBrandIds[getCurrSite()];
        var areacode = document.LandingSignUpForm2.primaryPhone1.value;
        var prefix = document.LandingSignUpForm2.primaryPhone2.value;
        var suffix = document.LandingSignUpForm2.primaryPhone3.value;
        var eventContext = "LANDING_PAGE";
        var topicID = 11;   // contactUs.Subject = "Pulte Landing Page Enquiry";
        ajaxLoading('submitButton', 'submit', 'start');
        var isLoggedIn = NotebookUtils.getSignInStatus();
        var message = "";
        //alert(message);



        //SiteCatalyst code to track form conversion***************************************************************************************
        if (UseSiteCatalyst) {
            var sOldPageName = s.pageName;

            s.pageName = 'CooksForTheCure-ThankYou';
            if (sOldPageName.length > 0) {
                if (sOldPageName.indexOf(" - index") > 0)
                    sOldPageName = sOldPageName.replace(" - index", "")
                s.pageName = sOldPageName + " - CooksForTheCure-ThankYou"
            }

            s.events = "event2"
            s.eVar1 = "contact us";

            //s.channel=sContentGroup;
            //s.hier1=sContentGroup;

            var sContactUsContentGroup = sContentGroup;
            if (sContactUsContentGroup.length > 0) {
                if (sContactUsContentGroup.indexOf("/index") > 0)
                    sContactUsContentGroup = sContactUsContentGroup.replace("/index", "");
                else if (sContactUsContentGroup.indexOf("/Find a Home") > 0)
                    sContactUsContentGroup = sContactUsContentGroup.replace("/Find a Home", "");
            }
            else
                sContactUsContentGroup = communityID;

            s.channel = sContactUsContentGroup + "/" + "CooksForTheCure-ThankYou";
            s.hier1 = sContactUsContentGroup + "/" + "CooksForTheCure-ThankYou";
            s.prop1 = sTProp1;
            s.prop2 = sTProp2;
            s.prop3 = sTProp3;
            s.prop4 = sTProp4;
            s.prop5 = sTProp5;
            s.prop6 = sTProp6;
            s.t();
            s.pageName = sOldPageName;
            s.events = "None"
            s.eVar1 = "";

        }
        //End SiteCatalyst code to track form conversion***********************************************************************************





        Pulte08.AjaxWebServices.PredefinedLandingService.SubmitSignUpWithLeadSourceCodeAndMessage(siteID, eventContext, isLoggedIn, topicID, selectedCommList,
                fName, lName, email, state, areacode, prefix, suffix, isInternational, country, "Resident_Ambassador_Party", message, callBackSubmitSuccessCookforCure2, callBackSubmitFailedCookforCure);

    }
}

function submitSoftJoinCookForCure() {
    var selectedCommList = new Array();
    var fName = document.LandingSignUpForm.landingFirstName.value;
    var lName = document.LandingSignUpForm.landingLastName.value;
    var email = document.LandingSignUpForm.landingEmail.value;
    var communityID = document.LandingSignUpForm.communityIDHidden.value;
    var state = document.LandingSignUpForm.landingState.value;
    var country = "US";
    var isInternational = false;
    if (document.LandingSignUpForm.landingCommunity && document.LandingSignUpForm.landingCommunity != null) {
        communityID = document.LandingSignUpForm.landingCommunity.value;
        document.LandingSignUpForm.communityIDHidden.value = communityID;
    }
    //alert(communityID);
    if (communityID > 0) {
        selectedCommList[0] = communityID;
    }
    if (fName == "first name") {
        fName = "";
    }
    if (lName == "last name") {
        lName = "";
    }
    var validity = SoftJoinControl.validateReferral(fName, lName, email, "noName", selectedCommList, state, "noCaptcha");
    //    alert("validity:" + validity);

    if (validity == false) {
        var target = YUD.getElementsByClassName('alertBar');
        for (var i = 0; i < target.length; i++) {
            //alert(i);
            if (YUD.getAncestorByClassName(target[i], 'softJoinReferral')) target[i].style.display = 'block';
        }
    }
    selectedCommList[0] = communityID;
    if (validity) {
        var siteID = productSearchBrandIds[getCurrSite()];
        var areacode = document.LandingSignUpForm.primaryPhone1.value;
        var prefix = document.LandingSignUpForm.primaryPhone2.value;
        var suffix = document.LandingSignUpForm.primaryPhone3.value;
        var eventContext = "LANDING_PAGE";
        var topicID = 10;   // contactUs.Subject = "Pulte Landing Page Enquiry";
        ajaxLoading('submitButton', 'submit', 'start');
        var isLoggedIn = NotebookUtils.getSignInStatus();
        var message = "";
        //alert(message);



        //SiteCatalyst code to track form conversion***************************************************************************************
        if (UseSiteCatalyst) {
            var sOldPageName = s.pageName;

            s.pageName = 'CooksForTheCure-ThankYou';
            if (sOldPageName.length > 0) {
                if (sOldPageName.indexOf(" - index") > 0)
                    sOldPageName = sOldPageName.replace(" - index", "")
                s.pageName = sOldPageName + " - CooksForTheCure-ThankYou"
            }

            s.events = "event2"
            s.eVar1 = "contact us";

            //s.channel=sContentGroup;
            //s.hier1=sContentGroup;

            var sContactUsContentGroup = sContentGroup;
            if (sContactUsContentGroup.length > 0) {
                if (sContactUsContentGroup.indexOf("/index") > 0)
                    sContactUsContentGroup = sContactUsContentGroup.replace("/index", "");
                else if (sContactUsContentGroup.indexOf("/Find a Home") > 0)
                    sContactUsContentGroup = sContactUsContentGroup.replace("/Find a Home", "");
            }
            else
                sContactUsContentGroup = communityID;

            s.channel = sContactUsContentGroup + "/" + "CooksForTheCure-ThankYou";
            s.hier1 = sContactUsContentGroup + "/" + "CooksForTheCure-ThankYou";
            s.prop1 = sTProp1;
            s.prop2 = sTProp2;
            s.prop3 = sTProp3;
            s.prop4 = sTProp4;
            s.prop5 = sTProp5;
            s.prop6 = sTProp6;
            s.t();
            s.pageName = sOldPageName;
            s.events = "None"
            s.eVar1 = "";

        }
        //End SiteCatalyst code to track form conversion***********************************************************************************





        Pulte08.AjaxWebServices.PredefinedLandingService.SubmitSignUpWithLeadSourceCodeAndMessage(siteID, eventContext, isLoggedIn, topicID, selectedCommList,
                fName, lName, email, state, areacode, prefix, suffix, isInternational, country, "Resident_Ambassador_Party", message, callBackSubmitSuccessCookforCure, callBackSubmitFailedCookforCure);
    }
}

function submitSoftJoinInvited() {
    var selectedCommList = new Array();
    var fName = document.LandingSignUpForm.landingFirstName.value;
    var lName = document.LandingSignUpForm.landingLastName.value;
    var email = document.LandingSignUpForm.landingEmail.value;
    var communityID = document.LandingSignUpForm.communityIDHidden.value;
    var state = document.LandingSignUpForm.landingState.value;
    var country = "US";
    var isInternational = false;
    if (document.LandingSignUpForm.landingCommunity && document.LandingSignUpForm.landingCommunity != null) {
        communityID = document.LandingSignUpForm.landingCommunity.value;
        document.LandingSignUpForm.communityIDHidden.value = communityID;
    }
    //alert(communityID);
    if (communityID > 0) {
        selectedCommList[0] = communityID;
    }
    if (fName == "first name") {
        fName = "";
    }
    if (lName == "last name") {
        lName = "";
    }
    var validity = SoftJoinControl.validateReferral(fName, lName, email, "noName", selectedCommList, state, "noCaptcha");
//    alert("validity:" + validity);

    if (validity == false) {
        var target = YUD.getElementsByClassName('alertBar');
        for (var i = 0; i < target.length; i++) {
            //alert(i);
            if (YUD.getAncestorByClassName(target[i], 'softJoinReferral')) target[i].style.display = 'block';
        }
    }
    selectedCommList[0] = communityID;
    if (validity) {
        var comment = document.LandingSignUpForm.message.value;
        var dateTime = document.LandingSignUpForm.dateTimeHidden.value;
        var siteID = productSearchBrandIds[getCurrSite()];
        var nextStep = document.LandingSignUpForm.nextStep.value;
        var referrer = document.LandingSignUpForm.referrer.value;
        var phone = document.LandingSignUpForm.phone.value;
        var areacode = "";
        var prefix = "";
        var suffix = "";
        if (phone != "your phone number (optional)") {
            phone = phone.replace("(", "")
            areacode = phone.substring(0, 3);
            prefix = phone.substring(4, 7);
            suffix = phone.substring(8, 12);
        }
        var eventContext = "LANDING_PAGE";
        var topicID = 10;   // contactUs.Subject = "Pulte Landing Page Enquiry";
        ajaxLoading('submitReferralButton', 'submit', 'start');
        var isLoggedIn = NotebookUtils.getSignInStatus();
        message = "DWRR from " + referrer + " - " + nextStep + " - " + comment;
        //alert(message);
        Pulte08.AjaxWebServices.PredefinedLandingService.SubmitSignUpWithLeadSourceCodeAndMessage(siteID, eventContext, isLoggedIn, topicID, selectedCommList,
                fName, lName, email, state, areacode, prefix, suffix, isInternational, country, "DW_Resident_Referral", message, callBackSubmitSuccessSoftJoinPopUp, callBackSubmitFailedSoftJoinPopUp);

    }
}
function callBackRecaptcha(RecaptchaValidity) {
    var state = document.LandingSignUpForm.landingState.value;
    var communityID = document.LandingSignUpForm.landingCommunity.value;
    var email = document.LandingSignUpForm.landingEmail.value;
    var error_string = document.getElementById("landingFormError").innerHTML
    var errorConsole = YUD.getElementsByClassName('alertBar', '', document.LandingSignUpForm)[0];
    ajaxLoading('submitReferralButton', 'submit', 'stop');
    if (RecaptchaValidity) {
        //alert("Recap Success");
        Pulte08.AjaxWebServices.PredefinedLandingService.ValidateRestrictedMarketing(state, communityID, email, callBackRestrictedMarketing);
    } else {
        error_string += '<li>Please enter a valid security key from the image into the Textbox that is provided</li>';
        YUD.replaceClass(errorConsole, 'hide', 'show');
        displayError('landingFormError', error_string);
        Recaptcha.destroy();
        showRecaptcha('recaptcha_div');
    }
}
function callBackRestrictedMarketing(result) {
    //alert("restriced" + result);
    var fName = document.LandingSignUpForm.landingFirstName.value;
    var lName = document.LandingSignUpForm.landingLastName.value;
    var email = document.LandingSignUpForm.landingEmail.value;
    var landingDearFirstName = document.LandingSignUpForm.landingDearFirstName.value;
    var message = document.LandingSignUpForm.message.value;
    var state = document.LandingSignUpForm.landingState.value;
    var communityid = document.LandingSignUpForm.landingCommunity.options[document.LandingSignUpForm.landingCommunity.selectedIndex].value;
    //alert("communityID:" + communityid);
    //alert("message:" + message);
   // alert("callBackRestrictedMarketing:" + result);
    if(result){
        Pulte08.AjaxWebServices.TellAFriendService.SendReferral(2, communityid, landingDearFirstName, email, fName, lName, state, message, callBackEmail); 
    } else {
        callBackEmail();
    }
}
function callBackEmail() {
    var url = window.location.href;
    window.location.href = url.substr(0, url.lastIndexOf(".")) + "-thankyou.aspx";
}

function callBackSubmitSuccessSoftJoinPopUp(result, fName, lName) {
    ajaxLoading('submitReferralButton', 'submit', 'stop');
    var softJoinForm = document.getElementById("softJoinPopUpContent");
    var url = window.location.href;
    var communityID = document.LandingSignUpForm.communityIDHidden.value;
    url = url.toLowerCase();
    url = url.replace("predefined", "value-of-delwebb") 
    url = url.replace("postcardinvited", "invited"); // both use the same Thank You page
    window.location.href = url.substr(0, url.lastIndexOf(".")) + "-thankyou.aspx?c=" + communityID;
}

function callBackSubmitFailedSoftJoinPopUp(result, fName) {
    ajaxLoading('submitReferralButton', 'submit', 'stop');
    var softJoinForm = document.getElementById("softJoinPopUpContent");
    softJoinForm.style.display = "none";
    var softJoinThankYou = document.getElementById("softJoinPopUpThankYou");
    softJoinThankYou.innerHTML = "Ooops.... something is broken." + result.Title;
    softJoinThankYou.style.display = "block";
}

function callBackSubmitSuccessCookforCure(result, fName, lName) {
    ajaxLoading('submitButton', 'submit', 'stop');
    pointrollLeadSubmit(); // pointroll action code
    var softJoinForm = document.getElementById("softJoinPopUpContent");
    var contactForm = document.getElementById("softJoinPopUpRSVPForm");
    var thankYouForm = document.getElementById("softJoinPopUpThankYou");
    contactForm.style.display = "none";
    thankYouForm.style.display = "block";
}

function callBackSubmitSuccessCookforCure2(result, fName, lName) {
    ajaxLoading('submitButton', 'submit', 'stop');
    Pulte_TAB_SUBMIT2(); // pointroll action code
    var softJoinForm = document.getElementById("softJoinPopUpContent");
    var contactForm = document.getElementById("softJoinPopUpRSVPForm");
    var thankYouForm = document.getElementById("softJoinPopUpThankYou");
    contactForm.style.display = "none";
    thankYouForm.style.display = "block";




  }

function callBackSubmitFailedCookforCure(result, fName) {
    ajaxLoading('submitButton', 'submit', 'stop');
    var softJoinForm = document.getElementById("softJoinPopUpContent");
    softJoinForm.style.display = "none";
    var softJoinThankYou = document.getElementById("softJoinPopUpThankYou");
    softJoinThankYou.innerHTML = "Ooops.... something is broken." + result.Title;
    softJoinThankYou.style.display = "block";
}


