var sections;
var focusField = "";
var frm;
var i;

var emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/

var phoneRegex = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

var ssnRegex = /^\d{3}-\d{2}-\d{4}$/

var dateRegex = /^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/

var zipRegex = /^\d{5}$|^\d{5}-\d{4}$/

function init(jobCode) {

	if(jobCode=="")
		{sections = new Array('part1', 'part2', 'part3', 'part4', 'part5', 'part6', 'part7', 'part8');}
	else
		{sections = new Array('part2', 'part3', 'part4', 'part5', 'part6', 'part7', 'part8');}
	

	if(document.getElementById(sections[0]))
	{
    show(sections[0]);
    frm=document.forms[0];
    
    if(jobCode=="")
		{document.forms[0].elements['ApplicationContent1:lbPositions'].focus();}
    else
		{document.forms[0].elements['First_Name'].focus();}
		
    
    // if the additional employer sections are empty, turn them off:
    if(frm.Emp2_Company_Name.value=="") off('emp2');
    if(frm.Emp3_Company_Name.value=="") off('emp3');
    
    }
    
    
}



function show(section) {
    for (var i=0; i < sections.length; i++) {
        if (sections[i] == section) {
			on(sections[i]);
            //document.getElementById(sections[i]).style.display = 'block';
        } else {
			off(sections[i]);
            //document.getElementById(sections[i]).style.display = 'none';
        }
    }
    
    // scroll the view to the top left of the window
    scroll(0,0)
}


function on(section) {
	document.getElementById(section).style.display = 'block';
}

function off(section) {
	document.getElementById(section).style.display = 'none';
}


function noPostBack(sNewFormAction)
{
    if(document.layers) //The browser is Netscape 4
    {
        document.layers['Content'].document.forms[0].__VIEWSTATE.name = 
                                                           'NOVIEWSTATE';
        document.layers['Content'].document.forms[0].action = 
                                                     sNewFormAction;
    }
    else //It is some other browser that understands the DOM
    {
        document.forms[0].action = sNewFormAction;
        document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
    }
}


function setFocusField(field){
	if (focusField==""){
		focusField=field;
	}

}

function setFocus(){
	focusField.focus();
	focusField="";
}

function checkRadio(radioBtn){
	for(i=0;i<radioBtn.length;i++){
		if(radioBtn[i].checked==true) return false;
	}
	
	return true;
}



