//=======[GRID MAKER]========//
//-----------------------------------
//  This code is copyrighted property
//  ISDN*tek © 2005
//  www.isdntek.com
//  do not use without permission
//-----------------------------------
//    global variables
//-----------------------------------
var fontFace    ="arial"  // arial, sans-serif
var textBold    ="N"      // Y or N
var textSize    ="12"

var headerColor ="#502020"
var headBgColor ="#CCBB99"
var HeadAlign   ="right"   // center, left, right

var textColor   ="#602400"
var textBgColor ="#E8E0BB"
var TextAlign   ="center"  // center, left, right

var BorderColor ="#FFFFFF"
var BorderWidth ="4"
var shadowEdges ="y"       // Y or N

var savedColor  =BorderColor


var quot="'"
var maxActiveRows=0; 
var maxActiveCols=0; 
var GridString=""
var clickcell="headbg"
var clicktext="heading"
var eCode="Thanks for trying"
var activeLayer="border"
var GridTitle="Size & Weight Chart"
var hiliteLayer="none"
var busyflag=false

//-----------------------------------
//   Change maximum grid size above 
//-----------------------------------
    var MaxNumRows  =12
    var MaxNumCols  =10
    var savedRows   =12
    var savedCols   =10
    var startRow    =0
    var startCol    =0
    gridcell=new Array(100); //maxRows
    for (var i=0; i < gridcell.length; i++ ){gridcell[i] = new Array(100);} //maxCols
    var quot="'"
//-----------------------------------
//   create data entry cells 
//-----------------------------------
//  Anything in a "0" field is a heading.
//  The first number is the row and the 
//  second number is the column.
//-----------------------------------

function TestGridSize(rc) {   
	var y=Math.abs(document.getElementById('TableRows').value)
	var x=Math.abs(document.getElementById('TableCols').value)
	if (x+y>=70){var slow="very slow"; var period="20 seconds."}
		else{slow="slower"; var period="10 seconds."}
    	if ((x+y>=38) && (y>=savedRows) && (x>=savedCols)){
		var ans=confirm("The large number of cells ("+y+"x"+x+") "+
			"\n will cause "+slow+" page performance."+
			"\n Please allow about "+period+
			"\n\n Continue anyway? ","")
		if (ans==false){ if (rc=="row")
		{document.getElementById('TableRows').value=savedRows;
			document.getElementById('TableRows').text=savedRows}
		else{document.getElementById('TableCols').value=savedCols;
			document.getElementById('TableCols').text=savedCols}
		return}; 
    	}
	MaxNumRows=y; MaxNumCols=x; savedRows=y; savedCols=x; MakeEntryCells();
	//setTimeout('cursorNormal()',500)
}

// neither is used
function cursorNormal(){document.getElementById('inputTable').style.cursor="normal"}

function cursorWait(rc){ var b=rc
	//document.getElementById('inputTable').style.cursor="wait";
	setTimeout('TestGridSize(b)',50);
}



function MakeEntryCells() { var cellArray=""
     for (i=0; i<MaxNumRows; i++){cellArray=cellArray+'<nobr>'; for (j=0; j<MaxNumCols; j++){
	cellArray=cellArray+'<input type=text size=7 id=slot'+i+'_'+j+' value="test'+i+'"' 
	if (j==0||i==0) {cellArray=cellArray+' style="background-color:#E8E0C8; margin:0; '+
		' border-color:#e8e8e8; border-size: thin; border-style: solid;" '}
	else{cellArray=cellArray+' style="background-color:#fefefe; margin:0; '+
		' border-color:#e8e8e8; border-size: thin; border-style: solid;" '}
	cellArray=cellArray+'>'
	}
	cellArray=cellArray+'</nobr><br>'
     }
     document.getElementById('inputGrid').innerHTML=cellArray
  }


function ClearEntryCells(){ 
	for (i=0; i<MaxNumRows; i++){for (j=0; j<MaxNumCols; j++){document.getElementById('slot'+i+'_'+j).value=""}}
  }



