// show/hide the images
// pass the object's id
function nf_hide_show_images(img_to_hide,img_to_show){
//   alert(img_to_hide+" :: "+img_to_show);
  // hide first
  set_class(img_to_hide,'hidden_images');
  // then show
  set_class(img_to_show,'');
  return true;
}

function nf_year_round(season_start,season_end){
  if(document.getElementById){
    if(document.getElementById(season_start[0])){document.getElementById(season_start[0]).selectedIndex = 1;}
    if(document.getElementById(season_start[1])){document.getElementById(season_start[1]).selectedIndex = 1;}
    if(document.getElementById(season_end[0])){document.getElementById(season_end[0]).selectedIndex = 31;}
    if(document.getElementById(season_end[1])){document.getElementById(season_end[1]).selectedIndex = 12;}
  }
}


// active search functions, created by Chris Gazard
// modified by Stephen Gazard 20/Sep/2008 to take account of the CMS
// structure and the possibility of having >26 items attributes
// names altered so that relatively unique to the setup and don't trample other functions (prepended all with 'nf_'

// must be defined in the code output by the CMS.  e.g.:
//  properties are separated by a '|'
// var nf_ItemProps = new Array("A", "A|B", "A|B|C", "D", "A|E", "F", "G", "H", "I", "A"); //array of item properties
// var nf_Filters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"); //Filters available *MUST* be written in order

var nf_Visible = new Array(); //Array of which rows should visible
var nf_ActiveFilters = new Array();
var nf_avail_filters = new Array();
var nf_AS_options = new Array();
nf_AS_options['num_per_page'] = 10;
nf_AS_options['page'] = 1;
nf_AS_options['href'] = 'index.php?';

//initialization of 'active search.  perhaps not needed?
function nf_AS_initialize(){ //produces the initial page setup
	if (nf_ItemProps.length>0){
		document.getElementById("NoItems").style.display="none"; //if there are items, hide the "no items found row" at the start
	}
}

function nf_AS_update(page){ //runs each times checkboxes are changed.
        // tmp_alert
        var tmp_alert = '';
	if(!document.getElementById){return false;}
	// check for a new set of filters; move to page 1
	if(parseInt(page)>0){nf_AS_options['page'] = 1;}
	nf_AS_GetFilters();
//         nf_Visible = Array();
	var i=0;var j=0; var e;var prop_len = nf_ItemProps.length;
	// tmp
	var prop_tmp = Array();
	var tmp_active_filters = nf_ActiveFilters.toString()+',';
 	tmp_alert +=('The active filters are:'+tmp_active_filters+"\nSearching '"+prop_len+"' items\n");
        var tmp2= '';var tmp='';
        var filter_len = nf_ActiveFilters.length;
        var filter_tmp;
        // set all to '0'
        for(x in nf_Filters){
          nf_avail_filters[nf_Filters[x].replace(/f_/,'')] = 0;
        }


	while (i<prop_len){
 	        // split apart the properties by '|'
                // item is considered not visible initially.
                nf_Visible[i]=0;
		e=0;
// 		tmp+=i+': ';
		// no properties
                if(nf_ItemProps[i].length == 0){
                  // let get hidden since we know nothing about it
                  if(filter_len>0){
                    i++;
                    tmp_alert += 'Allowing the item '+i+' to be hidden since we know nothing about it'+"\n";
//                     tmp+="\n";
                    continue;
                  }
                  // no properties, but no filters either
                  else{nf_Visible[i]=1;}
                }
                // got multiple properties or just one
                if(nf_ItemProps[i].match(/\|/)){prop_tmp = nf_ItemProps[i].split('|');}
                else{prop_tmp[0] = nf_ItemProps[i];}

	        tmp_alert +=(filter_len+" facilities.  List checking against: '"+nf_ItemProps[i]+"'\n");
                while(e<=prop_tmp.length){
//                   tmp+=e+":";
                  if(prop_tmp[e]){
			if(tmp_active_filters.indexOf('f_'+prop_tmp[e]+',')>-1){
//                           tmp+=e+".\ne:"+e+' f_'+prop_tmp[e]+','+"? -> '"+tmp_active_filters.indexOf('f_'+prop_tmp[e]+',')+"'.";
//                           tmp+=" Visible!\n";
			  nf_Visible[i]++; //if it has the property, then give it a point.
			  tmp_alert += 'setting '+i+' as visible'+"\n";
//                           // no more looping needed
//                           break;
// 			if((nf_ItemProps[i].indexOf(nf_ActiveFilters[e]))>(-1)){
			}
// 			else{tmp+="HIDE";}
                   }
		   e++;
		}
// 		if(document.getElementById("inclusive").checked==true){
			nf_Visible[i]=(nf_Visible[i]+1)-filter_len ; //eliminate all options that don't have everything
// 		}
//
		if(nf_ActiveFilters.length==0){
			nf_Visible[i]=1; //if nothing is selected, show everything.
		}
// 		tmp+=" --> tagged as visible?"+nf_Visible[i];
// 		tmp+="\n";
                if(nf_Visible[i]>0){
                  for(j=0;j<prop_tmp.length;j++){
                    filter_tmp = prop_tmp[j];
                    if(nf_avail_filters[filter_tmp]){nf_avail_filters[filter_tmp]++;}
                    else{nf_avail_filters[filter_tmp] = 1;}
//                     tmp2+="filter "+filter_tmp+" set to "+nf_avail_filters[filter_tmp]+"\n";
                  };
                }
                // clear things up
		prop_tmp = Array();
		i++;
	}
//         alert("conditions for showing the items:\nFilters are marked as:"+tmp_active_filters+" (length of the array: "+filter_len+")\n"+tmp+"\nShowing '"+nf_Visible.length+"' items");
//         alert(tmp2);

//         alert(tmp_alert);

	nf_AS_WriteTable();
	return false;
}

