///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// This set of functions are general includes for validation
// They are designed in pairs the validation and the event function
// the event function will call the validation with the event src
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

var props;

//////////////////////////////////////////////////////
//Validation function for application forms, built
//upon objVal.  Objects created on page load, with all
//properties set.  This function is universal for all
//forms but contains a few proprietary lines.
//////////////////////////////////////////////////////
function ValidateForm(frm){
    
    BuildFieldObjects(frm)
    
    for (var i=0;i<=fldArray.length-1;i++){
        
        var fld = fldArray[i].mGetFieldObject()
        
        /////////////////////////////////////////////////////
        //If the form only contains one element with the
        //given name, the length property isNaN.
        //We also have to check to see what type of control
        //we are working with to make sure that the .length
        //property is valid.  E.g. A select control's length
        //property contains the number of options is has.
        /////////////////////////////////////////////////////
    

        if (fldArray[i].pType=='Selection' || fldArray[i].pType=='Radio'){
            //TODO: Determine how to count the number
            //of <Select> tags with the same name.
            var fldCount = new Number(1)
        }
        //There must be more than one element.
        else{
            if (isNaN(fld.length)==true){
                var fldCount = new Number(1)
            }
            else{
                var fldCount = new Number(fld.length)
            }
        }
        
        //Now let's perform the validation
        if (fldCount > 1){
            
            for (var j=0; j < fldCount; j++){
                
                //Create the new object based on the
                //base object from fldArray[i].
                var objValNew = fldArray[i].mCopy(j)
                
                switch (frm.processCode.value){
                    case 'c' || 'd' || 'e':
                        if(objValNew.mGetFieldObject().value==''){
                            bolNeedsValidation = false;
                        }
                        else{
                            bolNeedsValidation = true;
                        }
                    default:
                        bolNeedsValidation = true;
                }
                
                if (bolNeedsValidation==true){
                    if (objValNew.mValidate(true)==false){
                        return false;
                    }
                }
            }
        }
        else{
            if (fldArray[i].mValidate(true)==false){
                return false;
            }
        }
    }
    return true;
}
//////////////////////////////////////////////////////


//////////////////////////////////////////////////////
//Sets the placeholder for checkboxes since an 
//unchecked checkbox sends no value during post in asp
//////////////////////////////////////////////////////
function SetCheckFlag(val, name, formName, idx){
    var fld = eval("document."+formName+"."+name+"["+idx+"]")
    if (val==true){
        fld.value = '1'
    }
    else{
        fld.value = '0'
    }
}
//////////////////////////////////////////////////////



//////////////////////////////////////////////////////
//Used in app_g to show or hide the EEOC fields.
//////////////////////////////////////////////////////
function ToggleParticipation(bitParticipateValue){
    var frm = document.g;
    
    if(bitParticipateValue==false){
        divEEOC.style.display='';
    }
    else{
        divEEOC.style.display='none';
    }
}
//////////////////////////////////////////////////////



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) d=document; 
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }

    if(!(x=d[n])&&d.all) 
        
        x=d.all[n]; 
        
        for (i=0;!x&&i<d.forms.length;i++) 
            x=d.forms[i][n];
        
        for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
            x=MM_findObj(n,d.layers[i].document);
        
        if(!x && d.getElementById) x=d.getElementById(n); 
            return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  
  document.MM_sr=new Array; 
  
  for(i=0;i<(a.length-2);i+=3)

    if ((x=MM_findObj(a[i]))!=null){
    
        document.MM_sr[j++]=x; 
        if(!x.oSrc) 
            x.oSrc=x.src; 
            x.src=a[i+2];
    }
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

