var lpenabled = true;
var activeTab = "defaulttab";
var activeButton = "";
function checklp() {
	if (document.getElementById("livepreviewcheck").checked == true) {
	changePropById('livepreview', 'style.display', 'none');
	lpenabled = false; }
	else {
	changePropById('livepreview', 'style.display', 'block');
	lpenabled = true; }
}
function OpenTab(tabName, tabButtonName)
{
    if (activeTab != "")
        document.getElementById(activeTab).style.display = "none";
        
    activeTab = tabName;
    document.getElementById(tabName).style.display = "block";

    if (activeButton != "")
        document.getElementById(activeButton).style.backgroundImage = "url(../images/tab_off.gif)";

	if (tabButtonName != "")
    {
        activeButton = tabButtonName;
        document.getElementById(tabButtonName).style.backgroundImage = "url(../images/tab_on.gif)";
    }
}

function getObj(id)
{
    if (document.all) { return document.all[id]; } 
    else {	return document.getElementById(id);	}
}

function changePropById(objId, propName, propValue)
{
    var obj = getObj(objId);
    if (obj && (propName.indexOf("style.")==-1 || obj.style))
    {
        if (propValue == true || propValue == false)
            eval("obj."+propName+"="+propValue);
        else
            eval("obj."+propName+"='"+propValue+"'");
    }
}

function changePropsByTagId(tagName, tagId, propName, propValue)
{
	if (!lpenabled) return;
    var elems = document.getElementsByTagName(tagName);
    for (var i = 0; i < elems.length; i++)
    {
        if (elems[i].id && elems[i].id.indexOf(tagId) == 0)
        {
            changePropById(elems[i].id, propName, propValue);
        }
    }
}

