ie4 = document.all;
ie = (document.all)? true:false;
nn4 = document.layers;
n4 = nn4;
ie5 = (ie && navigator.appVersion.indexOf('MSIE 5')>0)? true:false;
ie6 = (ie && navigator.appVersion.indexOf('MSIE 6')>0)? true:false;
ie7 = (ie && navigator.appVersion.indexOf('MSIE 7')>0)? true:false;
if(ie6)
	ie5 = true;
if(ie7)
    ie6 = true;

ver4 = ie4 || nn4;
currScoreElem = null;
var improc = ver4 || ie5;

function elem(objname)
{
	if(nn4 != null)
		return document.layers[objname];
	else
		return obj(objname);
}

function show(object, mouse) {
	if(!object || !mouse)
		return;	
	var p = obj(object);
	var left = mouse.clientX - p.offsetWidth - 10;
	if(left < 0)
		left = mouse.clientX + 10;
	p.style.visibility = 'hidden';
	p.style.left = left + window.document.body.scrollLeft;
	p.style.top = mouse.clientY - 5 + window.document.body.scrollTop;
	p.style.visibility = 'visible';	
}
 
function hide(object) {
	if(!object)
		return;
	obj(object).style.visibility = 'hidden';
}


function checkComment(name, email, comm, commScoreAllowed, useForms){	
	emptyBoxes = new String();
	if(!isText(name))
		emptyBoxes += "Name ";
	if(!isEmail(email))
		emptyBoxes += "E-mail ";
	if(!isText(comm))
		emptyBoxes += "Comment ";	
	
	if(emptyBoxes != ""){
		alert("Fill in the following fields: " + emptyBoxes);
		return;
	}
	var form1 = elem("form1");	
	if(useForms == 'yes')
		form1.submit();
	else
		doUpdateComment(commScoreAllowed);
}


function doUpdateComment(commScoreAllowed){
	form1.submit1.disabled = true;
	floatMessage("please wait...", tooltip);
	window.setTimeout("updateComment("+commScoreAllowed+")", 10);	
}

function charCounter(){
	var limtext = elem("limtext");
	var form1 = elem("form1");
	var limbox = elem("limbox");
	var textarea1 = elem("textarea1");
	limtext.style.visibility = 'visible';
	limbox.style.visibility = 'visible';
	var txt = textarea1.value;
	l = txt.length;	
	limbox.innerHTML = l;
}

function updateComment(commScoreAllowed){
	try{
		errCode = submitCommentToServer(commScoreAllowed);
	}catch(e){
		msg = "Error occured. If you have this message often\n " +
				"please consider changing your scoring/commenting method \n " +
				" from the 'My PhotoBlink' page ";
		unfloatMessage(tooltip);
		alert(msg);
		return;	
	}
	unfloatMessage(tooltip);
	//limtext.style.visibility = 'hidden';
	limbox.style.visibility = 'hidden';
	if(errCode){
		alert("commenting failed");
		form1.submit1.disabled = false;
		return;
	}
	floatMessage("Thank you for participation", tooltip);
	window.setTimeout("unfloatMessage(tooltip)", 1000);
	addCommentDiv();
	form1.textarea1.value = "";
	if(commScoreAllowed == 1)
		updateVoteControls();
	
}


function addCommentDiv(){
	comdiv = document.createElement("div");
	//comdiv.id = "d3";
	//comdiv.style.position = "absolute";
	comdiv.innerHTML = form1.textarea1.value;
	comdiv.style.zIndex = 30;
	comdiv.style.background = 'white';
	comdiv.style.visibility = 'visible';
	var commentEnder = elem("commentEnder");
	commentPlacer.insertBefore(comdiv, commentEnder);
	form1.submit1.disabled = false;
	crop = form1.cropString.value;
	if(crop != ""){
		filename = form1.filename.value;
		w = form1.filewidth.value;
		h = form1.fileheight.value;
		comdiv = document.createElement("a");
		commentPlacer.insertBefore(comdiv, commentEnder);
		comdiv.innerHTML = 'suggested crop >>';
		cropRef = "cropdisplay.asp?photo=" + filename;
		cropRef += "&cropString=" + crop;
		cropRef += "&w=" + w + "&h=" + h;
		comdiv.href= cropRef;
	}
	comdiv = document.createElement("p");
	commentPlacer.insertBefore(comdiv, commentEnder);
}

function xmldom(){
	var source = null;
	try{
		source = new ActiveXObject("Microsoft.XMLDOM");
		source.async = false;
	}catch(e){
		source = document.implementation.createDocument("","doc",null);
		source.async = false;
	}
	return source;
}


