﻿function selectedChange(radioID)
{   //Show the approiate textbox's, hide the others 
    if(radioID == "Homebuyer")
    {   
        $get("divHomebuyer").style.height = "auto";
        $get("divHomebuyer").style.visibility = "visible";
        $get("divBuilderMember").style.height = "0px";
        $get("divBuilderMember").style.visibility = "hidden";       
    }
    else
    {
        $get("divHomebuyer").style.height = "0px";
        $get("divHomebuyer").style.visibility = "hidden";
        $get("divBuilderMember").style.height = "auto";
        $get("divBuilderMember").style.visibility = "visible";       
    }
}

function setValidators(type)
{
        //True enables the homebuyer validators, false for the builder member ones
        //Disables the opposite validators        
        ValidatorEnable($get("rfvHBName"), type);
        ValidatorEnable($get("rfvHBAddress"), type);
        ValidatorEnable($get("rfvHBPostalCode"), type);
        ValidatorEnable($get("rfvHBCity"), type);
        ValidatorEnable($get("rfvHBPhone"), type);
        ValidatorEnable($get("rfvHBEmailAddr"), type);
        
        ValidatorEnable($get("rfvBMCompanyName"), !type);
        ValidatorEnable($get("rfvBMRNumber"), !type);
        ValidatorEnable($get("rfvBMContact"), !type);
        ValidatorEnable($get("rfvBMAddress"), !type);
        ValidatorEnable($get("rfvBMPostalCode"), !type);
        ValidatorEnable($get("rfvBMCity"), !type);
        ValidatorEnable($get("rfvBMPhone"), !type);
        ValidatorEnable($get("rfvBMEmailAddr"), !type);
        ValidatorEnable($get("rfvBMGuidebookQuantity"), !type);
        ValidatorEnable($get("rfvBMPostcardQuantity"), !type);  
}

var alreadyRan = false; //Make sure button can only be pressed once
function submitHB()
{
    setValidators(true); 
    var validated = Page_ClientValidate();
    if(validated && alreadyRan == false) //All required fields are filled in and button hasnt been pushed
    {
        var Name = $get("txtHBName").value;
        var Address = $get("txtHBAddress").value;
        var PostalCode = $get("txtHBPostalCode").value;
        var City = $get("txtHBCity").value;
        var Phone = $get("txtHBPhone").value;
        var EmailAddress = $get("txtHBEmailAddr").value;
        var PossessionDate = $get("txtHBPossessionDate").value;
        alreadyRan = true;
        //Run web service to send email to marketing
        PurchaseToPossessionOrder.homebuyerOrder(PossessionDate, Name, Address, PostalCode, City, Phone, EmailAddress, Results, ErrorRetrieving);
    }

}

function submitBM()
{
    setValidators(false);
    var validated = Page_ClientValidate();
    if(validated && alreadyRan == false) //All required fields are filled in and button hasnt been pushed
    {
        var Company = $get("txtBMCompanyName").value;
        var RNumber = $get("txtBMRNumber").value;
        var Contact = $get("txtBMContact").value;
        var Address = $get("txtBMAddress").value;
        var PostalCode = $get("txtBMPostalCode").value;
        var City = $get("txtBMCity").value;
        var Phone = $get("txtBMPhone").value;
        var EmailAddress = $get("txtBMEmailAddr").value;
        var GuideQuantity = $get("txtBMGuidebookQuantity").value;
        var PostCardQuantity = $get("txtBMPostcardQuantity").value;
        alreadyRan = true;    
        //Run web service to send email to marketing
        PurchaseToPossessionOrder.builderOrder(Company, RNumber, Contact, Address, PostalCode, City, Phone, EmailAddress, GuideQuantity, PostCardQuantity, Results, ErrorRetrieving);
    }
}


function Results(result, userContext, methodName)
{
    alert(result); //Display success or fail message
    window.close(); //Close window
}
function ErrorRetrieving(exception)
{
    alert("Order has not been completed. An internal error has occurred."); //Webservice failed

}

