﻿

// the following control variables on set on RunOnLoad()

// holds the function to call on an ajax end request
var callBackFunction_OnSchoolsLoaded;

// the schools dropdown control
var dlistSchools;

// the states dropdown control
var dlistStates;

// hidden span that stores the user's selected value of states dropdown control
var hidSpanSelectedStateVal;

// hidden span that stores the uers's selected value of schools dropdown control
var hidSpanSelectedSchoolId;

// the category dropdown control
var catDropdown;

// stores value of choosen category
var hidCatId;

// the sub-category dropdown control
var subCatDropdown;

// stores the value of the choosen sub-category
var hidSubCatId;

// stores the index of the choosen sub-category
var hidSubCatIndex;

// postback defined by .net
var __oldPostBack;    


// keeps track of whether the page is currently in the process of getting getting the next or previous page in the grid
var bGettingNextOrPrevPage;

// keeps track of the number of times next or prev have been clicked before the first request for next/prev has been fulfilled
var iNumTimesPrevOrNextClick;    

// keeps track of whether the page is currently in the processing of getting search results 
var bGettingSearchResults;

// keeps track of the number of times search has been clicked before the first search request has been fulfilled
var iNumTimesSearchClick;


//save the newly selected value in a hidden field that can then be used by the page the form is submitted to - to extract the value from the hidden field
function SaveCategorySelection()
{    
    if(catDropdown.options.length > 0)
    {
        hidCatId.value = catDropdown.options[catDropdown.selectedIndex].value;
    }
}


// saves the newly selected value and index of value in hidden fields that can then be used by the page the form is submitted to
function SaveSubCategorySelection()
{            
    if(subCatDropdown.options.length > 0)
    {
        hidSubCatId.value = subCatDropdown.options[subCatDropdown.selectedIndex].value;
        hidSubCatIndex.value = subCatDropdown.selectedIndex;
    }   
}


// called by the sub-category control, determines which sub-cat dlist to show/hide
function ShowHideSubCatLbl(bShow)
{
    if(bShow)
    {
        document.getElementById("dlistDisabledSubCat").style.display = "none";
        document.getElementById(subCatDListId).style.display = "inline";
        
        document.getElementById("hidDisabledSubCatShowing").value = "false";
    }
    else
    {
       document.getElementById("dlistDisabledSubCat").style.display = "inline";
       document.getElementById("create_hidSubCatId").value = 0;
       document.getElementById("create_hidSubCatSelectedIndex").value = 0;
       
       document.getElementById("hidDisabledSubCatShowing").value = "true";
    }
}


// called on an ajax end request event
function OnEndRequest(sender, args)
{
    if(callBackFunction_OnXmlEndRequest != null)
    {
        callBackFunction_OnXmlEndRequest();
    }
}


