$(document).ready(function(){
	
	// Register Box ----------------------------------------------------------------------------------------------
	
	// Show Registration Box
	$("#register").show();
	
	// Setup Validation
	var validator = $("#registerForm").validate({
			rules: {
				ContactForm_company: { required: true, checkCompany: true },
				ContactForm_name: { required: true, checkName: true },
				ContactForm_email: { required: true, checkEmail: true, email: true }
			},
			messages:{ 
            	ContactForm_company: "",
            	ContactForm_name: "",
            	ContactForm_email: ""
        	}
	});
	
	//Extend Validator
	$.validator.addMethod("checkCompany", function(value) {
		if (value == 'Company') {
			return false;
		} else {
			return true;
		}
	});
	$.validator.addMethod("checkName", function(value) {
		if (value == 'Name') {
			return false;
		} else {
			return true;
		}
	});
	$.validator.addMethod("checkEmail", function(value) {
		if (value == 'Email') {
			return false;
		} else {
			return true;
		}
	});
	$.validator.addMethod("checkQuestion", function(value) {
		if (value == 'Your Question') {
			return false;
		} else {
			return true;
		}
	});
	
	// Confirm Form
	function confirmForm() {
		validator.form( )	
		if ( validator.numberOfInvalids() == 0 ) {
			$("#registerFormInitial").hide();
			$("#registerFormConfirm").show();
			$("#replaceName").html( $("#ContactForm_name").val() );
			$("#replaceCompany").html( $("#ContactForm_company").val() );
			$("#replaceEmail").html( $("#ContactForm_email").val() );
		}
	}
	
	// Register User
	function registerUser( thisName , thisCompany , thisEmail, thisFneh ) {
		$("#divcon").hide();
		var xmlhttp=null;
			xmlhttp=getxmlhttp();
			thisName    = escape(thisName);
                        thisName    = thisName.replace(/%20/g, "_");

                        thisCompany = escape(thisCompany);
                        thisCompany = thisCompany.replace(/%20/g, "_");

                        var url = "http://www.britainandirelandevent.co.uk/traderegister?action=register&name=" + thisName + "&company="+ thisCompany + "&email="+ thisEmail;

//                        var url = "http://sputcom3.co.uk/bobidev/traderegister?action=register&name=" + thisName + "&company="+ thisCompany + "&email="+ thisEmail;
                        $("#registerFormConfirm").html( '<h1>Thank you...</h1><p>We are redirecting you to some important questions, please wait a moment...</p>' );
                        xmlhttp.open("GET", url, true);

                        xmlhttp.onreadystatechange=function() {
                                if (xmlhttp.readyState==4) {
                                    response = xmlhttp.responseText;
                                    $("#registerFormConfirm").html( response );
                                }
                            
			}
			xmlhttp.send(null);

//if (xmlhttp.readyState==4) {
//                                var url = "/scripts/send_email/index.php?action=register&name=" + thisName + "&company="+ thisCompany + "&email="+ thisEmail;
//                                xmlhttp.open("GET", url, true);
//                                if (xmlhttp.readyState==4) {
//                                    response = xmlhttp.responseText;
//                                    $("#registerFormConfirm").html( response );
//                                }
//                            }
	}
	
	// Register Button
	$(".button_register").click(function(event){
		confirmForm();
	});
	
	// Confirm Button
	$(".button_confirm").click(function(event){
		thisName = $("#ContactForm_name").val();
		thisCompany = $("#ContactForm_company").val();
		thisEmail = $("#ContactForm_email").val();
                thisFneh = $("#ContactForm_fneh").val();
		registerUser( thisName , thisCompany , thisEmail, thisFneh );
	});
	
	// Back Button				
	$("#button_back").click(function(event){
		$("#registerFormInitial").show();
		$("#registerFormConfirm").hide();
	});
	
	// Default States
	// Company
	$("#ContactForm_company").focus(function(event){
		if ($("#ContactForm_company").val()=='Company') {
			$("#ContactForm_company").val('');
		}
	});
	$("#ContactForm_company").blur(function(event){
		if ($("#ContactForm_company").val()=='') {
			$("#ContactForm_company").val('Company');
		}
	});
	// Name
	$("#ContactForm_name").focus(function(event){
		if ($("#ContactForm_name").val()=='Name') {
			$("#ContactForm_name").val('');
		}
	});
	$("#ContactForm_name").blur(function(event){
		if ($("#ContactForm_name").val()=='') {
			$("#ContactForm_name").val('Name');
		}
	});
	// Email
	$("#ContactForm_email").focus(function(event){
		if ($("#ContactForm_email").val()=='Email') {
			$("#ContactForm_email").val('');
		}
	});
	$("#ContactForm_email").blur(function(event){
		if ($("#ContactForm_email").val()=='') {
			$("#ContactForm_email").val('Email');
		}
	});
	
	// Contact Box ----------------------------------------------------------------------------------------------
	
	// Show Registration Box
	$("#contact-form-outer").show();
	
	// Setup Validation
	var validator2 = $("#contactForm").validate({
			rules: {
				ContactUS_name: { required: true, checkName: true },
				ContactUS_email: { required: true, checkEmail: true, email: true },
				ContactUS_question: { required: true, checkQuestion: true }
			},
			messages:{ 
            	ContactUS_name: "",
            	ContactUS_email: "",
				ContactUS_question: ""
        	}
	});
	
	// Send Contact US
	function sendContactUs( thisName , thisEmail , thisMessage , thisTel ) {
		var xmlhttp=null;
			xmlhttp=getxmlhttp();
			var url = "http://www.britainandirelandevent.co.uk/scripts/send_email/index.php?action=contact&name=" + thisName + "&email="+ thisEmail + "&message="+ thisMessage + "&tel="+ thisTel;
			xmlhttp.open("GET", url, true);
			xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				response = xmlhttp.responseText;
				$("#ContactUS_replace").html( response );
				}
			}
			xmlhttp.send(null);
	}
	
	// Submit
	$("#ContactUS_submit").click(function(event){
		validator2.form( );
		if ( validator2.numberOfInvalids() == 0 ) {
			thisName = $("#ContactUS_name").val();
			thisEmail = $("#ContactUS_email").val();
			thisMessage = $("#ContactUS_question").val();
			thisTel = $("#ContactUS_tel").val();
			$("#ContactUS_replace").html("Sending...");
			sendContactUs( thisName , thisEmail , thisMessage , thisTel )
		}
	});
	
	// Default States
	// Name
	$("#ContactUS_name").focus(function(event){
		if ($("#ContactUS_name").val()=='Name') {
			$("#ContactUS_name").val('');
		}
	});
	$("#ContactUS_name").blur(function(event){
		if ($("#ContactUS_name").val()=='') {
			$("#ContactUS_name").val('Name');
		}
	});
	// Email
	$("#ContactUS_email").focus(function(event){
		if ($("#ContactUS_email").val()=='Email') {
			$("#ContactUS_email").val('');
		}
	});
	$("#ContactUS_email").blur(function(event){
		if ($("#ContactUS_email").val()=='') {
			$("#ContactUS_email").val('Email');
		}
	});
	// Message
	$("#ContactUS_question").focus(function(event){
		if ($("#ContactUS_question").val()=='Your Question') {
			$("#ContactUS_question").text('');
		}
	});
	$("#ContactUS_question").blur(function(event){
		if ($("#ContactUS_question").val()=='') {
			$("#ContactUS_question").text('Your Question');
		}
	});
	// Tel
	$("#ContactUS_tel").focus(function(event){
		if ($("#ContactUS_tel").val()=='Tel') {
			$("#ContactUS_tel").val('');
		}
	});
	$("#ContactUS_tel").blur(function(event){
		if ($("#ContactUS_tel").val()=='') {
			$("#ContactUS_tel").val('Tel');
		}
	});
	
	// Reset
	$("#ContactUS_reset").click(function(event){
		$("#ContactUS_name").val('Name');
		$("#ContactUS_email").val('Email');
		$("#ContactUS_question").text('Your Question');
		$("#ContactUS_tel").text('Tel');
		$("#ContactUS_name").removeClass('error');
		$("#ContactUS_email").removeClass('error');
		$("#ContactUS_question").removeClass('error');
		$("#ContactUS_tel").removeClass('error');
		$("#ContactUS_name").removeClass('valid');
		$("#ContactUS_email").removeClass('valid');
		$("#ContactUS_question").removeClass('valid');
		$("#ContactUS_tel").removeClass('valid');
	});
		
});

