var spamSumAnswer;
var demoRequestFormSubmitted = false;

$(document).ready(function() {
	
	$('.demoRequestForm').stop().animate({ height: 'hide' },{duration: 0, easing: 'swing'});
	
	$('.closeBtn').click(CloseRequestForm);
	$('.contactFormButton').click(OpenRequestForm);
	
	$("#demoSubmit").click(function(event){
		event.preventDefault();
		ValidateForm();
	});
	
});

function formSuccess()
{
	newSpamSum();
	
	window.location.href = "http://www.coins-global.com/asiapacific/page.htm?cpi_id=1089";
}

function OpenRequestForm()
{
	newSpamSum();
	$('.demoRequestForm').stop().animate({ height: 'show' },{duration: 500, easing: 'swing'});
	$(".formSuccess").slideUp(0);
	
	$(".demoFormWrapper").slideDown(0);
	
	$('.request').removeClass('buttonOff').addClass('buttonOn');
	$('.request a').attr('href', 'javascript:CloseRequestForm();');
	
	$('html, body').animate({
	scrollTop: $(".demoRequestForm").offset().top
	}, 500);

}			

function CloseRequestForm()
{
	newSpamSum();
	$('.demoRequestForm').stop().animate({ height: 'hide' },{duration: 500, easing: 'swing'});
	$('.request').removeClass('buttonOn').addClass('buttonOff');
	$('.request a').attr('href', 'javascript:OpenRequestForm();');
}	

function newSpamSum()
{
	var figure1 = Math.round(Math.random() * 5);
	var figure2 = Math.round(Math.random() * 4);
	spamSumAnswer = figure1 + figure2;
	$(".demoFormWrapper label.spamSum p").empty();
	$(".demoFormWrapper label.spamSum p").append('What is ' + figure1 + ' + ' + figure2 + '?');
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	
	var emailID = document.contactForm.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address");
		emailID.focus();
		newSpamSum();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		newSpamSum();
		return false;
	}
	
	var newSpamSumAnswer = parseInt(document.contactForm.spamSumAnswer.value);
	
	if(newSpamSumAnswer != spamSumAnswer)
	{
		alert("Please answer the sum correctly so we can prevent spam");
		newSpamSum();
		return false;
	}
	
			
	var name = $("input#name").val();
	var email = $("input#email").val();
	var position = $("input#position").val();
	var company = $("input#company").val();
	var enquiry = $("#enquiry").val();

	var dataString = 'name='+ name + '&email=' + email + '&position=' + position + '&company=' + company + '&enquiry=' + enquiry;
		
	if(!demoRequestFormSubmitted)
	{
		demoRequestFormSubmitted = true;
		$.ajax({
			type: "POST",
			url: "contactform.php",
			data: dataString,
			success: function() {
				formSuccess();
			}
		});
	}
    return false;

 }

