function $(i){return document.getElementById(i)}

function confirm_delete(){
	if(confirm('Are you sure want to delete?')){
		return true;
	}else{
		return false;
	}
}

function addTask(e){
	var key=(window.event)?e.keyCode:e.which;
	if(key==13){
		funcadd($('newtask').value, false);
		if($('newtask').value==""){
			$('plus').src='images/plus_sign.gif';
			hideAdd();
		}
	}
	return true;
}

function showPolicy(){
	var string='We promise to never spam you. We hate getting ads in our inbox so we wont do it to you. <br /><br />Seriously, email marketing needs to die. It\'s evil and no one likes it. And now companies just go out and buy some list with a bunch of emails in it and think it\'s legitamate. <br /><br />So all it takes is for you to sign up for one website that sells your email to an email list compiler and BAM, you get 18 spam messages a day. It\'s like dude...I don\'t care where you got my email, don\'t send me ads, I\'m busy I have things I need to get done. <br /><br />And when they do send me ads for their crappy products they aren\'t even quick about it either. Their spam is like half a page! Jeez, do you really expect me to read all this? <br /><br />Anyway, the only time we\'ll email you is NEVER, we have NO newsletters, NO update emails, NO email marketing, Nothing (the way it should be). Unless you click the \" Forgot my password link \" and email yourself your password you\'ll never receive an email from us. Now go. Go and be productive! ... AND don\'t forget to tell your friends about us!!!';
	var policy=$('policy');
	policy.innerHTML=string;
	policy.style.display='';
	$('policylink').href='javascript:hidePolicy()';
}

function hidePolicy(){
	$('policy').style.display='none';
	$('policylink').href='javascript:showPolicy()';
}

function showRequest(){
	var string='<form action="login.php" method="post" onsubmit="return validateForgot(this)">';
	string+='<span class="input reminder"><label for="reminderemail">e-mail</label>';
	string+='<input type="text" name="reminderemail" id="reminderemail" />';
	string+='<input type="image" name="submit" class="submit" src="images/enter.gif" /></span>';
	string+='</form>';
	var request= $('request');
	request.innerHTML=string;
	request.style.display='';
	$('requestlink').href='javascript:hideRequest()';
}

function hideRequest(){
	$('request').style.display='none';
	$('requestlink').href='javascript:showRequest()';
}

function show(id){
	var task=$(id);
	var div=task.parentNode.getElementsByTagName('p')[0];
	var dwidth=div.offsetWidth;
	var parent=task.parentNode, width=parent.offsetWidth, height=parent.offsetHeight;
	task.style.top=Math.round(height/2)-11+'px';
	task.style.left=dwidth+'px';
	task.style.display='';
    showRemoval('removecomplete');
	showRemoval('removeincomplete');
}

function hide(id){
	$(id).style.display='none';
	hideRemoval('removecomplete');
	hideRemoval('removeincomplete');
}

function showAdd(){
	$('add').style.display='';
	document.forms[0].newtask.focus();
}

function hideAdd(){
	$('add').style.display='none';
}

function check_email(field){
	if(field.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!=-1){ 
		return true;
	}else{
		return false;
	}
}

var xmlHttp;

function completeItem(id){
	xmlHttp=initRequest();
	if(xmlHttp==null){
		alert('Your browser does not support AJAX.');
		return;
	}
	var url='complete.php';
	url=url+'?q1='+id;
	url=url+'&sid='+Math.random();
	xmlHttp.onreadystatechange=stateChanged1;
	xmlHttp.open('GET',url,true); 
	xmlHttp.send(null);
}

function deleteItem(id){
	xmlHttp=initRequest();
	if(xmlHttp==null){
		alert('Your browser does not support AJAX.');
		return;
	}
	var url='delete.php';
	url=url+'?q1='+id;
	url=url+'&sid='+Math.random();
	xmlHttp.onreadystatechange=stateChanged2;
	xmlHttp.open('GET',url,true); 
	xmlHttp.send(null);
}

