// requires vars id and number 
var past_treatments_template = new Template('<div class="box_bianco" id="outer_#{id}" style="display:none"> <label class="labeltit"> <img alt="" src="images/collapsed.png" id="#{id}_collapsed" /> <img alt="" src="images/expanded.png" id="#{id}_expanded" style="display:none"/> <a href="#" onclick="slidePanel(\'#{id}\'); return false;"> Past Treatment #{number} </a> </label><div class="treatment_wrapper" id="#{id}" style="display:none;"><div class="box_alert" id="#{id}_alert" style="display:none;"><h3> Alert </h3> More than three treatments are very unlikely to be found. </div><div class="inner_box_tre"> <label class="labelsott"> Nucleoside Rt inhibitors </label> <input type="checkbox"/> <label >Abacavir</label> <br /> <input type="checkbox" /> <label>Emtricitabine </label> <br /> <input type="checkbox"/> <label>Dideoxycytidine*</label> <br /> <input type="checkbox"/> <label>Didanosine</label> <br /> <input type="checkbox"/> <label>Lamivudine</label> <br /> <input type="checkbox"/> <label>Stavudine</label> <br /> <input type="checkbox" /> <label>Tenofovir</label><br /> <input type="checkbox"/> <label> Zidovudine </label></div><div class="inner_box_tre"> <label class="labelsott">Non-Nucleoside Rt inhibitors</label> <input type="checkbox"/> <label >Delavirdine</label> <br /> <input type="checkbox"/> <label>Efavirenz</label> <br /> <input type="checkbox" /> <label>Nevirapine</label></div><div class="inner_box_tre"> <label class="labelsott">Protease inhibitors</label> <input type="checkbox"/> <label>Atazanavir</label> <br /> <input type="checkbox"/> <label>Amprenavir/fosamprenavir</label> <br /> <input type="checkbox"/> <label>Indinavir</label> <br /> <input type="checkbox"/> <label>Lopinavir</label> <br /> <input type="checkbox"/> <label>Nelfinavir</label> <br /> <input type="checkbox"/> <label>Ritonavir (boosted dose)</label> <br /> <input type="checkbox"/> <label>Saquinavir</label> <br /> <input type="checkbox" /> <label>Tipranavir</label></div></div><div class="clear"></div> ');

var treatments = $w('treatment_wrapper_0');

/* 
new PeriodicalExecuter(function(pe) {
	treatments.each(function(item){ 
			if ($(item)) {
				check_checkboxes(item);
			}
		}); 
	}, 1);
*/

// conta le checkbox attive, 
// tra quelle figlie dell'elemento passato.
/*
function get_checkboxes_count(eid) {
	var i = 0;
	$(eid).descendants().each( function(item) {
		if (item.tagName=='INPUT') {
			if (item.getAttribute('type')=='checkbox') {
				if (item.checked) { i++; }
			}
		}
	});
	return i;
}
*/


// demo 
function reverset_demo() {
	v = "M41LM, D67N, K70R, L74LV, M184V, T215FIST, K219Q";
	$('reverset_textarea').value = v.gsub(',', '') ; 
}
	
function protease_demo() {
	$('protease_textarea').value = "T12S I13V K14R L63P" ; 
}

