function S4() 
{
	sto = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	return sto
}

function newGUID()
{
	
	ko = new Date();
	ji = ko.getTime();
	Ans =ji+(S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4()).toUpperCase();
	return Ans
}

function pageeditor(cn)
{
	this.cn = cn;
	this.idname = '';
	
	this.items = new Array();
	this.codeview=0;

}
/*
prints to the debug layer if the debug is on.
*/
pageeditor.prototype.init = function(name)
{
	this.idname = name;
	this.items = new Array();
	this.codeview=0;
}
pageeditor.prototype.formedit = function()
{
	document.getElementById(this.idname+"CODE").value=this.rebuildXML();
}
pageeditor.prototype.togglecode = function()
{
	document.getElementById(this.idname+"CODE").value=this.rebuildXML();
	if(this.codeview==0)
	{
		show=1;
	}
	else
	{
		show=0;
	}
	lenth= this.items.length;

	for(o=0;o<lenth;o++)
	{
		if(show==1)
		{
			document.getElementById(this.items[o].guid+'Wrapper').style.display='none';
		}
		else
		{
			document.getElementById(this.items[o].guid+'Wrapper').style.display='';
			
		}
		
	}
		if(show==1)
		{
			document.getElementById(this.idname+'CODE').style.display='block';
				this.codeview =1;
		}
		else
		{
			document.getElementById(this.idname+'CODE').style.display='none';
				this.codeview =0;
			
		}	

	
}
pageeditor.prototype.getguidnr = function(guid)
{
	lenth= this.items.length;
	for(o=0;o<lenth;o++)
	{
		try
		{
			if(this.items[o].guid==guid)
			{
				return o;
			}
		}
		catch(e)
		{
			
		}
	}
	return -1;
}
pageeditor.prototype.updatevals = function(guid)
{
	guidnr = this.getguidnr(guid);
	if(guidnr!=-1)
	{
		try
		{
			type = this.items[guidnr].type;
			
			if(type=='text')
			{
				vals.title= document.getElementById(guid+'Title').value;
				vals.text=document.getElementById(guid+'Text').value;
			}
			else if(type=='commandbutton')
			{
				vals.title= document.getElementById(guid+'Title').value;
				vals.text=document.getElementById(guid+'Text').value;	
				vals.command=document.getElementById(guid+'Command').value;		
				vals.data=document.getElementById(guid+'CommandData').value;	
				vals.hardlink=document.getElementById(guid+'HardLink').checked;
				
			}
			
			this.items[guidnr].vals = vals;
		}
		catch(e) {


			
		}
	}
	
	
}
pageeditor.prototype.moveUpItem = function(guid)
{
	guidnra = this.getguidnr(guid);
	lenth=this.items.length;
	if(guidnra!=-1)
	{	
		if(guidnra>0)
		{
			

				olditem= this.items[guidnra-1];
				this.items[guidnra-1] = this.items[guidnra];
				this.items[guidnra] = olditem;
				
			
		}
	}
	this.reload();
}
pageeditor.prototype.moveDownItem = function(guid)
{
	guidnra = this.getguidnr(guid);
	lenth=this.items.length;
	if(guidnra!=-1)
	{	

		if(lenth-1>(guidnra+1));
		{
			
			olditem= this.items[guidnra+1];
			this.items[guidnra+1] = this.items[guidnra];
			this.items[guidnra] = olditem;
		}
	}
	this.reload();
}
pageeditor.prototype.removeItem = function(guid)
{
	guidnra = this.getguidnr(guid);
	lenth=this.items.length;
	if(guidnra!=-1)
	{	
		newitems = new Array();
		for(o=0;o<lenth;o++)
		{
			try
			{
				if(o!=guidnra)
				{
					this.updatevals(this.items[o].guid);		
					newitems[newitems.length]= this.items[o];
				}
				else
				{
					
				}
			}
			catch(e)
			{
				
			}
		}
		this.items = newitems;
		this.reload();
	}
	
	
}
pageeditor.prototype.registerGuid = function(guid,type)
{
	vals = new Array();

	if(type=='text')
	{
		vals.title= document.getElementById(guid+'Title').value;
		vals.text=document.getElementById(guid+'Text').value;
	}
	else if(type=='commandbutton')
	{
		vals.title= document.getElementById(guid+'Title').value;
		vals.text=document.getElementById(guid+'Text').value;	
		vals.command=document.getElementById(guid+'Command').value;		
		vals.data=document.getElementById(guid+'CommandData').value;	
		vals.hardlink=document.getElementById(guid+'HardLink').checked;
		
	}
	newitem = this.items.length;	
	this.items[newitem]= new Array();
	this.items[newitem].guid = guid;
	this.items[newitem].type = type;
	this.items[newitem].vals = vals;

	
	
}

