/* JavaScript for the account section
 * 
 * Requires /resources/scripts/common.js
 */
var initialStateProvince = -1;
var stateProvinces = new Array();

// Prototype for managing States and Provinces
function StateProvince(stateId, name, abbrev, countryId) {
    this.stateId = stateId;
    this.name = name;
    this.abbrev = abbrev;
    this.countryId = countryId;
}

function addStateProvince(stateId, name, abbrev, countryId) {
    stateProvinces[stateProvinces.length] = new StateProvince(stateId, name, abbrev, countryId);
}

function changeProfilePicScreenFlow() {
    var saveTypeInput = document.getElementById("saveType");
    var updateButton = document.getElementById("updateAccountButton");

    saveTypeInput.value = "changeProfilePic";
    updateButton.click();

    return false;
}

function lookingForWorkArtistChanged() {
    var mainFrameDocument = document;
    var artistCheckbox = mainFrameDocument.getElementById("lookingForWorkArtist");
    var inkerCheckbox = mainFrameDocument.getElementById("lookingForWorkInker");
    var colorerCheckbox = mainFrameDocument.getElementById("lookingForWorkColorer");
    var drawerCheckbox = mainFrameDocument.getElementById("lookingForWorkIllustrator");
    var lettererCheckbox = mainFrameDocument.getElementById("lookingForWorkLetterer");

    if (!artistCheckbox.checked) {
        inkerCheckbox.checked = false;
        inkerCheckbox.disabled = true;
        colorerCheckbox.checked = false;
        colorerCheckbox.disabled = true;
        drawerCheckbox.checked = false;
        drawerCheckbox.disabled = true;
        lettererCheckbox.checked = false;
        lettererCheckbox.disabled = true;
    } else {
        inkerCheckbox.disabled = false;
        colorerCheckbox.disabled = false;
        drawerCheckbox.disabled = false;
        lettererCheckbox.disabled = false;
    }

    return false;
}

function lookingForWorkChanged() {
    var mainFrameDocument = document;
    var lfwCheckbox = mainFrameDocument.getElementById("lookingForWork");
    var artistCheckbox = mainFrameDocument.getElementById("lookingForWorkArtist");
    var writerCheckbox = mainFrameDocument.getElementById("lookingForWorkWriter");
    var managerCheckbox = mainFrameDocument.getElementById("lookingForWorkManager");
    var musicianCheckbox = mainFrameDocument.getElementById("lookingForWorkMusician");
    var voActorCheckbox = mainFrameDocument.getElementById("lookingForWorkVoActor");

    if (!lfwCheckbox.checked) {
        artistCheckbox.checked = false;
        artistCheckbox.disabled = true;
        writerCheckbox.checked = false;
        writerCheckbox.disabled = true;
        managerCheckbox.checked = false;
        managerCheckbox.disabled = true;
        musicianCheckbox.checked = false;
        musicianCheckbox.disabled = true;
        voActorCheckbox.checked = false;
        voActorCheckbox.disabled = true;
    } else {
        artistCheckbox.disabled = false;
        writerCheckbox.disabled = false;
        managerCheckbox.disabled = false;
        musicianCheckbox.disabled = false;
        voActorCheckbox.disabled = false;
    }

    lookingForWorkArtistChanged();
    lookingForWorkWriterChanged();

    return false;
}

function lookingForWorkFormReset() {
    var mainFrameDocument = document;
    var lfwForm = mainFrameDocument.getElementById("lookingForWorkForm");
    lfwForm.reset();
    lookingForWorkChanged();
}

