var XML_START_TAG = "<input>";
var XML_END_TAG = "</input>";

function OpenProfilePage(memNum, encMemNum, socNetDomain)
{
    var xmlInput = "<input><sId></sId><memNum>" + memNum + "</memNum></input>";
    var remoteInput = "<remoteInput>xmlInput=" + xmlInput + "</remoteInput>";
    
    var xmlData = "<remoteUrl>" + socNetDomain + "/Services/UserServicePage.aspx" + "</remoteUrl><postData><remoteMethod>method=GetProfile</remoteMethod>" + remoteInput + "</postData>";
    
    var xmlInput = PrepareXmlInput(xmlData);

    $.ajax({
        type: "POST",
        url: "/httpservices/proxyservice.aspx",
        data: "method=RemotePost&xmlInput=" + xmlInput,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function(xml) {
            OpenProfileResultHandler(xml, memNum, encMemNum, socNetDomain);
        } //end of success handler
    });
}
function PrepareXmlInput(xmlData) {
    return XML_START_TAG + xmlData + XML_END_TAG;
}
function getProfileImage(memNum, encMemNum, socNetSvcDomain, imgDefaultURL, imgProfileID, hlnkImgProfileID, socNetDomain) {
    var xmlInput = "<input><sId></sId><memNum>" + memNum + "</memNum></input>";
    var remoteInput = "<remoteInput>xmlInput=" + xmlInput + "</remoteInput>";

    var xmlData = "<remoteUrl>" + socNetSvcDomain + "/Services/UserServicePage.aspx" + "</remoteUrl><postData><remoteMethod>method=GetProfile</remoteMethod>" + remoteInput + "</postData>";
    var imageURL;
    var xmlInput = PrepareXmlInput(xmlData);

    $.ajax({
        type: "POST",
        url: "/httpservices/proxyservice.aspx",
        data: "method=RemotePost&xmlInput=" + xmlInput,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function (xml) {
            imageURL = getProfileImageURL(xml, socNetDomain, imgDefaultURL, imgProfileID, hlnkImgProfileID);
        } //end of success handler
    });
    return imageURL;
}

function OpenProfileResultHandler(xml, memNum, encMemNum, socNetDomain)
{
    var sid;
    
     $(xml).find('output').each(function(i){
        sid = $(this).find("sid").text();    
     });
          
     if (sid > 0)
     {
        //SocNet Profile exists
        //window.location = socNetDomain + "/Profile/UserProfile.aspx?sid=" + sid;
        window.location = "/vendor/dispatcher.aspx?login=False&application=SocNet&section=PersonalPage?sid=" + sid;
     }
     else if(sid == "0")
     {
        //Socnet profile doesn't exists
        var url = "profile.aspx?member_no=" + encMemNum;
        windowOpen(url,610,469,0,1);
     }
     else
     {
        //Info not available
        //do nothing
     }
 }

 function getProfileImageURL(xml, socNetDomain, imgDefaultURL, imgProfileID, hlnkImgProfileID) {
     var sid;
     var imageURL;

     $(xml).find('output').each(function (i) {
         sid = $(this).find("sid").text();
         imageURL = $(this).find("image").text();
     });

     if (sid > 0 && imageURL != "") {
         //SocNet Profile exists
         //window.location = socNetDomain + "/Profile/UserProfile.aspx?sid=" + sid;
         document.getElementById(imgProfileID).src = imageURL;
         document.getElementById(hlnkImgProfileID).href = socNetDomain + "/Profile/UserProfile.aspx";
         return imageURL;
     }
     else {
         document.getElementById(imgProfileID).src = imgDefaultURL;
         document.getElementById(hlnkImgProfileID).href = socNetDomain + "/Profile/EditProfile.aspx";
         return "";
     }
 }


