var w = screen.availWidth;
var h = screen.availHeight;

function openWindow(strUrl, strParams, strWindowName, nWidth, nHeight, strMenubar, strResizable, strScrollbars, strToolbar, strStatus)
{
    var leftPos = (w-nWidth)/2, topPos = (h-nHeight)/2;
    if (strParams != '') strUrl += '?' + strParams;
    var strAttributes = 'width=' + nWidth + ',height=' + nHeight + ',top=' + topPos + ',left=' + leftPos + ', menubar=' + strMenubar + ',resizable=' + strResizable + ',scrollbars=' + strScrollbars + ',toolbar=' + strToolbar + ',status=' + strStatus;
    
    var popup = window.open(strUrl, strWindowName, strAttributes);
    popup.focus();
}

function openTrailerWindow(id)
{
	openWindow('/MovieTrailer.aspx', 'trailerid=' + id, 'Trailers', 360, 202, 'no', 'no', 'no', 'no', 'yes');
}
function openGalleryWindow(id)
{
	openWindow('/Gallery.aspx', 'enid=' + id, 'Gallery', 770, 800, 'no', 'no', 'no', 'no', 'yes');
}

function confirmDelete()
{
	var agree = confirm("Are you sure you wish to delete this item?");
	
	if (agree)
		return true;
	else
		return false;
}

function isPlannerEmpty(nCount)
{
    if(nCount > 0)
        return true;
        
    alert('You do not have any saved events for this month to sync with your calendar.');
    return false;
}

function openDatePicker(strField)
{
    openWindow('/Admin/Controls/CinemaDatePicker.aspx', 'field=' + strField, 'DateSelection', 180, 170, 'no', 'no', 'no', 'no', 'no');
}

function highlightTime(objCell, strSwitch)
{
    if (strSwitch == 'on')
        objCell.className = 'ct_time_on';
    else if (strSwitch == 'off')
        objCell.className = 'ct_time_off';
}

function switchColumnStyle(strWeekDates, strTime, idCell, valCell, toDeleteCell)
{
    var arrStrCells = new Array();
    var nOnCount = 0;
    
    arrStrCells = strWeekDates.split(",");
    for (var i = 0; i < arrStrCells.length; i++)
    {
        var obj = document.getElementById(arrStrCells[i] + ' ' + strTime);
        var currClassName = obj.className;
        if (currClassName == 'ct_select_on' || currClassName == 'ct_select_saved')
            nOnCount++;
    }
    
    for (var i = 0; i < arrStrCells.length; i++)
    {
        var obj = document.getElementById(arrStrCells[i] + ' ' + strTime);
        
        if (nOnCount == 7)
        {
            removeTimes(obj.id, idCell, valCell, toDeleteCell);
            obj.className = 'ct_select_off';
        }
        else
        {
            if (obj.className == 'ct_select_off')
            {
                addTimes(obj.id, idCell, valCell, toDeleteCell);
                obj.className = 'ct_select_on';
            }
        }
    }
}

function switchCellStyle(objCell, idCell, valCell, toDeleteCell)
{
    var currClassName = objCell.className;
    
    switch (currClassName)
    {
        case 'ct_select_off': //Change to Orange
            addTimes(objCell.id, idCell, valCell, toDeleteCell);
            objCell.className = 'ct_select_on';
            break;
            
        case 'ct_select_on': //Change to Grey
            removeTimes(objCell.id, idCell, valCell, toDeleteCell);
            objCell.className = 'ct_select_off';
            break;
            
        case 'ct_select_saved': //Change to Grey
            removeTimes(objCell.id, idCell, valCell, toDeleteCell);
            objCell.className = 'ct_select_off'; 
            break;
    }
}

var arrSelectedTimeIds = new Array();
var arrSelectedTimes = new Array();
var arrOriginalTimeIds = new Array();
var arrOriginalTimes = new Array();
var arrToDelete = new Array();
        
function addTimes(strTime, idCell, valCell, toDeleteCell)
{
    var bFound = false;
    var bFoundOriginal = false;
    
    for (var i = 0; i < arrSelectedTimes.length; i++)
    {
        if(strTime == arrSelectedTimes[i])
            bFound = true;
    }
    
    if (bFound == false)
    {
        for (var j = 0; j < arrOriginalTimes.length; j++)
        {
            if (strTime == arrOriginalTimes[j])
            {
                arrSelectedTimeIds.push(arrOriginalTimeIds[j]);
                for(var k = 0; k < arrToDelete.length; k++)
                {
                    if(arrOriginalTimeIds[j] == arrToDelete[k])
                        arrToDelete.splice(k,1);
                }
                bFoundOriginal = true;
                break;
            }
        }
        
        if (bFoundOriginal == false)
            arrSelectedTimeIds.push('-1');
        
        arrSelectedTimes.push(strTime);
    }
    
    saveArrays(idCell, valCell, toDeleteCell);
}

function removeTimes(strTime, idCell, valCell, toDeleteCell)
{
    var removedID = '';
    
    for (var i = 0; i < arrSelectedTimes.length; i++)
    {
        if(strTime == arrSelectedTimes[i])
        {
            if(arrSelectedTimeIds[i] != '-1')
                arrToDelete.push(arrSelectedTimeIds[i]);
                        
            arrSelectedTimeIds.splice(i,1);
            arrSelectedTimes.splice(i,1);
        }
    }
    
    saveArrays(idCell, valCell, toDeleteCell);
}

function loadTimes(idCell, valCell)
{   
    var objIds = parent.document.getElementById(idCell);
    var objVals = parent.document.getElementById(valCell);
    var strIds = '';
    var strVals = '';
    
    if(objIds != null && objVals != null)
    {
        if (objIds.value.length > 0 && objVals.value.length > 0)
        {
            strIds = objIds.value;
            strVals = objVals.value;
            arrSelectedTimeIds = strIds.split(',');
            arrSelectedTimes = strVals.split(',');
            
            arrOriginalTimeIds = arrSelectedTimeIds.slice(0);
            arrOriginalTimes = arrSelectedTimes.slice(0);
            
            for (var i = 0; i < arrSelectedTimes.length; i++)
            {
                if(document.getElementById(arrSelectedTimes[i]) != null)
                    document.getElementById(arrSelectedTimes[i]).className = 'ct_select_saved';
            }
        }
    }    
}

function saveArrays(idCell, valCell, toDeleteCell)
{
    parent.document.getElementById(idCell).value = arrSelectedTimeIds;
    parent.document.getElementById(valCell).value = arrSelectedTimes;
    parent.document.getElementById(toDeleteCell).value = arrToDelete; 
}