function lookingForWorkWriterChanged() {
    var mainFrameDocument = document;
    var writerCheckbox = mainFrameDocument.getElementById("lookingForWorkWriter");
    var actionCheckbox = mainFrameDocument.getElementById("lookingForWorkAction");
    var adventureCheckbox = mainFrameDocument.getElementById("lookingForWorkAdventure");
    var comedyCheckbox = mainFrameDocument.getElementById("lookingForWorkComedy");
    var dramaCheckbox = mainFrameDocument.getElementById("lookingForWorkDrama");
    var fantasyCheckbox = mainFrameDocument.getElementById("lookingForWorkFantasy");
    var horrorCheckbox = mainFrameDocument.getElementById("lookingForWorkHorror");
    var novellaCheckbox = mainFrameDocument.getElementById("lookingForWorkNovella");
    var childrensCheckbox = mainFrameDocument.getElementById("lookingForWorkChildrens");

    if (!writerCheckbox.checked) {
        actionCheckbox.checked = false;
        actionCheckbox.disabled = true;
        adventureCheckbox.checked = false;
        adventureCheckbox.disabled = true;
        comedyCheckbox.checked = false;
        comedyCheckbox.disabled = true;
        dramaCheckbox.checked = false;
        dramaCheckbox.disabled = true;
        fantasyCheckbox.checked = false;
        fantasyCheckbox.disabled = true;
        horrorCheckbox.checked = false;
        horrorCheckbox.disabled = true;
        novellaCheckbox.checked = false;
        novellaCheckbox.disabled = true;
        childrensCheckbox.checked = false;
        childrensCheckbox.disabled = true;
    } else {
        actionCheckbox.disabled = false;
        adventureCheckbox.disabled = false;
        comedyCheckbox.disabled = false;
        dramaCheckbox.disabled = false;
        fantasyCheckbox.disabled = false;
        horrorCheckbox.disabled = false;
        novellaCheckbox.disabled = false;
        childrensCheckbox.disabled = false;
    }

    return false;
}

function newProfilePicUpload() {
    var newProfilePic = document.getElementById("newProfilePicInput").value;
    var changeProfilePicForm = document.getElementById("changeProfilePicForm");
    var uploadingPictureCell = document.getElementById("uploadingPictureCell");
    var fieldAsterisks = document.getElementById("newProfilePicInput.asterisks");
    var errorDiv = document.getElementById("errorDiv");
    var cropButton = document.getElementById('cropButton');

    if (newProfilePic != null && newProfilePic.length != 0) {
        newProfilePic = newProfilePic.toLowerCase();
        if (newProfilePic.lastIndexOf(".png") != (newProfilePic.length - 4)
                && newProfilePic.lastIndexOf(".jpg") != (newProfilePic.length - 4)
                && newProfilePic.lastIndexOf(".jpeg") != (newProfilePic.length - 5)) {
            errorDiv.innerHTML = "Only PNG and JPG files are currently accepted.";
            fieldAsterisks.style.visibility = "visible";
        } else {
            if (errorDiv.innerHTML.length > 0) {
                errorDiv.innerHTML = "";
            }
            cropButton.disabled = "disabled";
            newProfilePic.disabled = "disabled";
            fieldAsterisks.style.visibility = "hidden";
            uploadingPictureCell.innerHTML =
                    "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"25\" height=\"25\">"
                    + "<param name=\"movie\" value=\"http://" + host + "/resources/swf/rotating_bubbles.swf\">"
                    + "<param name=\"quality\" value=\"high\">"
                    + "<param name=\"bgcolor\" value=\"#FFFFFF\">"
                    + "<embed name=\"rotating_bubbles\" src=\"http://" + host + "/resources/swf/rotating_bubbles.swf\" width=\"25\" height=\"25\" quality=\"high\" bgcolor=\"#FFFFFF\" type=\"application/x-shockwave-flash\" pluginspage=\"https://www.macromedia.com/go/getflashplayer\">"
                    + "</object> Uploading...";
            changeProfilePicForm.submit();
        }
    }
}

function setInitialStateProvince(stateProvinceId) {
    initialStateProvince = stateProvinceId;
}

/**
 * Validate some data on the form before submitting the form.  All validation
 * will still be provided in the servlet or back end, but this can save some
 * time for submission, and help the user experience.
 */
