var numberTicked = 0;
var productSelected = '';
var cmprArray = new Array();
var formName = 'aspnetForm';

function RunComparison(url,width,height,chartid,chartname,proceed)
{
    if(countChecks(formName) >= 2)
    {  
        $(document).ready(function() {
            tb_show('', url + getproductquerystring(formName) + '&amp;chartid=' + chartid + '&amp;chartname=' + chartname + '&amp;proceed=' + proceed + '&amp;KeepThis=true&TB_iframe=true&amp;height=' + height + '&amp;width=' + width, null);
        });
    } 
    else
    {
        alert('In order to compare products you must have at least 2 checkboxes selected.\n\nPlease select another box before comparing.'); 
    }
}

function RunMortgageComparison(url,width,height,chartid,chartname,proceed)
{
    if(countChecks(formName) >= 2)
    {  
        $(document).ready(function() {
            tb_show('', url + getmortgagequerystring(formName) + '&amp;chartid=' + chartid + '&amp;chartname=' + chartname + '&amp;proceed=' + proceed + '&amp;KeepThis=true&TB_iframe=true&amp;height=' + height + '&amp;width=' + width, null);
        });
    }
    else
    {
        alert('In order to compare products you must have at least 2 checkboxes selected.\n\nPlease select another box before comparing.');
    }
}

function getproductquerystring(formName)
{
    var selectedlist = ""; 
    var productcount = 1;
    with(document.getElementById(formName)) { 
        for(var i = 0; i < checkbox.length; i++){ 
            if(checkbox[i].checked) { 
                selectedlist += "productid" + productcount + "=" + checkbox[i].value + "&amp;"; 
                productcount++;
            }
        }
    } 
    
    if(selectedlist != "") { 
        //trim last &amp; from string
        var strOutput = selectedlist.substring(0,selectedlist.length-5);

        //Uncheck all checkboxes prior to postback, so if they click the back button
        //everything is ready to go again
        UncheckAll();

        return strOutput;
    }
}

function getmortgagequerystring(formName)
{
    var selectedlist = ""; 
    var productcount = 1;

    with(document.getElementById(formName)) { 
        for(var i = 0; i < checkbox.length; i++){ 
            if(checkbox[i].checked) { 

                substring = new Array();

                //Split the String into an array
                substring = checkbox[i].value.split(",");

                if(substring[0] != undefined)
                {
                    //MoneyfactsID
                    selectedlist += "productid" + productcount + "=" + substring[0] + "&amp;"; 
                }
                if(substring[1] != undefined)
                {
                    //SelectionID
                    selectedlist += "selectionid" + productcount + "=" + substring[1] + "&amp;";
                }
                if(substring[2] != undefined)
                {
                    //ClickTicketID
                    selectedlist += "clickticketid" + productcount + "=" + substring[2] + "&amp;";
                }
                if(substring[3] != undefined)
                {
                    //ImagePath
                    selectedlist += "imgpath" + productcount + "=" + substring[3] + "&amp;";
                }
                //increment product count
                productcount++;
            } 
        }
    }

    if(selectedlist != "") { 
        //trim last &amp; from string
        var strOutput = selectedlist.substring(0,selectedlist.length-5);

        //Uncheck all checkboxes prior to postback, so if they click the back button
        //everything is ready to go again
        UncheckAll();

        return strOutput;
    }
}

//function to check total number of CheckBoxes 
//that are checked in a form
function countChecks() 
{
    //initialize total count to zero
    var totalChecked = 0;
    //get total number of CheckBoxes in form
    var chkBoxCount = document.getElementById(formName).checkbox.length;

    //loop through each CheckBox
    for (var i = 0; i < chkBoxCount; i++) 
    {
        //check the state of each CheckBox
        //replace "YourFormName" with the name of your form
        if (eval("document." + formName + ".checkbox[" + i + "].checked") == true) 
        {
            //it's checked so increment the counter
            totalChecked += 1;
        }
    }
    //return the number of checked
    return totalChecked;
}

