YUI({base:'app/yui_3.0.0b1/build/'}).use('node','io-base','json', function(Y) {

    var sUrl = 'app/addres.php?add=';
    var data;
    MgAddChk = YUI.namespace('mg.addres');
    var addressPriv = false;
    var setAddrsPri = function(val){
      addressPriv = val;
    }
    MgAddChk.getAddrsPri = function(){
      return addressPriv;
    }
    var handleSuccess = function(ioId, o){
			//Y.log("The success handler was called.  Id: " + ioId + ".", "info", "example");
      //Y.log(o.responseText);
      try {
          data = Y.JSON.parse(o.responseText);
          //jsonData = data;
      }
      catch (e) {
          //alert("Invalid json data");
          data = Y.JSON.parse('{ "stat" : "false" }');
      }
      if(data.stat == 'false'){
        setAddrsPri(false);
        showPrompt();
      }
      else{
        setAddrsPri(true);
      }
    }
    var handleFailure = function(ioId, o){
			//Y.log("The failure handler was called.  Id: " + ioId + ".", "info", "example");

			if(o.responseText !== undefined){
				var s = "<li>Transaction id: " + ioId + "</li>";
				s += "<li>HTTP status: " + o.status + "</li>";
				s += "<li>Status code message: " + o.statusText + "</li>";
				//div.set("innerHTML", s);
			}
		};
    Y.on('io:success', handleSuccess);
		Y.on('io:failure', handleFailure);
    MgAddChk.onChange = function() {
        //alert('eeeee');
        //alert(e.target);
        //alert(Y.get(e.target).get('value'));
        //alert('mopo4');
        var request = Y.io(sUrl + Y.get('#postcode').get('value'));
    };
    
    var addField = Y.get('#postcode');
    if(addField != null){
      //.on('value', onChange);
      
      addField.on('blur', MgAddChk.onChange);
    }
    //alert('mopo2' + addField);
    var showPrompt = function(){
      var replyMsg = Y.get('#mg_msg_add_reply');
      replyMsg.setStyle("display", "none");
      dislog.show();

    }
    var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
		location.href='http://www.siistipiha.fi/kauppa';
	};
	var handleSuccess = function(o) {
    var replyMsgTh = Y.get('#mg_msg_add_reply');
    replyMsgTh.setStyle("display", "block");
		var response = o.responseText;
    var replyMsg = Y.get('#mg_msg_add_form');
    replyMsg.setStyle("display", "none");
    var replyMsgFt = Y.get('#msg_dialog_ft');
    replyMsgFt.setStyle("display", "none");
    dislog.show();
		
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	var dislog = new YAHOO.widget.Dialog("dialog1",
							{ width : "620px",
							  fixedcenter : true,
							  visible : false,
							  constraintoviewport : true,
                modal:true,
							  buttons : [ { text:"L&auml;het&auml;", handler:handleSubmit, isDefault:true },
								      { text:"Peruuta", handler:handleCancel } ]
							});

	// Validate the entries in the form to require that both first and last name are entered
	dislog.validate = function() {
        //alert('in valid');
		var data = this.getData();
		if (data.msg_nimi == "" || data.msg_puhelin == "" || data.msg_email == "" || data.msg_viesti == "") {
			alert("Palautelomakkeen kaikki kent�t ovat pakollisia t�ytt��.");
			return false;
		} else {
			return true;
		}
	};

	// Wire up the success and failure handlers
	dislog.callback = { success: handleSuccess,
						     failure: handleFailure };

	// Render the Dialog
  dislog.render();
});

