// Client side validation routines
MRG_Helpers = function() {
    /// <summary>
    /// The _Helpers class contains functionality utilized across a number
    /// of controls (but not universally)
    /// </summary>
    /// <remarks>
    /// You should not create new instances of _Helpers.  Instead you should use the shared instance MRGHelpers (or MRGHelpers).
    /// </remarks>
}
MRG_Helpers.prototype = {
    keyDown : function(iKeyCode, sFunc, e){
        if (e == null)
		    e = window.event;

	    if (e.keyCode == iKeyCode)
	    {
		    eval(unescape(sFunc));
		    return false;
	    }
    },
	uncheck : function (btn1, btn2)
	{
		btn1.checked = false;
		btn2.checked = false;
	},
    adjustDivs : function()
    {
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop  = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var clientWidth;
        if (window.innerWidth) {
                clientWidth = (window.__safari ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
            } else {
                clientWidth = document.documentElement.clientWidth;
            }
        var clientHeight;
        if (window.innerHeight) {
            clientHeight = (window.__safari ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
        } else {
            clientHeight = document.documentElement.clientHeight;
        }
        var i=0;
        var df;
        var foreItem;
        for(i=0;i<divs.length;i++)
        {
            df=document.getElementById(divs[i]['Template']);
            foreItem=document.getElementById(divs[i]['Display']);
            if(df != null && foreItem != null)
            {
                df.style.position='fixed';
                foreItem.style.position='fixed';
                df.style.width = clientWidth+'px';
                df.style.height = clientHeight+'px';
                df.style.display = '';
                foreItem.style.display = '';
            }
        }
    },
    hideDivs : function(){
        for(i=0;i<divs.length;i++)
        {
            df=document.getElementById(divs[i]['Template']);
            foreItem=document.getElementById(divs[i]['Display']);
            if(df != null && foreItem != null)
            {
                df.style.position='';
                foreItem.style.position='';
                //df.style.width = clientWidth+'px';
                //df.style.height = clientHeight+'px';
                df.style.display = 'none';
                foreItem.style.display = 'none';
            }
        }
    },
    autoTab : function(currentInput, id, length){
    
            if (currentInput.id.indexOf('FC') > -1 && id.id.indexOf('AR') && length == 1)
            {
                length = 3;
            }
            if(currentInput.value.length == length)
            {
            document.getElementById(id.id).focus();
            }
            else
            {
               document.getElementById(currentInput.id).focus();
            }

    },
    setRowBackgroundColor : function(checkbox, otherCheckboxId, selectedBackColor, normalBackColor, showHeader){

        if(checkbox){
            var td = checkbox.parentNode;
            if (td == null) 
                return; 
            var tr = td.parentNode;
            var otherCheckbox = document.getElementById(otherCheckboxId);
            if(tr == null)
                return;
            if(showHeader)
                tr = tr.parentNode;
            if(tr == null)
                return;
            if(otherCheckbox){
                if(checkbox.checked || otherCheckbox.checked){
                    tr.style.backgroundColor = selectedBackColor;
                }
                else{
                    tr.style.backgroundColor = normalBackColor;
                }
            }
            else{            
                if(checkbox.checked){
                    tr.style.backgroundColor = selectedBackColor;
                }
                else{
                    tr.style.backgroundColor = normalBackColor;
                }
            }
        }
    },
    setOtherCheckbox : function(checked, checkbox, forceCheck, forceUncheck){

        var anyPrintAndShip = false;
        
        var respondentAtHand = 0;
        
        if(checkbox.indexOf("_FullWFPO_") > - 1){
        
            respondentAtHand = checkbox.replace(checkbox.substring((checkbox.indexOf("_FullWFPO_") +10), (checkbox.indexOf("_FullWFPO_", checkbox.indexOf("_FullWFPO_")) - checkbox.indexOf("_FullWFPO_")  -10)), "");
        } 
        
        var e =  document.getElementById(checkbox);
        if(e){
            if(checked && forceCheck){
                e.checked = checked;
            }
            if(!checked && forceUncheck){
                    e.checked = checked;
            }
        }
        
        var inputs = document.getElementsByTagName("input");
        var images = document.getElementsByTagName("img");
        
        for(var i = 0; i < inputs.length; i++){
            
            if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].id.indexOf('PrintAndShip') > -1 && inputs[i].id != 'checkbox' && inputs[i].checked == true){
                anyPrintAndShip = true;
            }
        
             //uncheck the corresponding FPO if full report is being checked
            if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].id.indexOf('_FPO_') > -1){
                if (respondentAtHand != 0)
                {
                    var RespondentTiedToCurrentCheckbox = inputs[i].id.replace(inputs[i].id.substring((inputs[i].id.indexOf("_FPO_") +5), (inputs[i].id.indexOf("_FPO_", inputs[i].id.indexOf("_FPO_")) - inputs[i].id.indexOf("_FPO_")  -5)), "");
                    
                    if (respondentAtHand == RespondentTiedToCurrentCheckbox){
                    
                        var currentCheckBox = inputs[i];
                        currentCheckBox.checked = false;
                        $mrg_helpers.setRowBackgroundColor(currentCheckBox, null, 'Bisque', 'Transparent', false);
                    }
                
                }
            }
           
            
        }
        
        for(var x = 0; x < images.length; x++){ 
            if ((images[x].id.indexOf('PreferredShipper') > -1 && anyPrintAndShip == true) || (images[x].id.indexOf('ShipToRequired') > -1 && anyPrintAndShip == true) || (images[x].id.indexOf('ShippingMethodRequired') > -1 && anyPrintAndShip == true)){
        
                images[x].style.display = 'block';  
                          
            }
            else if((images[x].id.indexOf('PreferredShipper') > -1 && anyPrintAndShip == false) ||(images[x].id.indexOf('ShipToRequired') > -1 && anyPrintAndShip == false)||(images[x].id.indexOf('ShippingMethodRequired') > -1 && anyPrintAndShip == false)){
            
                images[x].style.display = 'none';  
            }
        }
    },
        setOtherCheckboxFacilitator : function(checked, checkbox, forceCheck, forceUncheck){

        var anyPrintAndShip = false;
        var respondentAtHand = 0;
        
        if(checkbox.indexOf("_FPO_") > - 1){
        
            respondentAtHand = checkbox.replace(checkbox.substring((checkbox.indexOf("_FPO_") +5), (checkbox.indexOf("_FPO_", checkbox.indexOf("_FPO_")) - checkbox.indexOf("_FPO_")  -5)), "");
        } 
        
        var e = document.getElementById(checkbox);
        if(e){
            if(checked && forceCheck){
                e.checked = checked;
            }
            if(!checked && forceUncheck){
                    e.checked = checked;
            }
         }
         
        var inputs = document.getElementsByTagName("input");
        var images = document.getElementsByTagName("img");
               
        var PrintAndShipChecked = false;
        
        for(var i = 0; i < inputs.length; i++){
        
      
            if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].id.indexOf('PrintAndShip') > -1 && inputs[i].id != checkbox && inputs[i].checked == true){
                anyPrintAndShip = true;
            }
            
            //uncheck the corresponding full report if FPO is being checked
            if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].id.indexOf('FullWFPO') > -1){
                if (respondentAtHand != 0)
                {
                    var RespondentTiedToCurrentCheckbox = inputs[i].id.replace(inputs[i].id.substring((inputs[i].id.indexOf("_FullWFPO_") +10), (inputs[i].id.indexOf("_FullWFPO_", inputs[i].id.indexOf("_FullWFPO_")) - inputs[i].id.indexOf("_FullWFPO_")  -10)), "");
                    
                    if (respondentAtHand == RespondentTiedToCurrentCheckbox){
                    
                        var currentCheckBox = inputs[i];
                        currentCheckBox.checked = false;
                        $mrg_helpers.setRowBackgroundColor(currentCheckBox, null, 'Bisque', 'Transparent', false);
                    
                    }
                
                }
            }

       }
       
        for(var x = 0; x < images.length; x++){
            if ((images[x].id.indexOf('PreferredShipper') > -1 && anyPrintAndShip == true) || (images[x].id.indexOf('ShipToRequired') > -1 && anyPrintAndShip == true) || (images[x].id.indexOf('ShippingMethodRequired') > -1 && anyPrintAndShip == true)){
        
                images[x].style.display = 'block';  
                          
            }
            else if((images[x].id.indexOf('PreferredShipper') > -1 && anyPrintAndShip == false) ||(images[x].id.indexOf('ShipToRequired') > -1 && anyPrintAndShip == false)||(images[x].id.indexOf('ShippingMethodRequired') > -1 && anyPrintAndShip == false)){
            
                images[x].style.display = 'none';  
            }
        }
        
    },
    setPaperSize : function(e, selectControlId){
        var selectControl = document.getElementById(selectControlId);
        var paperSize = document.getElementById(e.id);
        
        if(selectControl){

            if(paperSize.value != 'en-US'){
                selectControl.selectedIndex = 1;
            }else{
                selectControl.selectedIndex = 0;
            }
        }
    },
    confirmPrice : function(message){

        var x = window.confirm(message);

        if (x){
        return true;
        }
        else {
        return false;
        }
    },
    hideOtherMC : function (ddl, obj1, obj2){
        if (ddl.options[ddl.selectedIndex].value.indexOf('~999') > -1){
            obj1.style.visibility = 'visible';
            document.getElementById(obj2).style.visibility = 'visible';
        }
        else{
            obj1.style.visibility = 'hidden';
            document.getElementById(obj2).style.visibility = 'hidden';
        }
    },
    addNoCommentText : function (responseField, noCommentText) {
    
        document.getElementById(responseField).value = noCommentText;
        return false;
    },
    hideOtherMS : function (ddl, obj1, obj2){
        var i=0;
        var other=false;
        for(i=1;i<ddl.options.length;i++){
        if ((ddl.options[i].value.indexOf('~999') > -1) && ddl.options[i].selected){
            other = true;
        }
        if(other){
            obj1.style.visibility = 'visible';
            document.getElementById(obj2).style.visibility = 'visible';
        }
        else{
            obj1.style.visibility = 'hidden';
            document.getElementById(obj2).style.visibility = 'hidden';
            }
        }
    }    
}
    
var MRGHelpers;
var $mrg_helpers = MRGHelpers = new MRG_Helpers();