﻿/* Validation functions for IKEA profile signup */
/*                                              */

// array of fields currently highlighted
var highlighted = Array();

/* ---------  Validate fields for optout form ----------- */

function validateoptout() {
// if errorlabels exist, de-highlight
unhighlight(highlighted);

var errmsg = "";
var errorlabels = Array();
    
if (isEmpty(txtZip) || isNaN(txtZip.value) || txtZip.value.length<5)  {
        errmsg += "Please enter a valid ZIP Code.  ";
        errorlabels.push(lblZip);
    }

        if ( isEmpty(txtFirstName))  {
            errmsg += "Please enter your first name.  ";
            errorlabels.push(lblFirstName);
        } 

        if ( isEmpty(txtLastName))  {
            errmsg += "Please enter your last name.  ";
            errorlabels.push(lblLastName);
        } 

        if ( isEmpty(txtAddress1))  {
            errmsg += "Please enter your address.  ";
            errorlabels.push(lblAddress1);
        } 

        if ( isEmpty(txtCity))  {
            errmsg += "Please enter your city.  ";
            errorlabels.push(lblCity);
        } 

        if ( isEmpty(ddlState))  {
            errmsg += "Please select your state.  ";
            errorlabels.push(lblState);
        }


    // update error msg and highlight errorfields

    if (errmsg !="") {
        highlight(errorlabels);

        // Use generic error msg:
        errOptOut.innerHTML = "Please complete or revise items now highlighted.";

        return false ;
    }
    else {
        return true;
    }
} // end function validateoptout()



/* ---------  Validate fields for catalog request without email ----------- */

function validatecatalogrequest() {
// if errorlabels exist, de-highlight

 unhighlight(highlighted);

var errmsg = "";
var errorlabels = Array();
    
if (isEmpty(txtZip) || isNaN(txtZip.value) || txtZip.value.length<5)  {
        errmsg += "Please enter a valid ZIP Code.  ";
        errorlabels.push(lblZip);
    }

        if ( isEmpty(txtFirstName))  {
            errmsg += "Please enter your first name.  ";
            errorlabels.push(lblFirstName);
        } 

        if ( isEmpty(txtLastName))  {
            errmsg += "Please enter your last name.  ";
            errorlabels.push(lblLastName);
        } 

        if ( isEmpty(txtAddress1))  {
            errmsg += "Please enter your address.  ";
            errorlabels.push(lblAddress1);
        } 

        if ( isEmpty(txtCity))  {
            errmsg += "Please enter your city.  ";
            errorlabels.push(lblCity);
        } 

        if ( isEmpty(ddlState))  {
            errmsg += "Please select your state.  ";
            errorlabels.push(lblState);
        }

 if (! chkCatalog.checked)  {
            errmsg += "Please check box to confirm your catalog request.  ";
            errorlabels.push(lblCatalog);
        }
        
    // update error msg and highlight errorfields

    if (errmsg !="") {
        highlight(errorlabels);

        // Use generic error msg:
        errCatalogRequest.innerHTML = "Please complete or revise items now highlighted.";
        
        return false ;
    }
    else {
        return true;
    }

} // end function validatecataloguerequest()




function optedInText(chk) {
    // Display language options and make Mobile Phone inputs required if text msgs opted

    var makerequired = [lblPhone, lblPhoneConfirm];
    var thislabel = ""
  
    if (chk.checked) {
       unhide(trTextLangOpts); // show English/Spanish radio buttons

        for (var label in makerequired) {
            document.getElementById(makerequired[label]).className += " required";
        }
    }
    else {
        hide(trTextLangOpts); // hide English/Spanish radio buttons
        for (var label in makerequired) {
            currentClass = document.getElementById(makerequired[label]).className;
            unrequiredClass = currentClass.replace(/required/g, "");
            document.getElementById(makerequired[label]).className = unrequiredClass;
        }
    }

} // end function optedInText()


