﻿
/*  ====================

    Helpers
    
    ==================== */

function toggleFreeText(soiId)
{
    for(var i = 0; i < freetextArray.length; i++)
    {
        if(freetextArray[i][0] == soiId)
        {
            if(document.getElementById(freetextArray[i][1]) != null)
            {
                if(!document.getElementById(freetextArray[i][1]).checked)
                {
                    document.getElementById(freetextArray[i][2]).style.backgroundColor = '#eeeeee';
                    document.getElementById(freetextArray[i][2]).disabled = true;
                }
            }
        }
    }
}

function maxLength(obj, len)
{
    if(obj.value.length > len-1)
        return false;
}

function numbers(e, object)
{
    e = e || event;

    var c;
    if (e.keyCode) c = e.keyCode;
    else c = e.charCode;
    if((c < 58 && c > 47) || c == 45 || c == 46 || c == 44 || c == 8) return true;
    else return false;
    //alert(c);
}

function checkMaxLength(obj, max)
{
    if(max == '') max = 0;

    if(max > 0)
    { 
        if(obj.value.length > max)
            obj.value = obj.value.substring(0, max);
    }
}

function checkNumbersOnly(obj)
{
    var objValue = obj.value;
    for(var i = 0; i < objValue.length; i++)
    {
    }
}




/*  ==================

    One choice helpers
    
    ================== */
    
function setFocusOnImage(selectedImage, focusid)
{
    var hiddenId = document.getElementById(focusid);
    if (hiddenId.value.length > 0)
    {
        var lastSelectedImage = document.getElementById(hiddenId.value);
        lastSelectedImage.style.margin = "2px";
        lastSelectedImage.style.border = "none";
    }
    
    if(selectedImage.id == hiddenId.value) 
    {
        hiddenId.value = '';
        return;
    }
    
    selectedImage.style.margin = "0px";
    selectedImage.style.border = "dotted 2px green";                
    hiddenId.value = selectedImage.id;
}

function setRadioValue(index, id, soiId)
{
    var objId = document.getElementById(id);
    var al = document.getElementsByName(soiId); 
        
    if (objId.value.length > 0)
    {
        var lastCheckedImage = al[objId.value];
        lastCheckedImage.checked = false;
        
        if(objId.value == index) 
        {
            objId.value = '';
            return;
        }
    }  
    
    al[index].checked = true;
    
    objId.value = index;
}


/*  ==================

    Singlematrix
    
    ================== */

rankedMatrices = new Object();

// Call this for every ranked matrix
// It fills up the look-up table with its initial values
// (first time, all is false, but on postback some may have been changed)

// group      :   (string) radio button group, sans the last character (which is the specific row)
//                         actually the Soi.Id
// rows, cols :   (ints) size of matrix 
// isTouch    :   (bool) is touchscreen? can be omitted otherwise
function populateRankedLookup(group, rows, cols, isTouch) 
{
    // Get selected radio buttons.
    // Format: a string with comma-delimited pairs of coordinates: '1|4,2|4' means that row 1, col 4 and row 2, col 4 is checked
    var selected = '';
    if (isTouch == null || !isTouch)
        selected = CheckIfRankedMatrixAlternativeIsSelected(group, cols, rows);

    // Add the matrix to the look up
    rankedMatrices[group] = new Array();
    var matrix = rankedMatrices[group];
 
    for(var r = 0; r < rows; r++) 
    {
        matrix[r] = new Array();
        for (var c = 0; c < cols; c++) 
        {
            // If the radio was selected
            if(selected.indexOf(r + '|' + c + ',') > -1)
                matrix[r][c] = true;
            else
                matrix[r][c] = false;
        }
    }
}

// Checks if a certain radio button can be checked
function checkMatrixRadio(group, row, col, radio, ts_group, ts_hid) 
{
    var matrix = rankedMatrices[group];
    
    var isTS = false;
    if(ts_group != null && ts_hid != null)
        isTS = true;
          
    // If already checked, uncheck
    if(matrix[row][col])
    {    
        matrix[row][col] = false;
        if(!isTS)
            radio.checked = false;
        else 
        {
            TSRadio_check(ts_group, radio, false);
            setElementValueTo(ts_hid, '', '');
        }
        return false;
    }
    else 
    {
        // Iterate through all rows and check so that col isn't true anywhere
        var alreadyChecked = false;
        for(var r = 0; r < matrix.length; r++) 
        {
            if (r == row)
                continue;
                
                
            if(matrix[r][col]) 
            {
                alreadyChecked = true;
                break;
            }
        }
        
        // Set all in group to false (actually it's only one of them that needs unchecking, but we don't know which)
        for(var c = 0; c < matrix[row].length; c++) 
        {
            matrix[row][c] = false;
        }
        
        if(alreadyChecked)
        {
            if(!isTS)
                radio.checked = false;
            else 
            {
                setElementValueTo(ts_hid, '', '');
                TSRadio_check(ts_group, radio, false);
            }
            return false;
        }
        else 
        {
            matrix[row][col] = true;
            return true;
        }   
    }
}