function submitNewPublisherForm() {
    var publisherName = nvl(document.getElementById("newPublisher.name").value,"");
    var phone = nvl(document.getElementById("newPublisher.phone").value,"");
    var address1 = nvl(document.getElementById("newPublisher.address1").value,"");
    var city = nvl(document.getElementById("newPublisher.city").value,"");
    var zipcode = nvl(document.getElementById("newPublisher.zipcode").value,"");
    var contactTitle = nvl(document.getElementById("newPublisher.contactTitle").value,"");
    var contactFName = nvl(document.getElementById("newPublisher.contactFName").value,"");
    var contactLName = nvl(document.getElementById("newPublisher.contactLName").value,"");
    var contactEmail = nvl(document.getElementById("newPublisher.contactEmail").value,"");
    var errorDiv = document.getElementById("errorDiv");
    var publisherNameAsterisks = document.getElementById("newPublisher.name.asterisks");
    var phoneAsterisks = document.getElementById("newPublisher.phone.asterisks");
    var address1Asterisks = document.getElementById("newPublisher.address1.asterisks");
    var cityAsterisks = document.getElementById("newPublisher.city.asterisks");
    var zipcodeAsterisks = document.getElementById("newPublisher.zipcode.asterisks");
    var contactTitleAsterisks = document.getElementById("newPublisher.contactTitle.asterisks");
    var contactFNameAsterisks = document.getElementById("newPublisher.contactFName.asterisks");
    var contactLNameAsterisks = document.getElementById("newPublisher.contactLName.asterisks");
    var contactEmailAsterisks = document.getElementById("newPublisher.contactEmail.asterisks");
    var errorString = "";

    if (publisherName.length == 0) {
        errorString = appendErrorString(errorString, "Publisher name is required for"
                + " the publisher application.");
        publisherNameAsterisks.style.visibility = "visible";
    } else {
        publisherNameAsterisks.style.visibility = "hidden";
    }

    if (phone.length == 0) {
        errorString = appendErrorString(errorString, "Phone number is required for"
                + " the publisher application.");
        phoneAsterisks.style.visibility = "visible";
    } else {
        phoneAsterisks.style.visibility = "hidden";
    }

    if (address1.length == 0) {
        errorString = appendErrorString(errorString, "Address line 1 is required for"
                + " the publisher application.");
        address1Asterisks.style.visibility = "visible";
    } else {
        address1Asterisks.style.visibility = "hidden";
    }

    if (city.length == 0) {
        errorString = appendErrorString(errorString, "City is required for"
                + " the publisher application.");
        cityAsterisks.style.visibility = "visible";
    } else {
        cityAsterisks.style.visibility = "hidden";
    }

    if (zipcode.length == 0) {
        errorString = appendErrorString(errorString, "Zipcode is required for"
                + " the publisher application.");
        zipcodeAsterisks.style.visibility = "visible";
    } else {
        zipcodeAsterisks.style.visibility = "hidden";
    }

    if (contactTitle.length == 0) {
        errorString = appendErrorString(errorString, "Contact title is required for"
                + " the publisher application.");
        contactTitleAsterisks.style.visibility = "visible";
    } else {
        contactTitleAsterisks.style.visibility = "hidden";
    }

    if (contactFName.length == 0) {
        errorString = appendErrorString(errorString, "Contact first name is required for"
                + " the publisher application.");
        contactFNameAsterisks.style.visibility = "visible";
    } else {
        contactFNameAsterisks.style.visibility = "hidden";
    }

    if (contactLName.length == 0) {
        errorString = appendErrorString(errorString, "Contact last name is required for"
                + " the publisher application.");
        contactLNameAsterisks.style.visibility = "visible";
    } else {
        contactLNameAsterisks.style.visibility = "hidden";
    }

    if (contactEmail.length == 0) {
        errorString = appendErrorString(errorString, "Contact email is required for"
                + " the publisher application.");
        contactEmailAsterisks.style.visibility = "visible";
    } else {
        contactEmailAsterisks.style.visibility = "hidden";
    }

    errorDiv.innerHTML = errorString;
    return (errorString.length == 0);
}

