
// This code is necessary for browsers that don't reflect the DOM
// constants (like IE).
if (document.ELEMENT_NODE == null) {
  document.ELEMENT_NODE = 1;
  document.TEXT_NODE = 3;
}


// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");


function dis_ag (a) {

	ag_display = document.getElementById('ag_'+a);
	ag_display_img = document.getElementById('button_'+a);
	
	if (ag_display.style.display == 'none') {
		ag_display.style.display='';
		//ag_display_img.src = button_minus.src;
		document.getElementById("s_button_"+a).src="templates/default/images/minus.gif";
	} else {
		ag_display.style.display='none';
		//ag_display_img.src = button_plus.src;
		document.getElementById("s_button_"+a).src="templates/default/images/pluse.gif";
	}		
}


function dis_s_ad (a) {

	sa_display = document.getElementById('s_ad_'+a);
	//sa_display_img = document.getElementById('s_button_'+a);
	
	if (sa_display.style.display == 'none') {
		sa_display.style.display='';
		//sa_display_img.src = button_minus.src;
		document.getElementById("s_button_"+a).src="templates/default/images/minus.gif";
	} else {
		sa_display.style.display='none';
		//sa_display_img.src = button_plus.src;
		document.getElementById("s_button_"+a).src="templates/default/images/plus.gif";
	}		
}
function run(a, b) {
	document.getElementById(a).value = b;
	document.getElementById('sort_frm').submit();
}

function dis_r_ad (a) {

	r_display = document.getElementById('r_ad_'+a);
	//r_display_img = document.getElementById('r_button_'+a);
	
	if (r_display.style.display == 'none') {
		r_display.style.display='';
		//r_display_img.src = button_minus.src;
		document.getElementById("r_button_"+a).src="templates/default/images/minus.gif";
	} else {
		r_display.style.display='none';
		//r_display_img.src = button_plus.src;
		document.getElementById("r_button_"+a).src="templates/default/images/plus.gif";
	}		
}

function getTextValue(el) {

  var i;
  var s;

  // Find and concatenate the values of all text nodes contained
  // within the element.
  s = "";
  for (i = 0; i < el.childNodes.length; i++)
    if (el.childNodes[i].nodeType == document.TEXT_NODE)
      s += el.childNodes[i].nodeValue;
    else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
             el.childNodes[i].tagName == "BR")
      s += " ";
    else
      // Use recursion to get text within sub-elements.
      s += getTextValue(el.childNodes[i]);

  return normalizeString(s);
}

function normalizeString(s) {

  s = s.replace(whtSpMult, " ");  // Collapse any multiple whites space.
  s = s.replace(whtSpEnds, "");   // Remove leading or trailing white
                                  // space.
  return s;
}
function compareValues(v1, v2) {

  var f1, f2;

  // If the values are numeric, convert them to floats.

  f1 = parseFloat(v1);
  f2 = parseFloat(v2);
  if (!isNaN(f1) && !isNaN(f2)) {
    v1 = f1;
    v2 = f2;
  }

  // Compare the two values.
  if (v1 == v2)
    return 0;
  if (v1 > v2)
    return 1
  return -1;
}
function removeCommas(aNum) {
	//remove any commas
	aNum=aNum.replace(/,/g,"");
	//remove any spaces
	aNum=aNum.replace(/\s/g,"");
	re = /^\$|,/g;
	// remove "$" and ","
	aNum=aNum.replace(re, "");
	aNum=aNum.replace('<br>', "");
	aNum=aNum.replace('<img src="/images/or_star.gif" width=8 height=9 border=0>', "");
	aNum=aNum.replace('<img src="/images/gr_star.gif" width=8 height=9 border=0>', "");
	aNum=aNum.replace('</td></tr></table>', "");
	aNum=aNum.replace('<table border="0" cellpadding="0" cellspacing="0"><tr><td align="right" nowrap class=smallText>', "");
	aNum=aNum.replace('-', "");

	return aNum;
}

