﻿// JavaScript File

// WebEnrollment.js
// ========================================
// This file contains the client-side script
// library used in the Real Estate Institute
// WebEnrollment project.


// infowindow is used to hold a reference to the "More Info" popup window
// on the ChoosePrograms usercontrol
var infowindow;

// ssnwindow is used to hold a reference to the "Why We Need Your SSN" popup window
// on the AdditionalInfo usercontrol
// var ssnwindow;

// cvvwindow is used to hold a reference to the "What is a CVV" popup window
// on the PaymentInfo usercontrol
// var cvvwindow;

function displayMoreInfoWindow(windowId, contentSource)
{
    infowindow = dhtmlmodal.open(windowId, 'div', contentSource, 'Program Description', 'width=500px,height=310px,center=1,resize=0,scrolling=1'); 
    
    //return false;
}


function displayModalDialogWithCustomSize(windowId, elementId, windowTitle, width, height)
{
    var element = document.getElementById(elementId);
    element.style.display = "";
    infowindow = dhtmlmodal.open(windowId, 'div', elementId, windowTitle, 'width='+width+',height='+height+',center=1,resize=0,scrolling=1');
}

function displayModalDialogWithCustomHeight(windowId, elementId, windowTitle, height)
{
    displayModalDialogWithCustomSize(windowId, elementId, windowTitle, '500px', height)
}

function displayModalDialog(windowId, elementId, windowTitle)
{
    displayModalDialogWithCustomHeight(windowId, elementId, windowTitle, '300px')
}

function displayModalMessageBoxFromDiv(windowId, contentSource, windowCaption)
{
    msgwindow = dhtmlmodal.open(windowId, 'div', contentSource, windowCaption, 'width=450px,height=250px,center=1,resize=0,scrolling=1'); 
}

function displayModalMessageBoxFromDiv(windowId, contentSource, windowCaption, windowHeight)
{
    msgwindow = dhtmlmodal.open(windowId, 'div', contentSource, windowCaption, 'width=450px,height=' + windowHeight + 'px,center=1,resize=0,scrolling=1'); 
}

function forceUpperCase(txtBox)
{
    txtBox.value = txtBox.value.toUpperCase();
}

// UnselectAllProgramTypesExcept unselects all program type radio buttons
// on the ProgramType usercontrol except the for one just clicked.
function UnselectAllProgramTypesExcept(selectedButton)
{

  if (ptRadiobuttonList != null)
  {
  
     for (var i = 0; i < ptRadiobuttonList.length; i++)
     {
        var rdoBtn = document.getElementById(ptRadiobuttonList[i]);
        
        if (rdoBtn != selectedButton)
        {
            rdoBtn.checked = false;
        }
     }
  }  
}


// CUSTOM VALIDATOR FUNCTIONS -----------------------------------------------------

// Validation for CUSTV_CardExpirationYear on the PaymentInfo usercontrol
// ----------------------------------------------------------------------
function validation_CUSTV_CardExpirationYear(sender, args)
{
    // CardExpirationMonthControlName stores the qualified client name (ClientID) of the 
    // CardExpirationMonth control and is used by validation_CUSTV_CardExpirationYear to 
    // get the value of that control. This value is inserted via the ClientScript manager
    // in the PaymentInfo usercontrol.
    var monthCtrl = document.getElementById(CardExpirationMonthControlName);
    
    var d = new Date();
    var curYear = d.getFullYear();
    var selYear = parseInt(args.Value);
    var selMonth = parseInt(monthCtrl.value);
    
    if (selMonth > 0)
    {
        if (curYear == selYear)
        {
            var curMonth = d.getMonth() + 1;
            
            if (selMonth < curMonth)
            {
                args.IsValid = false;
            }
        }
    }
}

// Validation for CUSTV_ProgramType on the ProgramTypes usercontrol
// ----------------------------------------------------------------------
function validation_CUSTV_ProgramType(sender, args)
{
    if (ptRadiobuttonList != null)
    {

        for (var i = 0; i < ptRadiobuttonList.length; i++)
        {
            var rdoBtn = document.getElementById(ptRadiobuttonList[i]);
            
            if (rdoBtn.checked)
            {
                args.IsValid = true;
                return true;
            }
        }
    
        args.IsValid = false;
        return false;
    }  
}

// END CUSTOM VALIDATOR FUNCTIONS -------------------------------------------------


// AUTO-TAB FUNCTIONALITY ---------------------------------------------------------

function autoTab(input, len, e) 
{
    var bIsNN = isNN();
    var keyCode = (bIsNN) ? e.which : e.keyCode;
    var filter = (bIsNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

    if (input.value.length >= len && !containsElement(filter,keyCode)) 
    {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }
}

function isNN()
{
    return (navigator.appName.indexOf("Netscape")!= -1);
}

function containsElement(arr, ele) 
{
    var found = false;
    var index = 0;
    
    while(!found && (index < arr.length))
    {
        if(arr[index] == ele)
        {
            found = true;
        }
        else
        {
            index++;
        }
    }
    
    return found;
}

function getIndex(input) 
{
    var index = -1, i = 0, found = false;
    
    while (i < input.form.length && index == -1)
    {
        if (input.form[i] == input)
        {
            index = i;
        }
        else 
        {
            i++;
        }
    }
    
    return index;
}

// END AUTO-TAB FUNCTIONALITY ---------------------------------------------------------

