﻿$j(document).ready(function(){
	liveForm.init();
});

liveForm = function(){
	var config = {
		form: $j('#LiveForm'),
		hints:{  },
		CSS:{
			validateEmpty: '.v-empty',
			validateUsername: '.v-username',
			validatePassword: '.v-password',
			validateConfirmPassword: '.v-confirm-password',
			validateEmail: '.v-email'	 
		}
	}
	var shelf = {};
	function init(){
		events();
		checkForSubmittedErrors();
	};
	function checkForSubmittedErrors(){
		shelf.submittedErrors = $j('div.fieldWithErrors');
	};
	function events(){
		$j('input').focus(function(){
			hideHint($j(this));
		});
		//On focus generate sample username
		$j('#Username').focus(function(){
			$j(this)[0].value = sampleUsername($j(this));
			
		});
		//On Blur valiation
		$j('input' + config.CSS.validateEmpty).blur(function(){
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { hint(true,$j(this),''); }
		});
		$j('input' + config.CSS.validateUsername).blur(function(){
			var value = $j(this).attr('value');
			if(value == ''){ hint(false,$j(this),'is required'); }
			else {
				if(verifyUsername(value)== true) {
					hint(true,$j(this),''); 
				} else {
					hint(false,$j(this),' already in use'); 
				}
			}	
		});
		$j('input' + config.CSS.validatePassword).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { hint(true,$j(this),'');  } //check password strength
		});
		$j('input' + config.CSS.validateConfirmPassword).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { 
				var password = $j('.v-password').attr('value');
				var confirm = $j('.v-confirm-password').attr('value');
				if(verifyPassword(password,confirm) == true) { hint(true,$j(this),''); }
				else { hint(false,$j(this),'does not match'); }
			} 
		});
		$j('input' + config.CSS.validateEmail).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { 
				if(verifyEmail(value) == true) { hint(true,$j(this),''); }
				else { hint(false,$j(this),'is not valid'); }
			} 
		});
	};
	function hint(passed,$e,msg){
		var $parent = $e.parent().parent().parent();
		var $hint = $j('span.hint',$parent);
		if(passed == true){
			$hint.removeClass('hint-error');
			$hint.addClass('hint-ok');
			$hint.show();
		} else {
			$hint.removeClass('hint-ok');
			$hint.addClass('hint-error');
			$hint.show();	
		}
	};
	function hideHint($e){
		var $parent = $e.parent().parent().parent();
		var $hint = $j('span.hint',$parent);
		$hint.hide();
	}
	function sampleUsername($e){
		var name = { first: $j('#FirstName').attr('value'), second: $j('#SecondName').attr('value')}
		var result;
		if(name.first == '' || name.second == '') { result = ''}
		else {
			result = name.first + name.second;
			if(verifyUsername($e[0].value) == true) { hint(true,$e,''); }
			else { hint(false,$e,'already in use');}
		}
		return result
	};
	function verifyUsername(u){
		return true;
	};
	function verifyPassword(p,cp){
		var result = false;
		if(p == cp) {
			//Passwords match
			return true;
		} else { 
			//Passwords do not match
			return false;
		}
	};
	function verifyEmail(e){
		var at="@"
		var dot="."
		var lat=e.indexOf(at)
		var le=e.length
		var ldot=e.indexOf(dot)
		if (e.indexOf(at)==-1){
		return false
		}
		if (e.indexOf(at)==-1 || e.indexOf(at)==0 || e.indexOf(at)==le){
		return false
		}
		if (e.indexOf(dot)==-1 || e.indexOf(dot)==0 || e.indexOf(dot)==le){
		return false
		}
		if (e.indexOf(at,(lat+1))!=-1){
		return false
		}
		if (e.substring(lat-1,lat)==dot || e.substring(lat+1,lat+2)==dot){
		return false
		}
		if (e.indexOf(dot,(lat+2))==-1){
		return false
		}
		if (e.indexOf(" ")!=-1){
		return false
		}
		return true
	};
	function databaseTest(a){
		return false
	};
	return {
		init:init,
		config:config
	}
}();

$j(document).ready(function(){
	liveForm.init();
});

