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 printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function printpage() {
window.print();  
}

    var ddlbElement = null;
    var parentDdlbElement = null;
    var customFields = "";
    
    function activateDDLB( innerField, field, ddlbValues, index, id ) {
        var cell = document.getElementById( field );
        oldValue = cell.childNodes[0].value;
        oldCellColor = cell.parentNode.parentNode.style.background;
        
        color = document.getElementById( "color"+id ).value;
        var xloc = findPosX( cell );
        var yloc = findPosY( cell );
        
        if ( ddlbElement != null ) {
            parentDdlbElement.removeChild( ddlbElement );
            ddlbElement = null;
            parentDdlbElement = null;
        }
        ddlbElement = document.createElement("DIV");
        ddlbElement.style.position = "absolute";
        ddlbElement.style.top = yloc + "px";
        ddlbElement.style.left = xloc + "px";
        
        var ddlbValuesArray = ddlbValues.split(",");
        var select = "<select id='colorValues' size='"+ddlbValuesArray.length+"' onclick='updateDDLBField(this, \""+innerField+"\", \""+field+"\", \""+id+"\", "+index+" );' >";
        for ( var t=0; t<ddlbValuesArray.length; t++ ) {
            var value = ddlbValuesArray[t];
            select += "<option value='"+value+"'";
            var selected = "";
            if ( value == "B/W" ) {
                if ( color == value ) {
                    selected = "selected";
                }
                value = "B/W";
            } else if ( value == "+1" || value == " 1" ) {
                if ( color == value ) {
                    selected = "selected";
                }
                value = "+1";
            } else if ( value == "+2" || value == " 2" ) {
                if ( color == value ) {
                    selected = "selected";
                }
                value = "+2";
            } else if ( value == "Full" ) {
                 if ( color == value ) {
                    selected = "selected";
                }
                value = "Full Color";
           }
            select += ( " " + selected + " >" + value + "</option>" );      
        }
        select += "</select>";      
        
        ddlbElement.innerHTML = select;
        cell.appendChild( ddlbElement );
        parentDdlbElement = cell;
        ddlbElement.focus();
    }
    
    function getIncommingSort( field ) {
        var sortField = document.getElementById("order").value;
        //alert(sortField);
        if ( ( field == "1" || field.indexOf("PR") != -1 ) && sortField.indexOf("npprovince") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        } else if ( ( field == "2" || field.indexOf("PUBLICATION") != -1 ) && sortField.indexOf("npname") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        } else if ( ( field == "3" || field.indexOf("FREQ") != -1 ) && sortField.indexOf("frequency") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        } else if ( ( field == "4" || field.indexOf("DIST") != -1 ) && sortField.indexOf("distrubution") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        } else if ( ( field == "5" || field.indexOf("CIRC") != -1 ) && sortField.indexOf("circulation") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        } else if ( ( field == "6" || field.indexOf("LINE") != -1 ) && sortField.indexOf("line") != -1 ) {
            return ( sortField.indexOf("DESC") != -1 ? "DESC" : "ASC" );
        }
        return null;
    }
    
    function setSortField( field, dir ) {
        var sortField = "";
        if ( field == "1" || field.indexOf("PR") != -1 ) {
            sortField = ("npprovince|" + dir);
        } else if ( field == "2" || field.indexOf("PUBLICATION") != -1 ) {
            sortField = ("npcity,npname|" + dir);
        } else if ( field == "3" || field.indexOf("FREQ") != -1 ) {
            sortField = ("frequency|" + dir);
        } else if ( field == "4" || field.indexOf("DIST") != -1 ) {
            sortField = ("distrubution|" + dir);
        } else if ( field == "5" || field.indexOf("CIRC") != -1 ) {
            sortField = ("cast(circulation|as|unsigned)|" + dir);
        } else if ( field == "6" || field.indexOf("LINE") != -1 ) {
            sortField = ("linerate|" + dir);
        }
        if ( sortField != "" ) {
            document.getElementById("order").value = sortField;
        }
    }
    
    function updateDDLBField( obj, innerfield, field, id, index ) {
        var cell = document.getElementById( "color"+id );
        var value = obj.options[ obj.selectedIndex ].value;
        cell.value = value;
        deActivateDDLB( field, innerfield, id, index );
        var cell = document.getElementById( field );
        //cell.style.background = "#90ff90";
        cell.childNodes[0].style.color = "#000000";
    }
      
    function deActivateDDLB( field, innerField, id, index ) {
        var cell = document.getElementById( field );
        var origValue = getOrigValue( innerField, id );
        
        if ( origValue != null && origValue != cell.childNodes[0].value ) {
            cell.style.background = "#90ff90";
            updateFieldData( "color", id, index );
        } else {
            cell.style.background = oldCellColor;
            updateFieldData( "color", id, index );
            killCustomField( "color", id, oldValue );
        }
        if ( ddlbElement != null ) {
            parentDdlbElement.removeChild( ddlbElement );
            ddlbElement = null;
            parentDdlbElement = null;
        }
        populateTotals();
    }


    var colArray = null;
    var lineArray = null;
    var colorArray = null;
    var runArray = null;
    var rateArray = null;
 
    var oldValue = 0;
    var oldCellColor;
    function activateRow( field ) {
        var cell = document.getElementById( field );
        //alert(cell.childNodes[0].value);
        oldValue = cell.childNodes[0].value;
        oldCellColor = cell.parentNode.parentNode.style.backgroundColor;
        cell.style.background = "#000099";
        
	    cell.childNodes[0].focus();
        cell.childNodes[0].select();
    }
    
    function getOrigValue( innerField, id ) {
        if ( innerField == "columns" ) {
            for ( var t=0; t<colArray.length; t++ ) {
                if ( id == colArray[t].substring( colArray[t].indexOf("~")+1 ) ) {
                    return colArray[t].substring( 0, colArray[t].indexOf("~") );
                }
            }
        } else if ( innerField == "lines" ) {
             for ( var t=0; t<lineArray.length; t++ ) {
                if ( id == lineArray[t].substring( lineArray[t].indexOf("~")+1 ) ) {
                    return lineArray[t].substring( 0, lineArray[t].indexOf("~") );
                }
            }
        } else if ( innerField == "color" ) {
             for ( var t=0; t<colorArray.length; t++ ) {
                if ( id == colorArray[t].substring( colorArray[t].indexOf("~")+1 ) ) {
                    return colorArray[t].substring( 0, colorArray[t].indexOf("~") );
                }
            }
        } else if ( innerField == "numIns" ) {
             for ( var t=0; t<runArray.length; t++ ) {
                if ( id == runArray[t].substring( runArray[t].indexOf("~")+1 ) ) {
                    return runArray[t].substring( 0, runArray[t].indexOf("~") );
                }
            }
        } else if ( innerField == "rate" ) {
             for ( var t=0; t<rateArray.length; t++ ) {
                if ( id == rateArray[t].substring( rateArray[t].indexOf("~")+1 ) ) {
                    return rateArray[t].substring( 0, rateArray[t].indexOf("~") );
                }
            }
        }
        return null;   
    }
    
    function deActivateRow( innerField, field, id, index ) {
        var cell = document.getElementById( field );
        var origValue = getOrigValue( innerField, id );
        if ( origValue != null && origValue != cell.childNodes[0].value ) {
            cell.style.background = "#90ff90";
            updateFieldData( innerField, id, index );
            if ( innerField == "rate" ) {
                var val = document.getElementById( innerField+id ).value;
                if ( val.substring(0,1) == "." ) {
                    val = "0" + val;
                }
                document.getElementById( innerField+id ).value = "$" + val;
            }
        } else {
            cell.style.background = oldCellColor;
            updateFieldData( innerField, id, index );
            killCustomField( innerField, id, oldValue );
        }
        cell.childNodes[0].style.color = "#000000";
        populateTotals();
    }

    function handleEnter (input, event, field ) {

        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
         // deActivateRow( field );
          var cell = document.getElementById( field );
          cell.childNodes[0].blur();
          return false;
        } else if ( ( keyCode <48 || keyCode > 57 ) && keyCode != 46 ) { 
            return false;
        } else {
            var cell = document.getElementById( field );
            cell.childNodes[0].style.color = "#ffffff";
            return true;
        }

    }
    
    function addToCustomField( innerField, id ) {
        
        var newCustomFields = "";
        var amount = document.getElementById( innerField + id ).value;
        var customFieldsArray = customFields.split("|");
        for ( var t=0; t<customFieldsArray.length; t++ ) {
            if ( customFieldsArray[t].indexOf( innerField + id + "~" ) == -1 ) {
                if ( newCustomFields != "" ) {
                    newCustomFields += "|";
                }
                newCustomFields += ( customFieldsArray[t] );
            }
        }
        if ( newCustomFields != "" ) {
            newCustomFields += "|";
        }
        newCustomFields += ( innerField + id + "~" + amount );
        customFields = newCustomFields;   
        
    }
    
    function selectAll() {
    
        var checkAll = document.getElementById("checkAll").checked;
        var ids = document.getElementById("uniqueIds").value;
        var idArray = ids.split(",");
        for ( var t=0; t<idArray.length; t++ ) {
            var isChecked = document.getElementById("check"+idArray[t]).checked;
            if ( checkAll ) {
                if ( !isChecked ) {
                    simulateClick("check"+idArray[t]);
                }
            } else {
                if ( isChecked ) {
                    simulateClick("check"+idArray[t]);
                }
            }
        }
    }
    
    function killCustomField( innerField, id, value ) {
        var fieldData = innerField + id + "~" + value;
        customFields = customFields.replace(fieldData + "|", "");
        customFields = customFields.replace(fieldData, "");
    }

    function updateFieldData( innerField, id, index ) {
    
        var sizeSelection = document.getElementById("hSizeSelection").value;
        var numIns = document.getElementById("numIns"+id).value;
        var columns = 0;
        var lines = 0;
        var color = "B";
        var rate = 0.0;
        if ( sizeSelection != "insert" ) {
            columns = document.getElementById("columns"+id).value;
            lines = document.getElementById("lines"+id).value;
            color = document.getElementById("color"+id).value;
            rate = document.getElementById("rate"+id).value.replace("$","");
        }

       var isSelected = false;
        if ( document.getElementById("check"+id) ) {
            isSelected = document.getElementById("check"+id).checked;
            if ( isSelected ) {
                if ( innerField != "color" ) {
                    document.getElementById(innerField+id).value = oldValue;
                }
                simulateClick("check"+id);
            }
        }
        
        var admin = document.getElementById("admin").value;
        var contents = cInclude( "RateCardAccess.aspx?function=getRowData&parameters="+id+","+numIns+","+columns+","+lines+","+color+","+sizeSelection+","+admin+","+rate );
        
        contentsArray = contents.split(",");     
        if ( sizeSelection != "insert" ) {  
            document.getElementById("columns"+id).value = contentsArray[0];
            document.getElementById("lines"+id).value = contentsArray[1];
            document.getElementById("adWidth"+id).value = contentsArray[2];
            document.getElementById("adHeight"+id).value = contentsArray[3];
            document.getElementById("adCost"+id).value = "$"+contentsArray[4];
            document.getElementById("clrChg"+id).value = "$"+contentsArray[5];
        }
        document.getElementById("numIns"+id).value = numIns;
        document.getElementById("adTotal"+id).value = "$"+contentsArray[6];
        document.getElementById("cpm"+id).value = "$"+contentsArray[7];
        
        if ( isSelected ) {
        //alert("was checked, turn on");
            simulateClick("check"+id);
        }
        
        if ( !document.getElementById("check"+id) ) {
            populateTotals();
        }
        addToCustomField( innerField, id );
    }

    function simulateClick( field ) {
      if ( document.all ) {
       // alert( field );
        
        document.getElementById(field).click();
      } else {

      var evt = document.createEvent("MouseEvents");
      evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
      var cb = document.getElementById(field); 
      var canceled = !cb.dispatchEvent(evt);
      if(canceled) {
        // A handler called preventDefault
        //alert("canceled");
      } else {
        // None of the handlers called preventDefault
        //alert("not canceled");
      }
      }
    }

   function cInclude(url) {
    var req = false;
    // For Safari, Firefox, and other non-MS browsers
    if (window.XMLHttpRequest) {
            try {
            req = new XMLHttpRequest();
            } catch (e) {
            req = false;
            }
    } else if (window.ActiveXObject) {
            // For Internet Explorer on Windows
            try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                    req = false;
            }
            }
    }


    if (req) {
            // Synchronous request, wait till we have it all
            var randomnumber = Math.floor(Math.random()*1000001);
            req.open('GET', url + '&' + randomnumber, false);
            req.send(null);
            return req.responseText;

    } else {
            element.innerHTML =
    "Sorry, your browser does not support " +
            "XMLHTTPRequest objects. This page requires " +
            "Internet Explorer 5 or better for Windows, " +
            "or Firefox for any system, or Safari. Other " +
            "compatible browsers may also exist.";
    }
   }
   
   
    function findPosX(obj)
      {
        var curleft = 0;
        if(obj.offsetParent)
            while(1) 
            {
              curleft += obj.offsetLeft;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.x)
            curleft += obj.x;
        return curleft;
      }

      function findPosY(obj)
      {
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
              curtop += obj.offsetTop;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.y)
            curtop += obj.y;
        return curtop;
      }

     function getQueryVar( name ) {
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( window.location.href );
          if( results == null )
            return "";
          else
            return results[1];
    }



var tipWidth = "Edit the # of columns here.";
var tipHeight = "Edit the # of lines here.";
var tipRuns = "Edit the # of ad runs here.";
var tipColor = "Edit the ad color here. B, 1, 2 or F.";
var tipRate = "Edit the line rate here.";

function doTooltip(e, msg) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}
