<!--
var HS_Structure = new Array();
var HS_Output = new Array();
var HS_SelectedItem = "";
var HS_InsertPos = "";
var HS_XML = "";
HS_DoDrag = 0;
HS_StartX = 0;
HS_StartY = 0;
document.onmousedown = HS_TrackMouse;
document.onmouseup = HS_DropItem;
document.onmousemove = HS_DragItem;




function HS_BuildPage(){
	HS_Output['column'] = new Array();
	HS_Output['cell'] = new Array();
	HS_Output['container'] = new Array();
	HS_Output['block'] = new Array();
	HS_Output['template'] = new Array();
	HS_Output['page_content'] = "<table width='100%' cellpadding='0' cellspacing='3' border='0'>";
	HS_Output['page_content']+= "<tr><td colspan='"+HS_ColumnsCount()+"'>"+HS_BuildItem("['header']")+"</td></tr>";
	HS_Output['page_content']+= "<tr>";
	for(var Column in HS_Structure){
		if(!isNaN(Column)){
			HS_Output['page_content']+= "<td valign='top' width='"+ HS_Structure[Column]['width'] +"'>";
			HS_Output['page_content']+= HS_BuildItem("["+Column+"]");
			HS_Output['page_content']+= "</td>";
		}//if
	}//for
	HS_Output['page_content']+= "</tr>";
	HS_Output['page_content']+= "<tr><td colspan='"+HS_ColumnsCount()+"'>"+HS_BuildItem("['footer']")+"</td></tr>";
	HS_Output['page_content']+= "</table>";
}//HS_BuildPage

function HS_ColumnsCount(){
	var HS_Count = 0;
	for(var Column in HS_Structure){
		if(!isNaN(Column))
			HS_Count++;
	}//for
	return HS_Count;
}//HS_ColumnsCount

function HS_BuildItem(ItemID){
		
	var ItemOfArray = new Array();
	ItemOfArray = eval("HS_Structure"+ItemID);

	var ItemType = ItemOfArray['type'];
	if(ItemType != "header" && ItemType != "footer")
		var ItemName = ItemOfArray['id'];

	var Depth = ItemID.split("]");

	var SubItemStart = 0;
	
	var CloseButton = "but_close_grey.gif";
	var ItemStyle = "nontemplate";

	if(ItemType == 'column'){
		ItemName = "Column: "+ItemName;
	}//if
	else if(ItemType == 'cell')
		ItemName = "Cell: "+ItemName;
	else if(ItemType == "container")
		ItemName = "Container: "+ItemName;
	else if(ItemType == "block")
		ItemName = "Block: "+ItemName;
	else if(ItemType == 'template'){
		ItemName = "Smarty Template";
		CloseButton = "but_close_blue.gif";
		ItemStyle = "template";
	}//else if
	else if(ItemType == 'header'){
		ItemName = "Header";
		ItemStyle = "other";
	}//else if
	else if(ItemType == 'footer'){
		ItemName = "Footer";
		ItemStyle = "other";
	}//else if

	if(ItemName.length > 28){
		ItemName = ItemName.substr(0,26)+"...";
	}//if

	var ItemOutput = "";
	
	if(ItemType != "header" && ItemType != "footer"){

		ItemOutput+= "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
		ItemOutput+= "<tr><td onMouseDown='HS_TrackItem(\""+ ItemID +"\")' class='"+ItemStyle+"_header'><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td class='"+ItemStyle+"_title'>"+ItemName+"</td><td align='right'><a href='javascript:HS_RemoveItem(\""+ ItemID +"\");'><img onMouseDown='HS_RemoveItem(\""+ ItemID +"\");' src='"+HS_Path+"gfx/buttons/"+CloseButton+"' width='20' height='20' border='0'></a></td></tr></table></td></tr>";
		ItemOutput+= "<tr><td class='"+ItemStyle+"_box body' align='center'>";
		ItemOutput+= "<div id='"+ ItemID +"[\"c\"]'></div>";
		ItemOutput+= "</td></tr></table>";

		HS_Output[ItemType][ItemID] = new Array();
		HS_Output[ItemType][ItemID]["h"] = ItemOutput;
		HS_Output[ItemType][ItemID]["c"] = "";

		if(ItemType!='template'){
			for(var SubItem in ItemOfArray){
				if(!isNaN(SubItem)){
					HS_Output[ItemType][ItemID]["c"]+= HS_BuildItem(ItemID+"["+SubItem+"]")+"<table cellpadding='0' cellspacing='0' border='0'><tr><td><img src='"+HS_Path+"gfx/spacer.gif' width='1' height='2' border='0'></td></tr></table>";
				}//if
			}//for
		}//if
		else{
			var FileName = ItemOfArray['file'];
			if(FileName.length > 28){
				FileName = FileName.substr(0,26)+"...";
			}//if
			HS_Output[ItemType][ItemID]["c"] = FileName;
		}//else
		var ItemHandler = "<div id='"+ ItemID +"[\"h\"]'></div>";
	}//if
	else{
		ItemOutput+= "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
		ItemOutput+= "<tr><td onMouseDown='HS_TrackItem(\"[\\\""+ ItemType +"\\\"]\")' class='"+ItemStyle+"_header'><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td class='"+ItemStyle+"_title'>"+ItemName+"</td></tr></table></td></tr>";
		ItemOutput+= "<tr><td class='"+ItemStyle+"_box body' align='center'>";
		ItemOutput+= "<div id='"+ ItemType +"[\"c\"]'></div>";
		ItemOutput+= "</td></tr></table>";
		
		HS_Output[ItemType] = new Array();
		HS_Output[ItemType]["h"] = ItemOutput;
		HS_Output[ItemType]["c"] = "";
		
		var FileName = ItemOfArray['file'];
		if(FileName.length > 28){
			FileName = FileName.substr(0,26)+"...";
		}//if
		HS_Output[ItemType]["c"] = FileName;
		var ItemHandler = "<div id='"+ ItemType +"[\"h\"]'></div>";
	}//else
		
	return ItemHandler;
}//HS_BuildItem

