function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
ajax_url = '/baby-names/request.php';

function record_vote(id)
{
	var link_td = $('#vote_for_'+id);
	var vote_td = $('#vote_total_'+id);
	link_td.text('Thanks for your vote');
	vote_td.load( ajax_url,
	  {
		  call: 'record_vote', 
		  id: id
	  },
	  function()
	  {
		  vote_td.effect('highlight');
	  });
}

function record_vote_prototype(id)
{
	var link_td = $('vote_for_'+id);
	var vote_td = $('vote_total_'+id);
	link_td.update('Thanks for your vote');
	new Ajax.Updater(vote_td, ajax_url,
	{
		parameters: 
		{
			call: 'record_vote', 
			id: id
		},
		onCreate: function()
		{
			vote_td.highlight();
		}
		
	});
}

function make_favorite(id)
{
	var link_td = $('#favorite_'+id);
	link_td.html('<a class="fave" href="/baby-names/favorites.php">Saved to Favorites</a>');
	$.post( ajax_url,
	  {
		  call: 'make_favorite', 
		  id: id
	  },
	  function()
	  {
		  link_td.effect('highlight');
	  });
	
}

function make_favorite_prototype(id)
{
	var link_td = $('favorite_'+id);
	link_td.update('<a class="fave">Saved to Favorites</a>');
	new Ajax.Request(ajax_url,
	{
		parameters: 
		{
			call: 'make_favorite', 
			id: id
		},
		onCreate: function()
		{
			link_td.highlight();
		}
		
	});
}


function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}

function validateRule(value,rule)
{
	if(rule == 'number')
	{
		return /^\d+$/.test(value);//&& value > 0;
	}
	else if(rule == 'email')
	{
		return validateEmail(email);
	}
	else if(rule == 'nospaces')
	{
		return !/\s/.test(value)
	}
	return true;
}

function validate_input_prototype(input_box)
{
	var value = $F(input_box);
  	//Check if it's filled
	if(value == '')
	{
		input_box.addClassName('invalid');
		$(input_box.id+'-error').show().highlight();
		return false;
	}
	else if(input_box.name == 'email' && !validateEmail(value))
	{
		input_box.addClassName('invalid')
		$(input_box.id+'-error').show().highlight();
		return false;
	}
	else if(input_box.readAttribute('validate') && !validateRule(value,input_box.readAttribute('validate')))
	{
		input_box.addClassName('invalid');
		$(input_box.id+'-error').show().highlight();
		return false;
	}
	else
	{
		input_box.removeClassName('invalid');
		$(input_box.id+'-error').hide();
		return true;
	}
}
function validate_input(input_box)
{
	var value = input_box.value;
	input_box = $(input_box);
  	//Check if it's filled
	if(value == '')
	{
		input_box.addClass('invalid');
		$('#'+input_box.id+'-error').show();
		return false;
	}
	else if(input_box.name == 'email' && !validateEmail(value))
	{
		input_box.addClass('invalid')
		$('#'+input_box.id+'-error').show();
		return false;
	}
	else if(input_box.attr('validate') && !validateRule(value,input_box.attr('validate')))
	{
		input_box.addClass('invalid');
		$('#'+input_box.id+'-error').show();
		return false;
	}
	else
	{
		input_box.removeClass('invalid');
		$('#'+input_box.id+'-error').hide();
		return true;
	}
}

function validate_form()
{
	//console.log(form);
	var acceptable = true, input;
	jQuery.each(required,function()
	{
		input = $('#'+this)[0];
		console.log(input);
		acceptable = validate_input(input) && acceptable;
	});
	return acceptable;
}


AdLoader = {
	list : [],
	size : 0,
	cls : 'adv',
	listAll : function()
	{
		console.log(this.list);
	},
	add: function(id, code)
	{
		var obj = {id: id, el: $(id), code: code};
		console.log('Added obj: '+obj.el);
		this.list.push(obj);
		this.size = this.list.size();
	},
	render: function()
	{
		this.list.each(function(obj)
		{
			console.log('Rendering obj: ',obj.el);
			$(obj.id).update(obj.code);
		});
		return this;
	},
	clear: function()
	{
		$$('.'+this.cls).invoke('update');
		return this;
	},
	hide: function()
	{
		$$('.'+this.cls).invoke('hide');	
	},
	show: function()
	{
		//$$('.'+this.cls).invoke('show');
		this.list.each(function(obj)
		{
			$(obj.id).setStyle({display:'block'});
		});
	},
	fetch: function()
	{
		var ads = $$('.'+this.cls);
		var el = false;
		for(var i=0; i<ads.length; i++)
		{
			el = ads[i];
			this.add(el.id,el.innerHTML);
		}
		return this;
	}
};
