/* This script is for use on the Consultations page, so that when someone enters two valid dates for searching in a date range it automatically checks the 'Filter by date' checkbox for them.

Relies on jQuery.

*/

//TO DO - change element.style.color = 'red' to use .error-color class instead

var jq2 = jQuery.noConflict(); //make our own instancey thing of jQuery to stop us ruining other things


jq2(document).ready(function(){
		
	//advanced consultation finder search
	from_field = document.getElementById('fd');
	to_field = document.getElementById('td');
	if(from_field != null && to_field != null){ //if we're on a page that has both of those fields
		//check them, and set to red to show that they're wrong if nec.
		//check_stuff(from_field, to_field);
		
		//initial checks
		check_field(from_field);
		check_field(to_field);
		//blur_date will put in dd/mm/yyyy help text
		blur_date(from_field);
		blur_date(to_field);
		
		//set onchange functions to validate the date(s) and check chronology
		from_field.onchange = function(){ check_field(from_field); };
		to_field.onchange = function(){ check_field(to_field); };
		
		//set on focus stuff to remove default grey dd/mm/yyyy
		from_field.onfocus = function(){ focus_date(from_field); };
		to_field.onfocus = function(){ focus_date(to_field); };
		
		//set onblur stuff to put back grey dd/mm/yyyy if nec
		from_field.onblur = function(){ blur_date(from_field); };
		to_field.onblur = function(){ blur_date(to_field); };
	}
	
	
	//consult_edit page
	start = document.getElementById('form.startdate');
	end = document.getElementById('form.enddate');
	result = document.getElementById('form.resultdate');
	feedback = document.getElementById('form.feedbackdate');
	
	if(start != null & end != null){ //if we're on the /consult_edit page
		//initial check
		check_all(start, end, result, feedback);
		//blur_date will put in dd/mm/yyyy help text
		blur_date(start);
		blur_date(end);
		blur_date(result);
		blur_date(feedback);
		
		//change checks
		start.onchange = function(){ check_all(start, end, result, feedback); };
		end.onchange = function(){ check_all(start, end, result, feedback); };
		result.onchange = function(){ check_all(start, end, result, feedback); };
		feedback.onchange = function(){ check_all(start, end, result, feedback); };
		
		//focus checks
		start.onfocus = function(){ focus_date(start); };
		end.onfocus = function(){ focus_date(end); };
		result.onfocus = function(){ focus_date(result); };
		feedback.onfocus = function(){ focus_date(feedback); };
		
		//blur checks
		start.onblur = function(){ blur_date(start); };
		end.onblur = function(){ blur_date(end); };
		result.onblur = function(){ blur_date(result); };
		feedback.onblur = function(){ blur_date(feedback); };
		
		//clear any dd/mm/yyyy stuff when you submit the form
		document.getElementById('browser_form').onsubmit = function(){
			clear_default_text(start);
			clear_default_text(end);
			clear_default_text(result);
			clear_default_text(feedback);
		}
	}
});


function check_field(field){//called when a field isFinite changed - checks its validity and marks appropriately
	
	un_invalidate(field); //incase the field is invalid and has been changed using the datepicker so onkeydown didn't get called
	
	if(!is_date_valid(field.value)){
		mark_invalid(field);
		return;
	}
}

