function SubmitCompareProductForm(newWindowBool, formObj, maxToCompare, newPageParams)  // this is a replacement for SubmitCompareProduct
{
    // unlike its predecessor, it allows the caller to choose whether
    // a pop-up window should be spawned
    var compareForm;
    if ( formObj )
        compareForm =  formObj;
    else
        compareForm = document.CompareProduct;

    if(ValidateData( compareForm, maxToCompare ))
    {
        if( typeof compareForm.overridePgId  != 'undefined' && compareForm.overridePgId.value)
            var pgId = compareForm.overridePgId.value;
        else
            var pgId = 100;
            
        var serverUrl = 'http://wsf4.letstalk.com';
        var tNav =  '1';
        var indexAdd =  '';
        var changePrId = '';
        var isFamilyPlan = '';  
        var showPlansCompatibleWithPhonePrice = '0';    

        if( typeof compareForm.noBuyButton  != 'undefined' && compareForm.noBuyButton.value)
            var noBuyButton = compareForm.noBuyButton.value;

        var redirectPage = "";
        if(typeof compareForm.buyLinkRedirect  != 'undefined' && compareForm.buyLinkRedirect.value)
        {
        	var charIndex = compareForm.buyLinkRedirect.value.indexOf('&redirect=');
        	if ( charIndex != -1 )
        		redirectPage = '&redirectPage=' + compareForm.buyLinkRedirect.value.substr(charIndex+10);
        	else
            	redirectPage = '&redirectPage=' + compareForm.buyLinkRedirect.value;
        }
		 
        var productCompareSearch;
        var arrLength = compareForm.length;
        var checkedCount = 0;
        
        //Create underscored sting of products to compare
        for(var i=0; i< arrLength; i++)
        {
            var elementType = compareForm.elements[i].type;

            if(elementType == 'checkbox' && compareForm.elements[i].checked == true)
            {
                var inputName = compareForm.elements[i].name;
                var startIndex = inputName.indexOf('[') +1;
                var endIndex = inputName.indexOf(']');
                var eachPrId = inputName.substring(startIndex,endIndex);

                if(checkedCount > 0)
                    productCompareSearch = productCompareSearch + '_' + eachPrId;
                else
                    productCompareSearch = eachPrId;

                checkedCount++;
            }
        }

        //If products were checked need to create compare page url 
        //and popup or redirect user
        if(productCompareSearch)
        {
            var urlParams = "";
            if(indexAdd)
                urlParams = '&indexAdd=' + indexAdd;
            if(changePrId)
                urlParams = urlParams+'&changePrId='+changePrId;
            if(noBuyButton)
                urlParams = urlParams+'&noBuyButton='+noBuyButton;
            if(isFamilyPlan)    
                urlParams = urlParams +'&isFamilyPlan=' + isFamilyPlan;    
           
            var productCompUrl = serverUrl + '/product/productComp.htm?pgId=' + pgId + '&tNav=' + tNav +'&overrideLimit=1&search=' + productCompareSearch + urlParams+redirectPage;
 
            if(!showPlansCompatibleWithPhonePrice && (compareForm && compareForm.name != 'chooseLines'))
                productCompUrl = productCompUrl + '&useDefaultPlan=1';    
 
            //If newPageParams are passed in then need to pass along to parent window
            //currently used from F&C page
            if(newPageParams)
                 parent.location = productCompUrl+newPageParams;
            else if (newWindowBool)
                popupWin(productCompUrl,'_productComp', '900', '600', '');
            else
                window.location.href = productCompUrl+'&back=1';
        }
    }
}

function ValidateData(formObj, maxToCompare) 
{

    var d;
    if ( formObj )
        d = formObj;
    else
        d = document.CompareProduct;
    var i = 0;
    var numCheckboxes = 0;
    var numChecked = 0;
    
    while ( d.elements[i] != null )
    {
        if (d.elements[i].type == 'checkbox')
        {
            numCheckboxes++;
            if (d.elements[i].checked)
            {
                numChecked++;
            }
        }
        i++;
    }

    if(numCheckboxes < 8 && numChecked == 0)
    {
        // just add them
        for (i=0; d.elements[i] != null; i++)
        {
            if (d.elements[i].type == 'checkbox')
            {
                d.elements[i].checked = true;
                numChecked++;                
            }
        }
        return true
    }
    

    if(maxToCompare && maxToCompare < numChecked)
    {
        alert('Please limit your selection to  '+ maxToCompare +' products.');
        return false;
    }

    if (numChecked == 0)
    {
        if(maxToCompare)
            alert('Please select up to '+ maxToCompare +' products for comparison by clicking in the checkboxes beneath Compare.');
        else
            alert('Please select up to 8 products for comparison by clicking in the checkboxes beneath Compare.');
        return false;
    }  
  
    return true;
}

