$(document).ready(function() {
	$("#registerpod").hide();
});
function fetchMeta(){
	var nsfw=0;
	var shorty=document.fGetShorty;
	var data=shorty.data.value;
	var actn=shorty.actn.value;
	var code=document.fTwitter.twiturl.value;
	$.post('metaData.php',{url:data,code:code},function(data){});
}
function getShorty(){
	var nsfw=0;
	var shorty=document.fGetShorty;
	var data=shorty.data.value;
	var actn=shorty.actn.value;
	//(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i
	//var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|http:\/\/|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
	var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|http:\/\/|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)+[a-z]{2,6}");
	//var urlregex = new RegExp("^(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)");
	if(shorty.nsfw.checked == true){
		nsfw = 1;
	}
	if(actn == '' || data == "Paste your Text here" || data == "Paste one or more Url's here" ||  data == ''){
		$("#displayLink").hide();
		$("#ShortyHelp").hide();
		$("#ShortyError").show();
		$("#showShortyError").hide().html("What do you want to shorten?").fadeIn("slow");
		//var t = setTimeout("clearALL('Shorty')",3000);
		//var t = setTimeout("displayALL('registerpod')",3001);
	}else if(!urlregex.test(data) && actn=="m"){
		$("#displayLink").hide();
		$("#ShortyHelp").hide();
		$("#ShortyError").show();
		$("#showShortyError").hide().html("please enter the valid url").fadeIn("slow");
		//var t = setTimeout("clearALL('Shorty')",3000);
		//var t = setTimeout("displayALL('registerpod')",3001);
	}else{
		$("#displayLink").hide();
		$("#ShortyHelp").hide();
		$("#ShortyError").show();
		$("#showShortyError").hide().html("L o a d i n g ....").fadeIn("slow");
		$.post('makeshorty.php',{data:data,action:actn,nsfw:nsfw},function(data){
			if(data == "Please enter a valid URL"){
				$("#ShortyError").show();
				$("#showShortyError").hide().html("Please enter a valid URL").fadeIn("slow");
			}else if(data=="What do you wanna do?"){
				$("#ShortyError").show();
				$("#showShortyError").hide().html("What do you want to shorten?").fadeIn("slow");
			}else if(data=="please sign in ...."){
				$("#ShortyError").show();
				$("#showShortyError").hide().html("please sign in ....").fadeIn("slow");
			}else{
				var tempArrayValue=data.split('~##~');
				document.getElementById("hiddenmsg").value="Wassup,\nI like this link to you please check it out "+tempArrayValue[1];
				$("#ShortyHelp").hide();
				$("#ShortyError").hide();
				$("#displayLink").hide().html(tempArrayValue[0]).fadeIn("slow");
				var actualData=$('#appendDiv').html();
				var appendData=tempArrayValue[2]+actualData;
				$('#appendDiv').html(appendData);
				if(actn=="m"){
					fetchMeta();
				}
			}
		});
	}
}
function selfClose(divid){
	document.getElementById(divid).style.display='none';
}
function  clearALL(divid){
	selfClose(divid);
}
function displayALL(divid){
document.getElementById(divid).style.display='block';
}
function SignUp(){
	var signup=document.fSignUp;
	var name=Trim(signup.name.value);
	var email=Trim(signup.email.value);
	var pswd=Trim(signup.pswd.value);
	emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	if(name == ''){
		document.getElementById("registerpod").style.display="none";
		$("#Shorty").hide().html("<br><b>Please fill in your name</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		var t = setTimeout("displayALL('registerpod')",3001);
		signup.name.focus();
	}else if(email == ''){
		document.getElementById("registerpod").style.display="none";
		$("#Shorty").hide().html("<br><b>Please fill in your email address</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		var t = setTimeout("displayALL('registerpod')",3001);
		signup.email.focus();
	}else if(pswd == ''){
		document.getElementById("registerpod").style.display="none";
		$("#Shorty").hide().html("<br><b>Please fill in the password</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		var t = setTimeout("displayALL('registerpod')",3001);
		signup.pswd.focus();
	}else if(!emailRe.test(email)){
		document.getElementById("registerpod").style.display="none";
		$("#Shorty").hide().html("<br><b>Please fill in a valid email id</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		var t = setTimeout("displayALL('registerpod')",3001);
		signup.name.focus();
	}else{
		document.getElementById("registerpod").style.display="none";
		$("#Shorty").hide().html("<br><b>Siging you up... Wait</b>").fadeIn("slow");
		$.post('signup.php',{name:name,email:email,pswd:pswd},function(data){
			if(data == "Duplicate"){
				$("#Shorty").hide().html(data).append("<br><b>email id already exist</b>").fadeIn("slow");
				var t = setTimeout("clearALL('Shorty')",3000);
				var t = setTimeout("displayALL('registerpod')",3001);
				signup.email.focus();
			}else{
				$("#dSignUp").hide().html(data).fadeIn("slow");
				$("#Shorty").hide().html("<br><b>Sign In to Continue</b>").fadeIn("slow");
				document.fSignIn.email.focus();
			}
		});
	}
}
function SignIn(){
	var signin=document.fSignIn;
	var email=Trim(signin.email.value);
	var pswd=Trim(signin.pswd.value);
	$("#SignUpMain").hide();
	if(email == ''){
		$("#Shorty").hide().html("<br><b>Please enter the email id</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		signin.email.focus();
	}else if(pswd == ''){
		$("#Shorty").hide().html("<br><b>Please enter the password</b>").fadeIn("slow");
		var t = setTimeout("clearALL('Shorty')",3000);
		signin.pswd.focus();
	}else{
		$("#Shorty").hide().html("<br><b>Signing you In...</b>").fadeIn("slow");
		$.post('signin.php',{email:email,pswd:pswd},function(data){
			if(data == "error"){
				$("#Shorty").hide();
				$("#Shorty").hide().html(data).fadeIn("slow");
				var t = setTimeout("clearALL('Shorty')",3000);
				signin.email.focus();
			}else{
				var temp=data.split('~#~');
				$("#Shorty").hide();
				$("#dSignIn").hide().html(temp[0]).fadeIn("slow");
				$("#afterLogin").hide().html(temp[1]).fadeIn("slow");
				$("#SignUpMain").hide();
				afterLoginAction(1);
			}
		});
	}
}
function TwitIt(){
	var twit=document.fTwitter;
	var usrname=twit.usrname.value;
	var pswd=twit.pswd.value;
	var twiturl=twit.twiturl.value;
	var msg="";
	if(usrname == '' || pswd == ''){
		$("#twitterForm").hide();
		$("#twitterMsg").show();
		$("#twitterMsgError").hide().html("Wassup? You in a hurry?").fadeIn("slow");
		var t = setTimeout("clearALL('twitterMsg')",3000);
		var t = setTimeout("displayALL('twitterForm')",3001);
		twit.usrname.focus();
	}else{
		$("#twitterForm").hide();
		$("#twitterMsg").show();
		$("#twitterMsgError").hide().html("Connecting to  twitter").fadeIn("slow");
		$.post('twitter.php',{usrname:usrname,pswd:pswd,msg:twiturl,twiturl:twiturl},function(data){
			$("#twitterMsgError").hide().html("P o s t e d to  twitter").fadeIn("slow");
		});
	}
}
function cMsg(act){
	var doc=document.fGetShorty;
	doc.actn.value = act;
	//if((doc.data.value == "Paste your Text here" || doc.data.value == '') && act == 'm'){
	if(act == 'm'){
		doc.data.value = "Paste one or more Url's here";
	}
	//if((doc.data.value == "Paste one or more Url's here" || doc.data.value == '') && act == 't'){
	if(act == 't'){
		doc.data.value = "Paste your Text here";
	}
}
function Cleanup(){
	var doc=document.fGetShorty;
	if(doc.data.value == "Paste your Text here" || doc.data.value == "Paste one or more Url's here" ){
		doc.data.value = "";
	}
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function Get_Cookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function afterLoginAction(pg){
	if(readCookie("userid")){
		$.post('afterLoginAction.php',{pg:pg},function(data){
			$("#afterLoginFunction").hide().html(data).fadeIn("slow");
		});
	}else{
		$.post('afterLoginActionWithout.php',{},function(data){
			$("#afterLoginFunction").hide().html(data).fadeIn("slow");
		});
	}
}
function deleteQuick(id){
	if(readCookie("userid")){
		$.post('deleteQuick.php',{id:id},function(data){
			if(data=="success"){
				afterLoginAction(1);
			}
		});
	}
}
function Trim(str){
	 var s = str.replace(/^(\s)*/, '');
	 s = s.replace(/(\s)*$/, '');
	 return s;
}
function qikTagIt(code){
	var qikTagItVal=document.getElementById("qikTag").value;
	if(Trim(qikTagItVal)==""){
		document.getElementById("displayLink").style.display="none";
		$("#ShortyError").show();
		$("#showShortyError").hide().html("What do you wanna to tag?").fadeIn("slow");
		var t = setTimeout("clearALL('ShortyError')",3000);
		var t = setTimeout("displayALL('displayLink')",3001);
		document.getElementById("qikTag").focus();
	}else{
		$.post('qikTagIt.php',{code:code,tag:qikTagItVal},function(data){
			if(data=="success"){
				//afterLoginAction();
				$("#afterQikTagIt").hide().html('<span class="white14">Thank you for tagging.</span>').fadeIn("slow");
			}
		});
	}
}
function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}
function logintoaccessfun(url){
	if(getCookie("USERID")){
		window.location.href=url;
	}else{
		$.blockUI(var1, {width:'400px'});
	}
}
function sendmsg(){
	$.blockUI(var2, {width:'400px'});
}
function footerpop(type){
	$.unblockUI();
	document.getElementById('footertype').value=type;
	$.blockUI(var3, {width:'400px'});
	switch(document.getElementById('footertype').value){
		case 'contact':
			document.getElementById('footertypetitle').innerHTML="Contact Us";
			break;
		case 'feedback':
			document.getElementById('footertypetitle').innerHTML="Feedback";
			break;
		case 'bug':
			document.getElementById('footertypetitle').innerHTML="Bug On";
			break;
		default :
			break;
	}
}
function sendmsg2db(){
	emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	var toEmail=document.getElementById("hiddenToAddress").value;
	var fromEmail=document.getElementById("hiddenFromAddress").value;
	if(!emailRe.test(fromEmail)){
		$("#hiddenFromAddressError").show();
		return;
	}
	if(!emailRe.test(toEmail)){
		$("#hiddenFromAddressError").hide();
		$("#hiddenToAddressError").show();
		return;
	}
	$("#hiddenToAddressError").hide();
	$("#hiddenFromAddressError").hide();
	var hiddenmsg=document.getElementById("hiddenmsg").value;
	$.post('sendmessage.php', { toEmail:toEmail,fromEmail:fromEmail, msg:hiddenmsg},
	function(data){
		if(data=="success"){}
	});
	document.getElementById("hiddenToAddress").value="";
	document.getElementById("hiddenFromAddress").value="";
	//document.getElementById("hiddenmsg").value="";
	$.unblockUI();
}
function sendfootermsg2db(){
	emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	var fromEmail=document.getElementById("hiddenfooterFromAddress").value;
	if(!emailRe.test(fromEmail)){
		$("#hiddenfooterFromAddressError").show();
		return;
	}
	$("#hiddenfooterFromAddressError").hide();
	var hiddenmsg=document.getElementById("hiddenfootermsg").value;
	var footertype=document.getElementById("footertype").value;
	$.post('sendfootermessage.php', {fromEmail:fromEmail, msg:hiddenmsg, footertype:footertype},
	function(data){
		if(data=="success"){}
	});
	document.getElementById("hiddenfooterFromAddress").value="";
	$.unblockUI();
}
function changeclass(selectedid,nextid){
		document.getElementById(selectedid).className="unselected";
		document.getElementById(nextid).className="selected";
	}