function bgPosition(position)
{
    if (position == "Positioned")
    {
        changePropById('positioning','style.display','block');
        changePropById('tiling','style.display','none');
        changePropById('previewbody','style.backgroundRepeat','no-repeat');
		changePropbyID('previewbody','style.backgroundPosition','center center');
		changePropById('previewbody','style.backgroundAttachment','scroll');
    }
    else if (position == "Tiled")
    {
        changePropById('tiling','style.display','block');
        changePropById('positioning','style.display','none');
        changePropById('previewbody','style.backgroundRepeat','repeat');
    }
}
function bgAttachment(position)
{
    if (position == "Scroll")
    {
        changePropById('previewbody','style.backgroundAttachment','scroll');
    }
    else if (position == "Fixed")
    {
		changePropById('previewbody','style.backgroundAttachment','fixed');
    }
}
function changeCSS(theClass,element,value)
{
	if (!lpenabled) return;
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	  }
	 }	
}
function checkcheckbox(tagname,tagid,underline,linethrough) 
{
	if (!lpenabled) return;
    var style = "";
    if (document.getElementById(underline).checked == true) { style += 'underline '; }
    if (document.getElementById(linethrough).checked == true) { style += 'line-through '; }
    if (style == "") { textDecoration = 'none'; } else { textDecoration = style; }
    changePropsByTagId(tagname,tagid,'style.textDecoration',textDecoration);
}
document.onkeypress = checkCR;
function checkCR(evt)
{
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
function checktables(styleChoice)
{
	if (!lpenabled) return;
	if (styleChoice != "thickness" && document.getElementById("tableborderthickness").value == "") { changePropsByTagId('table', 'PreviewTable', 'style.borderWidth', '2px'); document.form22.tableborderthickness.options[3].selected = true; }
	if (styleChoice != "style" && document.getElementById("tableborderstyle").value == "") { changePropsByTagId('table', 'PreviewTable', 'style.borderStyle', 'solid'); document.form22.tableborderstyle.options[2].selected = true; }
	if (document.getElementById("tablebordercolor").value == "") { changePropsByTagId('table', 'PreviewTable', 'style.borderColor', 'black'); }
}
function gotoprev(id)
{
	window.location = id;
}

var tablesTrans = false;
var tableSettings = Array();
var trSettings = Array();
var tdSettings = Array();
function saveTables()
{
    var livepreview = document.getElementById("previewbody");
    var tables = livepreview.getElementsByTagName("table");
    var trs = livepreview.getElementsByTagName("tr");
    var tds = livepreview.getElementsByTagName("td");
    
    for (i = 0; i < tables.length; i++)
    {
        tableSettings[i] = Array();
        tableSettings[i]["colour"] = tables[i].style.backgroundColor;
        tableSettings[i]["border"] = tables[i].style.border;
    }
    
    for (i = 0; i < trs.length; i++)
    {
        trSettings[i] = Array();
        trSettings[i]["colour"] = trs[i].style.backgroundColor;
        trSettings[i]["border"] = trs[i].style.border;
   }
    
    for (i = 0; i < tds.length; i++)
    {
        tdSettings[i] = Array();
        tdSettings[i]["colour"] = tds[i].style.backgroundColor;
        tdSettings[i]["border"] = tds[i].style.border;
    }
}
function changeTables(fromTableBgColour)
{
    var livepreview = document.getElementById("previewbody");
    var tables = livepreview.getElementsByTagName("table");
    var trs = livepreview.getElementsByTagName("tr");
    var tds = livepreview.getElementsByTagName("td");
    
    var bgColour = document.form22.bgcolor.value;
    var bgUrl = document.form22.bgurl.value;
    var borderThickness = document.form22.tableborderthickness.value;
    var borderStyle = document.form22.tableborderstyle.value;
    var borderColour = document.form22.tablebordercolor.value;
    var tableBgColour = document.form22.tablebgcolor.value;
    var tableBgUrl = document.form22.tablebgurl.value;
    
    if (tablesTrans == false && bgColour != "" || bgUrl != "" || tableBgColour != "" || tableBgUrl != "")
    {
        for (i = 0; i < tables.length; i++)
        {
            if (fromTableBgColour || tableBgColour == "")
                tables[i].style.backgroundColor = "transparent";
            if (borderThickness == "" && borderStyle == "" && borderColour == "")
                tables[i].style.border = "none";
        }
        for (i = 0; i < trs.length; i++)
        {
            trs[i].style.backgroundColor = "transparent";
            trs[i].style.border = "none";
        }
        for (i = 0; i < tds.length; i++)
        {
            tds[i].style.backgroundColor = "transparent";
            tds[i].style.border = "none";
        }
        tablesTrans = true;
    }
    else if  (tablesTrans && bgColour == "" && bgUrl == "" && borderThickness == "" && borderStyle == "" && borderColour == "" && tableBgColour == "" && tableBgUrl == "")
    {
        for (i = 0; i < tables.length; i++)
        {
            tables[i].style.backgroundColor = tableSettings[i]["colour"];
            tables[i].style.border = tableSettings[i]["border"];
        }
        for (i = 0; i < trs.length; i++)
        {
            trs[i].style.backgroundColor = trSettings[i]["colour"];
            trs[i].style.border = trSettings[i]["border"];
        }
        for (i = 0; i < tds.length; i++)
        {
            tds[i].style.backgroundColor = tdSettings[i]["colour"];
            tds[i].style.border = tdSettings[i]["border"];
        }
        tablesTrans = false;
    }
}
function selectImage(element,preview,tag,layer)
{
	if (document.form22.select_background.options[document.form22.select_background.selectedIndex].value !== 'none')
	{
		document.getElementById(element).value = 'http://www.imagecloset.com/mygen/backgrounds/' + document.form22.select_background.options[document.form22.select_background.selectedIndex].value;
		changePropsByTagId(tag,preview, 'style.backgroundImage', 'url(' + document.getElementById(element).value + ')');
	}
	else
	{
		document.getElementById(element).value = '';
	}
	changePropById('background-select', 'style.display', 'none');
	changePropById(layer, 'style.display', 'none');
}
function showimage()
{
	if (!document.images)
		return;
	if (document.form22.select_background.options[document.form22.select_background.selectedIndex].value !== 'none')
		document.images.background.src = 'images/backgrounds/' + document.form22.select_background.options[document.form22.select_background.selectedIndex].value;
	else
		document.images.background.src= 'images/spacer.gif';
}
function showImageSelect(elem,layer) {
	var imageselect = getObj('background-select');
	imageselect.style.top = getAbsoluteOffsetTop(elem) + 20 + "px";
    imageselect.style.left = getAbsoluteOffsetLeft(elem) + "px";
	changePropById('background-select', 'style.display', 'block');
	changePropById(layer, 'style.display', 'block');
}