function optedInCatalog(chk) {
    var makerequired = [lblFirstName, lblLastName, lblAddress, lblCity, lblState];
    if (chk.checked) {

        //  if catalog checked, make fields required:
        //      [language], firstname, lastname, address1, city, state, zip

        unhide(trCatLangOpts); // show English/Spanish radio buttons

        // on profile page, says "Please add address above"
        //  on reg page, this is an empty div

        // show Please Provide Address message, only if fields are not completed

        unhide(litCatalogMsg);

        for (var label in makerequired) {
            document.getElementById(makerequired[label]).className += " required";
        }
    }
    else {
        hide(trCatLangOpts); // hide English/Spanish radio buttons

        var currentClass;
        var unrequiredClass;

        for (var label in makerequired) {
            currentClass = document.getElementById(makerequired[label]).className;
            unrequiredClass = currentClass.replace(/required/g, "");
            document.getElementById(makerequired[label]).className = unrequiredClass;
        }

        hide(lblSelectLanguage);
        hide(litCatalogMsg);
    }

} // end function optedInCatalog()




function showpanel(thisPanel, panelsArray) {
    // Toggle on thisPanel and toggle off other panels in panelsArray
    var x;
  
    for (x in panelsArray) {
    
        if (panelsArray[x]==thisPanel) {
            document.getElementById(panelsArray[x]).style.display = "block";
        }
        else {
            document.getElementById(panelsArray[x]).style.display = "none";
        }
    }
} // end function showPanel()



function setPanelState(strPanelState) {
    if (strPanelState == 'editProfile') {
        document.getElementById(panel_DisplayProfile).style.display = "none";
        document.getElementById(panel_EditProfile).style.display = "block";
        document.getElementById(panel_DisplaySurvey).style.display = "block";
        document.getElementById(panel_EditSurvey).style.display = "none";
    }
    else if (strPanelState == 'editSurvey') {
        document.getElementById(panel_DisplayProfile).style.display = "block";
        document.getElementById(panel_EditProfile).style.display = "none";
        document.getElementById(panel_DisplaySurvey).style.display = "none";
        document.getElementById(panel_EditSurvey).style.display = "block";
    }
    // default to the "display" state
    else {
        document.getElementById(panel_DisplayProfile).style.display = "block";
        document.getElementById(panel_EditProfile).style.display = "none";
        document.getElementById(panel_DisplaySurvey).style.display = "block";
        document.getElementById(panel_EditSurvey).style.display = "none";
    }
    document.forms[0].reset();
}



