// JavaScript document

//***************************************************************
// Function to check the data from the Contact.html form page
//***************************************************************
function checkData(w)
{
	// required field
	if (w.user_name.value == "")
	{	alert("Please enter your NAME.");
		w.user_name.focus();
		return false;
	}
	
	if (w.user_login.value == "")
	{	alert("Please enter ARCH login name.");
		w.user_login.focus();
		return false;
	}
	
	if(w.user_email && !validEmail(w.user_email.value, "Email address"))
	{	w.user_email.focus();
		return false;
	}	

	// required field
	if (w.issue_short_description.value == "" || w.issue_short_description.value == "A short description of the problem")
	{	alert("Please enter the ISSUE SHORT DESCRIPTION.");
		w.issue_short_description.focus();
		return false;
	}
	// required field
	if (w.issue_description.value == "")
	{	alert("Please enter the ISSUE DESCRIPTION.");
		w.issue_description.focus();
		return false;
	}
	

	return true;
}
//***************************************************************
// Fuction to check the data from the software_form.html Form
//***************************************************************
function checkData_Soft(w)
{
	if (w.staff_name.value == "")
	{	alert("Please enter your name.");
		w.staff_name.focus();
		return false;
	}

	if(w.staff_email && !validEmail(w.staff_email.value, "Email address"))
	{	w.staff_email.focus();
		return false;
	}	
	
	if (w.software_description.value == "")
	{	alert("Please fill out the Software Description box.");
		w.software_description.focus();
		return false;
	}

	if (! checkSoftwareGroup(w,'soft_name[]'))
	{
		return false;
	}
	
}

//****************************************************************
// Function to check the data in the COMPLAINTS form
//****************************************************************
function check_Complaints(w)
{
	//all the fields are optionals, but if user clicks on a checkbox,
	//they have to input the textarea of it
	if (w.cb_Login.checked) {
		if (w.tx_Login.value == "") {
			alert ("If Login checkbox selected, you need to input your comments");
			w.tx_Login.focus();
			return false;
		}
	}
	
	if (w.cb_Soft.checked) {
		if (w.tx_Soft.value == "") {
			alert ("If Software checkbox selected, you need to input your comments");
			w.tx_Soft.focus();
			return false;
		}
	}

	if (w.cb_Customer.checked) {
		if (w.tx_Customer.value == "") {
			alert ("If Customer checkbox selected, you need to input your comments");
			w.tx_Customer.focus();
			return false;
		}
	}

	if (w.cb_Labs.checked) {
		if (w.tx_Labs.value == "") {
			alert ("If Labs checkbox selected, you need to input your comments");
			w.tx_Labs.focus();
			return false;
		}
	}

	if (w.cb_Website.checked) {
		if (w.tx_Website.value == "") {
			alert ("If Website checkbox selected, you need to input your comments");
			w.tx_Website.focus();
			return false;
		}
	}

	if (w.cb_Other.checked) {
		if (w.tx_Other.value == "") {
			alert ("If Other checkbox selected, you need to input your comments");
			w.tx_Other.focus();
			return false;
		}
	}

	if (w.cb_Print.checked) {
		if (w.tx_Print.value == "") {
			alert ("If Printing checkbox selected, you need to input your comments");
			w.tx_Print.focus();
			return false;
		}
	}

	if (w.cb_Task.checked) {
		if (w.tx_Task.value == "") {
			alert ("If Task checkbox selected, you need to input your comments");
			w.tx_Task.focus();
			return false;
		}
	}
	
	return true;
}

//***************************************************************
// Check the input from staff_resquest.php form
//***************************************************************
function check_Request(w)
{
	if (w.request_login.value == "")
	{	alert("Please enter your ARCH Login name.");
		w.request_login.focus();
		return false;
	}
	
	if(w.request_email && !validEmail(w.request_email.value, "Email address"))
	{	w.request_email.focus();
		return false;
	}
	
	if (w.request_detail.value == "Be as detail as possible. Also, type the quantity here.")
	{	alert("Please enter the description.");
		w.request_detail.value = "";
		w.request_detail.focus();
		return false;
	}	

	//check for special date if entered
	if (w.request_datespecial.value) {
		var dt=w.request_datespecial
		if (isDate(dt.value)==false){
			dt.focus()
			return false
		}
	}
	return true
	//end new stuff
}

//**************************************************************
// EXTRA Functions
//**************************************************************

// this function will validate the checkboxes
function checkCheckboxGroup(theForm,theGroupname) 
{
	var theElements = theForm.getElementsByTagName("input")
	for (var i = 0; i < theElements.length; i++) 
	{
		if (theElements[i].type == 'checkbox' && theElements[i].name == theGroupname && theElements[i].checked)
		{
			return true;
		}
	}
	alert ('You need to select at least 1 Computer Lab.');
	return false;
}


