// JScript File
// Copyright MachBros 2006

var g_activityTypes = new Array();
var g_trainers = new Array();
var g_backLink = "";
var g_scheduleHeight = 0;
var g_scheduleWidth = 0;
var g_cancelledString = '<b>Det h&auml;r passet &auml;r tyv&auml;rr inst&auml;llt</b><br/>';

var g_bookButtonLocation = '';

function doPopup(){
    alert('my popup!');
}

function setBackLink(backLink){
    g_backLink = backLink;
}

function setScheduleSize(width,height){
    g_scheduleHeight = height;
    g_scheduleWidth = width;
}

function time_format(v){
    if(v < 10)
        return '0' + v;
    else
        return v;
}

var g_activityAction = '';
function showActivityInfo(top,left,activityId,activityTypeId,isBooked, year,week,isLoggedIn,bookingId,trainerId,substitute,startHour,startMinute,endHour,endMinute,cancelled){
    
    
    if (document.getElementById){
        var trainer = g_trainers[trainerId];
                
        var activityType = g_activityTypes[activityTypeId];
        var trainerString = '';
        
        if(trainer != null){
            trainerString = trainer['firstname'] + ' ' + trainer['lastname'];
            if(substitute == 1){
                trainerString += ' (vik)';
            }
        }

        document.getElementById('activity_title').innerHTML = activityType['name'];
        document.getElementById('activity_time').innerHTML = ''+time_format(startHour) + ':' + time_format(startMinute) + ' - ' + time_format(endHour) + ':' + time_format(endMinute);
        document.getElementById('activity_trainer').innerHTML = '<b>'+trainerString+'</b>';
        
        if(cancelled == 1)
            document.getElementById('activity_text').innerHTML = g_cancelledString + activityType['description'];
        else
            document.getElementById('activity_text').innerHTML = activityType['description'];
        
        
        document.getElementById('activity_info_div').style.top = top;
        if(left+150 <= g_scheduleWidth)
            document.getElementById('activity_info_div').style.left = left;
        else
            document.getElementById('activity_info_div').style.left = (g_scheduleWidth-150);
        
         
            
        document.getElementById('activity_info_div').style.visibility = 'visible';
        if(isLoggedIn) {
            if(isBooked == 0 && cancelled == 0){
                //document.getElementById('activity_booking_link').href = 'DoBooking.aspx?activityId='+activityId+'&year='+year+'&week='+week+'&returnUrl=' + g_backLink;
                //document.getElementById('activity_booking_link').innerHTML = 'Boka';
                //g_bookButtonLocation = 'DoBooking.aspx?activityId='+activityId+'&year='+year+'&week='+week+'&returnUrl=' + g_backLink;
                g_activityAction = 'book';
                document.getElementById('bookingFormActivityId').value = activityId;
                document.getElementById('bookingFormYear').value = year;
                document.getElementById('bookingFormWeek').value = week;
                document.getElementById('bookingFormAction').value = 'DoBooking';
                //document.getElementById('bookingFormReturnUrl').value = g_backLink;
                document.getElementById('activity_booking_button').innerHTML = 'Boka';
            } else if(isBooked == 1) {
                //document.getElementById('activity_booking_link').href = 'CancelBookingDo.aspx?bookingId='+bookingId+'&activityId='+activityId+'&year='+year+'&week='+week+'&returnUrl='+g_backLink;
                //document.getElementById('activity_booking_link').innerHTML = 'Avboka';
                //g_bookButtonLocation = 'CancelBookingDo.aspx?bookingId='+bookingId+'&activityId='+activityId+'&year='+year+'&week='+week+'&returnUrl='+g_backLink;
                g_activityAction = 'cancel';
                document.getElementById('bookingFormActivityId').value = activityId;
                document.getElementById('bookingFormYear').value = year;
                document.getElementById('bookingFormWeek').value = week;
                document.getElementById('bookingFormBookingId').value = bookingId;
                document.getElementById('bookingFormAction').value = 'CancelBooking';
//                document.getElementById('bookingFormReturnUrl').value = g_backLink;

                document.getElementById('activity_booking_button').innerHTML = 'Avboka';
                
            }
        }
        
        if( (top + document.getElementById('activity_info_div').offsetHeight) > g_scheduleHeight){
            top = g_scheduleHeight - document.getElementById('activity_info_div').offsetHeight;   
            document.getElementById('activity_info_div').style.top = top;
        }
        
        //alert(document.getElementById('activity_info_div').offsetHeight);
        
    }else{
        alert('browser error');
    }
    
}

