//requires Scriptaculous.js (just load the effects lib), which requires Prototype.js
//for application.tile

//    check year length and display the div
//from dropdowns
function compare(who,max,divname) {
    if (who.presLengthYrs.options[who.presLengthYrs.selectedIndex].value < max ){        
        document.getElementById(divname).style.display = 'block';
	}else{
		 document.getElementById(divname).style.display = 'none';
	}
}

function compareLess(who,max,divname) {
    if (who.presLengthYrs.options[who.presLengthYrs.selectedIndex].value < max ){
    	if( $(divname).style.display == 'none'){  //this prevents the effect if the element is already showing
       		new Effect.BlindDown($(divname));
    	}
	}else{
		new Effect.BlindUp($(divname));
	}
}

function compareEqual(who,desired,divname) {		
    if (who.selectedIndex == desired ){        
        if( $(divname).style.display == 'none'){  //this prevents the effect if the element is already showing
       		new Effect.BlindDown($(divname));
    	}
	}else{
		 new Effect.BlindUp($(divname));
	}
}
function getElementByClassName(classname){
	var inc=0;		
	var customcollection = new Array();	
	//var alltags=document.all? document.all : document.getElementsByTagName("*");	
	var tagNames = new Array();
	tagNames[0] = 'div';
	tagNames[1] = 'fieldset';
	tagNames[2] = 'a';
	tagNames[3] = 'input';
	tagNames[4] = 'span';
	tagNames[5] = 'li';
	
	for ( var n = 0; n < tagNames.length; n++) {
		alltags = document.getElementsByTagName( tagNames[n]);		
		for (i=0; i<alltags.length; i++){
			if (alltags[i].className.indexOf(classname)!= -1){
			customcollection[inc++]=alltags[i];
			}
		}
	}
	return customcollection;
}

/**
  * Checkbox Section
  */
//master is the 'select all' checkbox, which should be what called this
//class1 is the class of the checkboxes we want to check
function checkAll(master,class1) {	
	var checkit = new Array();
	checkit = getElementByClassName(class1);
	
	if(master.checked == true){
		for (i=0; i<(checkit.length); i++) {
			checkit[i].checked = true;			
		}	
	}else{
		for (i=0; i<(checkit.length); i++) {
			checkit[i].checked = false;			
		}			
	}
	return;
}
//check1 is the checkbox that called this function
//masterID is the id of the 'select all' checkbox
//class1 is the class of the checkboxes controlled by the masterID checkbox
function updateChecks(check1,masterID,class1) {		
	if(check1.checked == false){
		document.getElementById(masterID).checked = false;
		return;
	}
	
	var checkit = new Array();
	checkit = getElementByClassName(class1);
	for (i=0; i<(checkit.length); i++) {
		if(checkit[i].checked == false){
			document.getElementById(masterID).checked = false;
			return;
		}
	}
	//if none of the boxes are unchecked, check the master checkbox as well
	document.getElementById(masterID).checked = true;
	return;
}
function showRequired(check1,class1){		
	var checkit = new Array();
	checkit = getElementByClassName(class1);	
	for (i=0; i<(checkit.length); i++) {
		if(check1.checked == true){			
			checkit[i].style.visibility='visible';
		}else{			
			checkit[i].style.visibility='hidden';
		}
	}
}


//toggle checked property of the id sent in
function toggleCheck(id1) {
	if(document.getElementById(id1).checked  == false){
			document.getElementById(id1).checked = true;
	}else{
		document.getElementById(id1).checked = false;
	}
}

/** Open/Close with Effects **/
function openCloseBlind(id1, id2, id3) {	
	var delayLength = 0;
	if (id2 != null){
		var hideit = new Array();		
		hideit = getElementByClassName(id2);		
		for (i=0; i<(hideit.length); i++) {
			if(hideit[i].style.display != 'none'){
				delayLength = 0.5;
				new Effect.BlindUp($(hideit[i]),{duration:0.3});
			}
		}
	}
	if (id3 != null){
		var hidelink = new Array();
		hidelink = getElementByClassName(id3);
		for (t=0; t<(hidelink.length); t++) {
			hidelink[t].className = '';
		}
	}	
	if( $(id1).style.display == 'none'){  //this prevents the effect if the element is already showing
       		new Effect.BlindDown($(id1),{duration:0.3,delay:delayLength});
    	}	
	if (document.getElementById(id1+'-link') != null){
		document.getElementById(id1+'-link').className = 'active';
	}
}

function changeFollowup( dropdown,affectedDropdown ){	
	var testThis = dropdown.options[dropdown.selectedIndex].innerHTML;	
	if(testThis == 'Completed'){		
		$(affectedDropdown).selectedIndex = 1;
	}
}
function changeStatusDropdown( dropdown,affectedDropdown ){	
	var testThis = dropdown.options[dropdown.selectedIndex].innerHTML;	
	if(testThis == 'Completed'){		
		$(affectedDropdown).selectedIndex = 3;
	}
}