

/* Validation functions for hhgregg profile signup */


/* Array of fields currently highlighted: */

var highlighted = new Array();

/* --------- Showpanel show one panel, hide others ----------- */

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()


/* --------- Show/hide functions to trigger from backend ----------- */

function hideID(id) {    thisID = "#" + id; $(thisID).hide();}

function showID(id) {
    thisID = "#" + id;
    $(thisID).show();
}

function hideClass(myclass) {
    thisClass = "." + myclass;
    $(thisClass).hide();
}

function showClass(myclass) {
    thisClass = "." + myclass;
    $(thisClass).show();
}



/* -----------------  Validate fields for user signup  ------------- */
function validateSignup() {

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
   
    var errorsArray = Array();

    if (isEmpty(document.getElementById(txtFirstName))) {
        errorsArray[flblFirstName] = "Please enter your first name.";
    }

    if (isEmpty(document.getElementById(txtLastName))) {
        errorsArray[flblLastName] = "Please enter your last name.";
    }

    if ( isEmpty(document.getElementById(txtZip)) || isNaN(document.getElementById(txtZip).value)  || document.getElementById(txtZip).value.length < 5) {
        errorsArray[flblZip] = "Please enter a valid ZIP Code.";
}

if (isEmpty(document.getElementById(txtEmail)) || !isValidEmail(document.getElementById(txtEmail).value)) {
    errorsArray[flblEmail] = "Please enter a valid email address. ";
}

if (isEmpty(document.getElementById(txtEmailConfirm)) ) {
    errorsArray[flblEmailConfirm] = "Please confirm your email address. ";
} else {

    if (document.getElementById(txtEmail).value != document.getElementById(txtEmailConfirm).value) {
        errorsArray[flblEmailConfirm] = "Email and confirmation email do not match. ";
    }
}

tmpPhone = document.getElementById(txtPhone1).value.toString() + document.getElementById(txtPhone2).value.toString() +  document.getElementById(txtPhone3).value.toString()  ;


tmpConfPhone = document.getElementById(txtConfPhone1).value.toString() + document.getElementById(txtConfPhone2).value.toString() + document.getElementById(txtConfPhone3).value.toString();

// debug: alert("ph: " + tmpPhone + "\nlength: " + tmpPhone.length + "\ncnf: " + tmpConfPhone + "\nlength: " + tmpConfPhone.length);

// opted in to txt or VM, phone field required

if (document.getElementById(chkTextMsgs).checked || document.getElementById(chkVoiceMails).checked  || tmpPhone.length > 0 || tmpConfPhone.length > 0 ) {


// if either is not a 10 digit number, alert

    if (tmpPhone.length != 10 || isNaN(tmpPhone)) {
        errorsArray[flblPhone] = "Please enter a valid phone number. ";
    }

    if (tmpConfPhone != tmpPhone) {
        errorsArray[flblConfPhone] = "The phone numbers you entered do not match. ";
    }
    if (tmpConfPhone.length != 10 || isNaN(tmpConfPhone)) {
        errorsArray[flblConfPhone] = "Please enter a valid confirmation phone number. ";
    }

} // end if opted in for txt or VM

// DEBUG:  for (var i in errorsArray) {  alert('key is: ' + i + ', value is: ' + errorsArray[i]); }

var errorsCount = 0;
for (k in errorsArray) {  errorsCount += 1;
}
 
    if (errorsCount > 0) {
       
        highlight(errorsArray);
        return false ;
    }
    else {
    
        return true;
    }

} // end function validateSignup()



