/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Script Name: jverify.inc
' Created On: 4/11/2000
' Author: Sequoia
'
' PURPOSE: Javascript functions for validating user entered values
'
' History: 
'         Sequoia, 4/11/2000
'         Initial creation
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/



/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Name:    isValidPwd
'Purpose: Performs password security check.
'		  Password must contain at least 1 non-alpha character.	
'			
'Input:   Password entered by user
'
'Output:  True if passed security check, else false
'
'Calls: None
'
'Called By: Verify
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
var validCharsUserID = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
var validCharsFirstName = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -.";
var validCharsLastName = validCharsFirstName + "\',";

function isValidPwd(password)
{
	count = 0
	if (password.length >= 7)
	{
		for (i = 0; i < password.length; i++)
		{
			if ((password.charAt(i) >= 'a' && password.charAt(i) <= 'z')||(password.charAt(i) >= 'A' && password.charAt(i) <= 'Z'))
				count++;
		}
	}
	else {return false;}

	if (count == password.length)
	{	return false;
	} else 
	{
		return true;
	}
}

function VerifyPwd()
{
	var strUserPwd = Trim(document.form2.userpassword.value);
	var strVerifyPwd = Trim(document.form2.verifypassword.value);

	if (strUserPwd.length > 0)
	{
		if (strUserPwd != strVerifyPwd) 
		{
			alert("Passwords do not match, please re-enter.");
			document.form2.userpassword.value = '';
			document.form2.verifypassword.value = '';
			return false;
		}
		if(!isValidPwd(strUserPwd))
		{
			alert("Your password must be at least 7 characters and contain at least 1 non-alphabetic character.");
			document.form2.userpassword.value = '';
			document.form2.verifypassword.value = '';
			return false;
		}        
	}
	return true;
}