function SubmitCompareProduct(formName)  // now just a wrapper for SubmitCompareProductForm
{
   // the old behavior was to *always* open a new window
   // we preserve the old behavior for those folks who still use it!
   SubmitCompareProductForm(true, formName, 10, '');
}


function ChangeCheckBoxStatus( prId, pgId)
{
 
	var d = document.CompareProduct;
	var item = 'comp_' + prId;
	if ( pgId )
		var ck = 'compareCheckbox[' + pgId + '-' + prId + ']';
	else
		var ck = 'compareCheckbox[' + prId + ']';
 
 	 
	if ( document.getElementById(ck).checked == true  )
	 	 document.getElementById(item).className= 'col1 active';
	else
		document.getElementById(item).className= 'col1';
}

function ChangeCompareSelect( prId, pgId, page, type)
{
 
	var d = document.CompareProduct;
	if (page == 'acc')
	{
		var item = 'comp-' + pgId + '-' + prId;
		var checkbox = 'compareCheckbox[' + pgId + '-' + prId + ']'; 
		var thumbItem = 'comp-' + pgId + '-' + prId + '-thumb';
		var thumbCheckBox = 'compareCheckbox[' + pgId + '-' + prId + '-thumb]';
	}
	else
	{
		var item = 'comp_' + prId;
		var checkbox = 'compareCheckbox[' + prId + ']'; 
	}
	
	var listElement = document.getElementById(item);
	var thumbElement = document.getElementById(thumbItem);
	
	var imgBg = 'imgbg_' + prId;
	 
	if ( pgId  && pgId != 101)
	{
		if ( type == 'thumb')
			var ck = 'compareCheckbox[' + pgId + '-' + prId + '-thumb]';
		else
			var ck = 'compareCheckbox[' + pgId + '-' + prId + ']';
	}
	else
		var ck = 'compareCheckbox[' + prId + ']';
 
 	if ( document.getElementById(ck).checked == true  )
	{
		if (listElement)
		{
			listElement.className= 'compareBtnOn';
			document.getElementById(checkbox).checked = true;
		}
		 
		
		 
	 	if (page == 'acc' && thumbElement)
	 	{
	 		thumbElement.className= 'compareBtnOn';
	 		document.getElementById(thumbCheckBox).checked = true;
	 	}
	 	
		if(document.getElementById(imgBg))
		{
			if(pgId)
			    document.getElementById(imgBg).className= 'col2 compareBgSm';
			else
			    document.getElementById(imgBg).className= 'col2 compareBg';
		}
	}
	else
	{
		if (listElement)
		{
			listElement.className= 'compareBtnOff';
			document.getElementById(checkbox).checked = false;
		}
		if (page == 'acc' && thumbElement)
	 	{
	 		thumbElement.className= 'compareBtnOff';
	 		document.getElementById(thumbCheckBox).checked = false;
	 	}
		if(document.getElementById(imgBg))
		    document.getElementById(imgBg).className= 'col2';
    }
}

function CompareLinkClick(prId, pgId, page, type)
{
    var selectBox = 'compareCheckbox[' + prId + ']';
    if(pgId && pgId != 101)
    {
        selectBox = 'compareCheckbox[' + pgId + '-' + prId + ']';
        selectBoxThumb = 'compareCheckbox[' + pgId + '-' + prId + '-thumb]';
 	}
   
    if (typeof pgId  != 'undefined' && pgId)
    	document.CompareProduct.overridePgId.value = pgId;
    
    if (document.getElementById(selectBox))
   		document.getElementById(selectBox).checked = true;
   	
   	if ( page == 'acc' && document.getElementById(selectBoxThumb))
   		document.getElementById(selectBoxThumb).checked = true;
   	
    ChangeCompareSelect(prId, pgId, page, type);
    /*if(pgId && pgId != 101)
    {   
        if(window.SubmitCompareProductCheck)
        {
            SubmitCompareProductCheck(1);
        }
        else
            SubmitCompareProduct();
    }
    else
    { */   
      
		SubmitCompareProduct();    
    //}
    
}

