﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Cachinko");

Cachinko.DynamicPostBehavior = function(element) {
    Cachinko.DynamicPostBehavior.initializeBase(this, [element]);
    this._url = '';
    this._textControlsCollection = null;    
    this._clickDelegate = null;
    this._validationGroupText = '';
    this._hfUsertoConnect = null;
}


Cachinko.DynamicPostBehavior.prototype = {
    initialize: function() {
        Cachinko.DynamicPostBehavior.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        this._clickDelegate = Function.createDelegate(this,this._clickHandler);
        $addHandler(this.get_element(),"click",this._clickDelegate);
    },
    dispose: function() {        
        //Add custom dispose actions here
        Cachinko.DynamicPostBehavior.callBaseMethod(this, 'dispose');
    },
    _clickHandler : function( e ){
        if (e.rawEvent.cancelBubble) return;
        if(this._validate())
        {
            e.preventDefault();
            e.stopPropagation();
            this._sendDynamicPost();
        }        
    },
    _sendDynamicPost : function(){
        var textBoxes = this._textControlsCollection.split(';');
        var textBox;
        var qs = '';
        for(index in textBoxes)
        {
            qs = qs + $get(textBoxes[index]).value + '&';
        }
        qs= qs.substr(0,qs.lastIndexOf('&'));
        
        if( this._hfUsertoConnect != null )
        {
          if( this._hfUsertoConnect.value.trim().length > 0)
            qs= qs + '&'+ this._hfUsertoConnect.value.trim();
        } 
        
        this._form = document.createElement("form");
        atr = document.createAttribute("id");
        atr.value = 'frmLogin';
        this._form.setAttributeNode(atr);
        atr = document.createAttribute("action");
        atr.value = this._url.replace('http:','https:');
        this._form.setAttributeNode(atr);
        atr = document.createAttribute("method");
        atr.value = 'post';
        this._form.setAttributeNode(atr);
        
        
        this._hdnFormData = document.createElement("input");
        atr = document.createAttribute("type");
        atr.value = 'hidden';
        this._hdnFormData.setAttributeNode(atr);
        atr = document.createAttribute("id");
        atr.value = 'hdnFormData';
        this._hdnFormData.setAttributeNode(atr);
        atr = document.createAttribute("name");
        atr.value = 'hdnFormData';        
        this._hdnFormData.value = qs;        
        this._hdnFormData.setAttributeNode(atr);
        this._form.appendChild(this._hdnFormData);        

        this._hdnUrl = document.createElement("input");
        atr = document.createAttribute("type");
        atr.value = 'hidden';
        this._hdnUrl.setAttributeNode(atr);
        atr = document.createAttribute("id");
        atr.value = 'hdnUrl';
        this._hdnUrl.setAttributeNode(atr);
        atr = document.createAttribute("name");
        atr.value = 'hdnUrl';        
        this._hdnUrl.value = document.location.href;
        this._hdnUrl.setAttributeNode(atr);
        
        this._form.appendChild(this._hdnUrl); 
                
        document.body.appendChild(this._form);
        
        this._form.submit();

    },
    _validate: function()
    {
        var validateFlag = Page_ClientValidate(this._validationGroupText);
        return validateFlag;
  
    },    
    get_validationGroupText : function(){
        return this._validationGroupText;
    },
    set_validationGroupText : function( value ){
        this._validationGroupText = value ;
    },
    get_url : function(){
        return this._url;
    },
    set_url : function( value ){
        this._url = value;
    },    
    get_textControlsCollection : function(){
        return this._textControlsCollection;
    },
    set_textControlsCollection : function( value ){
        this._textControlsCollection = value;
    },
    get_hfUsertoConnect : function(){
        return this._hfUsertoConnect;
    },
    set_hfUsertoConnect : function( value ){
        this._hfUsertoConnect = value;
    }
}
Cachinko.DynamicPostBehavior.registerClass('Cachinko.DynamicPostBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
