var domain = {
  checkformat: function (dominio)
  {
    formato = new RegExp (/^\w+([\.-]?\w+)*(\.\w{2,4})+$/);
    if (!formato.test(dominio))
    {
      alert ('Su busqueda no cumple con el formato de un dominio');
      return false;
    }
    
    return true;
  }
}

var total = 0;

$(document).ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    function sameOrigin(url) {
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

$(document).ready(function()
{
  if ($('#dsearch'))
  {
    $('#btn').click(function(event)
    {
      event.preventDefault();
      
      if ($('#id_dominio').attr('value') != "")
      {
        if (domain.checkformat($('#id_dominio').attr('value')))
        {
          $('#dsearch').submit();
        }
      }
      else
      {
        alert('Mensaje del Sistema", "Debe de escribir un dominio, ej: midominio.com');
      }
    });
  }
  
	//if ($('input[name="csrfmiddlewaretoken"]').val() != '') 
	//{
	//	$.ajaxSetup({
	//		beforeSend: function(xhr, settings) {
	//			if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
	//				// Only send the token to relative URLs i.e. locally.
	//				xhr.setRequestHeader("X-CSRFToken",
	//				$('input[name="csrfmiddlewaretoken"]').val());
	//			}
	//		}
	//	});
	//}
  
  if ($('#resultado'))
  {
    $('#lnk').click(function(event)
    {
      event.preventDefault();
      var formElem = "";
     $("input:checkbox:not(#rememberme)" ).filter('input:checked').each(function(index)
      {
        valores = $(this).attr('value').split(':');
        registrar = {
          dominio: valores[0],
          precio: parseFloat(valores[1].split(',').join('.')),
        }
        
        total += registrar.precio;
        id = index+1;
        formElem += '<input type="hidden" name="c_prod_'+ id + '" value="Registro dominio .'+registrar.dominio.split('.')[1]+'" /> \n\
                     <input type="hidden" name="c_name_'+ id + '" value="Registro dominio .'+registrar.dominio.split('.')[1]+'" /> \n\
                     <input type="hidden" name="c_description_'+ id + '" value="Registro del dominio '+registrar.dominio+' 1 año" /> \n\
                     <input type="hidden" name="c_price_'+ id + '" value="'+registrar.precio+'" />\n';
        
      });
      formElem += '<input type="hidden" name="total" value="'+total.toFixed(2)+'" />';
      $(formElem).appendTo('#2CO');
      if (total > 0)
      {
        $('#2CO').submit();
      }
    });
  }
  
  if ($('#submit'))
  {
  	$('#submit').click(function(event)
  	{
  		$.post('/login/', {'log': $('#log').attr('value'), 'password': $('#pwd').attr('value')}, function(data)
  		{
  			
  		});
  	});
  }
  
  $('#content_tab').mootabs({
  	ajaxUrl: '/planes/show-plan/',
  });
});

