// JavaScript Document
function SubmitForm()
{
  document.thisform.submit();
}

function doRedirect(rid)
{
	if (rid == 1) { document.location = "../Admin/index.php"; }
}

function loginError()
{
	document.location = "../Login/error.php";
}

function verifyEmailPwd(pwd, rpwd, email, fname, lname)
{
	// Set variables.
	var start = email.value.lastIndexOf(".");
	var end = email.value.length;
	var emailEXT = email.value.slice(start, end);
	var iNums = "0123456789";
	var iChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var beforeAmp = iNums + iChars + "._-";
	var afterAmp = iNums + iChars + ".-";
	var baNum = email.value.indexOf("@") -1;  //number of characters before @
	var aftNum = baNum + 2;
	var bEXT = email.value.slice(0, baNum);  //characters before @
	var aEXT = email.value.slice(aftNum, start);  //characters after the @ and before the final extension.
	
	// Check for a value and no white spaces.
	if (email.value == '' || email.value.indexOf(" ") !== -1) {
		alert('Please enter a valid email address.');
		email.select();
		return false;
	}
	
	// Checks for white spaces, if @ sign exists and if more the one @ sign is in the email.
	if (email.value.indexOf(" ") !== -1 ||  //is in the email and should not be
		email.value.indexOf("@") == -1 ||  //is in email and should be
		email.value.indexOf("@") !== email.value.lastIndexOf("@")) {  //there are more than one @ in email
			alert('You have an invalid email address.\n\nERROR 001');
			email.select();
			return false;
	}
		
	// Invalid characters in the extension.
	if (end > 0) {
		for (i=1; i <= emailEXT.length; i++) {
			if (iChars.indexOf(emailEXT.charAt(i)) == -1) {
				alert('You have an invalid email address.\n\nERROR 002');
				email.select();
				return false;
			}
		}
	}
	
	// Check invalid characters before the @ sign
	if (baNum > 0) {
		for (i=0; i<= baNum; i++) {
			if (beforeAmp.indexOf(bEXT.charAt(i)) == -1 ||
				bEXT.substring(0,1) == "." ||
				bEXT.substring(0,1) == "-" ||
				bEXT.substring(0,1) == "_") {
				alert('You have an invalid email address.\n\nERROR 003');
				email.select();
				return false;
			}
		}
	}
	
	// Check invalid characters after the @ sign and before the final extension
	if (aftNum > 0) {
		for (i=0; i<= aftNum; i++) {
			if (afterAmp.indexOf(aEXT.charAt(i)) == -1) {
				alert('You have an invalid email address.\n\nERROR 004');
				email.select();
				return false;
			}
		}
	}
	
	if (document.thisform.pwd.value.length < 6) {
		alert("Your password must consists of a minimum of 6 characters.");
		document.thisform.pwd.select();
		return false;
	} else if (document.thisform.rpwd.value.length < 6) {
		alert("Your password must consists of a minimum of 6 characters.");
		document.thisform.rpwd.select();
		return false;
	} else if (pwd.value == '' || pwd.value.indexOf(" ") !== -1) {
		alert('Please enter a valid password. NO SPACES ALLOWED.');
		document.thisform.pwd.select();
		return false;
	} else if (rpwd.value == '' || rpwd.value.indexOf(" ") !== -1) {
		alert('Please retype the valid password again for verification. NO SPACES ALLOWED.');
		document.thisform.rpwd.select();
		return false;
		// Verify passwords entered are the same.
	} else if (document.thisform.pwd.value !== document.thisform.rpwd.value) {
		alert("The password fields are not the same.  Therefore, please check your entries and try again.");
		return false;
	} else if (fname.value == '' || fname.value.substring(0,1) == " ") {
		alert('Missing first name value or invalid white space at the beginning of the first name.');
		fname.select();
		return false;
	} else if (lname.value == '' || lname.value.substring(0,1) == " ") {
		alert('Missing last name value or invalid white space at the beginning of the last name.');
		lname.select();
		return false;
	} 
	
	if (doResubmit() !== true) { return false; }

return true;
}

