/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 254 2010-07-23 05:14:44Z emartin24 $
 */
jQuery(function ($) {
	// Load dialog on page load
	//$('#basic-modal-content').modal();
	// Load dialog on click
	
	$("#enquiry_form").validate({
		rules: {
			enqname: "required",
			enqmessage: "required",
			enqemail: "required email"
		  },
		submitHandler: function() { sendEnquiryForm(); },
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				//alert(message);
				$("#enqvalidationerrors").html("All fields are required");
			} else {
				$("#enqsendingStatus").html("");
			}
		},
		messages: {
			enqname: {required: ""},
			enqmessage: {required: ""},
			enqemail: {required: "", email: ""},
		},		
	});
	
	function sendEnquiryForm() {
        var _name = $('#cenqname').val();
        var _email = $('#cenqemail').val();
        var _message = $('#cenqmessage').val();
        var _type = $('#enqformtype').val();
        var _typeemail = $('#enqformtypeemail').val();
        //$('#enquiry_form').hide();
        $("#enqsendingStatus").html("Sending..");
        $("#enqvalidationerrors").html("");
        $.post("/submit_enquiry_form", {name: _name, email: _email, message: _message, type: _type, typeemail: _typeemail}, function(data) {
        	//alert("response is " + data.status);
        	if (data.status == "success") {
        		$("#enqsendingStatus").html("Enquiry sent");
        		//$("#hideaftersendingenquiry").hide();
        		//$("#sentaddress").html(_email);
        		//$("#showaftersendingenquiry").show();
        	} else {
        		$("#enqsendingStatus").html(data.status);
        		//$('#enquiry_form').show();
        	}
        }, "json");
	}

	$('#basic-modal .basic').click(function (e) {
		$("#licensee_form").validate({
			rules: {
				name: "required",
				company: "required",
				contact: "required",
				selCountry: "required",
			    email: "required email"
			  },
			submitHandler: function() { sendForm(); }
		});
		
		function sendForm() {
	        var _name = $('#cname').val();
	        var _email = $('#cemail').val();
	        var _company = $('#ccompany').val();
	        var _country = $('#selCountry').val();
	        var _contact = $('#ccontact').val();
	        var _type = $('#formtype').val();
	        var _typeemail = $('#formtypeemail').val();
	        $('#submit_licensee_form').hide();
	        $("#sendingStatus").html("Sending..");
	        $.post("/submit_licensee_form", {name: _name, email: _email, company: _company, country: _country, contact: _contact, type: _type, typeemail: _typeemail}, function(data) {
	        	if (data.status == "success") {
	        		$("#sendingStatus").html("");
	        		$("#hideaftersent").hide();
	        		$("#sentaddress").html(_email);
	        		$("#showaftersent").show();
	        	} else {
	        		$("#sendingStatus").html(data.status);
	        		$('#submit_licensee_form').show();
	        	}
	        }, "json");
		}

		$('#basic-modal-content').modal();

		return false;
	});
});


