$(document).ready(function() {
						   
	$('.png').ifixpng();
	
	toggle_field('.lightgrey');
	
	// search
	$('.btn_search').click(function(){
		$(this).parent().submit();
		return false;
	});
	
	// captcha
	$('#question_mark').mouseover(function(e){
		$('.explaination').show();
	});
	
	$('#question_mark').mouseout(function(e){
		$('.explaination').hide();
	});
	
	// fast answer form
	if($("#fast_answer_form").html() != undefined){
		$('#get_answers').click(function(){
			$('#fast_answer_form').submit();
			return false;
		});
		
		$('#fast_answer_form').ajaxForm({
			beforeSubmit:function(){
				$('#fast_answer_form :input').attr('disabled', 'disabled');
				$('#process').show();
			},
			dataType:'json',
			success:function(res){
				if(res.status==1){
					$('#process').hide();
					$('#step2_fail').show();
				}else{
					$('#step1').hide();
					$('#process').hide();
					$('#step2_fail').hide();
					$('#step2_success').show();
					pageTracker._trackPageview("/fastanswers/submit");
				}
				$('#fast_answer_form :input').removeAttr('disabled');
			}
		});
	}
	
	// enquiry form
	ajax_form();
	
	// carousel - sliding bar
	if($("#carousel_area").html() != undefined){
		$('#carousel_main').show();
		$(".carousel").jCarouselLite({
		   btnNext: "#arr_right",
		   btnPrev: "#arr_left",
		   visible: 1,
		   scroll: 1,
			 auto: 2500,
			 speed: 800
		 });
	}
	$('.print_page').click(function(){
		var link = self.location.href;
		var action = 'print_page';//$(this).attr('href');
		if(link.substr(-1, 1) != '/')
		{
			action = '/' + action;
		}
		self.open(link+action);
		return false;
	});
	
	// pop up - share with friend
	if($(".cbox").html() != undefined){
		$('.cbox').cBox({
			width:525, 
			height:500, 
			callback:function(){
				if($.browser.msie && $.browser.version < 7)
				{
					$('.png').ifixpng();
				}
			}
		});
	}
	$('#ddl_industry').change(function(){
		if($(this).val() != '')
		{
			self.location.href = '/' + $(this).val();
		}
	});
									  
});

function validate(){
	if($('#question').val() == ''){
		alert("Please type in your question.");
		return false;
	}
	if(!verifyEmail($('#email').val())){
		return false;
	}
	return true;
}

function verifyEmail(email){
	var status = true;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (email.search(emailRegEx) == -1){
          alert("Please enter a valid email address.");
          status = false;
     }
     return status;
}

function toggle_field(el)
{
	$(el).focus(function(){
		$(this).select().removeClass('lightgrey');
	});
	$(el).blur(function(){
		$(this).addClass('lightgrey');
	});
}

function refreshCaptcha(id)
{
	var src = $(id).attr('src');
	$(id).attr('src', src+'/'+new Date().getTime());
}

function ajax_form(){
	if($('#submit').val() != undefined){
		$('#enquiry_form').ajaxForm({
			beforeSubmit:function(){
				$('#submit').attr('disabled','disabled');
				$('#status').html('<img src="/images/admin/loading.gif" /><span class="success">Processing your request, please wait...</span>');
			},
			dataType:'json',
			success:function(response){
				if(response.status==1)
				{
					$("#status").html('');
					$('#submit').removeAttr('disabled');
					
					for(i=0;i<response.message.length;i++)
					{
						if(response.message[i].error!=""){
							$("#lbl_"+response.message[i].id).css("color","#ed1848");
							$("#status").append(response.message[i].error);
						}else{
							$("#lbl_"+response.message[i].id).css("color","#333333");
						}
					}
					
					return false;
				}
				else if(response.status==0)
				{
					$('#enquiry_form #status').html('<span class="success">'+response.message+'</span>');
					msg = response.html;
					setTimeout("$('html, body').animate({scrollTop: '0px'}); $('#enquiry_form').html('<strong>'+msg+'</strong>');", 1500);
					pageTracker._trackPageview("/enquiry/submit");
				}
				else
				{
					$('#enquiry_form').html('<span class="error">'+response.message+'</span>');
				}
			}
		});
	}
}

