var vo = 'overlay1';
var doo = 'overlay1';
var timer1 = null;
var timer2 = null;


function splashIn( splashId ) {
	if (timer1 != null) {
		clearTimeout(timer1);
	}
	if (vo != splashId) {
		timer1 = setTimeout(
			function() {	
				$('#splash').css("background-image", $('#'+vo).css("background-image"));
				$('#'+vo).fadeOut('fast');
				vo = splashId;
				$('#'+splashId).fadeIn('fast');	
			}, 250);
	}
}

function splashOut() {
	
	if (timer2 != null) {
		clearTimeout(timer2);
	}
	if (timer1 != null) {
		clearTimeout(timer1);
	}
	if (vo != doo) {
		timer2 = setTimeout(
			function () {
				$('#splash').css("background-image", $('#'+vo).css("background-image"));
				$('#'+vo).fadeOut('slow');
				$('#'+doo).fadeIn('slow');
				vo = doo;
			}, 5000);
	}
}

function clearTextarea() {
	if ($('#contactMessage').val() == 'Your message here...') $('#contactMessage').val('');
}

function submitContactForm() {
	$('#ajaxLoader').show();
	document.getElementById('contactForm').submit();
}

function updateBuyForm() {

	var vat = true;
	var total = 0.0;

	$('#bl').val( removeNonNumeric($('#bl').val()) );
	$('#rl').val( removeNonNumeric($('#rl').val()) );
	$('#ro').val( removeNonNumeric($('#ro').val()) );

	if ($('#country').val() == 'United Kingdom') {
		$('#vatrow').show();
		$('.usd').hide();
		$('.eur').hide();
		$('.gbp').show();
		var blp = $('#blGbpCost').val();
		var rop = $('#roGbpCost').val();
		var rlp = $('#rlGbpCost').val();
	} else if ($('#country').val() == 'Austria' || $('#country').val() == 'Belgium' || $('#country').val() == 'Bulgaria' || $('#country').val() == 'Cyprus' || $('#country').val() == 'Czech Republic' || $('#country').val() == 'Denmark' || $('#country').val() == 'Estonia' || $('#country').val() == 'Finland' || $('#country').val() == 'France' || $('#country').val() == 'Germany' || $('#country').val() == 'Greece' || $('#country').val() == 'Hungary' || $('#country').val() == 'Ireland' || $('#country').val() == 'Italy' || $('#country').val() == 'Latvia' || $('#country').val() == 'Lithuania' || $('#country').val() == 'Luxembourg' || $('#country').val() == 'Malta' || $('#country').val() == 'Netherlands' || $('#country').val() == 'Poland' || $('#country').val() == 'Portugal' || $('#country').val() == 'Romania' || $('#country').val() == 'Slovakia' || $('#country').val() == 'Slovenia' || $('#country').val() == 'Spain' || $('#country').val() == 'Sweden'  ) {
		$('#vatrow').show();
		$('.usd').hide();
		$('.eur').show();
		$('.gbp').hide();
		if ($('#vatNumber').val() != '') {
			$('#vattotal').html('0.00');
			vat = false;
		}
		var blp = $('#blEurCost').val();
		var rop = $('#roEurCost').val();
		var rlp = $('#rlEurCost').val();
	} else {
		$('#vattotal').html('0.00');
		vat = false;
		$('#vatrow').hide();
		$('#vatNumber').val('');
		$('.usd').show();
		$('.eur').hide();
		$('.gbp').hide();
		var blp = $('#blUsdCost').val();
		var rop = $('#roUsdCost').val();
		var rlp = $('#rlUsdCost').val();
	}
	
	total = ($('#bl').val() * blp) + ($('#rl').val() * rlp) + ($('#ro').val() * rop);
	$('#subtotal').html(twoDecimals(total));
	
	if(vat) {
		vattotal = (total / 100)*17.5;
		$('#vattotal').html(twoDecimals(vattotal));
	} else {
		vattotal = 0;
	}
	total = total + vattotal;
	$('#total').html(twoDecimals(total));
}

function submitBuyform() {
	$('#ajaxLoader').show();
	document.getElementById('buyForm').submit();
}

function removeNonNumeric( strString ) {

	var strValidCharacters = "1234567890";
	var strReturn = "";
	var strBuffer = "";
	var intIndex = 0;
	for( intIndex = 0; intIndex < strString.length; intIndex++ ) {
          strBuffer = strString.substr( intIndex, 1 );
          if( strValidCharacters.indexOf( strBuffer ) > -1 ) {
               strReturn += strBuffer;
          }
	}
	return strReturn;
	
}

function twoDecimals(n) {
   var s = "" + Math.round(n * 100) / 100
   var i = s.indexOf('.')
   if (i < 0) return s + ".00"
   var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
   if (i + 2 == s.length) t += "0"
   return t
}
