
var errorMsg;
var count;
var static_msg = "<b>Error:</b> please correct the following: <ol>";
var end_msg = "</ol>";
var companyName = "unassigned";
var userName = "unassigned";

function getCheckedValue(radio) {
    for (var i = 0; i < radio.length ; i++) {
        if (radio[i].checked)
            return radio[i].value; 
    }
    return "";
}

function isCheckboxChecked(name) {
    var count = 0;
    var option = $(name + '_' + count++);
    while (option != null) {
        if (option.checked)
            return true;
        option = $(name + '_' + count++);
    }
    return false;
}

function getCheckboxValue(name) {
    var count = 0;
    var option = $(name + '_' + count++);
    var returnValue = "";
    while (option != null) {
        if (option.checked) {
            if (returnValue != "") returnValue += ", ";
            returnValue += option.value;
        }
        option = $(name + '_' + count++);
    }
    return returnValue;
}

function isOtherCheckboxChecked(name) {
    var lastObj;
    var count = 0;
    lastObj = $(name + '_' + count++);
    while (lastObj != null) {
       if (lastObj.value == "Other") 
            if (lastObj.checked)
                return true;
       lastObj = $(name + '_' + count++);
    }
    return false;
}

function addErrorMessage( msg ) {
    if (errorMsg.length != 0) {
        errorMsg += "<li>" + msg + "</li>";
    }
    else errorMsg = static_msg + "<li>" + msg + "</li>";
}

function _checkBasicFields() {
    if ($("First_Name").value == "")
        addErrorMessage("First Name");
    if ($("Last_Name").value == "")
        addErrorMessage("Last Name");
    if ($("Email").value == "")
        addErrorMessage("Email is required");
    else {
        var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var OK = re.test($("Email").value);
        if (! OK)
            addErrorMessage("Invalid email address");
    }
    if ($("Job_Title").value == "")
        addErrorMessage("Job Title is required");
    if ($("Company").value == "")
        addErrorMessage("Company is required");
    if ($("Phone").value == "")
        addErrorMessage("Phone is required");
    if ($("Address").value == "")
        addErrorMessage("Address is required");
    if ($("City").value == "")
        addErrorMessage("City is required");
    if ($("State").value == "")
        addErrorMessage("State is required");
    if ($("Zip").value == "")
        addErrorMessage("Zip is required");
    if ($("Country").value == "")
        addErrorMessage("Country is required");
}

function _checkOrganizationData() {
    if ($("Organization_Size").selectedIndex <= 0)
         addErrorMessage("Answer Required: How many people are employed in your organization?");
    if ($("IT_Team_Size").selectedIndex <= 0)
         addErrorMessage("Answer Required: How large is your IT team?");
}

function _checkCRQuestions() {
    var isCRUser = getCheckedValue(document.form_registration.Current_CR_User);
    if (isCRUser == "") 
        addErrorMessage("Answer Required: Are you currently a customer of Business Objects Crystal Reports?");
    if (isCRUser == "Yes") {
        if ($("CR_Version").selectedIndex <= 0) 
            addErrorMessage("Answer Required: What version of Crystal Reports are you using?");
        else {
            if ($("CR_Version")[$("CR_Version").selectedIndex].value == "Other")
                if ($("CR_Version_Other").value == "")
                    addErrorMessage("Other version of Crystal Reports being used not specified");
        }
    }
    if (isOtherCheckboxChecked("Reporting_Tech_Used")) {
        if ($("Other_Reporting_Tech").value == "")
            addErrorMessage("Name of other reporting technology you use not specified");
    }
}

function _checkBOBJQuestions() {
    var isBOBJUser = getCheckedValue(document.form_registration.BOBJ_Customer);
    if (isBOBJUser == "") 
        addErrorMessage("Answer Required: Do you currently use other Business Objects products?");
    if (isBOBJUser == "Yes") {
        if (! isCheckboxChecked("BOBJ_Products_Used"))
            addErrorMessage("Answer Required: Which other Business Objects products do you use?");
        else {
            if (isOtherCheckboxChecked("BOBJ_Products_Used")) {
                if ($("Other_BOBJ_Product").value == "")
                    addErrorMessage("Name of other Business Objects product you use not specified");
            }
        }
    }
}

function _checkVirtualizationQuestions() {
    var usesVirtualization = getCheckedValue(document.form_registration.Deploy_Virtualization_Technology);
    if (usesVirtualization == "") 
        addErrorMessage("Answer Required: Do you currently deploy virtualization technology?");
    if (usesVirtualization == "Yes") {
        if (! isCheckboxChecked("Virtualization_Products_Deployed"))
            addErrorMessage("Answer Required: Which virtualization technologies do you deploy?");
        else {
            if (isOtherCheckboxChecked("Virtualization_Products_Deployed")) {
                if ($("Other_Virtualization_Product").value == "")
                    addErrorMessage("Name of other virtualization product you use not specified");
            }
        }
    }
    else if (usesVirtualization == "No" || usesVirtualization == "Don't Know") {
        var willDeploy = getCheckedValue(document.form_registration.Plan_To_Deploy_Virtualization);
        if (willDeploy == "") 
            addErrorMessage("Answer Required: Do you plan on deploying virtualization technology in the next 12 months?");
    }
}

