/* Require ui/dialog.js
 * 
 * 
 * 
 */

var CVDialogForm = function() {
	this.fields = [];
	this.buttons = [];
	this.title = "";
	this.info = "";
	this.action = "";
	this.id = "popAutoForm";
	this.customCallback = false;

	this._buildForm = function() {
		var content = "";
		content += '<form method="post" action="/'+this.action+'/" class="popAutoForm" id="'+this.id+'">';
		if (this.info) {
			content += '<p class="pFormInfo">'+this.info+'</p>';
		}
		for (var i in this.fields) {
			if (this.fields[i].type == 'hidden') {
				// HIDDEN FIELD
				content += '<input type="hidden" name="' + this.fields[i].name + '" id="id_' + this.fields[i].name + '" value="">';
			}
			else if (this.fields[i].type == 'info') {
				// FREE TEXT BLOCK
				content += '<div class="frmTextRow' + (this.fields[i].extra_class ? ' ' + this.fields[i].extra_class : '') + '">' + this.fields[i].title + '</div>';
			} else if (this.fields[i].type == 'text') {
				// TEXT INPUT FIELD
				content += '<div class="frmRow">';
				content += '<label for="id_' + this.fields[i].name + '">' + this.fields[i].title + ':' + (this.fields[i].required ? '<span class="req">*</span>' : '') + '</label>';
				content += '<div class="input"><input type="text" name="' + this.fields[i].name + '" id="id_' + this.fields[i].name + '" value=""' + (this.fields[i].extra_class ? ' class="' + this.fields[i].extra_class + '"' : '') + ' /></div>';
				if (this.fields[i].hint) 
					content += '<div class="hint">' + this.fields[i].hint + '</div>';
				content += '</div>';
			} else if (this.fields[i].type == 'static') {
				// STATIC FIELD
				content += '<div class="frmRow">';
				content += '<label for="id_' + this.fields[i].name + '">' + this.fields[i].title + ':</label>';
				content += '<div class="static" id="id_' + this.fields[i].name + '">&nbsp;</div>';
				if (this.fields[i].hint) 
					content += '<div class="hint">' + this.fields[i].hint + '</div>';
				content += '</div>';
			} else if (this.fields[i].type == 'select') {
				// SELECT FIELD
				content += '<div class="frmRow">';
				content += '<label for="id_' + this.fields[i].name + '">' + this.fields[i].title + ':' + (this.fields[i].required ? '<span class="req">*</span>' : '') + '</label>';
				content += '<div class="input"><select size="1" name="' + this.fields[i].name + '" id="id_' + this.fields[i].name + '"' + (this.fields[i].extra_class ? ' class="' + this.fields[i].extra_class + '"' : '') + '>';
				content += '<option value="">' + (this.fields[i].default_text ? this.fields[i].default_text : '-- Please Select --') + '</option>';
				if (this.fields[i].options) 
					for (var x in this.fields[i].options) 
						content += '<option value="' + this.fields[i].options[x].value.replace('"', '&quot;') + '">' + this.fields[i].options[x].name + '</option>';
				content += '</select></div>';
				if (this.fields[i].hint) 
					content += '<div class="hint">' + this.fields[i].hint + '</div>';
				content += '</div>';
			} else if (this.fields[i].type == 'textarea') {
				// TEXTAREA FIELD
				content += '<div class="frmTextareaRow">';
				content += '<label for="id_' + this.fields[i].name + '">' + this.fields[i].title + ':' + (this.fields[i].required ? '<span class="req">*</span>' : '') + '</label>';
				content += '<textarea name="' + this.fields[i].name + '" id="id_' + this.fields[i].name + '"' + (this.fields[i].extra_class ? ' class="' + this.fields[i].extra_class + '"' : '') + '></textarea>';
				if (this.fields[i].hint) 
					content += '<div class="hint">' + this.fields[i].hint + '</div>';
				content += '</div>';
			} else if (this.fields[i].type == 'checkbox') {
				// CHECKBOX FIELD
				content += '<div class="frmCheckboxRow">';
				content += '<input type="checkbox" name="' + this.fields[i].name + '" id="id_' + this.fields[i].name + '"' + (this.fields[i].extra_class ? ' class="' + this.fields[i].extra_class + '"' : '') + ' value="1" />';
				content += '<label for="id_' + this.fields[i].name + '">' + this.fields[i].title + (this.fields[i].required ? '<span class="req">*</span>' : '') + '</label>';
				if (this.fields[i].hint) 
					content += '<div class="hint">' + this.fields[i].hint + '</div>';
				content += '</div>';
			}
		}
		content += '<div class="frmBtnRow">';
		for (var i in this.buttons) {
			var classname = "bt_sec";
			if (this.buttons[i].extra_class) classname = this.buttons[i].extra_class;
			else if (this.buttons[i].type == 'submit') classname = 'bt_prim';
			content += '<button id="btn_id_' + this.buttons[i].name + '" class="bt_shadow_norm ' + classname + '"'+ (this.buttons[i].type == 'submit'?' type="submit"':'') +'>' + this.buttons[i].title + '</button> ';			
		}
		content += '</div>';
		content += '</form>';
		content += '<div class="popAutoFromPreload" id="'+this.id+'Preload"></div>';
		return content;
	}

	this._fillFormData = function() {
		for (var i in this.fields) {
			if (this.fields[i].type == 'checkbox') {
				if (this.fields[i].value) $('#id_' + this.fields[i].name).attr('checked', 'checked');
			} else if (this.fields[i].type == 'static') {
				
				$('#id_' + this.fields[i].name).text(this.fields[i].value);
			} else {
				$('#id_' + this.fields[i].name).val(this.fields[i].value);
			}
		}
	}
	
	this._getFormData = function() {
		return $('#' +this.id).serializeArray();
	}

	this._checkForm = function() {
		var valid = true;
		for (var i in this.fields) {
			if (this.fields[i].required) {
				if (this.fields[i].type == 'checkbox') {
					valid = valid && $('#id_' + this.fields[i].name).is(':checked');
				} else {
					valid = valid && $('#id_' + this.fields[i].name).val();
				}
			}
		}
		return valid;
	}

	this._updateForm = function() {
		var valid = this._checkForm();
		for (var i in this.buttons) {
			if (this.buttons[i].type == 'submit') {
				if (valid) $('#btn_id_' + this.buttons[i].name).removeClass('bt_disabled_prim').addClass('bt_prim');
				else $('#btn_id_' + this.buttons[i].name).removeClass('bt_prim').addClass('bt_disabled_prim');
			}
		}
		return valid;
	}

	this._submitForm = function() {
		var valid = this._updateForm();
		if (valid) {
			$('#'+this.id+'Preload').height($('#'+this.id).height());
			$('#'+this.id).hide();
			$('#'+this.id+'Preload').show();
			var data = this._getFormData();
			$.post('/'+this.action+'/', data, dialogFormPostCallback, 'json');
			
		}
	}

	this._postCallback = function(responce) {
		if (responce.result) {
			if (this.customCallback) this.customCallback();
			else {
				cvDialog.hide();
				window.location.replace(responce.link)
			}
		} else {
			$('#'+this.id+' .pFormInfo').html(responce.error_message + (responce.error_text?'<span>'+responce.error_text+'</span>':'')).addClass('msg_warn_sm');		
			$('#'+this.id+'Preload').hide();
			$('#'+this.id).show();
			cvDialog.resize(500);
		}
	}

	this._initButtons = function() {
		for (var i in this.buttons) {
			if (this.buttons[i].type == 'cancel') {
				$('#btn_id_' + this.buttons[i].name).click(function() {
					cvDialog.hide();
					return false;
				});
			}
		}
	}

	this._buildFormPopup = function() {
		var content = this._buildForm();
		cvDialog.resize(500);
		cvDialog.attach(this.title, content);
		cvDialog.show();
		cvDialog.resize(500);
		this._fillFormData();
		this._updateForm();
		function validateForm() { cvDialogForm._updateForm(); }
		$('input, select, textarea', $('#'+this.id)).click(validateForm).change(validateForm).keyup(validateForm);	
	}

	this.show = function(title, fields, buttons, info, action, id, customCallback) {
		if (!title || !fields || !action) return false;
		this.title = title;
		this.fields = fields;
		this.action = action;
		this.info = info;
		if (buttons && buttons.length) this.buttons = buttons;
		else this.buttons = [
			{'name' : 'submit', 'type' : 'submit', 'title' : 'Submit' },
			{'name' : 'cancel', 'type' : 'cancel', 'title' : 'Cancel' }
		]
		if (id) this.id = id;
		else this.id = "popAutoForm";
		if (customCallback) this.customCallback = customCallback;
		else this.customCallback = false;
		this._buildFormPopup();
		this._initButtons();
		$('#' + this.id).submit(function(){
			cvDialogForm._submitForm();
			return false;
		});
	}


	
}

cvDialogForm = new CVDialogForm();

function dialogFormPostCallback(responce) {
	cvDialogForm._postCallback(responce);
}

