﻿var AlertStr="";
var dtCh= "/";
var minYear=1900;
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 strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	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)
        {
        DateFormatStr="The date format should be : mm/dd/yyyy\n";
		return false;
	    }
	if (strMonth.length<1 || month<1 || month>12)
        {
        DateFormatStr="Please enter a valid month\n";
		return false;
	    }
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
        {
        DateFormatStr="Please enter a valid day.\n";
		return false;
	    }
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
        {
        DateFormatStr="Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"\n";
		return false;
	    }
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
        {
        DateFormatStr="Please enter a valid date";
		return false;
	    }
    return true;
    }
function Validate1Fn()
    {
    document.getElementById('RadioTD').style.backgroundColor='#DCDCDC';
    document.UpdateFrm.last_name.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.first_name.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.home_phone.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.cell_phone.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.email.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.birthdate.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.start_date.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.campus.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.password.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.password_2.style.backgroundColor='#FFFFFF';
    AlertStr="";
    var returnval=true;
    if(!document.UpdateFrm.baptized[0].checked && !document.UpdateFrm.baptized[1].checked)
        {
        document.getElementById('RadioTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Baptism' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.campus.value==0)
        {
        document.UpdateFrm.campus.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter the campus you primarily attend.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.last_name.value == "" || document.UpdateFrm.first_name.value == "")
        {
        document.UpdateFrm.last_name.style.backgroundColor='#FFFF99';
        document.UpdateFrm.first_name.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter your first and last name on the form before submitting.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.home_phone.value == "" && document.UpdateFrm.cell_phone.value == "")
        {
        document.UpdateFrm.home_phone.style.backgroundColor='#FFFF99';
        document.UpdateFrm.cell_phone.style.backgroundColor='#FFFF99';
        document.UpdateFrm.home_phone.value='NA';
        document.UpdateFrm.cell_phone.value='NA';
        AlertStr=AlertStr+"Please enter a Home or Cell phone number on the form before submitting. If you don't have a phone number enter NA as your home phone.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.email.value == "")
        {
        document.UpdateFrm.email.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter an email address on the form before submitting. If you don't have an email address enter NA.\n\n";
        returnval=false;
        }
    if(!isDate(document.UpdateFrm.birthdate.value))
        {
        document.UpdateFrm.birthdate.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Birthdate format invalid. "+DateFormatStr+"\n";
        returnval=false;
        }
    if(!isDate(document.UpdateFrm.start_date.value))
        {
        document.UpdateFrm.start_date.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Began attending date format invalid. "+DateFormatStr+"\n";
        returnval=false;
        }
    if(document.UpdateFrm.user_name.value == "")
        {
        document.UpdateFrm.user_name.value=document.UpdateFrm.first_name.value+document.UpdateFrm.last_name.value;
        }
    if(document.UpdateFrm.password.value != document.UpdateFrm.password_2.value || document.UpdateFrm.password.value=="")
        {
        document.UpdateFrm.password.style.backgroundColor='#FFFF99';
        document.UpdateFrm.password_2.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Password fields don't match, or are blank. Please enter the same password in each box\n\n";
        returnval=false;
        }
    if(AlertStr!="")
        alert(AlertStr);
    return returnval;
    }
function Validate2Fn()
    {
    document.getElementById('ReadingTD').style.backgroundColor='#DCDCDC';
    document.UpdateFrm.question_3.style.backgroundColor='#FFFFFF';
    document.getElementById('CounselingTD').style.backgroundColor='#DCDCDC';
    document.getElementById('AccountabilityTD').style.backgroundColor='#DCDCDC';
    AlertStr="";
    var returnval=true;
    if(!document.UpdateFrm.question_1[0].checked && !document.UpdateFrm.question_1[1].checked)
        {
        document.getElementById('ReadingTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Required Reading' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.question_2.value == "")
        {
        document.UpdateFrm.question_2.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please answer the question 'Briefly explain what stood out to you in the required reading.'\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.question_3.value == "")
        {
        document.UpdateFrm.question_3.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please answer the question 'How knowing the Lord is changing your life.'\n\n";
        returnval=false;
        }
    if(!document.UpdateFrm.question_4[0].checked && !document.UpdateFrm.question_4[1].checked)
        {
        document.getElementById('CounselingTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Biblical Counseling' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.password.value != document.UpdateFrm.password_2.value || document.UpdateFrm.password.value=="")
        {
        document.UpdateFrm.password.style.backgroundColor='#FFFF99';
        document.UpdateFrm.password_2.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Password fields don't match, or are blank. Please enter the same password in each box\n\n";
        returnval=false;
        }
    if(!document.UpdateFrm.accountability[0].checked && !document.UpdateFrm.accountability[1].checked)
        {
        document.getElementById('AccountabilityTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Statement of Accountability' question.\n\n";
        returnval=false;
        }
    if(AlertStr!="")
        alert(AlertStr);
    return returnval;
    }
function ValidateResultFn()
    {
    document.getElementById('FaithTD').style.backgroundColor='#DCDCDC';
    document.getElementById('BaptizedTD').style.backgroundColor='#DCDCDC';
    document.getElementById('AccountabilityTD').style.backgroundColor='#DCDCDC';
    document.UpdateFrm.year_saved.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.last_name.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.first_name.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.home_phone.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.cell_phone.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.email.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.birthdate.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.start_date.style.backgroundColor='#FFFFFF';
    document.getElementById('ReadingTD').style.backgroundColor='#DCDCDC';
    document.UpdateFrm.question_3.style.backgroundColor='#FFFFFF';
    document.getElementById('CounselingTD').style.backgroundColor='#DCDCDC';
    document.UpdateFrm.password.style.backgroundColor='#FFFFFF';
    document.UpdateFrm.password_2.style.backgroundColor='#FFFFFF';
    document.getElementById('AccountabilityTD').style.backgroundColor='#DCDCDC';
    AlertStr="";
    var returnval=true;
    if(!document.UpdateFrm.statement[0].checked && !document.UpdateFrm.statement[1].checked)
        {
        document.getElementById('FaithTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Statement of Faith' question.\n\n";
        returnval=false;
        }
    if(!document.UpdateFrm.baptized[0].checked && !document.UpdateFrm.baptized[1].checked)
        {
        document.getElementById('BaptizedTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Baptism' question.\n\n";
        returnval=false;
        }
    if(!document.UpdateFrm.accountability[0].checked && !document.UpdateFrm.accountability[1].checked)
        {
        document.getElementById('AccountabilityTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Statement of Accountability' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.year_saved.value==-1 && document.UpdateFrm.saved_check.checked == false)
        {
        document.UpdateFrm.year_saved.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter the year you accepted Jesus or check the check box.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.last_name.value == "" || document.UpdateFrm.first_name.value == "")
        {
        document.UpdateFrm.last_name.style.backgroundColor='#FFFF99';
        document.UpdateFrm.first_name.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter your first and last name on the form before submitting.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.home_phone.value == "" && document.UpdateFrm.cell_phone.value == "")
        {
        document.UpdateFrm.home_phone.style.backgroundColor='#FFFF99';
        document.UpdateFrm.cell_phone.style.backgroundColor='#FFFF99';
        document.UpdateFrm.home_phone.value='NA';
        document.UpdateFrm.cell_phone.value='NA';
        AlertStr=AlertStr+"Please enter a Home or Cell phone number on the form before submitting. If you don't have a phone number enter NA as your home phone.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.email.value == "")
        {
        document.UpdateFrm.email.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please enter an email address on the form before submitting. If you don't have an email address enter NA.\n\n";
        returnval=false;
        }
    if(!isDate(document.UpdateFrm.birthdate.value))
        {
        document.UpdateFrm.birthdate.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Birthdate format invalid. "+DateFormatStr+"\n";
        returnval=false;
        }
    if(!isDate(document.UpdateFrm.start_date.value))
        {
        document.UpdateFrm.start_date.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Began attending date format invalid. "+DateFormatStr+"\n";
        returnval=false;
        }
    if(!document.UpdateFrm.question_1[0].checked && !document.UpdateFrm.question_1[1].checked)
        {
        document.getElementById('ReadingTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Statement of Faith' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.question_3.value == "")
        {
        document.UpdateFrm.question_3.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please answer the question 'How knowing the Lord is changing your life.'\n\n";
        returnval=false;
        }
    if(!document.UpdateFrm.question_4[0].checked && !document.UpdateFrm.question_4[1].checked)
        {
        document.getElementById('CounselingTD').style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Please select an answer for the 'Biblical Counseling' question.\n\n";
        returnval=false;
        }
    if(document.UpdateFrm.password.value != document.UpdateFrm.password_2.value || document.UpdateFrm.password.value=="")
        {
        document.UpdateFrm.password.style.backgroundColor='#FFFF99';
        document.UpdateFrm.password_2.style.backgroundColor='#FFFF99';
        AlertStr=AlertStr+"Password fields don't match, or are blank. Please enter the same password in each box\n\n";
        returnval=false;
        }
    if(AlertStr!="")
        alert(AlertStr);
    return returnval;
    }

function pictureCheck()
    {
    fileName=document.UpdateFrm.picture.value;
    fileType=fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
    if(fileType=="jpg"||fileType=="jpeg")
        return true;
    else
        {
        document.UpdateFrm.picture.value="";
        alert(fileName+" is not a 'jpeg' file. Look for a file that ends in '.jpg' or '.jpeg'.");
        }
    }