var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject() { 
	var xmlHttp; 

    if(window.ActiveXObject) { 
        try { 
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (e) { 
            xmlHttp = false; 
        } 
    }else { 
        try { 
            xmlHttp = new XMLHttpRequest(); 
        } catch (e) { 
            xmlHttp = false; 
        } 
    } 

    if (!xmlHttp) 
        alert("Error creating the XMLHttpRequest object."); 
    else 
        return xmlHttp; 
} 

function submitForm(attachVariables) {
	var submitform;
	submitform="submitadopt.php?"+attachVariables;
	
	//alert("Submit Form : "+submitform);
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { 
		xmlHttp.open("GET", submitform, true); 
		
        xmlHttp.onreadystatechange = handleSubmitFormResponse; 

        xmlHttp.send(null); 
    } else {
		alert("Request Reject");
        setTimeout('submitForm()', 1000); 
    }
}

function getRandomChild(minAge, maxAge, gender) {
	var form;
	form ="angel.php?action=random&min=" + minAge 
        + "&max=" + maxAge
        + "&g=" + gender
		+ "&sid=" + Math.random();


	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { 
		xmlHttp.open("GET", form, true); 
		
        xmlHttp.onreadystatechange = handleRandomChildResponse; 

        xmlHttp.send(null); 
    } else {
		//alert("Request Reject");
        setTimeout(getRandomChild(minAge, maxAge, gender), 1000); 
    }
}

function handleRandomChildResponse() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") { 
        if (xmlHttp.status == 200) {
            xmlResponse = xmlHttp.responseXML;
			
			document.forms[0].elements["childid"].value = 
				xmlResponse.getElementsByTagName("childid")[0].childNodes[0].nodeValue;;

			document.getElementById("name").innerHTML =
				xmlResponse.getElementsByTagName("name")[0].childNodes[0].nodeValue;

			document.getElementById("age").innerHTML =
				"Age: " + xmlResponse.getElementsByTagName("age")[0].childNodes[0].nodeValue + " years";

			document.getElementById("toy").innerHTML =
				"Toy: " + xmlResponse.getElementsByTagName("toy")[0].childNodes[0].nodeValue;
                
            var clothing = "Clothes: " + xmlResponse.getElementsByTagName("clothing")[0].childNodes[0].nodeValue +
                "-" + xmlResponse.getElementsByTagName("size")[0].childNodes[0].nodeValue;

			document.getElementById("clothes").innerHTML = clothing;
        } else { 
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
    }
}


function adoptAngel(childId, firstName, lastName, address, cityState, zip, 
	phoneNo, cellNo, email, altEmail, birthDay, church) {
	
    var url = "angel.php";

    var form ="action=adopt&childid=" + childId
		+ "&fname=" + escape(firstName)
		+ "&lname=" + escape(lastName)
		+ "&address=" + escape(address)
		+ "&citystate=" + escape(cityState)
		+ "&zip=" + escape(zip)
		+ "&phone=" + escape(phoneNo)
		+ "&cell=" + escape(cellNo)
		+ "&email=" + escape(email)
		+ "&altemail=" + escape(altEmail)
		+ "&birthday=" + escape(birthDay)
		+ "&church=" + escape(church);
        
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { 
		xmlHttp.open("POST", url, true); 

        xmlHttp.onreadystatechange = handleAdoptAngelResponse; 

        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", form.length);
        xmlHttp.setRequestHeader("Connection", "close");

        xmlHttp.send(form); 
    } else {
		alert("Request Reject");
        //setTimeout('adoptAngel()', 1000); 
    }
}


function handleAdoptAngelResponse() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") { 
        if (xmlHttp.status == 200) {
            xmlResponse = xmlHttp.responseXML;

        } else { 
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
    }
}

// executed automatically when a message is received from the server 
function handleSingleServerResponse() { 
	if (xmlHttp.readyState == 4) { 

        if (xmlHttp.status == 200) { 
            xmlResponse = xmlHttp.responseXML; 

            xmlDocumentElement = xmlResponse.documentElement; 

            message = xmlDocumentElement.firstChild.data; 

            document.getElementById("st2text").innerHTML = 
            '<i>' + message + '</i>'; 
            
        } else { 
            alert("There was a problem accessing the server: " + xmlHttp.statusText); 
        } 
    } 
}

