Date.first_day_of_week = 1;
Date.weekdays = $w("M D M D F S S");
strShowAll = "Show All";
strFilterResults = "Filter Results";

 var d = new Date();
function changeAffid(hostel){
    if (hostel == 4643) {$('affid').value = 5;$('currency').value = 'GBP'; }
    else if (hostel == 16672){$('affid').value = 16672;$('currency').value = 'DKK';}
    else {$('affid').value = hostel;$('currency').value = 'EUR';}
}
function changeDate(dateString){
	
	var dateArray = dateString.split('/');
	var new_date = dateArray[1]+'/'+dateArray[0]+'/'+dateArray[2];
	$('arrival_new').value = new_date;

}

//the following function replaces checkHostelSet, and also checks Affid and Date are as they should be in case 
// user has come 'back' from PH site
function doubleCheckFields(submission){
   
  	if (submission.elements["hostel"].value != 0){
    		changeAffid(submission.elements["hostel"].value);
    		changeDate($("arrival").value);
    		var d= new Date($("arrival_new").value);
    		if (isNaN(d.getTime())){
        		alert("Please provide a valid date in the format DD/MM/YYYY.");
        		return false;
    		}else{
               		pageTracker._linkByPost(submission);
		        return true;
    		}
  	}else {
      		alert("Please select a hostel.");
      		return false;
  	}
}


function doubleCheckFields2(submission){
  if (submission.elements["hostel"].value != 0){
    if (submission.elements["affid"].value == 0) {
  	changeAffid(submission.elements["hostel"].value);
  }
  if ($('arrival_new').value == (d.getMonth())+'/'+d.getDate()+'/'+d.getFullYear()){
  	changeDate($("arrival").value);
  
  }
    pageTracker._linkByPost(submission)
    return true;
  }
  else{
    alert("Please select a hostel.");
    return false;
  }
}

	
jQuery(document).ready(function(){
// David Lockie Additions
	// automatically add arrows to 2nd level menu items that have children
	jQuery.each(jQuery("#access ul.sub-menu li ul.sub-menu").get(),function() {
		var str = jQuery(this).prev("a:first").text();
		jQuery(this).prev("a:first").html(str + ' &raquo;');
	});


//	jQuery('select[name=hostel]').val(pieSwitchedAffId);
	var options =  [];
	var optstr = '<option value="show-all">'+strShowAll+'</option>';
	var i = 0;	
	jQuery.each(jQuery('.listing').get(),function(key,value){
	jQuery.each(value.className.split(/\s/),function(key2,value2){
	if (value2 !== 'listing' && value2 !== 'entry-summary') {			
			options[i++]=value2;
			}
		});
	});
	options = options.unique();
	options = options.sort();

if (options.length > 1){
	jQuery.each (options,function(key3,value3){
		
		optstr += '<option value="'+value3+'">'+value3.replace('-',' ').replace(/^(.)|\s(.)/g, function ($1) {return $1.toUpperCase();})+'</option>';

	});
	jQuery('ol:first').before('<form style="float:right;"><label>' + strFilterResults + ': <select id="EventsFilter">' + optstr + '</select></label></form>');
	
	}
	jQuery('select#EventsFilter').change(function(){
	if(jQuery(this).val() === 'show-all'){
		jQuery('.listing').parent().show().next().show();
		}else{
		jQuery('.listing').parent().hide().next().hide();
		}
		jQuery('.'+jQuery(this).val()).parent().show().next().show();
		
	});

});

// Return new array with duplicate values removed
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

