

/*	James

	File:		Contact JS
	Author:		James Brannon

	-------------------------------------------------------------------------------------------------------------------- */
	
	function Contact(form) {this.inputs;this.submit;this.active;this.init(form);}
		Contact.prototype = {
			init: function(form) {
				this.form = form;
				this.active = false;
				
				// find user inputs
				this.text = $('#'+form+' input');
				this.textareas = $('#'+form+' textarea');
				
				// populate 
				this.inputs=[];
				var i,t;
				for(i=0;i<this.text.length;i++){
					this.inputs.push($(this.text[i]));
				}
				for(t=0;t<this.textareas.length;t++){
					this.inputs.push($(this.textareas[t]));
				}
				
				// add placeholders
				this.placeholders(this.inputs);
				
				// activate form
				this.run(this.inputs);
				
			},
			placeholders: function(inputs){
				var i;
				for(i=0;i<inputs.length;i++){
					// add placeholder functionality if required
					if($(inputs[i]).attr('data-placeholder')){
						// text inputs
						if($(inputs[i]).attr('type')=='text'){
							$(inputs[i]).val($(inputs[i]).attr('data-placeholder'));
						// textareas
						}else{
							$(inputs[i]).html($(inputs[i]).attr('data-placeholder'));
						}
						// add focus
						$(inputs[i]).focus(function() {
							if($(this).val()==$(this).attr('data-placeholder')){
								$(this).val('');
							}
						});
						// add blur
						$(inputs[i]).blur(function() {
							if(!$(this).val()){
								$(this).val($(this).attr('data-placeholder'));
							}
						});
					}
				}
			},
			run: function(inputs){
				var instance = this,s;
				
				// loop through inputs
				for(s=0;s<inputs.length;s++){
					if($(inputs[s]).attr('type')=='text'||$(inputs[s]).attr('type')=='textarea'){
						$(inputs[s]).keypress(function() { instance.validate(inputs) });
						$(inputs[s]).focus(function() { instance.validate(inputs) });
						$(inputs[s]).blur(function() { instance.validate(inputs) });
					}
					
					// find, define and activate submit button
					if($(inputs[s]).attr('type')=='submit'){
						// define submit button
						this.submit = $(inputs[s]);
						// run validation before sending
						this.submit.click(function() { instance.validate(inputs,true);return false; });
					}
				}
			},
			sendmail: function(data){
			
				console.log('Post Mail: ',data.length)
				
				var instance=this,array=[],d;
				
				// define data set
				for(d=0;d<data.length;d++){
					if(data[d].attr('type')!='submit'){
						// ensure placeholder data is not sent on non-required inputs
						if(data[d].attr('data-placeholder')&&$(data[d]).val()==data[d].attr('data-placeholder')){
							array.push(false);
						}else{
							array.push($(data[d]).val());
						}
					}
				}
				
				// disble form for sending
				this.sending();
				
				// post data
				$.ajax({
					type: "post",
					url: "send.php",
		//			data: "name=" + name + "&email=" + email + "&comment=" + comment,
					error: function() {
						console.log('Error sending');
						instance.error();
					},
					success: function () {
						console.log('Success');
						// success, print thankyou
						instance.thankyou();
					}				
				});	
				
				
				console.log(array);
				
				
			},
			sending: function(){
				
				// define
				var form = $('#'+this.form),
					article = form.parent(),
					mask = document.createElement('div'),
					box = document.createElement('div'),
					img = document.createElement('img'),
					message = document.createElement('p');
				
				// add mask attriutes
				mask.className = 'input-mask';
				img.src = 'http://jamesbrannon.co.uk/assets/gif/ajax-loader.gif';
				message.innerHTML = 'Sending...';
				
				// scroll page to anchor
				var anchor = $("#page-anchor").offset().top-$('header.main nav').height();
				if($(window).scrollTop()>anchor){
					$('html,body').stop().animate({scrollTop: anchor}, 250);
				}
				
				// build loader
				box.appendChild(img);
				box.appendChild(message);
				
				//- add mask to stage
				mask.appendChild(box);
				article.append(mask);
				
				// fade form
				form.fadeTo(250, .3);
			},
			thankyou: function(){
			
				// define
				var instance = this,
					form = $('#'+this.form),
					title = form.parent().children('h2'),
					mask = form.parent().children('.input-mask'),
					message = document.createElement('div'),
					copy = document.createElement('p'),
					highlight = document.createElement('h3'),
					link = document.createElement('a');
				
				//- add attributes
				copy.innerHTML = 'Your message has been sent, I\'ll be in thouch as soon as I can.',
				link.innerHTML = 'Reset';
				$(link).click(function() { instance.reset(form) });
				message.className = 'message';
				message.style.display = "none";
				message.style.opacity = 0;
				
				//- append to stage
				highlight.appendChild(link);
				message.appendChild(copy);
				message.appendChild(highlight);
				form.before(message);
				
				// animate message in, form out
				form.stop().animate({opacity:0}, 500,function(){ form.hide() });
				mask.stop().animate({opacity:0}, 500,function(){ 
					mask.remove();
					$(message).fadeTo(500, 1);
				});
				title.fadeTo(500, 0, function(){ 
					title.html('Thank you!');
					title.fadeTo(500, 1);
				});
			},
			error: function(){
			
				// define
				var instance = this,
					form = $('#'+this.form),
					title = form.parent().children('h2'),
					mask = form.parent().children('.input-mask'),
					message = document.createElement('div'),
					copy = document.createElement('p'),
					highlight = document.createElement('h3'),
					link = document.createElement('a');
				
				//- add attributes
				copy.innerHTML = 'Something has gone wrong, I\'ll get it fixed as soon as I can.',
				link.innerHTML = 'Retry';
				$(link).click(function() { instance.reset(form) });
				message.className = 'message';
				message.style.display = "none";
				message.style.opacity = 0;
				
				//- append to stage
				highlight.appendChild(link);
				message.appendChild(copy);
				message.appendChild(highlight);
				form.before(message);
				
				// animate message in, form out
				form.stop().animate({opacity:0}, 500,function(){ form.hide() });
				mask.stop().animate({opacity:0}, 500,function(){ 
					mask.remove();
					$(message).fadeTo(500, 1);
				});
				title.fadeTo(500, 0, function(){ 
					title.html('Oops sorry!');
					title.fadeTo(500, 1);
				});
			},
			reset: function(form){
				
				// define
				var message = form.parent().children('.message'),
					title = form.parent().children('h2');
				
				// reset inputs
				var r;
				for(r=0;r<this.inputs.length;r++){
					if($(this.inputs[r]).attr('data-placeholder')){
						$(this.inputs[r]).removeClass('error');
						$(this.inputs[r]).val($(this.inputs[r]).attr('data-placeholder'));
					}
				}
				
				// animate
				title.fadeTo(250, 0, function(){ 
					title.html('Contact me');
					title.fadeTo(250, 1);
				});
				message.fadeTo(250, 0, function(){ 
					message.remove();
					form.css('display','block').fadeTo(250, 1);
				});
				
				// reset active status
				this.active=false;
				
			},
			validate: function(inputs,submit){
			
				// submit request set form state to 'active'
				if(submit){ this.active=true };
				
				// define validation variables
				var errors,pass,v,field;

				// if the form is set to active begin validation
				if(this.active){
					
					// reset error log
					errors=0;
					
					// loop through form
					for(v=0;v<inputs.length;v++){
						
						// define field and reset status (pass)
						field=$(inputs[v]);
						pass=false;
						
						// only validate user input fields (not submit button)
						if(field.attr('type')=='text'||field.attr('type')=='textarea'){
							
							// validate required fields
							if(field.attr('data-required')){
								if(this.isEmpty(field.val(),field)){
									field.addClass('error');
									if(!field.next().html()){
										field.after('<span>Sorry, this field is required</span>');
									}else{ field.next().html('Sorry, this field is required') };
									pass=false;
								}else{
									field.removeClass('error');
									field.next().remove();
									pass=true;
								}
							}else{
								pass=true;
							}
							// validate email fields
							if(pass&&field.attr('data-placeholder').indexOf('@')>-1){
								if(!this.isEmail(field.val(),field)){
									field.addClass('error');
									field.next().remove();
									field.after('<span>Invalid email address</span>');
									pass=false;
								}else{ 
									field.removeClass('error');
									field.next().remove();
									pass=true;
								};
							}
							// validate phone number
							if(pass&&field.attr('name')=='phone'){
								if(!this.isNumber(field.val())){
									field.addClass('error');
									field.next().remove();
									field.after('<span>Invalid phone number</span>');
									pass=false;										
								}else{ 
									field.removeClass('error');
									field.next().remove();
									pass=true;
								};
							}
							// update errors log
							if(!pass){ errors++ };
						}
					}
				}
				
				// validation passed with no errors
				if(submit&&errors==0){
				
					// send email
					this.sendmail(inputs);
				};
			},
			isEmpty: function(val,subject){
				if(val==''||val==subject.attr('data-placeholder')){
					return true;
				}else{
					return false;
				}
			},
			isEmail: function(val,subject){
				var regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
				if(regex.test(val)&&val!=subject.attr('data-placeholder')) {
					return true;
				}else{
					return false;
				}
			},
			isNumber: function(val){
				var regex = /^[0-9]+$/g;
					strip = val.replace('Eg.','').replace('+','').replace(' ','');
				if(regex.test(strip)) {
					return true;
				}else{
					return false;
				}
			}	
	}
