﻿var validate = {
	ajaxing : false,
	success : true,
	showError : function(options){
		$.extend({'field':'','scrollTo':false,'error':false},options);
		if(options.scrollTo){
			$.scrollTo(options.field);
		}

		var parentsUntil = options.field.parentsUntil('li');
		var selector = parentsUntil.length == 0 ? options.field.parent() : parentsUntil.parent();
		options.field.removeClass('success-on-field');
		options.field.addClass('error-on-field');
		selector.find('.validation-error').hide(); //remove any errors		
		selector.find((options.error ? options.error : '.validation-error')).css('display','inline-block');
		options.field.addClass('error-on-field');		
	},
	showSuccess : function(field){		
		var parentsUntil = field.parentsUntil('li');
		var selector = parentsUntil.length == 0 ? field.parent() : parentsUntil.parent();		
		field.removeClass('error-on-field');

		selector.find('.validation-error').hide(); //remove any errors
		field.addClass('success-on-field');		
	},
	submittingForm : false,
	checkBeforeSubmit : function(){
		validate.reset();
		validate.submittingForm = true;
		for(var i in validate.checks){
			if($('#'+i).is(':visible')){
				validate.checks[i](true);
				if(validate.success == false){
					validate.submittingForm = false;
					return false;
				}
			}
		}
		return true;
	},
	reset : function(){
		validate.success = true;
		$('.alert-wrapper').remove();
	},
	checks : {
		'uname' : function(scrollTo){
			if(!/^[\w]{1,15}$/i.test($('#uname').val())){
				validate.showError({'field':$('#uname'),'scrollTo':scrollTo,'error':'#invalid-username'});
				validate.success = false;
			}
			else if(!validate.submittingForm){
				validate.ajaxing = true;
				fadeOut($('#uname'));
				$.ajax({
					type	: 'post',
					url		: '/ajax/username-available.php',
					data	: {'username':$('#uname').val()},
					success : function(res){
						//ignore the results when the form is being submitted. If a form is submitted while ajaxing, the ajax will return with an error
						if(validate.submittingForm){ return; }					
						
						validate.ajaxing = false;
						if(res == 'AVAILABLE'){
							validate.showSuccess($('#uname'));
						}
						else{
							validate.showError({'field':$('#uname'),'scrollTo':scrollTo,'error':'#new-customer'});
							validate.success = false;
						}
						
					}
				});
			}
		},
		'full_name' : function(scrollTo){
			if(!/^[\w\s.]{1,}$/i.test($('#full_name').val())){
				validate.showError({'field':$('#full_name'),'scrollTo':scrollTo});
				validate.success = false;
			}
			else{
				validate.showSuccess($('#full_name'));
			}
		},
		'email1' : function(scrollTo){
			if(!/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/i.test($('#email1').val())){
				validate.showError({'field':$('#email1'),'scrollTo':scrollTo,'error':'#invalid-email'});
				validate.success = false;
			}
			else if(!validate.submittingForm){
				validate.ajaxing = true;
				fadeOut($('#email1'));
				$.ajax({
					type	: 'post',
					url		: 'ajax/email-available.php',
					data	: {'email':$('#email1').val()},
					success : function(res){
						//ignore the results when the form is being submitted. If a form is submitted while ajaxing, the ajax will return with an error
						if(validate.submittingForm){ return; }
						
						validate.ajaxing = false;
						if(res == 'AVAILABLE'){
							validate.showSuccess($('#email1'));
						}
						else if(res == 'INVALID_DOMAIN'){
							validate.showError({'field':$('#email1'),'scrollTo':scrollTo,'error':'#email-domain-invalid'});
							validate.success = false;
						}
						else{
							validate.showError({'field':$('#email1'),'scrollTo':scrollTo,'error':'#email-taken'});
							validate.success = false;

						}
					}
				});	
			}
		},
		'password' : function(scrollTo){
			if(!/^.{4,32}$/i.test($('#password').val())){
				validate.showError({'field':$('#password'),'scrollTo':scrollTo});
				validate.success = false;
			}
			else{
				validate.showSuccess($('#password'));
			}
		},
		'agree' : function(scrollTo){
			if($('#agree:checked').val() != 'on'){
				validate.showError({'field':$('#agree'),'scrollTo':scrollTo});
				validate.success = false;
			}
			else{
				validate.showSuccess($('#agree'));
			}
		}
	}
}
$(function(){
	var event = isIE6 ? 'blur' : 'change'; //ie 6 is busted
	for(var i in validate.checks){
		$('#'+i).bind(event,function(){
			validate.checks[$(this).attr('id').replace('/#/','')](false);
		});
	}
	
	//does the order form have to option to select that you're an existing customer?
	if($('#existing-customer').length){
		$('#existing-customer').bind(event,toggleExtraInfo);
		toggleExtraInfo();
	}
	
	$('#tos').dialog({
		autoOpen	: false,
		modal		: true,
		draggable	: false,
		resizable	: false,
		height		: 400,
		width		: 400,
		bgiframe	: isIE6,
		open		: function(){
			$('.ui-dialog').css('overflow','visible'); //so we can display the X button outside the dialog
			if(isIE6){ //if its ie6
				DD_belatedPNG.fix('.ui-dialog-titlebar-close'); //our close button is a png
			}
		}
	});
	

});

function toggleExtraInfo(){
	if($('#existing-customer:checked').length){
		$('#full-name-tags,#email-tags').hide();
	}
	else{
		$('#full-name-tags,#email-tags').show();
	}
}

function fadeOut(obj){
	if(validate.ajaxing){
		obj.fadeOut(500,function(){
			fadeIn(obj);
		});
	}
}

function fadeIn(obj){
	obj.fadeIn(500,function(){
		fadeOut(obj)
	});
}
