$(document).ready(function() {
	$('#redirect-errors').hide();
	$('#redirect-errors').click(function()
	{
		$('#redirect-errors').hide();
	});

	$('#formanualurl').hide();
	$('#manualurl').hide();
	$('#pages').attr('disabled','');
	$('#manualurl').val('');

	$("#rediractions").click(function(){

		    var urlwebsite = $('#website_url').val();
			var arrIds = [];
			var checkboxes = $(".checkboxes input[type=checkbox]");

			var flag = false;
			//var checkboxes = document.getElementsByTagName('input');
			for(var i = 0, len = checkboxes.length; i < len; i++) {
				if(checkboxes[i].checked == true ) {
					arrIds[i] = checkboxes[i].id;
					flag = true;
				}
			}

			if(!flag){
				return;
			}

			arrIds = json_encode(arrIds);
	
			$.ajax({
				type : 'POST',
				url:''+urlwebsite+'sys/backend_ajax/deletemassredirects/',
				data: ({
					arrIds : arrIds
				}),
				success: function(response){
					response = json_decode(response);
					for(i=0;i<response.length;i++){
						$('#redirect-'+response[i]).remove();
					}
					$("#rediractions").val("With selected");
				// $('#redirect-'+response).remove();
				//top.location.reload();
				}
			});
	});



	$('#isexternal').click(function(){
		if($('#isexternal').attr('checked')==true)
		{
			$('#formanualurl').show();
			$('#manualurl').show();
			$('#pages').attr('disabled','true');
		}else
		{
			$('#formanualurl').hide();
			$('#manualurl').hide();
			$('#pages').attr('disabled','');
			$('#manualurl').val('');
		}
	});
});




function addRedirect()
{
	hideRedirectErrors();
	var urlwebsite = $('#website_url').val();
	var oldUrl = $('#oldurl').val();
	var pageId = $('#pages').val();
	var manualurl = trimString($('#manualurl').val());

	if(checkOldUrl(oldUrl)){
		$.ajax({
			type : 'POST',
			url:''+urlwebsite+'sys/backend_ajax/addredirect/',
			data: ({
				oldUrl : oldUrl,
				pageId : pageId,
				manualurl: manualurl
			}),
			success: function(response){
				
				if(response=='exists')
				{

					$('#redirect-errors').show();
					$('#redirect-exists-error').show();
				}
               
				else if (response =='equal urls')
				{
					$('#oldurl-emptypage-error').show();
					$('#redirect-errors').show();
				}
				else
				{
					$('#redir').prepend(response);
					$('#oldurl').val("");
					$('#pages').val("index");
				}
			/*else if(response=='domain error')
                {
                    $('#oldurl-domain-error').show();
                    $('#redirect-errors').show();
                }*/
			//top.location.reload();
			}
		});
        
	}
}

function checkOldUrl(feildValue)
{
	var urlwebsite = $('#website_url').val();
	var ltrim = /^\s+/g;
	var rtrim = /\s+$/g;
    
	feildValue = feildValue.replace(ltrim,'');
	feildValue = feildValue.replace(rtrim,'');

	if(feildValue=="" || feildValue=="http://")
	{
		$('#redirect-errors').show();
		$('#oldurl-empty-error').show();
		return false;
	}
	if(feildValue.length>255)
	{
		$('#redirect-errors').show();
		$('#oldurl-length-error').show();
		return false;
	}
   

	/* if(!feildValue.match(/^[a-zA-Z0-9_\.\s-\.\/]{1,225}$/))
    {
        $('#redirect-errors').show();
        $('#oldurl-regex-error').show();
        return false;
    }*/
	//all is ok
	return true;
}

function hideRedirectErrors()
{
	$('#oldurl-length-error').hide();
	$('#oldurl-regex-error').hide();
	$('#oldurl-empty-error').hide();
	$('#redirect-exists-error').hide();
	$('#oldurl-domain-error').hide();
	$('#oldurl-emptypage-error').hide();
	$('#redirect-errors').hide();
}

function deleteRedirect(pageId)
{
	var urlwebsite = $('#website_url').val();
	if(confirm('Do you really want to delete this 301 redirect?'))
	{
		if(parseInt(pageId))
		{
			$.ajax({
				type : 'POST',
				url:''+urlwebsite+'sys/backend_ajax/deleteredirect/',
				data: ({
					pageId : pageId
				}),
				success: function(response){
					$('#redirect-'+response).remove();
				//top.location.reload();
				}
			});
		}
	}
}

function proccessUrl(url)
{
	var ltrim = /^\s+/g;
	var rtrim = /\s+$/g;

	url = url.replace(ltrim,'');
	url = url.replace(rtrim,'');
	if(url != '')
	{
		url = url+'.html';
		return replaceSpace(url,'-');
	}else
	{
		return false;
	}

}
//method puts delimiter '-' istead spaces
function replaceSpace(str,delimiter)
{
	var trim = /\s+/g;
	return str.replace(trim,delimiter);
}

function trimString(text)
{
	var ltrim = /^\s+/g;
	var rtrim = /\s+$/g;

	text = text.replace(ltrim,'');
	text = text.replace(rtrim,'');
	return text;
}