function submitCommentToServer(commScoreAllowed){
//debugger
	var form1 = elem("form1");
	var textarea1 = elem("textarea1");
	pageRef = "commentReqNoUI.asp";
	var comm = escape(textarea1.value);
	pageRef += "?comm="+comm;
	subscribeFlag = form1.subscrCheck.status ? "on" : "";

	var anonFlag = "";
	if(form1.anonCheck != null)
		anonFlag = form1.anonCheck.status ? "on" : "";	
	
	pageRef += "&subscr="+subscribeFlag;
	pageRef += "&crop="+form1.cropString.value;
	pageRef += "&imid="+form1.text3.value;
	pageRef += "&anon="+anonFlag;
	
	//source = new ActiveXObject("Microsoft.XMLDOM");
	source = xmldom();
	//source.async = false;		//Don't forget to set this
//alert(pageRef);
//return;	
	source.load(pageRef)
	
	var status, errstat, syserr, conerr, score, rank;

// 	data = source.selectSingleNode("data");	
// 	vals = data.childNodes;
// 	i = 0;
// 	status = vals(i++).text;
// 	errstat = vals(i++).text;
// 	syserr = vals(i++).text;
// 	conerr = vals(i++).text;
// 	errmess = vals(i++).text;
// 	commid = vals(i++).text;
	
	var data = source.getElementsByTagName("data");	
	vals = data[0].childNodes;
	i = 0;
	status = vals[i++].firstChild.nodeValue;	
	errstat = vals[i++].firstChild.nodeValue;
	syserr = vals[i++].firstChild.nodeValue;
	conerr = vals[i++].firstChild.nodeValue;
	errmess = vals[i++].firstChild.nodeValue;
	commid = vals[i++].firstChild.nodeValue;
	
	if(commScoreAllowed)
		elem("voteForm").commentID.value = commid;
	source = null;	
	errCode = status | errstat | syserr | conerr;
	return errCode;
}

function checkVote(myForm, vote){
	if(!isNum(vote))
		alert("Input a number");
	else
		myForm.submit();
}


function subscribe(email, status, hidden){
	var subscribeForm = elem("subscribeForm");
	if(!isEmail(email))
		alert("provide your e-mail");
	else{
		subscribeForm.subscribeFlag.value = status ? 1 : 0;
		subscribeForm.submit();
	}
}




function onRemoveComment(commID){
	if(!confirm("Are you sure you want to remove this comment?")){
		return;
	}
	//alert("removing...");
	removeCommForm.commID.value = commID;
	removeCommForm.submit();
}

function onRemoveImage(imID){
	if(!confirm("Are you sure you want to remove this image?")){
		return;
	}
	//alert("removing...");
	removeImageForm.imageID.value = imID;
	removeImageForm.submit();
}





////////// magnifier code ///////////////////

div = new Array();
dragActive = false
xoffset=100;
yoffset=50;
diameter=180;
radius=120;
firstTime = true;
currentLense = 0
futureLense = 1
lenses = new Array();
lenses[0] = "lenson.gif"
lenses[1] = "lensoff.gif"



document.onmousedown = mouseDown
document.onmousemove = mouseMove 
document.onmouseup = mouseUp
if (n4) {document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)}

var divsInitialized = false;

function initLayers(n) {
	for (var i = 1; i <= n; i++) {
		if (n4) {
			div[i] = eval("document.div" + i);
			div[i].x = div[i].left;
			div[i].y = div[i].top;
			div[i].w = div[i].clip.width;
			div[i].h = div[i].clip.height;
		}
		if (ie4) {
			div[i] = eval("div" + i + ".style");
			div[i].x = div[i].pixelLeft;
			div[i].y = div[i].pixelTop;
			div[i].w = div[i].pixelWidth;
			div[i].h = div[i].pixelHeight;
		}
		if (ie5) {
			div[i] = eval("div" + i);
			div[i].x = offset(div[i], "offsetLeft");
			div[i].y = offset(div[i], "offsetTop") / 2;
			div[i].w = div[i].offsetWidth;
			div[i].h = div[i].offsetHeight;
		}
	}
	divsInitialized = true;
}

function offset(o, offsetname){
	var p = o;
	var val = 0;
	while(p != null){
		val += eval("p." + offsetname);
		p = p.parentElement;
	}
	return val;
}


function mouseDown(e) {
	if(!divsInitialized || !improc)
		return true;
	dragActive = mouseOver(e);
	dragClickX = x - div[5].x;
	dragClickY = y - div[5].y;
	if (dragActive) {
		x = eventXcoord(e);
		y = eventYcoord(e);
		return false
	}
	else 
		return true
}

