$(document).ready(function() {

	//Make "Security Code" disappear when input box is in focus
		
	$("#sCode").val("Security Code");
	
	$("#sCode").blur(function(){
		if ($("#sCode").val() == ''){
			$("#sCode").val("Security Code");
		}
	});
	$("#sCode").focus(function(){
		$("#sCode").val("");	
	});
	//END
	
	$error = false;
	$("#signupsubmit").click(function(){
	
		if ($("#sCode").val() == "Security Code"){
			$("#sCode").val("");
			$error = true;
		}
	
		
	});
	
	if (!$error){
		
		
	
		$("#signupform").validate({
			rules: {
				email: {
					required: true,
					email: true
				},
				password: {
					required: true,
					minlength: 6
				},
				cpassword: {
					required: true,
					minlength: 6,
					equalTo: "#password"
				},
				accountType: "required",
				fname: "required",
				lname: "required",
				country: "required",
				address: "required",
				city: "required",
				zip: "required",
				phone: "required",
				type: "required",
				number: "required",
				sCode: "required",
				expiresMonth: "required",
				expiresYear: "required"
			},
			messages: {
				email: {
					required: "Please enter a valid email address",
					minlength: "Please enter a valid email address",
					remote: jQuery.format("{0} is already in use, would you like to <a href='/login'>login</a>?")
				},
				password: {
					required: "Provide a password",
					rangelength: jQuery.format("Enter at least {0} characters")
				},
				cpassword: {
					required: "Repeat your password",
					minlength: jQuery.format("Enter at least {0} characters"),
					equalTo: "Enter the same password as above"
				},
				accountType: "Please choose a Linkshiftr plan type",
				fname: "Enter your first name",
				lname: "Enter your last name",
				country: "Please choose your country",
				address: "Enter your address",
				city: "Enter your city",
				zip: "Enter your zip or postal code",
				phone: "Enter your phone number",
				type: "Please choose your credit card type",
				number: "Enter your credit card number<br />",
				sCode: "Enter your security code<br />",
				expiresMonth: "Enter your expiry date (Month)<br />",
				expiresYear: "Enter your expiry date (Year)<br />"
				
			},
			// the errorPlacement has to take the table layout into account
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo( element.parent().next().next() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.next() );
				else if (element.is("input#number.cNumber") || element.is("input#sCode.sCode") || element.is("select#expiresMonth.monthSelector") || element.is("select#expiresYear.yearSelector")){
					// Find elements that have 2 or more inputs in one row
					error.appendTo(element.parent().siblings(".error"));
	/* 					console.log(element.parent().siblings(".error")); */
				}
				else
					error.appendTo( element.parent().next() );
			},
			submitHandler: function(signupform) {
				jQuery(signupform).ajaxSubmit({
			        dataType:  'json',
			        beforeSubmit: function () {
			        $("#errorAlert").css("visibility","hidden");
			        $("#loadingError").css("visibility","hidden");
			        $("#loading").css("visibility","visible");
	                $("#loading").html('<img src="/img/signup-loading.gif" alt="loading" title="loading" /> Processing your transaction... this may take upto 30 seconds. Please do not click again, hit the back button or refresh the page until the process has completed.')
	                },
			        success:   processJson,
                    error: processJson
				});
			}
			
		});
		
	}
	
	function processJson(data) { 
		$("#loading").css("visibility","hidden");
		$("#errorAlert").html(data);
	    if (data.error){
	    	$("#errorAlert").css("visibility","visible");
	    	$("#loadingError").css("visibility","visible");
	    	if (data.errorDetail){
		    	$("#errorAlert").html(data.errorDetail);
		    	$("#loadingError").html("There was an error(s), see above for details");
		    }
		    if (data.error == "Password doesn't match"){//User has an account but the password used does not match the password in DB.
				$('#dv_password').html("");
				$('#dv_cpassword').html("Password doesn't match");
			}
	    }else if (data.response == "SUCCESS"){
	    	window.location = "/login?welcome=1";
	    }else if (data.insert_id > 0){
	    	confirm('Your account has already been created. Would you like to login?');
	    	window.location = "/login";
	    }
	    
	    
	}
});

