
var pageLimit=20;
var pageStart=0;
var popup_top,popup_left,popup_height,popup_width = 0;

if((navigator.userAgent.indexOf("Mac")>-1)&&(navigator.appName=="Microsoft Internet Explorer"))ie_mac = 1;
else ie_mac = 0;

function initPopup(){
  if (self.innerHeight) {
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) {
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  popup_top = (windowHeight-popup_height)/2;
  popup_left = (windowWidth-popup_width)/2;
}


function imp_k12(city,grade1,grade2,scenario1,scenario2,scenario3,state,teacher,school,purpose1,purpose2,ref){
	this.city = city;
	this.grade1 = grade1;
	this.grade2 = grade2;
	this.scenario1 = scenario1;
	this.scenario2 = scenario2;
	this.scenario3 = scenario3;
	this.state = state;
	this.teacher = teacher;
	this.school = school;
	this.purpose1 = purpose1;
	this.purpose2 = purpose2;
	this.ref = ref;

	this.checkValues = function(valueArray){
		var grade = valueArray[0];
		var sval = valueArray[1];
		var pval = valueArray[2];
		var stateval = valueArray[3];
		if( (grade=='all_grades' || (((this.grade1=='K' && 1*grade>=0) || (1*grade>=1*this.grade1)) && 1*grade<=1*this.grade2))
			&& (sval=='all_scenarii' || (sval==this.scenario1 || sval==this.scenario2 || sval==this.scenario3))
			&& (pval=='all_purposes' || (pval==this.purpose1 || pval==this.purpose2))
      && (stateval=='all_states' || stateval==this.state)
		   ){
			return true;
		}else{
			return false;
		}
	}

	this.myDisplay = function(){
		var str = '<span class="user_link">'+this.teacher+': <span class="italic">'+this.school+' '+in_word+' '+this.city+', '+this.state+'</span></span>';;
		str += '<div id="extra'+i+'" class="scenario_link italic">';
		str += grade_word+': '+this.grade1+' to '+this.grade2;
		str += '<br />'+scenario_word+': '+selectDisplay('scenario_select',this.scenario1);
		if(this.scenario2!='none'){
			str += ', '+selectDisplay('scenario_select',this.scenario2);
		}
		if(this.scenario3!='none'){
			str += ', '+selectDisplay('scenario_select',this.scenario3);
		}
		str += '<br /> '+purpose_word+': '+selectDisplay('purpose_select',this.purpose1);
		if(this.purpose2!='none'){
			str += ', '+selectDisplay('purpose_select',this.purpose2);
		}
		str += '</div>';
		return str;
	}
}

function impMath(state,country,teacher,college,institution_type,scenario,purpose1,purpose2,course,ref){
	this.state = state;
	this.teacher = teacher;
	this.country = country;
	this.college = college;
	this.institution_type = institution_type;
	this.scenario = scenario;
	this.purpose1 = purpose1;
	this.purpose2 = purpose2;
	this.course = course;
	this.ref = ref;

	this.checkValues = function(valueArray){
		var itype = valueArray[0];
		var sval = valueArray[1];
		var pval = valueArray[2];
		var cval = valueArray[3];
		var stateval = valueArray[4];
		if( (itype=='all_types' || itype==this.institution_type)
			&& (sval=='all_scenarii' || sval==this.scenario)
			&& (pval=='all_purposes' || (pval==this.purpose1 || pval==this.purpose2))
      && (cval=='all_courses' || cval==this.course)
      && (stateval=='all_states' || stateval==this.state)
		   ){
			return true;
		}else{
			return false;
		}
	}

	this.myDisplay = function(){
		var str = '<span class="user_link">'+this.teacher+': <span class="italic">'+this.college+', '+(this.state=='XX' ? this.country : this.state)+'</span></span>';;
		str += '<div id="extra'+i+'" class="scenario_link italic">';
		str += type_word+': '+selectDisplay('type_select',this.institution_type);
		str += '<br />'+scenario_word+': '+selectDisplay('scenario_select',this.scenario);
		str += '<br /> '+purpose_word+': '+selectDisplay('purpose_select',this.purpose1);
		if(this.purpose2!='none'){
			str += ', '+selectDisplay('purpose_select',this.purpose2);
		}
		str += '<br /> '+course_word+': '+selectDisplay('course_select',this.course);
		str += '</div>';
		return str;
	}
}

function impHomeschool(state,country,city,parent,scenario,purpose1,purpose2,course1,course2,course3,course4,ref){
	this.state = state;
	this.city = city;
	this.country = country;
	this.parent = parent;
	this.scenario = scenario;
	this.purpose1 = purpose1;
	this.purpose2 = purpose2;
	this.course1 = course1;
	this.course2 = course2;
	this.course3 = course3;
	this.course4 = course4;
	this.school = parent;
	this.ref = ref;

	this.checkValues = function(valueArray){
		var sval = valueArray[0];
		var pval = valueArray[1];
		var courseval = valueArray[2];
		var stateval = valueArray[3];
		if( (sval=='all_scenarii' || sval==this.scenario)
			&& (pval=='all_purposes' || (pval==this.purpose1 || pval==this.purpose2))
			&& (courseval=='all_course_products' || (courseval==this.course1 || courseval==this.course2 || courseval==this.course3 || courseval==this.course4))
      			&& (stateval=='all_states' || stateval==this.state)
		   ){
			return true;
		}else{
			return false;
		}
	}

	this.myDisplay = function(){
		var lstr = (this.state=='XX' ? this.country : this.state);
		var str = '<span class="user_link">'+this.parent+': <span class="italic">'+this.city+(lstr==''?'': ', ')+ lstr +'</span></span>';;
		str += '<div id="extra'+i+'" class="scenario_link italic">';
		str += scenario_word+': '+selectDisplay('scenario_select',this.scenario);
		var pstr = '';
		if(this.purpose1!='none' && this.purpose1!=''){
			pstr = selectDisplay('purpose_select',this.purpose1);
		}
		if(this.purpose2!='none' && this.purpose2!=''){
			pstr += ', '+selectDisplay('purpose_select',this.purpose2);
		}
		str += '<br /> '+purpose_word+': '+(pstr=='' ? 'N/A' : pstr);

		var cstr = '';
		if(this.course1!='none'){
			cstr = selectDisplay('course_select',this.course1);
		}
		if(this.course2!='none'){
			cstr += ', '+selectDisplay('course_select',this.course2);
		}
		if(this.course3!='none'){
			cstr += ', '+selectDisplay('course_select',this.course3);
		}
		if(this.course4!='none'){
			cstr += ', '+selectDisplay('course_select',this.course4);
		}
		str += '<br /> '+course_word+': '+(cstr=='' ? 'N/A' : cstr);
		str += '</div>';
		return str;
	}
}


function sort_imp(a,b){
	if(a.state==b.state){
		if(a.school<b.school)		return -1;
		else if(a.school==b.school)	return 0;
		else				return 1;
	}else if(a.state<b.state){
		return -1;
	}else{
		return 1;
	}
}


function di(refer){
	return document.getElementById(refer);
}

function print_list(parse_list_view,impstratNb){
  if(impstratNb<pageLimit || confirm('You\'ve selected '+impstratNb+' implementation strategies. This might take a while to load. Do you still want to continue?')){
    mywindow = window.open(path+"/popup?_form_=true&parse_list="+parse_list_view+"&parse_request=true&cmscache=parse_list:parse_request","mywindow","location=0,status=1,resizable=yes,scrollbars=1,width="+popup_width+",height="+popup_height);
    mywindow.moveTo(popup_left, popup_top);
    mywindow.focus();
  }
}


//removeAllChildNodes() : useful function that cleans the nodes of an element
function removeAllChildNodes(target_element, length_node){
  if(target_element.hasChildNodes()){
    while(target_element.childNodes.length >= length_node){
      target_element.removeChild(target_element.firstChild);
    }
  }
}

function selectDisplay(type,option){
	var opt_sc = di(type).options;
  for(k=1;k<opt_sc.length;k++){
    if(option==opt_sc[k].value){
			if(!opt_sc[k].origText){
				opt_sc[k].origText=opt_sc[k].text;
			}
      return opt_sc[k].origText;
    }
  }
return;
}

//resetLists : to reset all the search fields and rewrite the entire TOC
function resetLists(){
  for(var i=0;i<listSearch.length;i++){
    di(listSearch[i]).selectedIndex = 0;
	}
  removeAllChildNodes(di('container_toc'),1);
  update_TOC();
}


// initialize pulldowns
function update_TOC(flag){
  disp_array = [];
  pageStart = 0;
  var count_array = new Array();
  var allValues = new Array();
  for(var i=0;i<listSearch.length;i++){
  	var obj = di(listSearch[i]);
		if(!count_array[i]){
			count_array[i]=new Array();
		}
		for(var j=0;j<obj.options.length;j++){
			if(!count_array[i][j]){
				count_array[i][j]=0;
			}
			var listValue = new Array();
	  	for(var k=0;k<listSearch.length;k++){
				listValue[k]=((k==i)?obj.options[j].value:di(listSearch[k]).options[di(listSearch[k]).selectedIndex].value);
			}
			for (l=0; l<master_list.length; l++){
				if(master_list[l].checkValues(listValue)){
					count = count_array[i][j];
					count_array[i][j] = (count+1);
					if(i==0 && j==obj.selectedIndex){
						disp_array.push(master_list[l]);
					}
				}
			}
			if(!obj.options[j].origText){
				obj.options[j].origText=obj.options[j].text;
			}
			obj.options[j].text = obj.options[j].origText + ' ('+count_array[i][j]+')        ';
		}
	}
  disp_array.sort(sort_imp);
  writeTOC();
}

function writeTOC(){
	removeAllChildNodes(di('container_toc'),1);
	var parse_list = new Array();
	var parent_div = document.createElement('div');
	parent_div.className = 'principal_div';

  var endDisp = Math.min(disp_array.length,pageStart+pageLimit);
  for(i=pageStart; i<endDisp; i++){
    var temp_div = document.createElement('a');
    temp_div.className='inner_content';
    temp_div.isIn=false;
    temp_div.targetImpstrat = disp_array[i].ref;
    temp_div.href='javascript:void(0)';
    temp_div.style.display='block';
    temp_div.onclick = function(){
      print_list(this.targetImpstrat,1);
    }
    temp_div.onmouseover = function() {
      if(!temp_div.isIn){
        this.isIn=true;
        this.className='inner_content_sel';
      }
    }
    temp_div.onmouseout = function(e) {
      this.isIn=false;
      this.className='inner_content';
    };

		temp_div.innerHTML = disp_array[i].myDisplay();
		parent_div.appendChild(temp_div);
	}
  for(i=0; i<disp_array.length; i++){
    parse_list.push(disp_array[i].ref);
  }

    var head_word = '';
    //if(student_type=='homeschool'){
      var scenario_val = di('scenario_select').options[di('scenario_select').selectedIndex].origText;
      var purpose_val = di('purpose_select').options[di('purpose_select').selectedIndex].origText;
      if(purpose_val=='Other') purpose_val+=' '+purpose_word;
      var state_val = di('state_select').options[di('state_select').selectedIndex].origText;
      if(student_type=='k12'){
	      var grade_val = di('grade_select').options[di('grade_select').selectedIndex].origText;
		var head_word = '<div class="head_word">Implementation Strategies for '+grade_val+', '+scenario_val+', '+purpose_val+' and '+state_val;
	}else if(student_type=='math'){
	      var type_val = di('type_select').options[di('type_select').selectedIndex].origText;
	      var course_val = di('course_select').options[di('course_select').selectedIndex].origText;
		var head_word = '<div class="head_word">Implementation Strategies for '+type_val+', '+scenario_val+', '+purpose_val+', '+course_val+' and '+state_val;
	}else if(student_type=='homeschool'){
	      var course_val = di('course_select').options[di('course_select').selectedIndex].origText;
		var head_word = '<div class="head_word">Implementation Strategies for '+scenario_val+', '+purpose_val+', '+course_val+' and '+state_val;
	}
    //}else{
    //  var head_word = type_list[j];
    //}
		if(parse_list.length==0){
			parent_div.innerHTML = head_word+'</div><div class="inner_content"></br>None</div>';
		}else{
			var head_title = document.createElement('span');
      head_word += ' <a class=link_impstrat href="javascript:void(0)" onclick="print_list(\''+parse_list.join(",")+'\',\''+parse_list.length+'\')">('+view_all_word+') </a>';
      searchBox = document.createElement('div');
      searchBox.className='search_box';
      var searchBoxExtra = '';
      var searchBoxExtraEnd = '';
    //  if(student_type!='homeschool'){
        if(disp_array.length>pageLimit){
          searchBoxExtra='<div style="margin-top: 10px">';
          searchBoxExtraEnd='</div>';
          var endDisp = Math.min(disp_array.length,pageStart+pageLimit);
          var navHTML = '<div class="result_right">';
          if(pageStart==0){
            navHTML+='<span style="color: #666666">|&lt;&lt; '+navText[0]+'</span> - <span style="color: #666666">&lt; '+navText[1]+'</span>';
          }else{
            navHTML+='<a href="javascript: goTo(0)">|&lt;&lt; '+navText[0]+'</a> - <a href="javascript: goTo('+(pageStart-20)+')">&lt; '+navText[1]+'</a>';
          }
          var max=(20*Math.floor((disp_array.length-1)/20));
          if(pageStart==max){
            navHTML+=' - <span style="color: #666666">'+navText[2]+' &gt;</span> - <span style="color: #666666">'+navText[3]+' &gt;&gt;|</span>';
          }else{
            navHTML+=' - <a href="javascript: goTo('+(pageStart+20)+')">'+navText[2]+' &gt;</a> - <a href="javascript: goTo('+max+')">'+navText[3]+' &gt;&gt;|</a>';
          }
          searchBox.innerHTML += navHTML+'</div>';
          searchBox.innerHTML += '<div class="result">Results <b>'+(pageStart+1)+'</b> - <b>'+endDisp+'</b> of <b>'+disp_array.length+'</b></div></div>';
        }
    //  }
      head_title.innerHTML = head_word + searchBoxExtra + searchBox.innerHTML + searchBoxExtraEnd + '</div>';
			parent_div.insertBefore(head_title,parent_div.firstChild);
      parent_div.appendChild(searchBox);
		}
	  di('container_toc').appendChild(parent_div);
}

function goTo(i){
  pageStart=i;
  writeTOC();
}