/* -----------------  Validate fields for user signup  ------------- */
function validateSignUp() {

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    if ( isEmpty(txtEmail) || !isValidEmail(txtEmail.value)   )  {
        errmsg += "Please enter a valid email address.  ";
        errorlabels.push(lblEmail);
    } 

    if ( isEmpty(txtEmailConfirm) || !isValidEmail(txtEmailConfirm.value)   )  {
        errmsg += "Please confirm your email address.  ";
        errorlabels.push(lblEmailConfirm);
    }

    if (txtEmail.value != txtEmailConfirm.value )  {
        errmsg += "Email and confirmation email do not match.  ";
        errorlabels.push(lblEmailConfirm);
    }

    if (isEmpty(txtZip) || isNaN(txtZip.value) || txtZip.value.length<5)  {
        errmsg += "Please enter a valid ZIP Code.  ";
        errorlabels.push(lblZip);
    }

    // value is 0 for IKEA Direct, blank if none selected
    if (ddlPreferredStore.value == "") {
        errmsg += "Please select your preferred store.  ";
        errorlabels.push(lblPreferredStore);
    }

// If catalog checked, required: firstname, lastname, address, city, state
//  If language option not checked, display highlighted label

    if (chkCatalog.checked) {
        if ( isEmpty(txtFirstName))  {
            errmsg += "Please enter your first name.  ";
            errorlabels.push(lblFirstName);
        } 

        if ( isEmpty(txtLastName))  {
            errmsg += "Please enter your last name.  ";
            errorlabels.push(lblLastName);
        } 

        if ( isEmpty(txtAddress1))  {
            errmsg += "Please enter your address.  ";
            errorlabels.push(lblAddress);
        } 

        if ( isEmpty(txtCity))  {
            errmsg += "Please enter your city.  ";
            errorlabels.push(lblCity);
        } 

        if ( isEmpty(ddlState))  {
            errmsg += "Please select your state.  ";
            errorlabels.push(lblState);
        } 

        if (!(optCatalogEnglish.checked || optCatalogSpanish.checked)) {
            errmsg += "Please choose a catalog language.  ";
            document.getElementById(lblSelectLanguage).style.display="block"; 
        }
    } // if catalog checked

// if Text Messages checked, or if any phone #s entered, check for phone number and/or validate

    if (chkTextMsgs.checked || !isEmpty(txtPhone1) ||  !isEmpty(txtPhone2) ||  !isEmpty(txtPhone3)) {
        if ( isEmpty(txtPhone1) ||  isEmpty(txtPhone2) ||  isEmpty(txtPhone3))
        {
            errmsg += "Please enter a valid mobile phone number.  ";
            errorlabels.push(lblPhone);
            errorlabels.push(lblPhoneConfirm);
        } 
        else if (isNaN(txtPhone1.value) || isNaN(txtPhone2.value) || isNaN(txtPhone3.value) || txtPhone1.value.length < 3 || txtPhone2.value.length < 3 || txtPhone3.value.length < 4) {
            errmsg += "Please enter a valid mobile phone number.  ";
            errorlabels.push(lblPhone);
        }
        else if ((txtPhone1.value != txtPhoneConfirm1.value) || (txtPhone2.value != txtPhoneConfirm2.value) || (txtPhone3.value != txtPhoneConfirm3.value)) {
            errmsg += "Mobile number and confirmation mobile number do not match.  ";
            errorlabels.push(lblPhone);
            errorlabels.push(lblPhoneConfirm);
        }
    } // if text msgs checked


    // Require either one of first three optins, OR mobile
    if (!chkNewsletterEmails.checked && !chkEventEmails.checked && !chkBusinessEmails.checked && !chkTextMsgs.checked && !chkCatalog.checked) {
    
        if (typeof (errOptins) != "undefined") {
            errOptins.style.display = "block";
            errmsg += "To activate an account, you must choose one or more subscription options below.";
        }

        errorlabels.push(lblChkNewsletterEmails);
        errorlabels.push(lblChkEventEmails);
        errorlabels.push(lblChkTextMsgs);
        errorlabels.push(lblChkBusinessEmails);
        errorlabels.push(lblChkCatalog);
        
        
    } else {
        if (typeof(errOptins) != "undefined") {
            errOptins.style.display = "none";
        }
    }


    // update error msg and highlight errorfields

    if (errmsg !="") {
        highlight(errorlabels);

        // Use generic error msg:
        errSignup.innerHTML = "Please complete or revise items now highlighted.";

        return false ;
    }
    else {
        return true;
    }
} // end function validatesignup()