function checkSoftwareGroup(theForm,theGroupname) 
{
	var theElements = theForm.getElementsByTagName("input")
	for (var i = 0; i < theElements.length; i++) 
	{
		if (theElements[i].type == 'text' && theElements[i].name == theGroupname && theElements[i].value)
		{
			return true;
		}
	}
	alert ('You need to input at least 1 Software.');
	return false;
}

function validEmail(e, t)
{	
	if (e == "")
	{
		alert("Please enter your "+t);
		return false;
	}
	var emailStr = e;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\*\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="([^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + specialChars + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{	alert("Your "+ t +" seems incorrect. Please check @ and .'s!");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null)
	{	alert("Please do not use any symbols (ie. quotes or asterisks) other than @ in your "+ t +".");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{  for (var i=1;i<=4;i++)
		  {	if (IPArray[i]>255)
			{	alert("Destination IP address is invalid!");
				return false;
			}
		}
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{	alert("Your "+ t +" domain name doesn't seem to be valid.");
		return false;
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{	alert("Your "+ t +" must end in a three-letter domain, or two letter country.");
		return false;
	}

	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (domArr[domArr.length-1].length == 2 || domArr[domArr.length-1].length == 3)
	{	for (var i = 0; i < domArr[domArr.length-1].length; i++)
		{	if (Chars.indexOf(domArr[domArr.length-1].charAt(i)) == -1)
			{	alert("The domain name can only contain letters.");
				return false;
			}
		}
	}

	if (len<2 || len>4)
	{	alert("Your "+ t +" is missing a hostname!");
		return false;
	}

	return true;
}

function validNums(n, t)
{	if (n == "")
	{	alert("Please enter a number in "+ t +".")
		return false;
	}
	else
	{	var Chars = "0123456789";
		for (var i = 0; i < n.length; i++)
		{	if (Chars.indexOf(n.charAt(i)) == -1)
			{	alert ("Please enter only numbers in "+ t +".")
				return false
			}
		}
	}
	
	return true;
}

function validName(n, t)
{	if (n == "")
	{	alert("Please enter a "+ t +".")
		return false;
	}
	else
	{	var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
		for (var i = 0; i < n.length; i++)
		{	if (Chars.indexOf(n.charAt(i)) == -1)
			{	alert ("Please enter a valid "+ t +".")
				return false
			}
		}
	}

	return true;
}


//************************************************************************8
// This function is to get the date and format it 
//************************************************************************

function todo_date()
{
	var dateStr;
	var d = new Date();
	
	var curr_year = d.getFullYear();
	var curr_month = d.getMonth();
	if (curr_month < 10) {
		curr_month = "0" + curr_month;
	}
	var curr_day = d.getDate();
	if (curr_day < 10) {
		curr_day = "0" + curr_day;
	}
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	if (curr_min < 10) {
		curr_min = "0" + curr_min;
	}
	var curr_sec = d.getSeconds();
	if (curr_sec < 10) {
		curr_sec = "0" + curr_sec;
	}
	
	dateStr = "" +  curr_year + curr_month + curr_day + curr_hour + curr_min + curr_sec;

	
	document.form_todo.todo_date.value = dateStr;	
	
	if (dis_Login.style.display == "") {
		dis_Login.style.display = "none";
	}
//	document.write(curr_year + curr_month + curr_day + curr_hour + curr_min + curr_sec);
//	document.write ("Test");
}

//************************************************************************
// This function will display or don't display the content on the Labs2.php
// I know, I know, this function has nothing to do with "form" content, but
// I didn't want to write the script inside the page
// code obtained from http://www.codeave.com/javascript/code.asp?u_log=7078
//*************************************************************************
function Show_Stuff(Click_Menu, Image_Little)
// Function that will swap the display/no display for
// all content within span tags
{
	if (Click_Menu.style.display == "none")
	{
		Click_Menu.style.display = "";
		if (Image_Little) {
			Image_Little.src = "images/little-plus-minus/minus.gif";
		}
	}
	else
	{
		Click_Menu.style.display = "none";
		if (Image_Little) {
			Image_Little.src = "images/little-plus-minus/plus.gif";
		}
	}
}

function Hide_All() 
{
	dis_Login.style.display = "none";
	dis_Soft.style.display = "none";
	dis_Customer.style.display = "none";
	dis_Labs.style.display = "none";
	dis_Website.style.display = "none";
	dis_Other.style.display = "none";
	dis_Print.style.display = "none";	
	dis_Task.style.display = "none";	
}


//***********************************************8
//extra function to check the date on staff request
//****************************************************

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=2005;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	//check the day for 2 digits
	if (strDay.length != 2) 
	{
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length != 2) 
	{
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
//	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
//	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
