
function RGB2Hex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}
function ChangeHex(Num)
{
	var E = document.getElementById('Hex' + Num);
	var Red = document.getElementById('Red' + Num);
	Red.value = FixRGBValue(Red.value, 255, 0);
	var Green = document.getElementById('Green' + Num);
	Green.value = FixRGBValue(Green.value, 255, 0);
	var Blue = document.getElementById('Blue' + Num);
	Blue.value = FixRGBValue(Blue.value, 255, 0);
	var Hex = '#' + RGB2Hex(Red.value, Green.value, Blue.value);
	E.style.background = Hex;
	E.value = Hex;
	var E2 = document.getElementById('SimpleEditMes');
	E2.style.visibility = 'hidden';
}
function FixRGBValue(Value, Check1, Check2)
{
	if(Value > Check1) { Value = Check1; }
	else if(Value < Check2) { Value = Check2; }
	return Value;
}
function ChangeIntensity(Num)
{
	var I = document.getElementById('Intensity' + Num);
	I.value = FixRGBValue(I.value, 100, 0);
}
function ChangeRGBs(Num)
{
	var E = document.getElementById('Hex' + Num);
	var Red = document.getElementById('Red' + Num);
	Red.value = Hex2Red(E.value);
	var Green = document.getElementById('Green' + Num);
	Green.value = Hex2Green(E.value);
	var Blue = document.getElementById('Blue' + Num);
	Blue.value = Hex2Blue(E.value);
	E.style.background = E.value;
	var E2 = document.getElementById('SimpleEditMes');
	E2.style.visibility = 'hidden';
}
function Hex2Red(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function Hex2Green(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function Hex2Blue(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

function ChangeCSS()
{
	var CSS = document.getElementById('DefaultCSS');
	var Value = CSS.value;
	//Primary color
	var Red1 = document.getElementById('Red1');
	var Red = parseInt(FixRGBValue(Red1.value, 245, 10));
	var Green1 = document.getElementById('Green1');
	var Green = parseInt(FixRGBValue(Green1.value, 245, 10));
	var Blue1 = document.getElementById('Blue1');
	var Blue = parseInt(FixRGBValue(Blue1.value, 245, 10));
	var Intensity1 = document.getElementById('Intensity1');
	var Intensity = parseInt(FixRGBValue(Intensity1.value, 100, 10));
	
	var IntNum = Math.round(Intensity/100*255);
	Value = Value.replace(/#C8C8C8/g, '#' + RGB2Hex(Red+(IntNum-30), Green+(IntNum-30), Blue+(IntNum-30)).toString()); //ContentTabInfo background
	Value = Value.replace(/#C2C8CE/g, '#' + RGB2Hex(Red+(IntNum-40), Green+(IntNum-40), Blue+(IntNum-40)).toString()); //UnderInfoContent background
	Value = Value.replace(/#CBDCEE/g, '#' + RGB2Hex(Red, Green, Blue).toString()); //ContentBox background
	Value = Value.replace(/#2666A1/g, '#' + RGB2Hex(Red-(IntNum-25), Green-(IntNum-25), Blue-(IntNum-25)).toString()); //Body background
	Value = Value.replace(/#357EC1/g, '#' + RGB2Hex(Red-(IntNum-16), Green-(IntNum-16), Blue-(IntNum-16)).toString()); //Title background
	Value = Value.replace(/#155998/g, '#' + RGB2Hex(Red-(IntNum-10), Green-(IntNum-10), Blue-(IntNum-10)).toString()); //Borders
	Value = Value.replace(/#8F8F8F/g, '#' + RGB2Hex(Red-IntNum, Green-IntNum, Blue-IntNum).toString()); //Title border
	
	//Secondary color
	var Red2 = document.getElementById('Red2');
	Red = parseInt(FixRGBValue(Red2.value, 245, 10));
	var Green2 = document.getElementById('Green2');
	Green = parseInt(FixRGBValue(Green2.value, 245, 10));
	var Blue2 = document.getElementById('Blue2');
	Blue = parseInt(FixRGBValue(Blue2.value, 245, 10));
	var Intensity2 = document.getElementById('Intensity2');
	Intensity = parseInt(FixRGBValue(Intensity2.value, 100, 10));
	
	IntNum = Math.round(Intensity/100*255);
	Value = Value.replace(/#5F5F5C/g, '#' + RGB2Hex(Red+(IntNum-40), Green+(IntNum-40), Blue+(IntNum-40))); //Input background
	Value = Value.replace(/#DFE9F4/g, '#' + RGB2Hex(Red, Green, Blue)); //InnerBackground background
	Value = Value.replace(/#7AA0C6/g, '#' + RGB2Hex(Red-(IntNum-40), Green-(IntNum-40), Blue-(IntNum-40))); //Submit background over
	Value = Value.replace(/#9C9C9C/g, '#' + RGB2Hex(Red-(IntNum-30), Green-(IntNum-30), Blue-(IntNum-30))); //Submit background
	Value = Value.replace(/#4A410C/g, '#' + RGB2Hex(Red-(IntNum-10), Green-(IntNum-10), Blue-(IntNum-10))); //Submit border
	Value = Value.replace(/#C2C8CE/g, '#' + RGB2Hex(Red-(IntNum-25), Green-(IntNum-25), Blue-(IntNum-25))); //Top menu button background
	Value = Value.replace(/#CBDCEE/g, '#' + RGB2Hex(Red-(IntNum-20), Green-(IntNum-20), Blue-(IntNum-20))); //Top menu button background
	Value = Value.replace(/#A8A8A3/g, '#' + RGB2Hex(Red-(IntNum-15), Green-(IntNum-15), Blue-(IntNum-15))); //Top menu button background over
	Value = Value.replace(/#3A3209/g, '#' + RGB2Hex(Red-IntNum, Green-IntNum, Blue-IntNum)); //Top menu button border
	
	//Extra color
	var Red3 = document.getElementById('Red3');
	Red = parseInt(FixRGBValue(Red3.value, 245, 10));
	var Green3 = document.getElementById('Green3');
	Green = parseInt(FixRGBValue(Green3.value, 245, 10));
	var Blue3 = document.getElementById('Blue3');
	Blue = parseInt(FixRGBValue(Blue3.value, 245, 10));
	var Intensity3 = document.getElementById('Intensity3');
	Intensity = parseInt(FixRGBValue(Intensity3.value, 100, 10));
	
	IntNum = Math.round(Intensity/100*255);
	Value = Value.replace(/#284386/g, '#' + RGB2Hex(Red+(IntNum-40), Green+(IntNum-40), Blue+(IntNum-40))); //Link
	Value = Value.replace(/#1C1803/g, '#' + RGB2Hex(Red, Green, Blue)); //Main text
	Value = Value.replace(/#203A79/g, '#' + RGB2Hex(Red-(IntNum-35), Green-(IntNum-35), Blue-(IntNum-35))); //Link visited
	Value = Value.replace(/#F3EEDA/g, '#' + RGB2Hex(Red-IntNum, Green-IntNum, Blue-IntNum)); //Title text
	
	var OldCSS = document.getElementById('CSS');
	OldCSS.value = Value;
	var E = document.getElementById('SimpleEditMes');
	E.style.visibility = 'visible';
	var E = document.getElementById('UpdateSimpleCSS');
	E.style.visibility = 'visible';
}

// Drag and drop

var Drag;
var LastC;
var LastR;
function StartDrag(E) {
	if(!E){ var E = window.event };
	var T = E.target?E.target:E.srcElement;
	if(T.className != 'Title' || !T.id) { return };
	CloseAllBoxes();
	var OldBox = T.parentNode.parentNode;
	var Box = OldBox.cloneNode(true);
	var Table = document.getElementById('EditBoxesTable');
	Table.style.height = GetHeight(Table) + GetHeight(OldBox)+25 + 'px';
	var P = document.getElementById('editBoxes');
	P.appendChild(Box);
	P.style.height = GetHeight(P) + 'px';
	var Top = OldBox.offsetTop;
	var Left = OldBox.offsetLeft;
	var Height = GetHeight(OldBox);
	OldBox.id = 'TempDragBox';
	OldBox.style.borderStyle = 'dashed';
	OldBox.style.borderWidth = '2px';
	OldBox.innerHTML = '';
	OldBox.style.height = Height + 'px';
	Box.style.width = GetWidth(OldBox) + 'px';
	Box.style.top = Top - Box.offsetTop + 'px';
	Box.style.left = Left - Box.offsetLeft + 10 + 'px';
	offsetX = E.clientX + (Left - Box.offsetLeft + 10);
	offsetY = E.clientY + (Top - Box.offsetTop);
	coordX = parseInt(Box.style.left);
	coordY = parseInt(Box.style.top);
	Box.style.zIndex += 30;
	LastR = null;
	LastC = null;
	Drag = Box;
	document.onmousemove = DragDiv;
	return false;
}
function DragDiv(E) {
	if(!Drag) { return };
	if(!E) { var E = window.event };

	var Box = Drag;
	var ID = parseInt(Box.id.substring(7,Box.id.toString().length+1));
	var Content = document.getElementById('BoxContent' + ID);
	if(Content && Content.className == 'UnderInfo') 
	{
		Content.className = 'UnderInfoHidden';
		var Button = document.getElementById('BoxContentButton' + ID);
		var Del = document.getElementById('BoxContentDel' + ID);
		if(Del) { Del.className = 'UnderInfoHidden'; }
		Button.value = '+';
	}
	
	var oLeft = E.clientX-offsetX;
	var oTop = E.clientY-offsetY;
	var Left = coordX+oLeft;
	var Top = coordY+oTop;
	Box.style.left = Left + 'px';
	Box.style.top = Top + 'px';
	
	var NewC;
	var NewR;

	var ID = parseInt(Box.id.substring(7,Box.id.toString().length+1));
	var C = parseInt(document.getElementById('Column' + ID).value);
	var R = parseInt(document.getElementById('Order' + ID).value);
	if(!LastC || !LastR) { LastC = C; LastR = R; }
	
	var AddX = Math.round(Math.abs(oLeft)/GetWidth(Box));
	var AddY = Math.round(Math.abs(oTop)/GetHeight(Box));
	if(oLeft > 0) { NewC = AddX + C; }
	else if(oLeft < 0) { NewC = C - AddX; }
	if(oTop > 0) { NewR = AddY + R; }
	else if(oTop < 0) { NewR = R - AddY; }
	if(NewC && !NewR) { NewR = R; }
	if(NewR && !NewC) { NewC = C; }

	if(NewC && NewR && LastC && LastR && (LastC != NewC || LastR != NewR))
	{
		var TB = document.getElementById('TempDragBox');
		if(TB)
		{
			var P = TB.parentNode;
			P.removeChild(TB);
		}
		if(NewC && NewR)
		{
			LastC = NewC;
			LastR = NewR;
			
			RebuildColumn('<div id="TempDragBox" class="ContentBox" style="border-width: 2px; border-style: dashed; margin: 0px;"></div>');
			document.getElementById('TempDragBox').style.height = GetHeight(Box) + 'px';
		}
	}
	return false;
}
function StopDrag() {
	if(Drag)
	{
		var Box = Drag;
		Drag = false;
		Box.style.zIndex -= 30;
		var ID = parseInt(Box.id.substring(7,Box.id.toString().length+1));
		var C = parseInt(document.getElementById('Column' + ID).value);
		var R = parseInt(document.getElementById('Order' + ID).value);
		var Owner = parseInt(document.getElementById('Owner' + ID).value);
		var Group = parseInt(document.getElementById('Group' + ID).value);
		var Table = document.getElementById('EditBoxesTable');
		var Max = Math.round(GetHeight(Table)/GetHeight(Box));
		var P = Box.parentNode;
		P.style.height = 'auto';

		if(LastR && LastR > Max + 1)
		{
			return ajaxQuery('GET', 'http://youricy.com/ajax.php?Update=EditBoxes&C=Disable&ID=' + ID + '&OwnerID=' + Owner + '&Group=' + Group, setEditBoxHandler);
		}
		if(LastC && LastR) { return ajaxQuery('GET', 'http://youricy.com/ajax.php?Update=EditBoxes&C=Position&ID=' + ID + '&OwnerID=' + Owner + '&Group=' + Group + '&Column=' + LastC + '&Order=' + LastR, setEditBoxHandler); }
		else
		{
			LastC = C;
			LastR = R;
			RebuildColumn('<div id="' + Box.id + '" class="ContentBox" style="position: relative; margin: 0px;">' + Box.innerHTML + '</div>');
			P.removeChild(Box);
			Table.style.height = 'auto';
			var TB = document.getElementById('TempDragBox');
			if(TB)
			{
				var P = TB.parentNode;
				P.removeChild(TB);
			}
			CloseAllBoxes(1);
		}
	}
	Drag = false;
}
function GetHeight(Obj)
{
	var Height;
	if(Obj.offsetHeight) { Height = Obj.offsetHeight; }
	else if(Obj.style.pixelHeight) { Height = Obj.style.pixelHeight; }
	return Height;
}
function GetWidth(Obj)
{
	var Width;
	if(Obj.offsetWidth) { Width = Obj.offsetWidth; }
	else if(Obj.style.pixelWidth) { Width = Obj.style.pixelWidth; }
	return Width;
}
function ShowHideBoxContents(ID)
{
	var Box = document.getElementById('BoxContent' + ID);
	var Button = document.getElementById('BoxContentButton' + ID);
	var Del = document.getElementById('BoxContentDel' + ID);
	if(Button.value == '+') { Button.value='-'; }
	else { Button.value='+'; }
	if(Del) { ChangeUnderInfo(Del); }
	ChangeUnderInfo(Box);
	return false;
}
function EnableContentBox(ID, Owner, Group)
{
	ajaxQuery('GET', 'http://youricy.com/ajax.php?Update=EditBoxes&C=Enable&ID=' + ID + '&OwnerID=' + Owner + '&Group=' + Group, setEditBoxHandler);
	return false;
}
function CloseAllBoxes(M)
{
	CloseColumnBoxes(1,M);
	CloseColumnBoxes(2,M);
	CloseColumnBoxes(3,M);
}
function CloseColumnBoxes(C,M)
{
	var Column = document.getElementById('C' + C);
	var Children = Column.childNodes;
	for(var I = 0; I < Children.length; I++)
	{
		if(Children[I].className == 'ContentBox' && Children[I].id != 'TempDragBox')
		{
			if(M) { Children[I].style.margin = '10px 8px 10px 8px'; }
			else { Children[I].style.margin = '0px'; }
			var ID = parseInt(Children[I].id.substring(7,Children[I].id.toString().length+1));
			var Content = document.getElementById('BoxContent' + ID);
			if(Content && Content.className == 'UnderInfo') 
			{
				Content.className = 'UnderInfoHidden';
				var Button = document.getElementById('BoxContentButton' + ID);
				var Del = document.getElementById('BoxContentDel' + ID);
				if(Del) { Del.className = 'UnderInfoHidden'; }
				Button.value = '+';
			}
		}
	};
}
function RebuildColumn(HTML)
{
	var Column = document.getElementById('C' + LastC);
	var Children = Column.childNodes;
	var NewHTML = '';
	var X = 0;
	var Found = 0;
	for(var I = 0; I < Children.length; I++)
	{
		if(Children[I].className == 'ContentBox')
		{
			X++;
			if(X == LastR)
			{
				NewHTML = NewHTML + HTML + '</div>';
				Found = 1;
				X++;
			}
			if(Children[I].id != 'TempDragBox')
			{
				var ID = parseInt(Children[I].id.substring(7,Children[I].id.toString().length+1));
				document.getElementById('Order' + ID).value = X;
			}
			NewHTML = NewHTML + '<div id="' + Children[I].id + '" class="ContentBox" style="position: relative; margin: 0px;" >' + Children[I].innerHTML + '</div>';
		}
	};
	if(!Found) { NewHTML = NewHTML + HTML + '</div>'; }
	
	Column.innerHTML = NewHTML;
}