$j(document).ready(function() {

    do_sIFR_headers();

    /* Setup for the Get Started Popup */
    $j("a#inline").fancybox({
	    'hideOnContentClick': false,
	    'showCloseButton' :true,
        'onClosed'      : function() { fancyBoxClosed(); },
        'overlayOpacity' : 0.4
    });

    /* Setup for the Call Me Back (CMB) Popup */
    $j("a#CMBHeader").fancybox({
	    'hideOnContentClick': false,
	    'showCloseButton' :true,
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'      : function() { CMBfancyBoxClosed(); },
        'overlayOpacity' : 0.4
    });

    /* Setup for the Call Me Back (CMB) Popup */
    $j("a#CMBFooter").fancybox({
	    'hideOnContentClick': false,
	    'showCloseButton' :true,
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'      : function() { CMBfancyBoxClosed(); },
        'overlayOpacity' : 0.4
    });
    	
    /* Ajax function for first form button click */
    $j('.GoButton1').click(function()
    {
        
        //get value
        firstname = $j('#ctl00_MiddleBottom_xContent_ctl00_txtFirstName').val();
        surname = $j('#ctl00_MiddleBottom_xContent_ctl00_txtSurname').val();
        phone = $j('#ctl00_MiddleBottom_xContent_ctl00_txtPhone').val();
        email = $j('#ctl00_MiddleBottom_xContent_ctl00_txtEmail').val();
        
        isValid = true;
        if($j('.validation').css('display') == 'block')
        {
            
            isValid = false;
        }
        
        querystring = "&task=form1&firstname=" + firstname + "&surname=" + surname + "&phone=" + phone + "&email="+email + "&isValid=" + isValid;
        
        $j.ajax({
           type: "GET",
           cache: false,
            
           url: "/AjaxControlHandler.aspx",
           data: querystring,
           success: function(msg){
            if(msg == 'Success')
            {
                $j("a#inline").click();
            }
            
           }
         });
    });


    /* Ajax function for second form button click */
    $j('.GoButton2').click(function()
    {
        
        //get values
        borrowAmount = $j('#ctl00_PopUpMaster_txtBorrowAmount').val();
        propertyValue = $j('#ctl00_PopUpMaster_txtPropertyValue').val();
        grossIncome = $j('#ctl00_PopUpMaster_txtGrossIncome').val();
        when = $j('#ctl00_PopUpMaster_ddWhen').val();
        
        isValid = true;
        if($j('.validation2').css('display') == 'block')
        {
            
            isValid = false;
        }
        
        querystring = "&task=form2&borrowAmount=" + borrowAmount + "&propertyValue=" + propertyValue + "&grossIncome=" + grossIncome + "&when="+when + "&isValid=" + isValid;
        
        $j.fancybox.resize();
        
        $j.ajax({
            type: "GET",
            cache: false,
            
            url: "/AjaxControlHandler.aspx",
            data: querystring,
            success: function(msg){
                if(msg == 'Success')
                {
                    //Clear out the parent page data
                    firstNameObj = document.getElementById("ctl00_MiddleBottom_xContent_ctl00_txtFirstName");
                    surnameObj = document.getElementById("ctl00_MiddleBottom_xContent_ctl00_txtSurname");
                    phoneObj = document.getElementById("ctl00_MiddleBottom_xContent_ctl00_txtPhone");
                    emailObj = document.getElementById("ctl00_MiddleBottom_xContent_ctl00_txtEmail");
                    firstNameObj.value = "";
                    surnameObj.value = "";
                    phoneObj.value = "";
                    emailObj.value = "";

                    ThankYouObj = document.getElementById("ThankYouMessage");
                    BorrowObj = document.getElementById("ctl00_PopUpMaster_txtBorrowAmount");
                    PropertyObj = document.getElementById("ctl00_PopUpMaster_txtPropertyValue");
                    IncomeObj = document.getElementById("ctl00_PopUpMaster_txtGrossIncome");
                    WhenObj = document.getElementById("ctl00_PopUpMaster_ddWhen");
                    ButtonObj = document.getElementById("ctl00_PopUpMaster_btnGo2");
                    
                    BorrowObj.disabled = true;
                    PropertyObj.disabled = true;
                    IncomeObj.disabled = true;
                    WhenObj.disabled = true;
                    ButtonObj.disabled = true;
                    //display the thank you message.
                    ThankYouObj.style.display = "";

                    //Resize if required
                    $j.fancybox.resize();
                }
            }
        });
    });
    
    /* Code required on the Call Me Back Button (button 3) */
    $j('.GoButton3').click(function()
    {   
        //get values
        CMB_Name = $j('#ctl00_PopUpMaster_txtCMBName').val();
        CMB_Phone = $j('#ctl00_PopUpMaster_txtCMBPhone').val();
        CMB_State = $j('#ctl00_PopUpMaster_ddlCMBState').val();
        CMB_When = $j('#ctl00_PopUpMaster_ddCMBWhen').val();
        
        isValid = true;
        if($j('.validation3').css('display') == 'block')
        {
            isValid = false;
            //Resize if required
            $j.fancybox.resize();
        }        
        
        querystring = "&task=form3&CMB_Name=" + CMB_Name + "&CMB_Phone=" + CMB_Phone + "&CMB_State=" + CMB_State + "&CMB_When=" + CMB_When + "&isValid=" + isValid;

        $j.ajax({
            type: "GET",
            cache: false,
            
            url: "/AjaxControlHandler.aspx",
            data: querystring,
            success: function(msg) {
                if(msg == 'Success')
                {
                    ThankYouObj = document.getElementById("CMBThankYouMessage");
                    NameObj = document.getElementById("ctl00_PopUpMaster_txtCMBName");
                    PhoneObj = document.getElementById("ctl00_PopUpMaster_txtCMBPhone");
                    StateObj = document.getElementById("ctl00_PopUpMaster_ddlCMBState");
                    WhenObj = document.getElementById("ctl00_PopUpMaster_ddCMBWhen");
                    ButtonObj = document.getElementById("ctl00_PopUpMaster_btnGo3");
                    
                    NameObj.disabled = true;
                    PhoneObj.disabled = true;
                    StateObj.disabled = true;
                    WhenObj.disabled = true;
                    ButtonObj.disabled = true;
                    //display the thank you message.
                    ThankYouObj.style.display = "";

                    //Resize if required
                    $j.fancybox.resize();
                }
            }
        });
    });
});