function RunOnLoad() 
{               
    callBackFunction_OnSchoolsLoaded = null;
    
    // the label that displays the state currently being viewed
    viewStateLbl = document.getElementById("lblViewingState");
    __oldPostBack = __doPostBack;
    __doPostBack = __newPostBack;
        
         
    // displays the paging controls which are hidden until the page loads - this is to avoid an error that occurs when paging is used before the entire page is loaded
    try
    {
        // this function won't even exist when the gridview is hidden, which can happen when the gridview is hidden when either no search results are found or category/school combo has no items to display
        CallOnPageLoadForPostingsGrid();
    }
    catch(exc){}
                    
    // initialize vars pointing to controls on page
    catDropdown = document.getElementById(CategoryControlId);
    hidCatId = document.getElementById("create_hidCatId");
    subCatDropdown = document.getElementById(subCatVar);
    hidSubCatId = document.getElementById("create_hidSubCatId");
    hidSubCatIndex = document.getElementById("create_hidSubCatSelectedIndex");
    
    dlistStates = document.getElementById("dlistStates");
    dlistSchools = document.getElementById("dlistSchools");
    hidSpanSelectedStateVal = document.getElementById("spanHidSelectedStateVal");
    hidSpanSelectedSchoolId = document.getElementById("spanSelectedSchoolId");
    
    CreateSubCatArray();
    
    if(document.getElementById("create_hidSubCatSelectedIndex").value > 0)
    {
        document.getElementById(subCatDListId).selectedIndex = document.getElementById("create_hidSubCatSelectedIndex").value;
    }
    
    // these are used for setting the correct selected values of the dropdown lists on clicking back button
    //document.getElementById("dlistSchools").selectedIndex = document.getElementById("hidSchoolSelectedIndex").value;
    document.getElementById(CategoryControlId).selectedIndex = document.getElementById("hidCatIdSelectedIndex").value;
    document.getElementById("create_hidCatId").value = document.getElementById(CategoryControlId).options[document.getElementById(CategoryControlId).selectedIndex].value;
    CategorySelectionChanged(false);
    
    if(document.getElementById("hidDisabledSubCatShowing").value == "false")
    {
        document.getElementById(subCatDListId).selectedIndex = document.getElementById("hidSubCatSelectedIndex").value;
        document.getElementById("create_hidSubCatId").value = document.getElementById(subCatDListId).options[document.getElementById(subCatDListId).selectedIndex].value;
    }
    
    SaveSubCategorySelection();   
    
    bGettingNextOrPrevPage = false;    
    iNumTimesPrevOrNextClick = 0;
    
    document.getElementById(searchSpanVar).style.display = "inline";
    
    document.getElementById("imgRetrievingSchools").style.display = "none";
    LoadStatesAndSchoolsControls();
    
    
    // set the selected state index, download schools if state index is not 0, and select the previously selected school
    
    InitBottomBanner();
    
    InitLeftLongBanner();
}


function LoadStatesAndSchoolsControls() 
{
    for (i = 0; i < dlistStates.options.length; i++) 
    {
        if (dlistStates.options[i].value == hidSpanSelectedStateVal.innerHTML) 
        {
            dlistStates.selectedIndex = i;
            break;   
        }
    }
    
    
    
    // if a state is selected, load its schools
    if (dlistStates.selectedIndex > "0") 
    {
        document.getElementById("imgRetrievingSchools").style.display = "inline";
        callBackFunction_OnSchoolsLoaded = SelectSelectedSchool;
        RetrieveSchoolsForState(dlistStates.options[dlistStates.selectedIndex].value);
    }
    else 
    {
        var defaultOption = document.createElement("OPTION");
        defaultOption.text = "Select Your State First, Then Choose Campus Here";
        defaultOption.value = "0";
        dlistSchools.options[0] = defaultOption;
    }
        
}


function SelectSelectedSchool()
{
    callBackFunction_OnXmlEndRequest = null;
    // if a school was previously selected
    if (hidSpanSelectedSchoolId.innerHTML.value != 0)
    {
        for(i=0; i<dlistSchools.options.length; i++)
        {
            if (dlistSchools.options[i].value == hidSpanSelectedSchoolId.innerHTML)
            {
                dlistSchools.selectedIndex = i;
                break;
            }
        }
    }
}


// handles Postback requests via javascript
function __newPostBack(eventTarget, eventArg)
{
    var bSubmit = true;
    
    // if submitting zipcode
    if(eventTarget == "cmdZipcodeLookup")
    {                
        if(!ValidateZipcode(zipCodeInput.value))
        {
            alert("Please enter a valid zipcode");
            bSubmit = false;
        }
    }
    
    // search click
    else if(eventTarget == cmdSearchUniqueId)
    {
        if(trim(document.getElementById(txtSearchId).value).length == 0)
        {
            alert("Please enter search text");
            bSubmit = false;
        }
        else
        {
            if(bGettingSearchResults  &&  ++iNumTimesSearchClick < 15)
            {
                bSubmit = false;
            }
            else
            {
                iNumTimesSearchClick = 1;
                bGettingSearchResults = true;
            }
        }
    }
    
    // when the next or the previous button is clicked from the paging controls of the PostingsGrid, then it is only submitted on the first click, succeeding clicks are ignored up to the next
    // 15 times; this is b/c rapidly clicking on a slow connection before the page can postback and load the request can overload the server create errors
    else if (eventTarget == postingGridBottomNextVar  ||  eventTarget == postingGridBottomPrevVar)
    {
        if(bGettingNextOrPrevPage)
        {
            if(++iNumTimesPrevOrNextClick < 15)
            {
                bSubmit = false;
            }
        }
        else
        {
            iNumTimesPrevOrNextClick = 1;
            bGettingNextOrPrevPage = true;
        }
    }
        
    
    if(bSubmit) __oldPostBack(eventTarget, eventArg);
}




