	
	var reg_upload_path = 'temp/';
	var reg_max_size = 200 * 1024; //200kb
	
	$(document).ready(function() {
	
		init_focusEvent();
		init_hintsEvent();
		
		$('#reset-btn-ro').bind('click', function () {
			regBoxReset();
		});
		
		$('#home-btn-ro').bind('click', function () {
			homeBtnHandler();
		});
		
		$('#create-btn-ro').bind('click', function () {
			if (validation()) {
				regFormSubmit();
			}
		});
		
		var uploader = new qq.FileUploaderBasic({
			action: 'php_098.php',
			button: document.getElementById('pickfile-btn-ro'),
			allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
			sizeLimit: reg_max_size,  
			debug: false,
			onComplete: function(id, fileName, responseJSON){
				if (responseJSON.success) {
					$.getJSON('fileupload.php', { action: "upload", file: fileName, path: reg_upload_path }, function(data) {
						$('#uploded-photo img').attr('src', reg_upload_path + data.filename_29);
						$('#pic_size_100').val(data.filename_100);
						$('#default-btn').fadeIn(300);
						//$('#deny-btn').fadeIn(300);
						/*if (!data.blnSquare) {
							$('#uploaded-photo-box').fadeIn(300);
							$('#deny-btn').fadeIn(300);
						} else {
							$('#uploaded-photo-box').fadeOut(300);
							$('#deny-btn').fadeIn(300);
						}*/
					});
				}
				$('#pickfile-btn-ro').fadeOut(300);
			}
		});		

		$('#default-btn-ro').bind('click', function () {
			delProfilePic();
		});
		
		$('input#bday-inputbox').datepicker({dateFormat: 'yy/mm/dd', changeMonth: true, changeYear: true, yearRange: '1900:2011'});
		
	});
		
	function regFormSubmit() {
		
		openLoadingBar();
		
	  $.post('./json/reg_form.json.php?action=newMember', {
			 member : {
				 
				 firstName : $('#firstN-inputbox').val(),
				 lastName : $('#lastN-inputbox').val(),
				 username : $('#userN-inputbox').val(),
				 password : $('#pw-inputbox').val(),
				 email : $('#email-inputbox').val(),
				 contactno : $('#phone-inputbox').val(),
				 address_1 : $('#add1-inputbox').val(),
				 address_2 : $('#add2-inputbox').val(),
				 city : $('#city-inputbox').val(),
				 zip : $('#zip-inputbox').val(),
				 state : $('#state-inputbox').val(),
				 countryId : $('#country-inputbox').val(),
				 birthday : $('#bday-inputbox').val(),
				 gender : $('#gender-inputbox').val(),
				 profilePicPath : $('#pic_size_100').val()
				 
			 }
			 
			 
			 }, function(data) {
				 
				 closeLoadingBar();
				 
				 if (data.blnRegistration) {
				 	homeBtnHandler(true);
				 } else {
					 	
					var errMsg = '';
					
					if (data.errMsg_user !== null) {
						errMsg += data.errMsg_user;
						$('#userN_wd_ro').fadeIn(300);
					} else {
						$('#userN_wd_ro').fadeOut(300);
					}
					
					if (data.errMsg_email !== null) {
						errMsg += data.errMsg_email;
						$('#email_wd_ro').fadeIn(300);
					} else {
						$('#email_wd_ro').fadeOut(300);
					}
					
					if (data.errMsg_contact !== null) {
						errMsg += data.errMsg_contact;
						$('#phone_wd_ro').fadeIn(300);
					} else {
						$('#phone_wd_ro').fadeOut(300);
					}
					
					if (data.errMsg_shippingAddress !== null) {
						errMsg += data.errMsg_shippingAddress;
						$('#add_wd_ro').fadeIn(300);
					} else {
						$('#add_wd_ro').fadeOut(300);
					}
					
					if (errMsg != "") {
						if ($('#reg-logo-ng').css('display') == "none") {
							$('#reg-logo-ng').fadeIn(300);
						}
						$('#reg_err_msg').fadeOut(300, function() {
							$('#reg_err_msg').html(errMsg);
						});
						$('#reg_err_msg').fadeIn(300);
						return false;
					}
					
				 }
				 
			}, 
				 
		 	"json"
		);
		
	}
	
	function delProfilePic() {
		
		$.getJSON('fileupload.php', { action: "delete", size_100: reg_upload_path+$('#pic_size_100').val(), size_29: $('#uploded-photo img').attr('src') }, function(data) {
			if (data.blnDelete) {
				$('#pic_size_100').val('');
//				$('#uploaded-photo-box').fadeOut(300, function(){
//					$('#pickfile-btn-orange').fadeOut(300, function(){
//							$('#pickfile-btn-grey').fadeIn(300);
//						});
//				});
				$('#uploded-photo img').attr('src', 'img/reg_form/uploaded_photo.gif');
				$('#default-btn-ro').fadeOut(300);
				$('#default-btn').fadeOut(300);
				$('#uploded-photo-confirm').attr('checked', false);
				$('#uploded-photo-confirm').dgUncheck('#uploded-photo-confirm-span');
			}
		});
		
	}
	
	function homeBtnHandler(register) {

		loginInit();
		
		if (register) {
			$('#login_info-box').html('Your account has been created and an activation link is sent to the email you entered. Please activate your account by clicking on the activation link sent to your email before you login.');
			$('#login_info-box').css('display', 'block');
		}

		document.getElementById("login-form-container").style.display = "block";

		var heighY = parseInt(document.getElementById( "login-form-container" ).style.height);
		
		document.getElementById("login-form-container").style.top = String(-heighY)+"px";
		
		//alert(document.getElementById( "reg-form-container" ).style.top);
		var targetY = String(-document.documentElement.clientHeight) + "px";
		$('#reg-form-container').animate({top:targetY},500);
		setIntervalHome = setInterval(setIntervalHomeHandler, 500);
		
	}
	
	function setIntervalHomeHandler() {
		
		nowSituation = "login-form";
		
		clearInterval( setIntervalHome );
		document.getElementById("reg-form-container").style.display = "none";
		//var targetY = String(document.getElementById( "reg-form-container" ).style.height) + "px";
		$('#login-form-container').animate({'top':"0px"},600);
		idTimer = setInterval(idTimerStart, 1000);
		passTimer = setInterval(passFoucs, 1000);
		//alert(document.getElementById( "reg-form-container" ).style.top);
		
		initEvent();
	
	}
	


