
// Validate and submit form
// ************************
function validate(){
	send = true;
	with(document.frm_criteria){
		
		// check if output selected, if not ask for it
		result = check_output();
		if(!result){return false;}
		
		
		// Build Query Statement for Duration
		StrDuration = "";
		if(SQLStartDate.value.indexOf("!") == -1)
			StrDuration = StrDuration + SQLStartDate.value;
		if(SQLEndDate.value.indexOf("!") == -1)
			StrDuration = StrDuration + SQLEndDate.value;
		if(SQLYearDuration.value.indexOf("!") == -1)
			StrDuration = StrDuration + SQLYearDuration.value;
		
		duration.value = StrDuration;
		
		
		// if Output is 'table', define first selected field as default order criteria
			for(i = 0; i < field.length; i++){
				if(field[i].checked == true){
					OrderBy.value = field[i].value + " ASC";
					break;
					}
				}
		
		/*	Agency, Sector or Province selected?
			(if not function check_criteria sets default to 'all'
			and returns the name of the criteria) */
//		str = check_criteria("agency,sector,province");
//		if(str.length > 0){
//			proceed = confirm("\nShall the database search for projects \nof each " + str + "?");
//			if(! proceed){return false;}
//			}
		
		// set form action according to output-selection
		if(output[0].checked){action = "table.asp";target = "WinResult";}
		if(output[1].checked){action = "download.asp";target = "_self"}
		if(output[2].checked){action = "map.asp";target = "WinResult";}
		
		
		// ...if more than one is selected, ask which criteria should be visualized by map
		if(output[2].checked)
			{
			// set default if no pattern selected
			if(!criteria_1[0].checked && !criteria_1[1].checked && !criteria_1[2].checked)
				{
				criteria_1[0].checked = true;
				criteria_2[1].checked = true;
				}
			if(criteria_1[0].checked && (!criteria_2[0].checked && !criteria_2[1].checked))
				criteria_2[0].checked = true;
			
			if(criteria_1[1].checked && (!criteria_2[2].checked && !criteria_2[3].checked))
				criteria_2[2].checked = true;
				
			if(criteria_1[2].checked && (!criteria_2[4].checked && !criteria_2[5].checked))
				criteria_2[4].checked = true;
			}
		
		//WinResult = window.open("about:blank","WinResult","location=no,menubar=no,resizable=yes,top=0,left=0,width=" + (screen.availWidth-12) + ",height=" + (screen.availHeight));
		if((!window["WinResult"] || window["WinResult"].closed) && !output[1].checked)
			{
			if(output[0].checked)
				{
				WindowHeight = 500;
				WindowWidth = 800;
				}
			else
				{
				if(screen.width > 1024)
					{
					WindowHeight = 768;
					WindowWidth = 1024;
					}
				else
					{
					WindowHeight = screen.availHeight - (screen.Height - screen.availHeight);
					WindowWidth = screen.availWidth;
					}
				}
			WindowPositionLeft = Math.abs(((screen.availWidth - (screen.Width - screen.availWidth)) - WindowWidth) / 2);
			WindowPositionTop = Math.abs(((screen.availHeight - (screen.Height - screen.availHeight)) - WindowHeight) / 2);
			WinResult = window.open("about:blank","WinResult","width=" + WindowWidth + ",height=" + WindowHeight + ",left=" + WindowPositionLeft + ",top=" + WindowPositionTop + ",resizable=yes,scrollbars=yes,location=no,menubar=no");
			}
		document.frm_criteria.submit();
		
		}
	}


// Event: Click on Agency-, Sector or Location filter criteria
// ***********************************************************
//		(c = criteria type (agency, sector etc.), x = index value of form element)
function select_box(c, x){
	with(document.frm_criteria){
		// if 'all' gets selected, execute function 'select_all)
		if(x == 0){
			select_all(c);
			}
		else{
			// if criteria gets selected...
			if(elements[c][x].checked == true){
				
				// ...then unselect 'all'
				elements[c][0].checked = false;
				}
			// if criteria gets unselected...
			else{
				//...check if there are other criteria of the same type selected
				NoValue = true;
				for (i = 1; i < elements[c].length; i++){
					if(elements[c][i].checked){
						NoValue = false;
						break;
						}
					}
				//...and if nothing selected, then select 'all'
				if(NoValue){elements[c][0].checked = true}
				}
			}
		}
	}


