$(document).ready(function() {
	
	var validator = $("#addcomercio").validate({
		rules: {
			//--p1
		empresa: "required",
			email_comercio: {
				email: true
				},
			calle1: "required",
			numero:{
				required: true,
				number: true
				},
			ciudad: "required",
			codigo_postal: "required",
			rubro: "required",
			//--p2
			nombre: "required",
			apellido: "required",
			e_mail: {
				email: true,
				required: true
			},
			area_con: "required",
			telefono_con: "required",
			//--p3
			pago: "required"
			
		},
		messages: {
			//--p1
			empresa: "Nombre requerido",
			email_comercio: {
				minlength: "E-mail invalido",
				email:"E-mail invalido"
			},
			calle1: " ",
			numero: "Dirección requerida",
			ciudad: "Ciudad requerida",
			codigo_postal: "Código postal requerido",
			//--p2
			nombre: "Nombre requerido",
			apellido: "Apellido requerido",
			e_mail: "E-mail invalido",
			area_con: "",
			telefono_con: "Teléfono requerido",
			rubro: "Elija un rubro",
			//--p3
			pago: "Seleccione una forma de pago."
			
		}
		// the errorPlacement has to take the table layout into account
		// specifying a submitHandler prevents the default submit, good for the demo
		// set this class to error-labels to indicate valid fields
		/*success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}*/
	});

	
	$("#calle1").blur(function(){//mostrar dir en map-		
		if($("#calle1").val().trim()!="" && $("#numero").val().trim()!="" && $("#numero").valid())
		{
			$("#mapcalle").addLoader("../img/ajax-loader.gif");
			$("#mapcalle").useLoader("mapPoint?Calle="+$.URLEncode($("#calle1").val()+" "+$("#numero").val()));
		}
	});
	$("#numero").blur(function(){//mostrar dir en map-
		if($("#calle1").val().trim()!="" && $("#numero").val().trim()!="" && $("#numero").valid())
		{
			$("#mapcalle").addLoader("../img/ajax-loader.gif");
			$("#mapcalle").useLoader("mapPoint?Calle="+$.URLEncode($("#calle1").val()+" "+$("#numero").val()));
		}	
	});
	
	$("#red").treeview({//rubros-
			animated: "fast",
			collapsed: true,
			unique: true,
			persist: "location",
			toggle: function() {
				window.console && console.log("%o was toggled", this);
			}
		});
		
		settings = {//redondeo-
          tl: { radius: 8 },
          tr: { radius: 8 },
          bl: { radius: 8 },
          br: { radius: 8 },
          antiAlias: true,
          autoPad: true,
          validTags: ["div"]
      }
	   $('.wizard-ayuda').corner(settings);
	
	$("#paso4 *").tooltip({//tooltops diescripcion
			track: true,
			delay: 300,
			showURL: false,
			fixPNG: true,
			showBody: " - ",
			extraClass: "pretty fancy",
			top: -15,
			left: 5
		});
	
});
function loadnext(divout,divin){
	var isFill =true;
	if (divout<divin && divout==1)//paso1 
	{
	 if (!$("#empresa").valid())isFill=false;
	 if (!$("#rubro").valid())isFill=false;
	
	 if (!$("#email_comercio").valid() && $("#email_comercio").val() != "")isFill=false;
	 if (!$("#calle1").valid())isFill=false;
	 if (!$("#numero").valid())isFill=false;
	 if (!$("#ciudad").valid())isFill=false;
	 if (!$("#codigo_postal").valid())isFill=false;
	 
	}
	else if (divout<divin && divout==2)
	{
	 
	 if (!$("#nombre").valid())isFill=false;
	 if (!$("#apellido").valid())isFill=false;
	 if (!$("#e_mail").valid() && $("#e_mail").val() != "")isFill=false;
	 if (!$("#area_con").valid())isFill=false;
	 if (!$("#telefono_con").valid())isFill=false;
	  
	}
	else if (divout<divin && divout==3)
	{
		if (!$("#pago").valid())isFill=false;
	}
	else if (divout<divin && divout==4)
	{
		
	}
	if (!isFill)return;
	$("." + divout).hide();
	$("." + divin).fadeIn("fast");
}
function UpdateShowLimit()
{
$('#tagspermit').html('('+tagsAdded+' de '+tagsMax+')');
}
function ShowMessage(m)
{
		$('#dialog').remove();
		$('#mensaje').append('<div id="dialog" title="Cercamio.com"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'+m+'</div>');
		$("#dialog").dialog({
			bgiframe: true,
			resizable: false,
			height:250,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				"Aceptar": function() {
					$(this).dialog('close');
				}
			}
		});

}
function TagsServicioCompatibilidad(idservAnt,tmptagsMax)
{
	if(tagsAdded > tagsMax)
	{ 
		var nEliminar = tagsAdded-tagsMax;
		ShowMessage('Usted ha ingresado <strong>'+tagsAdded+'</strong> tags y el limite del servicio que acaba de seleccionar es de <strong>'+tagsMax+'</strong> tags. Para poder utilizar este este servicio debe eliminar <strong>'+nEliminar+'</strong> tags. <br><center><img src="../img/wizard/ayudagrafica.gif" boder="0"></center>');
		$('input[name="servicio"]')[idservAnt].checked = true;
		 tagsMax=tmptagsMax;
		return false;
	}else if (tagsMax<tmptagsMax)
	{
		
		var aTextTagsTMP  = new Array();
		aTextTagsTMP.length=tagsMax;
		
		for(var i=0;i<tmptagsMax;i++)
		{
			if (aTextTags[i]!=null)
			for(var e=0;e<tagsMax;e++)
			{
				if(aTextTagsTMP[e]==null)
				{
					aTextTagsTMP[e]=aTextTags[i];
					break;
				}
			}		
		}
		aTextTags =aTextTagsTMP;
	}
	return true;
}
function SetComunProp(id)
{
	switch(id)
	{
		case '0':
			$(".panelpagos").hide();
			$("#pago").val("0");
		break;
		case '1':
			$(".panelpagos").fadeIn('slow');
			$("#pago").val("1");
		break;
		case '2':
			$(".panelpagos").fadeIn('slow');
			$("#pago").val("1");
		break;
	}
	/*
	if (id=='0')
	{
		$(".panelpagos").hide();
		$("#pago").val("0");
		$("#pago_info").val("No Cobrar");
		$("#rpago1").attr("disabled",true);
		$("#rpago2").attr("disabled",true);
		$("#rpago3").attr("disabled",true);
		$("#rpago4").attr("disabled",true);
		
	}
	else
	{
		$(".panelpagos").fadeIn('slow');
		$("#pago").val('');
		$("#rpago1").removeAttr("disabled",true);
		$("#rpago2").removeAttr("disabled",true);
		$("#rpago3").removeAttr("disabled",true);
		$("#rpago4").removeAttr("disabled",true);
		if ($("#rpago1").is(':checked'))
		{
			$("#pago").val($("#rpago1").val());
		}
		if ($("#rpago2").is(':checked'))
		{
			$("#pago").val($("#rpago2").val());
		}
		if ($("#rpago3").is(':checked'))
		{
			$("#pago").val($("#rpago3").val());
		}
		if ($("#rpago4").is(':checked'))
		{
			$("#pago").val($("#rpago4").val());
		}
		
	}
	*/
}
var ServicioActivo;
function setServicio(id) {
	ServicioActivo = id;
		switch(id)
		{
			case '0':
			tmptagsMax=tagsMax;
			tagsMax=5;
			if (!TagsServicioCompatibilidad(selectedServicio,tmptagsMax))return false;
			selectedServicio=0;
			aTextTags.length = tagsMax;
			UpdateShowLimit();
			//--- esto es un chiquero porque aun no esta en la db
			//
			// si, me pareció un chiquero! parq! 090930
			//
			$(".plan").html("Gratuito");$("#plan_id_info").val("Gratuito");
			$(".CosteMensual").html("0.00");$("#plan_costo").val("0.00");
			$(".CantidadMes").html("0");$("#plan_minimo").val("0");
			$(".CostoTotal").html("0.00");$("#plan_total").val("0.00");
			SetComunProp(id);
			$("#descrip").attr("maxlength",0);
			$("#descrip").val('');
			//$("#logo").attr("disabled",true);
			$("#imgEjemplo").html('<img src="../img/cm2.png" border="0" style="border: Solid #CCC 2px">');
			//$("#imgs_comercio").attr("disabled",true);
			
			break;
			case '1':
			tmptagsMax=tagsMax;
			tagsMax=15;
			if (!TagsServicioCompatibilidad(selectedServicio,tmptagsMax))return false;
			selectedServicio=1;
			aTextTags.length = tagsMax;	
			UpdateShowLimit();			
			//--- esto es un chiquero porque aun no esta en la db
			$(".plan").html("Básico");$("#plan_id_info").val("Básico");
			$(".CosteMensual").html("20.00"); $("#plan_costo").val("20.00");
			$(".CantidadMes").html("1 Mes"); $("#plan_minimo").val("1 Mes");
			$(".CostoTotal").html("20.00"); $("#plan_total").val("20.00");
			$(".MensajeAyuda").html('Promo <strong>Fiestas 2010</strong>:<br />Contratando 6 meses, pagás solo <strong>$60</strong>!<br />Sí! Te bonificamos el <strong>50%</strong>!!!<br /><br />Válido hasta el 31 de Enero de 2011.');
			SetComunProp(id);
			$("#descrip").removeAttr("maxlength");
			
			//$("#logo").attr("disabled",true);
			$("#imgEjemplo").html('<img src="../img/cm3.png" border="0" style="border: Solid #CCC 2px">');
			//$("#imgs_comercio").removeAttr("disabled");
			//$("#imgs_comercio").MultiFile({max:2});
		
			break;
			case '2':
			tmptagsMax=tagsMax;
			tagsMax=30;
			if (!TagsServicioCompatibilidad(selectedServicio,tmptagsMax))return false;
			selectedServicio=2;
			aTextTags.length = tagsMax;
			UpdateShowLimit();
			//--- esto es un chiquero porque aun no esta en la db
			$(".plan").html("Avanzado");$("#plan_id_info").val("Avanzado");
			$(".CosteMensual").html("50.00");$("#plan_costo").val("50.00");
			$(".CantidadMes").html("1 Mes"); $("#plan_minimo").val("1 Mes");
			$(".CostoTotal").html("50.00");$("#plan_total").val("50.00");
			$(".MensajeAyuda").html('Promo <strong>Fiestas 2010</strong>:<br />Contratando 3 meses, pagás solo <strong>$75</strong>!<br />Sí! Te bonificamos el <strong>50%</strong>!!!<br /><br />Válido hasta el 31 de Enero de 2011.');
			SetComunProp(id);
			$("#descrip").removeAttr("maxlength");
			//$("#logo").removeAttr("disabled");
			$("#imgEjemplo").html('<img src="../img/cm4.png" border="0" style="border: Solid #CCC 2px">');
			//$("#imgs_comercio").removeAttr("disabled");
			//$("#imgs_comercio").MultiFile(5);
			break;
			case '3':
			tmptagsMax=tagsMax;
			tagsMax=60;
			if (!TagsServicioCompatibilidad(selectedServicio,tmptagsMax))return false;
			selectedServicio=3;
			aTextTags.length = tagsMax;
			UpdateShowLimit();
			//--- esto es un chiquero porque aun no esta en la db
			$(".plan").html("Premium");$("#plan_id_info").val("Premium");
			$(".CosteMensual").html("$100");$("#plan_costo").val("$100,00");
			$(".CantidadMes").html("2 Meses");$("#plan_minimo").val("2 Meses");
			$(".CostoTotal").html("$200");$("#plan_total").val("$200,00");
			SetComunProp(id);
			//$("#descrip").removeAttr("disabled");
			//$("#logo").removeAttr("disabled");
			//$("#imgs_comercio").removeAttr("disabled");
			//$("#imgs_comercio").MultiFile(10);
			break;
		}
		
		
}
function addTag()
{
	for(var i=0; i< tagsMax;i++)
	{
		if (aTextTags[i]==null)
		{
			if (jQuery.inArray("t"+i, aTextTags)==-1)
			{	
				
				aTextTags[i]="t"+i;
				$('#dtags').append('<br id="nl'+i+'"><input id="t'+i+'" name="t'+i+'" value="" type="text" maxlength="25" size="30"><a id="bt'+i+'" href="javascript:removeTag('+i+')"><img src="../img/wizard/deltag.gif" border="0"></a>');
				tagsAdded++;
				
				$('#t'+i).focus();
				UpdateShowLimit();
				return ;
			}
			else
			{
				for(var e=0; e < tagsMax;e++)
				{
					if (jQuery.inArray("t"+e, aTextTags)==-1)
					{
						
						aTextTags[i]="t"+e;
						
						$('#dtags').append('<br id="nl'+e+'"><input id="t'+e+'" name="t'+e+'" value="" type="text" maxlength="25" size="30"><a id="bt'+e+'" href="javascript:removeTag('+e+')"><img src="../img/wizard/deltag.gif" border="0"></a>');
						tagsAdded++;
					
						$('#t'+e).focus();
						UpdateShowLimit();
						return;
					}
					
				}
			}
		}
	}
	ShowMessage("Supero el numero de tags. Si desea agregar mas, seleccione en la tabla de servicios uno superior.");
}
function removeTag(idr)
{
	id=jQuery.inArray("t"+idr, aTextTags);
	aTextTags[id]=null;
		$('#t'+idr).fadeOut("slow");
		$('#nl'+idr).fadeOut("slow");
		$('#bt'+idr).fadeOut("slow", function bt()
									{
									$('#t'+idr).remove();
									$('#nl'+idr).remove();
									$('#bt'+idr).remove();
									});
	tagsAdded--;
	UpdateShowLimit();
}
function IsPermit(id)
{
switch (ServicioActivo)
{
	case '0':
		return MessagePermit(id)
	break;	
	case '1':
		if (id !="descrip")
		return MessagePermit(id)
	break;	
	case '2':
		
	break;	
}
	
}
function MessagePermit(id)
{
	switch(id)
	{
		case 'descrip':
		ShowMessage("Para agregar descripción a su comercio, primero elija el servicio Básico o Avanzado.");
		return false;
		break;
		case 'logo':
			ShowMessage("Para agregar el logo de su comercio, primero elija el servicio Avanzado.");
		return false;
		break;	
	}
}