/* -----------------  Validate fields for user editing profile  ------------- */
function validateProfileEdit() {
    
    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";

    var errorsArray = Array();

    if (isEmpty(document.getElementById(txtFirstName))) {
        errorsArray[flblFirstName] = "Please enter your first name.";
    }

    if (isEmpty(document.getElementById(txtLastName))) {
        errorsArray[flblLastName] = "Please enter your last name.";
    }

    if (isEmpty(document.getElementById(txtZip)) || isNaN(document.getElementById(txtZip).value) || document.getElementById(txtZip).value.length < 5) {
        errorsArray[flblZip] = "Please enter a valid ZIP Code.";
    }

    if (isEmpty(document.getElementById(txtEmail)) || !isValidEmail(document.getElementById(txtEmail).value)) {
        errorsArray[flblEmail] = "Please enter a valid email address. ";
    }

    if (isEmpty(document.getElementById(txtEmailConfirm))) {
        errorsArray[flblEmailConfirm] = "Please confirm your email address. ";
    } else {

        if (document.getElementById(txtEmail).value != document.getElementById(txtEmailConfirm).value) {
            errorsArray[flblEmailConfirm] = "Email and confirmation email do not match. ";
        }
    }

    tmpPhone = document.getElementById(txtPhone1).value.toString() + document.getElementById(txtPhone2).value.toString() + document.getElementById(txtPhone3).value.toString();


    tmpConfPhone = document.getElementById(txtConfPhone1).value.toString() + document.getElementById(txtConfPhone2).value.toString() + document.getElementById(txtConfPhone3).value.toString();

    // debug: alert("ph: " + tmpPhone + "\nlength: " + tmpPhone.length + "\ncnf: " + tmpConfPhone + "\nlength: " + tmpConfPhone.length);

    // opted in to txt or VM, phone field required

    if (document.getElementById(chkTextMsgs).checked || document.getElementById(chkVoiceMails).checked || tmpPhone.length > 0 || tmpConfPhone.length > 0) {


        // if either is not a 10 digit number, alert

        if (tmpPhone.length != 10 || isNaN(tmpPhone)) {
            errorsArray[flblPhone] = "Please enter a valid phone number. ";
        }

        if (tmpConfPhone != tmpPhone) {
            errorsArray[flblConfPhone] = "The phone numbers you entered do not match. ";
        }
        if (tmpConfPhone.length != 10 || isNaN(tmpConfPhone)) {
            errorsArray[flblConfPhone] = "Please enter a valid confirmation phone number. ";
        }

    } // end if opted in for txt or VM


    var errorsCount = 0;
    for (k in errorsArray) {
        errorsCount += 1;
    }

    if (errorsCount > 0) {
       highlight(errorsArray);
        return false;
    }
    else {
        return true;
    }

} // end function validateProfileEdit()



/* -----------------  Validate fields for user login ------------- */
function validateLogin() {

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    var errorsArray = Array();

    if (isEmpty(document.getElementById(txtLoginZip)) || isNaN(document.getElementById(txtLoginZip).value) || (document.getElementById(txtLoginZip).value.length < 5)) {
        errorsArray[flblLoginZip] = "Please enter a valid ZIP Code.";
     }
    if (isEmpty(document.getElementById(txtLoginEmail)) || !isValidEmail(document.getElementById(txtLoginEmail).value)) {
        errorsArray[flblLoginEmail] = "Please enter a valid email address. ";
    }

var errorsCount = 0;
for (k in errorsArray) {
    errorsCount += 1;
}

    if (errorsCount > 0) {
        highlight(errorsArray);
        return false;
    }
    else {
        return true;
    }
    
}  // end function validateLogin



/* -----------------  Validate fields for Forgot Zip panel ------------- */
function validateForgotZip() {

    // if errorlabels exist, de-highlight
    unhighlight(highlighted);

    var errmsg = "";
    var errorlabels = Array();

    var errorsArray = Array();

    if (isEmpty(document.getElementById(txtForgotZipEmail)) || !isValidEmail(document.getElementById(txtForgotZipEmail).value)) {
        errorsArray[flblForgotZipEmail] = "Please enter a valid email address. ";
    }

   var errorsCount = 0;
for (k in errorsArray) {
    errorsCount += 1;
}
    if (errorsCount > 0) {

        highlight(errorsArray);
        return false;
    }
    else {
        return true;
    }
}  // end function validateForgotZip