// Click on checkbox 'Show Map'
// ****************************
function select_map(){
	with(document.frm_criteria){
		
	// if checkbox 'Map' gets selected...
		if(output[2].checked){
			
			//...then unselect checkboxes 'Web Document' and 'excel download'
			output[0].checked = false;
			output[1].checked = false;
			
			// ...and disable 'Display content' fields
			for (i = 0; i < field.length; i++){
				field[i].disabled = true;
				}
				criteria_1[0].disabled = false;
				criteria_1[1].disabled = false;
				criteria_1[2].disabled = false;
				criteria_2[0].disabled = false;
				criteria_2[1].disabled = false;
				criteria_2[2].disabled = false;
				criteria_2[3].disabled = false;
				criteria_2[4].disabled = false;
				criteria_2[5].disabled = false;
			}
		
		// if checkbox 'Map' gets unselected...
		else{
			
			// ...then disable checkbox 'Criteria Fields'
			criteria_1[0].disabled = true;
			criteria_1[1].disabled = true;
			criteria_1[2].disabled = true;
			criteria_2[0].disabled = true;
			criteria_2[1].disabled = true;
			criteria_2[2].disabled = true;
			criteria_2[3].disabled = true;
			criteria_2[4].disabled = true;
			criteria_2[5].disabled = true;
			}
		}
	}


function SP(C1d,C1s,C2d)
	{
	// C1d = disselect Criteria 1 checkboxes
	// C1s = select Criteria 1 checkboxes
	// C2d = disselect Criteria 2 checkboxes
	AC1d = C1d.split(",");
	AC2d = C2d.split(",");
	
	//alert(AC1d.length);
	for(var i = 0; i <= AC1d.length-1; i++)
		document.frm_criteria.criteria_1[AC1d[i]].checked = false;
	
	if(C1s != "")
		document.frm_criteria.criteria_1[C1s].checked = true;
	
	for(var i = 0; i <= AC2d.length-1; i++)
		document.frm_criteria.criteria_2[AC2d[i]].checked = false;
	}
	
// Click on checkbox 'Web document' or 'Excel download'
// ****************************************************
function select_table(x){
	with(document.frm_criteria){
		
		// if checkbox 'Web document' or 'Excel download' gets selected...
		if(output[x].checked){
			
			// ...then unselect other 'output' checkboxes
			if(x == 0){output[1].checked = false;}
			if(x == 1){output[0].checked = false;}
			output[2].checked = false;
			
			// ...and enable 'Display content'-fields
			if(field[0].disabled){
				for (i = 0; i < field.length; i++){
					field[i].disabled = false;
					}
				}
			criteria_1[0].disabled = true;
			criteria_1[1].disabled = true;
			criteria_1[2].disabled = true;
			criteria_2[0].disabled = true;
			criteria_2[1].disabled = true;
			criteria_2[2].disabled = true;
			criteria_2[3].disabled = true;
			criteria_2[4].disabled = true;
			criteria_2[5].disabled = true;
			}
		
		// if checkbox 'Web document' or 'Excel download' gets UNselected...
		if(! output[x].checked){
			
			//...then disable 'Display content'-fields
			for (i = 0; i < field.length; i++){
				field[i].disabled = true;
				}
			}
		}
	}


