﻿/* --------------------------------------------------------------------------------
 Filename: requestInfo.js                                                   

 Description:

    Javascript used by the requestInfo.cfm page.
 
 History:                                                                        
   Ver         Inits   Date        Comments                                        
   1.00.00     jwl     03/30/2010  Created

-------------------------------------------------------------------------------- */

// Globals:

var g_utility        = new Utility();           // Create a utility object so we can use it's functionality.  
var g_validationInfo = new ValidationInfo([
{"szName": "szFirstName",       "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a first name."},
{"szName": "szLastName",        "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a last name."},
{"szName": "szTitle",           "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a job title."},
// {"szName": "szSchoolDistrict",  "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a district name."},
// {"szName": "szSchoolName",      "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a school name."},
{"szName": "szPhone",           "szType": "input",  "szValidationType": "phone",  "szMessageLevel2": "Invalid format - Use: (999)999-9999"},
{"szName": "szPhone",           "szType": "input",  "szValidationType": "exists", "szMessageLevel1": "(999)999-9999",  "szMessageLevel2": "Phone phone format: (999)999-9999"},
{"szName": "szEmail",           "szType": "input",  "szValidationType": "email",  "szMessageLevel2": "Invalid e-mail format"},
{"szName": "szEmail",           "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply an e-mail address."},
// {"szName": "szDemoUsername",    "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a username."},
// {"szName": "szDemoPassword",    "szType": "input",  "szValidationType": "exists", "szMessageLevel2": "Please supply a password."}
],
"validateMessageHidden",
"validateMessageLevel1",
"validateMessageLevel2"
); // Create an instance of the ValidationInfo class.
var g_pForm = null;     // We'll use this to hang on to a reference to the form.


// On this page we don't want to automatically navigate to the left-hand menu links.
// So we change them to a function that will test whether anything has been changed in the form
// and then either put up an error message, or go to the link. 
//
$(document).ready(function() {

    g_pForm = document.getElementById('demoRequestForm') || null;

    g_validationInfo.Initialize(document, document.forms[0]);      
    
    var szRegExpression = /noValidation/gi;       // Don't change any elements with a class of 'noValidation'.
    
    // Add onchange methods to form elements so we'll know if anything changes.
    if(document.forms[0])
    {    
        for(var j=0; j<document.forms[0].elements.length; j++) {        
            // We don't validate if the element has a class of 'noValidation'.  This will work even if an element has more than one class.
            if(!document.forms[0].elements[j].className || (document.forms[0].elements[j].className && document.forms[0].elements[j].className.search(szRegExpression) < 0))                
                document.forms[0].elements[j].onchange = handleFormOnChange;
        }            
    }                            
    loadDistrictSelectBox();        // Load the district select box based on the default selection in the state select box.        
    g_validationInfo.Validate();                        
    hideEmptyMessageBox();          // If the message box is empty we will hide it.        
    
});

        
// handleFormOnChange:
//
// We come here anytime one of the fields on the form has changed.
//
function handleFormOnChange()
{              
    g_validationInfo.Validate();

} // handleFormOnChange


// handleStateSelectboxOnchange:
//
// We come here when the School State dropdown is changed.
// We get the state from the state dropdown, and do an AJAX call to get information
// about all the districts in that state, and use it to load the district dropdown.
//
function handleStateSelectboxOnchange() {

    loadDistrictSelectBox();     // This will load the districts into the dropdown based on the selected state.

} // handleStateSelectboxOnchange


// handleDistrictSelectboxOnchange:
//
// We come here when the District dropdown is changed.
//
function handleDistrictSelectboxOnchange() {

    loadSchoolSelectBox();  
    
} // handleDistrictSelectboxOnchange


// handleSchoolTypeRadioButtonOnclick:
//
// We come here when the school type radio button is changed.
// This will load the districts into the dropdown based on the selected state, if we've selected "public", and
// disable the district dropdown if we've selected "private".
//
function handleSchoolTypeRadioButtonOnclick() {

    loadDistrictSelectBox();    
                    
} // handleSchoolTypeRadioButtonOnclick


// hideBasedOnRadioButton:
//
// This hides or shows a section of HTML based on the value of a radio button.
//
function hideBasedOnRadioButton(szRadioButtonId, szClassName) {

   if(!g_pForm || !g_pForm.elements || g_pForm.elements[szRadioButtonId].length < 2)  // We need a form with two radio buttons.
        return;

    if (g_pForm.elements[szRadioButtonId][0].checked)
        $("." + szClassName).show();
    else
        $("." + szClassName).hide();

} // hideBasedOnRadioButton


