﻿// JScript File

var RETURNKEY = 13;

function submitOnEnter(ev)
{
    ev = $event(ev);
    var characterCode = 0;

    if(window.event)
        characterCode = window.event.keyCode;     //IE
    else
        characterCode = ev.which;                 // other
        
    if(characterCode == RETURNKEY)
        submitEnrouteLogin();
}        

function submitEnrouteLogin()
{
    var status = $("status"); 
    var loginID = $("username").value;
    var password = $("password").value;

   // var validUser = metasplatLoginService(loginID, password);

    if(loginID == "" || password == "")
    {
        $setOpacity(status, 100);
        status.style.visibility = "visible";
        window.setTimeout("removeLoginFailText()", 10000);
    }
    else
    {
        var btnSubmit = $("btnSubmit");
        btnSubmit.click(); 
    }    
}

function metasplatLoginService(loginID, password)
{
    var postData = "";
    var method = "enrouteLogin";
    var serviceURL = "enrouteWebService";     
    
    postData  = "<data><login>"+ loginID +"</login><password>"+ password +"</password></data>"
    
	var loader = new ajaxLoader();
	
	//m.CallService = function(metabookName, object, method, objectID, postData, onServiceLoaded)--Ajax loader
	loader.CallService(serviceURL, method, null, postData , null);
	
	var browserObj = navigator.userAgent.toLowerCase();

	if (browserObj.indexOf("msie") != -1)
		return loader.XMLHttpRequestObject.responseXML.text;
	else
		return loader.XMLHttpRequestObject.responseXML.firstChild.textContent;		
}

function onFocus(id)
{
    if (id == "username")
    {
        if (initialUsernameView)
        {
            var editBox = $(id);
            editBox.value = "";
        }

        initialUsernameView = false;
    }
    else if (id == "password")
    {
        if (initialPasswordView)
        {
            var editBox = $(id);
            editBox.value = "";
        }

        initialPasswordView = false;
    }
}

//function setPinFocus()
//{
//    var pin = $("pinNumber");
//    pin.focus();
//}

function adjustFooterPos(mapHeight)
{
    
    var winDim =  $getWindowDim();
    var screenDimHeight = winDim.height;
    var contentBody = $("contentBody");
    
    var bodyMinHeight =  parseInt(document.body.style.minHeight.replace("px", ""));
    
    if( isNaN(bodyMinHeight) )
        bodyMinHeight = 0;
    
    if(screenDimHeight > bodyMinHeight)
    {
        var screenDimWidth = winDim.width;
    
        var headerHeight = $("header").offsetHeight;
        var bannerHeight = $("banner").offsetHeight;    
         
        var footer = $("footer");
        var footerHeight = footer.offsetHeight;
        
        var spacerDiv = $("spacerDiv");

        var fixedHeight = headerHeight + bannerHeight + footerHeight;
        var availibleHeight = screenDimHeight - fixedHeight;
        
        //contentBody.style.height = availibleHeight;
        
         if(mapHeight)
         {
            contentBody.style.height = availibleHeight - 2 + "px";
            
            var contentBodyHeight = $("contentBody").offsetHeight;
            
            $("lhsContainer").style.height = contentBodyHeight + "px";
            $("map").style.height = contentBody.offsetHeight + "px";
            
            $("lhsMessagesContainer").style.height = contentBodyHeight - $("connectedUsersCount").offsetHeight  -10 + "px";
            $("lhsContainerMessages").style.height = contentBodyHeight - $("connectedUsersCount").offsetHeight  - $("topMenu").offsetHeight + "px";
                        
            var directions = $("directions");
            if(directions)
                directions.style.height = contentBodyHeight - $("connectedUsersCount").offsetHeight  - $("topMenu").offsetHeight + "px";
                        
            if(map)
                map.checkResize();
            //  setMapHeight(mapHeight);
         }
        else
        {
            var contentBodyHeight = $("contentBody").offsetHeight;
            var occupiedHeight = headerHeight + bannerHeight + contentBodyHeight + footerHeight;
            spacerDiv.style.height = Math.max(0, winDim.height - (occupiedHeight) ) + "px"; 
        
        }
    }
}

function getTrip()
{
    var pin = $("pinNumber");
    var pinNumber = pin.value;

    if (pinNumber == "")
    {
        pin.focus();
        alert("Please Enter a Pin Number/UserName");
        return false;
    }

    return true;
}
 
var refreshingTimeOut = null;
function sendMessage()
{

    setMsgStatus("Sending Message. Please Wait");
    if(refreshInProgress)
    {
        if(refreshingTimeOut != null)
        {
            window.clearTimeout(refreshingTimeOut);
            refreshingTimeOut = null;
        }
        refreshingTimeOut = window.setTimeout("sendMessage()", 1000);
        return;
    }

    clearRefreshMapInterval();
    var msg = $("txtMsg").value;
    var resultText = $("resultText");
    
    if(msg && msg !="")
    {
        var saveInfo = new ajaxLoader();

        var postData = "msg=" + $encodeText(msg) + "&pin="+ $("currentPin").value + "&instanceID="+ $("tripInstanceIDField").value + "&msgTimeStamp="+getDateTimeStampForSQL();
        
        saveInfo.onLoadComplete = function()
        {      
            var response = saveInfo.responseHTML;

            if(response != "0")
            {
                $("lastEntryId").value = lastEntry = parseInt(response);
                setMessageResult("Message sent successfully");

                var userMessageData = new Object();  
                userMessageData.type = 6;
                userMessageData.message = $("fullViewerName").value + " - '" +   $encodeText(msg) + "'";
                
                userMessageData.messageDateTime = getDateTimeStamp();
                
                userMessageData.senderImage = $("userImage").value;
                userMessageData.senderPin = $("currentPin").value;
                
                fillUserMessage(null, userMessageData);
            }
            else
            {
                alert("Error in sending message, please try again");
                setMessageResult("Error in sending message");
            }
            
            hideMsgStatus();
            hideOverLay();
            setRefreshMapInterval();
        };

        saveInfo.post("alertMsg.aspx", null, postData);  
    }
   
}


