// set the error messages

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

var Msg1 = "Please enter your first name.";
var Msg2 = "Please enter your last name.";
var Msg3 = "Please enter your address.";
var Msg4 = "Please enter your city.";
var Msg5 = "Please enter your state.";
var Msg6 = "Please enter your state.";
var Msg7 = "Please enter your zip code.";
var Msg8 = "Please enter only  5 digit characters in the \"Zip\" field.";
var Msg9 = "Please enter area code.";
var Msg10 = "Please enter 3 digit area code.";
var Msg11 = "Please enter only numbers in area code.";
var Msg12 = "Please enter area code.";
var Msg13 = "Please enter first 3 digit phone number.";
var Msg14 = "Please enter only numbers in first 3 digit phone number.";
var Msg15 = "Please enter last 4 digit phone number.";
var Msg16 = "Please enter only numbers in last 4 digit phone number.";
var Msg17 = "Please enter only numbers in last 4 digit phone number.";
var Msg18 = "Please enter your Email Address.";
var Msg19 = "The \"email\" field must contain an \"@\" and a \".\".";
var Msg20 = "Please enter your resume here.";
var Msg21 = "Please select store location from the list.";
var Msg22 = "Please select one of job postion";
var Msg23 = "You must be 18 years old to apply.";
var Msg24 = "The Picture People has a minimum age of 18 years of age.";
var Msg25 = "Please select \"Availablity Length\" field.";

var IsDateMsg1 = "The date format should be mm/dd/yyyy";
var IsDateMsg2 = "Please enter a valid month.";
var IsDateMsg3 = "Please enter a valid day";
var IsDateMsg4 = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
var IsDateMsg5 = "Please enter a valid date.";

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){
		alert(IsDateMsg1)
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert(IsDateMsg2)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert(IsDateMsg3)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert(IsDateMsg4)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(IsDateMsg5)
		return false
	}
return true
}

function FormValidation(myForm)
{
	var radioSelected = false;
	for (i = 0;  i < myForm.JobType.length;  i++)
	{
		if (myForm.JobType[i].checked)
		radioSelected = true;
	}
	if (!radioSelected)
	{
		alert(Msg22);
		myForm.JobType[1].focus();
		return false;
	}

	if (myForm.StoreNo.value == "")
	
	{
		alert(Msg21);
		myForm.StoreNo.focus();
		return false;
	}

	if (!myForm.Q1.checked)
	{
		alert(Msg24);
		myForm.Q1.focus();
		return false;
	}

	var radioSelected2 = false;
	for (i = 0;  i < myForm.AvailablityLength.length;  i++)
	{
		if (myForm.AvailablityLength[i].checked)
		radioSelected2 = true;
	}
	if (!radioSelected2)
	{
		alert(Msg25);
		myForm.AvailablityLength[1].focus();
		return false;
	}

	if (myForm.FirstName.value == "")
	{
		alert(Msg1);
		myForm.FirstName.focus();
		return false;
	}

	if (myForm.LastName.value == "")
	{
		alert(Msg2);
		myForm.LastName.focus();
		return false;
	}

	if (myForm.Address.value == "")
	{
		alert(Msg3);
		myForm.Address.focus();
		return false;
	}

	if (myForm.City.value == "")
	{
		alert(Msg4);
		myForm.City.focus();
		return false;
	}

	if (myForm.State.value == "")
	{
		alert(Msg5);
		myForm.State.focus();
		return false;
	}

	if (myForm.State.value.length != 2)
	{
		alert(Msg6);
		myForm.State.focus();
		return false;
	}

	if (myForm.Zip.value == "")
	{
		alert(Msg7);
		myForm.Zip.focus();
		return false;
	}

	var checkOK = "0123456789";
	var checkStr = myForm.Zip.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
		allNum += ch;
	}
	if (!allValid)
	{
		alert(Msg8);
		myForm.Zip.focus();
		return false;
	}

	if (myForm.Phone.value == "")
	{
		alert(Msg9);
		myForm.Phone.focus();
		return false;
	}

	if (myForm.Phone.value.length != 3)
	{
		alert(Msg10);
		myForm.Phone.focus();
		return false;
	}
	var checkOK = "0123456789";
	var checkStr = myForm.Phone.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
		allNum += ch;
	}

	if (!allValid)
	{
		alert(Msg11);
		myForm.Phone.focus();
		return false;
	}

	if (myForm.Phone1.value == "")
	{
		alert(Msg12);
		myForm.Phone1.focus();
		return false;
	}

	if (myForm.Phone1.value.length != 3)
	{
		alert(Msg13);
		myForm.Phone1.focus();
		return false;
	}
	var checkOK = "0123456789";
	var checkStr = myForm.Phone1.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
		allNum += ch;
	}

	if (!allValid)
	{
		alert(Msg14);
		myForm.Phone1.focus();
		return false;
	}

	if (myForm.Phone2.value == "")
	{
		alert(Msg15);
		myForm.Phone2.focus();
		return false;
	}

	if (myForm.Phone2.value.length != 4)
	{
		alert(Msg16);
		myForm.Phone2.focus();
		return false;
	}
	var checkOK = "0123456789";
	var checkStr = myForm.Phone2.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
		allNum += ch;
	}

	if (!allValid)
	{
		alert(Msg17);
		myForm.Phone2.focus();
		return false;
	}

	if (myForm.Email.value == "")
	{
		alert(Msg18);
		myForm.Email.focus();
		return false;
	}

	var checkEmail = "@.";
	var checkStr = myForm.Email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			if (EmailAt && EmailPeriod)
				break;
			if (j == checkEmail.length)
				break;
		}
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid)
	{
		alert(Msg19);
		myForm.Email.focus();
		return false;
	}

	if (myForm.ResumeText.value == "")
	{
		alert(Msg20);
		myForm.ResumeText.focus();
		return false;
	}
return true;
}