//-----------------------------------
//    Dynamic Row builder
//-----------------------------------
function MakeRows(){ 
	if (document.getElementById('textBold').checked==true) { 
		boldness="bold"}else{boldness="normal"} 
	if (document.getElementById('shadowEdges').checked==true){ 
		shadows="1"}else{shadows="0"} 

        //--- test number of cells
	maxActiveRows=0; maxActiveCols=0; 
	for (var i=0; i<MaxNumRows; i++){ for (var j=0; j<MaxNumCols; j++)
		{if (document.getElementById('slot'+i+'_'+j).value)
			{if (i>maxActiveRows){maxActiveRows=i}; 
			if (j>maxActiveCols){maxActiveCols=j} }
	}}
	// --- end test
	// --- test if first header line is used
	startRow=1
	for (var j=0; j<=maxActiveCols; j++){
		if (document.getElementById('slot0_'+j).value){startRow=0}}
	// --- end test
	// --- test if first header column is used
	startCol=1
	for (var j=0; j<=maxActiveRows; j++){
		if (document.getElementById('slot'+j+'_0').value){startCol=0}}
	// --- end test

	if (maxActiveRows>0){
		GridString=GridString+'<table id=gridTable onclick="filterclick(this.id)" cellspacing='+BorderWidth+' cellpadding=5 border='+shadows+'  style="background-color:'+BorderColor+';" >'
		for (var i=startRow; i<=maxActiveRows; i++){ 
			GridString=GridString+'<tr>';
 			MakeCells(i); 
			GridString=GridString+'</tr>'
		}
		GridString=GridString+'</table>'
	}
	document.getElementById('gTableHolder').innerHTML=GridString; GridString=""
	if (maxActiveRows==0 && maxActiveCols==0){uncheckColors()}
}


function MakeCells(i){  var bgcolr=""; var fontcolor=""; var halign; 
	for (var j=startCol; j<=maxActiveCols; j++){

		if (i==0||j==0) {bgcolr=headBgColor; halign=HeadAlign; fontcolor=headerColor; clicktext="heading"; clickcell="headbg"} 
			else{bgcolr=textBgColor; halign=TextAlign; fontcolor=textColor; clicktext="text"; clickcell="bodybg"}; 
		if (j>0){halign=TextAlign} 
		
		GridString=GridString+'<td id=gridcell'+i+'_'+j+' onclick="filterclick(this.id)" style="background-color:'+bgcolr+
		'; font-family:'+fontFace+'; font-weight:'+boldness+'; font-size:'+textSize+
		'; text-align:'+halign+'; color:'+fontcolor+'; cursor:normal; " ><span id=gridtext'+i+'_'+j+' style="cursor:pointer" onclick="filterclick(this.id)">'
		GridString=GridString+convertCharacters(document.getElementById('slot'+i+'_'+j).value)
		GridString=GridString+'</span></td>'
	//--need to modify the output code due to the addition of the span tags for editing.
	}
}



function paintcells(paintcolor){//dynamically change colors
    if (activeLayer!="none"){
	if (activeLayer=="border"){document.getElementById('gridTable').style.backgroundColor=paintcolor; BorderColor=paintcolor} else{
	for (var i=startRow; i<=maxActiveRows; i++)
        {for (var j=startCol; j<=maxActiveCols; j++){
 		if (activeLayer=='headbg' || activeLayer=='heading'){
		    if (i==0||j==0) 
		    {if (activeLayer=='headbg'){document.getElementById('gridcell'+i+'_'+j).style.backgroundColor=paintcolor; headBgColor=paintcolor}else {if (activeLayer=='heading'){document.getElementById('gridcell'+i+'_'+j).style.color=paintcolor; headerColor=paintcolor}}}
		}
		else {if (i!=0&&j!=0){if (activeLayer=='bodybg'){document.getElementById('gridcell'+i+'_'+j).style.backgroundColor=paintcolor;textBgColor=paintcolor}else {if (activeLayer=='text'){document.getElementById('gridcell'+i+'_'+j).style.color=paintcolor;textColor=paintcolor}}}
		}
	}}}
    }
}

