var BWflag = false;
var FITflag = false;
var HORflag = false;
var VERflag = false;
var Cropflag = false;
var Gridflag = false;
var ALflag = false;
var Zoomflag = false;
var IMGtruewidth;
var IMGtrueheight;
var CurrentSide = null;
var LastSide = 0;
var CropAreaStyle = null;
var ImageStyle = null;
var x0 = 0;
var y0 = 0;
var cropdx = 0;
var cropdy = 0;
var lastheight = 0

var Initialized = false;

function InitFilters()
{
	if(Initialized) return;
	Initialized = true;
	document.all.mpic.style.filter = "FlipH FlipV Gray";
	document.all.mpic.filters["Gray"].enabled = BWflag;
	document.all.mpic.filters["FlipH"].enabled = HORflag;
	document.all.mpic.filters["FlipV"].enabled = VERflag;
}

function flipBW()
{
	InitFilters();
	BWflag = !BWflag;

	document.all.mpic.filters["Gray"].enabled = BWflag;

//	if(BWflag) document.all.bw.src = "/pics/i_bw_on.gif";
//	else document.all.bw.src = "/pics/i_bw.gif";
}

function SetBG(color)
{
	e=document.getElementById("mpicbg");
    e.style.backgroundColor = color;
}

function FitImg()
{
FITflag = !FITflag;
if (FITflag)
{
e=document.getElementById("mpic");
IMGtruewidth = e.width;
IMGtrueheight = e.height;
}

var IE4 = (document.all);
		if (IE4)
	{
			if (FITflag)
			{
	browserwidth = document.body.clientWidth;
	browserheight= document.body.clientHeight;
	if (browserwidth < browserheight)
	{
		if (browserwidth < IMGtruewidth)
		{
			document.all.mpic.style.width = browserwidth*0.7;
		}
		else
		{
			document.all.mpic.style.height = IMGtrueheight*0.7;
		}
	    
	}
	else
		if (browserheight < IMGtrueheight)
		{
			document.all.mpic.style.height = browserheight*0.7;
		}
		else
		{
			document.all.mpic.style.width = IMGtruewidth*0.7;
		}
			}
			else
		{
			document.all.mpic.style.height = IMGtrueheight;
			document.all.mpic.style.width = IMGtruewidth;
		}
	}
}

function flipHOR()
{
	InitFilters();
	HORflag = !HORflag;
	document.all.mpic.filters["FlipH"].enabled = HORflag;
}

function flipVER()
{
	InitFilters();
	VERflag = !VERflag;
	document.all.mpic.filters["FlipV"].enabled = VERflag;
}

function flipCrop()
{
	Cropflag = !Cropflag;
	if(Cropflag) {
		e1=document.getElementById("cropt1");
		e2=document.getElementById("cropt2");
		m=document.getElementById("mpic");

		e1.style.width = m.width+16;
		if (lastheight > 0) e2.style.height = lastheight;
		else e2.style.height = m.height-10;
		e2.style.border = "1px dotted #000000";
		document.all.cropper.style.display = "";
	} else {
		document.all.cropper.style.display = "none";
	}
}

function AddClipping()
{
	x1 = document.getElementById("left").style.pixelWidth - 8; if(x1 < 0) x1 = 0;
	y1 = document.getElementById("top").style.pixelHeight - 8; if(y1 < 0) y1 = 0;
	x2 = document.getElementById("right").style.pixelWidth - 8; if(x2 < 0) x2 = 0;
	y2 = document.getElementById("bottom").style.pixelHeight - 8; if(y2 < 0) y2 = 0;

	s = '{CROP('+x1+','+y1+','+x2+','+y2+')}';

	document.REPLIER.Post.value += s;
	document.REPLIER.Post.focus();
}

function flipGrid()
{
	Gridflag = !Gridflag;
e1=document.getElementById("cropt1");
e2=document.getElementById("cropt2");
m=document.getElementById("mpic");

e1.style.width = m.width+16;
e2.style.height = m.height-8;

	if(Gridflag)
	{
		if(!Cropflag) flipCrop();
		document.all.grid.style.visibility = "visible";
	}
	else
	{
		document.all.grid.style.visibility = "hidden";
	}
}

