/*********************************************************************
// CHANGE LOCATION FIELDS PAGE FUNCTIONS
*****************************************************************/

/*********************************************************************
'***    Function: getLocationFieldValue
'***
'***    Parameters: fieldId - state field form element id
'***		
'***    Returns: element's value (may be StateCode or Province)
'***    Remarks: 
'***        
'***    Created by: niloa
'***    Changed by: 
'***    Last change: 06/22/2009
'*********************************************************************/
function getLocationFieldValue(fieldId) {
    
    if (typeof (fieldId) != "string")
        return "";
    
    var field = document.getElementById(fieldId);
    
    //NOTE: field type name will either be SELECT (ListBox)
    //this code will dynamically query the form field type to validate state value
    if (field == null)
        return "";

    //get the node name (uppercase string for consistency)
    var fieldTagName = new String(field.tagName).toUpperCase();

    //if field type is input (text box) get province value
    if (fieldTagName == "INPUT")
        return field.value;
    else if (fieldTagName == "SELECT") {
        var selectedIndex = field.selectedIndex;
        return field.options[selectedIndex].value;
    }
    return "";
}

/*********************************************************************
'***    Function: showStateField
'***
'***    Parameters: 
'***		
'***    Returns: void
'***    Remarks: uses ajax to show change state field by country
'***        if country has state entries, a listbox will appear
'***        else, a textfield a appear for province
'***    Created by: niloa
'***    Changed by: rburdan
'***    Last change: 09/21/11
'*********************************************************************/
function showStateField(fieldIdSuffix, showStateALL) {
    //set to empty string by default
    if (fieldIdSuffix == null) fieldIdSuffix = "";

    showStateALL = (showStateALL == null) ? false : showStateALL;

    var strQS_showStateALL = "";

    //get selected state field value from hidden input field
    var stateValue = getLocationFieldValue("OrigStateCode" + fieldIdSuffix);
    if (stateValue == "")
        stateValue = getLocationFieldValue("StateCode" + fieldIdSuffix);

    var countryId = getLocationFieldValue("CountryID" + fieldIdSuffix);

    //get field id suffix previously set to hidden input field
    var stateFieldName = "StateCode" + fieldIdSuffix;
    var dir = "";

    //if viewing from http://secure.searchease.com, move up one directory
    dir = isSecureClientSite() ? "../Components" : "/Components";

    if (showStateALL)
        strQS_showStateALL = "&fltr=y";

    //build the url with necessary params
    var url = dir + "/GetStateListBoxForCountry.asp?CountryID=" + countryId +
                        "&Name=" + stateFieldName + "&StateValue=" + stateValue + strQS_showStateALL;

    //get state field container id for embedding url request result html
    var stateFieldCntId = "StateFieldCnt" + fieldIdSuffix;
    AjaxGet(url, stateFieldCntId, "unable to load this field...");
}

/*********************************************************************
'***    Function: getStateValidationMessage
'***
'***    Parameters: 
'***		stateFieldId - state field id may be a 
'***                        listbox (select) or textfield object
'***		blnIsProvinceRequired - true, if must validate province 
'***
'***    Returns: string
'***    Remarks: returns validation message for state field
'***        Code dynamically queries field by element type
'***        field type may be either:
'***            * Listbox (SELECT) with StateCodes, or
'***            * Text field for Province
'***
'***    Created by: niloa
'***    Changed by: 
'***    Last change: 06/15/2009
'*********************************************************************/
function getStateValidationMessage(stateFieldId, blnRequireProvince) {
    //validate state code field
    var strMessage = "";

    //NOTE: field type name will either be a SELECT (ListBox) or text field
    //this code will dynamically query the form field type to validate state value
    var state = document.getElementById(stateFieldId);
    if (state == null)
        return "";
    
    //get the node name (as uppercase string for consistency)
    var fieldTagName = new String(state.tagName).toUpperCase();

    //if field type is input (text box) get province value
    if (fieldTagName == "INPUT") {
        if (typeof (blnRequireProvince) != "boolean")
            blnRequireProvince = false;

        if ((Trim(state.value) == "") && blnRequireProvince)
            strMessage = "Please enter a Province.";
    }
    //if field type is select (list box) get state code value
    else if (fieldTagName == "SELECT") {
        if (state.selectedIndex == 0)
            strMessage = "Please select a State.";
    }

    return strMessage;
}

/*********************************************************************
'***    Function: showStateFieldWithCallback
'***
'***    Parameters: 
        onSuccessFunc - callback func. if op. is successful
        onCompleteFunc- callback func. if op. is complete
'***		
'***    Returns: void
'***    Remarks: uses ajax to show change state field by country
'***        if country has state entries, a listbox will appear
'***        else, a textfield a appear for province
'***    Created by: vadymn
'***    Changed by: 
'***    Last change: 08/09/2010
'*********************************************************************/
function showStateFieldWithCallback(fieldIdSuffix, OnSuccessFunc, onCompleteFunc) {
    //set to empty string by default
    if (fieldIdSuffix == null) fieldIdSuffix = "";

    //get selected state field value from hidden input field
    var stateValue = getLocationFieldValue("OrigStateCode" + fieldIdSuffix);
    if (stateValue == "")
        stateValue = getLocationFieldValue("StateCode" + fieldIdSuffix);
    
    var countryId = getLocationFieldValue("CountryID" + fieldIdSuffix);
    
    //get field id suffix previously set to hidden input field
    var stateFieldName = "StateCode" + fieldIdSuffix;
    var dir = "";
    
    //if viewing from http://secure.searchease.com, move up one directory
    dir = isSecureClientSite() ? "../Components" : "/Components";

    //build the url with necessary params
    var url = dir + "/GetStateListBoxForCountry.asp?CountryID=" + countryId +
                "&Name=" + stateFieldName + "&StateValue=" + stateValue;
    
    //get state field container id for embedding url request result html
    var stateFieldCntId = "StateFieldCnt" + fieldIdSuffix;
    AjaxGetWithCallback(url, stateFieldCntId, "unable to load this field...", OnSuccessFunc, onCompleteFunc);
}