//--new--
function setColor(palettecolor) { //-this a user-defined function
   	//--makes color available to the application
	// It can be used to tell the application what to paint.
	// Palettecolor will arrive as a hex value or empty value
	paintcells(palettecolor)
	//if (palettecolor!=''){
	//}
	//document.bgColor=palettecolor}else{document.body.style.backgroundColor=palettecolor}
	//if (palettecolor=="") {document.bgColor='none'//-process 'none' 
	//}else{document.bgColor=palettecolor}
  }
//--new--
function setDynaColor(palettecolor){
	//--palettecolor will be a hex value or empty value.
	//  give application opportunity to use the dynamic test colors
	paintcells(palettecolor)

	////if (palettecolor!=''){
	//  if (activeLayer=="heading"||activeLayer=="text") {
	//  document.getElementById(activeLayer).parentNode.style.color=palettecolor} 
	//  else {document.getElementById(activeLayer).style.backgroundColor=palettecolor}
	////}
	////document.bgColor=palettecolor }
}


function startColor(activeLayer){
	// set the color palette to match the selected item 
	switch (activeLayer){
		case "border":; sendColor(document.getElementById('gridTable').style.backgroundColor); 	 break 
		case "text":;   sendColor(document.getElementById('gridcell1_1').style.color); 		 break 
		case "bodybg":; sendColor(document.getElementById('gridcell1_1').style.backgroundColor); break 
		case "heading":;if (document.getElementById('gridcell1_0')){sendColor(document.getElementById('gridcell1_0').style.color)}	    else if (document.getElementById('gridcell0_1')){sendColor(document.getElementById('gridcell0_1').style.color)};       	 break 
		case "headbg":; if (document.getElementById('gridcell1_0')){sendColor(document.getElementById('gridcell1_0').style.backgroundColor)}else if (document.getElementById('gridcell0_1')){sendColor(document.getElementById('gridcell0_1').style.backgroundColor)}; 	 break 
		//default:document.getElementById('gridcell1_0').innerHTML="badTag"		
	}
}


function uncheckColors(){
	for (var i=1;i<=5; i++){
	document.getElementById('ColorThis'+i).checked=false}
	activeLayer='none'
}


//--new--
function filterclick(elemid){
	if (busyflag==true) {return}
	busyflag=true
	//--pass color up from application
	id2Layer(elemid)
	if (elemid.indexOf('text')>-1) {
		sendColor(document.getElementById(elemid).parentNode.style.color)}else{
  		sendColor(document.getElementById(elemid).style.backgroundColor)}
	a=setTimeout("busyflag=false",200)
	//if none, then activeLayer='none'; uncheckColors();   
}


function id2Layer(elemid){
	//
	uncheckColors()
	if (elemid=="gridTable"){document.getElementById('ColorThis1').checked=true;activeLayer="border";}
	else if (elemid.indexOf('gridcell')>-1 && (elemid.indexOf('cell0_')>0  || elemid.indexOf('_0')>0)){document.getElementById('ColorThis3').checked=true;activeLayer="headbg" ;}
	else if (elemid.indexOf('gridtext')>-1 && (elemid.indexOf('text0_')>0  || elemid.indexOf('_0')>0)){document.getElementById('ColorThis2').checked=true;activeLayer="heading";}
	else if (elemid.indexOf('gridcell')>-1 && (elemid.indexOf('cell0_')<0 && elemid.indexOf('_0')<0)) {document.getElementById('ColorThis5').checked=true;activeLayer="bodybg" ;}
	else if (elemid.indexOf('gridtext')>-1 && (elemid.indexOf('text0_')<0 && elemid.indexOf('_0')<0)) {document.getElementById('ColorThis4').checked=true;activeLayer="text"   ;}
	//else {activeLayer="none"}
}


function alignCellText(alignthis,tAlign){//dynamically change alignment
	for (var i=startRow; i<=maxActiveRows;i++)
        {for (var j=startCol; j<=maxActiveCols;j++)
 		if (alignthis=='HeadAlign' && (i==0 || j==0) )
		    {document.getElementById('gridcell'+i+'_'+j).style.textAlign=tAlign; HeadAlign=tAlign}
		else {if (alignthis=='TextAlign' && j>0 )
		    {document.getElementById('gridcell'+i+'_'+j).style.textAlign=tAlign; TextAlign=tAlign}}
	}
}