// function sequence_demo() {
// 	$('file_input_textarea').value = "CCTCAGATCACTCTTTGGCAACGACCcCTcGTCTCAGTAAGGATAGGGGGGCAGCTAAA" + 
// 		"GGaaGCTCTATTAGATACAgGAGCaGATGATACaGTATTAGAAGAAAT\n" + 
// 		"GAATTTGCCaGGAAGATGGAAACCAAAAATGATAGGGGGAATTGGAGGTTTTATCAAAGTAAGACAGTATGATCAGATACCCATAGAAATCTGTGGAC\n" + 
// 		"aTAAAGCTATAGGTACAGTATTAGTAGGACCTACACCTGTCAACATAATTGGAAGAAACCTGTtGACTCAGATTGGTTGCaCtTTAAaTTTTCCCATtAgTCCTATtGAA\n" + 
// 		"ACTGTACCAGTAAAATTAAAGCCaGGAATGGATGGCCCAAAGGTTAAACAATGGCCATTGACAGAAGAAAAAATaAARGCaTTAAGAGAgAtTTGTACAGAAWT\n" +
// 		"GGAAAAgGAAGGAAaAATTTCaAGAGTTGGGCCTGAAAATCCATACaAYACTccaataTTTgCTATAAAGAAAAAGAACAGTACTAGATGGAGAAAAKTAGTAGA\n" +
// 		"TTTCAGAGARCTTAATAAGAGAACTCAAGACTTCTGGGAAGTTCAATTAGGAATACCACATCCYGCAGGGTTgaamAAGAAMAAATCAGTAACAGTACTGGATGT\n"+
// 		"GGGTGATGCATATTTTTCAGTYCCYTTagaTgAAGaCtTCAGGAAgTATACTGCATTTACCATACCTAGTACAAACAATGARACACCAGGGATTAGaTATCAGTACA\n"+
// 		"ATGTGcTTCCACAGGGATgGAAAGGAtCACCAGCAAtATTCCAAAGTAgCATGACAAAAATCTTAGatCCTTTTAGAAAACAAAATccaGACATAGTTATCTrtCAR\n"+
// 		"tacGTGGATGATTTGTATGtAGGATCTGACTTAGAAATAGGgCAGaATAGAACAAAARTAGAGGAACTRAGASAACATYTGTTGAGGTGGGGATtYWYCACACCAGA\n"+
// 		"MCAAAAGCATCAGAAAGAMCCTCCATTCCTTTGGATGGGTTATGAACTCCATCCTGATAAATGGACAGTACAGCCTATAGTGCTACCAGAAAAAGACAGCTGGACTG\n" +
// 		"TCAATGACATACAGAAGTTAGTRGGAAAATTGAATTGGGCAAGTCAGATTTACCCAGGGATTAAAGTAAAGCACTTATGTAAACTCCTTAGGGGGACCAAAgCACTA\n" + 
// 		"ACAGAAGtAATACCTCTAACAGGAGAAgCAGAACTAGAACtGgCAGAAAAyAGGGAGaTYCTAARAGAACCAGTACaTGGAGtGtATtAtgaCcCATCAAAAGAATT\n" + 
// 		"AATAGCAGAAATACAGAAGCAGGGGcAAGGC";
// 		
// }

/** 
 * Simula il comportamento di una radio button tra i fratelli 
 * della checbox passata 
 */ 
 
function simulate_radio(el) {
	if (Prototype.Browser.IE) {
		Element.extend(el); // for IE, first time clicking casuse error without this 
	}
	el.siblings().each(function(item) {
		if (item.tagName=='INPUT' 
			&& item.getAttribute('type') =="checkbox") 
			{ 
				// item.setAttribute('checked', false);
				item.checked = false; 
			}
	});	
}


// restituisce il numero di switch attivi
function get_active_switches() {
	var i=0; 
	$$('.switch_on').each(function(item) {
		if (item.checked) {
			i++;
		}
	});
	return i;
}


/**
 * Restituisce il codice html per di un box di messaggio.
 * 
 */

function check_checkboxes(eid) {
	e = eid + "_alert";
	
	//if (get_checkboxes_count(eid) > 3) {
	if (get_active_switches() > 3) {
		if (!$(e).visible()) { Effect.BlindDown(e, {duration: 0.1} );}
	}
	else {
		if ($(e).visible()) { Effect.BlindUp(e, {duration: 0.1} );} 
	}
}



function get_selects(eid) {
	// var i = 0;
	var a = new Array();
	$(eid).descendants().each( function(item) {
		if (item.tagName=='SELECT') {
			s = a.size(); 
			a[s] = item;
		}
	});
	return a;	
}


