// JavaScript Document

$('.col1-tabs a').click
(
 	function ()
		{
			var n = $('.col1-tabs a').index(this);
			$(this).addClass('col1-sel').siblings().removeClass('col1-sel');
			$('.tslide').animate({left:(-280*n)});
			$('.bolpane:eq('+n+')').show().siblings('div.bolpane').hide();
			this.blur();
			return false;
		}
);

//form submission

function showform(from,isReset) //show form if unsuccessful
{
	var form = $(from).parents('form');
	if (isReset) $(form).trigger('reset');
	$('.fields',form).fadeIn('normal',function () {$('.rtb:first',form).trigger('focus');} );
	$('.response',form).empty();
	return false;
}

function procJSON(json,form)
{
	
	var rs = json.response;
	switch (json.action)
	{
		case 'success': rs += "<br /><br /><p><a href='#' onclick='return showform(this,true)'>Submit again?</a></p>"; break;
		case 'retry': rs += "<br /><br /><p><a href='#' onclick='return showform(this,false)'>Submit again?</a></p>"; break;
		case 'reload': self.location.reload(); break;
		case 'nomore': break;
	}
	$('.response',form).html(rs);
	$('.response a',form).trigger('focus');
	
}

var sboxes = $("a[rel^='shadowbox']").length;

    var options = {
		loadingImage:		root+'media/images/loading.gif',
        resizeLgImages:     true,
        handleUnsupported:  'remove',
		overlayOpacity:		0.6,
        keysClose:          ['c', 27] // c or esc
    };

if (sboxes>0)
    Shadowbox.init(options);

var novalidate = false;

$('.form, .sform').submit(
function ()
	{


			var form = this;
	
			if (!novalidate) {
			
			if (!$(this).hasClass('sform'))
				{
					if (!validate(form)) return false;
				}
			else
				{
					if ($('.nalert').length!=$('.ncheck').length) 
						{
						if (!$('div[class=nalert nerror]:first',form).length)
							$('div[class=nalert]:first',form).parent().find('input:first').trigger('focus');
						else
							$('div[class=nalert nerror]:first',form).parent().find('input:first').trigger('focus');
						return false;
						}
				}
			
			}

			novalidate = false;
			
			var dt = $(this).serializeArray();
			var action = root + 'main/post';
			$('.fields',this).slideUp('fast');
			$('.response',this).html("<div class='load'>Processing, please wait...</div>");
			
				$.ajax({
				   type: 'POST',
				   url: action,
				   data: dt,
				   dataType: 'json',
				   success: function(data){procJSON(data,form);}
				 });

			return false;
	}
);

function goChat()
{
	novalidate = true;
	$('.form').trigger('submit');
	var curl = "http://server.iad.liveperson.net/hc/5774993/?cmd=file&file=visitorWantsToChat&site=5774993&byhref=1"; 
	nw = window.open(curl,'chatlive','top=120,left=530,height=410,width=474,status=yes');	
	nw.focus();
	return false;
}

var rfr = cookieVal('referrer');

if (!rfr)
{
	if (document.referrer != '')
		saveCookie('referrer',document.referrer);	
}

//saveCookie('lastpage',document.referrer);

$('.form input.tb').each
(
 	function ()
		{
			$(this).attr('autocomplete','off');
			this.value = this.title;
			var dv = this.value;
			this.dv = dv;
			$(this).focus
				(
				 	function ()
						{
							if (this.value==dv)
								this.value = '';
						}
				);
			$(this).blur
				(
				 	function ()
						{
							if (this.value=='') this.value = dv;
						}
				);				
		}
);

$('.form textarea.ta').each
(
 	function ()
		{
			var dv = this.innerHTML;
			this.dv = dv;
			$(this).attr('autocomplete','off');
			
			$(this).focus
				(
				 	function ()
						{
							
							if (this.innerHTML==this.dv)
								$(this).html('');
						}
				);
			$(this).blur
				(
				 	function ()
						{
							//if (this.innerHTML=='') this.innerHTML = this.dv;
						}
				);				
		}
);