function _checkSource() {
    if ($("Heard_About_CR_Test_Drive").selectedIndex <= 0)
         addErrorMessage("Answer Required: How did you hear about the CR Server Test Drive?");
    else {
        if ($("Heard_About_CR_Test_Drive")[$("Heard_About_CR_Test_Drive").selectedIndex].value == "Other Conference")
            if ($("Other_Conference").value == "")
                    addErrorMessage("Name of conference where you learned about the CR Server Test Drive not specified");
    }
}

function validate_registration() {
    errorMsg = "";
    count = 1;
    hideElement("registration_error");
    $("registration_error").innerHTML = "";
        
    _checkBasicFields();
//    _checkOrganizationData();
//    _checkCRQuestions();
//    _checkBOBJQuestions();
//    _checkVirtualizationQuestions();
//    _checkSource();
    
    if (errorMsg != "") {
        errorMsg += end_msg;
        showElement("registration_error");
        $("registration_error").innerHTML = errorMsg;
        $("form_top").focus();
        return false;
    }
    else {
        email_registration();
        return true;
   }
        
}

function showCRQuestions ( value ) {
    if (value) {
        showElement("div_CR_version");
    }
    else {
        hideElement("div_CR_version");
    }
}

function showBOBJQuestions ( value ) {
    if (value) {
        showElement("div_BOBJ_products_used");
    }
    else {
        hideElement("div_BOBJ_products_used");
    }
}

function showVirtualizationQuestions ( value ) {
    if (value) {
        showElement("div_virtualization_techs");
        hideElement("deploy_in_12_months");
    }
    else {
        hideElement("div_virtualization_techs");
        showElement("deploy_in_12_months");
    }
}

function showOtherElement ( widget, otherString, element ) {
    if (widget[widget.selectedIndex].value == otherString)
        element.style.display = "inline";
    else hideElement(element);
}



            
function email_registration() {
    // disconnect the send button while we are sending to prevent double clicks
    //disconnectAll("send_email");
    
    // grab the data out of the form fields
    var keys = [ "submit_id",
            "First_Name", 
            "Last_Name", 
            "Email", 
            "Job_Title", 
            "Company", 
            "Phone", 
            "Address", "City", "State", "Zip", "Country",
            "Org_Size",
            "IT_Team_Size",
            "Current_CR_User",
            "CR_Version",
            "CR_Version_Other",
            "Reporting_Tech_Used",
            "Other_Reporting_Tech",
            "BOBJ_Customer",
            "BOBJ_Products_Used",
            "Deploy_Virt_Tech",
            "Virt_Products_Deployed",
            "Other_Virt_Product",
            "Plan_To_Deploy_Virt",
            "Heard_About",
            "Other_Conference"]
            
    var values = [ "1",
            $("First_Name").value,
            $("Last_Name").value,
            $("Email").value,
            $("Job_Title").value,
            $("Company").value,
            $("Phone").value,
            $("Address").value, $("City").value, $("State").value, $("Zip").value, $("Country").value,
            $("Organization_Size")[$("Organization_Size").selectedIndex].value,
            $("IT_Team_Size")[$("IT_Team_Size").selectedIndex].value,
            getCheckedValue(document.form_registration.Current_CR_User),
            $("CR_Version")[$("CR_Version").selectedIndex].value,
            $("CR_Version_Other").value,
            getCheckboxValue("Reporting_Tech_Used"),
            $("Other_Reporting_Tech").value,
            getCheckedValue(document.form_registration.BOBJ_Customer),
            getCheckboxValue("BOBJ_Products_Used"),
            getCheckedValue(document.form_registration.Deploy_Virtualization_Technology),
            getCheckboxValue("Virtualization_Products_Deployed"),
            $("Other_Virtualization_Product").value,
            getCheckedValue(document.form_registration.Plan_To_Deploy_Virtualization),
            $("Heard_About_CR_Test_Drive")[$("Heard_About_CR_Test_Drive").selectedIndex].value,
            $("Other_Conference").value]

    

    // send a post to the PHP script that sends e-mail
    goToNextStep();
    var req = getXMLHttpRequest();
    req.open("POST", "send-email.php", false);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.send(queryString(keys,values));

    companyName = $("Company").value;
    userName = $("FIrst_Name").value + " " + $("Last_Name").value;

    //alert("Request response: " + req.responseText);
    // reconnect the send e-mail button (in case they want to send another
    //connect("send_email", "onclick", this, "validate_registration");
}