/* -----------------  Validate fields for Mobile Registration  ------------- */
function validateMobileRegistration() {
   // Email optin is not required on this screen. 

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    // If no emails/txt are selected, highlight those options (will display
    // with notice above
    
 
    if (isEmpty(txtEmail) || !isValidEmail(txtEmail.value)) {
        errmsg += "Please enter a valid email address.  ";
        errorlabels.push(lblEmail);
    }

    if (isEmpty(txtEmailConfirm) || !isValidEmail(txtEmailConfirm.value)) {
        errmsg += "Please confirm your email address.  ";
        errorlabels.push(lblEmailconfirm);
    }

    if (txtEmail.value != txtEmailConfirm.value) {
        errmsg += "Email and confirmation email do not match.  ";
        errorlabels.push(lblEmailconfirm);
    }

    if (isEmpty(txtZip) || isNaN(txtZip.value) || txtZip.value.length < 5) {
        errmsg += "Please enter a valid ZIP Code.  ";
        errorlabels.push(lblZip);
    }

    // value is 0 for IKEA Direct, blank if none selected
    if (ddlPreferredStore.value == "") {
        errmsg += "Please select your preferred store.  ";
        errorlabels.push(lblPreferredStore);
    }

    // If catalog checked, required: firstname, lastname, address, city, state
    //  If language option not checked, display highlighted label

    if (chkCatalog.checked) {
        if (isEmpty(txtFirstName)) {
            errmsg += "Please enter your first name.  ";
            errorlabels.push(lblFirstName);
        }

        if (isEmpty(txtLastName)) {
            errmsg += "Please enter your last name.  ";
            errorlabels.push(lblLastName);
        }

        if (isEmpty(txtAddress1)) {
            errmsg += "Please enter your address.  ";
            errorlabels.push(lblAddress);
        }

        if (isEmpty(txtCity)) {
            errmsg += "Please enter your city.  ";
            errorlabels.push(lblCity);
        }

        if (isEmpty(ddlState)) {
            errmsg += "Please select your state.  ";
            errorlabels.push(lblState);
        }

        if (!(optCatalogEnglish.checked || optCatalogSpanish.checked)) {
            errmsg += "Please choose a catalog language.  ";
            document.getElementById(lblSelectLanguage).style.display = "block";
        }
    } // if catalog checked

   
    // update error msg and highlight errorfields

    if (errmsg != "") {
 
    
        highlight(errorlabels);

        // Use generic error msg:
        errSignup.innerHTML = "Please complete or revise items now highlighted.";

        return false;
    }
    else {
        return true;
    }
    
    
} // end function validateMobileRegistration()








/* -----------------  Validate fields for user login ------------- */
function validateLogin() {

    // New version: If not valid email, check if valid phone number
    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    if (isEmpty(txtLoginEmail) || !isValidEmailOrPhone(txtLoginEmail.value) ) {

        errmsg += "Please enter a valid email address or mobile phone number. ";
        errorlabels.push(lblLoginEmail);
    } 

    if (isEmpty(txtLoginZip) || isNaN(txtLoginZip.value) || (txtLoginZip.value.length < 5)  )  {
        errmsg += "Please enter a valid ZIP Code. ";
        errorlabels.push(lblLoginZip);
    }

    if (errmsg !="") {
        highlight(errorlabels);
        errLogin.innerHTML = errmsg;
        return false ;
    }
    else {
        return true;
    }
}  // end function validatelogin()




/* -----------------  Validate fields if user forgot zip ------------- */
function validateGetZip() {
    // Show error msgs and highlight labels in orange
    var errmsg = "";
    var errorlabels = Array();

    if (isEmpty(txtForgotZipEmail) || !isValidEmailOrPhone(txtForgotZipEmail.value)) {
        errmsg = "Please enter a valid email address or mobile phone number.";
    } 
    errorlabels.push(lblForgotZipEmail);

    if (errmsg) {
        highlight(errorlabels);
        errForgotZip.innerHTML = errmsg;
        return false ;
    }
    else {
        errForgotZip.innerHTML ="";
        return true;
    }
} // end function validategetzip()



