var BookingForm = new Class({
	
	Extends: Form,
	
	fieldProperties: {
		BOOK_NAME 		: {
			name	:"BOOK_NAME",
			label	:"BOOK_NAME",
			url		:"setbookname"},
			
		BOOK_SURNAME 	: {
			name	:"BOOK_SURNAME",
			label	:"BOOK_SURNAME",
			url		:"setbooksurname"},
			
		BOOK_EMAIL 		: {
			name	:"BOOK_EMAIL",
			label	:"BOOK_EMAIL",
			url		:"setbookemail"},
			
		BOOK_PHONE 		: {
			name	:"BOOK_PHONE",
			label	:"BOOK_PHONE",
			url		:"setbookphone"},
			
		CLIENT_NAME 	: {
			name	:"CLIENT_NAME",
			label	:"CLIENT_NAME",
			url		:"setclientname"},
			
		CLIENT_SURNAME 	: {
			name	:"CLIENT_SURNAME",
			label	:"CLIENT_SURNAME",
			url		:"setclientsurname"},
			
		CLIENT_EMAIL 	: {
			name	:"CLIENT_EMAIL",
			label	:"CLIENT_EMAIL",
			url		:"setclientemail"},
			
		CLIENT_PHONE 	: {
			name	:"CLIENT_PHONE",
			label	:"CLIENT_PHONE",
			url		:"setclientphone"},
			
		BOOK_CC 		: {
			name	:"BOOK_CC",
			label	:"BOOK_CC",
			url		:"setbookcc"},
			
		BOOK_CC_TYPE 	: {
			name	:"BOOK_CC_TYPE",
			label	:"BOOK_CC_TYPE",
			url		:"setbookcctype"},
			
		BOOK_CC_EXPIRATION_DATE : {
			name	:"BOOK_CC_EXPIRATION_DATE",
			label	:"BOOK_CC_EXPIRATION_DATE",
			url		:"setbookccexpirationdate"},
			
		BOOK_CC_SECURITY_CODE 	: {
			name	:"BOOK_CC_SECURITY_CODE",
			label	:"BOOK_CC_SECURITY_CODE",
			url		:"setbookccsecuritycode"}
	},
	
	initialize: function(container)
	{
		this.parent(container);
		this.makeForm();
	},
	
	makeForm: function()
	{
		this.addEvent("onFieldBlur", this.formfieldBlur.bind(this))
		
		this.addField(new FormField(this.fieldProperties.BOOK_NAME.name));
		this.addField(new FormField(this.fieldProperties.BOOK_SURNAME.name));
		this.addField(new FormField(this.fieldProperties.BOOK_EMAIL.name));
		this.addField(new FormField(this.fieldProperties.BOOK_PHONE.name));
		
		this.addField(new FormField(this.fieldProperties.CLIENT_NAME.name));
		this.addField(new FormField(this.fieldProperties.CLIENT_SURNAME.name));
		this.addField(new FormField(this.fieldProperties.CLIENT_EMAIL.name));
		this.addField(new FormField(this.fieldProperties.CLIENT_PHONE.name));
		
		this.setReplicantField(this.fieldProperties.BOOK_NAME.name		, this.fieldProperties.CLIENT_NAME.name);
		this.setReplicantField(this.fieldProperties.BOOK_SURNAME.name	, this.fieldProperties.CLIENT_SURNAME.name);
		this.setReplicantField(this.fieldProperties.BOOK_EMAIL.name	, this.fieldProperties.CLIENT_EMAIL.name);
		this.setReplicantField(this.fieldProperties.BOOK_PHONE.name	, this.fieldProperties.CLIENT_PHONE.name);
		
		
		this.addField(new FormField(this.fieldProperties.BOOK_CC.name));
		
		var ccTypeField = new RadioField(this.fieldProperties.BOOK_CC_TYPE.name)
		ccTypeField.setRadioOptions(this.getCCTypeOptions());
		
		this.addField(ccTypeField);
		this.addField(new ExpirationField(this.fieldProperties.BOOK_CC_EXPIRATION_DATE.name));
		this.addField(new FormField(this.fieldProperties.BOOK_CC_SECURITY_CODE.name));
		
		this.setFieldMask(this.fieldProperties.BOOK_CC_SECURITY_CODE.name, {mask:"9999"});
		this.setFieldMask(this.fieldProperties.BOOK_CC.name, {mask:"9999999999999999999999"});
		
		this.addLinkCallback(this.fieldProperties.BOOK_CC_SECURITY_CODE.name, "Que es esto?", this.ccSecurityHelp.bind(this));
	},
	
	getCCTypeOptions: function()
	{
		var options 	= {};
		options.visa 	= {};
		options.visa.label = "Visa";
		options.visa.value = "visa";
		
		options.electron 	= {};
		options.electron.label = "Electron - Visa";
		options.electron.value = "electron";
		
		options.jcb 	= {};
		options.jcb.label = "JCB";
		options.jcb.value = "jcb";
		
		options.mastercard 	= {};
		options.mastercard.label = "Mastercard";
		options.mastercard.value = "mastercard";
		
		options.americanexpress 	= {};
		options.americanexpress.label = "American express";
		options.americanexpress.value = "americanexpress";
		return options;
	},
	
	formfieldBlur: function(field)
	{
		var data = {};
		data[field.name] = field.getValue();
		
		var url = this.fieldProperties[field.name].url;
		
		//console.log(data);
		
		new Request.JSON({url:"public/bookingcontroller/"+url ,data:{"QUERY":JSON.encode(data)}, onComplete:function(response){
			//console.log(response);
		}}).send();
	},
	
	ccSecurityHelp: function(field)
	{
		if(!$defined(field.ccHelp))
		{
			field.ccHelp = new Element("div", {"class":"ccvHelp"}); 
			
		}
		
		if(field.ccHelp.getParent())
		{
			field.ccHelp.dispose();
		}
		else
		{
			field.ccHelp.inject(field.wrapper);
		}
	}
	
})
