$(document).ready(function(){
	//function for contact form dropdown
	function contact() {
		if ($("#contactForm").is(":hidden")){
			$("#contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.7"});
			$("#backgroundPopup").fadeIn("slow"); 
		}
		else{
			$("#contactForm").slideUp("slow");
			$("#backgroundPopup").fadeOut("slow");  
		}
	}
	 
	//run contact form when any contact link is clicked
	$(".contact").click(function(){contact()});
	$(".contact_footer").click(function(){contact(); $("#e-mail").focus(); return false;});
	//animation for same page links #
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($(this.hash).length) {
				$(this).click(function(event) {
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();			   
					$('html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				});
			}
		}
	});



   //submission scripts
  $('.contactForm').submit( function(){
		//statements to validate the form	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('e-mail');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.cform.name.value == "") {
			$('.name-missing').show();
		} else {$('.name-missing').hide();}	
		if (document.cform.message.value == "") {
			$('.message-missing').show();
		} else {$('.message-missing').hide();}		
		if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.message.value == "")){
			return false;
		} 
		
		if ((document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.message.value != "")) {
			//hide the form
			$('.contactForm').hide();
		
			//show the loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//send the ajax request
			$.post('/ajax-script/contact-mail.php',{name:$('#name').val(),
							  email:$('#e-mail').val(),
							  message:$('#message').val()},
		
			//return the data
			function(data){
			  //hide the graphic
			  $('.bar').css({display:'none'});
			  $('.loader').append(data);
			});
			
			//waits 2000, then closes the form and fades out
			setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);
			
			//stay on the page
			return false;
		} 
  });
	//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": document.documentElement.clientHeight 
	});  
	
	//START News Letter scripts
  	$('.newsletterForm').submit( function(){
		//statements to validate the form	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('news_e_mail');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.newsform.news_name.value == "") {
			$('.name-missing').show();
		} else {$('.name-missing').hide();}	
		if ((document.newsform.news_name.value == "") || (!filter.test(email.value)) ){
			return false;
		} 
		
		if ((document.newsform.news_name.value != "") && (filter.test(email.value)) ) {
			//hide the form
			$('.newsletterForm').hide();
			
			//show the loading bar
			$('.news_errorcode').css({display:'none'});
			$('.news_loader').append($('.news_bar'));
			$('.news_bar').css({display:'block'});
			
			//send the ajax request
			$.post('/ajax-script/newsletter-mail.php',{name:$('#news_name').val(),
							  email:$('#news_e_mail').val(),btn:$("#btn_click").val()},
		
			//return the data
			function(data){ news_process(data) }, "xml");
			
			//stay on the page
			return false;
		} 
  	});
	//END News Letter scripts	
	function news_process(response)
	{
		var result_flag;
		result_flag =	response.getElementsByTagName('result_flag')[0].childNodes[0].firstChild.nodeValue;
		if(result_flag == "true")
		{
			if(response.getElementsByTagName('result_flag')[0].childNodes[1].firstChild.nodeValue=="false")
				window.location.reload();
			else
				window.location.reload();
		}
		else
		{
			//Hide the loading bar
			$('.news_loader').append($('.news_bar'));
			$('.news_bar').css({display:'none'});	
			if(response.getElementsByTagName('result_flag')[0].childNodes[1].firstChild.nodeValue=="true")
			{
				var error_text = '';	
				var total_error	=	response.getElementsByTagName('error_field')[0].childNodes.length;
				for(var i=0;i<total_error;i++) {
					var field_name	=	response.getElementsByTagName('error_field')[0].childNodes[i].nodeName;
					var field_value	=	response.getElementsByTagName('error_field')[0].childNodes[i].firstChild.nodeValue;
					if(i>0)
						error_text = error_text + ' --- ' + field_value;
					else
						error_text = field_value;
				}
				//error_text = '<span style="font-family:Arial; size:11px; color:#ff0000">'+error_text+'</span>';
				$('.news_errorcode').css({display:'block'});
				$('.news_errorcode').html(error_text).fadeIn("slow");
				$('.newsletterForm').show();
			}
			else
			{
				if(response.getElementsByTagName('result_flag')[0].getElementsByTagName('message')[0].firstChild.nodeValue==1)
				{
					var error_text = 'Your have successfully subscribed to the vivcasino.co.uk Newsletter.<br><br>Regards, <br>Support Team <br>vivcasino.co.uk</span>';
				}
				else
				{
					var error_text = 'You have successfully unsubscribed to the vivcasino.co.uk Newsletter<br><br>Regards, <br>Support Team <br>vivcasino.co.uk';
				}
				$(".news_loader").addClass("news_loader_error");
				$('.news_loader').html(error_text).fadeIn("slow");	
				
				$("input#news_name").val('');
				$('#news_e_mail').val('');
			}
		}
	}
});
function news_button(val)
{
	document.newsform.btn_click.value = val;
}