/* -----------------  Validate survey questions ------------- */
function validatesurvey() {
    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    // Show error msgs and highlight labels in orange
    /*
        vars for Qs:
        label_ikeaexp    optlstIkeaExp
        label_designinterest  optlstDesignInterest
        label_livingarr  optlstLivingArr
        label_havekids  optlstHaveKids
        label_havebusiness  optlstHaveBusiness 
    */

    // div for displaying errors for grey survey div

    // var errSurvey = document.getElementById('<%=errSurvey.clientID%>');

    var errmsg = "";
    var errorlabels = Array();
    var thisquestion;

    // q1 :  What's your experience with IKEA?
    if (!getradioSelection(optlstIkeaExp))  {
        errmsg += "Please answer.  ";
        errorlabels.push(label_ikeaexp);
    } 

    // q2 :  How interested are you in interior design?
    if (!getradioSelection(optlstDesignInterest))  {
        errmsg += "Please answer.  ";
        errorlabels.push(label_designinterest);
    } 

    // q3 :  How would you describe your living arrangements?
    if (!getradioSelection(optlstLivingArr))  {
        errmsg += "Please answer.  ";
        errorlabels.push(label_livingarr);
    } 

    // q4 :  have kids?
    if (!getradioSelection(optlstHaveKids))  {
        errmsg += "Please answer.  ";
        errorlabels.push(label_havekids);
    } 

    // q5 :  have biz?
    if (!getradioSelection(optlstHaveBusiness))  {
        errmsg += "Please answer.  ";
        errorlabels.push(label_havebusiness);
    } 

    // value is 0 for IKEA Direct, blank if none selected
    if (ddlPreferredStore.value=="")  {
        errmsg += "Please select your preferred store.  ";
        errorlabels.push(lblPreferredStore);
    } 

    if (errmsg) {
        highlight(errorlabels);
        
        // Use generic error msg:
        errSurvey.innerHTML = "Please answer all questions.";
        return false ;
    }
    else {
        errSurvey.innerHTML ="";
        return true;
    }
} // end function validatesurvey()



function getradioSelection(group) { 
    for(var k=0;k<group.length;k++) {
        if(group[k].checked) {
            return group[k].value;
        }
    }
} 



/* ------------ Misc functions used by validation routines above --------- */

// highlight() displays given labels in reversed out orange
function highlight(fields) {
    for (var label in fields) {
        document.getElementById(fields[label]).className += " highlighted";    
    } 
    highlighted = fields; // remember what fields you just highlighted
} 


// unhighlight() turns highlighting off: revert to pre-highlighted CSS class 

function unhighlight(fields) {

    var currentClass;
    var unhighlightedClass;
   
    for (var label in fields) {
    
    currentClass = document.getElementById(fields[label]).className;
    unhighlightedClass = currentClass.replace(/highlighted/g,"");
    document.getElementById(fields[label]).className = unhighlightedClass;
    }
 
 // if the SelectLanguage label is defined, hide it (only
 //    on certain forms)

  if (lblSelectLanguage != null) {
    hide(lblSelectLanguage);
  }

//    hide(lblSelectLanguage);


} // end function unhighlight


/* ----------------------- Misc utils ----------------------- */

function isEmpty(field) {
    if ((field.value.length==0) || (field.value==null)) {
        return true;
    }
    else {
        return false;
    }
}


function isValidEmailOrPhone(str) {
    if (isValidEmail(str) || isValidPhone(str)) {
        return true;
    } else {
        return false;
    }
}

function isValidPhone(str) {

    var phoneFilter = /^\+?1?[\( \-\.]*[0-9]{3}[\) \-\.]*[0-9]{3}[ \-\.]?[0-9]{4}$/;
    return (str.match(phoneFilter) != null);
}


function isValidEmail(str) {

var emailFilter=/^[^+][+\-.\w]*@([\w\-+]+\.)+[\w]{2,}$/;
return (str.match(emailFilter) != null);
}



function disable (field) {
    field.disabled=true; 
}

function undisable(field) {
    field.disabled = false;
}



function unhide (div) {
    document.getElementById(div).style.display="block";
}

function hide (div) {
    document.getElementById(div).style.display="none";
}

// Checks if return key was pressed
function checkreturn(e){ 

var characterCode ;

if(e && e.which){ 
e = e;
characterCode = e.which; //NN4 which property
}
else{
e = event;
characterCode = e.keyCode //IE keyCode property
}


if(characterCode == 13){   // if hit return/enter, submit form
document.forms[0].submit();
return false;
}
else{
return true;
}

}

// show modal dialog

function modalDialog() {

myCurtain = document.getElementById("curtain");
myCurtain.style.display = (myCurtain.style.display == "block") ? "none" : "block";

}


function oldmodalDialog() {

myCurtain = document.getElementById("curtain");
myCurtain.style.visibility = (myCurtain.style.visibility == "visible") ? "hidden" : "visible";

}