function selectNewProduct(tableId, rowId)
{  
    if(document.getElementById(rowId+'_tick').checked == true)
    {
        foundSlot = false;
        if(cmprArray[tableId] == undefined)
        {
            cmprArray[tableId] = new Array();
        }
    
        if(countChecks(formName) < 4)
        {
            for(var i=0; i<3;i++)
            {
                if(cmprArray[tableId][i] == undefined)
                {
                    cmprArray[tableId][i] = document.getElementById(rowId+'_tick').value;
                    foundSlot = true;
                    break;
                }
            }
            if(foundSlot == false)
            {
                cmprArray[tableId] = new Array(); 
            }

            highlightRow(rowId);
        }
        else
        {
            alert('You can compare details of up to 3 products.\n\nPlease de-select another box before selecting this one.'); 
            document.getElementById(rowId+'_tick').checked = false; 
        }
    }
    else
    {
         //On browser back (with Viewstate)
        if(cmprArray[tableId] == undefined)
        {
            cmprArray[tableId] = new Array();
        }
    
        if(countChecks(formName) < 4)
        {
            for(var i=0; i<3;i++)
            {
                if(cmprArray[tableId][i] == document.getElementById(rowId+'_tick').value)
                {
                    cmprArray[tableId].splice(i, 1);
                    break;
                }
            }

            highlightRow(rowId);
        } 
        else
        {
            alert('You can compare details of up to 3 products.\n\nPlease de-select another box before selecting this one.'); 
            document.getElementById(rowId+'_tick').checked = false;
        }
    }
}

function highlightRow(rowId)
{
    //Retrieve current row's classname
    var rowClass = document.getElementById(rowId+'_row').className;

    if(rowClass == 'dataRow')
    {
        //var headerstyle='compareTick';
        var rowstyle='dataRowSelected';
    }
    //if(rowClass == 'bbresult bbaltresult')
    //{     
    //  var headerstyle='compareTick';
    //  var rowstyle='selectedRow2'
    //}
    if(rowClass == 'dataRowSelected')
    {
        //var headerstyle='';
        var rowstyle='dataRow';
    }
    
    // Logic for Enhanced Listing
    if(rowClass == 'enhanced')
    {
        //var headerstyle='compareTick';
        var rowstyle='dataRowSelected2';
    } 
    if(rowClass == 'dataRowSelected2')
    {
        //var headerstyle='compareTick';
        var rowstyle='enhanced';
    } 

    // Logic for Sponsored Product
    if(rowClass == 'dataRow sponsoredProductsRow')
    {
        //var headerstyle='compareTick';
        var rowstyle='dataRowSelected3';
    }
    if(rowClass == 'dataRowSelected3')
    {
        //var headerstyle='compareTick';
        var rowstyle='dataRow sponsoredProductsRow';
    }

    //if(rowClass == 'selectedRow2')
    //{     
    //  var headerstyle='';
    //  var rowstyle='bbresult bbaltresult'
    //}

    if(document.getElementById(rowId+'_tick').checked == false)
    {
        document.getElementById(rowId+'_row').className = rowstyle;
        //document.getElementById(rowId+'_header').className = headerstyle;
        //if(document.getElementById(rowId+'_footer') != null)
        //document.getElementById(rowId+'_footer').className = rowstyle;
    }
    else
    {
        document.getElementById(rowId+'_row').className = rowstyle;
        //document.getElementById(rowId+'_header').className = headerstyle;
        //if(document.getElementById(rowId+'_footer') != null)
        //document.getElementById(rowId+'_footer').className = srowstyle;
    }
}

//Unchecks all checkboxes and unhighlights selected row styles
function UncheckAll() {
    with(document.getElementById(formName))
	for (var i =0; i < checkbox.length; i++) 
	{
	    if(checkbox[i].checked) { 
            checkbox[i].checked = false;
            //Unhighlight all Selected Rows
            //trim _tick from ID
	        highlightRow(checkbox[i].id.substring(0,checkbox[i].id.length-5));
        } 
    }
}