function removeItem(id){
	xmlHttp=initRequest();
	if(xmlHttp==null){
		alert('Your browser does not support AJAX.');
		return;
	}
	var url='deletec.php';
	url=url+'?q1='+id;
	url=url+'&sid='+Math.random();
	xmlHttp.onreadystatechange=stateChanged3;
	xmlHttp.open('GET',url,true); 
	xmlHttp.send(null);
}

function removeCompleted(){
	if(confirm('Delete all DONE tasks?')){
		xmlHttp=initRequest();
		if(xmlHttp==null){
			alert('Your browser does not support AJAX.');
			return;
		}
		var url='deletec.php';
		url=url+'?q1=all';
		url=url+'&sid='+Math.random();
		xmlHttp.onreadystatechange=stateChanged3;
		xmlHttp.open('GET',url,true); 
		xmlHttp.send(null);
	}
}

function removeIncomplete(){
	if(confirm('Delete all TODO tasks?')){
		xmlHttp=initRequest();
		if(xmlHttp==null){
			alert('Your browser does not support AJAX.');
			return;
		}
		var url='delete.php';
		url=url+'?q1=all';
		url=url+'&sid='+Math.random();
		xmlHttp.onreadystatechange=stateChanged2;
		xmlHttp.open('GET',url,true); 
		xmlHttp.send(null);
		setPosition()
	}
}

function funcadd(var1,hide){
	if(var1!=''){
		xmlHttp=initRequest();
		if(xmlHttp==null){
			alert('Your browser does not support AJAX.');
			return;
		}
		var url='add.php';
		url=url+'?q1='+encodeURIComponent(var1);
		url=url+'&sid='+Math.random();
		xmlHttp.onreadystatechange=stateChanged4;
		xmlHttp.open('GET',url,true); 
		xmlHttp.send(null);
	}
	if(hide){
		hideAdd()
		$('plus').src='images/plus_sign.gif';
	}
}

function stateChanged1(){
	if(xmlHttp.readyState==4){
		var temp=xmlHttp.responseText;
		var data=temp.split('::');
		$('completelist').innerHTML=data[0];
		$('incompletelist').innerHTML=data[1];
		$('count').innerHTML=data[2];
		setPosition()
	}
}

function stateChanged2(){
	if(xmlHttp.readyState==4){
		$('incompletelist').innerHTML=xmlHttp.responseText;
		$('newtask').value='';
		setPosition()
	}
}

function stateChanged3(){
	if(xmlHttp.readyState==4){
		$('completelist').innerHTML=xmlHttp.responseText;
		setPosition()
	}
}

function stateChanged4(){
	if(xmlHttp.readyState==4){
		var temp=xmlHttp.responseText;
		var data=temp.split('::');
		$('incompletelist').innerHTML=data[0];
		$('newtask').value='';
		if(data[1]=='error'){
			showAlert();
		}
		setPosition()
	}
}