liveForm = function(){
	var config = {
		form: $j('#LiveForm'),
		hints:{  },
		CSS:{
			validateEmpty: '.v-empty',
			validateUsername: '.v-username',
			validatePassword: '.v-password',
			validateConfirmPassword: '.v-confirm-password',
			validateEmail: '.v-email'	 
		}
	}
	var shelf = {};
	function init(){
		events();
		checkForSubmittedErrors();
	};
	function checkForSubmittedErrors(){
		shelf.submittedErrors = $j('div.fieldWithErrors');
		shelf.submittedErrors.each(function(i){
			hint(false,$j(this),'');
		});
	};
	function events(){
		$j('input').focus(function(){
			hideHint($j(this));
		});
		//On focus generate sample username
		$j('#Username').focus(function(){
			$j(this)[0].value = sampleUsername($j(this));
			
		});
		//On Blur valiation
		$j('input' + config.CSS.validateEmpty).blur(function(){
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { hint(true,$j(this),''); }
		});
		$j('input' + config.CSS.validateUsername).blur(function(){
			var value = $j(this).attr('value');
			if(value == ''){ hint(false,$j(this),'is required'); }
			else {
				if(verifyUsername(value)== true) {
					hint(true,$j(this),''); 
				} else {
					hint(false,$j(this),' already in use'); 
				}
			}	
		});
		$j('input' + config.CSS.validatePassword).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { hint(true,$j(this),'');  } //check password strength
		});
		$j('input' + config.CSS.validateConfirmPassword).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { 
				var password = $j('.v-password').attr('value');
				var confirm = $j('.v-confirm-password').attr('value');
				if(verifyPassword(password,confirm) == true) { hint(true,$j(this),''); }
				else { hint(false,$j(this),'does not match'); }
			} 
		});
		$j('input' + config.CSS.validateEmail).blur(function(){
			var value = $j(this).attr('value');
			if($j(this).attr('value') == '') { hint(false,$j(this),'is required'); }
			else { 
				if(verifyEmail(value) == true) { hint(true,$j(this),''); }
				else { hint(false,$j(this),'is not valid'); }
			} 
		});
	};
	function hint(passed,$e,msg){
		var $parent = $e.parent().parent().parent();
		var $hint = $j('span.hint',$parent);
		if(passed == true){
			$hint.removeClass('hint-error');
			$hint.addClass('hint-ok');
			$hint.show();
		} else {
			$hint.removeClass('hint-ok');
			$hint.addClass('hint-error');
			$hint.show();	
		}
	};
	function hideHint($e){
		var $parent = $e.parent().parent().parent();
		var $hint = $j('span.hint',$parent);
		$hint.hide();
	}
	function sampleUsername($e){
		var name = { first: $j('#FirstName').attr('value'), second: $j('#SecondName').attr('value')}
		var result;
		if(name.first == '' || name.second == '') { result = ''}
		else {
			result = name.first + name.second;
			if(verifyUsername($e[0].value) == true) { hint(true,$e,''); }
			else { hint(false,$e,'already in use');}
		}
		return result
	};
	function verifyUsername(u){
		return true;
	};
	function verifyPassword(p,cp){
		var result = false;
		if(p == cp) {
			//Passwords match
			return true;
		} else { 
			//Passwords do not match
			return false;
		}
	};
	function verifyEmail(e){
		var at="@"
		var dot="."
		var lat=e.indexOf(at)
		var le=e.length
		var ldot=e.indexOf(dot)
		if (e.indexOf(at)==-1){
		return false
		}
		if (e.indexOf(at)==-1 || e.indexOf(at)==0 || e.indexOf(at)==le){
		return false
		}
		if (e.indexOf(dot)==-1 || e.indexOf(dot)==0 || e.indexOf(dot)==le){
		return false
		}
		if (e.indexOf(at,(lat+1))!=-1){
		return false
		}
		if (e.substring(lat-1,lat)==dot || e.substring(lat+1,lat+2)==dot){
		return false
		}
		if (e.indexOf(dot,(lat+2))==-1){
		return false
		}
		if (e.indexOf(" ")!=-1){
		return false
		}
		return true
	};
	function databaseTest(a){
		return false
	};
	return {
		init:init,
		config:config
	}
}();