function init_focusEvent() {
	
	$('#firstN-inputbox').bind('focus', function () {
		if ($('#lastN-box').css('display') == "none") {
			$('#lastN-box').fadeIn(300);
		}
	});
	
	$('#lastN-inputbox').bind('focus', function () {
		if ($('#userN-box').css('display') == "none") {
			$('#userN-box').fadeIn(300);
		}
	});
	
	$('#userN-inputbox').bind('focus', function () {
		if ($('#pw-box').css('display') == "none") {
			$('#pw-box').fadeIn(300);
		}
	});
	
	$('#pw-inputbox').bind('focus', function () {
		if ($('#Cpw-box').css('display') == "none") {
			$('#Cpw-box').fadeIn(300);
		}
	});
	
	$('#Cpw-inputbox').bind('focus', function () {
		if ($('#email-box').css('display') == "none") {
			$('#email-box').fadeIn(300);
		}
	});
	
	$('#email-inputbox').bind('focus', function () {
		if ($('#phone-box').css('display') == "none") {
			$('#phone-box').fadeIn(300);
		}
	});
	
	$('#phone-inputbox').bind('focus', function () {
		if ($('#add-box').css('display') == "none") {
			$('#add-box').fadeIn(300);
		}
	});
	
	$('#add1-inputbox').bind('focus', function () {
		if ($('#city-box').css('display') == "none") {
			$('#city-box').fadeIn(300);
		}
	});
	
	$('#add2-inputbox').bind('focus', function () {
		if ($('#city-box').css('display') == "none") {
			$('#city-box').fadeIn(300);
		}
	});
	
	$('#city-inputbox').bind('focus', function () {
		if ($('#zip-box').css('display') == "none") {
			$('#zip-box').fadeIn(300);
		}
	});
	
	$('#zip-inputbox').bind('focus', function () {
		if ($('#state-box').css('display') == "none") {
			$('#state-box').fadeIn(300);
		}
	});
	
	$('#state-inputbox').bind('focus', function () {
		if ($('#country-box').css('display') == "none") {
			$('#country-box').fadeIn(300);
		}
	});
	
	$('#country-inputbox').bind('focus', function () {
		if ($('#bday-box').css('display') == "none") {
			$('#bday-box').fadeIn(300);
		}
	});
	
	$('#bday-inputbox').bind('focus', function () {
		if ($('#gender-box').css('display') == "none") {
			$('#gender-box').fadeIn(300);
		}
	});
	
	$('#gender-inputbox').bind('focus', function () {
		if ($('#up_photo-box').css('display') == "none") {
			$('#up_photo-box').fadeIn(300);
			$('#reset-btn').fadeIn(300);
			$('#create-btn').fadeIn(300);
		}
	});
		
}


