var xmlhttp;
function fvoid() {}

function display_login () {
	document.getElementById('forms_answ').innerHTML='';
	document.getElementById('options').innerHTML='<input type="text" class="login_user" id="login_user" value="User" onblur="if (!this.value) this.value=\'User\';" onfocus="if (this.value==\'User\') this.value=\'\';" />';
	document.getElementById('options').innerHTML+='<input type="password" class="login_passw" id="login_passw" value="Password" onblur="if (!this.value) this.value=\'Password\';" onfocus="if (this.value==\'Password\') this.value=\'\';" />';
	document.getElementById('options').innerHTML+='<a href="javascript:fvoid();" onclick="login(document.getElementById(\'login_user\').value,document.getElementById(\'login_passw\').value);" onfocus="this.blur();" class="login_button" title="">Login</a>';
}

function display_signup () {
	document.getElementById('forms_answ').innerHTML='';
	document.getElementById('options').innerHTML='<ul class="signup"><li><input type="text" id="reg_u" class="reg_field" value="User:" onblur="if (!this.value) this.value=\'User:\';" onfocus="if (this.value==\'User:\') this.value=\'\';" /></li><li><input type="text" id="reg_n" class="reg_field" value="Name:" onblur="if (!this.value) this.value=\'Name:\';" onfocus="if (this.value==\'Name:\') this.value=\'\';" /></li><li><input type="password" id="reg_p" class="reg_field" value="Password:" onblur="if (!this.value) this.value=\'Password:\';" onfocus="if (this.value==\'Password:\') this.value=\'\';" /></li><li><input type="text" id="reg_e" class="reg_field" value="E-mail:" onblur="if (!this.value) this.value=\'E-mail:\';" onfocus="if (this.value==\'E-mail:\') this.value=\'\';" /></li><li><a href="javascript:fvoid();" onclick="signup(document.getElementById(\'reg_u\').value,document.getElementById(\'reg_p\').value,document.getElementById(\'reg_n\').value,document.getElementById(\'reg_e\').value);" onfocus="this.blur();" class="reg_submit">Send</a></li></ul>';
}

function getxmlhttp() {
	var xmlhttp=null;
	try {
		xmlhttp=new XMLHttpRequest();
	}
	catch (e) {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		if(!xmlhttp) {
			alert("Your browser does not support Ajax! Please update!");
		}
	}
	return xmlhttp;
}

function shrink(url) {
	if (url=='http://') document.getElementById('forms_answ').innerHTML='Enter a valid URL!';
	else if (url.length<=20) document.getElementById('forms_answ').innerHTML='The URL is small. It doesn\'t worth shrinking.';
	else {
		xmlhttp=getxmlhttp();
		if (xmlhttp==null) {
		  alert ("Your browser does not support AJAX! Please update!");
		  return;
		  }
		xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					document.getElementById('longurl').value='';
					document.getElementById('result').style.display='block';
					document.getElementById('result_url').value=xmlhttp.responseText;
				}
			}
		xmlhttp.open("POST","lib/create.php",true);
		params="url="+url;
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
	}
}

function login(user,passw) {
	if (!user || user=='User') document.getElementById('forms_answ').innerHTML='Username and/or password incorect!';
	else if (!passw || passw=='Password') document.getElementById('forms_answ').innerHTML='Username and/or password incorect!';
	else {
		xmlhttp=getxmlhttp();
		if (xmlhttp==null) {
			alert ("Your browser does not support AJAX! Please update!");
			return;
		}
		xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					if (xmlhttp.responseText=='auth') {
						var today = new Date();
						var expire = new Date();
						expire.setTime(today.getTime() + 3600000*24*30);
						document.cookie="aut=1;expires="+expire.toGMTString();
						document.cookie = "aus="+escape(user)+ ";expires="+expire.toGMTString();
						document.location="index.php";
					}
					else document.getElementById('forms_answ').innerHTML=xmlhttp.responseText;
				}
			}
		xmlhttp.open("POST","lib/cookie.php",true);
		params="user="+user+"&passw="+passw;
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
	}
}

function signup(user,passw,name,email) {
	xmlhttp=getxmlhttp();
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX! Please update!");
		return;
	}
	xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				document.getElementById('forms_answ').innerHTML=xmlhttp.responseText;
			}
		}
	xmlhttp.open("POST","lib/cookie.php",true);
	params="user="+user+"&passw="+passw+"&name="+name+"&email="+email;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}

function logout() {
	document.cookie="aut=0";
	document.cookie="aus=0";
	document.location="/";
}

function display_edit(id) {
	document.getElementById("edit_"+id).className="edit_on";
}

function edit(id,action,url) {
	xmlhttp=getxmlhttp();
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX! Please update!");
		return;
	}
	xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.responseText=='OK') {
					document.getElementById('edit_'+id).style.display='none';
					document.getElementById('l_answ_'+id).innerHTML='Your link has been updated and your clicks count reset!';
					window.location="manage.php";
				}
				else document.getElementById('l_answ_'+id).innerHTML=xmlhttp.responseText;
			}
		}
	xmlhttp.open("POST","lib/cookie.php",true);
	params="id="+id+"&action="+action+"&url="+url;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}

function del(id,action) {
	xmlhttp=getxmlhttp();
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX! Please update!");
		return;
	}
	xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.responseText=='OK') document.getElementById('link_'+id).style.display='none';
				else document.getElementById('l_answ_'+id).innerHTML=xmlhttp.responseText;
			}
		}
	xmlhttp.open("POST","lib/cookie.php",true);
	params="id="+id+"&action="+action;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}