// Open new Window
// ***************
function OpenWindow(FileName,WindowName,WindowWidth,WindowHeight,Scrollbars){
	//alert("verf&uuml;gbar: " + screen.availHeight + " / total: " + screen.height);
	WindowPositionLeft = Math.abs((screen.availWidth - WindowWidth) / 2);
	WindowPositionTop = Math.abs((screen.availHeight - WindowHeight) / 2);
	win = window.open(FileName,WindowName,"width=" + WindowWidth + ",height=" + WindowHeight + ",left=" + WindowPositionLeft + ",top=" + WindowPositionTop + ",resizable=yes,scrollbars=" + Scrollbars);
	win.focus();
	}


// Open Pop-up window for 'Other Criteria'
// ***************************************
function OpenPopup(FileName,WindowName,WindowWidth,WindowHeight,Scrollbars)
	{
	OpenWindow("about:blank",WindowName,WindowWidth,WindowHeight,Scrollbars);
	
	with(document.frm_criteria)
		{
		action = FileName;
		target = WindowName;
		submit();
		}
	}
	

// Clear value of Checkbox if disselected
// **************************************
function ClearValue(FieldName)
	{
	if(FieldName.indexOf("Date") != -1 || FieldName.indexOf("Duration") != -1)
		{
		with(document.frm_criteria)
			{
		if(elements[FieldName].checked == false)
			elements["SQL" + FieldName].value = "!" + elements["SQL" + FieldName].value;
		else
			elements["SQL" + FieldName].value = elements["SQL" + FieldName].value.replace(/!/,"");
			}
		}
	else
		{
		if(document.frm_criteria.elements[FieldName].checked == false)
			document.frm_criteria.elements[FieldName].value = "";
		}
	}


// Sub-Functions
// *******************************************************************************

// (Sub-Function of 'select_box'-Function)
// *************************************
//		Event: Click on checkbox 'all' (Agencies or Sector)
		function select_all(c){
			with(document.frm_criteria){
				
				// if criteria 'all' gets selected...
				if(elements[c][0].checked){
					
					//...then delete all other selections
					for (i = 1; i < elements[c].length; i++){
						elements[c][i].checked = false;
						}
					}
				}
			}


// Sub-Function of 'validate'-Function
// ***********************************
//	Action: Checks if kind of data output has been defined
//			and if not, opens pop-up window to define it.
	function check_output(){
		with(document.frm_criteria){
			
			// if none of the output options is selected...
			if(! output[0].checked && ! output[1].checked && ! output[2].checked){
				win_width = 300;
				win_height = 200;
				win_left = Math.abs((screen.availWidth - win_width) / 2);
				win_top = Math.abs((screen.availHeight - win_height) / 2);
				win = window.open("Err_NoOutput.htm","DefineOutput","width=" + win_width + ",height=" + win_height + ",left=" + win_left + ",top=" + win_top + ",resizable=yes");
				win.focus();
				return false;
				}
			else{return true;}
			}
		}

//	Action: Checks if selections have been made for filter-criterias
//			Agency, Sector, Province. If not, set default to 'all'
	function check_criteria(criteria){
		arr = criteria.split(",");
		result = "";
		// loop trough criterias
		for(x = 0; x < arr.length; x++){
			crit = arr[x];
			with(document.frm_criteria){
				selected = false;
				// loop through items of a criteria
				for(i = 0; i < (elements[crit].length); i++){
					if(elements[crit][i].checked == true){
						selected = true;
						break;
						}
					}
				if(selected == false){
					elements[crit][0].checked = true;
					result = result + "," + crit;
					}
				}
			}
		result = convert_list(result);
		return result;
		}

//	Action:	Convert a comma delimited list into a readable string
	function convert_list(list){
		if(list != ""){
			// remove leading commas
			if(list.indexOf(",") == 0){
				list = list.slice(1,list.length);
				}
			// replace last comma with 'and'
			lio = list.lastIndexOf(",");
			if(lio > 0){
				slice1 = list.slice(0,lio);
				slice2 = list.substring(lio,(list.length));
				slice2 = slice2.replace(/,/," and ");
				list = slice1 + slice2;
				}
			// replace ',' with ', '
			list = list.replace(/,/,", ");
			}
		return list;
		}

