function cc() {
    /* check for a cookie */
    if (document.cookie == "") {
        /* if a cookie is not found - alert user -
        change cookieexists field value to false */
        if (confirm("Cookies must be enabled!"))
            document.location = "http://www.stellarsurvey.com/help/faq.aspx?qid=38";

        /* If the user has Cookies disabled an alert will let him know 
        that cookies need to be enabled to log on.*/

        document.forms[0].cookieexists.value = "false"

    } else {
        /* this sets the value to true and nothing else will happen,
        the user will be able to log on*/
        document.forms[0].cookieexists.value = "true"
    }
}




function cb_list_verify(val) // val is of validator
{
    // find control to validate
    //var val = document.all[document.all[val.id].controltovalidate];
    var ctrl = document.getElementById(val.controltovalidate);
    var col = ctrl.all;
    if (col != null) {
        for (i = 0; i < col.length; i++)
            if (col[i].tagName.toUpperCase() == "INPUT")
            if (col[i].checked)
            return true;
    }
    return false;
}

function popup(url, winName, params) {
    var newwindow;
    newwindow = window.open(url, winName, params);
    newwindow.focus();
}

function setHiddenFieldFromPrompt(itemName, promptString, defaultValue) {
    var fn = prompt(promptString, defaultValue);
    if (fn && fn != null && fn != "null" && fn != "") {
        document.getElementById(itemName).value = fn;
        return true;
    }
    return false;
}

function getFullID(id, object) {
    var fullID = id;
    if (getPrefix(object) != "")
        fullID = getPrefix(object) + "_" + id;
    return fullID;
}

function selectCheckBox(id, object) {
    var fullID = id;
    if (getPrefix(object) != "")
        fullID = getPrefix(object) + "_" + id;

    document.getElementById(fullID).setAttribute("checked", "checked");
}

function getPrefix(object) {
    //this deals with prefixes that .net adds on to id's of controls.
    //we want id prefix of object control.
    var prefixArray = new Array();
    prefixArray = object.name.split(":");
    prefixArray.pop();
    return prefixArray.join("_");
}

function selectDropDownItem(dropDownId, itemName, object) {
    var fullID = dropDownId;
    if (getPrefix(object) != "")
        fullID = getPrefix(object) + "_" + dropDownId;

    var ddl = document.getElementById(fullID);
    for (i = 0; i < ddl.options.length; i++)
        if (trim(ddl.options[i].value) == trim(itemName))
        ddl.options[i].selected = true;
}

function trim(str) {
    return str.replace(/^\s*|\s*$/g, "");
}



function AdjustMatrixRow(radio) {
    var row;
    var columns = new Array();

    var elements = radio.form.elements; //document.forms[0].elements;
    for (var i = 0; i < elements.length; i++)
        if (elements[i].value == radio.value)
        columns.push(elements[i].name);

    // Find the row of the radio button that was just selected
    // How? For each item (row) in the column radio.form[radio.name],
    // check if radio button radio.form[radio.name][i] is checked
    // where i corresponds to row index
    for (var i = 0; i < radio.form[radio.name].length; i++)
        if (radio.form[radio.name][i].checked) {
        row = i;
        break;
    }

    // After we found the row of the checked radio button, 
    // uncheck all radio buttons in the same row except for the one 
    // just checked (in the radio.name column)
    for (var i = 0; i < columns.length; i++)
        if (columns[i] != radio.name)
        if (radio.form[columns[i]][row].checked == true)
        radio.form[columns[i]][row].checked = false;
}

function AdjustMatrixRow_old(radio, columns) {
    var row;
    // Find the row of the radio button that was just selected
    // How? For each item (row) in the column radio.form[radio.name],
    // check if radio button radio.form[radio.name][i] is checked
    // where i corresponds to row index
    for (var i = 0; i < radio.form[radio.name].length; i++)
        if (radio.form[radio.name][i].checked) {
        row = i;
        break;
    }

    // After we found the row of the checked radio button, 
    // uncheck all radio buttons in the same row except for the one 
    // just checked (in the radio.name column)
    for (var i = 0; i < columns.length; i++)
        if (columns[i] != radio.name)
        if (radio.form[columns[i]][row].checked == true)
        radio.form[columns[i]][row].checked = false;
}



function createReferrerCookie() {
    if (readCookie("Referrer") == null) {
        if (document.referrer && document.referrer != "")
            createCookie("Referrer", document.referrer, "");
        else
            createCookie("Referrer", "<No Referrer> for " + document.URL, "");
    }
}

/*
cookie functionality.
*/
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


