// JavaScript Document for TRC Questions Pages

var focused=false;
if (document.captureEvents) document.captureEvents(Event.KEYPRESS);
document.onkeypress=function(e) {
if (!focused) return true;
if (!e && event) e={which:event.keyCode};
if (e.which < 48 || e.which >57) return false;
}

// string prototypes for trim funciton
String.prototype.lTrim = function () {
    var whitespace = new RegExp("^\\s+","gm");
    return this.replace(whitespace, "");
}

String.prototype.rTrim = function () {
    var whitespace = new RegExp("\\s+$","gm");
    return this.replace(whitespace, "");
}

String.prototype.trim = function () {
    return this.rTrim().lTrim();
}

function check_fields()
{
        
        //verify if all the required fields were selected
        //create variables for verification of selection and assign default value
        var blQuestionAnswered = "N";
        //check for question about available transportation - Field commented out
        /*for (var i=0; i< document.trc_incenter_frm.transport.length; i++)
        {
                if(document.trc_incenter_frm.transport[i].checked)
                {
                        blQuestionAnswered = "Y";
                        break;
                }
        }//end for loop 
        
        if (blQuestionAnswered == "N")
        {
                //alert("Please answer question #1 before submitting the form. \n Note that all the questions marked with a * are required and must be answered before continuing with the application process.")       ;
                displayAlert("Please answer question #1 before submitting the form.");
                return false;
        }*/
        
        //reset variable 
        blQuestionAnswered = "N";
        
        //check for question about PC type
        for (var i=1; i< document.trc_incenter_frm.center_location.length; i++)
        {
                if(document.trc_incenter_frm.center_location.options[i].selected)
                {
                        blQuestionAnswered = "Y";
                        break;
                }
        }//end for loop 
        
        if (blQuestionAnswered == "N")
        {
                displayAlert("Please answer question #1 before submitting the form.");
                return false;
        }
        
        //reset variable 
        blQuestionAnswered = "N";
        
        //check for question about age of 18 or older
        for (var i=0; i< document.trc_incenter_frm.age_18.length; i++)
        {
                if(document.trc_incenter_frm.age_18[i].checked)
                {
                        blQuestionAnswered = "Y";
                        break;
                }
        }//end for loop 
        
        if (blQuestionAnswered == "N")
        {
                displayAlert("Please answer question #2 before submitting the form.");
                return false;
        }
        
        var intZipCode = document.trc_incenter_frm.zip_code.value
        intZipCode = intZipCode.trim()
        if(intZipCode.length > 0)
        {
                if(intZipCode.length != 5)
                {
                        alert("Please enter 5 numbers for the zip code.")
                        document.trc_incenter_frm.zip_code.focus();
                        document.trc_incenter_frm.zip_code.select();
                        return false
                }
                if(intZipCode != parseInt(intZipCode,10))
                {
                        alert ("Please enter only numbers for the Zip Code.");
                        document.trc_incenter_frm.zip_code.focus();
                        document.trc_incenter_frm.zip_code.select();
                        return false;
                }
        }
        else
        {
                displayAlert("Please enter your zip code before submitting the form.");
                return false;   
                
        }
        
        //reset variable 
        blQuestionAnswered = "N";
        
        //check for question about how they learned about the ad
        for (var i=0; i< document.trc_incenter_frm.source_type.length; i++)
        {
                if(document.trc_incenter_frm.source_type[i].checked)
                {
                        //check to see if the selection is web ad
                        if (document.trc_incenter_frm.source_type[i].value == "wa")
                        {
                                if (document.trc_incenter_frm.web_ad[document.trc_incenter_frm.web_ad.selectedIndex].value == 0)
                                {
                                        displayAlert("Please select which Web ad you saw the job posting for this position before continuing.");
                                        return false;
                                }
                        }
                        //check to see if the selection is newspaper ad
                        if (document.trc_incenter_frm.source_type[i].value == "np")
                        {
                                if (document.trc_incenter_frm.newspaper_ad[document.trc_incenter_frm.newspaper_ad.selectedIndex].value == 0)
                                {
                                        displayAlert("Please select which Newspaper ad you saw the job posting for this position before continuing.");
                                        return false;
                                }
                        }
                        //check to see if the selection is other
                        if (document.trc_incenter_frm.source_type[i].value == "ot")
                        {
                                var strOtherSourceType = document.trc_incenter_frm.source_type_specify.value;
                                strOtherSourceType = strOtherSourceType.trim();
                                if(strOtherSourceType.length == 0)
                                {
                                        displayAlert("Please specify the other source where you saw the job posting for this position before continuing.");
                                        return false;
                                }
                        }
                        blQuestionAnswered = "Y";
                        break;
                }
        }//end for loop 
        
        if (blQuestionAnswered == "N")
        {
                displayAlert("Please answer question #4 before submitting the form.");
                return false;
        }
        
        
        return true;
        
} //end of check_fields function