/* This function is used to set the primary email account based on the user selection */
function doPrimary(radioObj)
{
	if (radioObj.checked == true) {
		document.thisform.setPrimary.value = radioObj.value;
		//alert(radioObj.value);
	}
}

/* This function verify additonal email accounts in the users profile*/
function verifyAddEmail(email)
{
	if (email.name !== 'em1' &&
		(email.value.length == 0 || email.value.length == null)) {
		return true;
	}
	// Set variables.
	var start = email.value.lastIndexOf(".");
	var end = email.value.length;
	var emailEXT = email.value.slice(start, end);
	var iNums = "0123456789";
	var iChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var beforeAmp = iNums + iChars + "._-";
	var afterAmp = iNums + iChars + ".-";
	var baNum = email.value.indexOf("@") -1;  //number of characters before @
	var aftNum = baNum + 2;
	var bEXT = email.value.slice(0, baNum);  //characters before @
	var aEXT = email.value.slice(aftNum, start);  //characters after the @ and before the final extension.
	
	// Checks for white spaces, if @ sign exists and if more the one @ sign is in the email.
	if (email.value.indexOf(" ") !== -1 ||  //is in the email and should not be
		email.value.indexOf("@") == -1 ||  //is in email and should be
		email.value.indexOf("@") !== email.value.lastIndexOf("@")) {  //there are more than one @ in email
			alert('You have entered an invalid email address.\n\nERROR 001A');
			email.select();
			return false;
	}
		
	// Invalid characters in the extension.
	if (end > 0) {
		for (i=1; i <= emailEXT.length; i++) {
			if (iChars.indexOf(emailEXT.charAt(i)) == -1) {
				alert('You have entered an invalid email address.\n\nERROR 002A');
				email.select();
				return false;
			}
		}
	}
	
	// Check invalid characters before the @ sign
	if (baNum > 0) {
		for (i=0; i<= baNum; i++) {
			if (beforeAmp.indexOf(bEXT.charAt(i)) == -1) {
				alert('You have entered an invalid email address.\n\nERROR 003A');
				email.select();
				return false;
			}
		}
	}
	
	// Check invalid characters after the @ sign and before the final extension
	if (aftNum > 0) {
		for (i=0; i<=aftNum; i++) {
			if (afterAmp.indexOf(aEXT.charAt(i)) == -1) {
				alert('You have entered an invalid email address.\n\nERROR 004A');
				email.select();
				return false;
			}
		}
	}

return true;

}

function doEmails(em1,em2,em3,em4,obj)
{
	if (verifyAddEmail(em1) == false) {
		return false;
	} else if (verifyAddEmail(em2) == false) {
		return false;
	} else if (verifyAddEmail(em3) == false) {
		return false;
	} else if (verifyAddEmail(em4) == false) {
		return false;
	}
	
	if (validatePrimary(obj,em1,em2,em3,em4) == false) {
		return false;
	}
	
return true;
	
}

function validatePrimary (obj,em1,em2,em3,em4)
{

	//alert(obj.value);
	//alert(em4.value.length);
	if (obj.value == 1 && em1.value.length == 0) {
			alert("Missing primary email address.");
			em1.select();
			return false;
	} else if (obj.value == 2 && em2.value.length == 0) {
			alert("Missing primary email address.");
			em2.select();
			return false;
	} else if (obj.value == 3 && em3.value.length == 0) {
			alert("Missing primary email address.");
			em3.select();
			return false;
	} else if (obj.value == 4 && em4.value.length == 0) {
			alert("Missing primary email address.");
			em4.select();
			return false;
	}
	
}

function doResubmit()
{
	
	if (document.thisform.newUser.value == 0) {
		if (checkRoles() == true) {
			if (document.thisform.submit2.value.length == 0) {
				document.thisform.submit2.value = 1;
				return true;
			} else {
				document.thisform.submit2.value = null;
				return true;
			}
		}
	} else {
		if (document.thisform.submit2.value.length == 0) {
			document.thisform.submit2.value = 1;
			return true;
		} else {
			document.thisform.submit2.value = null;
			return true;
		}
	}

}

