﻿/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
** Code licensed under Creative Commons Attribution-ShareAlike License      **
** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn() {
    var xmlhttp, bComplete = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) {
            try { xmlhttp = new XMLHttpRequest(); }
            catch (e) { xmlhttp = false; }
        }
    }
    if (!xmlhttp) return null;
    this.connect = function(sURL, sMethod, sVars, fnDone) {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
        try {
            if (sMethod == "GET") {
                xmlhttp.open(sMethod, sURL + "?" + sVars, true);
                sVars = "";
            }
            else {
                xmlhttp.open(sMethod, sURL, true);
                xmlhttp.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1");
                xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && !bComplete) {
                    bComplete = true;
                    fnDone(xmlhttp);
                }
            };
            xmlhttp.send(sVars);
        }
        catch (z) { return false; }
        return true;
    };
    return this;
}

function Get_PetSelector_Output() {
    //initialize XHConn (if XHConn isn't created successfully, 
    //the client doesnt' support Ajax)
    var ajaxConn = new XHConn();

    //post to mypage.php with args foo and baz
    ajaxConn.connect("http://www.petwave.com/widgets/pet-selector.aspx", "POST",
  "foo=bar", fnWhenDone);
}

//when the server responds, javascript 
//will trigger this callback function
function fnWhenDone(XML) {
    document.getElementById('pw_petselector').innerHTML = XML.responseText;
    //alert(XML.responseText);
    //document.getElementById("pw_petselector").innerHTML = XML.responseText;
    //document.write(XML.responseText);
}

function postdata() {
    //var fieldValue = document.getElementById("field1").value;
    var valSize = "";
    valSize = setFormValue(valSize, document.getElementById("size_small"));
    valSize = setFormValue(valSize, document.getElementById("size_medium"));
    valSize = setFormValue(valSize, document.getElementById("size_large"));

    var valGrooming = "";
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_high"));
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_medium"));
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_low"));

    var valChildren = "";
    valChildren = setFormValue(valChildren, document.getElementById("children_yes"));
    valChildren = setFormValue(valChildren, document.getElementById("children_with_proper_training"));
    valChildren = setFormValue(valChildren, document.getElementById("children_not_advised"));

    var valTrain = "";
    valTrain = setFormValue(valTrain, document.getElementById("train_quick"));
    valTrain = setFormValue(valTrain, document.getElementById("train_fairly_easy"));
    valTrain = setFormValue(valTrain, document.getElementById("train_slow_learner"));

    var valPersonality = "";
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_friendly"));
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_protective"));
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_aloof"));
    
    postwith("http://www.petwave.com/Dog-Breed-Selector.aspx?external=1", { ps_size: valSize, ps_grooming: valGrooming, ps_children: valChildren, ps_trainability: valTrain, ps_personality: valPersonality });
}

function postdataPetSugar() {
    //var fieldValue = document.getElementById("field1").value;
    var valSize = "";
    valSize = setFormValue(valSize, document.getElementById("size_small"));
    valSize = setFormValue(valSize, document.getElementById("size_medium"));
    valSize = setFormValue(valSize, document.getElementById("size_large"));

    var valGrooming = "";
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_high"));
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_medium"));
    valGrooming = setFormValue(valGrooming, document.getElementById("grooming_low"));

    var valChildren = "";
    valChildren = setFormValue(valChildren, document.getElementById("children_yes"));
    valChildren = setFormValue(valChildren, document.getElementById("children_with_proper_training"));
    valChildren = setFormValue(valChildren, document.getElementById("children_not_advised"));

    var valTrain = "";
    valTrain = setFormValue(valTrain, document.getElementById("train_quick"));
    valTrain = setFormValue(valTrain, document.getElementById("train_fairly_easy"));
    valTrain = setFormValue(valTrain, document.getElementById("train_slow_learner"));

    var valPersonality = "";
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_friendly"));
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_protective"));
    valPersonality = setFormValue(valPersonality, document.getElementById("personality_aloof"));

    postwith("http://petsugar.petwave.com/Dog-Breed-Selector.aspx?external=1", { ps_size: valSize, ps_grooming: valGrooming, ps_children: valChildren, ps_trainability: valTrain, ps_personality: valPersonality });
}

function setFormValue(output, field) {
    if (field.checked) {
        if (output.length > 0) {
            output = output + ","
        }
        output = output + field.value;
    }
    return output;
}


function postwith(to, p) {
    var myForm = document.createElement("form");
    myForm.method = "post";
    myForm.action = to;
    for (var k in p) {
        var myInput = document.createElement("input");
        myInput.setAttribute("name", k);
        myInput.setAttribute("value", p[k]);
        myForm.appendChild(myInput);
    }
    document.body.appendChild(myForm);
    myForm.submit();
    document.body.removeChild(myForm);
}

function pageToggle(show, hide1, hide2) {
    var eleShow = document.getElementById(show);
    var eleHide1 = document.getElementById(hide1);
    var eleHide2 = document.getElementById(hide2);

    eleShow.style.display = "block";
    eleHide1.style.display = "none";
    eleHide2.style.display = "none";

}