function changeFontSize(fSize){
	for (var i=startRow; i<=maxActiveRows; i++)
        {for (var j=startCol; j<=maxActiveCols; j++) document.getElementById('gridcell'+i+'_'+j).style.fontSize=fSize
	textSize=fSize    
}
}


function changeFontFace(fFamily){
	for (var i=startRow; i<=maxActiveRows; i++){
	for (var j=startCol; j<=maxActiveCols; j++) document.getElementById('gridcell'+i+'_'+j).style.fontFamily=fFamily
	fontFace=fFamily
}
}


function changeFontBold(){
	if (document.getElementById('textBold').checked){fWt="bold"; textBold="Y"}else{fWt="normal"; textBold="N"}
	for (var i=startRow; i<=maxActiveRows; i++)
        {for (var j=startCol; j<=maxActiveCols; j++) document.getElementById('gridcell'+i+'_'+j).style.fontWeight=fWt
}
}

function changeShadows(isChecked){
	//document.getElementById('gridcell6_5').innerHTML=isChecked
	if (isChecked==true){document.getElementById('gridTable').border=1; shadows="1"} else{document.getElementById('gridTable').border=0; shadows="0"};  
}


function changeBorderWidth(bwidth){
	document.getElementById('gridTable').cellSpacing=bwidth
	BorderWidth=bwidth
}


function convertCharacters(cText){
	if (cText==" "||cText==""){cText="&nbsp;"}
	return cText;
}



//--------------------------
//   make HTML export code
//--------------------------


function fillcodeview(){
	//makeExportcode()
	makeCSScode()
	document.getElementById('HtmlCode').value=eCode
}


function makeExportcode(){
	if (maxActiveRows==0){eCode="";return}
	nl=' \n'  //--  '<br>' used for divs or ' \n' used for alerts or doc.writes
	eCode='<!-- chart table generated at www.isdntek.com/tagbot/grids.htm --> '+nl
	bordercolor=document.getElementById('gridTable').style.backgroundColor.toUpperCase()
		if (bordercolor.indexOf("RGB")>-1){bordercolor=rgb2hex(bordercolor)}
	eCode=eCode+' <table cellspacing='+document.getElementById("gridTable").cellSpacing +' cellpadding=5 border='+document.getElementById("gridTable").border+'  style="background-color:'+bordercolor+';">'+nl

	for (var i=startRow; i<=maxActiveRows; i++){
		eCode=eCode+'<!-- row '+i+'   -->'+nl+'<tr>'+nl	
		var bgcolr="";
		for (var j=startCol; j<=maxActiveCols; j++){
		bgcolr=document.getElementById('gridcell'+i+'_'+j).style.backgroundColor.toUpperCase()
		fontcolor=document.getElementById('gridcell'+i+'_'+j).style.color.toUpperCase()
		// gecko uses RGB and IE uses Hexvalues, so convert here
		if (bgcolr.indexOf("RGB")>-1){bgcolr=rgb2hex(bgcolr)}
		if (fontcolor.indexOf("RGB")>-1){fontcolor=rgb2hex(fontcolor)}
		halign=document.getElementById('gridcell'+i+'_'+j).style.textAlign
		boldness=document.getElementById('gridcell'+i+'_'+j).style.fontWeight
		fontFace=document.getElementById('gridcell'+i+'_'+j).style.fontFamily
		textSize=document.getElementById('gridcell'+i+'_'+j).style.fontSize
		eCode=eCode+'<td style="background-color:'+bgcolr;
		eCode=eCode+'; font-family:'+fontFace+'; font-weight:'+boldness+'; font-size:'+textSize;    
		eCode=eCode+'; text-align:'+halign+'; color:'+fontcolor+'">'
		//eCode=eCode+document.getElementById('gridcell'+i+'_'+j).innerHTML+'</td> '+nl
		eCode=eCode+convertCharacters(document.getElementById('slot'+i+'_'+j).value)+'</td> '+nl
		}
		eCode=eCode+'</tr> '+nl	
		}
	eCode=eCode+'<!-- end rows -->'+nl
	eCode=eCode+'</table> '+nl
	eCode=eCode+' <!-- chart table generated at www.isdntek.com/tagbot/grids.htm --> '
}

