
/*	Main functions
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

jQuery(document).ready(
	function()
	{
		
		jQuery(".formForValidation").submit( function() {
			return validateIt();
		});
			

		/*	Menu dots
			- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
			
			jQuery("#menu a").wrap("<div>");
			jQuery("#menu > li:first").addClass("first-child");
			jQuery("#menu > li:last").addClass("last-child");
			
			
			
		/*	Menu fx
			- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
			
			jQuery("#menu li:not(.active) ul").hide();
			
			jQuery('#calc .cancel_button > input:button').click(function() {
				jQuery(this).parent().parent().find('#calc_lending').val('');
				jQuery(this).parent().parent().find('#calc_repayment').val('');
				jQuery(this).parent().parent().find('#calc_interest').val('');
				jQuery(this).parent().parent().find('#payments').val('');
				jQuery(this).parent().parent().find('#repayment').val('');
				jQuery(this).parent().parent().find('#interest').val('');
			});

			
			jQuery('#calc .submit_button > input:button').click(function() {
				var loan = parseFloat(jQuery(this).parent().parent().find('#calc_lending').val());
				var periodOfRepayment = parseFloat(jQuery(this).parent().parent().find('#calc_repayment').val());
				var interestRate = parseFloat(jQuery(this).parent().parent().find('#calc_interest').val())/100;
				
				var payments = periodOfRepayment*12;
				var repayment = loan*(interestRate/12*(Math.pow(1+(interestRate/12), (periodOfRepayment*12)))) / (Math.pow((1+interestRate/12),(periodOfRepayment*12))-1);
				var interest = loan*interestRate/12;

				
				jQuery(this).parent().parent().find('#payments').val(payments.toFixed(0));
				jQuery(this).parent().parent().find('#repayment').val(repayment.toFixed(2));
				jQuery(this).parent().parent().find('#interest').val(interest.toFixed(2));
			});
			
			jQuery("#menu > li").click(function(){
				if (jQuery(this).children("ul").html()) {
					jQuery("#menu li ul").slideUp("normal");
					jQuery("#menu li").removeClass("active");
					jQuery(this).children("ul:hidden").parent().addClass("active");
					jQuery(this).children("ul:hidden").slideDown("normal");
					return false;
				}
				return true;
			});

			jQuery("#menu > li > ul > li").click(function(){
				location = jQuery(this).find("a").attr("href");
				exit();
			});

			
			if (((jQuery.browser.msie==true)&&(jQuery.browser.version>6))||(jQuery.browser.msie==false)) {
				jQuery("#menu > li").hover(function(){
					jQuery(this).children("div").animate({
												  textIndent: '10px'
												}, "fast");
				},function(){
				  jQuery(this).children("div").animate({
												  textIndent: '0'
												}, "fast");
				});
			}

		/*	Styling IE forms
			- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
			if (((jQuery.browser.msie==true)&&(jQuery.browser.version==6))) {
				jQuery("input[type='text'], input[type='password']").addClass("ieform_txt");
				jQuery("select").addClass("ieform_select");
				jQuery(":checkbox").addClass("iecheckbox");
				
				jQuery("#calc input[type='text']").removeClass("ieform_txt");
				jQuery("#calc input[type='text']").addClass("iecalc_txt");
			}

		/*	Image carousel
			- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

		jQuery("body:not(#home) #logos").css("display","block");
		
		function mycarousel_initCallback(carousel)
		{

			// Disable autoscrolling if the user clicks the prev or next button.
			carousel.buttonNext.bind('click', function() {
				carousel.startAuto(0);
			});
		
			carousel.buttonPrev.bind('click', function() {
				carousel.startAuto(0);
			});
	
			// Pause autoscrolling if the user moves with the cursor over the clip.
			carousel.clip.hover(function() {
				carousel.stopAuto();
			}, function() {
				carousel.startAuto();
			});
			
			carousel.clip.click(function() {
			});
				
		};
		
		jQuery(document).ready(function() {
			jQuery('body:not(#home) #imageCarousel').jcarousel({
				auto: 3,
				wrap: 'last',
				scroll: 1,
				initCallback: mycarousel_initCallback
			});
		});


	});


/*	Validation
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

function validateIt() {
	var error;
	var errorCount = 0;
	jQuery('span').remove('.formError');
	jQuery('input').each(function(i) {
		error = '';
		
		
		if(jQuery(this).is('.val_email') && !(isEmail(jQuery(this).val()))) {
			error = 'This is not a valid email address.';
			errorCount++;
		}
		
		if(jQuery(this).is('.val_digits') && !(isNumeric(jQuery(this).val()))) {
			error = 'Only digits allowed.';
			errorCount++;
		}
		
		if(jQuery(this).is('.val_letters') && !(isLetters(jQuery(this).val()))) {
			error = 'Only letters allowed.';
			errorCount++;
		}
		
		if(jQuery(this).is('.val_nonempty') && (isRequired(jQuery(this).val()))) {
			error = 'This field is required, you cannot leave it empty.';
			errorCount++;
		}
		if(error.length > 0) {
			if(!jQuery(this).parent().is('.wrap')) {
				jQuery(this).wrap("<span class='wrap'></span>");
			}
			jQuery(this).parent().append('<span class="formError">'+error+'</span>');
		}

		
	});
	
	if(errorCount > 0) {
		return false;
	}
	return true;
		
}


// returns true if the string
function isRequired(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isRequired(str))
		return true;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
	if(isRequired(str))
		return true;
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains letters
function isLetters(str){
	if(isRequired(str))
		return true;
	var re = /[^a-zA-Z ]/g
	if (re.test(str)) return false;
	return true;
}

var urlAddress = "http://www.simplymortgageadvice.com";
var pageName = "Simply Mortgage Advice"; 
function addToFavorites() { 
	if (window.external) {
		window.external.AddFavorite(urlAddress,pageName)
	} 
	else { 
		alert("Sorry! Your browser doesn't support this function.");
	}
}









	