﻿/*
Description:     JScript File for app to control the displaying of default values for the text in a text control.
Date: 29/02/2008 Peter Acevski - Created (for ANZ).
      20090203 George Petrakis - Redefined for Swaggie.
      20090224 George Petrakis - Altered so that style colours are set first, to stop weird UI effects, where the text for a control is set first, 
          and then the colour changes, when refreshs are slow, (or by the time it gets to the last control).
*/

/*
Under IE the event instance an undercase 'event' instance is not defined.
Under Firefox this is called 'Event'. Therefore when using the AJAXControlToolkit (in Firefox), the 'event' instance
reference (used deep in its core somewhere) is undefined
This is necessary because the CollapsiblePanels from the AJAXControlToolkit
will throw an exception (when collapsing/expanding) under Firefox because the 'event' object is undefined. 
*/

var DEFAULT_SEARCH_TEXT = "Find Suburb"; //"Location, accommodation or attraction";
var DEFAULT_ROUTE_FROM_TEXT = "From Suburb";
var DEFAULT_ROUTE_WAYPOINT_TEXT = "Via Suburb";
var DEFAULT_ROUTE_TO_TEXT = "To Suburb";

var DEFAULT_TEXT_COLOR = "#C0C0C0";
var ENTERED_TEXT_COLOR = "#655B55";
 
function clearSearchText(){
    if(consoleLog == true){ console.log("clearSearchText - Start.");}

    var textLocationSearch = $get(textSearchClientID);
    $get("hSearchID").value = '';

    setSearchXY('','');

    textLocationSearch.style.color = DEFAULT_TEXT_COLOR;
    textLocationSearch.value = DEFAULT_SEARCH_TEXT;
}

function clearRouteText(){
    if(consoleLog == true){ console.log("clearRouteText - Start.");}
    
    var textRouteFrom = $get(textRouteFromClientID);
    var textRouteTo = $get(textRouteToClientID);

    $get("hRouteFromID").value = '';
    $get("hRouteToID").value = '';
      
    setRouteFromToXY('','','','');

    textRouteFrom.style.color = DEFAULT_TEXT_COLOR;
    textRouteFrom.value = DEFAULT_ROUTE_FROM_TEXT;

    textRouteTo.style.color = DEFAULT_TEXT_COLOR;   
    textRouteTo.value = DEFAULT_ROUTE_TO_TEXT;
       
    clearWaypoints();
}