function nf_AS_GetFilters(){ //Gets selected Filter settings
	var i=0;
	var e=0;
	nf_ActiveFilters.length=0; //start afresh
        nf_avail_filters = Array();
	while (i<nf_Filters.length){
		var ElemID = "checkbox" + nf_Filters[i]; //generate elementID
		if(document.getElementById(ElemID).checked==true){
			nf_ActiveFilters[e] = nf_Filters[i]; //If it's ticked, add it to the list.
			e=e+1;
		}
		i++;
	}
}

function nf_AS_WriteTable(){ //rewrites the table to show what should be visible, and hide what shouldn't be
	// nothing done if not possible
        if(!document.getElementById){return false;}
        var i=0;
	var vis=0;
	var num_shown = 0;
	var tmp ='';
	var ElemID;
	// make sure we have integers
	nf_AS_options['page'] = parseInt(nf_AS_options['page']);
	nf_AS_options['num_per_page'] = parseInt(nf_AS_options['num_per_page']);
	var num_start = ((nf_AS_options['page'])-1)*(nf_AS_options['num_per_page']);
	// show the new
	while (i<nf_ItemProps.length){
		// bnb
                ElemID = "Row" + i;
// 		tmp+=ElemID+' '+": "+nf_Visible[i]+"-->";
		if(nf_Visible[i]>=1){ //if it should be shown, show it.
		        tmp+='('+num_start+'<='+num_shown+') && (('+num_shown+') < ('+num_start+'+'+nf_AS_options['num_per_page']+'))';
			if((num_start<=num_shown) && ((num_shown) < (num_start+nf_AS_options['num_per_page']))){
                          document.getElementById(ElemID).style.display="";  //IE and Mozilla compatible
   			}
			// hide it if outside the curent page range
			else{document.getElementById(ElemID).style.display="none";}

   			num_shown++;
			vis=1;
// 			tmp+='visible';
		}
		else { //if not to be shown, hide it

			document.getElementById(ElemID).style.display="none";
// 			tmp+='hidden';
		}
		i++;
// 		tmp+="\n";
	}
	// update variables to now have the number shown as 'correct'
	nf_AS_options['num_shown'] = num_shown;
	// refactor the page links
	nf_AS_update_pages();

	if (vis==0){
		 //if there are  no items, show "no items found"
// 		 document.getElementById("NoItems").style.display=""; //IE and Mozilla compatible

	}
	else{
// 		 document.getElementById("NoItems").style.display="none";
	}
	nf_AS_rejig_filters();
//         alert(tmp);
	return true;
}

