//====================================================================================================
//    File Name        :    index.js
//    # File Version: v 1.0
//    # Created By: Maulik Chandarana
//    # Created On: 18 June 2007
//    # Last Modified By:
//    # Last modified On:
//  # Purpose : This is file carries validation and other related function for Home page.
//----------------------------------------------------------------------------------------------------

//====================================================================================================
//    Function Name    :    Click_Login()
//    Created By: Maulik Chandarana
//    Created On: 18 June 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : Validates form variables 
//  Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
function Click_Login(frm)
{
    with(frm)
    {
        if(!IsEmpty(txtUsername, 'Please Enter Username.'))
        {
            return false;
        }
        if(!IsEmpty(txtPassword, 'Please Enter Password.'))
        {
            return false;
        }
    }
}
//====================================================================================================
//    Function Name    :    fnSplitSeconds()
//    Created By: Maulik Chandarana
//    Created On: 19 June 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : To get the value of time in seconds
//  Parameters: strTime : Value of Time
//----------------------------------------------------------------------------------------------------
var intCurrentSec = 0;
var boolStop = true;

function fnSplitSeconds(strTime)
{
    // splits string at text
    intSeconds = 0;
    var strLength = strTime.length;
    if (strLength == 0) return intSeconds;

    // convert into seconds
    arrIntHourSec = strTime.split(":");
    intHours =  parseInt(arrIntHourSec[0]); // for hours
    intMins =  parseInt(arrIntHourSec[1]); // for minutes
    intSecs = parseInt(arrIntHourSec[2]); // for seconds
    intSeconds = (intHours * 60 * 60) + (intMins * 60) + intSecs;
    return intSeconds;
}
//====================================================================================================
//    Function Name    :    fnCalculateTime()
//    Created By: Maulik Chandarana
//    Created On: 19 June 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : To Calculate the time difference and post the page again if the time slot completes
//  Parameters: frm : Form Object
//----------------------------------------------------------------------------------------------------
function fnCalculateTime(frm)
{    
    intCurrentSec++;
    strTime1 = fnSplitSeconds(frm.hdFrom.value);
    strTime2 = fnSplitSeconds(frm.hdTo.value);

    intDiffSecs = strTime2 - strTime1;    
    
    if (intDiffSecs <= intCurrentSec)
    {
        boolStop = false
    }
    
    intDiffSecs = intDiffSecs - intCurrentSec;
    
    // make counter
    intMins = Math.floor(intDiffSecs/60);
    intSeconds = (intDiffSecs % 60);

    strMins = (intMins < 10) ? "0"+intMins : intMins;
    strSecs = (intSeconds < 10) ? "0"+intSeconds : intSeconds;
    
    frm.txtRemainingTime.value = strMins+":"+strSecs;
    if (boolStop)
    {
        setTimeout("fnCalculateTime(document.frmIndex)", 1000);
    }
    
    if(strMins+":"+strSecs == "00:00")
    {
        frm.submit();
    }    
}
var url = "getTextVal.php?user_id=";
var isWorking = false;
var http = getHTTPObject();
//====================================================================================================
//    Function Name    :    fnGetOnlineOfflineStatus()
//    Created By: Maulik Chandarana
//    Created On: 02 August 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : Get the online/offline status for entered user id
//  Parameter : intUserId : User Id
//----------------------------------------------------------------------------------------------------
function fnGetOnlineOfflineStatus(strUserIds)
{
    if (!isWorking)
    {
		if (window.XMLHttpRequest)
        {            
            http = new XMLHttpRequest();
            http.open("GET", url + strUserIds, true);
            isWorking = true;
            http.onreadystatechange = handleHttpResponse;                        
            http.send(null);
            // branch for IE/Windows ActiveX version
        }
        else if (window.ActiveXObject)
        {            
            http = new ActiveXObject("Microsoft.XMLHTTP");
            if (http)
            {                                
                http.open("GET", url + strUserIds, true);
                isWorking = true;                
                http.onreadystatechange = handleHttpResponse;
                http.send();
            }
        }
    }
    setTimeout("fnGetOnlineOfflineStatus('"+strUserIds+"')", 1000); 
}
//====================================================================================================
//    Function Name    :    handleHttpResponse()
//    Created By: Maulik Chandarana
//    Created On: 02 August 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : Getting values from XML data
//  Parameters:
//----------------------------------------------------------------------------------------------------
function handleHttpResponse()
{    
    if (http.readyState == 4)
    {
        isWorking = false;
        
        if (http.responseText.indexOf('invalid') == -1)
        {
            var xmlDocument = http.responseXML;            
            var number = xmlDocument.getElementsByTagName('status').length;
            for(i=0;i<number;i++)
            {
                var strStatus = xmlDocument.getElementsByTagName('status').item(i).firstChild.data;
                var intUserId = xmlDocument.getElementsByTagName('id').item(i).firstChild.data;
                document.getElementById("idStatus_"+intUserId).innerHTML = strStatus;
                isWorking = false;
            }
        }
    }
}
