function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function checkTypeLink(url){
	
	var validatesVideo =  RegExp("http.*\\?v=([a-zA-Z0-9_\\-]+)(?:&.)*");
	
	if (trim(url) != "" && trim(url) != "http:\\") {
		
		var ext = url.substring(url.lastIndexOf(".") + 1, url.length);
		
		if (validatesVideo.test(url) == true) {
			window.location = 'publicar-video.jsp?url='+encodeURI(url);
			return true;
		}
		
		if (ext == "jpg" || ext == "png" || ext == "jpeg" || ext == "gif") { 
			window.location = 'publicar-imagem.jsp?url='+encodeURI(url);
			return true;
		}
	}
	
	return false;
}


function reduceLink() {
   var radioChecked = "";
   
   for (var i = 0; i < document.getElementsByName("typeOfReduction").length; i++) {
   		if (document.getElementsByName("typeOfReduction")[i].checked == true) {
			radioChecked = document.getElementsByName("typeOfReduction")[i].value;
		}
   }

   if (radioChecked == "custom") {
   		
		if (trim(document.getElementById("url").value) == "" || document.getElementById("url").value == "http://") {
			alert("Digite o link que deseja reduzir.");
			document.getElementById("url").focus();
			return false;
		}
		
		//Redireciona caso seja uma url de vídeo ou imagem
		checkTypeLink(document.getElementById("url").value);
				
		if (trim(document.getElementById("keyword").value) == "") {
			alert("Digite o nome do link personalizado.");
			document.getElementById("keyword").focus();
			return false;
		}
		
		var keyword = document.getElementById("keyword").value;
		var actionID = 5;
		
		$(document).ready(function() {
			
			$.get("/proc.jsp", { actionID: actionID, keyword: keyword }, function(response) {
				if(response == -1){
					alert("Já existe um link personalizado com este nome, Por favor digite outro nome.");
					document.getElementById("keyword").focus();
					return false;
				}else{
					document.newURL.action = "proc.jsp";
					document.getElementById("buttonReduce").style.display = "none";
					document.getElementById("loading").style.display = "block";
					document.newURL.submit();
				}
			});
		});
   }else{
		if (trim(document.getElementById("url").value) == "" || document.getElementById("url").value == "http://") {
			alert("Digite o link que deseja reduzir.");
			document.getElementById("url").focus();
			return false;
		} else {
			//Redireciona caso seja uma url de vídeo ou imagem
			if (!checkTypeLink(document.getElementById("url").value)) {
				//Limpando campo
				document.getElementById("keyword").value = "";
				document.newURL.action = "proc.jsp";
				document.getElementById("buttonReduce").style.display = "none";
				document.getElementById("loading").style.display = "block";
				document.newURL.submit();
			}
		}
   }
   return;
}

function searchUrl(){
	var link = document.getElementById("search").value;
	
	if (trim(link) == "") {
		alert("Digite o que deseja pesquisar.");
		document.getElementById("search").focus();
		return false;
	}
	
	window.location = "history.jsp?strUrl=" + link;
}


//Facebook
function openSharedPopUp(url) {
	if (url == '') {
		url = window.location;
	}
	window.open('http://www.facebook.com/sharer.php?u=' + url ,'ventanacompartir', 'toolbar=0, status=0, width=650, height=450');
}

//Twitter
function sharedPageTwitter() {
	window.location= "https://twitter.com/share";
}

//E-mail
function sharedPageEmail(){
	var url = window.location;
	top.location.href = "indic.jsp?url="+encodeURI(url);
}