function sortTable_ron(id, col, col2, rev, tcell) {

 for (z = 0; z < 6; z++) {
	document.getElementById(tcell+z).style.fontStyle='';
 }
 
 for (z = 0; z < 11; z++) { // Do sort for all ad types
 	
 	
	  // Get the table section to sort.
	  var tblEl = document.getElementById(id+z);
	
	  if (tblEl) {

		  // The first time this function is called for a given table, set up
		  // an array of reverse sort flags.
		  if (tblEl.reverseSort == null) {
		    tblEl.reverseSort = new Array();
		    // Also, assume the campaign column is initially sorted.
		    tblEl.lastColumn = 5;
		  }
		
		  // If this column has not been sorted before, set the initial sort
		  // direction.
		  if (tblEl.reverseSort[col] == null)
		    tblEl.reverseSort[col] = rev;
		
		  // If this column was the last one sorted, reverse its sort direction.
		  if (col == tblEl.lastColumn)
		    tblEl.reverseSort[col] = !tblEl.reverseSort[col];
		
		  // Remember this column as the last one sorted.
		  tblEl.lastColumn = col;
		    
		  
		  // Set the table display style to "none" - necessary for Netscape 6 
		  // browsers.
		  var oldDsply = tblEl.style.display;
		  tblEl.style.display = "none";
		 // Sort the rows based on the content of the specified column
		  // using a selection sort.
		
		  var tmpEl;
		  var i, j;
		  var minVal, minIdx;
		  var testVal;
		  var cmp;
		
		
		        
		  for (i = 0; i < tblEl.rows.length - 1; i++) {
		
		    // Assume the current row has the minimum value.
		    minIdx = i;
		    minVal = removeCommas(getTextValue(tblEl.rows[i].cells[col]));
		
		        
		    // Search the rows that follow the current one for a smaller value.
		    for (j = i + 1; j < tblEl.rows.length; j++) {
		      testVal = removeCommas(getTextValue(tblEl.rows[j].cells[col]));
		      cmp = compareValues(minVal, testVal);
		 	// Need to sort on secondary column?
		      if (cmp == 0)
		        compareValues(removeCommas(getTextValue(tblEl.rows[minIdx].cells[col2])),
		                      removeCommas(getTextValue(tblEl.rows[j].cells[col2])));
		      // Reverse order?
		      if (tblEl.reverseSort[col])
		        cmp = -cmp;
		 
		      
		      // If this row has a smaller value than the current minimum,
		      // remember its position and update the current minimum value.
		      if (cmp > 0) {
		        minIdx = j;
		        minVal = testVal;
		      }
		    }
		  // By now, we have the row with the smallest value. Remove it from
		    // the table and insert it before the current row.
		    if (minIdx > i) {
		      tmpEl = tblEl.removeChild(tblEl.rows[minIdx]);
		      tblEl.insertBefore(tmpEl, tblEl.rows[i]);
		    }
		  }
		 // Restore the table's display style.
		  tblEl.style.display = oldDsply;
	}
  }
  
  document.getElementById(tcell+col).style.fontStyle='italic';
  return false;
}  


function sortTable_sp(id, col, col2, rev, tcell) {

 for (z = 0; z < 7; z++) {
	document.getElementById(tcell+z).style.fontStyle='';
 }
 
 for (z = 0; z < 11; z++) { // Do sort for all ad types
 	
 	
	  // Get the table section to sort.
	  var tblEl_s = document.getElementById(id+z);
	
	  if (tblEl_s) {

		  // The first time this function is called for a given table, set up
		  // an array of reverse sort flags.
		  if (tblEl_s.reverseSort == null) {
		    tblEl_s.reverseSort = new Array();
		    // Also, assume the clicks column is initially sorted.
		    tblEl_s.lastColumn = 4;
		  }
		
		  // If this column has not been sorted before, set the initial sort
		  // direction.
		  if (tblEl_s.reverseSort[col] == null)
		    tblEl_s.reverseSort[col] = rev;
		
		  // If this column was the last one sorted, reverse its sort direction.
		  if (col == tblEl_s.lastColumn)
		    tblEl_s.reverseSort[col] = !tblEl_s.reverseSort[col];
		
		  // Remember this column as the last one sorted.
		  tblEl_s.lastColumn = col;
		    
		  
		  // Set the table display style to "none" - necessary for Netscape 6 
		  // browsers.
		  var oldDsply = tblEl_s.style.display;
		  tblEl_s.style.display = "none";
		 // Sort the rows based on the content of the specified column
		  // using a selection sort.
		
		  var tmpEl;
		  var i, j;
		  var minVal, minIdx;
		  var testVal;
		  var cmp;
		
		
		        
		  for (i = 0; i < tblEl_s.rows.length - 1; i++) {
		
		    // Assume the current row has the minimum value.
		    minIdx = i;
		    minVal = removeCommas(getTextValue(tblEl_s.rows[i].cells[col]));
		
		        
		    // Search the rows that follow the current one for a smaller value.
		    for (j = i + 1; j < tblEl_s.rows.length; j++) {
		      testVal = removeCommas(getTextValue(tblEl_s.rows[j].cells[col]));
		      cmp = compareValues(minVal, testVal);
		 	// Need to sort on secondary column?
		      if (cmp == 0)
		        compareValues(removeCommas(getTextValue(tblEl_s.rows[minIdx].cells[col2])),
		                      removeCommas(getTextValue(tblEl_s.rows[j].cells[col2])));
		      // Reverse order?
		      if (tblEl_s.reverseSort[col])
		        cmp = -cmp;
		 
		      
		      // If this row has a smaller value than the current minimum,
		      // remember its position and update the current minimum value.
		      if (cmp > 0) {
		        minIdx = j;
		        minVal = testVal;
		      }
		    }
		  // By now, we have the row with the smallest value. Remove it from
		    // the table and insert it before the current row.
		    if (minIdx > i) {
		      tmpEl = tblEl_s.removeChild(tblEl_s.rows[minIdx]);
		      tblEl_s.insertBefore(tmpEl, tblEl_s.rows[i]);
		    }
		  }
		 // Restore the table's display style.
		  tblEl_s.style.display = oldDsply;
	}
  }
  
  document.getElementById(tcell+col).style.fontStyle='italic';
  return false;
}  


