// My-Notepad
// By Mark Willis.co.uk

// My-Notepad.net
// By MLWDesign.co.uk


function switchBox(bid){
switch(bid)
{
case 1: 
document.getElementById('login-box').style.display = "block";
document.getElementById('signup-box').style.display = "none";
break;
case 2:
document.getElementById('login-box').style.display = "none";
document.getElementById('signup-box').style.display = "block";
break;
}
return false;
}

function changeForm(fid, pole)
{
switch(pole)
{
case 0: 
document.getElementById(fid).className = "input-dark"; break;
case 1: 
document.getElementById(fid).className = "input-light"; break;
}}

/* */
function focusThis(trigger, target){
if(trigger == 1){
document.getElementById(target).focus();}
}

function removeBlock(goDo){
if(goDo == 0){
setTimeout("removeBlock(1)", 3000);}else if(goDo == 1){
killBlock(50);
}
}

function killBlock(height){
document.getElementById('postback').style.height = height;
if(height > 5){
var newHeight = height - 5;
setTimeout("killBlock(" + newHeight + ")", 10);}else if(height == 5){
document.getElementById('postback').style.display = "none";}
}

function disableButton(){
document.getElementById('submitbutton').disabled = true;}

///////////////////////////////////////////////////
//      mFloat Interface by MarkWillis.co.uk     //
/*************************************************/
var mFloatRoot = 'http://www.my-notepad.net/inc/floater/';
var mFloatViewportWidth;
var mFloatViewportHeight;
var MouseX = 0; var MouseY = 0;
function TrackingMousePosition(e)
{
	e = e || window.event;
	if (e.pageX)
	{
		MouseX = e.pageX;
		MouseY = e.pageY;
		scanViewport();
		if(MouseX + 230 > mFloatViewportWidth){MouseX = MouseX - 200;}
		
		if(document.getElementById('mFloatBlock').style.display == "block")
		{
			//document.getElementById('mFloatBlock').style.left = (MouseX + 10) + "px";
			//document.getElementById('mFloatBlock').style.top = (MouseY + 25) + "px";
		}
		if(document.getElementById('mwBubble').style.display == "block")
		{
			document.getElementById('mwBubble').style.left = (MouseX + 10) + "px";
			document.getElementById('mwBubble').style.top = (MouseY + 10) + "px";
		}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
		scanViewport();
		if(x + 230 > mFloatViewportWidth){x = x - 200;}
		
		if(document.getElementById('mFloatBlock').style.display == "block")
		{
			//document.getElementById('mFloatBlock').style.left = (x + 10) + "px";
			//document.getElementById('mFloatBlock').style.top = (y + 25) + "px";
		}
		if(document.getElementById('mwBubble').style.display == "block")
		{
			document.getElementById('mwBubble').style.left = (x + 10) + "px";
			document.getElementById('mwBubble').style.top = (y + 10) + "px";
		}
	}
}
window.onmouseover = TrackingMousePosition;
window.onmousemove = TrackingMousePosition;
// 'normal' browsers 
function scanViewport()
{
if (typeof window.innerWidth != 'undefined')
{
	mFloatViewportWidth = window.innerWidth;
	mFloatViewportHeight = window.innerHeight;
}
// IE6
else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
{
	mFloatViewportWidth = document.documentElement.clientWidth;
	mFloatViewportHeight = document.documentElement.clientHeight;
}
// even older IE :O
else
{
	mFloatViewportWidth = document.getElementsByTagName('body')[0].clientWidth;
	mFloatViewportHeight = document.getElementsByTagName('body')[0].clientHeight;
}
}
scanViewport();

function mFloatOpen(useMouse, useY, command, query)
{
	// useMouse: set to 1 if you want to have Ian popup at the mouse's location (centered), set to 0 if you want manual Y pos
	// useY: manual y location of Ian popup (vertical position)
	var x = 0; var y = 0;
	var oY = useY;
	if(MouseX)
	{
		x = MouseX;
		
		if(useMouse == 1){
		y = MouseY;
		}else{
		scroll(0,(oY - 10));
		y = oY;
		}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		if(useMouse == 1){
		y = window.event.clientY+document.documentElement.scrollTop;
		}else{
		scroll(0,(oY - 10));
		y = oY;
		}
	}
	document.getElementById('mFloatBlock').style.left = ((mFloatViewportWidth / 2) - 300) + "px";
	document.getElementById('mFloatBlock').style.top = (y + 10) + "px";
	document.getElementById('mFloatBlock').innerHTML = '<div class="mFloat-inner" id="mFloatInner"><div class="mFloat-close"><a href="#" onclick="return mFloatClose();"><!-- close --></a></div><div class="mFloat-main"><h3>loading...</h3></div></div>';
	document.getElementById('mFloatBlock').className = 'mFloatBlock-on';
	document.getElementById('mFloatBlock').style.display = "block";
	var d = new Date();
	var tsec = d.getSeconds();
	var ajaxRequest;
	try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
	ajaxRequest.onreadystatechange = function()
	{
	if(ajaxRequest.readyState == 4){
		document.getElementById('mFloatBlock').innerHTML = ajaxRequest.responseText;
	}
	}
	var addQuery = query.replace(/amp/g, '&');
	var queryString = mFloatRoot + command + '?time=' + tsec + addQuery;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
	return false;
}
function mFloatClose()
{
	document.getElementById('mFloatBlock').innerHTML = ''; document.getElementById('mFloatBlock').className = 'aianBlock-off'; document.getElementById('mFloatBlock').style.display = "none";
	return false;
}