function check_all(field1, field2, field3, field4){//called when a date fields is changed - checks all fields for chronology
	
	//set them all to ok (if one was invalid and was changed by the datepicker then onkeydown wont have triggered)
	mark_black(field1);
	mark_black(field2);
	mark_black(field3);
	mark_black(field4);
	
	
	value1 = field1.value;
	value2 = field2.value;
	value3 = field3.value;
	value4 = field4.value;
	
	//check fields 1 and 2 (and maybe 3 and 4)
	if(!is_date_valid(value1)) //if the first field is invalid
		mark_invalid(field1); //make it red
	else if(is_date_valid(value2)){ //else if we can compare to field 2
		if(!check_chronology(value1, value2)){ //if 1 and 2 are not in order
			mark_invalid(field1); //mark them both as invalid
			mark_invalid(field2);
		}
	}
	else if(is_date_valid(value3)){ //else if we can compare to the next field
		if(!check_chronology(value1, value3)){ //if 1 and 3 are not in order
			mark_invalid(field1); //mark them both as invalid
			mark_invalid(field3);
		}
	}
	else if(is_date_valid(value4)){ //else if we can compare to the next field
		if(!check_chronology(value1, value4)){ //if 1 and 4 are not in order
			mark_invalid(field1); //mark them both as invalid
			mark_invalid(field4);
		}
	}
	
	//check fields 2 and 3 (and maybe 4)
	if(!is_date_valid(value2)) //if the first field is invalid
		mark_invalid(field2); //make it red
	else if(is_date_valid(value3)){ //else if we can compare to field 3
		if(!check_chronology(value2, value3)){ //if 2 and 3 are not in order
			mark_invalid(field2); //mark them both as invalid
			mark_invalid(field3);
		}
	}
	else if(is_date_valid(value4)){ //else if we can compare to the next field
		if(!check_chronology(value2, value4)){ //if 2 and 4 are not in order
			mark_invalid(field2); //mark them both as invalid
			mark_invalid(field4);
		}
	}
	
	//check fields 3 and 4
	if(!is_date_valid(value3)) //if the first field is invalid
		mark_invalid(field3); //make it red
	else if(is_date_valid(value4)){ //else if we can compare to field 4
		if(!check_chronology(value3, value4)){ //if 3 and 4 are not in order
			mark_invalid(field3); //mark them both as invalid
			mark_invalid(field4);
		}
	}
	
	//check field 4
	if(!is_date_valid(value4)) //if the first field is invalid
		mark_invalid(field4); //make it red
	//comparison to field 3 already done
	
	
}




function focus_date(field){ //called when a date field is focussed
	if(field.value.match(/^ *dd\/mm\/yyyy *$/)){ //if it's still got dd/mm/yyyy in it
		field.value = ''; //clear it
		mark_black(field);
	}
}




function blur_date(field){
	if(field.value.match(/^ *$/)){ //if the field is empty
		field.value = 'dd/mm/yyyy'; //put dd/mm/yyyy back in
	}
	if(field.value.match(/^ *dd\/mm\/yyyy *$/)){ //if it's still got dd/mm/yyyy in it
		mark_grey(field);
	}
}




function is_date_valid(date_string){ //is the date in dd/mm/yyyy format?
	date_pattern = /^((0[1-9])|((1|2)[0-9])|(3[0-1])|[1-9])\/(0[0-9]|1[0-2]|[1-9])\/[1-2][0-9]{3}/g; //bite me
	if(date_string.match(date_pattern))
		return true;
	else
		return false;
}


function check_chronology(first_date, second_date, reverse_chronology){//checks to make sure that the two dates are in chronological order
	//pass in 2 dates in dd//mm/yyyy format
	var date1 = new Date();
	var date2 = new Date();
	date1.setFullYear(first_date.substr(6,4),first_date.substr(3,2),first_date.substr(0,2));
	date2.setFullYear(second_date.substr(6,4),second_date.substr(3,2),second_date.substr(0,2));
	if(date1.getTime()<date2.getTime())
		return true;
	else if(reverse_chronology)
		return true
	else
		return false
}

function mark_invalid(field){
	if(field.value.match(/^ *dd\/mm\/yyyy *$/)){ //if it's actually just default text
		mark_grey(field)
		return;
	}
	field.style.color = 'red';
	field.onkeydown = function(){ un_invalidate(field); }; //set an on keydown so that the invalid colouring is removed when they start typing in it again
}

function un_invalidate(field){
	field.style.color = '';
	field.onkeydown = '';
}

function mark_black(field){
	field.style.color = '';
}

function mark_grey(field){
	field.style.color = '#999999';
}

function clear_default_text(field){
	if(field.value.match(/^ *dd\/mm\/yyyy *$/))
		field.value = '';
}