function HS_PullOutput(){
		
	HS_WriteOutput(HS_Output['page_content'],"page_content");	
	HS_WriteOutput(HS_Output['header']['h'],"header[\"h\"]");
	HS_WriteOutput(HS_Output['header']['c'],"header[\"c\"]");
	HS_WriteOutput(HS_Output['footer']['h'],"footer[\"h\"]");
	HS_WriteOutput(HS_Output['footer']['c'],"footer[\"c\"]");

	for(var Item in HS_Output['column']){
		HS_WriteOutput(HS_Output['column'][Item]['h'],Item+"[\"h\"]");
		HS_WriteOutput(HS_Output['column'][Item]['c'],Item+"[\"c\"]");
	}//for

	for(var Item in HS_Output['cell']){
		HS_WriteOutput(HS_Output['cell'][Item]['h'],Item+"[\"h\"]");
		HS_WriteOutput(HS_Output['cell'][Item]['c'],Item+"[\"c\"]");
	}//for

	for(var Item in HS_Output['container']){
		HS_WriteOutput(HS_Output['container'][Item]['h'],Item+"[\"h\"]");
		HS_WriteOutput(HS_Output['container'][Item]['c'],Item+"[\"c\"]");
	}//for

	for(var Item in HS_Output['block']){
		HS_WriteOutput(HS_Output['block'][Item]['h'],Item+"[\"h\"]");
		HS_WriteOutput(HS_Output['block'][Item]['c'],Item+"[\"c\"]");
	}//for

	for(var Item in HS_Output['template']){
		HS_WriteOutput(HS_Output['template'][Item]['h'],Item+"[\"h\"]");
		HS_WriteOutput(HS_Output['template'][Item]['c'],Item+"[\"c\"]");
	}//for

}//HS_PullOutput

function HS_WriteOutput(HTMLCode, HandlerID){
	if(HTMLCode == '')
		HTMLCode = "&nbsp;";
	var Handler = document.getElementById(HandlerID);
	Handler.innerHTML = HTMLCode;
}//HS_WriteOutput

function HS_SelectItem(ItemID){
	HS_UnselectItem();	

	HS_ItemProperties(ItemID);	

	var ItemOfArray = new Array();
	ItemOfArray = eval("HS_Structure"+ItemID);	
	var ItemType = ItemOfArray['type'];	
	var ItemParent = ItemOfArray['parent'];

	if(ItemID.indexOf("]")!=-1){
		var Depth = ItemID.split("]");
		var AddOffset = Depth.length-1;
	}//if
	else
		var AddOffset = 0;
	
	if(ItemType != "header" && ItemType != "footer" && ItemType != "column"){		
		var SourceHandler = document.getElementById(ItemID+"[\"h\"]");
		var SourcePosition = new Array();
		SourcePosition = HS_Position(SourceHandler);
		var SourceWidth = SourceHandler.offsetWidth;
		var SourceHeight = SourceHandler.offsetHeight;
		var DistHandler = document.getElementById('ItemSelector');
		DistHandler.innerHTML = SourceHandler.innerHTML;
		SourceHandler.style.height = SourceHeight;
		SourceHandler.innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'><tr><td>&nbsp;</td></tr></table>";
		DistHandler.style.top  = SourcePosition['Top']+1;
		DistHandler.style.left = SourcePosition['Left']+AddOffset;
		DistHandler.style.width = SourceWidth;
		DistHandler.style.height = SourceHeight-1;
		DistHandler.style.visibility = "visible";
		DistHandler.style.border = "1px solid #D22323";
	}//if

	HS_SelectedItem = ItemID;
}//HS_SelectItem

function HS_UnselectItem(){
	var ItemID = HS_SelectedItem;	

	if(ItemID!=''){
		var ItemOfArray = new Array();
		ItemOfArray = eval("HS_Structure"+ItemID);
		if(ItemOfArray && ItemOfArray['type'] != "header" && ItemOfArray['type'] != "footer" && ItemOfArray['type'] != "column"){
		var ItemType = ItemOfArray['type'];
		var SourceHandler = document.getElementById('ItemSelector');
		var DistHandler = document.getElementById(ItemID+"[\"h\"]");
		DistHandler.innerHTML = SourceHandler.innerHTML;
		SourceHandler.innerHTML = "&nbsp;";
		SourceHandler.style.visibility = "hidden";
		HS_SelectedItem = "";
		var PropertiesHandler = document.getElementById('item_properties');
		PropertiesHandler.innerHTML = "<span class='highlight_small'>&lt; please select an item &gt;</span>";
		}//if
		else{
		var SourceHandler = document.getElementById('ItemSelector');
		SourceHandler.innerHTML = "&nbsp;";
		SourceHandler.style.visibility = "hidden";
		HS_SelectedItem = "";
		var PropertiesHandler = document.getElementById('item_properties');
		PropertiesHandler.innerHTML = "<span class='highlight_small'>&lt; please select an item &gt;</span>";
		}//else
	}//if
}//HS_UnselectItem

function HS_Position(Obj){
	var Position = new Array();
	Position['Top']  = 0;
	Position['Left'] = 0;
	if (Obj.offsetParent){
		while (Obj.offsetParent){
			Position['Top']+= Obj.offsetTop;
			Position['Left']+= Obj.offsetLeft;
			Obj = Obj.offsetParent;
		}//while
	}//if
	else if (Obj.x && Obj.y){
		Position['Left']+= Obj.x;
		Position['Top']+= Obj.y;
	}//else if
	return Position;
}//HS_Position