// handleSubmitButton:
//
// Come in here when the submit button is clicked.
//
function handleSubmitButton()
{
    g_validationInfo.SetAllWarningsToThisLevel(2);
               
    if(g_validationInfo.Validate()) {       // This validates the form.
        clearErrorMessageOnPage();
        if(g_pForm)
            g_pForm.submit();
    }            
    else        
    {
        writeMessageToPage("Please correct the errors listed above and resubmit the form.");
        return false;
    }                
    return true;    

} // handleSubmitButton


// handleDirectorOfITradioButtons:
//
// This handles when someone clicks on the Yes/No radio button for the question...
// Do you have a Director of Instructional Technology in the District?
//
function handleDirectorOfITradioButtons() {

    hideBasedOnRadioButton('bDirectorOfTechnology', 'ITdirectorInfo')                        

} // handleDirectorOfITradioButtons


// handleOutsourceWebServicesRadioButtons
//
// This handles when someone clicks on the Yes/No radio button for the question...
// Does your district outsource web services?
//
function handleOutsourceWebServicesRadioButtons() {
       
    hideBasedOnRadioButton('bOutsourceWebServices', 'interestedInHostingInfo')                
                
} // handleOutsourceWebServicesRadioButtons


// loadDistrictSelectBox:
//
// If we're looking for a public school we do an AJAX call to load the district select box
// based on the state selected in the state dropdown.
// If we're looking for a private school we hide the district dropdown box.
//
function loadDistrictSelectBox() {

    var pStateSelectBox = document.getElementById('schoolStateDropdown') || null;
    if(!pStateSelectBox || !pStateSelectBox.options || !pStateSelectBox.options.length || pStateSelectBox.options.length < 1)
        return;
                   
    var pSchoolTypePublicRadioButton = document.getElementById('schoolTypePublic') || null;
    if(!pSchoolTypePublicRadioButton)
        return;
                
    if(pSchoolTypePublicRadioButton.checked) {   
        displayLoadingImage('schoolDistrictMessage', true);                             
        enableDistrictDropdown(true);               // Make sure the district dropdown isn't disabled.                                
        $.ajax({                                    // AJAX in the districts for the chosen state.
            type: 'POST',  
            url: "./ajax/ajaxGetDistricts.cfm",          
            dataType: 'json',
            data:
            {
                'bSchoolTypePublic': true,
                'szStateAbbr': pStateSelectBox.options[pStateSelectBox.selectedIndex].value
            },                                          
            error:   handleLoadDistrictSelectBoxFailure,           
            success: handleLoadDistrictSelectBoxSuccess
        });   
    }  
    else {    
        enableDistrictDropdown(false);              // We don't choose a district when looking for private schools.  
        loadSchoolSelectBox();                      // If private is selected, we'll load the schools for the state.           
    }        

} // loadDistrictSelectBox


// handleLoadDistrictSelectBoxFailure:
//
// We come here if we've been unsuccessful in retrieving districts from the server for the dropdown.
//
function handleLoadDistrictSelectBoxFailure() {

    displayLoadingImage('schoolDistrictMessage', false);                
    alert('There was a problem getting information about the districts from the server.');

} // handleLoadDistrictSelectBoxFailure     


// handleLoadDistrictSelectBoxSuccess:
//
// We come here if we've successfully retrieved districts from the server for the dropdown.
//
function handleLoadDistrictSelectBoxSuccess(pData) {
    displayLoadingImage('schoolDistrictMessage', false);                

    var pDistrictDropDown = document.getElementById('schoolDistrictDropdown') || null;    
    if(!pData || !pDistrictDropDown || !pDistrictDropDown.options)
        return;            
        
    pDistrictDropDown.options.length = 0;   // Get rid of any districts in the array, we're loading new ones.    
    var pOption = null;        
    for(var i=0; i<pData.districtInfo.length; i++) {
        pOption = new Option(unescape(pData.districtInfo[i].name), pData.districtInfo[i].id, false, false);
        if(pOption)
            pDistrictDropDown.options[pDistrictDropDown.options.length] = pOption;                           
    }    
   loadSchoolSelectBox();                                

} // handleLoadDistrictSelectBoxSuccess     


