/**
 * @author marco
 */
YAHOO.namespace("estela.mailForm.prepare");
YAHOO.namespace("estela.mailForm.show");
YAHOO.namespace("estela.mailForm.dinamicDialogUser");
YAHOO.estela.mailForm.dinamicDialogUser= function(text){
	   // Define various event handlers for Dialog
	var handleYesSaved = function() {
		this.hide();
	};


    var dinamicDialog=new YAHOO.widget.SimpleDialog("mailFormDialog", 
		 { width: "300px",
		   fixedcenter: true,
		   visible: false,
		   draggable: false,
		   modal: true,
		   close: false,
		   text: text,
		   icon: YAHOO.widget.SimpleDialog.ICON_INFO,
		   constraintoviewport: true,
		   buttons: [ { text:"Aceptar", handler:handleYesSaved, isDefault:true }]
		 } );
    dinamicDialog.setHeader("¡Información!");
    dinamicDialog.render(document.body);
    dinamicDialog.show();
    
}
YAHOO.estela.mailForm.prepare=function(){

var mainDiv=document.createElement('div');
mainDiv.id="mailForm";
var hdDiv=document.createElement('div');
hdDiv.className="hd";
var hdTitle=document.createTextNode('Formulario de correo');
hdDiv.appendChild(hdTitle);
mainDiv.appendChild(hdDiv);
var bdDiv=document.createElement('div');
bdDiv.className="bd";
var mailForm=document.createElement('form');
mailForm.method="POST";
mailForm.action=" ";
var divSender=document.createElement('div');
divSender.id="mailFormdivs";
var senderName_label=document.createElement('label');
senderName_label.htmlFor='senderName';
var senderName_labelText=document.createTextNode('Nombre: ');
var senderName=document.createElement('input');
senderName.type='text';
senderName.name='senderName';
senderName_label.appendChild(senderName_labelText);
divSender.appendChild(senderName_label);
divSender.appendChild(senderName);
mailForm.appendChild(divSender);
var divSenderMail=document.createElement('div');
divSenderMail.id="mailFormdivs";
var senderMail_label=document.createElement('label');
senderMail_label.htmlFor='senderMail';
var senderMail_labelText=document.createTextNode('e-mail: ');
var senderMail=document.createElement('input');
senderMail.type='text';
senderMail.name='senderMail';

senderMail_label.appendChild(senderMail_labelText);
divSenderMail.appendChild(senderMail_label);
divSenderMail.appendChild(senderMail);
mailForm.appendChild(divSenderMail);
var divRecipent=document.createElement('div');
divRecipent.id="mailFormdivs";
var recipentName_label=document.createElement('label');
recipentName_label.htmlFor='recipentName';
var recipentName_labelText=document.createTextNode('Nombre del destinatario: ');
var recipentName=document.createElement('input');
recipentName.type='text';
recipentName.name='recipentName';
recipentName_label.appendChild(recipentName_labelText);
divRecipent.appendChild(recipentName_label);
divRecipent.appendChild(recipentName);
mailForm.appendChild(divRecipent);
var divRecipentMail=document.createElement('div');
divRecipentMail.id="mailFormdivs";
var recipentMail_label=document.createElement('label');
recipentMail_label.htmlFor='recipentMail';
var recipentMail_labelText=document.createTextNode('e-mail del destinatario: ');
var recipentMail=document.createElement('input');
recipentMail.type='text';
recipentMail.name='recipentMail';
recipentMail_label.appendChild(recipentMail_labelText);
divRecipentMail.appendChild(recipentMail_label);
divRecipentMail.appendChild(recipentMail);
mailForm.appendChild(divRecipentMail);
var divComments=document.createElement('div');
divComments.id="mailFormdivs2";
var comments=document.createElement('textarea');
comments.name='comments';
var comments_label=document.createElement('label');
comments_label.htmlFor='comments';
var comments_labelText=document.createTextNode('Comentarios: ');
comments_label.appendChild(comments_labelText);
divComments.appendChild(comments_label);
divComments.appendChild(comments);
mailForm.appendChild(divComments);
/**
 * campos ocultos
 */
var componente=document.createElement('input');
componente.type='hidden';
componente.name='component';
componente.value='editor';
mailForm.appendChild(componente);
var file=document.createElement('input');
file.type='hidden';
file.name='file';
file.value='getMailContents';
mailForm.appendChild(file);
var instance=document.createElement('input');
instance.type='hidden';
instance.name='instance';
instance.id='mailForm_instance';
var node=document.createElement('input');
node.type='hidden';
node.name='node';
node.id='mailForm_node';
mailForm.appendChild(instance);
mailForm.appendChild(node);
bdDiv.appendChild(mailForm);
mainDiv.appendChild(bdDiv);
document.body.appendChild(mainDiv);
}
YAHOO.util.Event.onDOMReady(YAHOO.estela.mailForm.prepare);

/**
 * instanciamos el dialogo
 */

YAHOO.namespace("estela.mailForm.init");

YAHOO.estela.mailForm.init = function() {
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		var response = o.responseText;
		YAHOO.estela.mailForm.dinamicDialogUser(response);
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.estela.mailForm.dialog1 = new YAHOO.widget.Dialog("mailForm", 
							{ width : "30em",
							  fixedcenter : true,
                              modal: true,
							  visible : false, 
							  constraintoviewport : true,
							  buttons : [ { text:"Enviar", handler:handleSubmit, isDefault:true },
								      { text:"Cancelar", handler:handleCancel } ]
							});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.estela.mailForm.dialog1.validate = function() {
		var data = this.getData();
		if (data.senderName == "" || data.recipentName == "") {
			alert("Por favor proporcione el nombre del remitente y destinatario.");
			return false;
		} else {
			if (data.senderMail == "" || data.recipentMail == "") {
				alert("Por favor proporcione el correo del remitente y destinatario.");
			return false;
			}
			else {
				return true;
			}
		}
        return true;
	};

	// Wire up the success and failure handlers
	YAHOO.estela.mailForm.dialog1.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.estela.mailForm.dialog1.render();
}

YAHOO.util.Event.onDOMReady(YAHOO.estela.mailForm.init);
YAHOO.estela.mailForm.show = function(nodo, instancia, ruta){
	document.getElementById('mailForm_node').value = nodo;
	document.getElementById('mailForm_instance').value = instancia;
	document.getElementById('formularioCorreo').action = ruta + "/";
	YAHOO.estela.mailForm.dialog1.show();
}
