﻿/// <reference name="MicrosoftAjax.js"/>
function CheckOneClickMe(questionId, questionValue)
{
    var elements = document.getElementsByName(questionId)
    for (var index = 0; index < elements.length; index++)
    {
        if (elements[index].value == questionValue)
        {
            elements[index].checked = true;
        }
    }
}

var checkAllClicked = false;
function CheckAllClickMe(questionId)
{
    if (!checkAllClicked)
    {
        control = $get(questionId);
        control.checked = !control.checked;
        checkAllClicked = false;
    }
    else
    {
        checkAllClicked = false;
    }
}

function ClearVerbatimText(questionId, defaultText)
{
    control = $get(questionId);
    if (control.value == defaultText)
        control.value = '';
}

function CalculateConstantSum(questionPrefix, elementToUpdateTotal, elementToUpdateRemain, Total, preText, postText)
{
    var constantSum = 0;
    var allInputs = document.getElementsByTagName('input');
    for (onInput = 0; onInput < allInputs.length; onInput++)
    {
        if (allInputs[onInput].type == "text")//regular Constant sum
        {
            var thisTextBox = allInputs[onInput];
            if (thisTextBox.id.indexOf(questionPrefix + '_') == 0 && thisTextBox.value != '' && thisTextBox.value > -1)
                constantSum += parseInt(thisTextBox.value);
        }
        else
        {
            if (allInputs[onInput].type == "checkbox")//Market Basket Checkall/ConstantSum
            {
                if (allInputs[onInput].checked)
                {
                    var CellText = $get("CellText_" + allInputs[onInput].id);

                    var CardPoints = CellText.getElementsByTagName("span");
                    for (var i = 0; i < CardPoints.length; i++)
                    {
                        if (CardPoints[i].className == "CardPoints")
                        {
                            constantSum += parseInt(CardPoints[i].innerHTML);
                        }
                    }
                }
            }
        }
    }
    oTotal = $get(elementToUpdateTotal);
    oRemain = $get(elementToUpdateRemain);

    oTotal.innerHTML = preText + constantSum + postText;
    oRemain.innerHTML = Total - constantSum + postText;
    if (constantSum > Total)
    {
        oTotal.style.color = '#FF0000';
        oRemain.style.color = '#FF0000';
    }
    else
    {
        oTotal.style.color = '#000000';
        oRemain.style.color = '#000000';
    }
    window.setTimeout("CalculateConstantSum('" + questionPrefix + "', '" + elementToUpdateTotal + "','" + elementToUpdateRemain + "','" + Total + "', '" + preText + "', '" + postText + "')", 1000);
}

function NumbersOnly(textbox)
{
    textbox.value = textbox.value.replace(/[^0-9]/g, '');
}

function HideNextButton(milliseconds)
{
    $get("btnNext").style.visibility = "hidden";
    window.setTimeout("ShowNextButton();", milliseconds);
}

function ShowNextButton()
{
    $get("btnNext").style.visibility = "visible";
}

function ToggleCheckOneSelected(QId, QNo)
{
    $get(QId + "_" + QNo).checked = true;
    var QuestionDiv = document.getElementById("CommonQuestionArea"); //This DIV is always here, so narrow down the TD search area before grabbing them.
    var Cells = QuestionDiv.getElementsByTagName("td");    
    for (var i = 0; i < Cells.length; i++)
    {
        if (Cells[i].id.startsWith("CellOption_" + QId + "_") || Cells[i].id.startsWith("CellText_" + QId + "_"))
        {
            if (Cells[i].id.endsWith(QId + "_" + QNo))
            {
                Sys.UI.DomElement.addCssClass(Cells[i], 'SelectedCell');
            }
            else
            {
                Sys.UI.DomElement.removeCssClass(Cells[i], 'SelectedCell');
            }
        }
    }
}

var skipCheckAllWork = false;
function ToggleCheckAllSelected(CheckAllArray, QId, EnforceExclusive)
{
    if (!skipCheckAllWork)
    {
        $get(QId).checked = !$get(QId).checked;
        if ($get(QId).checked)
        {
            Sys.UI.DomElement.addCssClass($get("CellOption_" + QId), 'SelectedCell');
            if ($get("CellText_" + QId))
                Sys.UI.DomElement.addCssClass($get("CellText_" + QId), 'SelectedCell');
        }
        else
        {
            Sys.UI.DomElement.removeCssClass($get("CellOption_" + QId), 'SelectedCell');
            if ($get("CellText_" + QId))
                Sys.UI.DomElement.removeCssClass($get("CellText_" + QId), 'SelectedCell');
        }
        skipCheckAllWork = false;
        if (EnforceExclusive)
        {
            var selectedExclusive = false;
            for (var onCheckAll = 0; onCheckAll <= CheckAllArray.length - 1; onCheckAll++)
            {
                if (CheckAllArray[onCheckAll].QId == QId)
                    selectedExclusive = CheckAllArray[onCheckAll].Exclusive;
            }

            for (var onCheckAll = 0; onCheckAll <= CheckAllArray.length - 1; onCheckAll++)
            {
                if (selectedExclusive || CheckAllArray[onCheckAll].Exclusive == true)
                {
                    var thisQId = CheckAllArray[onCheckAll].QId;
                    if (thisQId != QId)
                    {
                        $get(thisQId).checked = false;
                        Sys.UI.DomElement.removeCssClass($get("CellOption_" + thisQId), 'SelectedCell');
                        if ($get("CellText_" + thisQId))
                            Sys.UI.DomElement.removeCssClass($get("CellText_" + thisQId), 'SelectedCell');
                    }
                }
            }
        }
    }
    else
    {
        skipCheckAllWork = false;
    }
}

