// JavaScript Document

jQuery(document).ready(function() {
	$("#buscar").val("Buscar");
	
	if ($("#smsprecio").length>0) {

		smsprecio();
	}
	var a=$('#frmvoxa iframe').contents().find('input[name=nonce]').val();
	
	
});

var onmipymes=false;

function altanewsletter(){
	
	$.ajax({
		url:'/index/altanewsletter',
		type:"POST",data:"mail="+$("#suscripcion").val(),
		success:function(datos){
		alert(datos);
		}
});
	
}
function AddSelectOption(selectObj, text, value, isSelected) 
{
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = 
            new Option(text, value, false, isSelected);
    }
}
function removegrupo(){
	
	gpassval=$('#selenviar :selected').val();
	gpasstext=$('#selenviar option:selected').text();
	if(typeof gpassval != 'undefined'){
	$('#dispoenviar').prepend('<option value="'+gpassval+'" selected="selected">'+gpasstext+'</option>');
	}
	$("#selenviar option:selected").remove();
	$("#gruposenviar").val("");
	$("#selenviar option").each(function(){

        $("#gruposenviar").val($("#gruposenviar").val()+$(this).attr('value')+",");

     });
	
	$("#gruposenviar").val($$("gruposenviar").value.substr(0,$$("gruposenviar").value.length-1));

}

function passgrupo(){
	var gpassval,gpasstext;

	
	gpassval=$('#dispoenviar :selected').val();
	gpasstext=$('#dispoenviar option:selected').text();
	
	if(typeof gpassval != 'undefined'){
	$("#dispoenviar option:selected").remove();

	$('#selenviar').prepend('<option value="'+gpassval+'" selected="selected">'+gpasstext+'</option>');
	$("#gruposenviar").val("");
	$("#selenviar option").each(function(){
    	
		
        $("#gruposenviar").val($("#gruposenviar").val()+$(this).attr('value')+",");
        
        
     });
	
	$("#gruposenviar").val($$("gruposenviar").value.substr(0,$$("gruposenviar").value.length-1));
}
}

function enviarsms(){
	var guardarhist;
	if($("#remitente").val()==""||($("#destinatarios").val()==""&&$("#gruposenviar").val()=="")||$("#mensaje").val()=="")
	{
		alert("El campo remitente, destinatarios y mensaje son requeridos");
	}
	else{
		
		if($$("guardarhist").checked){
			guardarhist="1";
		}else{
			guardarhist="0";
		}
		
		
		$("#buttonload").fadeOut(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');}
			$("#ajaxload").fadeIn(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');}
			$.ajax({
				url:'/enviosms/doenviar',
				type:"POST",data:"remitente="+$("#remitente").val()+
				"&destinatarios="+$("#destinatarios").val()+"&mensaje="
				+$("#mensaje").val()+"&gruposenviar="+$("#gruposenviar").val()+"&guardarhist="+guardarhist,
				success:function(datos){
					alert(datos);
					
					$("#buttonload").fadeIn(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');}});
					$("#ajaxload").fadeOut(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');}});
				}
			});
			
			});
		});
		
	}
	
}
function lineavoipchk(){
	if($('#saldo').val()!=""&&isNumber($('#saldo').val())){
		$('#frmcontratar').submit();
	}
	else{
		alert("El saldo no es correcto");
	}
}
function verifgeografico(){
	if($("#pais").val()!=""&&$("#ciudad").val()!=""&&$("#numero").val()!=""){
		$('#frmcontratar').submit();
	}else{
		alert("Seleccione el país, la ciudad y el número para el alta de número geográfico");
	}
}
function loadlinevoipcities(){
	
	$.ajax({
		url:'/tarifa/getciudadlineasvoip',
		type:"POST",data:"pais="+$("#pais").val(),
		success:function(datos){
		
			$("#ajaxcity").html(datos);

		}
	});
}
function loadcities(){
	
	$.ajax({
		url:'/contratar/loadcities',
		type:"POST",data:"pais="+$("#pais").val(),
		success:function(datos){
		
			$("#ajaxcity").html(datos);
			getfrecuencias();
		}
	});
}

function getfrecuencias(){
	$.ajax({
		url:'/contratar/getfrecuencias',
		type:"POST",data:"pais="+$("#pais").val()+"&ciudad="+$("#ciudad").val(),
		success:function(datos){
				
			$("#ajaxfrecuencia").html(datos);

		}
	});
	
}
function checkcontacto(){
	if($("#nombre").val()==""||$("#email").val()==""||$("#consulta").val()=="")
	{
		alert("El campo nombre, e-mail y consulta son requeridos");
	}else if(!document.getElementById("contaceptapolitica").checked)
	{
		alert("Debe aceptar la política de privacidad");	
	}
	else if(!chkmail($("#email").val()))
		{
		alert("El e-mail es incorrecto");	
		}

	else{$("#frmcontacto").submit();}
		
}
function datospago(){
	$("#formpagocesta").submit();
	$("#procesarpago").fadeIn(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');

	
	}});
	$("#seleccionpago").fadeOut(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');
	

	}});
	
}
function setmetodospago(tipo){
	
	if(tipo=="transferencia"){
		alert("Actualmente solo aceptamos tarjeta de crédito");
		
	}
}

function showmicuenta(){
		var x = $("#micuenta").offset().left;
		var y = $("#micuenta").offset().top;
		$("#menupymes").css('top',y+27);
		$("#menupymes").css('left',x-41);
		$("#menupymes").show(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');
		
		
		}});
		$('#menupymes').mouseleave(function(){
			hidemicuenta();
			
		});
}
function hidemicuenta(){

		$("#menupymes").hide(function() { if(jQuery.browser.msie) { this.style.removeAttribute('filter');}});
	
}

function confirmareliminarcont(){
	if(confirm("¿Está seguro que quiere eliminar los contactos?")) {
		$('#frmglobalcontacto').submit();
	}
}
function smsprecio(){
	var precioxsms=0;
	var nsms;
	var precio;
	nsms=$("#numsms").val();

	if(nsms<500){
		precioxsms=$("#preciox").val();
		
	}else if(nsms>=500&&nsms<1000){
		precioxsms=$("#preciox500").val();
	}
	else if(nsms>=1000&&nsms<1500){
		precioxsms=$("#preciox1000").val();
	}
	else if(nsms>=1500){
		precioxsms=$("#preciox1500").val();
	}
	
	
	if(is_int(nsms)){
		precio=nsms*precioxsms;

		$("#smsprecio").html(roundNumber(precio,2)+" € + iva.");
	}
	else{
		$("#smsprecio").html("Introduzca un número");
	}
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function chkmail(str) {

	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	}
function isNumber(n) {
	  return !isNaN(parseFloat(n)) && isFinite(n);
	}

function $$(id){
	return document.getElementById(id);
	
}
