/*
 checkbox behavior and attach boolean variable to class='check' object for "checked plans".
 replaces check.js mouseover/out behavior in  pages where this script is placed.
*/
var plans=Array();
var planString='';
function select(targ){
	targ.checked==false?targ.checked=true:targ.checked=false;
	var temp='';
	for(i=0;i<plans.length;i++){
		if(plans[i].checked==true)
			temp+=plans[i].planID+',';
	}
	temp=temp.substr(0,temp.lastIndexOf(','));
	planString=temp;
	temp='';
	document.plan_action.selected_plans.value=planString;
	
	if(targ.checked==false)//uncheck immediately on click
		targ.style.backgroundPosition='0px 0px';
}

function init(){
	a=document.getElementsByTagName('a');//some class='check' are <a>
	li=document.getElementsByTagName('li');//some class='check' are <li>
	if (document.getElementById('get')) {
		document.getElementById('get').onclick=function(){
			if(document.plan_action.selected_plans.value!=""){
				document.plan_action.submit_type.value ="save";
				document.plan_action.submit();
				return true;
			} else {
				alert("Select at least one plan to save");
				return false;
			}
		}
	}
	if (document.getElementById('save')) {
		document.getElementById('save').onclick=function(){
			if(document.plan_action.selected_plans.value!=""){
			   document.plan_action.submit_type.value ="submit";
			   document.plan_action.submit();
				return true;
			} else{
				alert("Select at least one plan to submit");
				return false;
			}

		}
	}
	importCnt=0;
	// when <a class='check'>
	for(i=0;i<a.length;i++){
		if(a[i].className=='check'){
			a[i].checked=false;
			a[i].planID=importID[importCnt];
			plans.push(a[i]);
			importCnt++;

			a[i].onmouseover=function(){
				height=this.offsetHeight;
				this.style.backgroundPosition='0px -'+height+'px';
				
			}
			a[i].onmouseout=function(){
				if(this.checked==false){
					this.style.backgroundPosition='0px 0px';}
			}
			a[i].onclick=function(){
				select(this);
			}
		}
	}
	
	// when <li class='check'>
	for(i=0;i<li.length;i++){
		if(li[i].className=='check'){
			li[i].checked=false;
			li[i].planID=importID[importCnt];
			plans.push(li[i]);
			importCnt++;
			li[i].onmouseover=function(){
				height=this.offsetHeight;
				this.style.backgroundPosition='0px -'+height+'px';
				
			}
			li[i].onmouseout=function(){
				if(this.checked==false){
					this.style.backgroundPosition='0px 0px';}
			}
			li[i].onclick=function(){
				select(this);
			}
		}
	}
}



function addEvent(elm, evType, fn, useCapture){  //cross-browser event handling
	if(elm.addEventListener){//firefox, safari, chrome, opera
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}else if(elm.attachEvent){//ie
		if(navigator.userAgent.indexOf('MSIE 6')!=-1)
			ie6=true;
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}else{
		elm['on' + evType] = fn;
	}
}
addEvent(window, 'load', init, false);
