// This code originally written for Shaw at Home and modified drastically for this site


var defaultEmptyOK = false;

var dfltWarningOn = true;

var digitsInPhoneNumber = 10;

var digits = "0123456789";

var visadigits = " 0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var whitespace = " \t\n\r";

var pEntryPrompt = "Please enter a ";

var pPhone = "10 digit phone number (like 403 555 1212). If it does not meet this format, use the comments.";

var pEmail = "a valid email address (like name@domain.com). ";

var sEmail = "Your Email";

var sName = "Full Name";

var sAddress = "Address";

var sDegree = "The degree(s), diploma(s) and/or certificate(s) you received";

var mWork = "One of the required fields has not been entered. Please fill in all fields marked with an asterisk (*) now.";


function openwindow(page,ww,hh) {
    ww = ww + 30;
    hh = hh + 30;
    windowprops = "width="+ww+",height="+hh+",location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no";
    window.open(page, "kjdh", windowprops);
    }

function promptEntry (s)

{   window.status = pEntryPrompt + s

}





function isEmpty(s)
{   return ((s == null) || (s.length == 0))

}







function warnEmpty (s)

{   alert(mWork)

    return false

}





function isDigit (c)

{   return ((c >= "0") && (c <= "9"))

}





function isAlpha (c)

{   return (((c >= "a") && (c <= "z")) ||

        ((c >= "A") && (c <= "Z")))

}





function isInteger (s)

{   var i;



    if (isEmpty(s))

       if (isInteger.arguments.length == 1) return defaultEmptyOK;

       else return (isInteger.arguments[1] == true);



    // Search through string's characters one by one

    // until we find a non-numeric character.

    // When we do, return false; if we don't, return true.



    for (i = 0; i < s.length; i++)

    {

        // Check that current character is number.

        var c = s.charAt(i);



        if (!isDigit(c)) 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++)

    {

        // Check that current character isn't whitespace.

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }



    return returnString;

}



function reformat (s)

{   var arg;

    var sPos = 0;

    var resultString = "";



    for (var i = 1; i < reformat.arguments.length; i++) {

       arg = reformat.arguments[i];

       if (i % 2 == 1) resultString += arg;

       else {

           resultString += s.substring(sPos, sPos + arg);

           sPos += arg;

       }

    }

    return resultString;

}





var phoneNumberDelimiters = "()- ";

function reformatPhone (Phone)

{   return (reformat (Phone, "(", 3, ") ", 3, "-", 4))

}





function isPhoneNumber (s)

{   if (isEmpty(s))

       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;

       else return (isPhoneNumber.arguments[1] == true);

    return (isInteger(s) && (s.length == digitsInPhoneNumber))

}





function checkPhone (theField, emptyOK, warningOn)

{   if (checkPhone.arguments.length == 1) emptyOK = defaultEmptyOK;

    if (checkPhone.arguments.length <= 2) warningOn = dfltWarningOn;

    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

    else

    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)

       if (!isPhoneNumber(normalizedPhone, false))

		 {  if (warningOn) return warnInvalid (theField, iPhone);

			 else return false;

		 }

       else

       {  // if you don't want to reformat as (123) 456-789, comment next line out

          theField.value = reformatPhone(normalizedPhone)

          return true;

       }

    }

}





function warnInvalid (theField, s)

{   theField.focus()

    alert(s)

    return false

}



function isWhitespace (s) {

	var i;



    // Is s empty?

    if (isEmpty(s)) return true;



    // Search through string's characters one by one

    // until we find a non-whitespace character.

    // When we do, return false; if we don't, return true.



    for (i = 0; i < s.length; i++)

    {

        // Check that current character isn't whitespace.

        var c = s.charAt(i);



        if (whitespace.indexOf(c) == -1) return false;

    }



    // All characters are whitespace.

    return true;

}



function isEmail (s)

{   if (isEmpty(s))

       if (isEmail.arguments.length == 1) return defaultEmptyOK;

       else return (isEmail.arguments[1] == true);



    // is s whitespace?

    if (isWhitespace(s)) return false;



    // there must be >= 1 character before @, so we

    // start looking at character position 1

    // (i.e. second character)

    var i = 1;

    var sLength = s.length;



    // look for @

    while ((i < sLength) && (s.charAt(i) != "@"))

    { i++

    }



    if ((i >= sLength) || (s.charAt(i) != "@")) return false;

    else i += 2;



    // look for .

    while ((i < sLength) && (s.charAt(i) != "."))

    { i++

    }



    // there must be at least one character after the .

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;

    else return true;

}





function checkEmail (theField, emptyOK, warningOn)

{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;

    if (checkEmail.arguments.length <= 2) warningOn = dfltWarningOn;

    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

    else if (!isEmail(theField.value, false))

	 {  if (warningOn) return warnInvalid(theField, iEmail);

		 else return false;

	 }

    else return true;

}



function checkString (theField, emptyOK) {

if (checkString.arguments.length == 1) emptyOK = defaultEmptyOK;

    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

    if (isWhitespace(theField.value))

       return false;

    else return true;

}





function runSubmit(form) {

        form.submit();



}



function AddRequiredList (s1, s2) {

	if (s1.length == 0)

		s1 = s2;

	else

		s1 += ", " + s2;

	return s1;

}



function validatePersonalInfo(form) {

	var RequiredString = "";

	var EmptyRequired = false;

	var InvalidString = "";

	var InvalidFound = false;

	var warningMsg = "";



	if (!checkString(form.elements["FullName"]))

	{	RequiredString = AddRequiredList(RequiredString, sFullName);

		EmptyRequired = true;

		last_field = form.Fullname;

	}
	if (!checkString(form.elements["fromemail"]))

	{	RequiredString = AddRequiredList(RequiredString, sEmail);

		EmptyRequired = true;

		last_field = form.fromemail;

	}

	if (!checkEmail(form.elements["fromemail"], false, false))

	{	InvalidString += sEmail + " must be a " + pEmail;

		InvalidFound = true;

		last_field = form.fromemail;

	}


	if (!checkString(form.elements["degree"]))

	{	RequiredString = AddRequiredList(RequiredString, sDegree);

		EmptyRequired = true;

		last_field = form.degree;

	}



	if (!checkString(form.elements["Address"]))

	{	RequiredString = AddRequiredList(RequiredString, sAddress);

		EmptyRequired = true;

		last_field = form.Address;

	}





	if (EmptyRequired || InvalidFound)

	{	if (EmptyRequired)

			warningMsg = "The following required fields were left blank or not selected: " + RequiredString + ". "

		warningMsg += InvalidString;

		return warnInvalid (form.FullName, warningMsg);

	}

	else

	{

		return true;

	}
}