function displayAlert(strMSG)
{
        
        alert(strMSG + "\n Note that all the questions marked with a * are required and must be answered before continuing with the application process.")
}

function clearWindowsVersion()
{
        //if type of computer is a MAC, it makes sure that all Windows versions are cleared, if any was selected.
        if(document.trc_incenter_frm.pc_type[1].value == 'mac')
        {
                for (var i=0; i< document.trc_incenter_frm.pc_version.length; i++)
                {
                        document.trc_incenter_frm.pc_version[i].checked = false;
                                
                }
        }
        

}//end funciton clearWindowsVersion

function clearInternetType()
{
        //if the user does not have Internet access from home, it makes sure that all type of Internet access answered are cleared, if any was previously selected.     
        if(document.trc_incenter_frm.internet_home[1].value == 'no')
        {
                for (var i=0; i< document.trc_incenter_frm.internet_type.length; i++)
                {
                        document.trc_incenter_frm.internet_type[i].checked = false;
                        
                }//end for loop 
        }
} // end function clearInternetType

function setInternetAccess()
{
        //set Internet Access to Yes if the use selects on any type of Internet Access
        document.trc_incenter_frm.internet_home[0].checked = true;
}

//enable the newspaper ad list and disable the web ad list, if enabled
function enableNewspaperList()
{
        document.trc_incenter_frm.newspaper_ad.disabled = false;
        document.trc_incenter_frm.newspaper_ad.options[0].selected;
        document.trc_incenter_frm.web_ad.selectedIndex = 0;
        document.trc_incenter_frm.web_ad.disabled = true;
        disable_other_source_type();
        
}

// enable the web ad list and disable the newspaper ad list, if enabled
function enableWebList()
{
        document.trc_incenter_frm.web_ad.disabled = false;
        document.trc_incenter_frm.web_ad.options[0].selected;
        document.trc_incenter_frm.newspaper_ad.disabled = true;
        document.trc_incenter_frm.newspaper_ad.selectedIndex = 0;
        disable_other_source_type();
        
}

// function to disable the web and newspaper ad lists when clicking on the other options for the source type
function disableAdLists()
{
        document.trc_incenter_frm.newspaper_ad.disabled = true;
        document.trc_incenter_frm.web_ad.disabled = true;
        document.trc_incenter_frm.web_ad.selectedIndex = 0;
        document.trc_incenter_frm.newspaper_ad.selectedIndex = 0;
        //disable the other specify field if other is not selected
        if(document.trc_incenter_frm.source_type[4].checked == false)
        {
                disable_other_source_type();
        }
}

function enable_other_source_type()
{
        document.trc_incenter_frm.source_type_specify.disabled = false; 
        document.trc_incenter_frm.source_type_specify.focus();
}

function disable_other_source_type()
{
        document.trc_incenter_frm.source_type_specify.value= "";
        document.trc_incenter_frm.source_type_specify.disabled = true;
        
}