function submitLookingForWorkForm() {
    return true;
}

/**
 * Validate some data on the form before submitting the form.  All validation
 * will still be provided in the servlet or back end, but this can save some
 * time for submission, and help the user experience.
 */
function submitUpdatePublisherAppDataForm() {
    var firstName = nvl(document.getElementById("person.fName").value,"");
    var lastName = nvl(document.getElementById("person.lName").value,"");
    var billingPhone = nvl(document.getElementById("billingPhone").value,"");
    var errorDiv = document.getElementById("errorDiv");
    var firstNameAsterisks = document.getElementById("person.fName.asterisks");
    var lastNameAsterisks = document.getElementById("person.lName.asterisks");
    var billingPhoneAsterisks = document.getElementById("billingPhone.asterisks");
    var errorString = "";

    if (firstName.length == 0) {
        errorString = appendErrorString(errorString, "First name is required for"
                + " the publisher application.");
        firstNameAsterisks.style.visibility = "visible";
    } else {
        firstNameAsterisks.style.visibility = "hidden";
    }

    if (lastName.length == 0) {
        errorString = appendErrorString(errorString, "Last name is required for"
                + " the publisher application.");
        lastNameAsterisks.style.visibility = "visible";
    } else {
        lastNameAsterisks.style.visibility = "hidden";
    }

    if (billingPhone.length == 0) {
        errorString = appendErrorString(errorString, "Phone number is required for"
                + " the publisher application.");
        billingPhoneAsterisks.style.visibility = "visible";
    } else {
        billingPhoneAsterisks.style.visibility = "hidden";
    }

    errorDiv.innerHTML = errorString;
    return (errorString.length == 0);
}

function updateStateProvDropdown(countryDropdown, stateProvDropdown) {
    if (countryDropdown == null
            || countryDropdown == undefined
            || stateProvDropdown == null
            || stateProvDropdown == undefined) {
        return;
    }
    
    var selectedCountry = countryDropdown.options[countryDropdown.selectedIndex].value;
    var stateProvDropdownLen = stateProvDropdown.length;
    var i;
    var initialStateFound = false;
    
    for (i = stateProvDropdownLen - 1; i >= 0; i--) {
        stateProvDropdown.remove(i);
    }
    
    var newIndex = 0;
    for (i = 0; i < stateProvinces.length; i++) {
        if (stateProvinces[i].countryId == selectedCountry) {
            addOption(stateProvDropdown, stateProvinces[i].name, stateProvinces[i].stateId);
            
            if (stateProvinces[i].stateId == initialStateProvince) {
                stateProvDropdown.selectedIndex = newIndex;
                initialStateFound = true;
            }
            newIndex++;
        }
    }
    
    if (!initialStateFound) {
        stateProvDropdown.selectedIndex = 0;   
    }
    
    // Reset it to the first selection.
    initialStateProvince = 0;    
}

function saveProfileImage() {
    var dosaveInput = document.getElementById("dosave");
    var changeProfilePicForm = document.getElementById("changeProfilePicForm");

    dosaveInput.value = "true";

    changeProfilePicForm.submit();
}