function initRequest(){
	var xmlHttp=null;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch(e){
		try{
			xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
			xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return xmlHttp;
}

function showAlert(){
	alert('You already have 200 tasks. Please check off some tasks before adding more');
}

var emailRegex=/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

function validateLogin(form){
	var email=form.loginemail.value;
	var password=form.loginpassword.value;
	if(email==""){
		inlineMsg('loginemail','You must enter your email.',2);
		return false;
	}
	if(!email.match(emailRegex)){
		inlineMsg('loginemail','You have entered an invalid email.',2);
		return false;
	}
	if(password==""){
		inlineMsg('loginpassword','You must enter your password.',2);
		return false;
	}
	return true;
}

function validateSignup(form){
	var email=form.signupemail.value;
	var password=form.signuppassword.value;
	var password2=form.confirmpassword.value;
	if(email==""){
		inlineMsg('signupemail','You must enter an email.',2);
		return false;
	}
	if(!email.match(emailRegex)){
		inlineMsg('signupemail','You have entered an invalid email.',2);
		return false;
	}
	if(password==""){
		inlineMsg('signuppassword','You must enter a password.',2);
		return false;
	}
	if(password2!=password){
		inlineMsg('confirmpassword','The passwords entered do not match.',2);
		return false;
	}
	return true;
}

function validateForgot(form){
	var email=form.reminderemail.value;
	if(email==""){
		inlineMsg('reminderemail','You must enter your email.',2);
		return false;
	}
	if(!email.match(emailRegex)){
		inlineMsg('reminderemail','You have entered an invalid email.',2);
		return false;
	}
	return true;
}

var MSGTIMER=20;
var MSGSPEED=5;
var MSGOFFSET=3;
var MSGHIDE=3;

function inlineMsg(target,string,autohide){
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')){
    msg=document.createElement('div');
    msg.id='msg';
    msgcontent=document.createElement('div');
    msgcontent.id='msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter='alpha(opacity=0)';
    msg.style.opacity=0;
    msg.alpha=0;
  }else {
    msg=document.getElementById('msg');
    msgcontent=document.getElementById('msgcontent');
  }
  msgcontent.innerHTML=string;
  msg.style.display='block';
  var msgheight=msg.offsetHeight;
  var targetdiv=document.getElementById(target);
  targetdiv.focus();
  var targetheight=targetdiv.offsetHeight;
  var targetwidth=targetdiv.offsetWidth;
  var topposition=topPosition(targetdiv)-((msgheight-targetheight)/2);
  var leftposition=leftPosition(targetdiv)+targetwidth+MSGOFFSET;
  msg.style.top=topposition+'px';
  msg.style.left=leftposition+'px';
  clearInterval(msg.timer);
  msg.timer=setInterval("fadeMsg(1)",MSGTIMER);
  if(!autohide){
    autohide=MSGHIDE;  
  }
  window.setTimeout("hideMsg()",(autohide * 1000));
}

function hideMsg(msg){
  var msg=document.getElementById('msg');
  if(!msg.timer){
    msg.timer=setInterval("fadeMsg(0)",MSGTIMER);
  }
}

function fadeMsg(flag){
  if(flag==null){
    flag=1;
  }
  var msg=document.getElementById('msg');
  var value;
  if(flag==1){
    value=msg.alpha+MSGSPEED;
  }else {
    value=msg.alpha-MSGSPEED;
  }
  msg.alpha=value;
  msg.style.opacity=(value/100);
  msg.style.filter='alpha(opacity='+value+')';
  if(value>=99){
    clearInterval(msg.timer);
    msg.timer=null;
  }else if(value<=1){
    msg.style.display="none";
    clearInterval(msg.timer);
  }
}

function leftPosition(target){
  var left=0;
  if(target.offsetParent){
    while(1){
      left+=target.offsetLeft;
      if(!target.offsetParent){
        break;
      }
      target=target.offsetParent;
    }
  }else if(target.x){
    left+=target.x;
  }
  return left;
}

function topPosition(target){
  var top=0;
  if(target.offsetParent){
    while(1){
      top+=target.offsetTop;
      if(!target.offsetParent){
        break;
      }
      target=target.offsetParent;
    }
  }else if(target.y){
    top+=target.y;
  }
  return top;
}

if(document.images){
  var arrow=new Image(7,80); 
  arrow.src="images/msg_arrow.gif"; 
}

function pageWidth(){
  return window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null;
}

function pageHeight(){
  return window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null? document.body.clientHeight:null;
}

function topAdPosition(){
  return typeof window.pageYOffset!='undefined'?window.pageYOffset:document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:0;
}

function leftAdPosition(){
  return typeof window.pageXOffset!='undefined'?window.pageXOffset:document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:0;
}

function showad(adlink){
  var ad,adclose,adcontent,admask;
  if(!document.getElementById('ad')){
    ad=document.createElement('div');
    ad.id='ad';
    adclose=document.createElement('div');
    adclose.id='ad-close'
    adcontent=document.createElement('div');
    adcontent.id='ad-content';
    admask=document.createElement('div');
    admask.id='ad-mask';
    document.body.appendChild(admask);
    document.body.appendChild(ad);
    ad.appendChild(adclose);
    ad.appendChild(adcontent);
    adclose.setAttribute('onclick','hidead()');
    adclose.onclick=hidead;
  }else{
    var ad=document.getElementById('ad');
    var adcontent=document.getElementById('ad-content');
    var admask=document.getElementById('ad-mask');
  }
  ad.style.opacity=admask.style.opacity=0;
  ad.style.filter=admask.style.filter='alpha(opacity=0)';
  admask.style.display='block';
  ad.style.display='block';
  adcontent.innerHTML='';
  adcontent.appendChild(adlink);
  ad.style.width=adcontent.offsetWidth+'px';
  ad.style.filter=admask.style.filter='alpha(opacity=0)';
  var width=pageWidth(), height=pageHeight(), left=leftAdPosition(), top=topAdPosition();
  var adwidth=ad.offsetWidth, adheight=ad.offsetHeight;
  ad.style.top=top+(height/2)-(adheight/2)+'px';
  ad.style.left=leftposition=left+(width/2)-(adwidth/2)+'px';
  admask.style.height=document.getElementById('content').offsetHeight+'px';
  tiny.alpha.set(admask,75,10);
  tiny.alpha.set(ad,100,6);
}

function hidead(){
	var ad=document.getElementById('ad');
	var mask=document.getElementById('ad-mask');
	tiny.alpha.set(mask,0,10);
	tiny.alpha.set(ad,0,6);
	adInit();
}

function getAd(){
	var random=Math.ceil(100*Math.random());
	var link=document.createElement('a');
	link.setAttribute('href','http://d1.openx.org/ck.php?n=adf2e26a&cb='+random);
	link.setAttribute('target','_blank');
	var image=new Image();
	image.src='http://d1.openx.org/avw.php?zoneid=6555&cb='+random+'&n=adf2e26a';
	link.appendChild(image);
	return link;
}

var tiny={};

tiny.alpha=function(){
	return{
		set:function(i,a,s){
			var t=(typeof i=='object')?i:tiny.$(i), o=(t.style.opacity)?t.style.opacity*100:0;
			if(o!=a){
				var d=(a>o)?1:-1; clearInterval(t.at); t.at=setInterval(function(){tiny.alpha.tween(t,a,d,s)},20)
			}
		},
		tween:function(t,a,d,s){
			var o=(t.style.opacity)?Math.round(t.style.opacity*100):0; 
			if(o==a){clearInterval(t.at); if(d!=1){t.style.display='none'}}
			else{var n=o+(Math.ceil(Math.abs(a-o)/s)*d); t.style.opacity=n/100; t.style.filter='alpha(opacity='+n+')'}
		}
	}
}()

tiny.style=function(){
	return{
		value:function(e,p){
			return (e.currentStyle)?e.currentStyle[p]:document.defaultView.getComputedStyle(e,null).getPropertyValue(p)
		}
	}
}();

function setPosition(){
	var wh=pageHeight(), ce=$('tasks'), ch=ce.offsetHeight;
	ce.style.top=wh-ch>0?((wh/2)-(ch/2))+'px':0;
	var rc=$('removecomplete'), ri=$('removeincomplete'), cl=$('completelist'), il=$('incompletelist');
	ri.style.height=il.offsetHeight+'px';
	rc.style.top=cl.offsetTop+'px'; rc.style.height=cl.offsetHeight+'px';
	if(wh<ch){document.body.style.height=ch+'px'}
}

window.onresize=function(){setPosition()}

function adInit(){
	var link=getAd();
	setTimeout(function(){showad(link)},600000)
}

function setRemoval(){
	var rc=$('removecomplete'), ri=$('removeincomplete')
	rc.onmouseover=function(){showRemoval('removecomplete')}
	ri.onmouseover=function(){showRemoval('removeincomplete')}
	rc.onmouseout=function(){hideRemoval('removecomplete')}
	ri.onmouseout=function(){hideRemoval('removeincomplete')}
	rc.onclick=removeCompleted;
	ri.onclick=removeIncomplete;
}

function showRemoval(e){
	var r=$(e);
	if(r.offsetHeight>20){r.className='removeall'}
}

function hideRemoval(e){
	$(e).className='';
}

function loadEvents(){
	// adInit();
	setPosition();
	setRemoval();
}