// loadSchoolSelectBox:
//
// If we're looking for a public school we do an AJAX call to load the school select box
// based on the district selected in the district dropdown.
// If we're looking for a private school we do an AJAX call to load the school select box
// based on the state selected in the state dropdown.
//
function loadSchoolSelectBox() {

    var pStateSelectBox    = document.getElementById('schoolStateDropdown') || null;
    if(!pStateSelectBox || !pStateSelectBox.options || !pStateSelectBox.options.length || pStateSelectBox.options.length < 1)
        return;
            
    var pDistrictSelectBox = document.getElementById('schoolDistrictDropdown') || null;    
    if(!pDistrictSelectBox || !pDistrictSelectBox.options || !pDistrictSelectBox.options.length || pDistrictSelectBox.options.length < 1)
        return;            
                   
    var pSchoolTypePublicRadioButton = document.getElementById('schoolTypePublic') || null;
    if(!pSchoolTypePublicRadioButton)
        return;
              
    displayLoadingImage('schoolNameMessage', true);                                                     
    $.ajax({                                        // AJAX in the schools for the chosen district (public) or state (private).
        type: 'POST',  
        url: "./ajax/ajaxGetSchools.cfm",          
        dataType: 'json',
        data:
        {
            'bSchoolTypePublic': pSchoolTypePublicRadioButton.checked,
            'szStateAbbr' : pStateSelectBox.options[pStateSelectBox.selectedIndex].value,
            'szDistrictId': pDistrictSelectBox.options[pDistrictSelectBox.selectedIndex].value
        },                                          
        error:   handleLoadSchoolSelectBoxFailure,           
        success: handleLoadSchoolSelectBoxSuccess
    });           

} // loadSchoolSelectBox


// handleLoadSchoolSelectBoxFailure:
//
// We come here if we've been unsuccessful in retrieving districts from the server for the dropdown.
//
function handleLoadSchoolSelectBoxFailure() {

    displayLoadingImage('schoolNameMessage', false);                
    alert('There was a problem getting information about the schools from the server.');

} // handleLoadSchoolSelectBoxFailure     


// handleLoadSchoolSelectBoxSuccess:
//
// We come here if we've successfully retrieved schools from the server for the dropdown.
//
function handleLoadSchoolSelectBoxSuccess(pData) {
    displayLoadingImage('schoolNameMessage', false);                

    var pSchoolDropDown = document.getElementById('schoolNameDropdown') || null;    
    if(!pData || !pSchoolDropDown || !pSchoolDropDown.options)
        return;            
                
    pSchoolDropDown.options.length = 0;   // Get rid of any schools in the array, we're loading new ones.    
    var pOption = null;        
    for(var i=0; i<pData.schoolInfo.length; i++) {
        pOption = new Option(unescape(pData.schoolInfo[i].name), pData.schoolInfo[i].id, false, false);
        if(pOption)
            pSchoolDropDown.options[pSchoolDropDown.options.length] = pOption;                           
    }   
    displayLoadingImage('schoolNameMessage', false);                                                                                        

} // handleLoadSchoolSelectBoxSuccess     


// enableDistrictDropdown:
//
// Hide the district dropdown box.
//
function enableDistrictDropdown(bEnable) {

    var pDistrictDropDown = document.getElementById('schoolDistrictDropdown') || null;    
    if(!pDistrictDropDown || !pDistrictDropDown.options)
        return;            
        
    pDistrictDropDown.options.length = 0;   // Get rid of any districts in the array.  
    if(bEnable)
        pDistrictDropDown.disabled = false;      
    else {              
        pOption = new Option("Not applicable", 0, false, false); 
        if(pOption)
            pDistrictDropDown.options[pDistrictDropDown.options.length] = pOption; 
        pDistrictDropDown.disabled = true;                                                                
    }        
             
} // enableDistrictDropdown


// displayLoadingImage:
//
// This adds or removes the loading image that we display when we AJAX information in,
// based on the value of bDisplay.
//
function displayLoadingImage(szSpanId, bDisplay) {

    var pSpanId = document.getElementById(szSpanId) || null;
    if(!pSpanId)
        return;
        
    if(bDisplay)
        pSpanId.innerHTML = '<img src="../../images/loading.gif">'; 
    else                
        pSpanId.innerHTML = '&nbsp;';   
             
} // displayLoadingImage




    


