package com.ssdi.test.client;

import java.util.Date;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.DateChooser;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class FormValidation extends Window {

	protected Window editWindow;
	public final String PASSWORD_REG_EXP = "^(^([\\x00-\\x80]+))$";
	public final static String GROUP_NAME_REG_EXP2 = "[\\S]";
	protected RegExpValidator passwordStringValidator = new RegExpValidator();
	protected RegExpValidator passwordStringValidatorForSpace = new RegExpValidator();
	DynamicForm dForm = new DynamicForm();
	final PasswordItem passwordTextItem = new PasswordItem();
	final PasswordItem passwordConfirmationTextItem = new PasswordItem();
    DateChooser datetimeChooser = new DateChooser();
    DateItem startDateItem;
	public void editfunction() {
		editWindow = new Window();
		editWindow.setHeight(230);
		editWindow.setWidth(550);
		editWindow.centerInPage();
		editWindow.setCanDragResize(true);

		VLayout vlay = new VLayout();
		vlay.setAlign(VerticalAlignment.TOP);
		vlay.setMembersMargin(5);
		vlay.addMembers(getStudentDynamicForm());

		HLayout hLay = new HLayout(10);
		hLay.setMembers(getBottomLayout());
		hLay.setAlign(Alignment.CENTER);
		VLayout main = new VLayout(10);
		main.setAlign(Alignment.CENTER);
		main.addMembers(vlay, hLay);

		editWindow.setTitle("Edit Window");
		editWindow.addItem(main);
		editWindow.show();
	}

	public DynamicForm getStudentDynamicForm() {

		dForm = new DynamicForm();
		dForm.setRequiredTitlePrefix("");
		dForm.setColWidths(155);

		passwordStringValidator.setExpression(PASSWORD_REG_EXP);
		passwordStringValidator.setStopIfFalse(true);
		passwordStringValidator.setStopOnError(true);

		passwordStringValidatorForSpace.setExpression(GROUP_NAME_REG_EXP2);
		passwordStringValidatorForSpace.setStopIfFalse(true);

		passwordTextItem.setTitle("password");
		passwordTextItem.setName("passwordTextItemField");
		passwordTextItem.setCanEdit(true);
		passwordTextItem.setWidth(250);
		passwordTextItem.setHeight("25px");
		passwordTextItem.setRequired(true);
		passwordTextItem.setShowErrorStyle(false);
		passwordTextItem.setValidateOnExit(false);

		passwordTextItem.setValidators(passwordStringValidator,
				passwordStringValidatorForSpace);
		passwordTextItem.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {

				if (!(passwordTextItem.validate())) {
					passwordTextItem.clearValue();

				} else {
				}

			}
		});

		passwordConfirmationTextItem.setTitle("Confirm password");
		passwordConfirmationTextItem.setName("passwordConfirmationItemField");
		passwordConfirmationTextItem.setCanEdit(true);
		passwordConfirmationTextItem.setWidth(250);
		passwordConfirmationTextItem.setHeight("25px");
		passwordConfirmationTextItem.setRequired(true);
		passwordConfirmationTextItem.setShowErrorStyle(false);
		passwordConfirmationTextItem.setWrapTitle(false);

		passwordConfirmationTextItem.setValidators(passwordStringValidator,
				passwordStringValidatorForSpace);
		passwordConfirmationTextItem.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				if (!(passwordConfirmationTextItem.validate())) {

					passwordConfirmationTextItem.setValue("");

				} else {	}
			}
		});

		
		startDateItem = new DateItem();
	    startDateItem.setTitle("Select Date");
	    startDateItem.setName("counterStartDateItem");
	    startDateItem.setValue(new Date());
	    startDateItem.setRequired(true);
	    startDateItem.setShowErrorStyle(false);
	    startDateItem.setTitleOrientation(TitleOrientation.LEFT);
	    startDateItem.setAlign(Alignment.LEFT);
	    startDateItem.setEnforceDate(true);
	    startDateItem.setTitleColSpan(1);
	    startDateItem.setPickerProperties(datetimeChooser);
	    startDateItem.setWidth(180);
	    startDateItem.setStopOnError(true);
	    startDateItem.setUseTextField(true);
	//    startDateItem.setTextAlign(Alignment.LEFT);
	   
	    startDateItem.addChangedHandler(new ChangedHandler() {
			
			@Override
			public void onChanged(ChangedEvent event) {
			
				startDateItem.validate();
			}
		});
		dForm.setItems(passwordTextItem, passwordConfirmationTextItem,startDateItem);
		return dForm;

	}

	public HLayout getBottomLayout() {
		HLayout footer=new HLayout(10);
		footer.setAlign(Alignment.CENTER);
		IButton ok = new IButton("OK");
		ok.setWidth(50);
		ok.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (dForm.validate() ) {// checking password limitation

					MatchesFieldValidator passwordMatchvalidator = new MatchesFieldValidator();
					passwordMatchvalidator
							.setOtherField("passwordConfirmationItemField");
					passwordMatchvalidator
							.setErrorMessage("Password and password confirmation are not same.");
					passwordTextItem.setValidators(passwordMatchvalidator);

				} 
			}

		});
		
		IButton cancel=new IButton("cancel");
		cancel.setWidth(50);
		cancel.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				editWindow.destroy();
			}
		});
		footer.addMembers(ok,cancel);
		return footer;
	}



}
