////////////////////////////////////////
///     Written by Mark Stutzman     ///
/// contact me if you have any       ///
/// suggestions/requests/changes for ///
/// this script.                     ///
////////////////////////////////////////

function ccini()
{   var bordCol, backCol, cornOff;
    var cornRad, bordW, length, height, tdLength, trHeight;
    var bordColT, bordColR, bordColB, bordColL;
    var newInner, corners;
	var agt=navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

    var objCTables = document.body.getElementsByTagName("SPAN");

    for(var i = 0; i < objCTables.length; i++)
    {
	var objCTab = objCTables[i];
	if (objCTab.className.toUpperCase()=="CURVEBOX"){
	var tabContent = objCTab.innerHTML;

	backCol = setVar (objCTab.getAttribute("backColor"), "#FFFFFF");
	bordCol = setVar (objCTab.getAttribute("bordColor"), "#000000");

	sqCorn = setVar (objCTab.getAttribute("squareCorners"), "");
	mkTab = setVar (objCTab.getAttribute("makeTab"), "0");
	selTab = setVar (objCTab.getAttribute("selTab"), "0");

	cornRad = setVar (objCTab.getAttribute("cornerRadius"), "5");
	bordW = setVar (objCTab.getAttribute("borderWidth"), 1);

	length = setVar (objCTab.getAttribute("length"), "");
	height = setVar (objCTab.getAttribute("height"), "");

	bordColT = bordCol;
	bordColR = bordCol;
	bordColB = bordCol;
	bordColL = bordCol;

	if(mkTab=="1")
	{	sqCorn = "34";
		if (selTab=="1") { bordColB = backCol;}
	}
	else if(mkTab=="2")
	{	sqCorn = "13";
		if (selTab=="1") { bordColL = backCol;}
	}
	else if(mkTab=="3")
	{	sqCorn = "12";
		if (selTab=="1") { bordColT = backCol;}
	}
	else if(mkTab=="4")
	{	sqCorn = "24";
		if (selTab=="1") { bordColR = backCol;}
	}

	tdLength = (length=="") ? "" : "100%";
	trHeight = (height=="") ? "" : "100%";

	newInner="";

	corners = getCorners(bordCol, backCol, cornRad, bordW);

	newInner += "<TABLE cellpadding='0' cellspacing='0' border='0' " +
				"width='" + length + "' height='" + height + "' style='border:0px;margin:0px;empty-cells:show;'>";

	newInner += "<TR valign='top'>";
	newInner += (sqCorn.indexOf("1")==-1) ? corners[0] :
		squareCell ("border-left:" + bordW + "px solid " + bordColL +
			   ";border-top:" + bordW + "px solid " + bordColT, backCol, cornRad, is_ie);;

	newInner += squareCell ("border-top:" + bordW + "px solid " + bordColT, backCol, tdLength, is_ie);

	newInner += (sqCorn.indexOf("2")==-1) ? corners[1] :
		squareCell ("border-right:" + bordW + "px solid " + bordColR +
			   ";border-top:" + bordW + "px solid " + bordColT, backCol, cornRad, is_ie);;
	newInner += "</TR>";

	newInner += "<TR valign='top' style='height:" + trHeight + ";'>";
	newInner += squareCell ("border-left:" + bordW + "px solid " + bordColL, backCol, "", is_ie);
	newInner += "<TD BGCOLOR='" + backCol + "'>";
	newInner += tabContent;
	newInner += "</TD>";

	newInner += squareCell ("border-right:" + bordW + "px solid " + bordColR, backCol, "", is_ie);
	newInner += "</TR>";

	newInner += "<TR valign='top'>";

	newInner += (sqCorn.indexOf("3")==-1) ? corners[2] :
		squareCell ("border-left:" + bordW + "px solid " + bordColL +
			   ";border-bottom:" + bordW + "px solid " + bordColB, backCol, cornRad, is_ie);

	newInner += squareCell ("border-bottom:" + bordW + "px solid " + bordColB, backCol, "", is_ie);

	newInner += (sqCorn.indexOf("4")==-1) ? corners[3] :
		squareCell ("border-right:" + bordW + "px solid " + bordColR +
			   ";border-bottom:" + bordW + "px solid " + bordColB, backCol, cornRad, is_ie);

	newInner += "</TR>";

	objCTab.innerHTML = newInner;
	}
    }
}

function setVar (ifSet, elseSet)
{	return ifSet ? ifSet : elseSet;
}

function squareCell (borderStr, backCol, width, doNullSpace)
{	return ("<TD BGCOLOR='" + backCol + "' WIDTH='" + width + "' HEIGHT='1' style='" +
		borderStr + ";font-size:1px;line-height:1px;height:1px;'>" +
		(doNullSpace ? "&nbsp;" : "") + "</TD>");
}

function getTab(width)
{	return("<TD colspan='" + width + "' style='width:" + width + "px;'></TD>");
}

function getTabPix(color, width)
{	return("<TD colspan='" + width + "' style='width:" + width + "px;background:" + color + "'></TD>");
}

function getCorners(bordCol, innerCol, cornerRad, width)
{	var tCorns = new Array(4);
	var iCell, bCell, oCell;
	var iLen, oLen, oRad, iRad;

	for (var z=0; z< tCorns.length; z++)
	{	tCorns[z] = "";
	}

	oRad = (cornerRad*cornerRad);
	iRad = ((cornerRad-width)*(cornerRad-width));

	for(var i=0; i<cornerRad; i++)
	{	iLen = Math.round(Math.sqrt(iRad - (i*i)));
		oLen = cornerRad - Math.round(Math.sqrt(oRad - (i*i)));

		iLen = iLen ? iLen : 0;
		oLen = (oLen < 0) ? 0 : oLen;

		iCell = iLen ? getTabPix(innerCol, iLen) : "";
		bCell = getTabPix (bordCol, (cornerRad - (oLen + iLen)));
		oCell = oLen ? getTab (oLen) : "";

		tCorns[0] = "<TR style='height:1px;'>" + oCell + bCell + iCell + "</TR>" + tCorns[0];
		tCorns[1] = "<TR style='height:1px;'>" + iCell + bCell + oCell + "</TR>" + tCorns[1];
		tCorns[2] += "<TR style='height:1px;'>" + oCell + bCell + iCell + "</TR>";
		tCorns[3] += "<TR style='height:1px;'>" + iCell + bCell + oCell + "</TR>";
	}

	for (var z=0; z< tCorns.length; z++)
	{	tCorns[z] = "<TD WIDTH='" + cornerRad + "' HEIGHT='1'><TABLE HEIGHT='1' border='0' cellpadding='0' cellspacing='0'>" + tCorns[z] + "</TABLE></TD>";
	}


	return (tCorns);
}
