$(document).ready(function(){


	//DD_belatedPNG.fix('img,div,li');
	$("#search_word")
		.bind("click", function()
		{
			if($(this).val() == 'Sök')
			{
				$(this).val('');
			}
		})
		.bind("blur", function() 
		{
			if($(this).val() == '')
			{
				$(this).val('Sök');
			}
		});
		if (!$('body').hasClass('preView')) {
			$(".greybox").click(function(){
				$("#GB_window").css("background-color","#aaaaaa").prepend('<span class="closeMe">St&auml;ng</span>');
				$(".closeMe").click(GB_hide);
			});
			
		}
	


		/*FAQ*/
		$('#faq p').hide();
	    $('#faq h4').click(function(){
	        $(this).toggleClass('selected').next('p').toggle();
	    });
		
		/*
		 * Populates the app_compensation selectbox with accurate values.
		 */
		$("#app_social1, #app_smoker").blur(function() {
			$("select#app_compensation").empty();
			var smoker = 0;
			
			if ($("#app_smoker") != undefined && $("#app_smoker") != null) {
				if ($("#app_smoker").val() == 'Yes') {
					smoker = 1;
				}
			}

			$.getJSON("/System/Templates/Sites/182/php/compensation.php",
				{product_type: $("#product_type").val(), ssnr: $("#app_social1").val(), smoker: smoker},
				function(data){
					var options = "";
//					data = trim(data);
					for (var i = 0; i < data.length; i++) {
						options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
					}
					$("#app_compensation").html(options);
					return false;
				}
			);
		});
			//Even and odd rows in tables
	/* if Environment is NOT Editor */
		$("table tr").mouseover(function() {
			$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");
		});
		$("table tr:even").addClass("even");
		$("table tr:odd").addClass("odd");
		var th = $("table tr:first-child");
		$(th).removeClass('even')
		$(th).addClass("th");
		$('table tr td:first-child').addClass('first');
		
		
	/*breadcrumb style*/
	$('#breadcrumb li:not(:last)').append(' &raquo;');
	
	/*questions and rewards hide and show*/
	//$('#survey_form #reward').hide()
	//$('#survey_form #questions a.button').click(function(){
	//		$('#survey_form #questions').hide('slow');
	//		$('#survey_form #reward').show('slow');
	//		return false;
	//})
	
	//surveyform validation
	//function validateSurvey(){
	//	$('#survey_form .selectfield').each(function(){

	//		validateSurveySelect();
	//	});
	//	
	//}
	//$('#survey_form button#submit').attr('disabled','disabled');
	function validateSurveySelect(theID){
		var selectVal = $('#'+theID).val();
	    if(selectVal == '' || selectVal == null && !$(this).hasClass('error')){
		    //console.log(selectVal);
		    $('#'+theID).focus().addClass('error');
		    
		    
	    }else if(selectVal == '' || selectVal == null && $(this).hasClass('error')){
	    	
	    } else{
	    	$('#'+theID).removeClass('error');
	    	//alert('Inga fel');
	    } 
		
	}
	$('#survey_form .selectfield').change(function(){
		var thisID = $(this).attr('id');
			validateSurveySelect(thisID);
	});

	$('#survey_form button#submit').click(function(e){
		//alert('klick på submit');
		//e.preventDefault();
		//validateSurvey();
		$('#survey_form .selectfield').each(function(){
			var thisID = $(this).attr('id');
			//alert(thisID);
			validateSurveySelect(thisID);
		});
		
		if ($('#survey_form select').hasClass('error')) {
			$('#survey_form select.error:eq(0)').focus();
			//add the error message
			if ($('#survey_form fieldset :first').is(':not(p)')) {
				$('#survey_form fieldset').prepend('<p class="error message">Fält markerade med * måste fyllas i.</p>');
			}
			
			//console.log('false');
			return false;
		} else{
			if ($('#survey_form fieldset :first').is('p')) {
				$('#survey_form fieldset p,.error').remove();
			}
			$('form#survey_form').submit();
			//console.log('true');
			return true;
		}

		
	});
	
	
	
	var is_submit_button_clicked = false;
	$('.application #formSubmit').click(function(event) {
		if (!is_submit_button_clicked) {
			is_submit_button_clicked = true;
			return true;
		} else {
			event.preventDefault();		
			return false;
		}
	});	
	//function only_once(){
	//		$.cookie(
	//		'only_once',
	//		'1',
	//		{ 
	//			expires: 365,
	//			//path: '/', 
	//			domain: 'aceforsakringar.se'
	//		});
	//}
	//if ($('#main h2').hasClass('only_once')) {
	//	only_once();
	//}
	
});