function sortTable_ps(id, col, col2, rev, tcell) {

 for (z = 0; z < 7; z++) {
	document.getElementById(tcell+z).style.fontStyle='';
 }
 	
  // Get the table section to sort.
  var tblEl_ps = document.getElementById(id);

  if (tblEl_ps) {

	  // The first time this function is called for a given table, set up
	  // an array of reverse sort flags.
	  if (tblEl_ps.reverseSort == null) {
	    tblEl_ps.reverseSort = new Array();
	    // Also, assume the clicks column is initially sorted.
	    tblEl_ps.lastColumn = 4;
	  }
	
	  // If this column has not been sorted before, set the initial sort
	  // direction.
	  if (tblEl_ps.reverseSort[col] == null)
	    tblEl_ps.reverseSort[col] = rev;
	
	  // If this column was the last one sorted, reverse its sort direction.
	  if (col == tblEl_ps.lastColumn)
	    tblEl_ps.reverseSort[col] = !tblEl_ps.reverseSort[col];
	
	  // Remember this column as the last one sorted.
	  tblEl_ps.lastColumn = col;
	    
	  
	  // Set the table display style to "none" - necessary for Netscape 6 
	  // browsers.
	  var oldDsply = tblEl_ps.style.display;
	  tblEl_ps.style.display = "none";
	 // Sort the rows based on the content of the specified column
	  // using a selection sort.
	
	  var tmpEl;
	  var i, j;
	  var minVal, minIdx;
	  var testVal;
	  var cmp;
	
	
	        
	  for (i = 0; i < tblEl_ps.rows.length - 1; i++) {
	
	    // Assume the current row has the minimum value.
	    minIdx = i;
	    minVal = removeCommas(getTextValue(tblEl_ps.rows[i].cells[col]));
	
	        
	    // Search the rows that follow the current one for a smaller value.
	    for (j = i + 1; j < tblEl_ps.rows.length; j++) {
	      testVal = removeCommas(getTextValue(tblEl_ps.rows[j].cells[col]));
	      cmp = compareValues(minVal, testVal);
	 	// Need to sort on secondary column?
	      if (cmp == 0)
	        compareValues(removeCommas(getTextValue(tblEl_ps.rows[minIdx].cells[col2])),
	                      removeCommas(getTextValue(tblEl_ps.rows[j].cells[col2])));
	      // Reverse order?
	      if (tblEl_ps.reverseSort[col])
	        cmp = -cmp;
	 
	      
	      // If this row has a smaller value than the current minimum,
	      // remember its position and update the current minimum value.
	      if (cmp > 0) {
	        minIdx = j;
	        minVal = testVal;
	      }
	    }
	  // By now, we have the row with the smallest value. Remove it from
	    // the table and insert it before the current row.
	    if (minIdx > i) {
	      tmpEl = tblEl_ps.removeChild(tblEl_ps.rows[minIdx]);
	      tblEl_ps.insertBefore(tmpEl, tblEl_ps.rows[i]);
	    }
	  }
	 // Restore the table's display style.
	  tblEl_ps.style.display = oldDsply;
  }
  
  document.getElementById(tcell+col).style.fontStyle='italic';
  return false;
}  