function doAddUserFields(email,fname,lname,pwd,adm,app,fs,att,rev,s2)
{
	//alert("add user stuff");
	document.thisform.email.value = email;
	document.thisform.fname.value = fname;
	document.thisform.lname.value = lname;
	document.thisform.pwd.value = pwd;
	document.thisform.rpwd.value = pwd;
	document.thisform.submit2.value = s2;
	document.thisform.email.readOnly = true;
	document.thisform.fname.readOnly = true;
	document.thisform.lname.readOnly = true;
	document.thisform.pwdH.value = document.thisform.pwd.value;
	document.thisform.rpwdH.value = document.thisform.rpwd.value;
	document.thisform.pwd.readOnly = true;
	document.thisform.rpwd.readOnly = true;
	
	if (document.thisform.newUser.value == 0) {
		if (adm == 1) { document.thisform.r1.checked = true; }
		if (app == 2) { document.thisform.r2.checked = true; }
		if (fs == 3) { document.thisform.r3.checked = true; }
		if (att == 4) { document.thisform.r4.checked = true; }
		if (rev == 5) { document.thisform.r5.checked = true; }
	}
	
}

function checkRoles()
{
	if (document.thisform.r1.checked !== true &&
		document.thisform.r2.checked !== true &&
		document.thisform.r3.checked !== true &&
		document.thisform.r4.checked !== true &&
		document.thisform.r5.checked !== true) {
		alert("You must select at least on role.")
		return false;
	}
return true;
}

function resetNewUser(tableID)
{
	document.getElementById(tableID).style.display = 'none';
	document.thisform.email.disabled = false;
	document.thisform.fname.disabled = false;
	document.thisform.lname.disabled = false;
	document.thisform.pwd.disabled = false;
	document.thisform.rpwd.disabled = false;
}

function doPwd(obj)
{
	if (obj.name == 'pwd') {
		document.thisform.pwdH.value = obj.value;
	} else if (obj.name == 'rpwd') {
		document.thisform.rpwdH.value = obj.value;
	}
	
}

function impersonateUser(cid,host)
{
	if (cid.value.length == 0) {
		alert('Please select a name.');
		return false;
	}
	var url = "http://" + host + "/index.php?cid=" + cid.value;
	//alert(cid.value);
	window.location = url;
	//window.open(url, 'IU_window',"menubar=0,resizable=0,status=0,scrollbars=1,width=825,height=600");
	return false;
}

function doCID(id)
{
	document.thisform.CID.value = id.value;	
}

function goLastMonth(month, year)
{
	
	// If the month is January, decrement the year
	if(month == 1) {
		--year;
		month = 13;
	}
	
	document.location.href = 'calendar.php?month='+(month-1)+'&year='+year;
} 

function goNextMonth(month, year)
{
	
	// If the month is December, increment the year
	if(month == 12) {
		++year;
		month = 0;
	}

	document.location.href = 'calendar.php?month='+(month+1)+'&year='+year;
}

function doEntireMonth (al, month, year)
{
	//alert(al);
	if (al != 1) {
		location.href = 'calendar.php?month=' + month + '&year=' + year + '&all';
	} else {
		location.href = 'calendar.php?month=' + month + '&year=' + year;
	}
}

function doDays(mo, yr, days)
{
	var month = mo.value;
	var year = yr.value;
	//var days = document.thisform.startDay;
	var dd = new Date(year, month, 0);
	var num = dd.getDate();
	var day = days.value;
	//alert(num);
	
	for (i=1; i<=num;i++) {
		if (i == day) {
			days.options[i].selected = new Option(i,i);
		} else {
			days.options[i] = new Option(i,i);
		}
	}
	
	for (var x=1; x<=3; x++) {
		days.options[num+1] = null;
	}
}