function getDateTimeStamp()
{
    var date = new Date();
    
    var dateOfMonth = date.getDate();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();
    
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    
    var retDate = getLeadingZeroIfNeeded(dateOfMonth) + "/" + 
                  getLeadingZeroIfNeeded(month) + "/" + 
                  year + " " + 
                  
                  getLeadingZeroIfNeeded(hour) + ":" + 
                  getLeadingZeroIfNeeded(minutes);
                  //getLeadingZeroIfNeeded(seconds);
                  
    return retDate;
}

function getDateTimeStampForSQL()
{
    var date = new Date();
    
    var dateOfMonth = date.getDate();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();
    
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    
    var retDate = year+"/"+
                  getLeadingZeroIfNeeded(month) + "/" + 
                  getLeadingZeroIfNeeded(dateOfMonth) + " " + 
                  
                  getLeadingZeroIfNeeded(hour) + ":" + 
                  getLeadingZeroIfNeeded(minutes) + ":" + 
                  getLeadingZeroIfNeeded(seconds);
                  
    return retDate;
}

function getLeadingZeroIfNeeded(number)
{
    if(number >9)
        return number;
    else
        return ("0" + number);
}


function setKeyPress(ev, onEnter, onEscape)
{
    var keyCode = $getCharCode(ev);
    
    switch (keyCode)
    {
        case 13:            // Enter Key
            eval(onEnter);
            break;
        case 27:            // Escape Key
            eval(onEscape);
            break;
    }
}

function cancelMessage()
{
    hideOverLay();
}

function userMessage()
{
    var isTripEnd = $("isTripEnd").value;
    
    isTripEnd = (isTripEnd.toLowerCase() === 'true');
    
     if(isTripEnd) 
     {
        setMessageResult("Trip ended. Cannot send message")
    }
    else
    {
        hideMsgStatus();
        showOverLay();
        var txtMsg = $("txtMsg");
        txtMsg.value = "";
        txtMsg.focus();
    }
}

//  resize the overlay to the window size
function resizeOverlay()
{
    var overlay = $("overlay");
    
    var dimensions = $getWindowDim();

    overlay.style.height = document.documentElement.scrollHeight + "px";
    overlay.style.width = document.documentElement.scrollWidth + "px";
    
    var chatPopup = $("chatPopup");
    chatPopup.style.top = ((dimensions.height-200)/2) + "px";
    chatPopup.style.left = ((dimensions.width - 400) / 2) + "px";      
}

//  show the overlay
function showOverLay()
{
    var overlay = $("overlay");
    overlay.style.display = "";
    
    var chatPopup = $("chatPopup");
    chatPopup.style.display = "";
    
    resizeOverlay();  
}

//  hide the overlay
function hideOverLay()
{
    var overlay = $("overlay");
    overlay.style.display = "none";
    
    var chatPopup = $("chatPopup");
    chatPopup.style.display = "none";
}

var resultTextTimeout = null;

function setMessageResult(message)
{
    if( resultTextTimeout !=null )
    {
        window.clearTimeout(resultTextTimeout);
        resultTextTimeout = null;
    }
    
    var resultText = $("resultText");
    $setText(resultText, message);
    var status = $("status");
    status.style.visibility = "visible";
    $setOpacity(status, 100);
    resultTextTimeout =  window.setTimeout("removeMessageResult()", 4000);
}

function setMsgStatus(msg)
{
    var msgBody = $("msgBody");
    msgBody.style.display = "none";
    
    var msgStatus = $("msgStatus");
    msgStatus.style.display = "";
    
    var msgStatusText = $("msgStatusText");
    $setText(msgStatusText,msg);
    
}

function hideMsgStatus()
{
    var msgBody = $("msgBody");
    msgBody.style.display = "";
    
    var msgStatus = $("msgStatus");
    msgStatus.style.display = "none";
    
    var msgStatusText = $("msgStatusText");
    $setText(msgStatusText,"&nbsp;");
}

function removeMessageResult()
{
    var status = $("status");
    $fadeOutObject(status, 50, 8);
}

function loadViewTrip(isQuickTrip, isNoTrip, action, isLooged, checkPrivacy)
{
    setMapStartEndPoints(isQuickTrip, isNoTrip, action, isLooged, checkPrivacy);
    adjustFooterPos(64);
}

function loadViewHistory(isQuickTrip, checkPrivacy)
{
   setHistoryMapPoints(isQuickTrip, checkPrivacy);
    adjustFooterPos(64);
    //setMapZoomOn($("zoomOn"));
}

function LHSMenuWidth()
{
    var browserObj = navigator.userAgent.toLowerCase();
	var topMenu = $("topMenu");

    if(topMenu)
    {
	    if (browserObj.indexOf("msie") != -1)
	    {
	        topMenu.style.width = "95%";
	    }
	    else
	    {
		    topMenu.style.width = "100%";
	    }	
	}
}