function HS_ItemProperties(ItemID){

	var ItemOfArray = new Array();
	ItemOfArray = eval("HS_Structure"+ItemID);	
	var ItemType    = ItemOfArray['type'];	

	var Labels = new Array();
	var Fields = new Array();
	var Additional = new Array();
	var FormOutput;
	var Button;
	var ColSpan;

	switch(ItemType){
		case "column":
			Labels[0] = "Type";
			Fields[0] = "Column";
			Labels[1] = "ID";
			Fields[1] = ItemOfArray['id'];			
			Labels[2] = "Width";
			Fields[2] = "<input type='text' name='width' size='5' class='textfield' value='"+ ItemOfArray['width'] +"'>";
			Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
		case "cell":
		    Labels[0] = "Type";
		    Fields[0] = "Cell";		    
		    Labels[1] = "ID";
			Fields[1] = ItemOfArray['id'];
		    Button = "";
		    ColSpan = 1;
			break;
		case "container":
		    Labels[0] = "Type";
		    Fields[0] = "Container";
		    Labels[1] = "Name";
		    Fields[1] = "<input type='text' name='id' size='30' class='textfield' value='"+ ItemOfArray['id'] +"'>";
		    Labels[2] = "Open Template";
		    Fields[2] = "<input type='text' name='open' size='30' class='textfield' value='"+ ItemOfArray['open'] +"'>";
		    Labels[3] = "Close Template";
		    Fields[3] = "<input type='text' name='close' size='30' class='textfield' value='"+ ItemOfArray['close'] +"'>";
		    if(ItemOfArray['open'] && ItemOfArray['open'] != "")
		    Additional[0] = "Open Template:<br><textarea wrap='off' class='textfield' name='source_open' style='width:100%; height:150px'>...loading...</textarea>";
		    if(ItemOfArray['close'] && ItemOfArray['close'] != "")
		    Additional[1] = "Close Template:<br><textarea wrap='off' class='textfield' name='source_close' style='width:100%; height:150px'>...loading...</textarea>";
		    Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
		case "block":
			Labels[0] = "Type";
		    Fields[0] = "Block";
		    Labels[1] = "Name";
		    Fields[1] = "<input type='text' name='id' size='30' class='textfield' value='"+ ItemOfArray['id'] +"'>";
		    Labels[2] = "Open Template";
		    Fields[2] = "<input type='text' name='open' size='30' class='textfield' value='"+ ItemOfArray['open'] +"'>";
		    Labels[3] = "Close Template";
		    Fields[3] = "<input type='text' name='close' size='30' class='textfield' value='"+ ItemOfArray['close'] +"'>";
		    Labels[4] = "Assign Variable";
		    Fields[4] = "<input type='text' name='assign' size='30' class='textfield' value='"+ ItemOfArray['assign'] +"'>";
		    Labels[5] = "Block Type";
		    Fields[5] = "<input type='text' name='btype' size='30' class='textfield' value='"+ ItemOfArray['btype'] +"'>";
		    if(ItemOfArray['open'] && ItemOfArray['open'] != "")
		    Additional[0] = "Open Template:<br><textarea wrap='off' class='textfield' name='source_open' style='width:100%; height:150px'>...loading...</textarea>";
		    if(ItemOfArray['close'] && ItemOfArray['close'] != "")
		    Additional[1] = "Close Template:<br><textarea wrap='off' class='textfield' name='source_close' style='width:100%; height:150px'>...loading...</textarea>";
		    Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
		case "template":
			Labels[0] = "Type";
		    Fields[0] = "Template";
		    Labels[1] = "Template File";
		    Fields[1] = "<input type='text' name='file' size='30' class='textfield' value='"+ ItemOfArray['file'] +"'>";
		    if(ItemOfArray['file'] && ItemOfArray['file'] != "")
		    Additional[0] = "<textarea wrap='off' class='textfield' name='source' style='width:100%; height:300px'>...loading...</textarea>";
		    Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
		case "header":
			Labels[0] = "Type";
		    Fields[0] = "Header";
		    Labels[1] = "Template File";
		    Fields[1] = "<input type='text' name='file' size='30' class='textfield' value='"+ ItemOfArray['file'] +"'>";
		    if(ItemOfArray['file'] && ItemOfArray['file'] != "")
		    Additional[0] = "<textarea wrap='off' class='textfield' name='source' style='width:100%; height:300px'>...loading...</textarea>";
		    Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
		case "footer":
			Labels[0] = "Type";
		    Fields[0] = "Footer";
		    Labels[1] = "Template File";
		    Fields[1] = "<input type='text' name='file' size='30' class='textfield' value='"+ ItemOfArray['file'] +"'>";
		    if(ItemOfArray['file'] && ItemOfArray['file'] != "")
		    Additional[0] = "<textarea wrap='off' class='textfield' name='source' style='width:100%; height:300px'>...loading...</textarea>";
		    Button = "<input type='button' class='button' value='Apply Changes' onclick='HS_UpdateItemProperties();'>";
			break;
	}//switch

	FormOutput = "<table cellpadding='0' cellspacing='3' border='0' align='center'";
	if(ItemOfArray['file'] || ItemOfArray['open'] || ItemOfArray['close'])
		 FormOutput+= " width='100%'";
	FormOutput+= "><tr>";
	FormOutput+= "<form action='' method='post' onSubmit='HS_UpdateItemProperties();return false;' name='properties_form'>";
	for(var i in Labels){
		FormOutput+= "<tr><td class='body'>"+ Labels[i] +"</td>";
		FormOutput+= "<td class='highlight'>"+ Fields[i] +"</td></tr>";
	}//for
	for(var i in Additional){
		FormOutput+= "<tr><td colspan='2' align='center' class='body'>"+Additional[i]+"</td></tr>";
	}//for
	FormOutput+= "</tr><tr><td colspan='2' align='center'>"+ Button +"</td></tr>";
	FormOutput+= "</form>";
	FormOutput+= "</table>";

	var PropertiesHandler = document.getElementById('item_properties');
	PropertiesHandler.innerHTML = FormOutput;
	
	if(ItemType == "template" || ItemType == "header" || ItemType == "footer"){
		if(ItemOfArray['file'] && ItemOfArray['file'] != "")
			HS_EditFile(ItemOfArray['file'],"source");
	}//if
	else if(ItemType == "container" || ItemType == "block"){
		if(ItemOfArray['open'] && ItemOfArray['open'] != "" && ItemOfArray['close'] && ItemOfArray['close'] != "")
			HS_EditFile(ItemOfArray['open']+"|"+ItemOfArray['close'],"source_open","source_close");
		else if(ItemOfArray['open'] && ItemOfArray['open'] != "")
			HS_EditFile(ItemOfArray['open'],"source_open");
		else if(ItemOfArray['close'] && ItemOfArray['close'] != "")
			HS_EditFile(ItemOfArray['close'],"source_close");
	}//else if

	HS_CheckSize();
}//HS_ItemProperties

