var pFams=null;
var hidePreFilterWarning=false;


var planFamily=Class.create({
	init:function(obj){
		this.node=$(obj);
		this.plans=this.node.$css('tr[filter]');
	},
	checkPlans:function(fNum){
		if(fNum==-1){
			this.plans.forEach(function(val){
				$(val).toggleOn();
			});
			setDisplay(this.node,'block');
			filterRadios.forEach(function(val){val.checked=false});
			if(hidePreFilterWarning){
				setDisplay("preFilterMessageBox","none");
				hidePreFilterWarning=false;
			}
			return true;
		}
		var hiddenCount=0;
		var _t=this;
		this.plans.forEach(function(val){
			var f=val.getAttribute('filter');
			if(f.charAt(fNum)=='0'){
				$(val).toggleOff();
				hiddenCount++;
			}
			else{
				$(val).toggleOn();
			}
		});
		if(hiddenCount==this.plans.length){
			setDisplay(this.node,'none');
			return false
		}
		else{
			setDisplay(this.node,'block');
			return true
		}
	}
});

function doFilter(e,el,fNum){
	var showing=pFams.filter(function(val){
		return val.checkPlans(fNum);
	});
	setDisplay('no-plans',(showing.length>0?'none':'block'));
}


var filterForm=null;
var filterRadios=null;
function doPlanLoad(){
	pFams=$css('div.plan-family');
	pFams.exec(function(val){
		return (new planFamily(val))
	});
	filterForm=$('plan-filters');
	if(filterForm){
		filterRadios=filterForm.$t('input');
		filterRadios.forEach(function(val,key){
			Events.add('filterClick'+key,'click');
			Events.inQ('filterClick'+key,{f:doFilter,p:[key]});
			Events.attach(val,'filterClick'+key);
			if(val.checked){
				doFilter(1,1,key);
			}
		});			
		var rst=$css('table.feature-bar td.last a');
		Events.add('filterReset','click');
		Events.inQ('filterReset',{f:doFilter,p:[-1]});
		Events.attach(rst[0],'filterReset',false,true,true);
		Events.add('filterForm','submit');
		Events.attach(filterForm,'filterForm',false,true,true);
	}
	if($("preFilterMessageBox")){
		hidePreFilterWarning=true;
	}
	
	//removes the yellow select image from rows that have a flag of div class='hideSelect'
	var root = $css(".hideSelect");
	root.forEach(function(item,k){
		try{
			var img = item.$css(".hideSelectImage").getHTML()
			var url = item.$css(".hideSelectUrl").getHTML();
			item.$parent().$next().$next().childAt(0).$parent().setHTML('<a href="'+url+'"><img src="'+img+'"> </img> </a>');
		}catch(e){}
	});

	
}

function planFamilyFormPrepare(ev,el){
    var temp="";
    var j=0;

	var elems=el.$css('input[type=checkbox]; input[type=radio]');
	elems.forEach(function(val){
		if(val.checked){
			temp+=((temp!="")?"|":"")+val.value;
		}
	});
    el.elements["selServiceString"].value=temp;
}

//Added the function for the plan qty change
function changePrice(rowId){
	var value = document.getElementById(rowId+"simplyEverythingCostOption").value;
	var pValue=value.split('|');
	document.getElementById(rowId+"simplyEverythingCost").innerHTML = pValue[1];
}

Events.add('planFamilyFormSubmit','submit');
Events.inQ('planFamilyFormSubmit',planFamilyFormPrepare);

plansBehaviors=new Array();
plansBehaviors[0]=new Behavior({
	css:'div.plan-family form',
	events:{planFamilyFormSubmit:true}
});


// ---------------------------------------------------------------------------
// ----- Ad Dialog -----------------------------------------------------------

var lcDialog=null;
//Initialize the dialog widget that will display the pop-up dialog message
function initLCDialog(){   
	lcDialog=new widgets.dialog({id:'lcDialogId',className:'podDialogClass',backingClass:'podBackgroundClass',css:'position:absolute; z-index:200; width:650px;'},true);
	Events.attach($(lcDialog.backer.id),'closeOverlay',false,true,true);
}  
// Function to hide the dialog widget when invoked
function removeDialog(){
	lcDialog.remove(); 
	lcDialog=null;
}  
Events.add('closeOverlay','click',removeDialog);
function showAdDialog(){
	if(!lcDialog){
		//Initialize the dialog if it is not initialized
		initLCDialog();
	}
		var template='<div style="background-color:white;text-align:right; height:30px;_height:30px;padding:10px 5px 0px 0px;">'+
						'<a style="border:1px solid #B1B1B1;text-align:center;padding:0px 5px 0px 5px; position:relative; display:inline;" href="javascript:removeDialog();">CLOSE X</a>'+
					 '</div>';
			template +='<div id="content_eLearningButtonLeft" style="display:block;"><iframe name="FRAME1" src="http://www.nextel.com/en/stores/popups/everything_data_comparison_popup.shtml" width="650" height="550" frameborder="0" scrolling="yes" ></iframe></div>';
	lcDialog.setContent(template);  
	lcDialog.show(); 
	lcDialog.center();
}
function modalScripts(){
	$css('head')[0].appendChild($e('script',{src:'/assets/scripts/fx/centering.js'}));
	$css('head')[0].appendChild($e('script',{src:'/assets/scripts/widgets/dialogs.js'}));
	$css('head')[0].appendChild($e('script',{src:'/assets/scripts/fx/fading.js'}));
}

// ----- Ad Dialog -----------------------------------------------------------
// ---------------------------------------------------------------------------

Events.inQ('DOMLoad',{f:Behaviors.run,p:[plansBehaviors]});
Events.inQ('DOMLoad',{f:doPlanLoad});

Events.inQ('DOMLoad',{f:modalScripts});