function validate(section) {
	var missingInfo = "";
	frm=document.forms[0];
	
	switch(section){
	
		case 'part1':
		// Basic Information
			with(frm){
				var select = elements['ApplicationContent1:lbPositions'];
				
				
				if (select.selectedIndex<0){
					missingInfo+="You must select at least one position.\n";
					setFocusField(select);
				}	
				
				
			}
			
				
			if (missingInfo!="") {
				alert(missingInfo);
				setFocus();
			}
			
			else
			{show('part2');
			}
		
		
		
		break
		
		
		case 'part2':
		// Contact Information
			with(frm){
				if(First_Name.value=="") missingInfo+="Please enter your First Name.\n";
				if(Last_Name.value=="") missingInfo+="Please enter your Last Name.\n";
				
				if(Email_Address.value!=""){
					if(!emailRegex.test(Email_Address.value)){
						missingInfo+="Please enter a valid Email Address.\n";				
					}
				}
				
				if(Address.value=="") missingInfo+="Please enter your Address.\n";
				if(City.value=="") missingInfo+="Please enter your City.\n";
				if(State.selectedIndex<1) missingInfo+="Please enter your State.\n";
				if(Home_Phone.value=="") 
					{missingInfo+="Please enter your Home Phone.\n";} else
						{if(!phoneRegex.test(Home_Phone.value)){
							missingInfo+="Please enter a valid Home Phone.\n"}
						}
						
				if(Cell_Phone.value!=""){
					if(!phoneRegex.test(Cell_Phone.value)) missingInfo+="Please enter a valid Cell/Other Phone.\n"}
				
				if(ZIP.value=="") 
					{missingInfo+="Please enter your ZIP Code.\n";}else
						{if(!zipRegex.test(ZIP.value)){missingInfo+="Please enter a valid ZIP Code.\n"}
				}
				
				/*
				if(Social_Security_Number.value=="")
					{missingInfo+="Please enter your Social Security Number.\n";}else
						{if(!ssnRegex.test(Social_Security_Number.value))
							{missingInfo+="Please enter a valid Social Security Number.\n";}
					}
				*/
				
				if(checkRadio(Contact_at_Work)) missingInfo+="Please let us know if we can contact you at work.\n";
				
				if(Work_Phone.value!="")
					{if(!phoneRegex.test(Work_Phone.value)){
						missingInfo+="Please enter a valid Work Phone.\n";}
				}
				
			
			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part3');
			}
			
		break
		
		
		case 'part3':
		// General Information
			with(frm){
		
			if(checkRadio(Under_18)) missingInfo+="Please let us know if you are under 18.\n";
			
			if(checkRadio(Applied_Before)){ missingInfo+="Please let us know if you have applied here before.\n";} 
			
			if(Applied_Before[0].checked && !dateRegex.test(Date_Last_Applied.value)) missingInfo+="Please enter a valid date for when you last applied with WRMC.\n";
			
			if(checkRadio(Employed_Here_Before)) missingInfo+="Please let us know if you have been employed here before.\n";
			
			if(Employed_Here_Before[0].checked && !dateRegex.test(Previous_Employment_Start.value)){
			missingInfo+="Please enter a valid start date for your previous employment with WRMC.\n";}
				
			if(Employed_Here_Before[0].checked && !dateRegex.test(Previous_Employment_End.value)){missingInfo+="Please enter a valid end date for your previous employment with WRMC.\n";}

			
			if(checkRadio(Relative_Employee)) missingInfo+="Please let us know if you have a relative who is employed here.\n";
			
			if(Relative_Employee[0].checked && Relative_Relationship.value ==""){
			missingInfo+="Please give a name and relationship for your relative who is employed at WRMC.\n";
			}
			
			if(checkRadio(US_Eligible)) missingInfo+="Please let us know if you are eligible to work in the U.S.\n";
	
			if(Date_Available.value=="") {missingInfo+="Please let us know when you are available to start work.\n";} else{ if (!dateRegex.test(Date_Available.value)) missingInfo+="Please enter a valid date for when you are available to start work.\n";}

			if(Type_Emp_Desired_FT.checked==false && Type_Emp_Desired_PT.checked==false && Type_Emp_Desired_PRN.checked==false) missingInfo+="Please let us know what type of employment you desire (Full-time, Part-time, etc.)\n";
			
			if(Shift_First.checked==false && Shift_Second.checked==false && Shift_Third.checked==false && Shift_Weekend.checked==false) missingInfo+="Please let us know Which shifts would you consider working.\n";
			
			if(checkRadio(Will_Travel)) missingInfo+="Please let us know if you are willing to travel.\n";
			if(checkRadio(Will_Work_Overtime)) missingInfo+="Please let us know if you will work overtime.\n";
			if(checkRadio(Post_Offer_Screening)) missingInfo+="Please let us know if you agree to undergo a post-offer screening.\n";
			if(checkRadio(Ex_Convict)) missingInfo+="Please let us know if you have ever been convicted of a crime.\n";

			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part4');
			}

		break
		
		
		case 'part4':
		// Employment History
			with(frm){
	
			if(Emp1_Company_Name.value=="") missingInfo+="Please enter your most recent employer's Company Name.\n";
			if(Emp1_Address.value=="") missingInfo+="Please enter your most recent employer's Address.\n";
			if(Emp1_City.value=="") missingInfo+="Please enter your most recent employer's City.\n";
			if(Emp1_State.selectedIndex<1) missingInfo+="Please select your most recent employer's State.\n";
			
			/* */
			if(Emp1_ZIP.value=="") {missingInfo+="Please enter your most recent employer's ZIP code.\n";} else {if(!zipRegex.test(Emp1_ZIP.value)) missingInfo+="Please enter a valid ZIP code for your most recent employer.\n";}
			
			/* */
			if(Emp1_Phone.value=="") {missingInfo+="Please enter your most recent employer's Phone.\n";}else{if(!phoneRegex.test(Emp1_Phone.value)) missingInfo+="Please enter a valid phone number for your most recent employer.\n"}
			
			
			if(checkRadio(Emp1_May_Contact)) missingInfo+="Please let us know if we may contact your most recent employer for reference.\n";
			
			if(Emp1_Job_Title.value=="") missingInfo+="Please enter a Job Title for your most recent job.\n";
			
			/* */
			if(Emp1_Start_Date.value==""){
			missingInfo+="Please enter a Start Date for your most recent job.\n"}//else{if(!dateRegex.test(Emp1_Start_Date.value))missingInfo+="Please enter a valid Start Date for your most recent job.\n"}
			
			/* */
			if(Emp1_End_Date.value=="") {missingInfo+="Please enter an End Date for your most recent job.\n"}//else{if(!dateRegex.test(Emp1_End_Date.value))missingInfo+="Please enter a valid End Date for your most recent job.\n"}
			
			
			if(Emp1_Supervisor.value=="") missingInfo+="Please enter the name of your Supervisor at your most recent job.\n";
			
			if(Emp1_Starting_Pay.value=="") missingInfo+="Please enter your Starting Pay at your most recent job.\n";
			
			if(Emp1_Final_Pay.value=="") missingInfo+="Please enter your Final Pay at your most recent job.\n";
			
			if(Emp1_Reason_For_Leaving.value=="") missingInfo+="Please give a reason for leaving your most recent job.\n";
			
			if(Emp1_Duties.value=="") missingInfo+="Please enter your Duties at your most recent job.\n";

			/* */
			if(Emp2_ZIP.value!="" && !zipRegex.test(Emp2_ZIP.value)) missingInfo+="Please enter a valid ZIP code for employer #2.\n";
			
			/* */
			if(Emp2_Phone.value!="" && !phoneRegex.test(Emp2_Phone.value)) missingInfo+="Please enter a valid phone number for employer #2.\n";
			
			/* */
			//if(Emp2_Start_Date.value!="" && !dateRegex.test(Emp2_Start_Date.value))missingInfo+="Please enter a valid Start Date for employer #2.\n";
			
			/* */
			//if(Emp2_End_Date.value!="" && !dateRegex.test(Emp2_End_Date.value))missingInfo+="Please enter a valid End Date for employer #2.\n";



			/* */
			if(Emp3_ZIP.value!="" && !zipRegex.test(Emp3_ZIP.value)) missingInfo+="Please enter a valid ZIP code for employer #3.\n";
			
			/* */
			if(Emp3_Phone.value!="" && !phoneRegex.test(Emp3_Phone.value)) missingInfo+="Please enter a valid phone number for employer #3.\n";
			
			/* */
			//if(Emp3_Start_Date.value!="" && !dateRegex.test(Emp3_Start_Date.value))missingInfo+="Please enter a valid Start Date for employer #3.\n";
			
			/* */
			//if(Emp3_End_Date.value!="" && !dateRegex.test(Emp3_End_Date.value))missingInfo+="Please enter a valid End Date for employer #3.\n";


}
						
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part5');
			}
			
		break
		
		
		case 'part5':
		// Education & Qualifications
			with(frm){
			
			if(Education_Level.selectedIndex<1) {missingInfo+="Please select the highest level of education reached.\n";
			
			} else {
			
				// Count the number of Schools filled out. look at the Name field: 'Schooln_Name'
				
				var schoolCount = 0;
				
				
				for(i=1;i<6;i++){
					
					if(eval("School" + i + "_Name.value")!="") { schoolCount++; }
				
				}
				
				
				//Check requirements for level of education selected
				switch(Education_Level.value){

					case 'High School':
						if(schoolCount<1){missingInfo+="Please list at least one school you attended.\n"}				
					break
				
					case 'Some College':
						if(schoolCount<2){missingInfo+="Please list at least two schools you attended.\n"}				
					break
				
					case 'Associates Degree':
						if(schoolCount<2){missingInfo+="Please list at least two schools you attended.\n"}				
					break
				
					case 'Bachelors Degree':
						if(schoolCount<2){missingInfo+="Please list at least two schools you attended.\n"}				
					break
				
					case 'Masters Degree':
						if(schoolCount<3){missingInfo+="Please list at least three schools you attended.\n"}			
					break
				
					case 'Doctorate':
						if(schoolCount<3){missingInfo+="Please list at least three schools you attended.\n"}			
					break

				}
				
				
				
				// Check each of the Name fields. If the name field is completed, make sure the remaining fields are also
				
				for(i=1;i<6;i++){
				
					if(eval("School" + i + "_Name.value")!="")
						{
						if(eval("School" + i + "_Type.value")=="")
							{missingInfo+="Please select a Type for School " + i + ".\n"}

						if(eval("School" + i + "_Degree.value")=="")
							{missingInfo+="Please enter a Degree Received for School " + i + ".\n"}
						
						if(eval("School" + i + "_Date.value")=="")
							{missingInfo+="Please enter a Completion Date for School " + i + ".\n"}
						// else if(!dateRegex.test(eval("School" + i + "_Date.value")))
						//	{missingInfo+="Please enter a valid Completion Date for School " + i + ".\n"}
						
						if(eval("School" + i + "_Major.value")=="")
							{missingInfo+="Please enter a Major/Minor for School " + i + ".\n"}
												
						}
					
				
				}


			
			}
			
			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part6');
			}
			
		break
		
		
		case 'part6':
		// Affiliations, Awards & Accomplishments
			with(frm){
			
			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part7');
			}
			
		break
		
		
		case 'part7':
		// References
			with(frm){
			
			
			if(Reference1_Name.value=="") 
			missingInfo+="Please enter a Name for your first reference.\n";
			
			if(Reference1_Phone.value=="") {
			missingInfo+="Please enter a Phone Number for your first reference.\n";}
			else if(!phoneRegex.test(Reference1_Phone.value)) missingInfo+="Please enter a valid Phone Number for your first reference.\n";
			
			if(Reference1_Years_Known.value=="") 
			missingInfo+="Please enter Years Known for your first reference.\n";
			
			if(Reference1_Type.selectedIndex<1) 
			missingInfo+="Please indicate a Type of Reference for your first reference.\n";
			
			

			if(Reference2_Name.value=="") 
			missingInfo+="Please enter a Name for your second reference.\n";
			
			if(Reference2_Phone.value=="") {
			missingInfo+="Please enter a Phone Number for your second reference.\n";}
			else if(!phoneRegex.test(Reference2_Phone.value)) missingInfo+="Please enter a valid Phone Number for your second reference.\n";
			
			if(Reference2_Years_Known.value=="") 
			missingInfo+="Please enter Years Known for your second reference.\n";
			
			if(Reference2_Type.selectedIndex<1) 
			missingInfo+="Please indicate a Type of Reference for your second reference.\n";
			
			

			if(Reference3_Name.value=="") 
			missingInfo+="Please enter a Name for your third reference.\n";
			
			if(Reference3_Phone.value=="") {
			missingInfo+="Please enter a Phone Number for your third reference.\n";}
			else if(!phoneRegex.test(Reference3_Phone.value)) missingInfo+="Please enter a valid Phone Number for your third reference.\n";
			
			
			if(Reference3_Years_Known.value=="") 
			missingInfo+="Please enter Years Known for your third reference.\n";
			
			if(Reference3_Type.selectedIndex<1) 
			missingInfo+="Please indicate a Type of Reference for your third reference.\n";

			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
				show('part8');
			}
			
		break
		
		
		case 'part8':
		// Terms
			with(frm){
			
			if(cb_accept.checked==false) missingInfo+="You must accept the terms before submitting your application";
			
			}
			
			if (missingInfo!="") {
				alert(missingInfo);
			}else{
			// turn off postback and submit the form.
				noPostBack(''); 
				frm.submit();
			}
			
		break
		
		
		case 'EEO':
			with(frm){
				if(checkRadio(ethnicity)) 
					{missingInfo+="Please select your ethnicity. If you choose not to answer this question, please indicate that by selecting the first option.\n";} 
				else if(ethnicity[2].checked && checkRadio(race)) { 
					
					if(multiRace.checked==false)
						{missingInfo+="You have chosen 'Not Hispanic or Latino'. Please also select a race identification from one of the 5 choices below.\n";}	
					else {missingInfo+="You have chosen 'Not Hispanic or Latino' and you have checked 'Two or More Races'. Please also select the race/ethnic group with which you most closely identify.\n"}
					
				}				
				
			}
			
			if (missingInfo!=""){
				alert(missingInfo);
			}else{
			// turn of postback and submit the form.
				//noPostBack('');
				frm.submit();
			}
		
		break

	}
}