/*
    Touch screen stuff
*/

/* 
    RADIO BUTTONS
*/

TSRadioGroups = new Object();

checked_img = '';
unchecked_img = '';

function setRadioButtonsImages(ch, u) 
{
    checked_img = ch;
    unchecked_img = u;
}

// Constructor for a TS Radio Button
// ==================================
// elem     : The actual img
// checked  : (optional) Initial value
function TSRadioButton(elem, checked) 
{
    this.elem = elem;
    if(checked != null)
    {
        this.checked = checked;
        if(checked)
            this.elem.src = checked_img;
        else
            this.elem.src = unchecked_img;
    }
    else 
    {
        this.checked = false;
        this.elem.src = unchecked_img;
    }
}

// Create a TS radio button group
// ===============================
// First arg is name
function TSRadioGroup(/* There are arguments, reach them with arguments[]!! */) 
{
    this.name = arguments[0];
    this.buttons = new Object();
    this.current = '';  // Key in buttons (which is the id of an img)
}

// Add a button to a group
// ========================
// group : name of group
// button : the actual element (img)
// checked : (optional) inital value
function TSRadio_addButton(group, button, checked) 
{
    if(TSRadioGroups[group] == null) 
    {       
        TSRadioGroups[group] = new TSRadioGroup(group);
    }
    
    TSRadioGroups[group].buttons[button.id] = new TSRadioButton(button, checked);
}

// Toggle radio button
// ===================
// group : the group
// button : the actual img
// check : (optional) force a value
function TSRadio_check(group, button, check) 
{
    //alert("klick");
    grp = TSRadioGroups[group];
    
    newchecked = grp.buttons[button.id];
    if(check == null)
        check = !newchecked.elem.checked;
        
    // Disable old
    old = grp.buttons[grp.current];
    if(old != null) 
    {
        old.elem.src = unchecked_img;
        old.checked = false;
    }
    
    grp.current = '';
    
    if(old == newchecked)
        return;
    
    // Check new
    if(check) 
    {
        newchecked.elem.src = checked_img;
        newchecked.checked = true;
        grp.current = button.id;
    }
    else 
    {
        newchecked.elem.src = unchecked_img;
        newchecked.checked = false;
    }
}

/*
    CHECK BOXES
*/

checkbox_checked_img = '';
checkbox_unchecked_img = '';

function setCheckBoxesImages(ch, u) 
{
    checkbox_checked_img = ch;
    checkbox_unchecked_img = u;
}

TSCheckBoxes = new Object();

function TSCheckBox(elem, checked) 
{
    this.elem = elem;
    if(checked != null)
    {
        this.checked = checked;
        if(checked)
            this.elem.src = checkbox_checked_img;
        else
            this.elem.src = checkbox_unchecked_img;
    }
    else 
    {
        this.checked = false;
        this.elem.src = checkbox_unchecked_img;
    }
    
    //alert('Created: ' + this.elem.id + ', checked: ' + this.checked);
}

function TSCheck_addButton(button, checked) 
{
    TSCheckBoxes[button.id] = new TSCheckBox(button, checked);
}

function TSCheck_check(button, check) 
{   
    box = TSCheckBoxes[button.id];

    if(check == null)
        check = !box.checked;

    // Check new
    if(check) 
    {
        box.elem.src = checkbox_checked_img;
        box.checked = true;
    }
    else 
    {
        box.elem.src = checkbox_unchecked_img;
        box.checked = false;
    }
}

// Used by radio buttons, sets the value of a hidden input to str
function setElementValueTo(hidId, str, nil) 
{
    hid = document.getElementById(hidId);
    if(hid.value == str)
        hid.value = nil;
    else
        hid.value = str;
}

// Used by checkbox, adds or removes str from a hidden input
// Delimited by comma
function addValueTo(hidId, str) 
{
    hid = document.getElementById(hidId);
    //alert('add [' + str + '] to [' + hid.value + ']');
    current = hid.value.split(',');
    newvals = '';
    removed = false;
    for(var i = 0; i < current.length; i++) 
    {
        if(current[i] != str) 
        {
            newvals += (newvals.length == 0 ? '' : ',') + current[i];
        }
        else
            removed = true;
    }
    
    if(!removed)
        newvals += (newvals.length == 0 ? '' : ',') + str;
        
    hid.value = newvals;
}