/* START generic functions */

function checkForRequiredFields() {
    var requiredFieldArray = ['firstName','lastName','postalCode','phoneNumber','emailAddress'];
    var rfe = '';
    var rfeName = '';
    var alertMessage = '';
    for (i=0;i<requiredFieldArray.length;i++) {
        if (document.getElementById(requiredFieldArray[i]).value == '') {
            rfeName = requiredFieldArray[i] + '__label';
            rfe = document.getElementById(rfeName);
            alertMessage = alertMessage + rfe.value + '\n';
        }
    }
    if (alertMessage != '') {
        alertMessage = 'All required fields must be completed:\n' + alertMessage;
        alert(alertMessage);
        return false; 
    }
    return true;
}

function validateRadioParentTextChild(formId, parentRadioId, whatParentRadioValue, childTextId) {
    var thisForm = document.forms[formId];
    var parentRadioObject = thisForm[parentRadioId];
    var childTextObject = thisForm[childTextId];
    var isParentRadioChecked = false;
    var parentRadioValue = '';
    var isChildTextFilled = false;
    var i = 0;
    
    
    for (i=0; i<parentRadioObject.length; i++) {
        if (parentRadioObject[i].checked) {
            isParentRadioChecked = true;
            parentRadioValue = parentRadioObject[i].value;
        }
    }
    if (!isParentRadioChecked) {
        return 'parent';
    } else {
        if (parentRadioValue == whatParentRadioValue) {
            if (childTextObject.value == '') {
                return 'child';
            }
        }
        return 'none';
    }
}

function validateParentRadioChildRadio(formId, parentRadioId, whatParentRadioValue, childRadioId) {
    var thisForm = document.forms[formId];
    var parentRadioObject = thisForm[parentRadioId];
    var childRadioObject = thisForm[childRadioId];
    var isParentRadioChecked = false;
    var parentRadioValue = '';
    var isChildRadioChecked = false;
    var i = 0;
    
    for (i=0; i<parentRadioObject.length; i++) {
        if (parentRadioObject[i].checked) {
            isParentRadioChecked = true;
            parentRadioValue = parentRadioObject[i].value;
        }
    }
    if (!isParentRadioChecked) {
        return 'parent';
    } else {
        if (parentRadioValue == whatParentRadioValue) {
            for (i=0; i<childRadioObject.length; i++) {
                if (childRadioObject[i].checked) {
                    isChildRadioChecked = true;
                }
            }
            if (!isChildRadioChecked) {
                return 'child';
            }
        }
    }
    return 'none';
}

function validateEmail(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    
    if (str.indexOf(at)==-1){
     return false
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
    }
    
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
    }
    
    if (str.indexOf(at,(lat+1))!=-1){
      return false
    }
    
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
    }
    
    if (str.indexOf(dot,(lat+2))==-1){
      return false
    }
    
    if (str.indexOf(" ")!=-1){
      return false
    }
    
    return true					
}

/* allows show/hide of css elements */
function changeElementDisplay(showOrHide, elementID) {
    if (showOrHide == 'hide') {
        if (document.getElementById(elementID)) {
            document.getElementById(elementID).style.display = 'none';
        }
    }
    else if (showOrHide == 'show') {
        if (document.getElementById(elementID)) {
            document.getElementById(elementID).style.display = 'block';
        }
    }
}

function updateNav(mainNavTab, subNavTab) {
    if (!document.getElementById) return false;
    if (!document.getElementsByTagName) return false;
    
    if (mainNavTab != '' && document.getElementById('mainNav')) {
        var mainNav = document.getElementById('mainNav');
        var mnLinks = mainNav.getElementsByTagName('a');
        for (i=0;i<mnLinks.length;i++) {
            if (mnLinks[i].innerHTML.toLowerCase() == mainNavTab.toLowerCase()) {
                mnLinks[i].className = 'current';
            }
        }
    }
    
    if (subNavTab != ''  && document.getElementById('subNav')) {
        var subNav = document.getElementById('subNav');
        var snLinks = subNav.getElementsByTagName('a');
        for (i=0;i<snLinks.length;i++) {
            if (snLinks[i].innerHTML.toLowerCase() == subNavTab.toLowerCase()) {
                snLinks[i].className = 'current';
            }
        }
    }    
}

function highlightFieldWithError(formFieldLabelID, formFieldID) {
    /* change this value to change the color used to highlight the form field */
    var formFieldHighlightColor = '#ff8080'
    var x = document.getElementById(formFieldLabelID);
    var y = document.getElementById(formFieldID);
    x.style.color = 'Red';
    x.style.fontWeight = 'bold';
    y.style.background = formFieldHighlightColor;
}
function changeFieldStyleToNormal (formFieldLabelID, formFieldID) {
    var x = document.getElementById(formFieldLabelID);
    var y = document.getElementById(formFieldID);
    x.style.color = '';
    x.style.fontWeight = '';
    y.style.background = '';
}
function hideStatusMessage(statusMessageID) {
    var x = document.getElementById(statusMessageID);
    x.style.visibility = 'hidden';
}

/* END generic functions */
function showLoading(id) {
    var x = document.getElementById(id);
    x.innerHTML = '<div style=\"text-align:center; width:580px;\"><div style=\"margin:0 auto; width:66px;\"><img src=\"/images/ajax-loader.gif\" width=\"66\" height=\"66\"></div></div>';    
}

function putValInVar(valToPut) {
    jsDebugVar = valToPut;
}

function showVal(valToShow) {
    var x = document.getElementById('jsDebug');
    x.innerHTML = valToShow;
}

function displayResults(sr) {
    document.getElementById('gsaSearchResultsContainer').innerHTML = sr;
}

function validateContactForm() {
    var requiredTextFieldArray = ['contactName','professionalTitle','companyName','streetAddress1','cityName','postalCode','phoneNumber','emailAddress'];
    var requiredSelectFieldArray = ['stateOrProvince'];
    var alertMessage = '';
    var thisField = '';
    var thisSelect = '';
    var i = 0;
    var k = 0;
    
    
    // check for all required text fields
    for (i=0;i<requiredTextFieldArray.length;i++) {
        thisField = document.getElementById(requiredTextFieldArray[i]);
        if (thisField.value == '') {
            alertMessage = alertMessage + thisField.getAttribute('displayName') + '\n';
        }
    }
    
    // validate required selects fields
    for (k=0;k<requiredSelectFieldArray.length;k++) {
        thisSelect = document.getElementById(requiredSelectFieldArray[k]);
        if (thisSelect.options[thisSelect.selectedIndex].value == '') {
            alertMessage = alertMessage + thisSelect.getAttribute('displayName') + '\n';
        }
    }
    
    if (alertMessage != '') {
        alertMessage = 'All required fields must be completed:\n\n' + alertMessage;
        alert(alertMessage);
        return false; 
    }
    return true;
}


