//Option Box Functions
function emptyOptions(ele)
{
if(ele!==null) {
for (var x=ele.options.length;x>=0;x--){
ele.remove(x);
}
}
}

function addOption(ele,label,value)
{
  myEle = document.createElement("option") ;
  myEle.value = value;
  myEle.text = label;
  //ele.add(myEle);
  ele.options[ele.options.length] = new Option(label, value);
}

//String prototype functions

String.prototype.toCapitalCase = function() {
var re = /\s/;
var words = this.split(re);
re = /(\S)(\S+)/;
for (i = words.length - 1; i >= 0; i--) {
re.exec(words[i]);
words[i] = RegExp.$1.toUpperCase()
+ RegExp.$2.toLowerCase();
}
return words.join(' ');
} 

//Table Functions

function addRowsToTable(tableObj,rows)
{
	  //alert("Rows: " + rows.length);
  var tbl = document.getElementById(tableObj);

  //cell0 = [];
  //cell0['text'] = "test"
  //cell0['style'] = "padding-left:90px;padding-top:90px";
  //cell0['height'] = 100;
  //cell0['width'] = 100;
  //cell0['colSpan'] = 3;
  //cell1 = [];
  //cell1['text'] = "test2"
  //cell1['style'] = "padding-left:10px";
  //cell1['height'] = 100;
  //cell1['width'] = 100;
  //cell2 = [];
  //cell2['text'] = "test"
  //cell2['style'] = "padding-left:10px";
  //cell2['height'] = 100;
  //cell2['width'] = 100;
  //row0 = Array("thisid1",cell0,cell1,cell2);
  //row1 = Array("thisid2",cell2,cell1,cell0);
  //rows = Array(row0);

  // if there's no header row in the table, then iteration = lastRow + 1
  for(y=0;y<rows.length;y++){ 

  lastRow = tbl.rows.length;
  iteration = lastRow;
 
  row = tbl.insertRow(lastRow);
  if(undefined !== rows[y]['id']){
	 
  row.id = rows[y]['id'];

  }
  cells = rows[y];
  
  for(x=0;x<cells.length;x++){
	  //

  // left cell
  //var cellLeft = row.insertCell(0);
  //var textNode = document.createTextNode(iteration);
  //cellLeft.appendChild(textNode);
  thisArrayNo = x;
//   alert(cells[thisArrayNo]['text']);
  // first cell
  cellRight = row.insertCell(x);
  textNode = cells[thisArrayNo]['innerHTML'];
  //alert(cells[thisArrayNo]['style']);
  if(undefined !== cells[thisArrayNo]['colSpan']){
	
	cellRight.colSpan = cells[thisArrayNo]['colSpan'];

  }
  
  if(undefined !== cells[thisArrayNo]['style']){
  myStyles = cells[thisArrayNo]['style'];
  myStyles = myStyles.split(";");
  for(z=0;z<myStyles.length;z++){
try {
  thisStyle = myStyles[z].split(":");
  thisStyle[0] = thisStyle[0].split("-");
  thisStyle[0] = thisStyle[0][0] + "" + thisStyle[0][1].toCapitalCase(); 
 // alert("cellRight.style." + thisStyle[0] + " = " + thisStyle[1]);
  eval("cellRight.style." + escape(thisStyle[0]) + " = '" + escape(thisStyle[1])+"'");
} catch (err){

alert("Error adding cell style \""+ myStyles[z] +"\". Additional information is below:\r\n\r\n " + err.description);
	
}
  }
  }
  if(undefined !== cells[thisArrayNo]['width']){
  cellRight.width = cells[thisArrayNo]['width'];
  }
  if(undefined !== cells[thisArrayNo]['height']){
  cellRight.height = cells[thisArrayNo]['height'];
  }
  cellRight.innerHTML = textNode;

	  //}
  } // end cell repeat
  
}// end row repeat


} //end function


function removeRowFromTable(tableObj,rowId)
{
	
	var tbl = document.getElementById(tableObj);  
    row = document.getElementById(rowId);
	
    tbl.deleteRow(row.rowIndex); 
	
}

//Position Finding
function getPos(theObj){
  x = y = 0;
  while(theObj){
    x += theObj.offsetLeft;
    y += theObj.offsetTop;
    theObj = theObj.offsetParent;
  }
 return Array(x,y);
 
 }
 
 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;
}