function init_hintsEvent() {
	
	$('#userN-inputbox').bind('keyup change', function() {
		if (getLength($('#userN-inputbox').val()) > 0) {
			if ($('#userN-reg_hints-box').css('display') == "none") {
				$('#userN-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#pw-inputbox').bind('keyup change', function() {
		if (getLength($('#pw-inputbox').val()) > 0) {
			if ($('#pw-reg_hints-box').css('display') == "none") {
				$('#pw-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#email-inputbox').bind('keyup change', function() {
		if (getLength($('#email-inputbox').val()) > 0) {
			if ($('#email-reg_hints-box').css('display') == "none") {
				$('#email-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#phone-inputbox').bind('keyup change', function () {
		if (getLength($('#phone-inputbox').val()) > 0) {
			if ($('#phone-reg_hints-box').css('display') == "none") {
				$('#phone-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#zip-inputbox').bind('keyup change', function () {
		if (getLength($('#zip-inputbox').val()) > 0) {
			if ($('#zip-reg_hints-box').css('display') == "none") {
				$('#zip-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#state-inputbox').bind('keyup change', function () {
		if (getLength($('#state-inputbox').val()) > 0) {
			if ($('#state-reg_hints-box').css('display') == "none") {
				$('#state-reg_hints-box').fadeIn(300);
			}
		}
	});
	
	$('#pickfile_div').bind('mouseenter', function() {											   
		divShow('#reg-pickfile-btn-ro');											   
	});
	
}

//    <div style="height:12px">Sorry ! Information are missing.</div>
//    <div style="height:12px">This username already in use. Please try another.</div>
//    <div style="height:12px">This e-mail is already registered.</div>
//    <div style="height:12px">This contact phone number is already registered.</div>
//    <div style="height:12px">Confirm password and password do not match.</div>

function validation() {
	
	var errMsg = "";
	
	var blnMissingSuccess = true;
	var blnEmail = true;
	var blnVerfyPassword = true;
	var blnContactNo = true;
	
	if ($('#firstN-inputbox').val() == '') {
		$('#firstN_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#firstN_wd_ro').fadeOut(300);
	}
	
	if ($('#lastN-inputbox').val() == '') {
		$('#lastN_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#lastN_wd_ro').fadeOut(300);
	}
	
	if ($('#userN-inputbox').val() == '') {
		$('#userN_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#userN_wd_ro').fadeOut(300);
	}
	
	if ($('#pw-inputbox').val() == '') {
		$('#pw_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	}
	
	if ($('#Cpw-inputbox').val() == '') {
		$('#Cpw_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	}
	
	if  ($('#pw-inputbox').val() != '' || $('#Cpw-inputbox').val() != '') {
		if ($('#pw-inputbox').val().length < 6 || $('#pw-inputbox').val().length > 20) {
			$('#pw_wd_ro').fadeIn(300);
			blnMissingSuccess = false;
		} else {
			if ($('#pw-inputbox').val() != $('#Cpw-inputbox').val()) {
				$('#pw_wd_ro').fadeIn(300);
				$('#Cpw_wd_ro').fadeIn(300);
				blnVerfyPassword = false;
			} else {
				$('#pw_wd_ro').fadeOut(300);
				$('#Cpw_wd_ro').fadeOut(300);
			}
		}
	}
	
	if ($('#email-inputbox').val() == '') {
		$('#email_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		if (!echeck($('#email-inputbox').val())) {
			$('#email_wd_ro').fadeIn(300);
			blnEmail = false;
		} else {
			$('#email_wd_ro').fadeOut(300);
		}
	}
	
	if ($('#phone-inputbox').val() == '') {
		$('#phone_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		if (isInteger($('#phone-inputbox').val())) {
			if ($('#phone-inputbox').val().length < 8 ) {
				$('#phone_wd_ro').fadeIn(300);
				blnMissingSuccess = false;
			} else {
				if ($('#phone-inputbox').val().length > 20 ) {
					blnContactNo = false;
					$('#phone_wd_ro').fadeIn(300);
				} else {
					$('#phone_wd_ro').fadeOut(300);
				}
			}
		} else {
			blnMissingSuccess = false;
			$('#phone_wd_ro').fadeIn(300);
		}
	}
	
	if ($('#add1-inputbox').val() == '' && $('#add2-inputbox').val() == '') {
		$('#add_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#add_wd_ro').fadeOut(300);
	}

	if ($('#city-inputbox').val() == '') {
		$('#city_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#city_wd_ro').fadeOut(300);
	}
	
	if ($('#zip-inputbox').val() == '') {
		$('#zip_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#zip_wd_ro').fadeOut(300);
	}
	
	if ($('#state-inputbox').val() == '') {
		$('#state_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#state_wd_ro').fadeOut(300);
	}
	
	if ($('#bday-inputbox').val() == '') {
		$('#bday_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#bday_wd_ro').fadeOut(300);
	}
	
	if ($('#country-inputbox').val() == 'none') {
		$('#country_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#country_wd_ro').fadeOut(300);
	}
	
	if ($('#gender-inputbox').val() == 'none') {
		$('#gender_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		$('#gender_wd_ro').fadeOut(300);
	}
	
/*	if ($('#pic_size_100').val() == '') {
		$('#up_photo_wd_ro').fadeIn(300);
		blnMissingSuccess = false;
	} else {
		if ($('#uploaded-photo-box').css('display') == "block") {
			if (document.getElementById('uploded-photo-confirm').checked) {
				$('#up_photo_wd_ro').fadeOut(300);
			} else {
				$('#up_photo_wd_ro').fadeIn(300);
				blnMissingSuccess = false;
			}
		}
	}
*/	
	if (!blnMissingSuccess) {
		errMsg += "<div style=\"height:12px\">Sorry ! Information missing.</div>";
	}
	
	if (!blnVerfyPassword) {
		errMsg += "<div style=\"height:12px\">Confirm password and password do not match.</div>";
	}
	
	if (!blnEmail) {
		errMsg += "<div style=\"height:12px\">This email is invalid</div>";
	}
	
	if (!blnContactNo) {
		errMsg += "<div style=\"height:12px\">Sorry ! Contact phone number exceeds 20-digits.	</div>";
	}
	
	if (errMsg != "") {
		if ($('#reg-logo-ng').css('display') == "none") {
			$('#reg-logo-ng').fadeIn(300);
		}
		$('#reg_err_msg').fadeOut(300, function() {
			$('#reg_err_msg').html(errMsg);
		});
		$('#reg_err_msg').fadeIn(300);
		return false;
	}
	
	return true;
	
}

function getLength(value) {
	
	return value.length;
	
}

	function regBoxReset() {
		
		if ($('#reg_err_msg').css('display') == "block") {
			$('#reg_err_msg').fadeOut(300);
		}
		
		if ($('#reg-logo-ng').css('display') == "block") {
			$('#reg-logo-ng').fadeOut(300);
		}
		
		$('#firstN_wd_ro').fadeOut(300);
		$('#lastN_wd_ro').fadeOut(300);
		$('#userN_wd_ro').fadeOut(300);
		$('#pw_wd_ro').fadeOut(300);
		$('#Cpw_wd_ro').fadeOut(300);
		$('#email_wd_ro').fadeOut(300);
		$('#phone_wd_ro').fadeOut(300);
		$('#add_wd_ro').fadeOut(300);
		$('#city_wd_ro').fadeOut(300);
		$('#zip_wd_ro').fadeOut(300);
		$('#state_wd_ro').fadeOut(300);
		$('#bday_wd_ro').fadeOut(300);
		$('#country_wd_ro').fadeOut(300);
		$('#gender_wd_ro').fadeOut(300);
		
		$('#lastN-box').fadeOut(300);
		$('#userN-box').fadeOut(300);
		$('#pw-box').fadeOut(300);
		$('#Cpw-box').fadeOut(300);
		$('#email-box').fadeOut(300);
		$('#phone-box').fadeOut(300);
		$('#add-box').fadeOut(300);
		$('#city-box').fadeOut(300);
		$('#zip-box').fadeOut(300);
		$('#state-box').fadeOut(300);
		$('#country-box').fadeOut(300);
		$('#bday-box').fadeOut(300);
		$('#gender-box').fadeOut(300);
		$('#up_photo-box').fadeOut(300);
		$('#deny-btn').fadeOut(300);
		$('#pickfile-btn-orange').fadeIn(300);
		$('#pickfile-btn-grey').fadeOut(300);
		
		$('#reset-btn-ro').fadeOut(300);
		$('#reset-btn').fadeOut(300);
		
		$('#create-btn-ro').fadeOut(300);
		$('#create-btn').fadeOut(300, function(){
		
			$('#firstN-inputbox').val('');
			$('#lastN-inputbox').val('');
			$('#userN-inputbox').val('');
			$('#pw-inputbox').val('');
			$('#Cpw-inputbox').val('');
			$('#email-inputbox').val('');
			$('#phone-inputbox').val('');
			$('#add1-inputbox').val('');
			$('#add2-inputbox').val('');
			$('#city-inputbox').val('');
			$('#zip-inputbox').val('');
			$('#state-inputbox').val('');
			$('#bday-inputbox').val('');
			$('#country-inputbox').val('none');
			$('#gender-inputbox').val('none');
			
			$('#uploded-photo-confirm').attr('checked', false);
			$('#uploded-photo-confirm').dgUncheck('#uploded-photo-confirm-span');
		
		});
		
	}