function CompareDisableCheckBoxOtherGroup( prId, checkPgId )
{
    allFields = document.CompareProduct.elements.length;
     
    var allcheckboxChecked = 1;
    var onecheckboxChecked = 0;
     
     
     for( i= 0; i< allFields; i ++ )
    {
        if( document.CompareProduct.elements[i].type == 'checkbox')
        {
            startIndex =  document.CompareProduct.elements[i].id.indexOf('[') + 1;
            endIndex = document.CompareProduct.elements[i].id.indexOf('-');
            pgId = document.CompareProduct.elements[i].id.slice( startIndex, endIndex );
            if (pgId == checkPgId )
            {
                if (document.CompareProduct.elements[i].checked == true  )
                {
                    allcheckboxChecked = 1;
                    onecheckboxChecked = 1;
                    document.CompareProduct.overridePgId.value = checkPgId;
                }
                else
                    allcheckboxChecked = 0;
            }
        }
    }
    for( i= 0; i< allFields; i ++ )
    {
        if( document.CompareProduct.elements[i].type == 'checkbox')
        {
        	startIndex =  document.CompareProduct.elements[i].id.indexOf('[') + 1;
            endIndex = document.CompareProduct.elements[i].id.indexOf('-');
            pgId = document.CompareProduct.elements[i].id.slice( startIndex, endIndex );
           if( pgId != checkPgId )
           {
                if ( onecheckboxChecked )
                    document.CompareProduct.elements[i].disabled=true;
               else
                    document.CompareProduct.elements[i].disabled=false;
           }
        }
    }
}

function CompareDisableCheckBox( prId, checkPgId )
{
    allFields = document.CompareProduct.elements.length;

    
    var onecheckboxChecked = 0;
	 
    for( i= 0; i< allFields; i ++ )
    {
        if( document.CompareProduct.elements[i].type == 'checkbox')
        {
        	 
            if ( document.CompareProduct.elements[i].id.substr(0, 19)  == 'compareCheckbox['+ checkPgId )
            {
                if (document.CompareProduct.elements[i].checked == true  )
                	onecheckboxChecked = 1;
                 
            }
        }
    }
	 
    for( i= 0; i< allFields; i ++ )
    {
        if( document.CompareProduct.elements[i].type == 'checkbox')
        {
           if( document.CompareProduct.elements[i].id.substr(0, 19) != 'compareCheckbox['+ checkPgId )
           {
           
                if ( onecheckboxChecked )
                    document.CompareProduct.elements[i].disabled=true;
               else
                    document.CompareProduct.elements[i].disabled=false;
           }
        }
    }
}

function SwitchCompareImage( prId, checkPgId )
{
	allFields = document.CompareProduct.elements.length;

    
    var onecheckboxChecked = 0;
	 
    for( i= 0; i< allFields; i ++ )
    {
        if( document.CompareProduct.elements[i].type == 'checkbox')
        {
        	 
            if ( document.CompareProduct.elements[i].id.substr(0, 19)  == 'compareCheckbox['+ checkPgId )
            {
                if (document.CompareProduct.elements[i].checked == true  )
                	onecheckboxChecked = 1;
        	}
        }
    }
     
    var allP = document.CompareProduct.getElementsByTagName('p'); 
    
	for(var i=0;i<allP.length;i++)
	{
		if(allP[i].id.substr(0, 5) == 'comp-' && allP[i].id.substr(0, 8) != 'comp-' + checkPgId)
		{
		 
			if(onecheckboxChecked)
				allP[i].className = "compareBtnOff visibilityNo" ;
			else
				allP[i].className = "compareBtnOff";
		}
		
		if (allP[i].id.substr(0, 8) == 'compoff-' &&  allP[i].id.substr(0, 11) != 'compoff-' + checkPgId)
		{
			if (onecheckboxChecked)
				allP[i].className = "compareInactive visibilityYes";
			else
				allP[i].className = "compareInactive visibilityNo";
		}
	}
}