function ValidateEmail(strEmail)
{
	//var strEmailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
	var strEmailReg = /^[a-zA-Z0-9._\'-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var objRegExp = new RegExp(strEmailReg);
	return objRegExp.test(strEmail);
}

/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Name:	  VerifyCommonFields1
'Purpose: Validates common fields on user details pages
'Input:   None
'Output:  True if all required fields are populated and meet validation
'		  rules, else false.
'Calls: None
'Called By:  Submit on profile/Editprofile, secAdmin/profile1,
			 Admin/profile1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
function VerifyCommonFields1()
{
	// Name
	sGivenName = Trim(document.form2.givenName.value);
	if (sGivenName.length <= 0)
	{
		alert("The first name must be entered.");
		return false;
	}
	else
	{
		if (!ValidateObject(sGivenName, validCharsFirstName))
		{
			alert("The first name entered must be alphabetic.")
			document.form2.givenName.focus()
			return false;
		}
	}
	
	sSn = Trim(document.form2.sn.value);
	if (sSn.length <= 0)
	{
			alert("The last name must be entered.");
			return false;
	}
	else
	{
		if (!ValidateObject(sSn, validCharsLastName))
		{
			alert("The last name entered must be alphabetic.")
			document.form2.sn.focus()
			return false;
		}
	}	
		// Address
	if (Trim(document.form2.Street.value).length <= 0)
	{
			alert("Please enter the street address.");
			return false;
	}
	if (Trim(document.form2.l.value).length <= 0)
	{
			alert("Please enter the city.");
			return false;
	}

	iStSelectedIndex = document.form2.st.selectedIndex;
	if ((iStSelectedIndex == 0) && (document.form2.st[iStSelectedIndex].text == "Other"))
	{ 
		window.open("Province.asp", "Province", "menubar=no, width=250, height=200");
		return false;
	}
	
	if (Trim(document.form2.PostalCode.value).length <= 0)
	{
			alert("Please enter the zip code.");
			return false;
	}	
	
	len = Trim(document.form2.TelephoneNumber.value).length;
	if (len <= 0)
	{
			alert("Please enter the telephone number.");
			return false;
	}
	else
	{
		validChars = "( )-.0123456789";
		if(len < 10)
		{
			alert("Phone number is not the correct length. Please include the area code.")
		    document.form2.TelephoneNumber.focus()
		    return false;
		}
		for(i=0; i<len; i++)
   		{
			if (validChars.indexOf(document.form2.TelephoneNumber.value.charAt(i))<0)
			{
				alert("The telephone number must be numeric.")
				document.form2.TelephoneNumber.focus()
				return false;
			}
		}
	}
	
	mobile = Trim(document.form2.officemobilephone.value).length;
	if (mobile > 0)
	{
		if(mobile < 10)
		{
			alert("The mobile number must include the area code and be numeric.")
			document.form2.officemobilephone.focus()
			return false;
		}
		for(i=0; i<mobile; i++)
   		{
			if (validChars.indexOf(document.form2.officemobilephone.value.charAt(i))<0)
			{
				alert("The mobile number must be numeric.")
				document.form2.officemobilephone.focus()
				return false;
			}
		}
	}
		
	fax = Trim(document.form2.facsimileTelephoneNumber.value).length;
	if (fax > 0)
	{
		if(fax < 10)
		{
			alert("The fax number must include the area code and be numeric.")
			document.form2.facsimileTelephoneNumber.focus()
			return false;
		}
		for(i=0; i<fax; i++)
   		{
			if (validChars.indexOf(document.form2.facsimileTelephoneNumber.value.charAt(i))<0)
			{
				alert("The fax number must be numeric.")
				document.form2.facsimileTelephoneNumber.focus()
				return false;
			}
		}
	}

	// Email address
	sEmailValue = Trim(document.form2.mail.value);
	mail = sEmailValue.length;
	if (mail > 0)
	{
		if (!ValidateEmail(sEmailValue))
		{
			alert("This is an invalid email address. Email address example: johndoe@address.com");
			return false;
		}
	} 
	else 
	{
		alert("Please enter an email address.");
		return false;
	}
	
	return true;
}


/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Name:	  VerifyProf1
'Purpose: String concatenation for membership container
'
'Input:   bPwd indicates if password need to be verified or not
'
'Output:  True if all required fields are populated and meet validation
'		  rules, else false.
'
'Calls: None
'
'Called By:  Submit on profile/Editprofile, secAdmin/profile1,
			 Admin/profile1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/
function VerifyProf1(bPwd)
{
	var blnVerified = VerifyCommonFields1();
	if ((blnVerified) && (bPwd))
	{
		blnVerified = VerifyPwd();
	}
	
	return blnVerified;
}	

function VerifyEmp1()
{
	var blnVerified = VerifyCommonFields1();
	
	if (blnVerified)
	{
		if (document.form2.CustomerID.selectedIndex == 0)
		{ 
			alert("Please select the DDC location.");
			return false;
		}
	}
	return blnVerified;
}
	
function VerifyEmp2()
{
	if (Trim(document.form2.CompanyName.value).length <= 0)
	{
		alert("Please enter the Company name.");
		return false;
	}
	
	return true;		
}

function VerifyProf2(bDDCAdmin)
{
	if (Trim(document.form2.CompanyName.value).length <= 0)
	{
		alert("Please enter the Company name.");
		return false;
	}
	if (Trim(document.form2.CustomerID.value).length <= 0)
	{
			alert("Please enter the Customer ID.");
		return false;
	}
	var temp = 0;
	for (Count = 0; Count < document.form2.ApplicationID.length; Count++) 
	{
		if (document.form2.ApplicationID[Count].selected)
			temp += 1;
	}
	if (temp < 1)
    {		
		alert("Please select at least one Primary Group ID.");
		return false;
	}
	
	//if function called from Admin folder, check for Group selection also
	if (bDDCAdmin)
	{
		var temp = 0;
		for (Count = 0; Count < document.form2.GROUPS.length; Count++) 
		{
			if (document.form2.GROUPS[Count].selected)
				temp += 1;
		}
		if (temp < 1)		         
		{		
			alert("Please select the required Group IDs.");
			return false;
		}
	}

	return true;		
}

/*
	Name: Trim
	Purpose: Removes preceeding and trailing spaces from the string
	Inputs: any string
	Outputs: trimmed string
	Called By: any function that processes string variables
	Update: jroyze01 9/07/07 Initial creation
*/
function Trim(astrText)
{
	var strTrim = astrText;
	while (strTrim != strTrim.replace(' ', ''))
	{
		strTrim = strTrim.replace(' ', '');
	}	
	return(strTrim);
}

function VerifyName(objForm)
{		
	var elementCount;
	var newObj, objName, objVal;
	var validChars;
	
	//get all form elements
	elementCount = objForm.length;
	for (var i = 0; i < elementCount; i++)
	{
		newObj = objForm.elements[i];
		objName = newObj.name;
		if (newObj.type == "text")
		{
			objVal = Trim(newObj.value);
			objLen = objVal.length;
			if (objLen <= 0)
			{
				alert("First name and last name must be entered.");
				return false;
			}
			else
			{
				if (objName == "givenName")
				{
					validChars = validCharsFirstName;
				}
				else if (objName == "sn")
				{
					validChars = validCharsLastName;
				}

				if (!ValidateObject(objVal, validChars))
				{
					alert("The name entered must be alphabetic.")
					newObj.focus();
					return false;
				}
			}
		}
	}
}

function ValidateObject(objValue, validChars)
{
	for(var j = 0; j < objValue.length; j++)
   	{
   		if (validChars.indexOf(objValue.charAt(j)) < 0)
		{
			return false;
		}
	}
	return true;
}

