var lpenabled = true;
var activeTab = "defaulttab";
var activeButton = "";
function checklp(livepreviewcheck)
{
	if (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(http://www.glamisdunes.com/invision/images/tab_off.gif)";

	if (tabButtonName != "")
	{
		activeButton = tabButtonName;
		document.getElementById(tabButtonName).style.backgroundImage = "url(http://www.glamisdunes.com/invision/images/tab_on.gif)";
	}
}

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 lpChangePropsByTagId(tagName, tagId, propName, propValue)
{
	if (lpenabled) changePropsByTagId(tagName, tagId, propName, propValue);
}
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.personalize.tableborderthickness.options[3].selected = true; }
	if (styleChoice != "style" && document.getElementById("tableborderstyle").value == "") { changePropsByTagId('table', 'PreviewTable', 'style.borderStyle', 'solid'); document.personalize.tableborderstyle.options[2].selected = true; }
	if (document.getElementById("tablebordercolor").value == "") { changePropsByTagId('table', 'PreviewTable', 'style.borderColor', 'white'); }
}
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.personalize.bgcolor.value;
	var bgUrl = document.personalize.bgurl.value;
	var borderThickness = document.personalize.tableborderthickness.value;
	var borderStyle = document.personalize.tableborderstyle.value;
	var borderColour = document.personalize.tablebordercolor.value;
	var tableBgColour = document.personalize.tablebgcolor.value;
	var tableBgUrl = document.personalize.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.personalize.select_background.options[document.personalize.select_background.selectedIndex].value !== 'none')
	{
    	if (document.personalize.select_background.selectedIndex <47 )
        {
			document.getElementById(element).value = 'images/bg/' + document.personalize.select_background.options[document.personalize.select_background.selectedIndex].value;
			changePropsByTagId(tag,preview,'style.backgroundImage','url(' + document.getElementById(element).value + ')');
        }
        else
        {
			document.getElementById(element).value = 'gallery_imgs/'+document.personalize.select_background.options[document.personalize.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.personalize.select_background.options[document.personalize.select_background.selectedIndex].value !== 'none')
	{
    	if (document.personalize.select_background.selectedIndex < 47)
        {
			document.images.background.src = 'images/bg/preview/'+document.personalize.select_background.options[document.personalize.select_background.selectedIndex].value;
        }
        else
        {
			var temp = new Array();
			temp = document.personalize.select_background.options[document.personalize.select_background.selectedIndex].value.split('/');

			document.images.background.src = 'gallery_imgs/'+temp[0]+'/tn_'+temp[1];
        }
	}
	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');
}