var become_partner=function(lnk_hider,save_handler,div_container,div_buttons,tbname,tbcountry,tbcity,t_wapadress,rbwebmoney,rbyandexmoney,rbpostmoney,tbpaydata,tbemail,tbicq,info_texts)
{
    this.Init(lnk_hider,save_handler,div_container,div_buttons,tbname,tbcountry,tbcity,t_wapadress,rbwebmoney,rbyandexmoney,rbpostmoney,tbpaydata,tbemail,tbicq,info_texts);
}

become_partner.prototype={
    Init:function(lnk_hider,save_handler,div_container,div_buttons,tbname,tbcountry,tbcity,tbwapadress,rbwebmoney,rbyandexmoney,rbpostmoney,tbpaydata,tbemail,tbicq,info_texts)
    {            
        //ссылка по нажатию на которую скрывается панель редактирования
        this.lnk_hider = dxsoft_library.GetElement(lnk_hider);   
        
        //DIV - контейнер, в котором расположены все элементы управления
        this.div_container = dxsoft_library.GetElement(div_container); 
        
        //Handler Для сохранения
        this.SaveHandler = save_handler;
        
        //Получаем элементы управления
        this.tbName = dxsoft_library.GetElement(tbname); 
        this.tbCountry = dxsoft_library.GetElement(tbcountry); 
        this.tbCity = dxsoft_library.GetElement(tbcity);
        this.tbWapAdress = dxsoft_library.GetElement(tbwapadress);
        this.rbWebMoney = dxsoft_library.GetElement(rbwebmoney);
        this.rbYandexMoney = dxsoft_library.GetElement(rbyandexmoney);
        this.rbPostMoney = dxsoft_library.GetElement(rbpostmoney);
        this.tbPayData = dxsoft_library.GetElement(tbpaydata);
        this.tbEmail = dxsoft_library.GetElement(tbemail);
        this.tbIcq = dxsoft_library.GetElement(tbicq);
               
        // выбираем кнопки (ok и cancel)
        this.table = dxsoft_library.GetElement(div_buttons);
        var inputs = this.table.getElementsByTagName("input");        
        for(var i=0;i<inputs.length;++i)
            if(inputs[i].className.indexOf("cancel")>=0)
                this.btncancel = inputs[i];
            else
                this.btnok = inputs[i]; 
        
         // текст с сообщениями (типа ошибок)
        this.info_texts = info_texts;
        for(var a in this.info_texts)
            this.info_texts[a] =  dxsoft_library.DecodeText(this.info_texts[a]);        
        
        // создаем preloader
        this.preloader = new dxpreloader();        
        
        dxsoft_library.DoTask(this.btncancel,dxsoft_library.CreateMethod(this,this.OnCancel),"click");                
        dxsoft_library.DoTask(this.btnok,dxsoft_library.CreateMethod(this,this.OnOk),"click");
        dxsoft_library.DoTask(this.lnk_hider,dxsoft_library.CreateMethod(this.div_container,this.OnLinkClick),"click");        
    },
    
    isValidEmail:function (email)
    {
        return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
    },
    
    ChekMandatory:function()    
    {
        var haserror = false;
        if(this.tbName.value == "")
        {
            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbName.parentNode,this.tbName.parentNode.offsetWidth+"px","-14px");                                    
            haserror = true;
        }
        else
        if(this.tbCountry.value == "")
        {
            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbCountry.parentNode,this.tbCountry.parentNode.offsetWidth+"px","-14px");                                    
            haserror = true;
        }
//        else
//        if(this.tbCity.value == "")
//        {
//            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbCity.parentNode,this.tbCity.parentNode.offsetWidth+"px","-14px");                                    
//            haserror = true;
//        }
        else
        if(this.tbWapAdress.value == "")
        {
            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbWapAdress.parentNode,this.tbWapAdress.parentNode.offsetWidth+"px","-14px");                                    
            haserror = true;
        }  
//        else
//        if(this.tbPayData.value == "")
//        {
//            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbPayData.parentNode,this.tbPayData.parentNode.offsetWidth+"px","-14px");                                    
//            haserror = true;
//        }                
        else
        if(this.tbEmail.value == "")
        {
            dxerror_baloon.ShowError(this.info_texts.mandatoryfield,this.tbEmail.parentNode,this.tbEmail.parentNode.offsetWidth+"px","-14px");                                    
            haserror = true;
        }                       
        else
        if(!this.isValidEmail(this.tbEmail.value))
        {
            dxerror_baloon.ShowError(this.info_texts.dataerror,this.tbEmail.parentNode,this.tbEmail.parentNode.offsetWidth+"px","-14px");                                    
            haserror = true;
        }
        return haserror;    
    },
    
    OnLinkClick:function()
    {
        if(this.style.display == "none")        
            this.style.display = "inline";                    
        else        
            this.style.display = "none";                    
    },
                    
    OnCancel:function()
    {   
        this.div_container.style.display = "none";
    },    
    
    OnOk:function()
    {         
        if(!this.ChekMandatory())
        {        
            this.preloader.Show(this.div_container);
                        
//            var typemoney = 1;
//                        
//            if(this.rbWebMoney.checked)
//            typemoney = 1;
//            else    
//            if(this.rbYandexMoney.checked)
//            typemoney = 2;
//            else                
//            typemoney = 3;
            
            var result="<main>";
            var uid = dxsoft_library.MakeUID();
            result+="<uid>"+uid+"</uid>";            
            result+="<name>"+dxsoft_library.EncodeText(this.tbName.value)+"</name>";
            result+="<country>"+dxsoft_library.EncodeText(this.tbCountry.value)+"</country>";
            ////result+="<city>"+dxsoft_library.EncodeText(this.tbCity.value)+"</city>";
            result+="<wap>"+dxsoft_library.EncodeText(this.tbWapAdress.value)+"</wap>";
            result+="<paydata>"+dxsoft_library.EncodeText(this.tbPayData.value)+"</paydata>";
            result+="<email>"+dxsoft_library.EncodeText(this.tbEmail.value)+"</email>";
            result+="<icq>"+dxsoft_library.EncodeText(this.tbIcq.value)+"</icq>";                                    
           // result+="<typemoney>"+typemoney+"</typemoney>";
            result+="</main>";
                                           
            this.xml_request=dxsoft_library.CreateXMLRequest();       
            this.xml_request.onreadystatechange = dxsoft_library.CreateMethod(this,this.OnSaveComplete);        
            this.xml_request.open("POST",this.SaveHandler+"&uid="+uid,true);         
            this.xml_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");                                                
            this.xml_request.send("data="+result);
                                                            
        }        
    },
    OnSaveComplete:function()
    {
        if(this.xml_request)
        {
            if(this.xml_request.readyState == 4)
            {                                        
                if(this.xml_request.status == 200 && this.xml_request.responseText=="ok")
                {                     
                    this.preloader.Hide();
                    dxerror_baloon.wait_time = 4000;
                    dxerror_baloon.ShowError(this.info_texts.claimaccepted,this.table,this.table.offsetWidth+3+"px","1px");                                        
                }
                else       
                {   
                    dxerror_baloon.ShowError(this.info_texts.servererror,this.table,this.table.offsetWidth+3+"px","1px");                                    
                    this.preloader.Hide();
                }
            }
        }  
    }
}

