﻿/* Validation functions for IKEA catalog sweeps signup */
/*                                              */

// array of fields currently highlighted
var highlighted = Array();


function catalogSelected(chk) {
    var makerequired = [label_firstname, label_lastname, label_address, label_city, label_state];
    if (chk.checked) {

//  if catalog checked, make fields required:
//      [language], firstname, lastname, address1, city, state, zip

        optCatalogEnglish.disabled=false; 
        optCatalogSpanish.disabled=false;

// 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 {
        optCatalogEnglish.disabled=true;
        optCatalogSpanish.disabled=true;

        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 catalogSelected()



function txtmsgsSelected(chk) {
    // Make Mobile Phone inputs required if text msgs opted

    var makerequired = [label_phone];
    var thislabel = ""

    if (chk.checked) {
        for (var label in makerequired) {
            document.getElementById(makerequired[label]).className += " required";
        }
    }
    else {
        
        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;
  }
        
        
    }
} // end function txtmsgsSelected()



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()





/* -----------------  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(label_email);
    } 

    if ( isEmpty(txtEmailConfirm) || !isValidEmail(txtEmailConfirm.value)   )  {
        errmsg += "Please confirm your email address.  ";
        errorlabels.push(label_emailconfirm);
    }

    if (txtEmail.value != txtEmailConfirm.value )  {
        errmsg += "Email and confirmation email do not match.  ";
        errorlabels.push(label_emailconfirm);
    }

    if (isEmpty(txtZip) || isNaN(txtZip.value) || txtZip.value.length<5)  {
        errmsg += "Please enter a valid ZIP Code.  ";
        errorlabels.push(label_zip);
    }

// 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(label_firstname);
        } 

        if ( isEmpty(txtLastName))  {
            errmsg += "Please enter your last name.  ";
            errorlabels.push(label_lastname);
        } 

        if ( isEmpty(txtAddress1))  {
            errmsg += "Please enter your address.  ";
            errorlabels.push(label_address);
        } 

        if ( isEmpty(txtCity))  {
            errmsg += "Please enter your city.  ";
            errorlabels.push(label_city);
        } 

        if ( isEmpty(ddlState))  {
            errmsg += "Please select your state.  ";
            errorlabels.push(label_state);
        } 

        if (!(optCatalogEnglish.checked || optCatalogSpanish.checked)) {
            errmsg += "Please choose a catalog language.  ";
            document.getElementById(lblSelectLanguage).style.display="block"; 
        }
    } // if catalog checked



// if Text Messages checked, require phone number and validate
//   If not checked but data entered, validate

    if (chkTextMsgs.checked || (!isEmpty(txtPhone1) || !isEmpty(txtPhone2) || !isEmpty(txtPhone3))  ) {

        if ( isEmpty(txtPhone1) ||  isEmpty(txtPhone2) ||  isEmpty(txtPhone3) ||
            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(label_phone);
        } 
    } // if text msgs checked



    // update error msg and highlight errorfields

    if (errmsg !="") {
        highlight(errorlabels);
        // errSignup.innerHTML = errmsg;
        // Use generic error msg:
        errSignup.innerHTML = "Please complete or revise items now highlighted.";

        return false ;
    }
    else {
        return true;
    }
} // end function validatesignup()



/* -----------------  Validate fields for refer-a-friend form ------------- */
function validatereferrals() {
   
var errmsg = "";
    
// No labels get highlighted/set to bold here

// Check that any names have a valid email, any email addresses have names

var fldEmail;
var fldName;

// up to 5 referrals 
for (i=1; i<=5; i++) {

fldName = eval("txtFirstNameFriend" + i);
fldEmail = eval("txtEmailFriend" + i);

if (!(isEmpty(fldName) && isEmpty(fldEmail))) {

  if  (isEmpty(fldName) || isEmpty(fldEmail)) {
   errmsg += "Missing information in line  " + i +  ".<br />";
    }

  if (!isValidEmail(fldEmail.value)) {
   errmsg += "Email " + i+  " invalid. <br />";
    }

}
}


    if (errmsg !="") {
       // highlight(errorlabels);


        errReferrals.innerHTML = errmsg;
        return false ;
    }
    else {
        return true;
    }
}  // end function validatereferrals()




/* -----------------  Validate fields for user login ------------- */
function validatelogin() {
    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    if ( isEmpty(txtLoginEmail) || !isValidEmail(txtLoginEmail.value)   )  {
        errmsg += "Please enter a valid email address. ";
        errorlabels.push(label_loginemail);
    } 

    if (isEmpty(txtLoginZip) || isNaN(txtLoginZip.value) || (txtLoginZip.value.length < 5)  )  {
        errmsg += "Please enter a valid ZIP Code. ";
        errorlabels.push(label_loginzip);
    }

    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) || !isValidEmail(txtForgotZipEmail.value)   )  {
        errmsg = "Please enter a valid email address.";
    } 
    errorlabels.push(label_forgotzipemail);

    if (errmsg) {
        highlight(errorlabels);
        errForgotZip.innerHTML = errmsg;
        return false ;
    }
    else {
        errForgotZip.innerHTML ="";
        return true;
    }
} // end function validategetzip()



/* ------------ 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;
    }

    hide(lblSelectLanguage);

} // end function unhighlight




/* ----------------------- Misc utils ----------------------- */

function isEmpty(field) {
    if ((field.value.length==0) || (field.value==null)) {
        return true;
    }
    else {
        return false;
    }
}	



function isValidEmail(str) {

var emailFilter=/^[^+][+\-.\w]*@([\w\-+]+\.)+[\w]{2,}$/;
return (str.match(emailFilter) != null);
}


function disable (field) {
    field.disabled=true; 
}



function unhide (div) {
    document.getElementById(div).style.display="block";
}



function hide (div) {
    document.getElementById(div).style.display="none";
}
