﻿function QualifierBlock_MakePrefix(blockID)
{
    return "F" + blockID + "_" ;
}
/* This is a copy of the standard FilterBlock_Submit, but with the addition that it can handle radio buttons */
function QualifierBlock_Submit(blockID)
{
    var blockPrefix = QualifierBlock_MakePrefix(blockID) ; 
    var allInputs = document.getElementsByTagName("INPUT") ;
    
    var additionalArgs = "" ;
    var newQueryString = "" ;
    for (var ii=0 ; ii<allInputs.length ; ++ii)
    {
        var myId = allInputs[ii].id ;
    
        if (allInputs[ii].type == "hidden" &&             // it might be a page number
            myId.indexOf(blockPrefix) != 0 &&             // it is not ours so we might want to keep it
            myId.indexOf("_PageNum") == (myId.length - 8) // yes, it is a page number
           )            
            additionalArgs += (additionalArgs?"&":"") + myId + "=" + allInputs[ii].value ;
        else if (allInputs[ii].type == "checkbox" &&   // may be a filter checkbox
                 allInputs[ii].checked &&              // it is checked
                 myId.indexOf(blockPrefix) == 0)       // and it is mine, all mine
            newQueryString += myId + "!" ;
        else if (allInputs[ii].type == "radio" &&       // may be a filter checkbox
                 allInputs[ii].checked &&              // it is checked
                 myId.indexOf(blockPrefix) == 0)       // and it is mine, all mine
            newQueryString += myId + "!" ;
        else if (allInputs[ii].type == "checkbox" &&   // may be a filter checkbox  
                 allInputs[ii].defaultChecked &&       // it was checked when we entered
                 myId.indexOf(blockPrefix) == -1 &&    // it isn't mine
                 (/^F\d*_C\d*_A\d*$/).test(myId)      // but it is definitely a filter checkbox
                )
            newQueryString += myId + "!" ;      
    }
    
    location.search = "?FB_Values=" + newQueryString + (additionalArgs?"&":"") + additionalArgs ;
}

/* This is a custom function for when the form is submitted on the qualifier email portion of the block */
function QualifierBlock_EmailSubmit(blockID, queryFilters)
{    
    /* first validate any controls */
    var validControl = QualifierBlock_ValidateField(blockID, '_Email', /^ *[\w.-]+@[\w.-]*\.\w* *$/ ) ;

    if(validControl)    
    {
        var userName = document.getElementById('F' + blockID + '_Name').value ;
        var userEmail = document.getElementById('F' + blockID + '_Email').value ;
        var userPhone = document.getElementById('F' + blockID + '_Phone').value ;
        var userFax = document.getElementById('F' + blockID + '_Fax').value ;
        var userSubscribe = document.getElementById('F' + blockID + '_More').checked ;
        
        location.search="?FB_Values=" + queryFilters + "&" + 'E_Values=' + userName + '!' + userEmail + '!' + userPhone + '!' + userFax + '!' + userSubscribe ;
    }
    else
    {
        document.getElementById(blockID + '_errMsg').style.display = 'block' ;   
    }
}

function QualifierBlock_ValidateField(blockID, ctlName, regEx)
{
    var ctl = document.getElementById('F' + blockID + ctlName) ;
    if (!ctl)
        return false ; 
    return (ctl.value.match(regEx) != null); 
}

