//////////////////////////////////////////////////////////////
// Common functions
// v1.00 (Budd Wright)
//
// Description:
//////////////////////////////////////////////////////////////
// Contains common page functions
//////////////////////////////////////////////////////////////
//
// Usage:
//////////////////////////////////////////////////////////////
// <script language="javascript" src="js/common.js"></script>
//////////////////////////////////////////////////////////////
//
// v1.00 Notes
// -----------
// Requires pageName variable to be set elsewhere
//
// Known Issues
// ------------
// 
//////////////////////////////////////////////////////////////

/* pageName variable */
var pageName = location.href.substring(0, location.href.lastIndexOf("/") + 1);
	pageName = location.href.substring(pageName.length, location.href.length + 1);
	pageName = pageName.substring(0, pageName.indexOf("?"));




/* Action function */
// Jumps to a specified action for a given user
function Action(action)
{
	// Check for pageName variable
	if(pageName == null)
	{
		alert("The pageName variable must be set for this function to work.");
		return;
	}


	location.href = pageName + "?action=" + action;
}


/* ConfirmDelete function */
// Forces a user to confirm a delete action on a list page
function ConfirmDelete(action, formName)
{
	// One item must be marked for deletion
	var checked = false;
	var checkboxes = document.getElementsByName("deleteID[]");


	for(var x = 0; x < checkboxes.length; x++)
	{
		if(checkboxes[x].checked)
		{
			checked = true;
			break;
		}
	}

	if(! checked)
	{
		alert("You must select at least one item to be deleted before clicking the Delete button.");
		return;
	}


	// Confirm the delete
	if(confirm("Are you sure you want to delete these items? This action cannot be reversed."))
	{
		document.getElementById( formName ).submit();
	}
}



/* ValidateApplication function */
// Validates a user submitted application
function ValidateApplication()
{
	// Check validation requirements
	if( document.getElementById("requireValidation").value == 0 )
	{
		document.getElementById("editorFormTag").submit();
		return;
	}

	// Ensure a file has been uploaded if user selected that option
	var resumeOptions = document.getElementsByName("applicationOnFile");
	var resumeField = document.getElementById("resume");

	if(resumeOptions[0].checked && resumeField.value.length == 0)
	{
		alert("Please click the Browse button and select a resume to upload to Helpmates before continuing.");
		return;
	}
	else if(! resumeOptions[0].checked && resumeField.value.length > 0)
	{
		alert("You've selected a resume file, but have chosen a different option for submitting your resume. Please clear the resume file field if you are not submitting a resume via upload at this time.");
		return;
	} 
    else if( resumeOptions[0].checked && resumeField.value.length > 0)
    {
    
         var strLocation =resumeField.value;
         var strFile = strLocation.split(".");
         var arrFileExtension = new Array(3)
            arrFileExtension[0] = "DOC";
            arrFileExtension[1] = "PDF";
            arrFileExtension[2] = "TXT";
            arrFileExtension[3] = "DOCX";
         var len = arrFileExtension.length;
         var x=0;
         var found=false;
         for (x=0; x<len; x++)
         {
            if (strFile[strFile.length - 1].toUpperCase() == arrFileExtension[x].toUpperCase())
               {
                   found=true;
                   continue;
               } 
         }   
         if (! found == true) 
             {
                   alert("Sorry, we accept only DOC, DOCX, TXT, and PDF type resume submissions. Please upload only one of those file types");
                   return;
             }
         
    }
    // Ensure that at least one phone field is completed
    var phoneField = document.getElementById("phone");
    var cellField = document.getElementById("cellphone");
    
    if(phoneField.value.length == 0 && cellField.value.length == 0)
    { 
        alert("You must complete either the phone or cell phone field.");
        return;
    }
    
       
	// Validate the rest of the form
	ValidateForm("editorForm");
}


function ValidateResume()
{
    
    // Check validation requirements
    if( document.getElementById("requireValidation").value == 0 )
    {

        document.getElementById("editorFormTag").submit();
        return;
    }

    
    // Ensure that at least one phone field is completed
    var phoneField = document.getElementById("phone");
    var cellField = document.getElementById("cellphone");
    
    if(phoneField.value.length == 0 && cellField.value.length == 0)
    { 
        alert("You must complete either the phone or cell phone field.");
        return;
    }

        
       
    // Validate the rest of the form
    ValidateForm("editorForm");
}

function ValidateResumeConfirm()
{
    
    // Check validation requirements
    if( document.getElementById("requireValidation").value == 0 )
    {

        document.getElementById("editorFormTag").submit();
        return;
    }

    var resumeField = document.getElementById("resume");

    if(resumeField.value.length == 0)
    {
        alert("Please click the Browse button and select a resume to upload to Helpmates before continuing.");
        return;
    }
    else if(resumeField.value.length > 0)
    {
    
         var strLocation =resumeField.value;
         var strFile = strLocation.split(".");
         var arrFileExtension = new Array(3)
            arrFileExtension[0] = "DOC";
            arrFileExtension[1] = "PDF";
            arrFileExtension[2] = "TXT";
            arrFileExtension[3] = "DOCX";
         var len = arrFileExtension.length;
         var x=0;
         var found=false;
         for (x=0; x<len; x++)
         {
            if (strFile[strFile.length - 1].toUpperCase() == arrFileExtension[x].toUpperCase())
               {
                   found=true;
                   continue;
               } 
         }   
         if (! found == true) 
             {
                   alert("Sorry, we accept only DOC, DOCX, TXT, and PDF type resume submissions. Please upload only one of those file types");
                   return;
             }
         
    }
    
    // Ensure that at least one phone field is completed
    var phoneField = document.getElementById("phone");
    var cellField = document.getElementById("cellphone");
    
    if(phoneField.value.length == 0 && cellField.value.length == 0)
    { 
        alert("You must complete either the phone or cell phone field.");
        return;
    }

        
       
    // Validate the rest of the form
    ValidateForm("editorForm");
}

/* GoBa
ck function */
// Lets a form go back to update user-entered information
function GoBack( page, pageaction )
{
	document.getElementById("editorFormTag").action = page + "?action=" + pageaction;
	document.getElementById("requireValidation").value = 0;
}



/* SelectAllLocations */
function SelectAllLocations(isChecked)
{
	if(! isChecked)
		return;

	var options = document.getElementsByName("officeID[]");

	for(var x = 0; x < options.length; x++)
	{
		options[x].checked = true;
	}
}