function nf_AS_update_pages(){
  var search_H = Array('<h4 style="display:inline;">','</h4>')
  var num_from = ((nf_AS_options['page'])-1)*((nf_AS_options['num_per_page']))+1;
//   alert(num_from+" --> "+((nf_AS_options['page'])-1)+"*"+((nf_AS_options['num_per_page']))+"+1");
  nf_AS_options['num_pages'] = Math.ceil(nf_AS_options['num_shown']/nf_AS_options['num_per_page']);
  var search_top_prev = '';var search_top_next = '';
  var onclick='nf_AS_update();return false';
  // make the GET URL have the right filters if the Javascript does not work well
  var filters='';
  var x,i;
  for(x in nf_ActiveFilters){
    filters+='&amp;filter['+x+']=1';
  }
  nf_AS_options['href'] = nf_AS_options['href']+''+filters;

  // if >=1, then we get full page amount.  If not, use a modulus
  var num_to = ((nf_AS_options['num_shown']/(nf_AS_options['page']*nf_AS_options['num_per_page']))>=1)?num_from-1+nf_AS_options['num_per_page']:(nf_AS_options['num_shown']%(nf_AS_options['num_per_page']*nf_AS_options['page']));

  // common
  var search_prev = (nf_AS_options['page']>1)?'<a onclick="nf_AS_options[\'page\'] = \''+(nf_AS_options['page']-1)+"';"+onclick+'" href="'+nf_AS_options['href']+'&amp;p_num='+((nf_AS_options['page'])-1)+'">'+search_H[0]+'&lt; previous'+search_H[1]+'</a>':'&nbsp;';
  var search_next = ((nf_AS_options['num_shown']-(nf_AS_options['page']*nf_AS_options['num_per_page']))>0)?'<a onclick="nf_AS_options[\'page\'] = \''+(nf_AS_options['page']+1)+"';"+onclick+'" href="'+nf_AS_options['href']+'&amp;p_num='+((nf_AS_options['page'])+1)+'">'+search_H[0]+'next &gt;'+search_H[1]+'</a>':'&nbsp;';

  // top
  var search_top_central = (nf_AS_options['num_shown']>0)?search_H[0]+'Results ('+num_from+'-'+num_to+' of '+nf_AS_options['num_shown']+')'+search_H[1]:"No results to show.  Please deselect some options and try again";

  // bottom
  var search_bottom_central = 'page';
  if((nf_AS_options['num_shown']>0)){
    search_bottom_central+=(nf_AS_options['num_pages']>1)?'s: ':': ';
    for(i=1;i<=nf_AS_options['num_pages'];i++){
      search_bottom_central += '<a onclick="nf_AS_options[\'page\'] = \''+(i)+"';"+onclick+'"href="'+nf_AS_options['href']+'&amp;p_num='+i+'"><h4';
      if(i==nf_AS_options['page']){search_bottom_central += ' class="search_curr_page"';}
      search_bottom_central += '>'+i+'</h4></a>, ';
    }
    ;
  }
  else{search_bottom_central='';}
  // remove trailing comma
  search_bottom_central=search_bottom_central.replace(/, ?$/,'');
  //?search_H[0]+'Results ('+num_from+'-'+num_to+' of '+nf_AS_options['num_shown']+')'+search_H[1]:"No results to show.  Please deselect some options and try again";


  // perform the updating
  if(document.getElementById){
    document.getElementById('search_top_central').innerHTML = search_top_central;
    document.getElementById('search_top_prev').innerHTML    = search_prev;
    document.getElementById('search_top_next').innerHTML    = search_next;
    document.getElementById('search_bottom_central').innerHTML = search_bottom_central;
    document.getElementById('search_bottom_prev').innerHTML    = search_prev;
    document.getElementById('search_bottom_next').innerHTML    = search_next;
  }
}

function nf_AS_rejig_filters(){
        // now show the numbers again, but this time, we need to add in the counts...
	for (var x=0;x<nf_avail_filters.length;x++){
//           tmp+=x+":  "

          if(document.getElementById('f_n_'+x)){
            // bring to life and enable
            setOpacity(document.getElementById('f_'+x),'100');
            document.getElementById('checkboxf_'+x).disabled=false;
//             tmp+="setting "+nf_avail_filters[x];
            if(nf_avail_filters[x] >0){
              document.getElementById('f_n_'+x).innerHTML = nf_avail_filters[x];
	    }
            // fade, disable, and set to blank
            else{
              setOpacity(document.getElementById('f_'+x),'20');
              document.getElementById('checkboxf_'+x).disabled=true;
              document.getElementById('f_n_'+x).innerHTML = '';
            }
          }
          // only show a certain number max
        }
}

// CREDIT to Shawn Olson of http://www.shawnolson.net
function changecss(theClass,element,value) {

//Last Updated on May 21, 2008
//documentation for this script at
//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
 var cssRules;
 if (document.all) {
  cssRules = 'rules';
 }
 else if (document.getElementById) {
  cssRules = 'cssRules';
 }
 var added = false;
 for (var S = 0; S < document.styleSheets.length; S++){
  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {

   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
    if(document.styleSheets[S][cssRules][R].style[element]){
    document.styleSheets[S][cssRules][R].style[element] = value;
    added=true;
	break;
    }
   }
  }
  if(!added){
  if(document.styleSheets[S].insertRule){
		  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
		} else if (document.styleSheets[S].addRule) {
			document.styleSheets[S].addRule(theClass,element+': '+value+';');
		}
  }
 }

}

// assumes all must be selected
function form_check_radios(theForm){
  var i,n,a;
  var not_selected = new Array();
  var alert_text = '';
  var failed = 0;
  alert_text+= "We've got '"+theForm.elements.length+"' items to deal 'check' over\n";
  // hold the current radio
  var tmp_name = '';
  // hold the checked status of the line
  var is_checked = 0;
  for (i=0,n=theForm.elements.length;i<n;i++){
    a = theForm.elements[i];
    if((a.name.length>2) && (a.type == 'radio')){ // now worth checking
      alert_text+="\nname: '"+a.name+"' vs '"+tmp_name+"' value: '"+a.value+"' checked?: '"+a.checked+"' vs '"+is_checked+"'\n";
      // first time
      if(tmp_name == ''){tmp_name = a.name;alert_text += 'first_time ';}
      // since requiring all to be checked, first failure is all that is needed
      if((tmp_name != a.name) && is_checked == 0){tmp_name = a.name;failed ++;alert_text += 'failed ';}
      // reset if past was ok
      else if((tmp_name != a.name)){tmp_name = a.name;is_checked = 0;alert_text += 'reset counter';}
      if(a.checked){is_checked++;alert_text += 'checked';}
//       if(a.checked !== check){a.checked = check;}
    }
  }
  // final check.
  if(is_checked == 0){failed ++;}

//   alert(alert_text);
  if(failed ==0){return true;}
  else{
    alert('Please choose one from each of the options before submitting the form');
    return false;
  }
}
