/*###################################################
#Copyright (C) 2001 Stuart Wigley (stuartwigley@yahoo.co.uk) 
#
#general.js 18/04/2001   
#
#This file is part of workbench.
#
#workbench is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#workbench is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with workbench; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#####################################################
#this function validates forms for illegal characters and empty fields
#####################################################*/
function validateForm(name){
	var pattern = new RegExp(/[<>;]/);
	var theForm = eval("document." + name + ".elements");
	for (var i = 0; i < theForm.length; i++){
		 if (theForm[i].name != "file"){
		 	if (theForm[i].name != "resolution"){
				if(theForm[i].value == "" | theForm[i].value == null | theForm[i].value == "Choose"){
					alert("Please fill in all fields\n");
					return false;
					}
				}
			}
   		}
	if(theForm.theDay){
		return isDate(theForm.theDay.value, theForm.theMonth.value, theForm.theYear.value);
		}
	return true;
	}
/*#####################################################
#this function validates the new bug and edit bug forms
#####################################################*/
function validateBug(name){
	var theForm = eval("document." + name);
	if (name == "addbug"){
		if (!theForm.title.value){
			alert("Please enter a title for this bug.");
			return false;
			}
		else if (!theForm.description.value){
			alert("Please enter a description for this bug.");
			return false;
			}
		else if ((!theForm.app.value || theForm.app.value == "Choose") || (!theForm.appver.value || theForm.appver.value == "Choose") || (!theForm.mods.value || theForm.mods.value == "Choose")){
			alert("Please select a valid Application, Version and Module.");
			return false;
			}
		else if (!theForm.type.value || theForm.type.value == "Choose"){
			alert("Please select a type.");
			return false;
			}
		else if (!theForm.severity.value || theForm.severity.value == "Choose"){
			alert("Please select a severity.");
			return false;
			}
		else if (!theForm.foundIn.value || theForm.foundIn.value == "Choose"){
			alert("Please select where the bug was found.");
			return false;
			}
		else if (!theForm.assign.value || theForm.assign.value == "Choose"){
			alert("You must assign this bug to someone.");
			return false;
			}
		else if (theForm.theDay.value || theForm.theMonth.value || theForm.theYear.value){
			return isDate(theForm.theDay.value, theForm.theMonth.value, theForm.theYear.value);
			}
		}
	else if (name == "editbug"){
		if (!theForm.newresolution.value){
			alert("Please update the resolution for this bug.");
			return false;
			}
		else if (!theForm.type.value || theForm.type.value == "Choose"){
			alert("Please select a type.");
			return false;
			}
		else if (!theForm.severity.value || theForm.severity.value == "Choose"){
			alert("Please select a severity.");
			return false;
			}
		else if (!theForm.status.value || theForm.status.value == "Choose"){
			alert("Please select a status.");
			return false;
			}
		else if (!theForm.foundIn.value || theForm.foundIn.value == "Choose"){
			alert("Please select where the bug was found.");
			return false;
			}
		else if (!theForm.assign.value || theForm.assign.value == "Choose"){
			alert("You must assign this bug to someone.");
			return false;
			}
		else if (theForm.theDay.value || theForm.theMonth.value || theForm.theYear.value){
			return isDate(theForm.theDay.value, theForm.theMonth.value, theForm.theYear.value);
			}
		}
	else{
		return true;
		}
	}
/*######################################################
#this functions generates the dynamic select lists on the 'new bug' page
#######################################################*/
function dynamicSelects(theObject){
	 document.addbug.appver.length=0;
	 for(var x=0;x<versions[theObject.options[theObject.selectedIndex].value].length;x++){
		var newOption=new Option(versions[theObject.options[theObject.selectedIndex].value][x][0],versions[theObject.options[theObject.selectedIndex].value][x][1]);
		document.addbug.appver.options[document.addbug.appver.options.length]=newOption;
	 	};
		
	document.addbug.targetVer.length=0;
	 for(var x=0;x<versions[theObject.options[theObject.selectedIndex].value].length;x++){
		var newOption=new Option(versions[theObject.options[theObject.selectedIndex].value][x][0],versions[theObject.options[theObject.selectedIndex].value][x][0]);
		document.addbug.targetVer.options[document.addbug.targetVer.options.length]=newOption;
	 	};
	
	 document.addbug.mods.length=0;
	 for(var x=0;x<modules[theObject.options[theObject.selectedIndex].value].length;x++){
		var newOption=new Option(modules[theObject.options[theObject.selectedIndex].value][x][0],modules[theObject.options[theObject.selectedIndex].value][x][1]);
		document.addbug.mods.options[document.addbug.mods.options.length]=newOption;
	 	};
	return true;
	}
/*######################################################
#this functions generates the dynamic select lists on the 'summary' page
#######################################################*/
function dynamicSelects2(theObject){
	 document.summary.appver.length=0;
	 for(var x=0;x<versions[theObject.options[theObject.selectedIndex].value].length;x++){
		var newOption=new Option(versions[theObject.options[theObject.selectedIndex].value][x][0],versions[theObject.options[theObject.selectedIndex].value][x][1]);
		document.summary.appver.options[document.summary.appver.options.length]=newOption;
	 	};
	 document.summary.mods.length=0;
	 for(var x=0;x<modules[theObject.options[theObject.selectedIndex].value].length;x++){
		var newOption=new Option(modules[theObject.options[theObject.selectedIndex].value][x][0],modules[theObject.options[theObject.selectedIndex].value][x][1]);
		document.summary.mods.options[document.summary.mods.options.length]=newOption;
	 	};
	return true;
	}
/*######################################################
#this function opens a window
########################################################*/
function openWin(theURL){
	window.open(theURL,'','menubar=yes,scrollbars=yes,resizable=no,width=600,height=400');
	}
/*########################################################
#this function checks that a given date is not in the past, or invalid
#########################################################*/
function isDate (day,month,year){
    month--;
	var today = new Date();
    var test = new Date(year,month,day);	
	if ((month == test.getMonth()) && (day == test.getDate())){	
		if (year < today.getYear()){
			alert("Don't enter dates in the past. It upsets the developers.");
        	return false;
			}
		else if(year == today.getYear()){
			if(month < today.getMonth()){
				alert("Don't enter dates in the past. It upsets the developers.");
        		return false;
				}
			else if(month == today.getMonth()){
				if(day < today.getDate()){
					alert("Don't enter dates in the past. It upsets the developers.");
        			return false;		
					}
				else{
					return true;
					}
				}
			else{
				return true;
				}
			}
		else{
			return true;
			}
		}
	else{
		alert("Try entering a real date instead.");
        return false;
		}
	}