$(document).ready(function() {
	/*$('input[@type=text]').focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value
			}
		});
	});*/

	// fix png for IE
	$('img[@src$=.png], div').ifixpng();

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// remove any borders on last LI element
	$("#navigator> ul, #content .inner ul, #page-title ul, #fullside ul, ul.columns3").each(function(){
		 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",marginRight:"0",paddingRight:"0"});
	});

	// submenu
	//$("#navigator ul ul, #navigator ul ul ul").hide();
	$("#navigator ul li").hover(
		function(){ menu($(this).attr("id")+"ul","show"); },
		function(){ menu($(this).attr("id")+"ul","hide"); }
	);
	
	$("ul").prev("p").css({paddingBottom:0,marginBottom:0});	
	$("#left-large ul").css("margin-bottom","16px");

	// accordion
//	if ( $.browser.version!="6.0" ) 
{
		$('#acc1> div').not(".opened").hide();
		$('#acc1> h3').click(function() {
			var $nextDiv = $(this).next();
			var $visibleSiblings = $nextDiv.siblings('div:visible');
			if ($visibleSiblings.length ) {
				$visibleSiblings.slideUp('fast', function() {
					$nextDiv.slideToggle('fast');
				});
			} else {
			   $nextDiv.slideToggle('fast');
			}

			return false;
		});
	}
	
	// toggle optional fields
	$("select").change(function(){
		if ( $(this).val() == "Other" && $(this).siblings("input.optional") ){
			$(this).siblings("input.optional").css("visibility","visible");
		} else if ( $(this).siblings("input.optional") ) {
			$(this).siblings("input.optional").css("visibility","hidden");
		}
	});

	// validate forms
	if ( $("form.tovalidate").length ) {
		$("form.tovalidate").validate({
			rules:{
				excessDetails: {
					required: "#policyExcessYes:checked"
				},
				injurySustained: {
					required: "#personalInjuryYes:checked"
				},
				hireChargesDetails: {
					required: "#hireChargesYes:checked"
				},
				earningsLossDetails: {
					required: "#earningsLossYes:checked"
				},
				recoveryChargesDetails: {
					required: "#recoveryChargesYes:checked"
				},
				useLossDetails: {
					required: "#useLossYes:checked"
				},
				otherLossesDetails: {
					required: "#otherLossesYes:checked"
				},
				hear_other:{
					required: function(){
						return $("#hear").val() == "Other";
					}
				}
			}
		});
	}
	
	// toggle descriptions
	$("#hidedesc p").hide();
	$("#hidedesc h3").each(function(){
		$(this).hover(
			function(){ $(this).css("cursor","hand"); },
			function(){ $(this).css("cursor","default"); }
		).click(
			function(){
				$(this).next("p").toggle();
				return false;
			}
		);
	});
	
	// Terms and condition check
	//$("form.tovalidate .submit").attr("disabled","disabled");
	$("#tac").click(function(){
		if ( $("#tac:checked").length == 1 ) {
			$("form.tovalidate .submit").attr("disabled","");
		} else {
			$("form.tovalidate .submit").attr("disabled","disabled");
		}
	});
	
	// open popup window
	$("a.popup-tac").click(function(){
		window.open($(this).attr("href"),'TAC','menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,width=460,height=640,top=60,left=60');
		return false;
	});
});

if ( $("form.tovalidate").length ) {
	$("form.tovalidate").validate();
}

var detect = navigator.userAgent.toLowerCase();
var os_ = navigator.platform.toLowerCase();
var browser,thestring;

if (checkIt('safari')) browser = "Safari"
else browser = "not Safari"

if ( os_ == 'macppc' || os_ == 'macintel' ) os = "Mac"
else os = "not Mac"

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function menu(id,status) {
	switch (status) {
		case 'show':
			$("#"+id).show();
			break;
		case 'hide':
			$("#"+id).hide();
			break;
 		}
	}

var email_pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
var decimal_pattern = /^[0-9]{1,10}(\.?[0-9]*)$/;
var date_pattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
var str_pattern = /^(.*)$/;
var int_pattern = /^[0-9]+$/;
function validate_form()
{
	var msg = "";
	var err = 0;
	$(".required").each(function(){
		if ($(this).hasClass('terms') && !$(this).attr('checked')) {			
			msg += "You should agree the terms and conditions\n";
			err++;
		}
		else if ($(this).hasClass('decimal') && !$(this).val().match(decimal_pattern)) {
			$(this).css("border","1px solid red");
			msg += "Complete the field \""+$(this).attr('title')+"\" with a number\n";
			err++;
		} else if ($(this).hasClass('date') && !$(this).val().match(date_pattern)) {
			$(this).css("border","1px solid red");
			msg += "\"Complete the field \""+$(this).attr('title')+"\" with a date [dd/mm/yyyy]\n";
			err++;
		} else if ($(this).hasClass('int') && !$(this).val().match(int_pattern)) {
			$(this).css("border","1px solid red");
			//msg += "Complete the field \""+$(this).attr('title')+"\" with a integer\n";
			msg += "\""+$(this).attr('title')+"\" needs to be a number\n";
			err++;
		} else {
			if ($.trim($(this).val()).length < 1)
			{
				$(this).css("border","1px solid red");
				msg += "Complete the field \""+$(this).attr('title')+"\"\n";
				err++;
			} else if ($(this).attr('title')=="Email" && $(this).attr("class","required")) {
				if (!$(this).val().match(email_pattern))
				{
					$(this).css("border","1px solid red");
					msg += "Complete the field \""+$(this).attr('title') +"\" with a valid email address\n";
					err++;
					
				} else {
					$(this).css("border-color","#64AED3");
				}
			} else {
				if ($(this).hasClass('date_months')) {
					var yearObj = $("#expiry_date_year");
					var date = new Date();
					var currentMonth = date.getMonth() + 1;
					var currentYear = date.getFullYear();
					if ($(this).val() < currentMonth && yearObj.val() <= currentYear) {
						$(this).css('border','1px solid red');
						msg += "Expiry Date cannot be set in the past";
						err++;
					}
				} else {
					$(this).css("border-color","#64AED3");
				}
			}
		}
	});
	if (err > 0)
	{
		alert("There are errors in your form!\n"+msg);
		return false;
	} else {
		return true;
	}
}
$(function(){
	$(".required").keyup(function(){
		if ($(this).hasClass('decimal') && !$(this).val().match(decimal_pattern)) {
			$(this).css("border","1px solid red");
		} else if ($(this).hasClass('date') && !$(this).val().match(date_pattern)) {
			$(this).css("border","1px solid red");
		} else if ($(this).hasClass('int') && !$(this).val().match(int_pattern)) {
			$(this).css("border","1px solid red");
		} else if ($(this).hasClass('string') && !$(this).val().match(str_pattern)) {
			$(this).css("border","1px solid red");
		} else if ($(this).hasClass('email') && !$(this).val().match(email_pattern)) {
			$(this).css("border","1px solid red");
		} else {
			$(this).css("border","1px solid #64AED3");
		}
	});
	$(".required").change(function(){
		if ($.trim($(this).val()) < 1) {
			$(this).css("border","1px solid red");
		} else {
			$(this).css("border","1px solid #64AED3");
		}
	});
});