function openAddEnt()
{
	
	if(document.getElementById('addEnt2').style.display != "block"){document.getElementById('addEnt2').style.display = "block"; document.getElementById('addEnt1').style.display = "none";}
	else if(document.getElementById('addEnt2').style.display == "block"){document.getElementById('addEnt1').style.display = "block"; document.getElementById('addEnt2').style.display = "none";}
	return false;
}

function processAddEnt(target)
{
	var dasDate = escape(target.entDate.value);
	var dasEvent = escape(target.entEntry.value);
	var dasTimeH = escape(target.entHour.value);
	var dasTimeM = escape(target.entMin.value);
	var dasYear = escape(target.entYear.value);
	var dasMonth = escape(target.entMonth.value);
	var dasDay = escape(target.entDay.value);
	var dasSend = 1;
	document.getElementById('addEvtErr').innerHTML = '';
	if(dasEvent == "")
	{
		document.getElementById('addEvtErr').innerHTML = '<b style="color: #FF0000;">Please enter an event!</b>';
		dasSend = 0;
	}
	
	if(dasSend == 1)
	{
		document.getElementById('addEnt1').style.display = "none";
		document.getElementById('addEnt2').style.display = "none";
		document.getElementById('addEnt3').style.display = "block";
		document.getElementById('addEnt3').innerHTML = '<h2>Saving...</h2>';
		
		// Ajax
		var d = new Date();
		var tsec = d.getSeconds();
		var ajaxRequest;
		try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4){
				if(ajaxRequest.responseText == "1")
				{
					// All went according to plan!
					mFloatOpen(0, 200, 'calEntries.php', 'ampd=' + dasDay + 'ampm=' + dasMonth + 'ampy=' + dasYear + '');
				}else{
					// Something went wrong
					document.getElementById('addEnt3').innerHTML = ajaxRequest.responseText;
				}
			}
		}
		var queryString = mFloatRoot + 'addEntry.php?d=' + dasDate + '&e=' + dasEvent + '&h=' + dasTimeH + '&m=' + dasTimeM + '&t=' + tsec;
		ajaxRequest.open("GET", queryString, true);
		ajaxRequest.send(null);		
	}

	return false;
}

function resetAddEnt()
{
	document.getElementById('addEnt1').style.display = "none";
	document.getElementById('addEnt2').style.display = "block";
	document.getElementById('addEnt3').style.display = "none";
	return false;
}

function deleteEntry(eid, ename, eday, emonth, eyear)
{
	var answer = confirm("Are you sure you want to delete '" + ename + "'?");
	if(answer){
	var doitnow = 1;}
	else{
	var doitnow = 0;}
	
	//alert("Deleting " + eid + " from " + eday + "/" + emonth + "/" + eyear + ".");
	// Ajax
	if(doitnow == 1)
	{
		var d = new Date();
		var tsec = d.getSeconds();
		var dasDate = eday + "-" + emonth + "-" + eyear;
		var ajaxRequest;
		try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4){
				if(ajaxRequest.responseText == "1")
				{
					// All went according to plan!
					mFloatOpen(0, 200, 'calEntries.php', 'ampd=' + eday + 'ampm=' + emonth + 'ampy=' + eyear + '');
				}else{
					// Something went wrong
					alert('Something went wrong. Please try again.');
				}
			}
		}
		var queryString = mFloatRoot + 'delEntry.php?d=' + dasDate + '&e=' + eid + '&t=' + tsec;
		ajaxRequest.open("GET", queryString, true);
		ajaxRequest.send(null);
	}
	return false;
}

// mwBubble tooltip
var overit = 0;

function showBubble(bubbleTITLE, bubbleCONTENT)
{
	document.getElementById('mwBubble').innerHTML = "<div class=\"mwBubble-inside\"><b>" + bubbleTITLE + "</b><br />" + bubbleCONTENT + "</div>";
	document.getElementById('mwBubble').className = 'mwBubble-on';
	document.getElementById('mwBubble').style.display = "block";
	var x = 0; var y = 0;
	if(MouseX)
	{
		x = MouseX;
		y = MouseY;
		if(x + 200 > mFloatViewportWidth){x = x - 200;}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
		if(x + 200 > mFloatViewportWidth){x = x - 200;}
	}
	document.getElementById('mwBubble').style.left = (x + 5) + "px";
	document.getElementById('mwBubble').style.top = (y + 10) + "px";
	overit = 1;
}

function clearBubble()
{
	document.getElementById('mwBubble').innerHTML = '';
	document.getElementById('mwBubble').className = 'mwBubble-off';
	document.getElementById('mwBubble').style.display = "none";
	overit = 0;
}

function cookieMonster(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function hideCalAlert()
{
	document.getElementById('calAlert').style.display = "none";
	cookieMonster('mnp_calalert', 'hide' , 1);
	return false;
}