function MouseDown(side)
{
	CurrentSide = side;
	ev = window.event;
	x0 = ev.x;
	y0 = ev.y;
	CropAreaStyle = document.getElementById("cropt2").style;
	ImageStyle = document.getElementById("mpic");
	NewAreaHeight = CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2);
//	window.status = 'NewAreaHeight: '+NewAreaHeight+' Crop H: '+CropAreaStyle.height+' ----- Dy: '+cropdy+' Side: '+side;
}

function MouseUp()
{
	CurrentSide = null;
	if (cropdy != 0)
	{
		CropAreaStyle = document.getElementById("cropt2").style;
		lastheight = CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2);
	}
	cropdy = 0;
//	cropdy = ImageStyle.height - CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2);
//	window.status = 'NewAreaHeight: '+NewAreaHeight+' Crop H: '+CropAreaStyle.height+' ----- Dy: '+cropdy;
}

function MouseMove()
{
	ev = window.event;
	if((CurrentSide == null) || (ev.button != 1)) return;
	dx = ev.x - x0;
	dy = ev.y - y0;

style = document.getElementById(CurrentSide).style;
// style = document.all[CurrentSide].style;
//	
	switch (CurrentSide)
	{
		case "top":
			if(style.pixelHeight >= 8) { 
			if (CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2) - dy > 35 && dy > 0)
				{
					style.pixelHeight += dy; cropdy += dy; CropAreaStyle.height = NewAreaHeight - cropdy; 
				}
				else if (CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2) < ImageStyle.height - 10 && dy < 0)
				{
					style.pixelHeight += dy; cropdy += dy; CropAreaStyle.height = NewAreaHeight - cropdy; 
				}
			}
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
//  window.status = 'NewAreaHeight: '+NewAreaHeight+' Crop H: '+CropAreaStyle.height+' ----- Dy: '+dy;
		break;
		case "bottom":
			if(style.pixelHeight >= 8) { 
			if (CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2) - dy > 35 && dy < 0)
				{
					style.pixelHeight -= dy; cropdy -= dy; CropAreaStyle.height = NewAreaHeight - cropdy; 
				}
				else if (CropAreaStyle.height.substr(0,CropAreaStyle.height.length-2) < ImageStyle.height - 10 && dy > 0)
				{
					style.pixelHeight -= dy; cropdy -= dy; CropAreaStyle.height = NewAreaHeight - cropdy; 
				}
			}
			if(style.pixelHeight < 8) style.pixelHeight = 8;
			else {x0 = ev.x; y0 = ev.y;}
// window.status = 'NewAreaHeight: '+NewAreaHeight+' Crop H: '+CropAreaStyle.height+' ----- Dy: '+dy;
		break;
		case "left":
			if(style.pixelWidth >= 8) style.pixelWidth += dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
		case "right":
			if(style.pixelWidth >= 8) style.pixelWidth -= dx;
			if(style.pixelWidth < 8) style.pixelWidth = 8;
			else {x0 = ev.x; y0 = ev.y;}
		break;
	}
}

function SetCrop(left, top, right, bottom)
{
	if(!Cropflag) flipCrop();

	document.getElementById("left").style.pixelWidth = left + 8;
	document.getElementById("right").style.pixelWidth = right + 8;
	document.getElementById("top").style.pixelHeight = top + 8;
	document.getElementById("bottom").style.pixelHeight = bottom + 8;
	document.getElementById("cropt2").style.height = document.getElementById("mpic").height - top - bottom - 16;

	window.scrollTo(0,0);
}

function shsub(item)
{
	sd = document;
	if (sd.all['rate'+item].style.display!=null)
	{
		if (sd.all['rate'+item].style.display == "none")
		{
			sd.all['rate'+item].style.display = "";
		}
		else
		{
			sd.all['rate'+item].style.display = "none";
		}
	}
}