function InitBottomBanner()
{
    bbAdHrefCntrl = document.getElementById("bottomBannerAdHref");
    bbAdImgCntrl = document.getElementById("bottomBannerAdImg");

    // indexBBA declared near the function that rotates this specific ad - initialized to a random number between 0 and last index of bBAH inclusive
    bbAdHrefCntrl.href = bBAH[indexBBA];
    bbAdImgCntrl.src = bbAISA[indexBBA];
    bbAdImgCntrl.alt = bbAIAA[indexBBA];
                
                
    // rotate ads 
    setTimeout("Rotate()", 150000);   
}


function InitLeftLongBanner()
{
    leftLongAdHrefCntrl = document.getElementById("leftLongBannerAdHref");
    leftLongAdImgCntrl = document.getElementById("leftLongBannerAdImg");
    
    leftLongAdHrefCntrl.href = leftLongAdsHrefAry[indexLLA];
    leftLongAdImgCntrl.src = leftLongAdsImgSrcAry[indexLLA];
    leftLongAdImgCntrl.alt = leftLongAdsImgAltAry[indexLLA];
    
    setTimeout("RotateLeftLongAds()", 90000);
}
    


    
// calls webservice to retrieve schools for the given state initial
function RetrieveSchoolsForState(stateInitial)
{
    ShamzooSite.ShamzooWebService.GetSchools(stateInitial, SchoolsForStateReceivedCallback, null, null);
}

// callback function that receives the list of schools for the selected state initial
function SchoolsForStateReceivedCallback(schools) 
{     
    ClearDlist(dlistSchools);
    
    var defaultOption = document.createElement("OPTION");
    defaultOption.text = dlistStates.options[dlistStates.selectedIndex].text;
    
    if(schools.length > 0)
    {
        defaultOption.text += " Schools";
    }
    else
    {
        defaultOption.text = "Select Your State First, Then Choose Campus Here";
    }
    defaultOption.value = "0";
    dlistSchools.options[0] = defaultOption;
        
        
    for(i=0; i<schools.length; i++)
    {
        var schoolsSplit = schools[i].split(";");
        
        var option = document.createElement("OPTION");
        option.text = schoolsSplit[0];
        option.value = schoolsSplit[1];
        
        dlistSchools.options[i+1] = option;                
    }
    
    if(callBackFunction_OnSchoolsLoaded != null)
    {
        callBackFunction_OnSchoolsLoaded();
    }
    
    document.getElementById("imgRetrievingSchools").style.display = "none";
}    


// called when state selection is changed
function StateSelectionChanged(stateInitial)
{
    if(stateInitial != "0") {

        hidSpanSelectedStateVal.innerHTML = stateInitial;
        document.getElementById("imgRetrievingSchools").style.display = "inline";
        RetrieveSchoolsForState(stateInitial);
    }
    else
    {
        ClearDlist(dlistSchools);
        
        var defaultOption = document.createElement("OPTION");
        defaultOption.text = "Select Your State First, Then Choose Campus Here";
        defaultOption.value = "0";
        dlistSchools.options[0] = defaultOption;
    }
    
}


// called when school selection is changed
function SchoolSelectionChanged(schoolId)
{
    if(schoolId > 0)
    {
        __doPostBack('schoolSelected', schoolId);
    }
}


