var pos;
var console;
var services_ids_for_js;
var account_details_for_delivery;

if(!console) {
    console = { log: function() {} } 
}

$(document).ready(function() { 

	$('.bundle_mobile_link').click( function() {
		window.location = '/bundle/mobile';
		return false;
	});
	
	$('#mainNav ul li').hover( function() {
		$(this).children('ul').show();
		$(this).children('a').addClass('activeJS');
	}, function() {
		$(this).children('ul').hide();
		$(this).children('a').removeClass('activeJS');
	});

	$('.phone_tooltip').click( function() {
		return false;
	});
	
	$('.show_features').click( function() {
		var id = $(this).attr('id');
		$('.'+id).show();
		$(this).hide();
		$(this).siblings('.close_features').show();
		return false;
	});
	
	$('.close_features').click( function() {
		var id = $(this).attr('id');
		$('.'+id).hide();
		$(this).hide();
		$(this).siblings('.show_features').show();
		return false;
	});
	
	$('.phone_tooltip').hover( function() {
		var pos = $(this).position();
		$('#' + $(this).attr('href')).css({'left': (pos['left'] - 3), 'top' : (pos['top'] - 220)}).show();
	}, function() {
		$('#' + $(this).attr('href')).hide();
	});
	
    var cnt = $('.ColumnHeight').length;
	
	for (var i = 0; i < cnt; ++i) {
		var tmpH = 0;
		if( $('.ColumnHeight h2').eq(i).height() > tmpH ) tmpH = $('.ColumnHeight h2').eq(i).height();
		if( $('.ColumnHeight h2').eq(i+1).height() > tmpH ) tmpH = $('.ColumnHeight h2').eq(i+1).height();
		if( $('.ColumnHeight h2').eq(i+2).height() > tmpH ) tmpH = $('.ColumnHeight h2').eq(i+2).height();
		$('.ColumnHeight h2').eq(i).css('height', (tmpH + 0 ) + 'px');
		$('.ColumnHeight h2').eq(i+1).css('height', (tmpH + 0 )+ 'px');
		$('.ColumnHeight h2').eq(i+2).css('height', (tmpH + 0 )+ 'px');
	}
	
	for (var i = 0; i < cnt; ++i) {
		var tmpH = 0;
		if( $('.ColumnHeight p.bref').eq(i).height() > tmpH ) tmpH = $('.ColumnHeight p.bref').eq(i).height();
		if( $('.ColumnHeight p.bref').eq(i+1).height() > tmpH ) tmpH = $('.ColumnHeight p.bref').eq(i+1).height();
		if( $('.ColumnHeight p.bref').eq(i+2).height() > tmpH ) tmpH = $('.ColumnHeight p.bref').eq(i+2).height();
		$('.ColumnHeight p.bref').eq(i).css('height', (tmpH + 0 ) + 'px');
		$('.ColumnHeight p.bref').eq(i+1).css('height', (tmpH + 0 )+ 'px');
		$('.ColumnHeight p.bref').eq(i+2).css('height', (tmpH + 0 )+ 'px');
	}
	
	$('.button').hover( function() {
		$(this).css('background', '#FD1B14');
	}, function() {
		$(this).css('background', '#0c2577');
	});
	
	$('#t_show_1').click( function() {
		$('.bun').hide();
		$('.t_1').show();
		$('.landing a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	$('#t_show_2').click( function() {
		$('.bun').hide();
		$('.t_2').show();
		$('.landing a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	$('#t_show_3').click( function() {
		$('.bun').hide();
		$('.t_3').show();
		$('.landing a').removeClass('active');
		$(this).addClass('active');
		return false;
	});	
	$('#t_show_4').click( function() {
		$('.bun').hide();
		$('.t_4').show();
		$('.landing a').removeClass('active');
		$(this).addClass('active');
		return false;
	});	
	
	var is_display = 0;
	$('p.login a').click( function() {
		$('.login_area').show();
		is_display = 1;
		return false;
	});

	$('.login_area img').click( function() {
		$('.login_area').hide();
		is_display = 0;
		return false;
	});
	
	$('.tab1').click( function() {
		$('.header').hide();
		$('.image1').show();
		return false;
	});
	$('.tab2').click( function() {
		$('.header').hide();
		$('.image2').show();
		return false;
	});
	$('.tab3').click( function() {
		$('.header').hide();
		$('.image3').show();
		return false;
	});
	$('.tab4').click( function() {
		$('.header').hide();
		$('.image4').show();
		return false;
	});
	$('.questions p').click( function() {
		var i = $(this).attr('class');
		$('.questions .answear').hide();
		$('#' + i).show();
		return false;
	});
	var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;
	$('.ask_question').submit(function() {
		var name = $('.name-q').val();
		var mail = $('.mail-q').val();
		var phone = $('.phone-q').val();
		var question = $('.question-q').val();
		$('.info-console').fadeOut();
		if ($('.mail-q').get(0).value.search(regEmail) == -1) {
			$('h2.ask').after('<p class="info-console" style="margin: -10px 0 8px 0;"><small style="color: #60513A;">Please enter correct email address</small></p>');
			$('.mail-q').select();
			return false;
		}
		if (question == '') {
			$('h2.ask').after('<p class="info-console" style="margin: -10px 0 8px 0;"><small style="color: #60513A;">Please enter a question</small></p>');
			$('.question-q').select();
			return false;
		}
		$.post(document.location.toString() , { 
			action: 'ask_question', 
			name: name,
			mail: mail,
			phone: phone,
			question: question
		}, function(data) { 
			$('h2.ask').after('<p class="info-console" style="margin: -10px 0 8px 0;"><small style="color: #60513A;">Thank you for your message, your questions will be answered within 2 working hours.</small></p>');
			$('.name-q').attr('value', '');
			$('.mail-q').attr('value', '');
			$('.phone-q').attr('value', '');
			$('.question-q').attr('value', '');
			return false;
		});
		return false;
	});
	// ############################# //
/* 	$('#payment_types').click(function() {
		if ( parseFloat($('#checkout_basket').val().trim()) < 1 ) {
			jAlert('Your basket is empty.');
			return false;
		}
		$('input').each( function() {
			$(this).val($(this).val().trim());
		});
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;
		if ( 
			$('#checkout_address_name_number').val() == '' ||
			$('#checkout_address_name_number').val().length < 1
		) {
			jAlert('Please enter correct Address.', '', function() {
				$('#checkout_address_name_number').focus();
			});
			return false;		
		}
		if ( 
			$('#checkout_address_city_town').val() == '' ||
			$('#checkout_address_city_town').val().length < 1 
		) {
			jAlert('Please enter correct City / Town.', '', function() {
				$('#checkout_address_city_town').focus();
			});
			return false;		
		}
		if ( 
			$('#checkout_address_post_code').val() == '' ||
			$('#checkout_address_post_code').val().length > 20 ||
			$('#checkout_address_post_code').val().length < 1 
		) {
			jAlert('Please enter correct Post Code.', '', function() {
				$('#checkout_address_post_code').focus();
			});
			return false;		
		}
		if(
			$('#checkout_bussines_type_0').get(0).checked == false &&
			$('#checkout_bussines_type_1').get(0).checked == false &&
			$('#checkout_bussines_type_2').get(0).checked == false &&
			$('#checkout_bussines_type_3').get(0).checked == false
		) {
			jAlert('Please select Bussines type.');
			return false;
		}
		if(
			$('#checkout_bussines_type_0').get(0).checked == true && 
			$('#checkout_reg_number_company').val().length < 1 
		) {
			jAlert('Please select correct Reg. Number.', '', function() {
				$('#checkout_reg_number_company').focus();
			});

			return false;
		}
		if ( 
			$('#checkout_contact_firstname').val() == '' ||
			$('#checkout_contact_firstname').val().length > 30 ||
			$('#checkout_contact_firstname').val().length < 1
		) {
			jAlert('Please enter correct First Name.', '', function() {
				$('#checkout_contact_firstname').focus();
			});
			return false;		
		}
		if ( 
			$('#checkout_contact_surname').val() == '' ||
			$('#checkout_contact_surname').val().length > 30 ||
			$('#checkout_contact_surname').val().length < 1			
		) {
			jAlert('Please enter correct Surname.', '', function() {
				$('#checkout_contact_surname').focus();
			});
			return false;		
		}
		if ( 
			$('#checkout_contact_phone').val() == '' ||
			$('#checkout_contact_phone').val().length > 30 ||
			$('#checkout_contact_phone').val().length < 1			
		) {
			jAlert('Please enter correct Contact Phone.', '', function() {
				$('#checkout_contact_phone').focus();
			});
			return false;		
		}
		if ( $('#checkout_mail_address').get(0).value.search(regEmail) == -1 ) {
			jAlert('Please enter correct Email Address.', '', function() {
				$('#checkout_mail_address').focus();
			});
			return false;
		}
		if($('#checkout_client_id').size() < 1) {
			var client_id = 0;
			if ( 
				$('#checkout_contact_username').val() == '' ||
				$('#checkout_contact_username').val().length > 30 ||
				$('#checkout_contact_username').val().length < 1 
			) {
				jAlert('Please enter correct Username.', '', function() {
					$('#checkout_contact_username').focus();
				});
				return false;		
			}
			if ( 
				$('#checkout_contact_password').val() == '' ||
				$('#checkout_contact_password').val().length > 30 ||
				$('#checkout_contact_password').val().length < 1 
			) {
				jAlert('Please enter correct Password.', '', function() {
					$('#checkout_contact_password').focus();
				});
				return false;		
			}
			if ( 
				$('#checkout_contact_confirm_password').val() == '' ||
				$('#checkout_contact_confirm_password').val().length > 30 ||
				$('#checkout_contact_confirm_password').val().length < 1 
			) {
				jAlert('Please enter correct Confirmed password.', '', function() {
					$('#checkout_contact_confirm_password').focus();
				});
				return false;		
			}
			if($('#checkout_contact_password').val() != $('#checkout_contact_confirm_password').val()) {
				jAlert('Password that you specified do not match');
				return false;
			}
			if ( 
				$('#checkout_contact_security_question').val() == '' ||
				$('#checkout_contact_security_question').val().length > 240 ||
				$('#checkout_contact_security_question').val().length < 1 
			) {
				jAlert('Please enter correct Security question.', '', function() {
					$('#checkout_contact_security_question').focus();
				});
				return false;		
			}
			if ( 
				$('#checkout_contact_security_answer').val() == '' ||
				$('#checkout_contact_security_answer').val().length > 240 ||
				$('#checkout_contact_security_answer').val().length < 1 
			) {
				jAlert('Please enter correct Secutiry answer.', '', function() {
					$('#checkout_contact_security_answer').focus();
				});
				return false;		
			}
		} else
			var client_id = $('#checkout_client_id').val();

		post = {
			action: 'checkout_protx', 
			checkout_company_name: $('#checkout_company_name').val().trim(),
			checkout_address_name_number: $('#checkout_address_name_number').val().trim(),
			checkout_address_city_town: $('#checkout_address_city_town').val().trim(),
			checkout_address_post_code: $('#checkout_address_post_code').val().trim(),
			checkout_notes: $('#checkout_notes').val().trim(),
			checkout_reg_number_company: $('#checkout_reg_number_company').val().trim(),
			checkout_contact_firstname: $('#checkout_contact_firstname').val().trim(),
			checkout_contact_surname: $('#checkout_contact_surname').val().trim(),
			checkout_contact_phone: $('#checkout_contact_phone').val().trim(),
			checkout_mail_address: $('#checkout_mail_address').val().trim(),
			checkout_total_price: parseFloat($('#checkout_total_price').val()),
			checkout_contact_username: $('#checkout_contact_username').val().trim(),
			checkout_contact_password: $('#checkout_contact_password').val().trim(),
			checkout_contact_security_question: $('#checkout_contact_security_question').val().trim(),
			checkout_contact_security_answer: $('#checkout_contact_security_answer').val().trim(),
			checkout_client_id: client_id
		}

		for(i = 0; i < services_ids_for_js.length; i++) {
			var sifj = services_ids_for_js[i];

			if($('#checkout_company_name_' + sifj).size() > 0) 			post["checkout_company_name_" + sifj] 			= $('#checkout_company_name_' + sifj).val().trim();
			if($('#checkout_address_name_number_' + sifj).size() > 0) 	post["checkout_address_name_number_" + sifj]	= $('#checkout_address_name_number_' + sifj).val().trim();
			if($('#checkout_address_city_town_' + sifj).size() > 0) 	post["checkout_address_city_town_" + sifj] 		= $('#checkout_address_city_town_' + sifj).val().trim();
			if($('#checkout_address_post_code_' + sifj).size() > 0) 	post["checkout_address_post_code_" + sifj] 		= $('#checkout_address_post_code_' + sifj).val().trim();
			if($('#checkout_notes_' + sifj).size() > 0) 				post["checkout_notes_" + sifj] 					= $('#checkout_notes_' + sifj).val().trim();

			if($('#checkout_combination_notes_' + sifj).size() > 0) 	post["checkout_combination_notes_" + sifj] 		= $('#checkout_combination_notes_' + sifj).val().trim();

			if($('#checkout_repair_care_level_' + sifj).size() > 0) 	post["checkout_repair_care_level_" + sifj] 		= $('#checkout_repair_care_level_' + sifj).val().trim();
		}
		
		$('.keep_number').each( function() {
			post[$(this).attr('id')] = $(this).val().trim();
		});

		$.post(document.location.toString(), post, function(data) { 
			if ( data == 'OK' )
				document.location = '/payment-types';
			else
				jAlert(data);
		});
		return false;
	}); */
	
	$('#order_with_sage_pay').click( function() {
		document.location = '/orders';
	});
	
	$('#order_with_interbacs').click( function() {
		stop_saving = false;
		$('#interbacs_form input[type=text]').each( function() {
			if($(this).val().trim() == '') {
				label = $(this).parents('label:first').children('span').html();

				if(!label)
					label = 'Sort code';

				jAlert('Please fill "' + label + '" field');

				stop_saving = true;

				return false;
			}

		});

		if(stop_saving == true) 
			return false;

		if( $('#sort_code_1').val().trim().length != 2 ||
			$('#sort_code_2').val().trim().length != 2 ||
			$('#sort_code_3').val().trim().length != 2 ) {

			jAlert('Please enter correct "Sort code"');

			stop_saving = true;

			return false;
		}

		if( $('#account_number').val().trim().length < 7 ||
			$('#account_number').val().trim().length > 10) {

			jAlert('Please enter correct "Account number"');

			stop_saving = true;

			return false;
		}

		if(stop_saving == true) 
			return false;

  		post = {
			action: 'checkout_interbacs', 
			BankBuildingSociety: 	$('#bank_building_society').val().trim(),
			AccountName: 			$('#account_name').val().trim(),
			SortCode: 				$('#sort_code_1').val().trim() + $('#sort_code_2').val().trim() + $('#sort_code_3').val().trim(),
			AccountNumber: 			$('#account_number').val().trim()
		}

		$.post(document.location.toString(), post, function(data) { 
			if ( data == 'OK' )
				document.location = '/checkout/interbacs-success';
			else
				if(IsNumeric(data))
					document.location = '/checkout/interbacs-failed/reason:' + data;
				else
					jAlert(data);
		});
	
		return false;
	});
	// ############################# //
});

function IsNumeric(sText)

{
   var ValidChars = "0123456789-";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/,"");
};

function formValidateForgotten() {
	var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;
 
	if (document.getElementById('fpemail').value < 1) {
		jAlert("Please type your email");
           return false;
	}
	if (document.getElementById('fpemail').value.search(regEmail) == -1) {
		jAlert("Please enter valid e-mail!");
           return false;
	}
}

function formValidateReset() {
	var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;
 
	if (document.getElementById('fpemail').value < 1) {
		jAlert("Please type your email");
           return false;
	}
	if (document.getElementById('fpemail').value.search(regEmail) == -1) {
		jAlert("Please enter valid e-mail!");
           return false;
	}
	if ( 
		$('#rkey').val() == '' ||
		$('#rkey').val().length < 1			
	) {
		jAlert('Please enter correct Key.', '', function() {
			$('#rkey').focus();
		});
		return false;		
	}
			if ( 
				$('#rpassword').val() == '' ||
				$('#rpassword').val().length > 30 ||
				$('#rpassword').val().length < 1 
			) {
				jAlert('Please enter correct Password.', '', function() {
					$('#rpassword').focus();
				});
				return false;		
			}
			if ( 
				$('#rpassword_confirm').val() == '' ||
				$('#rpassword_confirm').val().length > 30 ||
				$('#rpassword_confirm').val().length < 1 
			) {
				jAlert('Please enter correct Confirmed password.', '', function() {
					$('#rpassword_confirm').focus();
				});
				return false;		
			}
			if($('#rpassword').val() != $('#rpassword_confirm').val()) {
				jAlert('Password that you specified do not match');
				return false;
			}
}
