﻿var l_TenantName = document.getElementById("TenantName");
var l_Email = document.getElementById("Email");
var l_ConfirmEmail = document.getElementById("ConfirmEmail");
var l_PropertyAddr = document.getElementById("PropertyAddress");
var l_ContactTelephone = document.getElementById("ContactTelephone");
var l_CardType = document.getElementById("CardType");
var l_Rent = document.getElementById("Rent");
var l_Fee = document.getElementById("Fee");
var l_Bond = document.getElementById("Bond");
var l_Other = document.getElementById("Other");
var l_Message = document.getElementById("Message");
var l_BilName = document.getElementById("BillingName");
var l_BilSurname = document.getElementById("BillingSurname");
var l_BilPostcode = document.getElementById("BillingPostcode");
var l_BilAddr1 = document.getElementById("BillingAddress1");
var l_BilAddr2 = document.getElementById("BillingAddress2");
var l_BilAddr3 = document.getElementById("BillingAddress3");
var l_BilTown = document.getElementById("BillingTown");
var l_BilCounty = document.getElementById("BillingCounty");
var l_BilCountry = document.getElementById("BillingCountry");
l_TenantName.focus();
// Function to valide Email Syntax
function IsValidEmail(email){
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    if(email.match(emailRegEx)){
        return true;
    }else{        
    return false;
    }
}
function filterInput(e,operator)
    {
//     var key;
//     if(window.event)
//          key = window.event.keyCode;     //IE
//     else
//          key = e.which;     //firefox
//    if (key == 13)
//        { 
//            if(pSuppressEnter != "True")
//                {SavePayments();return false;}
//        }
//    // for Numbers including decimal
//    if(operator == "number")
//        {
//        if(!((key >= 48 && key <= 57) || key == 46 || key == 8 || key == 190))
//            {
//            return false;
//            }
//        }
//    // for Name having spaces
//    else if(operator == "name")
//        {
//        if(!((key >= 65 && key <= 90) || (key >= 65 && key <= 97) || key == 122 || key == 8 || key == 190 || key == 32))
//            {
//            return false;
//            }
//        }
//    // for email
//    else if(operator == "email")
//        {
//        
//        }
//    // for telephone number
//    else if(operator == "telephone")
//        {
//        
//        }
//    // for general textbox not allowing single quote and script tag
//    else if(operator == "text")
//        {
//        
//        }
//    // name without any space
//    else if(operator == "surname")
//        {
//        if(!((key >= 65 && key <= 90) || (key >= 65 && key <= 97) || key == 122 || key == 8 || key == 190 || key == 32))
//            {
//            return false;
//            }
//        }
    }

// Function to hadle the keydown handler for a given field so that
// if user hits enter having focus on any control then it should post form
function doClick(pControl, pSuppressEnter,e)
    {
         var key;
         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox
        if (key == 13)
        { 
            if(pSuppressEnter != "True")
                {SavePayments();return false;}
        }
    }   
function ShowValidator(controlID)
    {
    $("#vld" + controlID.name).css({visibility:"visible",display:"inline"});
    $("#"+controlID.name).focus();
    }
function HideValidator(controlID)
    {
    $("#vld" + controlID).css({visibility:"visible",display:"inline"});
    }

// Function to hide message when user has provided the input for a given textbox
function hideMessage(controlID)
    {
    if (!(l_Rent.value != ""  || l_Bond.value != "" || l_Fee.value != "" || l_Other.value != ""))
        $("#vldRent").css({visibility:"visible",display:"inline"}); 
    else
        $("#vldRent").css({visibility:"hidden",display:"none"});
        
    if(document.getElementById(controlID.name).value!="") {
        if(controlID.name == "Email")
            $("#vld" + controlID.name).css({visibility:"hidden",display:"none"});
        else if(controlID.name == "ConfirmEmail")
            $("#vld" + controlID.name).css({visibility:"hidden",display:"none"});
            
        if(controlID.name == "Email" && !IsValidEmail(controlID.value))
            $("#vld" + controlID.name + "In").css({visibility:"visible",display:"inline"});
        else if(controlID.name == "Email" && IsValidEmail(controlID.value))
            $("#vld" + controlID.name + "In").css({visibility:"hidden",display:"none"});
        else if(controlID.name == "ConfirmEmail" && controlID.value != l_Email.value)
            $("#vld" + controlID.name + "In").css({visibility:"visible",display:"inline"});
        else if(controlID.name == "ConfirmEmail" && controlID.value == l_Email.value)
            $("#vld" + controlID.name + "In").css({visibility:"hidden",display:"none"});
        else
            $("#vld" + controlID.name).css({visibility:"hidden",display:"none"});
        }
    else
        {
        if(controlID.name == "Email")
            $("#vld" + controlID.name + "In").css({visibility:"hidden",display:"none"});
        else if(controlID.name == "ConfirmEmail")
            $("#vld" + controlID.name + "In").css({visibility:"hidden",display:"none"});
        
        $("#vld" + controlID.name).css({visibility:"visible",display:"inline"});
        }
    }
    
