﻿/* Validation functions for IKEA Seattle Mobile sweeps      */

/* array of fields currently highlighted */


var highlighted = Array();

function optedInText(chk) {
    // Show language options if text msgs opted in. These are already required.

    var makerequired = [lblPhone, lblPhoneConfirm];
    var thislabel = ""

    if (chk.checked) {
        unhide(divTextLangOpts); // show English/Spanish radio buttons     
    }
    else {
        hide(divTextLangOpts); // hide English/Spanish radio buttons
    }

} // end function optedInText()



function showpanel(thisPanel, panelsArray) {
    // Toggle on thisPanel and toggle off other panels in panelsArray
   
   var x;
       
    for (x=0; x<panelsArray.length; x++) {
        
        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();

//  Required: 
//    email, email confirm, firstname, lastname, zip, mobile #

    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);
    }
   
    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 Text Messages checked, check for phone number

    if (isEmpty(txtPhone1) || isEmpty(txtPhone2) || isEmpty(txtPhone3)) {
        errmsg += "Please enter a valid mobile phone number.  ";
        errorlabels.push(lblPhone);
    }
    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);
    }

    if (isEmpty(txtPhoneConfirm1) || isEmpty(txtPhoneConfirm2) || isEmpty(txtPhoneConfirm3)) {
        errmsg += "Please confirm your mobile phone number.  ";
        errorlabels.push(lblPhoneConfirm);
    }
    else if (isNaN(txtPhoneConfirm1.value) || isNaN(txtPhoneConfirm2.value) || isNaN(txtPhoneConfirm3.value) || txtPhoneConfirm1.value.length < 3 || txtPhoneConfirm2.value.length < 3 || txtPhoneConfirm3.value.length < 4) {
        errmsg += "Please confirm your mobile phone number.  ";
        errorlabels.push(lblPhoneConfirm);
    }


    // Make sure phone numbers match
    
  var strPhone = txtPhone1.value + txtPhone2.value + txtPhone3.value;
  var strPhone2 = txtPhoneConfirm1.value + txtPhoneConfirm2.value + txtPhoneConfirm3.value;

if (strPhone != strPhone2) {
        errmsg += "Phone numbers do not match.  ";
        errorlabels.push(lblPhone);
        errorlabels.push(lblPhoneConfirm);
}


    
// No signups checked, flag user
/*
if (!chkNewsletterEmails.checked && !chkEventEmails.checked &&  !chkEventEmails.checked && !chkTextMsgs.checked) {
 errOptins.style.display = "block";
 errmsg += "Please indicate newsletter/catalog preferences.";
} else {
errOptins.style.display = "none";
}
*/

/*
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 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(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) || !isValidEmail(txtForgotZipEmail.value)   )  {
        errmsg = "Please enter a valid email address.";
    } 
    errorlabels.push(lblForgotZipEmail);

    if (errmsg) {
        highlight(errorlabels);
        errForgotZip.innerHTML = errmsg;
        return false ;
    }
    else {
        errForgotZip.innerHTML ="";
        return true;
    }
} // end function validategetzip()


function getradioSelection(group) { 
    for(var k=0;k<group.length;k++) {
        if(group[k].checked) {
            return group[k].value;
        }
    }
} 



/* ------------ Misc functions --------- */

// highlight() displays given labels in reversed out orange
function highlight(fields) {

// can't use this syntax due scriptaculous:
//        for (var label in fields) {

    for (i=0; i<fields.length; i++) {

        document.getElementById(fields[i]).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;

    var i;

   for (i=0; i<fields.length; i++) {
    
    currentClass = document.getElementById(fields[i]).className;
    unhighlightedClass = currentClass.replace(/highlighted/g,"");
    document.getElementById(fields[i]).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 isValidEmail(str) {
  //OLD  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

var emailFilter=/^[^+][+\-.\w]*@([\w\-+]+\.)+[\w]{2,}$/;
return (str.match(emailFilter) != null);
}



function undisable(field) {
    field.disabled = false;
}


function disable (field) {
    field.disabled=true; 
}


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";

}