function notify_errors(alert_box_id, ul_id, errors) {
	// ul_id = 'error_ul';
	if($(ul_id)) { Element.remove(ul_id); }
	ul = new Element('ul', {id : 'error_ul'} );
	
	errors.each(function(pair) {
		li = new Element('li', {id : pair.key} );
		li.appendChild(document.createTextNode(pair.value)); 
		ul.insert(li, {position:"bottom"} );
		}
	); 
	$(alert_box_id).insert(ul, {position:"bottom"}); 
	$(alert_box_id).scrollTo();
	Effect.BlindDown(alert_box_id, {duration: 0.2});
}

/**
 * Controlla l'invio del form nella pagina sequence_analisys
 */ 
function checkSequenceForm() {
	alert_box_id = 'sequence_alert';
	if ($(alert_box_id).visible()) { $(alert_box_id).hide() ; }
	errors = new Hash();
	if ($('datafile').value != '' && $('file_input_textarea').value != '') {
		code = 'E1';
		errors.set(code,"Cannot specify both text and file source.");
	}
	if ($('datafile').value == '' && $('file_input_textarea').value == '') {
		code = "E2";
		errors.set(code, "You must specify a genotype.");
	}
	if (errors.keys().size() > 0) {
		notify_errors(alert_box_id,'error_ul',errors);
		return false;
	}
	return true;
}


function checkMutationForm() {
	// Nessun campo obbligatorio.
	return true; 
	
	
}

function checkXmlForm() {
	
	alert_box_id = 'xml_alert';
	if ($(alert_box_id).visible()) { $(alert_box_id).hide() ; }
	errors = new Hash();
	if ( !($('xml_textarea').value != '' ^ $('xml_datafile').value != '') ) {
		code = "E1"; 
		errors.set(code, "You must provide xml data, by text input or file upload either.");
	}
	if (errors.keys().size() > 0) {
		notify_errors(alert_box_id, 'error_ul', errors);
		return false; 
	}
	return true;
}

function OtherAA(a,b) {
	return false; 
}

function slidePanel(panel_id) {
	collapsed = panel_id + '_collapsed';
	expanded = panel_id + '_expanded';
	Effect.toggle(panel_id, 'slide', {duration : 0.2, queue : 'front', scope : panel_id});	
	if (!$(panel_id).visible()) {
		$(collapsed).hide();
		$(expanded).show();
	}
	else {
		$(collapsed).show();
		$(expanded).hide();
	}
	// Effect.toggle(expanded, null,  {duration : 0, queue : 'end', scope : panel_id } );		
	// 	Effect.toggle(collapsed, null, {duration : 0, queue : 'end', scope : panel_id } );	
	// Effect.toggle(expanded);		
	// 	Effect.toggle(collapsed);	
}



function updatePastTreatments(tnumber) {
	
	if (isNaN(tnumber)) { return false; }
	
	$('past_treatments_container').descendants().each(function(item) {
		// Effect.BlindUp(item.id, {duration : 0.2, queue : 'end', scope : 'clean' });
		item.remove(); 
	});
	
	$R(1,tnumber).each(function(item) {
		code = makePastTreatment(item);
		$('past_treatments_container').insert({'bottom' : code});
	});
	
	$R(1,tnumber).each(function(item) {
		element = 'outer_treatment_wrapper_' + item;
		// Effect.SlideDown(element, {duration : 0.1, queue : 'end', scope : 'show'} );
		$(element).show(); 
	});
	
	treatments = $w('treatment_wrapper_0');
	
	$R(1,tnumber).each(function(item) {
		i = treatments.size(); 
		treatments[i] = 'treatment_wrapper_' + item;
	});
}

function makePastTreatment(n) {
	id = "treatment_wrapper_" + n;
	code = past_treatments_template.evaluate({id : id, number : n} );
	return code;
}

function mend_ie6_remote_request() {
	a  = $F('file_input_textarea').gsub(" ", "\n");
	$('file_input_textarea').value = a ;
}

function activate_loader() {
  $('submit-container').hide();
  $('loading-div').show();
  return true; 
}
