/*
Studio 2 Online (www.s2o.co.uk)
-----------------------------------------------------------
This file contains functions used in leisureshack.co.uk

Author: andrew mcmahon
Date: 2006-01-18
*/

jQuery.noConflict();

(function ($) {
	$(function () {

		$('#payment').submit(function(event) {
			if ($('#termsandconditions:checked').length === 0) {
				alert('You must tick to confirm that you have understood and accepted our terms and conditions.');
				event.preventDefault();
			}
		});

		$('.please-call-me').change(function () {
			var isChecked = $(this).is(':checked'),
			container = $(this).parents('.callforprice');
			if (isChecked) {
				$('.prefer-phone-call', container).show();
			} else {
				$('.prefer-phone-call', container).hide();
			}
		});

		$('.close a').click(function (e) {
			$('.callforpricingouter').hide();
			$(this).parents('.callforprice').fadeOut();
			e.preventDefault();
		});

		$('.callforpricingouter').css({
			background: '#000',
			height: $('html, body').height(),
			left: 0,
			position: 'absolute',
			opacity: '.7',
			top: 0,
			width: $(window).width(),
			zIndex: 50
		}).click(function () {
			$('.callforpricingouter').hide();
			$('.callforprice:visible').fadeOut();
		}).hide();

		$('.show-pricing-form').click(function (e) {
			var container = $(this).parents('.list_item');
			if (container.length < 1) {
				container = $(this).parents('#content');
			}
			$('.callforpricingouter').show();
			$('.callforprice', container).css({
				top: 200 + $(window).scrollTop()
			}).fadeIn();
			e.preventDefault();
		});

		$('.callforpricingform').submit(function (e) {
			var self = $(this),
			html = self.html(),
			button = $('button', self);
			$.post('/cms/callforpricing.php', self.serialize(), function () {
				self.html('<p>Please check your email for our pricing information.</p>');
				setTimeout(function () {
					$('.close a').click();
					setTimeout(function () {
						self.html(html);
						button.attr('disabled', '')
					}, 1000);
				}, 3000);
			});
			button.attr('disabled', 'disabled').after(' Loading...');
			e.preventDefault();
		});

	});
}(window.jQuery));

function CheckNewsletterForm(){
	alertmessage = '';

	if (document.header_newsletter.name.value == '') {
		alertmessage += "Please enter your name.\n";
	}

	if (document.header_newsletter.email_address.value == '') {
		alertmessage += "Please enter your email address.\n";
	}else{
		if(!isEmailAddr(document.header_newsletter.email_address.value)){
			alertmessage += "Your email address is invalid.\n";
		}
	}

	if(alertmessage != ''){
		alert(alertmessage);
		return false;
	}
	return true;
}


function in_array(needle, haystack){
	for (var i in haystack){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

function isNumeric(input){
	var allowed_chars = new Array(1,2,3,4,5,6,7,8,9,0);
	for(var i = 0; i < input.length; i++){
		if(!in_array(input.charAt(i), allowed_chars)){
			return false;
		}
	}
	return true;
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function empty(input){
	if(input == ""){
		return true;
	}

	if(typeof(input) == "boolean"){
		if(input != true && input != false){
			return true;
		}
	}

	if(typeof(input) == "number"){
		if(input == 0){
			return true;
		}
	}

	if(typeof(input) == "string"){
		if(input == ""){
			return true;
		}
	}

	if(typeof(input) == "object"){
		if(input.length == 0){
			return true;
		}
	}
	return false;
}

function getErrorMessage(input){
	var output = "";
	for(i in input){
		output += input[i] + "\n";
	}
	return output;
}

function isEmailAddr(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
	return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
	return false;
	}

	if (str.indexOf(" ")!=-1){
	return false;
	}

	return true;
}

