//formfuncs.js $//
////
//*****************  Version 11  *****************//
//User: Rmj          Date: 11/14/03   Time: 2:48p//
//Updated in $/nrvl_corp.dev.elogiclearning.com_Web/includes//
//Initialize Keyword Expansion//

// Created by: Greg Leonardo //
// Copyright 2000 eLogic Learning //
// JS Application For Form Validation //
// 12/19/2001 //

// Begin Form Validation //
// t_ - For Required Text //
// n_ - For Required Number //
// s_ - For Required Selection //

function ValidateForm(form)
{
	var passtxt=true;
	var passtxtarea=true;
	var passnum=true;
	var passselect=true;
	if (document.images)
	{
		for (i=0;i<form.length;i++)
		{
			var tempobj=form.elements[i];
			if (tempobj.name.substring(0,2)=="t_")
			{
				if (tempobj.value=='')
				{
					passtxt=false;
					break;
				}
				
				if (tempobj.name.substring(5,35).toUpperCase() != "EMAIL")
				{
					var strLoValid = "abcdefghijklmnopqrstuvwxyz";
					var strUpValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ -()[]?/<>.,!@#$&{}_=+{}:";
					var whitespace = " \t\n\r";
					var digits = "0123456789";
					var strValid = strLoValid + strUpValid + digits + whitespace;
					for (s=0;s<tempobj.value.length;s++)
					{
						temp = "" + tempobj.value.substr(s, 1);
						if (strValid.indexOf(temp) == "-1")
						{
							passtxt=false;
							break;
						}
					}
					if (!passtxt)
					{
						break;
					}
				}
				// Email Validation
				var emailStr = form.elements[tempobj.name].value;
				if (tempobj.name.substring(5,35).toUpperCase() == "EMAIL")
				{
					var checkTLD=1;
					var knownDomsPat=/^(COM|NET|ORG|EDU|com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
					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 + "|" + quotedUser + ")";
					var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
					var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
					var matchArray=emailStr.match(emailPat);

					if (matchArray==null)
					{
						alert("E-mail address seems incorrect (check @ and .'s)");
						form.elements[tempobj.name].select();
						form.elements[tempobj.name].focus();
						return false;
					}
			
					var user=matchArray[1];
					var domain=matchArray[2];

					for (e=0;e<user.length;e++)
					{
						if (user.charCodeAt(e)>127)
						{
							alert("Ths username contains invalid characters.");
							form.elements[tempobj.name].select();
							form.elements[tempobj.name].focus();
							return false;
						}
					}
					
					for (e=0;e<domain.length;e++)
					{
						if (domain.charCodeAt(e)>127)
						{
							alert("Ths domain name contains invalid characters.");
							form.elements[tempobj.name].select();
							form.elements[tempobj.name].focus();
							return false;
						}
					}

					if (user.match(userPat)==null)
					{
						alert("The username doesn't seem to be valid.");
						form.elements[tempobj.name].select();
						form.elements[tempobj.name].focus();
						return false;
					}
					
					var IPArray=domain.match(ipDomainPat);
					if (IPArray!=null)
					{
						for (var e=1;e<=4;e++)
						{
							if (IPArray[e]>255)
							{
								alert("Destination IP address is invalid!");
								form.elements[tempobj.name].select();
								form.elements[tempobj.name].focus();
								return false;
							}
						}
					}
 
					var atomPat=new RegExp("^" + atom + "$");
					var domArr=domain.split(".");
					var len=domArr.length;
					
					for (e=0;e<len;e++)
					{
						if (domArr[e].search(atomPat)==-1)
						{
							alert("The domain name does not seem to be valid.");
							form.elements[tempobj.name].select();
							form.elements[tempobj.name].focus();
							return false;
						}
					}

					if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
					{
						alert("The address must end in a well-known domain or two letter " + "country.");
						form.elements[tempobj.name].select();
						form.elements[tempobj.name].focus();
						return false;
					}
					
					if (len<2)
					{
						alert("This address is missing a hostname!");
						form.elements[tempobj.name].select();
						form.elements[tempobj.name].focus();
						return false;
					}
				}	
			}
			
			if (tempobj.name.substring(0,2)=="n_")
			{
				if (tempobj.value == '')
				{
					passnum=false;
					break;
				}
				var numValid = "0123456789/-().$ ";
				for (n=0;n<tempobj.value.length;n++)
				{
					temp = "" + tempobj.value.substring(n, n+1);
					if (numValid.indexOf(temp) == "-1")
					{
						passnum=false;
						break;
					}
				}
				if (tempobj.name.substring(5,35).toUpperCase() == "ZIP")
				{
					if (tempobj.value.length < 5)
					{
						passnum=false;
						break;
					}
				}
				if (!passnum)
				{
					break;
				}
			}
			
			if (tempobj.name.substring(0,2)=="s_")
			{
				if (tempobj.options[tempobj.selectedIndex].value == "N")
				{
					passselect=false;
					break;
				}
			}
			
			if (tempobj.name.substring(0,3)== "ta_")
			{
				//alert("IN ta_ with tempobj.value=" + tempobj.value);
				if (tempobj.value == '')
				{
					passtxtarea=false;
					break;
				}
				var digits = "0123456789";
				var strLoValid = "abcdefghijklmnopqrstuvwxyz";
//100526				var strUpValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ -()[]/.,{}_{};:";
				var strUpValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ -()[]/.,{}_;:~`!@#$%^&*^?><\\|'+";
				var whitespace = " \t\n\r";
				var strValid = strLoValid + strUpValid + digits + whitespace;
				for (s=0;s<tempobj.value.length;s++)
				{
					temp = "" + tempobj.value.substr(s, 1);
//					alert("s=" + s + " temp=" + temp + "  strValid.indexOf(temp)= " + strValid.indexOf(temp)) 
					if (strValid.indexOf(temp) == "-1")
					{
						passtxtarea=false;
						break;
					}
				}
			}
			
		}
	
		// Error Handlers //
		if (!passtxt)
		{
		    //	shortFieldName=tempobj.name.substring(5,35).toUpperCase();
		    if (tempobj.name.substring(0,2)=="t_")
		    {
                    shortFieldName=tempobj.name.substring(5,35).toUpperCase();
			}

			if (tempobj.name.substring(0,2)=="t_")
	        {
		        alert(shortFieldName+" is a required field and does not accept any special characters.");
            }

//			alert("Please enter " + shortFieldName + ".");
//			alert("This required field accepts Alpha and Numbers only "+shortFieldName+".");
			form.elements[tempobj.name].select();
			form.elements[tempobj.name].focus();
			return false;
		}
		
		if (!passtxtarea)
		{
		    if (tempobj.name.substring(0,3)=="ta_")
			    {
                        shortFieldName=tempobj.name.substring(5,35).toUpperCase();
				    }


					    if (tempobj.name.substring(0,3)=="ta_")
			    {
        		    alert(shortFieldName+" is a required field and does not accept any special characters.");
                }
			    form.elements[tempobj.name].select();
			    form.elements[tempobj.name].focus();
			    return false;
        }

		if (!passselect)
		{
			shortFieldName=tempobj.name.substring(5,35).toUpperCase();
			alert("Please select a value for the required field "+shortFieldName+".");
			form.elements[tempobj.name].focus();
			return false;
		}
		
		if (!passnum)
		{
			shortFieldName=tempobj.name.substring(5,35).toUpperCase();
			alert("This required field accepts numeric values only "+shortFieldName+".");
			form.elements[tempobj.name].select();
			form.elements[tempobj.name].focus();
			return false;
		}
		
		if(tempobj.type.toLowerCase()=="button")
		{
			tempobj.value = "Processing......"
			tempobj.disabled = true
			form.submit();
		}
	}
}

var digitsInUSPhoneNumber = 10;
var digitsInSocialSecurityNumber = 9;
var digitsInDate = 8;
var digits = "0123456789";
var decimalPointDelimiter = ".";
var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var SSNDelimiters = "- ";
var DateDelimiters = "/- ";
var validDateChars = digits + DateDelimiters;
var validSSNChars = digits + SSNDelimiters;
var Error = false

function confirmPassword(form)
{

	if (form.t_txtPassword.value != form.t_txtPassword1.value)
	{	
		alert("Your Password does not match.");
		form.t_txtPassword.value = "";
		form.t_txtPassword1.value = "";
		form.t_txtPassword.focus();
		return false;
	}
}

function reformat(s)
{
	var arg;
    var sPos = 0;
    var resultString = "";

    for (var j=1;j<reformat.arguments.length;j++)
    {
		arg = reformat.arguments[j];
		if (j % 2 == 1)
		{
			resultString += arg;
		}
		else
		{
			resultString += s.substring(sPos, sPos + arg);
			sPos += arg;
		}
	}
    return resultString;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";

    for (j=0;j<s.length;j++)
    {   
        var c = s.charAt(j);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isUSPhoneNumber(s)
{   
	if (isEmpty(s))
	{
		if (isUSPhoneNumber.arguments.length == 1)
		{
			return defaultEmptyOK;
		}
		else
		{
			return (isUSPhoneNumber.arguments[1] == true);
		}
    }
    return (s.length == digitsInUSPhoneNumber)
}

function isEmpty(s)
{
	if (s.length == 0)
	{
		return true;
	}
	return false;
}

function checkPhone(theField)
{
        if (theField.value == "")
        {
                    return;
        }
        
        if(isNaN(theField.value)==true)
        { 
                    alert("Phone Numbers Require Numeric Values Only.");
                    Error = true;
                    theField.value= ""
                    theField.select();
                    theField.focus();
        
        }
        else
        {
        
        var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
    if (!isUSPhoneNumber(normalizedPhone, false) && (!Error))
    { 
                        alert("Please enter a valid phone number in XXX XXX XXXX format.");
                        Error = true;
                        theField.select();
                        theField.focus();
            }
    else 
    {  
       theField.value = reformatUSPhone(normalizedPhone);
       Error = false;
       return true;
    }
   }
}

function reformatUSPhone(USPhone)
{   
	return (reformat(USPhone, "(", 3, ") ", 3, "-", 4));
}

function checkSSN(theField)
{
	if (theField.value == "")
	{
		return;
	}
	var normalizedSSN = stripCharsInBag(theField.value, SSNDelimiters)
    if (!isSSN(normalizedSSN, false) && (!Error))
    { 
		alert("Please enter a valid SSN number in XXX-XX-XXXX format.");
		Error = true;
		theField.select();
		theField.focus();
	}
    else 
    {  
       theField.value = reformatSSN(normalizedSSN);
       Error = false;
       return true;
    }
}

function reformatSSN(SSN)
{   
	return (reformat(SSN, "", 3, "-", 2, "-", 4));
}
function isSSN(s)
{   
	if (isEmpty(s))
	{
		if (isSSN.arguments.length == 1)
		{
			return defaultEmptyOK;
		}
		else
		{
			return (isSSN.arguments[1] == true);
		}
    }
    return (s.length == digitsInSocialSecurityNumber)
}

function checkDate(theField)
{
	if (theField.value == "")
	{
		return;
	}
	var normalizedDate = stripCharsInBag(theField.value, DateDelimiters)
    if (!isDate(normalizedDate, false) && (!Error))
    { 
		alert("Please enter a valid Date in XX/XX/XXXX format.");
		Error = true;
		theField.select();
		theField.focus();
	}
    else 
    {  
       theField.value = reformatDate(normalizedDate);
       Error = false;
       return true;
    }
}

function reformatDate(sDate)
{   
	return (reformat(sDate, "", 2, "/", 2, "/", 4));
}

function isDate(s)
{   
	if (isEmpty(s))
	{
		if (isDate.arguments.length == 1)
		{
			return defaultEmptyOK;
		}
		else
		{
			return (isDate.arguments[1] == true);
		}
    }
    return (s.length == digitsInDate)
}

function checkTest(sURL)
{
	if (confirm(":: Important Message ::\nAre you sure you would like to start the test?\nOnce you begin the test you will no longer have access\nto the course materials until you complete the test.\nIf you are not ready to take the test, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}
function coursecancel(sURL)
{
	if (confirm(":: Important Message ::\nAre you sure you would like to cancel this course?\nIf you are not sure, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}

function courseCancelLP(sURL)
{
	if (confirm(":: Important Message ::\nAre you sure you would like to cancel this course?\nThis course is part of a certification Learning Path\nand all uncompleted courses will be dropped at the same time.\nIf you are not sure, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}


function checkResource(sURL)
{
	if (confirm(":: Important Message ::\nYou are about to delete this Item !\nIt will be deleted permanently\nIf you are not sure, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}
function checkGroups(sURL)
{
	if (confirm(":: Important Message ::\nYou are about to delete this Item !\nIt will be deleted permanently !\nBy deleting this group all of the Associates assigned to it will be deactivated. \nIf you are not sure, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}
function cancelNotice(sURL)
{
	if (confirm(":: Important Message ::\nAre you sure you want to cancel?\nIf you are not sure, please click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}

function UnregisterNotice(sURL)
{
	if (confirm(":: Important Message ::\nYou are about to unregister this user. \nIf you do not want to unregister this user, \nplease click 'Cancel'."))
	{
		window.location = sURL;
		return;
	}
	else
	{
		return;
	}   
}

function confirmEmail(form)
{
	if (form.txtEmail.value != form.txtEmail2.value)
	{	
		alert("Your Email does not match.");
		form.txtEmail.value = "";
		form.txtEmail2.value = "";
		form.t_txtEmail.focus();
		return false;
	}
}

function confirmEmailNonReq(form)
{
	if (form.txtEmail.value != form.txtEmail2.value)
	{	
		alert("Your Email does not match.");
	    form.txtEmail.value = "";
	    form.txtEmail2.value = "";
		form.txtEmail.focus();
		return false;
	}
}

function validateEmailValue(form)
{
	if (form.t_txtEmail.value.length = 0)
	{	
		alert("The Email field cannot be blank.");
		form.t_txtEmail.value = "";
		form.t_txtEmail.focus();
		return false;
	}
}
