
function FormDatePicker(element, warn) {
	if (!element) {
		this.fDiv = oEditor.FCK.EditorDocument.createElement( 'DIV' ) ;
		this.fDiv = oEditor.FCK.InsertElementAndGetIt( this.fDiv ) ;
		
		this.fSwitchDiv = oEditor.FCK.EditorDocument.createElement( 'DIV' ) ;
		this.fLabel = oEditor.FCK.EditorDocument.createElement( 'LABEL' );
		this.fSpan = oEditor.FCK.EditorDocument.createElement( 'SPAN' );
		this.fInput = oEditor.FCK.EditorDocument.createElement( 'INPUT' );
		this.ToolTipIMG     = oEditor.FCK.EditorDocument.createElement( 'IMG' );
		this.fCommentTextarea = oEditor.FCK.EditorDocument.createElement( 'TEXTAREA' ) ;
		
		this.fSpan.appendChild(this.fInput);
		this.fDiv.appendChild(this.fSwitchDiv);
		SetAttribute(this.fSwitchDiv, "class", "commentSwitch");
		
		this.fDiv.appendChild(this.fLabel);
		this.fDiv.appendChild(this.fSpan);
		
		this.fDiv.appendChild(this.ToolTipIMG);
		SetAttribute(this.ToolTipIMG, "class", "tooltipimg");
		SetAttribute(this.ToolTipIMG, "src", "images/help.gif");
		SetAttribute(this.ToolTipIMG, "align", "top");
		this.fDiv.appendChild(this.fCommentTextarea);
		this.fCommentTextarea.className='comment';
		
		
		this.fDivCl = new ClassList("form datepicker");
		SetAttribute(this.fLabel, "class", "controllCaption");
		
	} else {
		if (warn == undefined) warn = 0;
		this.fDiv = element;
		this.fLabel = this.fDiv.getElementsByTagName('label')[0];
		this.fSpan = this.fDiv.getElementsByTagName('span')[warn];
		this.fInput = this.fSpan.childNodes.item(0);
		this.fCommentTextarea = this.fDiv.getElementsByTagName('textarea')[0];
		
		this.fDivCl = new ClassList(DlgGetAttribute(this.fDiv, 'class'));
	}
	this.fDiv.contentEditable = false;
}

FormDatePicker.prototype.loadToDialog = function() {
    GetE('txtToolTip').value    = DlgGetAttribute(this.fDiv, 'title');

	GetE('txtLabel').value = this.fLabel.innerHTML;
	GetE('txtName').value = DlgGetAttribute(this.fDiv, 'ID');
	GetE('txtMinYear').value	= DlgGetAttribute( this.fDiv, 'minyear' ) ;
	GetE('txtMaxYear').value	= DlgGetAttribute( this.fDiv, 'maxyear' ) ;

	if (this.fDivCl.hasLabel('mandatory')) {
		GetE('txtMandatory').checked = true;
    GetE('txtLabel').value = GetE('txtLabel').value.replace(/<(strong|b).*/i,"");
	}
}

FormDatePicker.prototype.saveFromDialog = function() {

    SetAttribute( this.fDiv, 'title', GetE('txtToolTip').value) ;

	var lab = GetE('txtLabel').value;
	if (!lab.match('[0-9a-zA-ZáéíúõûüöóÁÉÍÚÕÛÜÖÓ]')) {
		this.fLabel.innerHTML = '';
		this.fDivCl.addLabel('nolabel');
	} else {
		this.fLabel.innerHTML = lab;
		this.fDivCl.removeLabel('nolabel');
	}

	this.fLabel.innerHTML = GetE('txtLabel').value;
  $('.kotelezo',this.fDivCl).remove();
	if (GetE('txtMandatory').checked) {
    this.fDivCl.addLabel('mandatory');
    var MandatorySign = oEditor.FCK.EditorDocument.createTextNode(' * ');
    var MandatorySignStrong = oEditor.FCK.EditorDocument.createElement( 'STRONG' );
    MandatorySignStrong.className = "kotelezo";
    MandatorySignStrong.appendChild(MandatorySign);
    this.fLabel.appendChild(MandatorySignStrong);
	} else {
		this.fDivCl.removeLabel('mandatory');
	}

	this.fDiv.className = this.fDivCl.toString();
	SetAttribute( this.fDiv, 'ID', GetE('txtName').value) ;

	SetAttribute( this.fInput, 'ID', GetE('txtName').value + '_input');
	SetAttribute( this.fInput, 'name', GetE('txtName').value + '_input') ;

	SetAttribute( this.fCommentTextarea, 'ID', GetE('txtName').value + '_comment');
	SetAttribute( this.fCommentTextarea, 'name', GetE('txtName').value + '_comment');
	
	SetAttribute( this.fDiv, 'minyear', GetE('txtMinYear').value) ;
	SetAttribute( this.fDiv, 'maxyear', GetE('txtMaxYear').value) ;
	
	SetAttribute( this.fInput, 'size', 10) ;
	SetAttribute( this.fInput, 'maxLength', 10) ;

	return true;
}

FormDatePicker.prototype.validate = function() {
	var spDate = this.fInput.value.split('.');
        var minYear = this.fDiv.getAttribute('minyear');
        var maxYear = this.fDiv.getAttribute('maxyear');

	if (spDate.length!=3 || isNaN(spDate[0]) || isNaN(spDate[1]) || isNaN(spDate[2])) {
		return false;
	}
	var myDate = new Date(spDate[0], spDate[1] - 1, spDate[2]);
	if (isNaN(myDate)) {
		return false;
	} else {
                // check for min- and max year
                if(minYear!=null && myDate.getFullYear() < minYear) {
                  return false;
                }
                if(maxYear!=null && myDate.getFullYear() > maxYear) {
                  return false;
                }

		var m = myDate.getMonth() + 1;
		var d = myDate.getDate();
		this.fInput.value  = myDate.getFullYear() + '.';
		this.fInput.value += (m < 10 ?'0':'') + m + '.';
		this.fInput.value += (d < 10 ?'0':'') + d;
		return true;
	}
	return false;
}

FormDatePicker.prototype.isEmpty = function() {
	if (!this.fInput.value.match('[0-9a-zA-ZáéíúõûüöóÁÉÍÚÕÛÜÖÓ]')) {
		this.fInput.value = '';
		return true;
	}
	return false;
} 