function doActivityAction() {
    //alert('doActivityAction: ' + g_activityAction);
    if(g_activityAction == 'book'){
//        alert('submitting bookingform...');
        
        document.getElementById('bookingFormAction').value = 'DoBooking';
        document.getElementById('scheduleForm').submit();
        /*
        var theForm = document.forms['bookingForm'];
        
        alert('bookinform: ' + theForm);
        alert('bookinform.activityId: ' + theForm.activityId.value);
        theForm.submit();
        */
    } else if(g_activityAction == 'cancel'){
  //      alert('submitting cancelform...');
        document.getElementById('bookingFormAction').value = 'CancelBooking';
        document.getElementById('scheduleForm').submit();
    } else {
        alert('ERROR: JS-203');
    }
    return true;
}

function hideActivityInfo(){
    document.getElementById('activity_info_div').style.visibility = 'hidden';
    g_activityAction = '';
    if( document.getElementById('bookingFormActivityId') ){
        document.getElementById('bookingFormActivityId').value = '';
        document.getElementById('bookingFormYear').value = '';
        document.getElementById('bookingFormWeek').value = '';
        document.getElementById('bookingFormBookingId').value = '';
        document.getElementById('bookingFormAction').value = '';
        document.getElementById('activity_booking_button').innerHTML = '';
    }
    
    g_bookButtonLocation = '';
}

function showPleaseLogin(){
    hideActivityInfo();
    document.getElementById('please_login_div').style.visibility = 'visible';
}

function hidePleaseLogin(){
    document.getElementById('please_login_div').style.visibility = 'hidden';
}

function addActivityType(id,shortname,name,description){
    var activityType = new Array();
    activityType['id'] = id;
    activityType['shortname'] = shortname;
    activityType['name'] = name;
    activityType['description'] = description;
    g_activityTypes[id] = activityType;
}

function addTrainer(id,firstname,lastname){
    var trainer = new Array();
    trainer['id'] = id;
    trainer['firstname'] = firstname;
    trainer['lastname'] = lastname;
    g_trainers[id] = trainer;
}

function closeOpenActivityInfo(){
    //document.body.style.cursor.
}

function showLoginDiv(){
    document.getElementById('login_div').style.visibility = 'visible';
}

function hideLoginDiv(){
    document.getElementById('login_div').style.visibility = 'hidden';
}

function showMessageBox(title,text){
    
  
    document.getElementById('message_box_title').innerHTML = title;
  
    text = replaceAll(text,'[br/]','<br/>');
    document.getElementById('message_box_text').innerHTML = text;
    
    
    document.getElementById('message_box').style.visibility = 'visible';
}

function hideMessageBox(){
    document.getElementById('message_box').style.visibility = 'hidden';
}

function showErrorBox(title,text){
    //alert('showErrorBox: ' + text);
    text = replaceAll(text,'[br/]','<br/>');
    text = replaceAll(text,'[','<');
    text = replaceAll(text,']','>');
    text = replaceAll(text,'@','"');
    document.getElementById('error_box_title').innerHTML = title;
    document.getElementById('error_box_text').innerHTML = text;
    document.getElementById('error_box').style.visibility = 'visible';
}

function hideErrorBox(){
    document.getElementById('error_box').style.visibility = 'hidden';
}

function replaceAll(OldString,FindString,ReplaceString) {
   var SearchIndex = 0;
   var NewString = "";
   while (OldString.indexOf(FindString,SearchIndex) != -1) {
   NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
   NewString += ReplaceString;
   SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);
   }
   NewString += OldString.substring(SearchIndex,OldString.length);
   return NewString;
}