function HS_AddItem(){
	HS_UnselectItem();
	var FormOutput;
	FormOutput = "<table cellpadding='0' cellspacing='3' border='0' align='center'>";
	FormOutput+= "<form action='' method='post' onSubmit='HS_CreateNewItem();return false;' name='new_item'>";
	FormOutput+= "<tr><td class='body'>Item Type</td>";
	FormOutput+= "<td class='highlight'>";
	FormOutput+= "<select name='ItemType' class='textfield'>";
	FormOutput+= "<option value='column'>Column</option>";
	FormOutput+= "<option value='cell'>Cell</option>";
	FormOutput+= "<option value='container'>Container</option>";
	FormOutput+= "<option value='block'>Block</option>";
	FormOutput+= "<option value='template'>Template</option>";
	FormOutput+= "</select>";
	FormOutput+= "</td></tr>";
	FormOutput+= "<tr><td colspan='2' align='center'>";
	FormOutput+= "<input type='submit' value='Add' class='button'>";
	FormOutput+= "</td></tr>";
	FormOutput+= "</form>";
	FormOutput+= "</table>";
	
	var PropertiesHandler = document.getElementById('item_properties');
	PropertiesHandler.innerHTML = FormOutput;
}//HS_AddItem

function HS_DisplayCreationError(){
	var ErrorOutput = "<span class='highlight_small'>Please, make sure you have created a parent element for this new item.</span>";
	var PropertiesHandler = document.getElementById('item_properties');
	PropertiesHandler.innerHTML = ErrorOutput;
}//HS_DisplayCreationError

function HS_CreateNewItem(){
	var Ind = document.new_item.ItemType.selectedIndex;	
	var NewItemType = document.new_item.ItemType.options[Ind].value;
	var JSCode;
	
	var NewPos = HS_GetNewItemPos(NewItemType);
	
	if(!NewPos){
		HS_DisplayCreationError();		
	}//if
	else {
		
	var NewPosID = NewPos['ID'];
	var NewItemShortID = NewPosID.substring(NewPosID.lastIndexOf('[')+1,NewPosID.lastIndexOf(']'));	
	var NewItemParent = NewPosID.substring(0,NewPosID.lastIndexOf('['));
	var NewItemParentType = NewPos['type'];
	
	var ItemOfArray = new Array;
	
	switch(NewItemType){
		case 'column':
			ItemOfArray['width'] = "";
			ItemOfArray['type'] = "column";
			ItemOfArray['id'] = "1";
			ItemOfArray['parent'] = "";
			break;
		case 'cell':
			ItemOfArray['type'] = "cell";
			ItemOfArray['id'] = "1";
			ItemOfArray['parent'] = NewItemParent;
			break;
		case 'container':
			ItemOfArray['type'] = "container";
			ItemOfArray['id'] = "";
			ItemOfArray['open'] = "";
			ItemOfArray['close'] = "";
			ItemOfArray['parent'] = NewItemParent;
			break;
		case 'block':
			ItemOfArray['type'] = "block";
			ItemOfArray['id'] = "";
			ItemOfArray['open'] = "";
			ItemOfArray['close'] = "";
			ItemOfArray['parent'] = NewItemParent;
			ItemOfArray['assign'] = "";
			ItemOfArray['btype'] = "";
			break;
		case 'template':
			ItemOfArray['type'] = "template";
			ItemOfArray['id'] = "";
			ItemOfArray['file'] = "";
			ItemOfArray['parent'] = NewItemParent;
			break;
	}//switch
	
	var NewStruct = new Array;
	if(NewItemParentType != "" && NewItemParentType != "column")
		var i=0;
	else
		var i=1;
	
	if(eval("HS_Structure"+NewPosID)){

	JSCode = "for(var Item in HS_Structure"+NewItemParent+"){\n"+
			 "if(isNaN(Item)){\n"+
			 "NewStruct[Item] = HS_Structure"+NewItemParent+"[Item];\n"+
		 	 "}//if\n"+
		 	 "else if(Item == NewItemShortID){\n"+
		  	 "NewStruct[i] = ItemOfArray;\n"+
		 	 "i++;\n"+
		 	 "NewStruct[i] = HS_Structure"+NewItemParent+"[Item];\n"+
		 	 "if(NewItemType == 'cell' || NewItemType == 'column')\n"+
		 	 "NewStruct[i]['id'] = i;\n"+
		 	 "i++;\n"+
		 	 "}//else if\n"+
		 	 "else{\n"+
		 	 "NewStruct[i] = HS_Structure"+NewItemParent+"[Item];\n"+
		 	 "if(NewItemType == 'cell' || NewItemType == 'column')\n"+
		 	 "NewStruct[i]['id'] = i;\n"+
		 	 "i++;\n"+
	     	 "}//else\n"+
		 	 "}//for\n"+
		 	 "HS_Structure"+NewItemParent+"= new Array;"+		 	 
	       	 "HS_Structure"+NewItemParent+"= NewStruct;";
	       	 
	}//if
	else{		
	JSCode = "HS_Structure"+NewPosID+" = ItemOfArray;";	
	}//else

	eval(JSCode);
	
	for(var Column in HS_Structure){
        if(!isNaN(Column))
        	HS_RewriteParent("["+Column+"]");
    }//for
	
	HS_BuildPage();
	HS_PullOutput();

	HS_CheckSize();

	HS_InsertPos = "";
	HS_HideSuggestor();
	
	HS_SelectItem(NewPosID);
	}//else
}//HS_CreateNewItem

