

// js for ""
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}


$(document).ready(function(){

	formElement(); // forms

	try {
		var x = getCookie('contacta');
		if (x == '1') {
			$('.form-email').show();
			$('.form-phone').hide();
			$('.contact-method option:eq(0)').attr('selected', 'selected')

		}
		if (x == '2') {
			$('.form-email').hide();
			$('.form-phone').show();
			$('.contact-method option:eq(1)').attr('selected', 'selected');
		}

	}
	catch(err) {

	}

	var strgurl=window.location.pathname;
	if (strgurl.indexOf('get-started.html') > 0) {
	try {
			var x = getCookie('contactb');
			if (x == '1') {
				$('.form-email-b').show();
				$('.form-phone-b').hide();
				$('.contact-method-getstarted option:eq(0)').attr('selected', 'selected')

			}
			if (x == '2') {
				$('.form-email-b').hide();
				$('.form-phone-b').show();
				$('.contact-method-getstarted option:eq(1)').attr('selected', 'selected');
			}

		}
		catch(err) {

		}
	}



	$('.am_submit').click(function(){
		var am1 = $('input:radio[name=am1]:checked').val();
		var am2 = $('input:radio[name=am2]:checked').val();
		var am3 = $('input:radio[name=am3]:checked').val();
		var am4 = $('input:radio[name=am4]:checked').val();
		if (am1 && am2 && am3 && am4){
			if (am1 == 'yes' && am2 == 'yes' && am3 == 'yes' && am4 == 'yes'){
				$('.answer_yes').show();
				$('.answer_no').hide();
			}
			else {
				$('.answer_no').show();
				$('.answer_yes').hide();
			}
		}
		else {
			alert('Answer to all questions!');
		}

		return false;
	});

	$('.how_submit').click(function(){
		var how1 = $('#how1').val();
		var how2 = $('#how2').val();

		var d = new Date();
		var curr_year = d.getFullYear();
		
		if (how1 && how2 && isNumber(how1) && isNumber(how2) && how1 > 0 && how2 > 0 && how2 <= curr_year){
			var v1 = how1 * 0.25;
			var v2 = curr_year - how2;
			var v3 = v1 * 0.1;
			var v4 = v2 * v3;

			var v5 = v1 + v4;
			$('.total .value').html('<b>&pound;' + v5.toFixed(2)+ '</b>');
			$('.total').show();
			$('.total').css('display','inline');
		} else {
			alert('Fill all fields!');
			$('.total').css('display','none');
		}
		return false;
	});

	$(".contact-method").change(function () {
		var method = $(".contact-method option:selected").val();

		if (method == 'email'){
			$('.form-email').show();
			$('.form-phone').hide();
			setCookie('contacta','1',1);

		}
		else {
			$('.form-email').hide();
			$('.form-phone').show();
			setCookie('contacta','2',1);
		}
	});

	// get started form
	$(".contact-method-getstarted").change(function () {
		var method = $(".contact-method-getstarted option:selected").val();

		if (method == 'email'){
			$('.form-email-b').show();
			$('.form-phone-b').hide();
			setCookie('contactb','1',1);

		}
		else {
			$('.form-email-b').hide();
			$('.form-phone-b').show();
			setCookie('contactb','2',1);
		}
	});

	function setCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}


	var url = document.location.toString();
	if (url.match('#')) {
	  var anchor = url.split('#')[1];
	  if (anchor == 'phone'){
			$(".contact-method option").filter(function() {
				return $(this).text() == 'Email';
			}).attr('selected', 'a');

			$(".contact-method option").filter(function() {
				return $(this).text() == 'Phone';
			}).attr('selected', 'selected');

			$('.form-email').hide();
			$('.form-phone').show();
	  }
	  if (anchor == 'call_back'){
			$(".contact-method option").filter(function() {
				return $(this).text() == 'Email';
			}).attr('selected', 'a');

			$(".contact-method option").filter(function() {
				return $(this).text() == 'Phone';
			}).attr('selected', 'selected');

			$('.form-email').hide();
			$('.form-phone').show();

			var border = 'no';
			var i = 0;



	  }
	}

	$('.form-email').submit(function(){
		var message = '';

		if ($('.form-email .your_name').val() == "" || $('.form-email .your_name').val() == "Tell us your name") message += " - Your Name\n";
		if ($('.form-email .your_email').val() == "" || $('.form-email .your_email').val() == 'What\'s your email address?') message += " - Your Email Address\n";
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    var address = $('.form-email .your_email').val();
	    if(reg.test(address) == false) {
		     message += " - Your Email Address\n";
	    }
		
		if ($('.form-email .your_phone').val() == "" || $('.form-email .your_phone').val() == 'Tell us your phone number') message += " - Your Phone Number\n";
		
		if (message) {
			alert('Please fill in the following fields correctly:\n\n' + message);
			return false;
		}

		return true;
	});

	$('.form-phone').submit(function(){
		var message = '';

		if ($('.form-phone .your_name').val() == "" || $('.form-phone .your_name').val() == "Tell us your name") message += " - Your Name\n";

		if ($('.form-phone .your_phone').val() == "" || $('.form-phone .your_phone').val() == 'Tell us your phone number') message += " - Your Phone Number\n";

		if (message) {


			alert('Please fill in the following fields correctly:\n\n' + message);
			return false;
		}

		return true;
	});

});
// form element
function formElement(){
	jQuery('input[type=text], textarea').each(function(index){
		jQuery(this).addClass('text_input');
		var defVal = jQuery(this)[0].defaultValue;
		if(jQuery(this).val().length === 0)jQuery(this).val(defVal);
		if(jQuery(this).val() != defVal)jQuery(this).addClass('fill');
	});
	jQuery('input[type=password]').each(function(index){
		jQuery(this).addClass('passw_input').attr('id','passw-'+index);
		var defVal = jQuery(this)[0].defaultValue;
		if(jQuery(this).val().length === 0) jQuery(this).val(defVal);
		if(jQuery(this).val() != defVal) jQuery(this).addClass('fill');
		else if(!jQuery.browser.msie) document.getElementById('passw-'+index).type = 'text';
	});


	jQuery('.text_input').focus(function(){
		var defVal = jQuery(this)[0].defaultValue;
		jQuery(this).addClass('fill');
		if(jQuery(this).val() == defVal)jQuery(this).val('');
	});
	jQuery('.text_input').blur(function(){
		var defVal = jQuery(this)[0].defaultValue;
		if(jQuery(this).val().length === 0) jQuery(this).removeClass('fill').val(defVal);
	});


	jQuery('.passw_input').focus(function(){
		Id = jQuery(this).attr('id');
		var defVal = jQuery(this)[0].defaultValue;
		if(jQuery(this).val() == defVal) {
			jQuery(this).val('').addClass('fill');
			if(!jQuery.browser.msie) document.getElementById(Id).type = 'password';
		}
	});

	jQuery('.passw_input').blur(function(){
		Id = jQuery(this).attr('id')
		var defVal = jQuery(this)[0].defaultValue;
		if(jQuery(this).val().length === 0) {
			jQuery(this).removeClass('fill').val(defVal);
			if(!jQuery.browser.msie) document.getElementById(Id).type = 'text';
		}
	});
}


