function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function processCookie (cookieName, func, name, value, days)　{
  if (func == 'kill' || (cookieName == name && func == 'clear')) {
    document.cookie = cookieName + '=; expires=-86400000';
    return true;
  }

  var nameQuery = cookieName == name ? name + '=' : name + '-';
  var thisCookie = '';
  var cookieString = '';
  var cookies = document.cookie.split(';');
  for (var x = 0; x < cookies.length; x++) {
    if (cookies[x].indexOf(cookieName) != -1)
      thisCookie = cookies[x].substring(cookies[x].indexOf('=') + 1);
      thisCookie = URLDecode(thisCookie);
  }

  if (func == 'clear' || func == 'set') {
    if (func == 'clear') {
      var myregexp = new RegExp('(\\|' + nameQuery + '[\\w]*|' +
                                 nameQuery + '[\\w]*\\||' +
                                 nameQuery + '[\\w]*)');
      cookieString = thisCookie.replace(myregexp, '');
    } else if (func=='set') {
      if (cookieName == name || thisCookie == '')
        cookieString = nameQuery + value;
      else {
        var myregexp = new RegExp('(' + nameQuery + '[\\w]*)');
        var match = myregexp.exec(thisCookie);
        if (match != null && match.length > 1 && match[1] != '')
          cookieString = thisCookie.replace(match[1], nameQuery + value);
        else
          cookieString = thisCookie + 
                         (thisCookie != '' ? '|' : '') + 
                         nameQuery + value;
      }
    }
    var expires = '';
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = cookieName + 
                      '=' +
                      URLEncode(cookieString) +
                      expires +
                      '; path=/; domain=.' +
                      window.location.hostname;
    return true;
  }

  if (func == 'read') {
    if (thisCookie != '') {
      var myregexp = new RegExp(nameQuery + '([\\w]*)');
      var match = myregexp.exec(thisCookie);
      if (match != null && match.length > 1 && match[1] != '')
        return match[1];
    }
    return null;
  }
}



var newsArray= new Array();
function rand( min, max ) {

    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}




function addRowToTable(tableID,catID) 
{
	var tbl = document.getElementById(tableID);
	var lastRow = tbl.rows.length;
	var iteration = lastRow;
	
	if (lastRow<=9) 
	{ 
		if (newsArraytop[catID][iteration-5]!=null)
		{
		var row = tbl.insertRow(lastRow);
		row.id = iteration;
		var lastRow2=lastRow+1;
		var cell2 = row.insertCell(0);
		cell2.innerHTML = newsArraytop[catID][iteration-5];
		processCookie ("limitCat"+catID , "set", "limitCat"+catID, lastRow, 400)
		}
	}
}

function removeRowFromTable(tableID,catID)
{

	var tbl = document.getElementById(tableID);
	if (tbl.rows.length>=6) 
	{
	tbl.deleteRow(tbl.rows.length-1);
	var lastRow = tbl.rows.length;
	processCookie ("limitCat"+catID , "set", "limitCat"+catID, lastRow-1, 400)
	}
}