function HS_GetNewItemPos(NewItemType){
	var AllowCreation;
	var ItemID;
	switch(NewItemType){
		case 'column':
			AllowCreation = 1;			
			ItemID = "[1]";
			ItemType = "";
			break;
		case 'cell':
			for(var Item in HS_Output['column']){
				ItemID = Item+"[1]";				
				ItemType = "column";
				break;
			}//for
			if(ItemID)
				AllowCreation = 1;
			else
				AllowCreation = 0;
			break;
		case 'container':
			for(var Item in HS_Output['cell']){				
				ItemID = Item+"[0]";				
				ItemType = "cell";
				break;
			}//for
			if(ItemID)
				AllowCreation = 1;
			else
				AllowCreation = 0;
			break;
		case 'block':
			for(var Item in HS_Output['cell']){
				ItemID = Item+"[0]";
				ItemType = "cell";
				break;
			}//for
			if(ItemID)
				AllowCreation = 1;
			else
				AllowCreation = 0;
			break;
		case 'template':
			for(var Item in HS_Output['block']){
				ItemID = Item+"[0]";
				ItemType = "block";
				break;
			}//for
			if(ItemID)
				AllowCreation = 1;
			else
				AllowCreation = 0;
			break;
	}//switch	
	
	if(AllowCreation){
		var RetAr = new Array;
		RetAr['ID'] = ItemID;
		RetAr['type'] = ItemType;
		return RetAr;
	}//if
	else
		return false;
}//HS_GetNewItemPos

function HS_UpdateItemProperties(){
	var Error       = 0;
	var ItemID      = HS_SelectedItem;
	var ArrayItem   = "HS_Structure"+ItemID;
	var ItemOfArray = new Array();
	ItemOfArray = eval(ArrayItem);
	var ItemType    = ItemOfArray['type'];
	var Properties  = document.properties_form;

	switch(ItemType){
		case "column":
			eval(ArrayItem+"['width'] = \""+Properties.width.value+"\"");
			break;
		case "cell":
		    //not applicable
			break;
		case "container":
			if(Properties.open.value.indexOf("./") != -1 ||
			   Properties.open.value.indexOf("../") != -1 ||
			   Properties.close.value.indexOf("./") != -1 ||
			   Properties.close.value.indexOf("../") != -1)
			{
				alert("File path shouldn't include relative paths (such as ./ or ../)");
				Error = 1;
			}//if
			else{
				eval(ArrayItem+"['open'] = \""+Properties.open.value+"\"");
				eval(ArrayItem+"['close'] = \""+Properties.close.value+"\"");
				eval(ArrayItem+"['id'] = \""+Properties.id.value+"\"");
			}//else
			break;
		case "block":
			if(Properties.open.value.indexOf("./") != -1 ||
			   Properties.open.value.indexOf("../") != -1 ||
			   Properties.close.value.indexOf("./") != -1 ||
			   Properties.close.value.indexOf("../") != -1)
			{
				alert("File path shouldn't include relative paths (such as ./ or ../)");
				Error = 1;
			}//if
			else{
				eval(ArrayItem+"['open'] = \""+Properties.open.value+"\"");
				eval(ArrayItem+"['close'] = \""+Properties.close.value+"\"");
				eval(ArrayItem+"['id'] = \""+Properties.id.value+"\"");
				eval(ArrayItem+"['assign'] = \""+Properties.assign.value+"\"");
				eval(ArrayItem+"['btype'] = \""+Properties.btype.value+"\"");
			}//else
			break;
		case "template":
			if(Properties.file.value.indexOf("./") != -1 || Properties.file.value.indexOf("../") != -1)
			{
				alert("File path shouldn't include relative paths (such as ./ or ../)");
				Error = 1;
			}//if
			else{
				eval(ArrayItem+"['file'] = \""+Properties.file.value+"\"");
			}//else
			break;
		case "header":
			if(Properties.file.value.indexOf("./") != -1 || Properties.file.value.indexOf("../") != -1)
			{
				alert("File path shouldn't include relative paths (such as ./ or ../)");
				Error = 1;
			}//if
			else{
				eval(ArrayItem+"['file'] = \""+Properties.file.value+"\"");
			}//else
			break;
		case "footer":
			if(Properties.file.value.indexOf("./") != -1 || Properties.file.value.indexOf("../") != -1)
			{
				alert("File path shouldn't include relative paths (such as ./ or ../)");
				Error = 1;
			}//if
			else{
				eval(ArrayItem+"['file'] = \""+Properties.file.value+"\"");
			}//else
			break;
	}//switch
	
	if(!Error){
	
		if(Properties.source){
			HS_SaveFile(ItemOfArray['file'],Properties.source.value);
		}//if
		if(Properties.source_open || Properties.source_close){
			if(Properties.source_open && Properties.source_close)
				HS_SaveFile(ItemOfArray['open']+"|"+ItemOfArray['close'],Properties.source_open.value+"|||--|||"+Properties.source_close.value);
			else if(Properties.source_open)
				HS_SaveFile(ItemOfArray['open'],Properties.source_open.value);
			else if(Properties.source_close)
				HS_SaveFile(ItemOfArray['close'],Properties.source_close.value);
		}//if

		HS_UnselectItem();
		HS_BuildPage();
		HS_PullOutput();
		HS_PreviewPage();
		HS_SelectItem(ItemID);	
	
	}//if

}//HS_UpdateItemProperties

function HS_Restore(){
	HS_UnselectItem();
	HS_CurrentStructure();
	HS_BuildPage();
	HS_PullOutput();
	HS_CheckSize();
}//HS_Restore