function loadDefaultText()
{
    if(consoleLog == true){ console.log("loadDefaultText - Start collapsiblePanelExtenderWaypoint1:" + '<%=collapsiblePanelExtenderWaypoint1.ClientID%>' + ".");}
    if(consoleLog == true){ console.log("loadDefaultText - Start textSearchClientID:" + textSearchClientID + ".");}

    var textBox 
    textBox = $get(textSearchClientID);
        if(consoleLog == true){ console.log("loadDefaultText - Start textBox:" + textBox + ".");}
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_SEARCH_TEXT;

    textBox = $get(textRouteFromClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_FROM_TEXT;
        
    textBox = $get(textRouteWaypoint1ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

    textBox = $get(textRouteWaypoint2ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

    textBox = $get(textRouteWaypoint3ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
        
    textBox = $get(textRouteWaypoint4ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
        
    textBox = $get(textRouteWaypoint5ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
    
    textBox = $get(textRouteToClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_TO_TEXT;
        
    //setFindLocationOptions('ddlFindLocationOptions');
}
/*
    'Adds' a waypoint by expanding the first colllapsed panel in our group of 5.
*/
function clickAddWaypoint(){
    if(consoleLog == true){ console.log("clickAddWaypoint - Start");}
    if( true == expandPanel(collapsiblePanelExtenderWaypoint1ClientID)){
        return true;
    }else if( true == expandPanel(collapsiblePanelExtenderWaypoint2ClientID)){
        return true;
    }else if( true == expandPanel(collapsiblePanelExtenderWaypoint3ClientID)){
        return true;
    }else if( true == expandPanel(collapsiblePanelExtenderWaypoint4ClientID)){
        return true;
    }else if( true == expandPanel(collapsiblePanelExtenderWaypoint5ClientID)){
        return true;
    }
    return true;
}

/*
    Expands the requested panel. Need to use the $find() fn, to obtain the generated control id.
    Returns either a true or false depending on whether the collapsiblepanel control, was expanded (true),
    or was already expanded (false).
*/
function expandPanel(idName){
    if(consoleLog == true){ console.log("expandPanel - Start idName:" + idName + ".");}

    var collPanel = $find(idName);
    
    if(consoleLog == true){ console.log("expandPanel - Start collPanel:" + collPanel+ ".");}
    if (collPanel.get_Collapsed()){
//        try{
            collPanel.set_Collapsed(false);
//        }catch (ex){
//            throw (ex);
//        }
        //if(consoleLog == true){ console.log("expandPanel - Finished (Expanded).");}
        return true;   
    }else{
        //if(consoleLog == true){ console.log("expandPanel - Finished (Already expanded).");}
        return false;
    }
}

/*
    Sets waypoint associated hidden values to '',
    also wipes out to lastSuccessfulRouteFrom and lastSuccessfulRouteTo to 'reset' a route.
*/
function clickRemoveWaypointData(idName){
    lastSuccessfulRouteFrom = null;
    lastSuccessfulRouteTo = null;
    
    removeWaypointData(idName);
}

/*
    Sets associated hidden values to ''.
*/
function removeWaypointData(idName)
{
    if(consoleLog == true){ console.log("removeWaypointData - Start idName:" + idName + ".");}
    if (idName == textRouteWaypoint1ClientID){
        textBox = $get(textRouteWaypoint1ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

        lastSuccessfulRouteWaypoint1 = null;
    
        $get('hRouteWaypoint1ID').value = '';

        $get('hRouteWaypoint1X').value = '';
        $get('hRouteWaypoint1Y').value = '';
    }else if (idName == textRouteWaypoint2ClientID){
        textBox = $get(textRouteWaypoint2ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

        lastSuccessfulRouteWaypoint2 = null;
    
        $get('hRouteWaypoint2ID').value = '';

        $get('hRouteWaypoint2X').value = '';
        $get('hRouteWaypoint2Y').value = '';
    }else if (idName == textRouteWaypoint3ClientID){
        textBox = $get(textRouteWaypoint3ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

        lastSuccessfulRouteWaypoint3 = null;
    
        $get('hRouteWaypoint3ID').value = '';

        $get('hRouteWaypoint3X').value = '';
        $get('hRouteWaypoint3Y').value = '';
    }else if (idName == textRouteWaypoint4ClientID){
        textBox = $get(textRouteWaypoint4ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

        lastSuccessfulRouteWaypoint4 = null;
    
        $get('hRouteWaypoint4ID').value = '';

        $get('hRouteWaypoint4X').value = '';
        $get('hRouteWaypoint4Y').value = '';
    }else if (idName == textRouteWaypoint5ClientID){
        textBox = $get(textRouteWaypoint5ClientID);
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;

        lastSuccessfulRouteWaypoint5 = null;
    
        $get('hRouteWaypoint5ID').value = '';

        $get('hRouteWaypoint5X').value = '';
        $get('hRouteWaypoint5Y').value = '';
    }
}

function clearWaypoints(){
    if(consoleLog == true){ console.log("clearWaypoints - Removing Waypoints...");}

    clickRemoveWaypointData(textRouteWaypoint1ClientID);
    clickRemoveWaypointData(textRouteWaypoint2ClientID);
    clickRemoveWaypointData(textRouteWaypoint3ClientID);
    clickRemoveWaypointData(textRouteWaypoint4ClientID);
    clickRemoveWaypointData(textRouteWaypoint5ClientID);
        
    collapsePanel(collapsiblePanelExtenderWaypoint5ClientID);
    collapsePanel(collapsiblePanelExtenderWaypoint4ClientID);
    collapsePanel(collapsiblePanelExtenderWaypoint3ClientID);
    collapsePanel(collapsiblePanelExtenderWaypoint2ClientID);
    collapsePanel(collapsiblePanelExtenderWaypoint1ClientID);

    if(consoleLog == true){ console.log("clearWaypoints - Removed Waypoints.");}
        
    return true;
}    

/*
    Collapses the requested panel. Need to use the $find() fn, to obtain the generated control id.
    Returns either a true or false depending on whether the collapsiblepanel control, was collapsed (true),
    or was already collapsed (false).
*/
function collapsePanel(idName){
    if(consoleLog == true){ console.log("collapsePanel - Start idName:" + idName + ".");}

    //var collPanel = $get(idName);
    var collPanel = $find(idName);
    if (! collPanel.get_Collapsed()){
//        try{
            collPanel.set_Collapsed(true);
//        }catch (ex){
//            if(consoleLog == true){ console.log("**********************   " + ex);}
//        }
        //if(consoleLog == true){ console.log("collapsePanel - Finished (Collapsed).");}
        return true;
    }else{
        //if(consoleLog == true){ console.log("collapsePanel - Finished (Already collapsed).");}
        return false;
    }
}       

//Wipes the text field clean when condition is meet.
function txtSearchFocus(idName)
{
    var textBox = $get(idName);
    if (textBox.value == DEFAULT_SEARCH_TEXT)
    {
        if(consoleLog == true){ console.log("txtSearchBlur - Is: '" + DEFAULT_SEARCH_TEXT + "'.");}
        textBox.style.color = ENTERED_TEXT_COLOR;
        textBox.value = '';
    }
    else
    {
        if(consoleLog == true){ console.log("txtSearchBlur - Is NOT '" + DEFAULT_SEARCH_TEXT + "'.");}
        textBox.style.color = ENTERED_TEXT_COLOR;
    }
}

//Puts default text in box if it is null.
function txtSearchBlur(idName)
{
    var textBox = $get(idName);
    //alert("txtFindLocationBlur() " + textBox.value);
    //Check to see if box is empty
    if (IsEmpty(textBox))
    {
        if(consoleLog == true){ console.log("txtSearchBlur - Is empty.");}

        //check to see which text box is focused
        textBox.style.color = DEFAULT_TEXT_COLOR;
        textBox.value = DEFAULT_SEARCH_TEXT;
    }
}

//Wipes the text field clean when condition is meet.
function txtRouteFocus(idName)
{
    var textBox = $get(idName);
    //alert("txtRouteFocus() " + textBox.value);
    
    switch(idName){
    case textRouteFromClientID: 
        if (textBox.value == DEFAULT_ROUTE_FROM_TEXT)
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
            textBox.value = '';
        }
        else
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
        }
        break;
    case textRouteWaypoint1ClientID: case textRouteWaypoint2ClientID: case textRouteWaypoint3ClientID: case textRouteWaypoint4ClientID: case textRouteWaypoint5ClientID:
        if (textBox.value == DEFAULT_ROUTE_WAYPOINT_TEXT)
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
            textBox.value = '';
        }
        else
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
        }
        break;
    case textRouteToClientID:
        if (textBox.value == DEFAULT_ROUTE_TO_TEXT)
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
            textBox.value = '';
        }
        else
        {
            textBox.style.color = ENTERED_TEXT_COLOR;
        }
        break;
    default :
        break;
    }
}

//Puts default text in box if it is null.
function txtRouteBlur(idName)
{
    var textBox = $get(idName);
    //alert("txtRouteBlur() " + textBox.value);

    //Check to see if box is empty
    if (IsEmpty(textBox))
    {
        //check to see which text box is focused
        if (idName == textRouteFromClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_FROM_TEXT;
            $get("hRouteFromID").value = '';
        }
        else if (idName == textRouteWaypoint1ClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
            $get('hRouteWaypoint1ID').value = '';
        }
        else if (idName == textRouteWaypoint2ClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
            $get('hRouteWaypoint2ID').value = '';
        }
        else if (idName == textRouteWaypoint3ClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
            $get('hRouteWaypoint3ID').value = '';
        }
        else if (idName == textRouteWaypoint4ClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
            $get('hRouteWaypoint4ID').value = '';
        }
        else if (idName == textRouteWaypoint5ClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_WAYPOINT_TEXT;
            $get('hRouteWaypoint5ID').value = '';            
        }
        else if (idName == textRouteToClientID){
            textBox.style.color = DEFAULT_TEXT_COLOR;
            textBox.value = DEFAULT_ROUTE_TO_TEXT;
            $get("hRouteToID").value = '';
        }
    }
    else
    {
        //Do nothing
    }
}

//Returns true if a textbox is empty.
function IsEmpty(aTextField) {
    if ((aTextField.value.length==0) || (aTextField.value==null))
    {
        return true;
    }
    else 
    { 
        return false; 
    }
}