pageeditor.prototype.addItem = function(type)
{
	newitem = this.items.length;
	this.items[newitem]= new Array();
	
	this.items[newitem].type =type;
	newvals = new Array();
	this.items[newitem].guid= newGUID();
	if(type=='text')
	{
		
		newvals.title= 'Title';
		newvals.text='Text';
	}
	else if(type=='commandbutton')
	{
		newvals.title= 'Link title';
		newvals.text='Link text';		
		newvals.command='page';		
		newvals.data=this.idname;	
		newvals.hardlink=false;	
		
		
	}

	this.items[newitem].vals=newvals;

	this.reload();
}

pageeditor.prototype.save = function()
{
	document.getElementById(this.idname+"CODE").value=this.rebuildXML();
	command="savepage";
	TANR++
	TA[TANR] = new TaroAJAX();
	TA[TANR].cn = 'TA['+TANR+']';
	TA[TANR].Sourcefile= basepath+'CMD/'+encodeURIComponent(command)+'/'+encodeURIComponent(this.idname);
	doc = 'xmldata='+encodeURIComponent(document.getElementById(this.idname+"CODE").value);
	TA[TANR].doctosend=doc;
	TA[TANR].onReadyresponsecommand = 'tarobly.out("PREVIEWpage'+this.idname+TANR+'",getresponse('+TANR+'));';
	
	TA[TANR].doPost();
}
pageeditor.prototype.reload = function()
{
	document.getElementById(this.idname+"CODE").value=this.rebuildXML();

	command="previewpage";
	TANR++
	TA[TANR] = new TaroAJAX();
	TA[TANR].cn = 'TA['+TANR+']';
	TA[TANR].Sourcefile= basepath+'CMD/'+encodeURIComponent(command)+'/'+encodeURIComponent(this.idname);

	doc = 'xmldata='+encodeURIComponent(document.getElementById(this.idname+"CODE").value);
	TA[TANR].doctosend=doc;
	TA[TANR].onReadyresponsecommand = 'tarobly.out("SAVEPAGE'+this.idname+TANR+'",getresponse('+TANR+'));';
	
	TA[TANR].doPost();
}

pageeditor.prototype.savedone = function(data)
{

}
pageeditor.prototype.rebuildXML = function()
{
	lenth= this.items.length;
	xml ='';
	for(o=0;o<lenth;o++)
	{
		try
		{
		this.updatevals(this.items[o].guid);
		
		if(this.items[o].type=='text')
		{
			xml+="<simpletext guid='"+this.items[o].guid+"'>\n<title><![CDATA["+this.items[o].vals.title+"]]></title>\n<text><![CDATA["+this.items[o].vals.text+"]]></text>\n</simpletext>\n\n";
		}
		else if(this.items[o].type=='commandbutton')
		{
			if(this.items[o].vals.hardlink)
			{
				hll="hardlink='yes'";
			}
			else
			{
				hll='';
			}
			xml+="<commandbutton guid='"+this.items[o].guid+"' "+hll+">\n<command>"+this.items[o].vals.command+"</command>\n<data>"+this.items[o].vals.data+"</data>\n<title><![CDATA["+this.items[o].vals.title+"]]></title>\n<text><![CDATA["+this.items[o].vals.text+"]]></text>\n</commandbutton>\n";
		}
		}
		catch(e)
		{
			
		}
	}
	return xml;
}

pageeditor = new pageeditor('pageeditor');