function HS_TrackItem(ItemID){
	if(HS_SelectedItem != ItemID){
		HS_SelectItem(ItemID);
	}//if
	else{
		var ItemOfArray = new Array();
		ItemOfArray = eval("HS_Structure"+ItemID);
		var ItemType    = ItemOfArray['type'];
		if(ItemType!='column' && ItemType!='header' && ItemType!='footer'){
			var ItemHandler = document.getElementById("ItemSelector");
			ItemHandler.style.filter = "Alpha(opacity=50)";
			HS_DoDrag = 1;
		}//if
	}//else
	return false;
}//HS_TrackItem

function HS_DropItem(){
	if(HS_DoDrag){
		HS_DoDrag = 0;
		var ItemHandler = document.getElementById("ItemSelector");
		ItemHandler.style.filter = "Alpha(opacity=100)";
		if(HS_InsertPos == "" && HS_SelectedItem!=""){
			var ItemID = HS_SelectedItem;
			HS_UnselectItem();
			HS_SelectItem(ItemID);
		}//if
		else if (HS_InsertPos != "")
			HS_RepositionItem();
    }//if
}//DropItem

function HS_TrackMouse(evn){
	var evn = (evn) ? evn : event;
	HS_StartX = evn.offsetX;
	HS_StartY = evn.offsetY;
}//HS_TrackMouse

function HS_DragItem(evn){
	if(HS_DoDrag){
		var evn = (evn) ? evn : event;
		var Handler = document.getElementById('ItemSelector');
		var NewX = evn.clientX - HS_StartX + document.body.scrollLeft;
        var NewY = evn.clientY - HS_StartY + document.body.scrollTop;
        var AllowMove = HS_CheckPosition(NewX,NewY);
        if(AllowMove){
			Handler.style.left = NewX;
			Handler.style.top  = NewY;
			HS_HighlightSuggestor(Handler.style.left, Handler.style.top);
        }//if
	}//if
}//HS_DragItem

function HS_CheckPosition(PosX,PosY,ItemWidth,ItemHeight){
	var PageHandler = document.getElementById('page_content');
	var PagePos = HS_Position(PageHandler);
	var DocXStart = PagePos['Left'];
	var DocYStart = PagePos['Top'];
	var DocXEnd = DocXStart + PageHandler.offsetWidth;
	var DocYEnd = DocYStart + PageHandler.offsetHeight;
	if(PosX < DocXStart || PosX+15 > DocXEnd || PosY < DocYStart || PosY+15 > DocYEnd){
		return false;
	}//if
	else
		return true;
}//HS_CheckPosition

function HS_HighlightSuggestor(ILeft,ITop){
	var ItemID = HS_SelectedItem;
	var ItemOfArray = new Array();
	ItemOfArray = eval("HS_Structure"+ItemID);
	var ItemType = ItemOfArray['type'];

	var SuggestTypes = new Array();
	var SuggestPTypes = new Array();
	var SuggestorShown = 0;

	switch(ItemType){
		case 'column':
			Suggest = 0;
			break;
		case 'cell':
			Suggest = 1;
			SuggestTypes[0] = "cell";
			SuggestPTypes[0] = "column";
			break;
		case 'container':
			Suggest = 1;
			SuggestTypes[0] = "container";
			SuggestTypes[1] = "block";
			SuggestPTypes[0] = "cell";
			break;
		case 'block':
			Suggest = 1;
			SuggestTypes[0] = "container";
			SuggestTypes[1] = "block";
			SuggestPTypes[0] = "cell";
			SuggestPTypes[1] = "container";
			break;
		case 'template':
			Suggest = 1;
			SuggestTypes[0] = "template";
			SuggestPTypes[1] = "block";
			break;
	}//switch

	ILeft = parseInt(ILeft);
	ITop = parseInt(ITop);

	if(Suggest){
		for(var SugType in SuggestTypes){
		var Type = SuggestTypes[SugType];
		for(var Item in HS_Output[Type]){
			var Handler = document.getElementById(Item+"[\"h\"]");
			var ItemPos = HS_Position(Handler);
			var ItemWidth = Handler.offsetWidth;
			var ItemHeight = Handler.offsetHeight;
			if(ILeft < (ItemPos['Left']+ItemWidth) &&
			   ILeft > ItemPos['Left'] &&
			   ITop <= (ItemPos['Top']+2) &&
			   ITop >= (ItemPos['Top']-2))
			{
				SuggestorShown = 1;
				HS_InsertPos = Item+"|before";
				HS_ShowSuggestor(ItemPos['Left'],ItemPos['Top'],ItemWidth);
				break;
			}//if
		}//for
		}//for
		if(!SuggestorShown){
		for(var SugType in SuggestPTypes){
		var Type = SuggestPTypes[SugType];
		for(var Item in HS_Output[Type]){
			var Handler = document.getElementById(Item+"[\"c\"]");
			var ItemPos = HS_Position(Handler);
			var ItemWidth = Handler.offsetWidth;
			var ItemHeight = Handler.offsetHeight;
			if(ILeft < (ItemPos['Left']+ItemWidth) &&
			   ILeft > ItemPos['Left'] &&
			   ITop <= (ItemPos['Top']+ItemHeight) &&
			   ITop >= (ItemPos['Top']+ItemHeight-4))
			{
				SuggestorShown = 1;
				HS_InsertPos = Item+"|after";
				HS_ShowSuggestor(ItemPos['Left']+2,ItemPos['Top']+ItemHeight+3,ItemWidth-6);
				break;
			}//if
		}//for
		}//for
		}//if
		if(!SuggestorShown){
			HS_InsertPos = "";
			HS_HideSuggestor();
		}//if
	}//if

}//HS_HighlightSuggestor

function HS_ShowSuggestor(SLeft,STop,SWidth){
	var SuggestorHandler = document.getElementById('InsertSuggestor');
	SuggestorHandler.style.top = STop-4;
	SuggestorHandler.style.left = SLeft+4;
	SuggestorHandler.style.width = SWidth;
	SuggestorHandler.style.visibility = "visible";
}//HS_ShowSuggestor

