﻿var frmContact
function getForm(){
    return  {
                Title:Ext.get("Title"),
                ContactName:Ext.get("ContactName"),
                EMail:Ext.get("EMail"),
                Phone:Ext.get("Phone"),
                Department:Ext.get("Department"),
                Contents:Ext.get("Contents"),
                CheckNumber:Ext.get("CheckNumber")
            }

}

function submitForm(){
    frmContact = getForm();
    var JSON = {}
    var JSONString = ""
    for(x in frmContact){
        if(x == 'Department'){
            var sOpt = frmContact[x].dom.getElementsByTagName("Option")[frmContact[x].dom.selectedIndex]
            JSON[x] = sOpt.text;
            JSON["SendMail"] = sOpt.value;
        }else{
            JSON[x] = frmContact[x].dom.value
        }
    }
    
    JSONString = Ext.util.JSON.encode(JSON)
    
    var cb=function(a,success,rspn){
    
        var JSON = eval("(" + rspn.responseText + ")");
        Ext.MessageBox.hide();
        
        if(eval(JSON.UpdateSuccess)){
            Ext.MessageBox.alert("Success",JSON.Message,clearForm,this);
        }else{
            Ext.MessageBox.alert("False",JSON.Message);
        }
    }.createDelegate(this);
    
    
    Ext.MessageBox.show({
        title: 'Send Contact...',
        msg: "Your Contact is sending , Please Wait...",
        width:260,
        progress:false,
        closable:false
    });
    
    

    if(JSON.Title && JSON.ContactName && JSON.EMail &&  JSON.Contents){
        if(eval(Ext.form.VTypes.email(JSON.EMail))){
            setTimeout(
                function(){
                    var cn=new Ext.data.Connection({url:"Services/login.aspx?Method=ContactUs"}).request({params:{"JSONPost":JSONString},scope:this,callback:cb}
                    )
                }.createDelegate(this),300
            )
        }else{
            Ext.Msg.alert("Error","Your Email Format is wrong")
        }
    }else{
        Ext.Msg.alert("Error","Please correctly fill all Fields");
    }
}

function clearForm(){
    frmContact = getForm()
    for(x in frmContact){
        if(frmContact[x].is("input")){
            frmContact[x].dom.value = "";
        }else if(frmContact[x].is("textarea")){
            frmContact[x].dom.innerHTML = "";
        }else{
            var options = frmContact[x].dom.getElementsByTagName("Option")[0].selected = true;
        }
    }
}