function HoverOn(QId)
{
    if ($get("CellOption_" + QId) != null)
    {
        Sys.UI.DomElement.addCssClass($get("CellOption_" + QId), "HoverCell");
    }
    if ($get("CellText_" + QId) != null)
    {
        Sys.UI.DomElement.addCssClass($get("CellText_" + QId), "HoverCell");
    }
}

function HoverOff(QId)
{
    Sys.UI.DomElement.removeCssClass($get("CellOption_" + QId), "HoverCell");
    if ($get("CellText_" + QId) != null)
    {
        Sys.UI.DomElement.removeCssClass($get("CellText_" + QId), "HoverCell");
    }
}
function onClickSwitchToTextBox(obj)
{
    var ta = obj.getElementsByTagName("textarea");
    ta[0].focus();
}

document.getElementsByClassName = function(clsName)
{
    return document.getElementsByClassNameAndTag(clsName, "*");
}
document.getElementsByClassNameAndTag = function(clsName, tag)
{
    var retVal = new Array();
    var elements = document.getElementsByTagName(tag);
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].className.indexOf(" ") >= 0)
        {
            var classes = elements[i].className.split(" ");
            for (var j = 0; j < classes.length; j++)
            {
                if (classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if (elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}
document.getElementsBybackgroundColor = function(color)
{
    var searchColor = color.replace("#", "").toLowerCase();
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].style.backgroundColor.replace("#", "").toLowerCase().indexOf(searchColor) >= 0)
        {
            retVal.push(elements[i]);
        }
    }
    return retVal;
}
document.getElementsByforeColor = function(color)
{
    var searchColor = color.replace("#", "").toLowerCase();
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].style.color.replace("#", "").toLowerCase().indexOf(searchColor) >= 0)
        {
            retVal.push(elements[i]);
        }
    }
    return retVal;
}
function InitializeStyle(QId)
{
    //	if ($get("Q110_1_1"))
    //		alert($get("Q110_1_1").checked + " from InitStyles");

    var Cells = document.getElementsByTagName("td");

    for (var i = 0; i < Cells.length; i++)
    {
        if (Cells[i].id.startsWith("CellOption_" + QId + "_"))
        {
            var Inputs = Cells[i].getElementsByTagName("input");
            if (Inputs.length > 0)
            {
                if (Inputs[0].checked)
                {
                    Sys.UI.DomElement.addCssClass(Cells[i], 'SelectedCell');
                    if ($get(Cells[i].id.replace("CellOption", "CellText")))
                    {
                        Sys.UI.DomElement.addCssClass($get(Cells[i].id.replace("CellOption", "CellText")), 'SelectedCell');
                    }
                }
                else
                {
                    Sys.UI.DomElement.removeCssClass(Cells[i], 'SelectedCell');
                    if ($get(Cells[i].id.replace("CellOption", "CellText")))
                    {
                        Sys.UI.DomElement.removeCssClass($get(Cells[i].id.replace("CellOption", "CellText")), 'SelectedCell');
                    }
                }
            }
        }
    }
}

var offsetxpoint = -60;
var offsetypoint = 20;
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var enabletip = false;
var tipobj;

function LoadToolTip() {
	if (ie || ns6)
		tipobj = document.all ? document.all["ToolTip"] : document.getElementById ? document.getElementById("ToolTip") : ""
}

function ietruebody() {
	return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth, optionalyoffset) {
	if (ns6 || ie) {
		if (typeof thewidth != "undefined")
			tipobj.style.width = thewidth + "px";
		if (typeof thecolor != "undefined" && thecolor != "")
			tipobj.style.backgroundColor = thecolor;
		if (typeof optionalyoffset != "undefined" && thecolor != "" && thewidth != "")
			offsetypoint = optionalyoffset;
		tipobj.innerHTML = thetext;
		enabletip = true;
		document.onmousemove = positiontip;
		return false
	}
}

function positiontip(e) {
	if (enabletip) {
		var curX = (ns6) ? e.pageX : event.x + ietruebody().scrollLeft;
		var curY = (ns6) ? e.pageY : event.y + ietruebody().scrollTop;

		//Find out how close the mouse is to the corner of the window
		var rightedge = ie && !window.opera ? ietruebody().clientWidth - event.clientX - offsetxpoint : window.innerWidth - e.clientX - offsetxpoint - 20;
		var bottomedge = ie && !window.opera ? ietruebody().clientHeight - event.clientY - offsetypoint : window.innerHeight - e.clientY - offsetypoint - 20;
		var leftedge = (offsetxpoint < 0) ? offsetxpoint * (-1) : -1000;

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge < tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
			tipobj.style.left = ie ? ietruebody().scrollLeft + event.clientX - tipobj.offsetWidth + "px" : window.pageXOffset + e.clientX - tipobj.offsetWidth + "px";
		else if (curX < leftedge)
			tipobj.style.left = "5px";
		else
		//position the horizontal position of the menu where the mouse is positioned
			tipobj.style.left = curX + offsetxpoint + "px";

		//same concept with the vertical position
		if (bottomedge < tipobj.offsetHeight)
			tipobj.style.top = ie ? ietruebody().scrollTop + event.clientY - tipobj.offsetHeight - offsetypoint + "px" : window.pageYOffset + e.clientY - tipobj.offsetHeight - offsetypoint + "px";
		else
			tipobj.style.top = curY + offsetypoint + "px";

		tipobj.style.visibility = "visible";
	}
}

function hideddrivetip() {
	if (ns6 || ie) {
		enabletip = false;
		tipobj.style.visibility = "hidden";
		tipobj.style.left = "-1000px";
		tipobj.style.backgroundColor = '';
		tipobj.style.width = '';
		document.onmousemove = positiontip;
	}
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();