// Function to validate user Input
function ValidateForm()
    {
    // bl =  Boolean Local variable    
    var bl_IsValide = "1";
    
    // check the mandatory fields
    if (l_BilCountry.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilCountry);
        }
    if (l_BilCounty.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilCounty);
        }
    if (l_BilTown.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilTown);
        }        
    if (l_BilAddr1.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilAddr1);
        }
    if (l_BilPostcode.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilPostcode);
        }
    if (l_BilSurname.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilSurname);
        }
    if (l_BilName.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_BilName);
        }
    if (!(l_Rent.value != ""  || l_Bond.value != "" || l_Fee.value != "" || l_Other.value != ""))
        {
        bl_IsValide = "0";
        ShowValidator(l_Rent);
        }        
    if (l_CardType.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_CardType);
        }
    if (l_ContactTelephone.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_ContactTelephone);
        }
    if (l_PropertyAddr.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_PropertyAddr);
        }    
    if (l_ConfirmEmail.value == "")
        {
        bl_IsValide = "0";
        HideValidator(l_ConfirmEmail+"In");
        ShowValidator(l_ConfirmEmail);
        }
    if (l_Email.value == "")
        {
        bl_IsValide = "0";
        HideValidator(l_Email+"In");
        ShowValidator(l_Email);
        }
    else if(!IsValidEmail(l_Email.value))
        {
        bl_IsValide = "0";
        HideValidator(l_Email);
        ShowValidator(l_Email);
        }    
    if (l_TenantName.value == "")
        {
        bl_IsValide = "0";
        ShowValidator(l_TenantName);
        }
    if (bl_IsValide == "0")
        return false;
    else
        return true;
    }
function SavePayments()
    {   
    try
        {
        // check whether form is valid or not
        if(!ValidateForm())
           return false;
            
        var itemID = {'Name' : l_TenantName.value,'Email':l_Email.value,'PropAddress':l_PropertyAddr.value,'Telephone':l_ContactTelephone.value,'CardType':l_CardType.value,'Rent':l_Rent.value,'Bond':l_Bond.value,'Other':l_Other.value,'Message':l_Message.value,'BilName':l_BilName.value,'BilPostcode':l_BilPostcode.value,'BilAddr1':l_BilAddr1.value,'BilAddr2':l_BilAddr2.value,'BilAddr3':l_BilAddr3.value,'BilTown':l_BilTown.value,'County':l_BilCounty.value,'Country':l_BilCountry.value,'Fee':l_Fee.value,'SurchargePer':l_ServiceCharge,'Surname':l_BilSurname.value};          
        var jsonStr = JSON.stringify(itemID); 
        $.ajax({  
             type: "POST",  
             contentType: "application/json; charset=utf-8",  
             dataType: "json",
             url: "default.aspx/Submit",  
             data: jsonStr,  
             success: function(msg){        
                var JObject = eval('(' + msg + ')');  
                var sResults = JObject.split('|');
                if(sResults[6]<=0)
                    {
                    alert('You can not submit Payment of zero amount.');
                    return;
                    }
                
                if(sResults[0] == "ok")
                    {  
                        document.getElementById("sTenantName").innerHTML = l_TenantName.value;
                        document.getElementById("sCardType").innerHTML = l_CardType.value;
                        document.getElementById("sRent").innerHTML = sResults[1];
                        document.getElementById("sBond").innerHTML = sResults[2];
                        document.getElementById("sFee").innerHTML = sResults[3];
                        document.getElementById("sOther").innerHTML = sResults[4];
                        document.getElementById("sSurcharge").innerHTML = sResults[5];
                        document.getElementById("sTotalPayments").innerHTML = sResults[6];
                        
                        // for Payment Form
                        
//                        document.getElementById("VPSProtocol").innerHTML = sResults[7];
//                        document.getElementById("Crypt").innerHTML = sResults[8];
//                        document.getElementById("Vendor").innerHTML = sResults[9];
//                        document.getElementById("TxType").innerHTML = sResults[10];
//                        
//                        alert(document.getElementById("VPSProtocol").innerHTML);
                        
                        document.getElementById("VPSProtocol").value = sResults[7];
                        document.getElementById("Crypt").value = sResults[8];
                        document.getElementById("Vendor").value = sResults[9];
                        document.getElementById("TxType").value = sResults[10];
                        document.SagePayForm.action = sResults[11];
                        
                       $("#divForm").hide();
                       $("#divPayForm").show();
                       return true;
                    }
                },
              error: function(xhr, msg)
                {
                alert(msg);
                return false; }  
        });           
       }
       catch(err)
        {
        alert(err.message);
        } 
    }