﻿var ajax = new sack();

// create a DOMParser if doesn't exist
if (typeof DOMParser == "undefined") {
    DOMParser = function() {
    };

    DOMParser.prototype.parseFromString = function(str, contentType) {
        if (typeof ActiveXObject != "undefined") {
            var d = new ActiveXObject("MSXML.DomDocument");
            d.loadXML(str);
            return d;
        }
    }
}

function ComputeGender() 
{
    try {
        document.getElementById("loadingimage").style.display = '';
        
        var motherDate = document.getElementById("MotherBirthDate");
        var fatherdate = document.getElementById("FatherBirthDate");

        ajax.setVar("MotherBirthDate", motherDate.value);
        ajax.setVar("FatherBirthDate", fatherdate.value);

        ajax.method = "POST";
        ajax.onCompletion = OperationCompleted;
        ajax.requestFile = "GenderService.asmx/GetGenderChangeList";

        ajax.runAJAX();
    }
    catch (err) {
        alert(err);
    }
}

function ProcessXML(xmlstring) 
{
    var returnValue = new Array();

    try {
        // convert the string to an XML object
        var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");

        // get the XML root item
        var root = xmlobject.getElementsByTagName('ArrayOfGenderChange')[0];

        var items = root.getElementsByTagName("GenderChange");
    }
    catch (ex) {
        alert("The data received was corrupt, please try again");
    }
    return items;
}

function OperationCompleted() 
{
    document.getElementById("loadingimage").style.display = 'none';

    if (ajax.responseStatus && (ajax.responseStatus[0] == 200)) 
    {
        var xndGenderChange = ProcessXML(ajax.response);

        if (xndGenderChange) {
            if (xndGenderChange.length > 0) {
                ShowErrorText(false);
                ShowHelpText(false);

                var tableResults = '<table id=\"resultsGrid\" class=\"helptext\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\">' +
                                   '<tbody><tr class=\"tableHeader\" align=\"left\">' +
		                           '<th scope=\"col\">Between</th><th scope=\"col\">And</th><th scope=\"col\">Baby Sex</th></tr>';

                for (var i = 0; i < xndGenderChange.length; i++) {

                    var change = new GenderChanger(xndGenderChange[i]);
                    var rowFormat = (i % 2) ? 'tableRowEven' : 'tableRowOdd';
                    tableResults += '<tr class=\"' + rowFormat + '\"><td>' + change.From + '</td><td>' + change.To + '</td><td>' + change.Gender + '</td></tr>';

                }
                tableResults += '</tbody></table>';

                document.getElementById("tableResults").innerHTML = tableResults;
            }
            else {
                ShowErrorText(true);
                ShowHelpText(true);
            }
        }
    }
    else 
    {
        alert("There was a fatal error processing your request, please try again later.");
    }
}

function GenderChanger(xndGenderChange)
{
    this.From = xndGenderChange.getElementsByTagName('fromDate')[0].firstChild.nodeValue;
    this.To = xndGenderChange.getElementsByTagName('toDate')[0].firstChild.nodeValue;
    this.Gender = xndGenderChange.getElementsByTagName('babyGender')[0].firstChild.nodeValue;
}

function ShowHelpText(show)
{
    if (show)
    {
        document.getElementById("helpLabel").innerHTML = 'Enter the date of birth of both parents and press the button to compute the range of dates when you should get pregnant to obtain a desired gender for your baby.<br>';
        document.getElementById("tableResults").innerHTML = '';
    }
    else
    {
        document.getElementById("helpLabel").innerHTML = '';
    }
}

function ShowErrorText(show)
{
    if (show)
    {
        document.getElementById("errorLabel").innerHTML = 'The birthdate of one of the parents is incorrect. Please specify a correct month/day/year date.<br><br>';
        document.getElementById("tableResults").innerHTML = '';
    }
    else
    {
        document.getElementById("errorLabel").innerHTML = '';
    }
}

function setTimerToHide()
{
	setTimeout("hideAddressBar()", 10);
}

function hideAddressBar()
{
	window.scrollTo(0, 1);
}

function clearControlValue(control)
{
    control.value = "";
}

function validateForm()
{
    if (ValidateDate(document.mainform.MotherBirthDate))
      if (ValidateDate(document.mainform.FatherBirthDate))
        return true;
    return false;
}

// google stuff
_uacct = "UA-427222-6";
urchinTracker();