function HS_HideSuggestor(){
	var SuggestorHandler = document.getElementById('InsertSuggestor');
	SuggestorHandler.style.visibility = "hidden";
}//HS_HideSuggestor

function HS_RepositionItem(){
	var ItemID = HS_SelectedItem;
	var NewPos = HS_InsertPos.split("|");
	var NewPosID = NewPos[0];
	var NewPosType = NewPos[1];
	var JSCode;
	
	if(ItemID != NewPosID){
    	
        var NewItemShortID = NewPosID.substring(NewPosID.lastIndexOf('[')+1,NewPosID.lastIndexOf(']'));
		var NewPosItem = eval("HS_Structure"+NewPosID);
		var NewItemType = NewPosItem['type'];
		var NewItemParent = NewPosItem['parent'];
		var NewItemParentType = eval("HS_Structure"+NewItemParent+"['type']");
		
		var ItemShortID = ItemID.substring(ItemID.lastIndexOf('[')+1,ItemID.lastIndexOf(']'));

		var ItemOfArray = new Array();
		ItemOfArray = eval("HS_Structure"+ItemID);
		var ItemType = ItemOfArray['type'];
		var ItemParent = ItemOfArray['parent'];
		var ItemParentType = eval("HS_Structure"+ItemParent+"['type']");
		
		var NewStruct = new Array;
		if(ItemParentType != "column")
			i=0;
		else
			i=1;
		JSCode = "for(var Item in HS_Structure"+ItemParent+"){\n"+
				 "if(isNaN(Item)){\n"+
				 "NewStruct[Item] = HS_Structure"+ItemParent+"[Item];\n"+
				 "}//if\n"+
				 "else if(Item == ItemShortID){\n"+
				 "continue;\n"+
				 "}//else if\n"+
				 "else{\n"+
				 "NewStruct[i] = HS_Structure"+ItemParent+"[Item];\n"+
				 "if(ItemType == 'cell')\n"+
			 	 "NewStruct[i]['id'] = i;\n"+
				 "i++;\n"+
		   		 "}//else\n"+
			 	 "}//for\n"+
			 	 "HS_Structure"+ItemParent+"= new Array;"+
	         	 "HS_Structure"+ItemParent+"= NewStruct;";

		eval(JSCode);

		if(NewPosType == "before"){

		if(ItemParent == NewItemParent && NewItemShortID > ItemShortID){
			NewItemShortID--;
		}//if

		var NewStruct = new Array;
		if(NewItemParentType != "column")
			i=0;
		else
			i=1;
		JSCode = "for(var Item in HS_Structure"+NewItemParent+"){\n"+
				 "if(isNaN(Item)){\n"+
				 "NewStruct[Item] = HS_Structure"+NewItemParent+"[Item];\n"+
			 	 "}//if\n"+
			 	 "else if(Item == NewItemShortID){\n"+
			  	 "NewStruct[i] = ItemOfArray;\n"+
			  	 "NewStruct[i]['parent'] = \""+NewItemParent+"\";\n"+
			  	 "if(NewItemType == 'cell')\n"+
			  	 "NewStruct[i]['id'] = i;\n"+
			 	 "i++;\n"+
			 	 "NewStruct[i] = HS_Structure"+NewItemParent+"[Item];\n"+
			 	 "if(NewItemType == 'cell')\n"+
			 	 "NewStruct[i]['id'] = i;\n"+
			 	 "i++;\n"+
			 	 "}//else if\n"+
			 	 "else{\n"+
			 	 "NewStruct[i] = HS_Structure"+NewItemParent+"[Item];\n"+
			 	 "if(NewItemType == 'cell')\n"+
			 	 "NewStruct[i]['id'] = i;\n"+
			 	 "i++;\n"+
		     	 "}//else\n"+
			 	 "}//for\n"+
			 	 "HS_Structure"+NewItemParent+"= new Array;"+
	         	 "HS_Structure"+NewItemParent+"= NewStruct;";

		eval(JSCode);

		}//if
		else{
		
		var NewItemShortID = NewPosID.substring(NewPosID.lastIndexOf('[')+1,NewPosID.lastIndexOf(']'));
		
		if(ItemParent == NewItemParent && NewItemShortID > ItemShortID && ItemType == "block" && NewItemType == "container"){
			NewItemShortID--;
			NewPosID = NewItemParent+"["+NewItemShortID+"]";
		}//if
		
		var NewItemType = ItemType;
		var NewItemParent = NewPosID;
		var NewItemParentType = eval("HS_Structure"+NewItemParent+"['type']");

		JSCode = "Ind = 0;\n"+
		         "for(var Item in HS_Structure"+NewItemParent+"){\n"+
				 "if(!isNaN(Item)){\n"+
				 "Ind = Item;\n"+
			 	 "}//if\n"+
			 	 "}//for\n"+
			 	 "Ind++;\n"+
			 	 "HS_Structure"+NewItemParent+"[Ind] = ItemOfArray;\n"+
			 	 "HS_Structure"+NewItemParent+"[Ind]['parent'] = \""+NewItemParent+"\";\n"+
			  	 "if(NewItemType == 'cell')\n"+
			  	 "HS_Structure"+NewItemParent+"[Ind]['id'] = Ind;\n";

		eval(JSCode);
		}//else
		
        for(var Column in HS_Structure){
        	if(!isNaN(Column))
        		HS_RewriteParent("["+Column+"]");
        }//for
        
        HS_SelectedItem = NewPosID;

        HS_UnselectItem();
		HS_BuildPage();
		HS_PullOutput();

		HS_CheckSize();

		HS_InsertPos = "";
		HS_HideSuggestor();

	}//if
	else{
		HS_UnselectItem();
		HS_SelectItem(ItemID);
		HS_InsertPos = "";
		HS_HideSuggestor();
	}//else

}//HS_RepositionItem

