  /**
	*	STOCK TABLE FORMATTING
	*	Copyright 2011 - Adnet Communications Inc.
	*	Author: James Clarke
	*	Rev: 0.02
	*
	*/

function jsonp(args){
	if (args.status == 'error'){
		alert('there was an error');	
	}else{
		writeTable(args.results,args.params);		
	}	
}
var obj;
var para;
var chartHtml;
var styles;

function getStyles(style){
	
	if(styles[style]){
	
	switch(style){
		
		case "headerColor":
			return 'background-color:#'+styles["headerColor"]+'; '; 
			break;
		case "headerTextColor":
			return 'color:#'+styles["headerTextColor"]+'; ';
			break;
		case "cellColor":
			return 'background-color:#'+styles["cellColor"]+'; ';
			break;
		case "cellTextColor":
			return 'color:#'+styles["cellTextColor"]+'; ';
			break;
		case "cellColor2":
			return 'background-color:#'+styles["cellColor2"]+'; ';
			break;                 
		case "cellTextColor2":
			return 'color:#'+styles["cellTextColor2"]+'; ';
			break;  
		case "colColor":
			return 'background-color:#'+styles["colColor"]+'; ';
			break;
		case "colColor2":
			return 'background-color:#'+styles["colColor2"]+'; ';
			break;
		case "positiveColor":
			return 'color:#'+styles["positiveColor"]+'; ';
			break;
		case "negativeColor":
			return 'color:#'+styles["negativeColor"]+'; '; 
			break;
		case "rowDividerColor":
			return 'border-bottom-color:#'+styles["rowDividerColor"]+'; ';
			break;
		case "rowDividerWidth":
			return 'border-bottom-width:'+styles["rowDividerWidth"]+'px; '; 
			break;
		case "cellDividerColor":
			return 'border-color:#'+styles["cellDividerColor"]+'; ';
			break;
		case "cellDividerWidth":
			return 'border-left-width:'+styles["cellDividerWidth"]+'px; '; 
			break;
		case "rowDividerColor2":
			return 'border-color:#'+styles["rowDividerColor2"]+'" ';
			break;
		case "rowDividerWidth2":
			return 'border-bottom-width:'+styles["rowDividerWidth2"]+'px; '; 
			break;
		case "cellDividerColor2":
			return 'border-color:#'+styles["cellDividerColor2"]+'; ';
			break;
		case "cellDividerWidth2":
			return 'border-left-width:'+styles["cellDividerWidth2"]+'px; ';  
			break;
		case "tableBorderColor":
			return 'border-color:#'+styles["tableBorderColor"]+'; ';
			break;
		case "tableBorderWidth":
			return 'border-left-width:'+styles["tableBorderWidth"]+'px; ';  
			break;       
		case "tableFooterColor":
			return 'border-bottom-color:#'+styles["tableFooterColor"]+'; ';
			break;
		case "tableFooterWidth":
			return 'border-left-width:'+styles["tableBorderWidth"]+'px; ';  
			break;       
		default:
			return false;
			
		}
	}else{
		return 'hello';	
	}
}

function writeTable(passedObj,params){
	var out = '';
		out += '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
	    obj = new Array();
	
	var p = parseInt(params.columns);
	
	styles = params.styling;
	
	for (i in passedObj){
		
		if(passedObj[i].visible){
			
			if(passedObj[i].name == 'Symbol'){				
				out += '<tr>';	
				out += '<th colspan="'+p*2+'" style="'+ getStyles("headerColor") +''+ getStyles("headerTextColor")+'">';
				out += passedObj[i].value;
				out += '</th>';	
				out += '</tr>';
				continue;
			}
			
			obj.push(passedObj[i]);
		}
	}



	var zeb = false;
	for (i=0; i<obj.length; i++){
		

				if((!p) || (p==1)){
					
					if(i%p==0){
						zeb = !zeb;
					}
					
					out += '<tr>';
					if(zeb){
						out += '<td style="'+ getStyles("cellColor") +''+ getStyles("cellTextColor") +'">';
					}else{
						out += '<td style="'+ getStyles("cellColor2") +''+ getStyles("cellTextColor2") +'">';
					}
					out += obj[i].name;
					out += '</td>';
					if(zeb){
						out += '<td align="right" style="'+ getStyles("cellColor") +''+ getStyles("cellTextColor") +'">';
					}else{
						out += '<td align="right" style="'+ getStyles("cellColor2") +''+ getStyles("cellTextColor2") +'">';
					}
					out += obj[i].value;
					out += '</td>';
					out += '</tr>';
					
				}else{
					
					if((i%p==0)&&(i!=0)){
						out += '</tr>';
					}
					
					if(i%p==0){
						out += '<tr>';
						zeb = !zeb;
					}
					
					if(zeb){
						out += '<td style="'+ getStyles("cellColor") +''+ getStyles("cellTextColor")  +'">';
					}else{
						out += '<td style="'+ getStyles("cellColor2") +''+ getStyles("cellTextColor2")  +'">';
					}
					out += obj[i].name;
					out += '</td>';
					if(zeb){
						out += '<td align="right" style="'+ getStyles("cellColor") +''+ getStyles("cellTextColor")  +'">';
					}else{
						out += '<td align="right" style="'+ getStyles("cellColor2") +''+ getStyles("cellTextColor")  +'">';
					}
					out += obj[i].value;
					out += '</td>';
					
					if(p==obj.length-1){
						out += '</tr class="last">';
					}
					
				}
			
	}
	
	out += '</table>';
	chartHtml = out;
	para = params;
}

	
addLoadEvent( function(){
	if(para.targetID){
		var d = document.getElementById(para.targetID)
			d.innerHTML = chartHtml;
	}else{
		document.write(chartHtml);	
	}	
});



function addLoadEvent(func) { 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') { 
	    window.onload = func; 
	  } else { 
	    window.onload = function() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    } 
	  } 
	} 