function mouseMove(e) {
	if (!dragActive) {
		return true
	}

	//<ssser>
	var p = div[1];
	var p = div[1];
	hshift = offset(p, "offsetLeft");
	vshift = offset(p, "offsetTop")/2;	
	//hshift = p.offsetLeft;
	//vshift = p.offsetTop;
	xoffset = hshift * 2;
	yoffset = vshift * 2;
	//</ssser>
	
	x = eventXcoord(e);
	y = eventYcoord(e);
	div[5].x = x - dragClickX;
	div[5].y = y - dragClickY;
	shiftTo(div[5],div[5].x,div[5].y)
	
	clipBy(div[2],	radius + div[5].y * 2 - yoffset, radius + diameter + div[5].x * 2 - xoffset,
					radius + diameter + div[5].y * 2 - yoffset, radius + div[5].x * 2 - xoffset);
	shiftTo(div[2],(xoffset-(radius+div[5].x)),(yoffset-(radius+div[5].y)));

	shiftTo(div[6],div[5].x,div[5].y);
	return false
}

function mouseUp(e) {
	x = eventXcoord(e);
	y = eventYcoord(e);
	dragActive = false
	return true
}

function mouseOver(e) {
	if(!improc)
		return false;
	var hit = false;
	x = eventXcoord(e);
	y = eventYcoord(e);
	if (x>=div[5].x && x<=div[5].x+div[5].w && y>=div[5].y && y<=div[5].y+div[5].h) {
			hit = true;
	}
	return hit;
}


function shiftTo(object,x,y) {
	object.x = x;
	object.y = y;
	object.style.left = object.x;
	object.style.top = object.y;
}

function clipBy(object,t,r,b,l) {
	if (n4) {
	object.clip.top = t;
	object.clip.right = r;
	object.clip.bottom = b;
	object.clip.left = l;
	}
	if (ie4) {
	object.clip = "rect(" + t + "px "+ r + "px " + b + "px " + l + "px)";
	}
	if (ie5) {
	object.style.clip = "rect(" + t + "px "+ r + "px " + b + "px " + l + "px)";
	}
	}

function hideObject(object) {
	if (n4) object.visibility = "hide";
	if (ie4) object.visibility = "hidden";
	if (ie5) object.style.visibility = "hidden";
	}

function eventXcoord(e) {
	if (n4) { var x = e.pageX }
	if (ie) { var x = event.x + document.body.scrollLeft }
	return x;
	}

function eventYcoord(e) {
	if (n4) { var y = e.pageY }
	if (ie) { var y = event.y+document.body.scrollTop }
	return y;
}

function lensIcon_onmouseover() {
	lensIcon.src = "design/" + lenses[currentLense];	
}

function lensIcon_onmouseout() {
	lensIcon.src = "design/" + lenses[futureLense];
}

var greyscale = false;

function toggleGreyScale(){
	greyscale = !greyscale;
	elem("imageview").style.filter = "Gray";
	elem("imageview").filters["Gray"].enabled = greyscale;
}


function toggleLens(){
	currentLense = currentLense == 0 ? 1 : 0;
	futureLense = futureLense == 0 ? 1 : 0;

	if(div5.style.visibility == "visible"){
		hideObject(div2);
		hideObject(div5);
		hideObject(div6);
		return;
	}
	div2.style.visibility = "visible";	
	div5.style.visibility = "visible";
	div6.style.visibility = "visible";
	if(!firstTime){
		return;
	}
	firstTime = false;
	//<ssser>
	var p = div[1];
	hshift = offset(p, "offsetLeft");
	vshift = offset(p, "offsetTop") / 2;		
	//hshift = p.offsetLeft;
	//vshift = p.offsetTop;	
	width = p.offsetWidth;
	xoffset = hshift * 2;
	yoffset = vshift * 2;
	//</ssser>
	
	div[5].x = hshift + width/2 - radius;
	div[5].y = vshift;

	shiftTo(div[5],div[5].x,div[5].y)
	
	clipBy(div[2],	radius + div[5].y * 2 - yoffset, radius + diameter + div[5].x * 2 - xoffset,
					radius + diameter + div[5].y * 2 - yoffset, radius + div[5].x * 2 - xoffset);
	shiftTo(div[2],(xoffset-(radius+div[5].x)),(yoffset-(radius+div[5].y)));

	shiftTo(div[6],div[5].x,div[5].y);
	return false
}