//////////////////////////////////////////////////////
//Show popup window
//////////////////////////////////////////////////////
function ShowApplicationWindow(url, prompt, promptText){
    
    if (prompt==true){
        var proceed = confirm(promptText)
    }
    else{
        var proceed = true
    }
    
    if (proceed==true){
        props=window.open(url, 'popup', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubars=0, resizable=1, width=800, height=600 left=0, top=0, title=<%=CONST_TITLE%>, modal=1');
        props.focus()
    }
    else{
        alert('Cancellation Confirmed')
    }
                
}

function CheckPopup(){
    try {props.focus()}catch(e){}
}

/////////////////////////////////////////////////////
function VerifyApp(url){
    var msg = 'Please make sure your resume is up-to-date before applying.\nClick OK to apply without updating your resume. Click Cancel and return to My Info page to update your resume.'
    if (confirm(msg)==true){
        window.location = url
    }
}

function display_name(item) {
    var strDisplay = item.getAttribute("DisplayName");
    if (strDisplay==null || strDisplay=="")
        strDisplay="Field";
    return strDisplay;
}

function default_value(item) {
    var strDefault = item.defaultValue;
    if (strDefault==null || strDefault=="")
        strDefault="";
    return strDefault;
}

function trim_string() {
    var ichar, icount;
    var strValue = this;
    ichar = strValue.length - 1;
    icount = -1;
    while (strValue.charAt(ichar)==' ' && ichar > icount)
        --ichar;
    if (ichar!=(strValue.length-1))
        strValue = strValue.slice(0,ichar+1);
    ichar = 0;
    icount = strValue.length - 1;
    while (strValue.charAt(ichar)==' ' && ichar < icount)
        ++ichar;
    if (ichar!=0)
        strValue = strValue.slice(ichar,strValue.length);
    return strValue;
}
function date_toSimpleForm() {
    var toSimpleForm = new String;
    toSimpleForm = this.toLocaleString();
    toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
    return toSimpleForm;
}


function vs_non_blank(item) {
    item.value=item.value.Trim();
    if (item.value.length==0) {
        item.focus();
        return false;
    }
    return true;
}
function vs_valid_number(item) {
    var strDefault = item.getAttribute('DefaultValue');
    if (strDefault.length==0) {
        strDefault="0";
    }
    item.value=item.value.Trim();
    if (item.value.length==0)
        item.value=strDefault;
    var num = ".0123456789";
    for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
        if (num.indexOf(item.value.charAt(intLoop)) == -1) {
            item.focus();
            alert(strErrorMsg);
            return false;
        }
    }
    if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {
        item.focus();
        alert(strErrorMsg);
        return false;
    }
    return true;
}
function vs_valid_hours(item) {
    if (!vs_valid_number(item))
        return false;
        var itemValue = new Number(item.value);
    if ((itemValue < 0 || itemValue > 80)) {
        item.focus();       
        return false;
    }
    itemValue *= 4;
    if ((itemValue)!=Math.ceil(itemValue)) {
        item.focus();
        return false;
    }
    return true;
}
function vs_valid_date(item) {
    if (isNaN(Date.parse(item.value))) {
        item.focus();
        return false;
    }
    var dtItem = new Date(Date.parse(item.value));
    //item.value = dtItem.toSimpleForm();
    return true;
}
function vs_item_selected(item) {
    if (item.selectedIndex==0) {
        item.focus();
        return false;
    }
    return true;
}

function vs_radio_selected(item) {
    
    for(var i=0;i<item.length;i++){
        if(item[i].checked){
            return true;
        }
    }
    return false;
}

function vs_valid_zip(item) {
    item.value=item.value.Trim();
    if (!(/^\d{5}$/.test(item.value) || /^\d{5}-\d{4}$/.test(item.value))) {
        item.focus();
        return false;
    }
    return true;
}
function vs_valid_ssnbr(item) {
    item.value=item.value.Trim();
    if (!(/^\d{3}-\d{2}-\d{4}$/.test(item.value))) {
        item.focus();
        return false;
    }
    return true;
}
function vs_valid_email(item) {
    //item.value=item.value.Trim();
    //if (!(/^[\w\.]+@[a-z\.]+$/.test(item.value))) {
    //  return false;
    //}
    return true;
}


function es_valid_ssnbr() {
    var item = event.srcElement;
    event.returnValue = vs_valid_ssnbr(item);
}
function es_non_blank() {
    var item = event.srcElement;
    event.returnValue = vs_non_blank(item);
}
function es_valid_number() {
    var item = event.srcElement;
    event.returnValue = vs_valid_number(item);
}
function es_valid_hours() {
    var item = event.srcElement;
    event.returnValue = vs_valid_hours(item);
}
function es_valid_date() {
    var item = event.srcElement;
    event.returnValue = vs_valid_date(item);
}
function es_item_selected() {
    var item = event.srcElement;
    event.returnValue = vs_item_selected(item);
}
function es_valid_zip() {
    var item = event.srcElement;
    event.returnValue = vs_valid_zip(item);
}
function es_valid_email() {
    var item = event.srcElement;
    event.returnValue = vs_valid_email(item);
}


// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;

// This set of function are for processing the key press event
// Used to restrict input on numerics and pure textual fields

function kp_integer() {
    if ((event.keyCode < 48 || event.keyCode > 57))
        event.returnValue = false;
}
function kp_numeric() {
    if ((event.keyCode != 46) && (event.keyCode < 48 || event.keyCode > 57))
        event.returnValue = false;
    if (event.keyCode == 46) {
        if (event.srcElement.value.indexOf(".") > -1)
            event.returnValue = false;
    }
}


function kp_character() {
    if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122))
        event.returnValue = false;
}
function kp_convert_upper() {
    if ((event.keyCode >= 97 && event.keyCode <= 122))
        event.keyCode -= 32;
}
function kp_convert_lower() {
    if ((event.keyCode >= 65 && event.keyCode <= 90))
        event.keyCode += 32;
}


function kp_setup() {
    this.Integer = kp_integer;
    this.Numeric = kp_numeric;
    this.Character = kp_character;
    this.ConvertUpper = kp_convert_upper;
    this.ConvertLower = kp_convert_lower;
    return this;
}

var keyPressInput = new Object;
keyPressInput = kp_setup();