/*
	Advanced Search Validator
	Dritan Xhabija	08/'06
	
	Validates the logic the user entered to search for an event.
	If any illogical input is found, validator stops the submission of the form
	until minimal logic is found.

*/
function advSearchValidator (){
	this.exec = exec;
}

function f_alert(msg) { alert (msg); return false; };

function exec(){
	var s_month, s_day, s_year, e_month, e_day, e_year;
	var sm = document.getElementById("start_month").value;
	var sd = document.getElementById("start_day").value;
	var sy = document.getElementById("start_year").value;
	var em = document.getElementById("end_month").value;
	var ed = document.getElementById("end_day").value;
	var ey = ey=document.getElementById("end_year").value;
	s_month = sm.length > 0; s_day = sd.length > 0; s_year = sy.length > 0; 
	e_month = em.length > 0; e_day = ed.length > 0; e_year = ey.length > 0; 
	
	/* From - Through Logic 
	*/ 
	var year_search = e_year && s_year;
	var month_search = e_month && s_month;
	var day_search = e_day && s_day;
	var isyear, ismonth; //is a year/month only search
	if (year_search){ //searching by years only, or incl. month or day or both
		if (ey < sy)
			verror('0',"Year"); //end year < start year
		if (month_search)
			if (em < sm && ey == sy)
			verror('0',"Month"); //end month < start month AND end year is same as start year
		if (day_search){
			//alert((ed < sd) + " m==m "+ (em == sm) + " y==y "+(ey == sy))
			if(ed < sd && em == sm && ey == sy)
			verror('0',"Day"); //end day < start day AND end month and year are the same as the start month and year
		}
	}
	if (!year_search && month_search) //searching by months only, year will be assumed to be current year in PHP
		if (em < sm)
			verror('0',"Month");
		
	return true;
	
}

function verror(numb, msg){
	if (numb==0)
			return f_alert("Can't search for an event which ends before the event starts. Fix Through "+msg);
	else if (numb==1)
			return f_alert("Can't search for days. A Month period must be present");
}