/* -----------  Validate phone, if submitted  ---------- */

function validatePhone(source, clientside_arguments) {
    var bolOK = false;
    var oPhoneAreaCode = document.getElementById('<%=txtPhone1.ClientID %>');
    var oPhoneNumber1 = document.getElementById('<%=txtPhone2.ClientID %>');
    var oPhoneNumber2 = document.getElementById('<%=txtPhone3.ClientID %>');

    var strPhone = oPhoneAreaCode.value.length + oPhoneNumber1.value.length + oPhoneNumber2.value.length;

    // validate all digits
    if (strPhone == 10) {
        if (!isNaN(oPhoneAreaCode.value + oPhoneNumber1.value + oPhoneNumber2.value)) bolOK = true;
    }
    clientside_arguments.IsValid = bolOK;
}
//]]>


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 --------- */


function highlight(fields) {

    for (var label in fields) {
        // Highlight label, highlight container (currently <tr>), and insert error message:
        $('#' + label).addClass('highlighted');
        $('#' + label).parent().parent().addClass('highlighted');
     
        // Clear first, then add error msg to class errmsg (no "!" character)
        $('#' + label).parent().parent().find(".errmsg").empty();
        $('#' + label).parent().parent().find(".errmsg").css('display', 'block');
        
        $('#' + label).parent().parent().find(".errmsg").append(fields[label]); ;
    }
    highlighted = fields; // remember what fields you just highlighted
}


function unhighlight(fields) {

    for (var label in fields) {

        // unhighlight field label and parent table row
        $('#' + label).removeClass('highlighted');
        $('#' + label).parent().parent().find(".errmsg").empty();
        $('#' + label).parent().parent().removeClass('highlighted');

    }

} // end function unhighlight





/* ------------ validate Refer a Friend ----------------- */

function validateRAF() {

    var errMsg = "";

    errMsg += checkRAFline(txtFirstName1, txtLastName1, txtEmail1, 1);
    /* (errMsg.length ? '<br/>' : '') +  */
    errMsg += checkRAFline(txtFirstName2, txtLastName2, txtEmail2, 2);
    errMsg += checkRAFline(txtFirstName3, txtLastName3, txtEmail3, 3);
    errMsg += checkRAFline(txtFirstName4, txtLastName4, txtEmail4, 4);
    errMsg += checkRAFline(txtFirstName5, txtLastName5, txtEmail5, 5);
    errMsg += checkRAFline(txtFirstName6, txtLastName6, txtEmail6, 6);

    if (errMsg != "") {


        $('#' + errRefer).text(errMsg);

        $('#' + errRefer).removeClass('hidden');

        return false;
    } else {
    $(errRefer).addClass('hidden');
        return true;
    }

} // eof validateRAF()

function checkRAFline(first, last, email, line) {
    var errMsg = "";

    // If any names entered, require valid email.
    // Validate any emails are entered without names.

    var first = document.getElementById(first);
    var last = document.getElementById(last);
    var emailtext = document.getElementById(email).value;
    var email = document.getElementById(email);

    if (((!isEmpty(first) || !isEmpty(last)) && (!isValidEmail(emailtext) || isEmpty(email))) || ((!isValidEmail(emailtext) && !isEmpty(email)))) {

        errMsg = "Please enter a valid email address on line " + line + ". ";

    } // end if

    return errMsg;

} // eof checkRAFline()






/* ----------------------- Misc utils ----------------------- */

function isEmpty(field) {
    if ((field.value.length==0) || (field.value==null)) {
        return true;
    }
    else {
        return false;
    }
}	


function isValidEmail(str) {
  // Now allows 1-letter domains (even ones that aren't grandfathered):
    var emailFilter = /[^+][+\-.\w]*@([\w\-+]+\.)*(\w{2,}|[a-zA-Z]{1})\.[a-zA-Z]{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";
}

// 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";

}