function submitCrop() {
    var docropInput = document.getElementById("docrop");
    var changeProfilePicForm = document.getElementById("changeProfilePicForm");
    var uploadingPictureCell = document.getElementById("uploadingPictureCell");
    var newProfilePic = document.getElementById("newProfilePicInput").value;
    var cropButton = document.getElementById('cropButton');
    var fieldAsterisks = document.getElementById("newProfilePicInput.asterisks");
    var errorDiv = document.getElementById("errorDiv");

    docropInput.value = "true";
    if (errorDiv.innerHTML.length > 0) {
        errorDiv.innerHTML = "";
    }
    cropButton.disabled = "disabled";
    newProfilePic.disabled = "disabled";
    fieldAsterisks.style.visibility = "hidden";
    uploadingPictureCell.innerHTML =
            "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"25\" height=\"25\">"
            + "<param name=\"movie\" value=\"http://" + host + "/resources/swf/rotating_bubbles.swf\">"
            + "<param name=\"quality\" value=\"high\">"
            + "<param name=\"bgcolor\" value=\"#FFFFFF\">"
            + "<embed name=\"rotating_bubbles\" src=\"http://" + host + "/resources/swf/rotating_bubbles.swf\" width=\"25\" height=\"25\" quality=\"high\" bgcolor=\"#FFFFFF\" type=\"application/x-shockwave-flash\" pluginspage=\"https://www.macromedia.com/go/getflashplayer\">"
            + "</object> Cropping...";

    changeProfilePicForm.submit();
}

function submitUpdateAccountForm() {
    return true;
}

/**
 * Validate some data on the form before submitting the form.  All validation 
 * will still be provided in the servlet or back end, but this can save some
 * time for submission, and help the user experience.
 */
function submitUpdateEmailForm() {
    var emailAddress = nvl(document.getElementById("newEmail").value,"");
    var vEmailAddress = nvl(document.getElementById("verifiedEmail").value,"");
    var errorDiv = document.getElementById("errorDiv");
    var emailAsterisks = document.getElementById("updateAccount.email.asterisks");
    var vemailAsterisks = document.getElementById("updateAccount.vemail.asterisks");
    var errorString = "";
    
    emailAddress = emailAddress.replace(/\s/g,'');
    vEmailAddress = vEmailAddress.replace(/\s/g,'');
    
    if (!isValidEmail(emailAddress)) {
        errorString = appendErrorString(errorString, "The email address"
                + " supplied is not valid: \"" + emailAddress + "\".");
        emailAsterisks.style.visibility = "visible";
    } else {
        emailAsterisks.style.visibility = "hidden";
    }
    
    if (vEmailAddress.toLowerCase() != emailAddress.toLowerCase()) {
        errorString = appendErrorString(errorString, "The verification email "
                + "address does not match the original email address.");
        vemailAsterisks.style.visibility = "visible";
    } else {
        vemailAsterisks.style.visibility = "hidden";
    }
    
    errorDiv.innerHTML = errorString;
    return (errorString.length == 0);
}

function submitUpdatePasswordForm() {
    var nPassword = nvl(document.getElementById("newPassword").value,"");
    var vNPassword = nvl(document.getElementById("verifiedPassword").value,"");
    var errorDiv = document.getElementById("errorDiv");
    var nPasswordAsterisks = document.getElementById("updateAccount.newpassword.asterisks");
    var vNPasswordAsterisks = document.getElementById("updateAccount.vpassword.asterisks");
    var errorString = "";

    if (nPassword.length < 8) {
        errorString = appendErrorString(errorString, "Password must be at least"
                + " 8 characters long.");
        nPasswordAsterisks.style.visibility = "visible";
    } else if (nPassword.match(passwordRegExp) == null
            || nPassword.match(passwordRegExp) != nPassword) {
        errorString = appendErrorString(errorString, "Password can only consist"
                + " of letters (A-Z, a-z), numbers (0-9), _, @, or ! only.");
        nPasswordAsterisks.style.visibility = "visible";
    } else {
        nPasswordAsterisks.style.visibility = "hidden";
    }

    if (vNPassword != nPassword) {
        errorString = appendErrorString(errorString, "The verification password"
                    + " does not match the original password.");
        if (vNPassword.toLowerCase() == nPassword.toLowerCase()) {
            errorString = appendErrorString(errorString, "Passwords are case"
                    +  " sensitive.");
        }
        vNPasswordAsterisks.style.visibility = "visible";
    } else {
        vNPasswordAsterisks.style.visibility = "hidden";
    }

    errorDiv.innerHTML = errorString;
    return (errorString.length == 0);
}