function hide(id) {
    document.getElementById(id).style.display = 'none';
}

function show(id) {
    document.getElementById(id).style.display = '';
}


/*
fade logic    
*/
var Color = new Array();
Color[7] = "#8FBAFF";
Color[6] = "#9FC4FF";
Color[5] = "#AFCEFF";
Color[4] = "#BFD8FF";
Color[3] = "#CFE3FF";
Color[2] = "#DFEDFF";
Color[1] = "#EFF6FF";

function waittofade(element) {
    if ($(element)) {
        setTimeout("fadeIn(7, \"" + element + "\")", 200);
    }
}

function fadeIn(where, elementID) {
    if (where >= 1) {
        $(elementID).style.backgroundColor = Color[where];
        if (where > 1) {
            where -= 1;
            setTimeout("fadeIn(" + where + ", \"" + elementID + "\" )", 200);
        }
        else {
            where -= 1;
            setTimeout("fadeIn(" + where + ", \"" + elementID + "\" )", 200);
            $(elementID).style.backgroundColor = "transparent";
        }
    }
}


function flashBorder(element) {
    //alert('in flashBorder, element: ' + $(element).id);

    if ($(element)) {
        setTimeout("flash(7, '" + element + "'" + ",'" + $(element).style.backgroundColor + "' )", 200);
    }
}

function flash(where, element, originalColor) {
    //alert('where: ' + where + 'element: ' + element);
    if (where >= 1) {
        if (where > 1) {
            where -= 1;
            $(element).style.backgroundColor = Color[where];
            setTimeout("flash(" + where + ", '" + element + "'" + ",'" + originalColor + "' )", 200);
        }
        else {
            where -= 1;
            setTimeout("flash(" + where + ", '" + element + "'" + ",'" + originalColor + "' )", 200);

            $(element).style.backgroundColor = originalColor;
        }
    }
}




/*

parse query string
 
*/

function createRequestObject() {
    FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
    separator = ',';
    // The token used to separate data from multi-select inputs
    query = '' + this.location;
    qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
    query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
    if (query.length < 1) { return false; }  // Perhaps we got some bad data?
    keypairs = new Object();
    numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
    while (query.indexOf('&') > -1) {
        keypairs[numKP] = query.substring(0, query.indexOf('&'));
        query = query.substring((query.indexOf('&')) + 1);
        numKP++;
        // Split the query string at each '&', storing the left-hand side
        // of the split in a new keypairs[] holder, and chopping the query
        // so that it gets the value of the right-hand string.
    }
    keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
    for (i in keypairs) {
        keyName = keypairs[i].substring(0, keypairs[i].indexOf('='));
        // Left of '=' is name.
        keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
        // Right of '=' is value.
        while (keyValue.indexOf('+') > -1) {
            keyValue = keyValue.substring(0, keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
            // Replace each '+' in data string with a space.
        }
        keyValue = unescape(keyValue);
        // Unescape non-alphanumerics
        if (FORM_DATA[keyName]) {
            FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
            // Object already exists, it is probably a multi-select input,
            // and we need to generate a separator-delimited string
            // by appending to what we already have stored.
        } else {
            FORM_DATA[keyName] = keyValue;
            // Normal case: name gets value.
        }
    }
    return FORM_DATA;
}
FORM_DATA = createRequestObject();
// This is the array/object containing the GET data.
// Retrieve information with 'FORM_DATA [ key ] = value'.

function CopyToClipboard(elementID) {
    document.getElementById(elementID).focus();
    document.getElementById(elementID).select();
    CopiedTxt = document.selection.createRange();
    CopiedTxt.execCommand("Copy");
}

///
///
/// Hiding modal popup
function pageLoad(sender, args) {
    $addHandler(document, "keydown", OnKeyPress);
}

function OnKeyPress(args) {
    if (args.keyCode == Sys.UI.Key.esc) {
        var controls = new Array("ctl00_ContentPlaceHolder1_NewSurveyFolder1_MPE", "ctl00_ContentPlaceHolder1_AddNewCategory_PanelAddNewItem_ModalPopupExtender");
        for (var i = 0; i < controls.length; i++)
            if ($find(controls[i]) != null)
                $find(controls[i]).hide();
    }
}
///

/// GridView checkbox changed
function ChangeCheckBoxState(id, checkState)
{
    var cb = document.getElementById(id);
    if (cb != null)
        cb.checked = checkState;
}        
function ChangeAllCheckBoxStates(checkState)
{
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    if (CheckBoxIDs != null)
    {
        for (var i = 0; i < CheckBoxIDs.length; i++)
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);
    }
}        
////
