$(document).ready(function() {
	// setup superdots counter
	setTimeout('increment()', interval); 

	// setup quote scroller 
	changequote(); 
	
	processtabs();
	
	if($('.flash').length>0)
	{
		showmodal($('.flash')); 
		setTimeout('hideflash()', 3000); 
	}
	
	$('#contactform').live('submit', (function(evt) {
		
		var formvalid = true;
		// check values
		$(this).children('input.required, select.required, textarea.required').each(function() {
			
			var invalid = false;
			if($(this).val() == '') {
				invalid = true; 
			}
			else {	
				if(($(this).attr('name') in inputreplaceritems)) {
					if($(this).val() == inputreplaceritems[$(this).attr('name')]) {
						invalid = true; 
					}; 
				}
			}

			if(invalid) {
				$(this).addClass('invalid'); 
				formvalid = false; 
			}
			else {
				$(this).removeClass('invalid'); 
			} 
		}); 
		
		if(!formvalid) {
			evt.preventDefault(); 
			alert('In order to help us respond efficiently to your contact, please fill in all required fields. Thank you');
			return false; 
		}
	}));
}); 

function hideflash()
{
	hidemodal(); 
}
var currentquote = 0;
function changequote()
{
	$('#quote').fadeOut(500, function()
						{
							$(this).load('/utils/quote/'+currentquote, {rnd:Math.random()}, function()
									{
										$(this).fadeIn(500); 
									});
						}); 
	setTimeout('changequote()', 10000); 
}

function processtabs()
{
	$('.tabs ul.tablist li.tablistitem a.tablistlink').live('click', function(evt)
			{
				evt.preventDefault(); 
				var target = $(this); 
				
				var parent = target.parents('.tabs'); 
				
				//switch tabcontent states
				var tabcontent = parent.find('#'+target.attr('href'));
				parent.find('.tabcontent').not(tabcontent).hide(); 
				tabcontent.show(); 
				//switch tab states.
				parent.find('a.tablistlink').not(target).removeClass('selected'); 
				target.addClass('selected'); 
			}); 
}

var counter = parseInt(getCookie('dotcounter', 0)); 
var dotspersec = 30;
var interval = 789;
var loop = 0; 
	
$.strPad = function(i,l,s) {
	var o = i.toString();
	if (!s) { s = '0'; }
	while (o.length < l) {
		o = s + o;
	}
	return o;
};
	 
function increment()
{
	counter += parseInt(dotspersec * (interval/1000));
	if(counter>99999999)
		counter = 0; 

	$('#dotcounter').text($.strPad(counter, 8));
	setTimeout('increment()', interval);  
	if(loop++>40)
	{
		loop = 0; 
		setCookie('dotcounter', counter, null); 
	}
}	

function getCookie(c_name, defaultval)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return defaultval;
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function setupMultiSelect()
{
	$("#multiselect").multiselect();
}
