﻿/********************************************************
 * Contact Us form
 
 Information on form fields:
 
 ID of field to use for email destination is: 'EmailDestination'
 To field is a select field with ID 'Recipient'
 
 Procedure:
 Check the Url's hash value e.g. http://hostname/contact_us/#admissions
 and see if it has a value in it that should trigger the drop down to change and
 email address be inserted.

 */
 
// define the IDs of the controls to manipulate
var emailFieldID = '#EmailDestination';
var dropdownFieldID = '#Recipient';

/*********************************************************************************/
// an array of arrays containing mappings to grouped items
// so that they can easily be associated with one another
//
// [ link id (hashmark value) , dropdown value , email address ]
/*
// ***
// *** IE 8 and 7 do not like the use of square bracket notation for defining arrays in this manner ***
// ***
var mappings = [
['admissions', 'Admission', 'admissions@cia.edu'],
['alumni', 'Alumni', 'alumni@cia.edu'],
['finaid', 'Financial Aid', 'finaid@cia.edu'],
['careers', 'Career Center', 'careers@cia.edu'],
['cinema', 'Cinematheque', 'cinema@cia.edu'],
['ce', 'Continuing Education', 'ce@cia.edu'],
['hr', 'Human Resources', 'hr@cia.edu'],
['library', 'Library', 'referencehelp@cia.edu'],
['registrar', 'Registrar', 'registrar@cia.edu'],
['reinbergergallery', 'Reinberger Galleries', 'reinbergergallery@cia.edu'],
['security', 'Security', 'hweiner@cia.edu'],
['studentaffairs', 'Student Affairs', 'studentlife@cia.edu'],
['support', 'Tech Support Center', 'support@cia.edu'],
['visitingartists', 'Visiting Artists', 'lcooper@cia.edu'],
['webmaster', 'Webmaster', 'web@cia.edu'],
];
*/
var mappings = new Array(
    new Array('admissions', 'Admission', 'admissions@cia.edu'),
    new Array('alumni', 'Alumni', 'alumni@cia.edu'),
    new Array('finaid', 'Financial Aid', 'finaid@cia.edu'),
    new Array('careers', 'Career Center', 'careers@cia.edu'),
    new Array('cinema', 'Cinematheque', 'cinema@cia.edu'),
    new Array('ce', 'Continuing Education', 'ce@cia.edu'),
    new Array('hr', 'Human Resources', 'hr@cia.edu'),
    new Array('library', 'Library', 'referencehelp@cia.edu'),
    new Array('registrar', 'Registrar', 'registrar@cia.edu'),
    new Array('reinbergergallery', 'Reinberger Galleries', 'reinbergergallery@cia.edu'),
    new Array('security', 'Security', 'hweiner@cia.edu'),
    new Array('studentaffairs', 'Student Affairs', 'studentlife@cia.edu'),
    new Array('support', 'Tech Support Center', 'support@cia.edu'),
    new Array('visitingartists', 'Visiting Artists', 'lcooper@cia.edu'),
    new Array('webmaster', 'Webmaster', 'web@cia.edu')
);

// a generic function to retrieve mapping values
function GetMappingItemValue(keyOrdinal, keyValue, valueOrdinal) {
    var rslt = '';
    for (var i=0; i < mappings.length;  i++) {
        if (mappings[i][keyOrdinal] == keyValue) {
            rslt = mappings[i][valueOrdinal];
        }
    }
    return rslt;
}

// utility functions built off of the generic one to provide better usability
function GetEmailFromLinkID(linkid) {
    return GetMappingItemValue(0, linkid, 2);
}
function GetDropDownValueFromLinkID(linkid) {
    return GetMappingItemValue(0, linkid, 1);
}
function GetEmailFromDropDownValue(dropdownvalue) {
    return GetMappingItemValue(1, dropdownvalue, 2);
}
function GetLinkIDFromDropDownValue(dropdownvalue) {
    return GetMappingItemValue(1, dropdownvalue, 0);
}
/*********************************************************************************/


function setDestinationEmail(email) {
    $(emailFieldID).attr('value', email);
}

function setDropDown(choice) {
    $(dropdownFieldID + ' > option').each(function() {
        $(this).removeAttr('selected');
        if ($(this).attr('value') == choice) {
            $(this).attr('selected', 'selected');
        }
    });
}

function makeDropDownReadOnly() {
    $(dropdownFieldID).attr('disabled', 'disabled');
}

// utility to convert a string to a number if possible
//function $n(n) { return (typeof (n) == 'number' || typeof (n) == 'string') ? new Number(n) : NaN; }

// retrieve the value of after the # mark in the URL
function GetLinkID() {
    var url = window.location.href;
    var temp = url.split('#');
    if (temp.length > 1) {
        return temp[1];
    }
    else {
        return '';
    }
}

$(document).ready(function() {
    // Wire up an event for when the dropdown choice changes. 
    // When it does, update the email address field.
    $(dropdownFieldID).change(function() {
        var current_value = $(dropdownFieldID).val();
        $(emailFieldID).attr('value', GetEmailFromDropDownValue(current_value));
    });

    // get the link identifier off of the URL if it's there
    var linkID = GetLinkID();

    if (linkID.length > 0) {
        var email = GetEmailFromLinkID(linkID);
        var dropdownSelection = GetDropDownValueFromLinkID(linkID);

        // if we have a valid email and dropdown selection based on the link passed in,
        // then set the appropriate controls.
        if (email.length > 0 && dropdownSelection.length > 0) {
            setDestinationEmail(email);
            setDropDown(dropdownSelection);
        }
    }
});