//Need sub to clear selections from Race/Ethnicity Section
function raceSwitch(action){
	if (action == "off")
	{
		
		off('race');

		frm=document.forms[0];


		for( i = 0; i < frm.race.length; i++ )
			{frm.race[i].checked = false;}
		
		frm.multiRace.checked = false;
		
	} else {
	
		on('race');
	
	}
      
     
}



/*
        Textarea-maxlength by frequency decoder (http://www.frequency-decoder.com/)
        
        Released under a Creative Commons nc-sa license.
        
        ---------------------------------------------------------------------------
        
        These two functions are called from within the namespace so should really
        be declared within the namespace but I've made an exception here as they
        are functions that will inevitably be used by other scripts.
*/
function addEvent( obj, type, fn, tmp ) {
        if( obj.attachEvent ) {
                obj["e"+type+fn] = fn;
                obj[type+fn] = function(){obj["e"+type+fn]( window.event );}
                obj.attachEvent( "on"+type, obj[type+fn] );
        } else
                obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) {
        if( obj.detachEvent ) {
                obj.detachEvent( "on"+type, obj[type+fn] );
                obj[type+fn] = null;
        } else
                obj.removeEventListener( type, fn, false );
}


// Declare the namespace
var fdTextareaController;

(function() {
        function fdTextareaMaxlength(inp, maxlength) {
                this._inp       = inp;
                this._max       = Number(maxlength);
                var self        = this;

                self.maxlength = function() {
                        if(self._inp.disabled) return false;

                        if(self._inp.value.length > self._max) {
                                self._inp.value = self._inp.value.substring(0, self._max);
                                return false;
                        }

                        return true;
                }


                // Has Safari keypress foibles ? Needs tested...
                addEvent(self._inp, 'keypress', self.maxlength, false);

                // Sorry folks, but using the "onblur" event is the only way to cut the text down to size
                // after a users cut & paste action
                addEvent(self._inp, 'blur',     self.maxlength, false);

                // IE only event 'onpaste'

                // Conditional compilation used to load only in IE win.
                // As we don't need the onblur event for IE, we remove it at the same time.

                /*@cc_on @*/
                /*@if (@_win32)
                addEvent(self._inp, 'paste', function(){ setTimeout(self.maxlength, 50); }, true);
                removeEvent(self._inp, 'blur', self.maxlength, false);
                /*@end @*/

                // Call the maxlength function immediately to trim any text inserted server-side to the required length.
                self.maxlength();
        };

        // Construct the previously declared namespace
        fdTextareaController = {
                textareas: [],

                _construct: function( e ) {

                        var regExp_1 = /fd_max_([0-9]+){1}/ig;

                        var textareas = document.getElementsByTagName("textarea");

                        for(var i = 0, textarea; textarea = textareas[i]; i++) {
                                if(textarea.className && textarea.className.search(regExp_1) != -1) {
                                        max = parseInt(textarea.className.match(regExp_1)[0].replace(/fd_max_/ig, ''));
                                        if(max) fdTextareaController.textareas[fdTextareaController.textareas.length] = new fdTextareaMaxlength(textarea, max);
                                }
                        }

                },

                _deconstruct: function( e ) {

                }
        }
})();

// onload events
addEvent(window, 'load', fdTextareaController._construct, false);
addEvent(window, 'unload', fdTextareaController._deconstruct, false);
	
