var VyouSignupFormAjax = {
	form_name:'',
	vuser_id:'',
	formvalid: 'false',
    bad: new Array(),
    init: function (form_name){
		this.form_name = form_name;
		$('form[name='+ VyouSignupFormAjax.form_name +']').find('.validate_email').change(function(event) {   
			VyouSignupFormAjax.email($(event.target));
		});
		$('form[name='+ VyouSignupFormAjax.form_name +']').find('.validate_display_name').change(function(event) {   
			VyouSignupFormAjax.fullname($(event.target));
		});
		$('form[name='+ VyouSignupFormAjax.form_name +']').find('.validate_password').change(function(event) {   
			VyouSignupFormAjax.password($(event.target));
		});
		$('.gray_button_03').click(function(event) {   
			event.stopImmediatePropagation();
			$.fancybox.close();
		});
	},
    required:function(val){//Here is a quirk, string length ranges are in here.
		var val = arguments[0];
		var min = arguments[1] ? arguments[1] : 1;
		var max = arguments[2] ? arguments[2] : 10000;
		
		if(val.length == 0 || val.length < min || val.length > max){
			val = false;
		}
		return val;
	},
	email:function(el){
		var val = el.val();
		var data = $.param({email:val});
		$.ajax({
		type: "POST",
		url: "/server/emailisvalidss",
		data: data,
		async:false,
		success: function(ajax_response) {
		email_feedback = ajax_response.split('||');
		}
		});
		
		if(email_feedback[0] == "false"){
			this.bad.push({email:email_feedback[1]});
			el.css('border','1px solid #fb0069');
			notification("▼ " + email_feedback[1],{inlineElement:el});
		}else{
			el.siblings('.inline_notification').hide();
			el.css('border','1px solid #D1D1D1');
		}
	},
	alphanumeric:function(val){
		var URL_RGX=/^[a-z0-9_ ]+$/i;

		if(!URL_RGX.test(val)){
		val = false;
		}
		return val;
	},
	password:function(el){
        var val = el.val();
		if(false == this.required(val,6, 255)){
			notification('▼ Your password must be more than six characters', {inlineElement:el});
			el.css('border','1px solid #fb0069');
			this.bad.push({password:"Your password must be more than six characters"});
			this.formvalid = false;
		}else{
			el.siblings('.inline_notification').hide();
			el.css('border','1px solid #D1D1D1');
		}
	},
	fullname:function(el){
        var val = el.val();
		if(false == this.alphanumeric(val)){
			notification('▼ Full name must be alphanumeric...', {inlineElement:el});
			el.css('border','1px solid #fb0069');
			this.bad.push({fullname:'Full name must be alphanumeric...ñº'});
			return false;
		}
		if(false == this.required(val,3, 50)|| val == 'Full Name'){
			notification('▼ Please enter your full name...', {inlineElement:el});
			el.css('border','1px solid #fb0069');
			this.bad.push({fullname:'Please enter your full name...'});
		}else{
			el.siblings('.inline_notification').hide();
			el.css('border','1px solid #D1D1D1');
		}
	},
	validate: function (redirect_gaq) {
		
		this.bad = [];
    	
    	this.email($('form[name='+ this.form_name +']').find('.validate_email'));
    	this.fullname($('form[name='+ this.form_name +']').find('.validate_display_name'));
    	this.password($('form[name='+ this.form_name +']').find('.validate_password'));
    	
    	//redirect is only for that static form
    	if(redirect_gaq != undefined){

    		if(this.bad.length > 0){
    			$('.inline_notification').hide();
    			document[this.form_name].submit();
    		}else{
    			_gaq.push(['_trackPageview', '/signup/email/success/'+ redirect_gaq]); 
    			document[this.form_name].submit();
    		}
    		return;
    		
    	}else{
    		return this.bad;
    	}
    }
};

function attach_placeholders(){
$(".placholder").hide();
	
	$(".placeholdable input").focus(function(event){
		var element = $(this);
		var placeholder = element.siblings('.placeholder');
	//	element.css('background-color','#ffffff');
		placeholder.css('color','#cfcfd1');
	//	placeholder.css('background-color','#ffffff');
	});
	$(".placeholdable input").blur(function(event){
		var element = $(this);
		if(element.val() == ''){
			element.siblings('.placeholder').show();
		}
	});
	$(".placeholdable").click(function(event){
		var element = $(this);
		var input = element.find('input');
		var placeholder = element.find('span.placeholder');
	//	input.css('background-color','#ffffff');
		placeholder.css('color','#cfcfd1');
	//	placeholder.css('background-color','#ffffff');
		input.focus();

	});
	$(".placeholdable input").keydown(function(e){
		var element = $(this);
		element.siblings('.placeholder').hide();
	});
}

$(document).ready(function () {
	attach_placeholders();
});
