var CustomerLoginForm = function( form_id, on_auth_url )
{
	var Form = $jq('#'+form_id).get(0);
	
	// sharing inputs for other component handlers
	this.id = form_id;
	this.email = Form.email;
	this.passwd = Form.passwd;
	
	this.submit = function() {
		$jq(Form).submit();
	}
	
	$jq(Form).submit(
		function() {
			var res = true;
			
			if ( !Form.email.value ) {
				res = false;
				Form_drawError(Form.email, 'Please enter your email');
			}
			
			if ( !Form.passwd.value ) {
				res = false;
				Form_drawError(Form.passwd, 'Please enter password');
			}
			
			if ( res ) {
				$jq.ajax({
					url: url_resp+'customer_auth.rsp.php',
					data: {
						email: Form.email.value,
						passwd: Form.passwd.value
					},
					success: function( rsp_data, status ) {
						if ( rsp_data.alert )
							alert(rsp_data.alert);
						
						if ( rsp_data.error_field ) {
							Form_drawError(Form[rsp_data.error_field], rsp_data.error_msg);
						}
						else if ( rsp_data.error_msg ) {
							Form_drawMessage(Form, rsp_data.error_msg, 'error');
						}
						else if ( rsp_data.success ) {
							Form_drawMessage(Form, 'Authentication success, please wait...', 'notice');
							window.location.href = on_auth_url;
						}
						
					}
				});
			}
			
			return false;
		}
	);
	
	
	$jq('#'+form_id+'_forgot_pass_btn').click(
		function() {
			if ( $jq('#customer_forgot_pass_form_container').length ) {
				$jq('#'+form_id+'_container').hide();
				$jq('#customer_forgot_pass_form_container').show();
			}
			else {
				$jq.ajax({
					url: url_resp+'customer_forgot_pass.rsp.php',
					data: {exec: '#load_form'},
					success: function( data ) {
						$jq('#'+form_id+'_container').hide().after(data.form_html);
						window.customer_forgot_pass_form = new CustomerForgotPassForm(data.form_id);
					}
				});
			}
			
			return false;
		}
	);
}


var CustomerForgotPassForm = function( form_id )
{
	var Form = $jq('#'+form_id).get(0);
	
	// sharing input for other component handlers
	this.email = Form.email;
	
	$jq(Form).submit(
		function() {
			if ( !Form.email.value ) {
				res = false;
				Form_drawError(Form.email, 'Please enter your email');
			}
			else {
				$jq.ajax({
					url: url_resp+'customer_forgot_pass.rsp.php',
					data: {
						exec: Form.exec_cmd.value,
						email: Form.email.value
					},
					success: function( rsp_data, status ) {
						if ( rsp_data.alert )
							alert(rsp_data.alert);
						
						if ( rsp_data.error_field ) {
							Form_drawError(Form[rsp_data.error_field], rsp_data.error_msg);
						}
						else if ( rsp_data.error_msg ) {
							Form_drawMessage(Form, rsp_data.error_msg, 'error');
						}
						else if ( rsp_data.success_msg ) {
							alert(rsp_data.success_msg);
							$jq('.back_btn', Form).click();
						}
						
					}
				});
			}
			
			return false;
		}
	);
	
	
	$jq('.back_btn', Form).click(
		function() {
			$jq('#'+form_id+'_container').hide();
			$jq('#'+window.customer_login_form.id+'_container').show();
			return false;
		}
	);
	
}