function makeCSScode(){
	if (maxActiveRows==0){eCode="";return}
	nl='\n'  //--  '<br>' used for divs or ' \n' used for alerts or doc.writes
	eCode='<!-- chart table generated at www.isdntek.com/tagbot/grids.htm --> '+nl

	bordercolor=colorcode(document.getElementById('gridTable').style.backgroundColor)
	eCode=eCode+'<table cellspacing='+document.getElementById("gridTable").cellSpacing +' cellpadding=5 border='+document.getElementById("gridTable").border+' style="background-color:'+bordercolor+';">'+nl

	for (var i=startRow; i<=maxActiveRows; i++){
		eCode=eCode+'<!-- row '+i+'   -->'+nl+' <tr>'
		var bgcolr="";
		for (var j=startCol; j<=maxActiveCols; j++){
		  if (i==0||j==0){cl='class="grdhdr"'}else{cl='class="grdbdy"'}
		  eCode=eCode+'<td '+cl+ '>'
		  eCode=eCode+convertCharacters(document.getElementById('slot'+i+'_'+j).value)+nl+'</td>'
		}
		eCode=eCode+'</tr> '+nl	
	}
	eCode=eCode+'<!-- end rows -->'+nl
	eCode=eCode+'</table> '+nl
	eCode=eCode+'<style>'+nl
	if (document.getElementById('gridcell1_0')){elem=document.getElementById('gridcell1_0');
		// test whether there is a left column of headings
		halign=   elem.style.textAlign
		boldness= elem.style.fontWeight
		fontFace= elem.style.fontFamily
		textSize= elem.style.fontSize
		fontcolor=colorcode(elem.style.color) //.toUpperCase()
		bgcolr=   colorcode(elem.style.backgroundColor) //.toUpperCase()
		eCode=eCode+'.grdhdr {background-color:'+bgcolr+';  font-family:'+fontFace+';\n  font-weight:'+boldness+'; font-size:'+textSize+'; text-align:'+halign+'; color:'+fontcolor+' }'+nl
	}
	else if (document.getElementById('gridcell0_1')){elem=document.getElementById('gridcell0_1');
		// test whether there is a top row of headings
		halign=   elem.style.textAlign
		boldness= elem.style.fontWeight
		fontFace= elem.style.fontFamily
		textSize= elem.style.fontSize
		fontcolor=colorcode(elem.style.color) //.toUpperCase()
		bgcolr=   colorcode(elem.style.backgroundColor) //.toUpperCase()
		eCode=eCode+'.grdhdr {background-color:'+bgcolr+';  font-family:'+fontFace+';\n  font-weight:'+boldness+'; font-size:'+textSize+'; text-align:'+halign+'; color:'+fontcolor+' }'+nl
	}
		// test body cells
		halign=   document.getElementById('gridcell1_1').style.textAlign
		boldness= document.getElementById('gridcell1_1').style.fontWeight
		fontFace= document.getElementById('gridcell1_1').style.fontFamily
		textSize= document.getElementById('gridcell1_1').style.fontSize
		fontcolor=colorcode(document.getElementById('gridcell1_1').style.color) //.toUpperCase()
		bgcolr=   colorcode(document.getElementById('gridcell1_1').style.backgroundColor) //.toUpperCase()
	eCode=eCode+'.grdbdy {background-color:'+bgcolr+';  font-family:'+fontFace+';\n  font-weight:'+boldness+'; font-size:'+textSize+'; text-align:'+halign+'; color:'+fontcolor+' }'+nl
	eCode=eCode+'</style>'+nl
	eCode=eCode+' <!-- chart table generated at www.isdntek.com/tagbot/grids.htm --> '
}
//===========[END]=============//