function HS_RemoveItem(ItemID){
	HS_UnselectItem();
	var ItemShortID = ItemID.substring(ItemID.lastIndexOf('[')+1,ItemID.lastIndexOf(']'));
	var ItemOfArray = new Array();
	ItemOfArray = eval("HS_Structure"+ItemID);
	var ItemType = ItemOfArray['type'];
	var ItemParent = ItemOfArray['parent'];
	var ItemParentType = eval("HS_Structure"+ItemParent+"['type']");
	var NewStruct = new Array;
	var JSCode;
	if(ItemParent!= "" && ItemParentType != "column")
		i=0;
	else
		i=1;
	
	JSCode = "for(var Item in HS_Structure"+ItemParent+"){\n"+
			 "if(isNaN(Item)){\n"+
			 "NewStruct[Item] = HS_Structure"+ItemParent+"[Item];\n"+
			 "}//if\n"+
			 "else if(Item == ItemShortID){\n"+
			 "continue;\n"+
			 "}//else if\n"+
			 "else{\n"+
			 "NewStruct[i] = HS_Structure"+ItemParent+"[Item];\n"+
			 "if(ItemType == 'cell' || ItemType == 'column')\n"+
		 	 "NewStruct[i]['id'] = i;\n"+
			 "i++;\n"+
	   		 "}//else\n"+
		 	 "}//for\n"+
		 	 "HS_Structure"+ItemParent+"= new Array;"+
         	 "HS_Structure"+ItemParent+"= NewStruct;";
	eval(JSCode);
	
	for(var Column in HS_Structure){
		if(!isNaN(Column))
        HS_RewriteParent("["+Column+"]");
    }//for
	
	HS_BuildPage();
	HS_PullOutput();

	HS_CheckSize();

	HS_InsertPos = "";
	HS_HideSuggestor();	
}//HS_RemoveItem

function HS_RewriteParent(ItemID){
    var ItemOfArray = new Array;
	ItemOfArray = eval("HS_Structure"+ItemID);
	var ItemParent = ItemID.substring(0,ItemID.lastIndexOf("["));
    eval("HS_Structure"+ItemID+"['parent'] = \""+ ItemParent +"\"");
    for(var Item in ItemOfArray){
    	if(!isNaN(Item)){
        	HS_RewriteParent(ItemID+"["+Item+"]");
        }//if
    }//for
}//HS_RewriteParent

function HS_SavePage(){
	HS_JS2XML();
	var FormHandler = document.composer_structure;
	FormHandler.structure.value = HS_XML;
	FormHandler.pro.value = "save";
	FormHandler.submit();
}//HS_SavePage

function HS_PreviewPage(){
	HS_JS2XML();
	var FormHandler = document.composer_structure;
	FormHandler.structure.value = HS_XML;
	FormHandler.pro.value = "preview";
	FormHandler.submit();
}//HS_PreviewPage

function HS_JS2XML(){
	HS_XML = "<structure>";
	HS_BuildXML("['header']");	
	for(var Column in HS_Structure){
		if(!isNaN(Column))
		HS_BuildXML("["+Column+"]");
	}//for
	HS_BuildXML("['footer']");
	HS_XML+= "\n</structure>";
}//HS_JS2XML

function HS_BuildXML(ItemID){
	var ItemOfArray = new Array;
	ItemOfArray = eval("HS_Structure"+ItemID);
	HS_XML+="\n";
	var Depth = ItemID.split("]");
	for(var k=1;k<Depth.length;k++){
		HS_XML+="\t";
	}//for
	HS_XML+= "<"+ItemOfArray['type'];
	if(ItemOfArray['type']!='template' && ItemOfArray['type']!='header' && ItemOfArray['type']!='footer'){
		for(var ItemProperty in ItemOfArray){
			if(isNaN(ItemProperty) && ItemProperty!='type' && ItemProperty!='parent'){
				HS_XML+= " "+ItemProperty+"=\""+ ItemOfArray[ItemProperty] +"\"";
			}//if
		}//for
		HS_XML+= ">";
		for(var ItemContent in ItemOfArray){
		if(!isNaN(ItemContent)){
			HS_BuildXML(ItemID+"["+ItemContent+"]");
		}//if
		}//for
		HS_XML+="\n";
		for(var c=1;c<Depth.length;c++){
			HS_XML+="\t";
		}//for
		HS_XML+= "</"+ItemOfArray['type']+">";
	}//if
	else{
		HS_XML+= ">"+ItemOfArray['file']+"</"+ItemOfArray['type']+">";
	}//else
}//HS_BuildXML

function HS_CheckSize(){
	
	var FrameHeight = document.body.scrollHeight;
	parent.HS_ResizeFrame(FrameHeight,"composer_editor");
	
}//HS_CheckSize

function HS_EditFile(FileName,FieldName,FieldName2){
	
	var Properties  = document.properties_form;
	var xmlHttpReq = false;	
    var self = this;    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
       	self.xmlHttpReq = new XMLHttpRequest();
    }//if
    // IE
    else if (window.ActiveXObject) {
       	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }//else if
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
    	if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
    		var Contents = new Array();
    		Contents = self.xmlHttpReq.responseText.split("|||--|||");  		
	    	Properties[FieldName].value = Contents[0];
	    	if(FieldName2 && Contents[1] != "")
	    		Properties[FieldName2].value = Contents[1];
    	}//if
    }//function
    self.xmlHttpReq.send("section=composer&action=edit_template&theme="+Theme+"&file="+FileName);
	
}//HS_EditFile

function HS_SaveFile(FileName,FileContent){
	
	FileContent = escape(FileContent);
	var TempAr = FileContent.split("+");
	FileContent = TempAr.join("%2B");
	
	var self = this;    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
       	self.xmlHttpReq = new XMLHttpRequest();
    }//if
    // IE
    else if (window.ActiveXObject) {
       	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }//else if
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send("section=composer&action=save_template&theme="+Theme+"&file="+FileName+"&content="+FileContent);
	
}//HS_SaveFile

//-->