﻿function Trim(str) {
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


function fillCity(varCityId, varStateID, varPagename) {

    var responseValue = "";

    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    function StateChange() {

        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {

            responseValue = xmlHttp.responseText;

            document.getElementById(varCityId).length = 0;
            var cityList = document.getElementById(varCityId);
            //Logic to bind data

            var cityNameValue = xmlHttp.responseText.split(",");

            //Add new states list to the state combo box.
            for (count = 0; count < cityNameValue.length; count++) {
                var strNameValueblock = cityNameValue[count].split("#");
                var optiontext = strNameValueblock[0];
                var optionvalue = strNameValueblock[1];
                var optionItem;

                optionItem = new Option(optiontext, optionvalue, false, false);
                cityList.options[cityList.length] = optionItem;
            }
        }

    }

    var url = "../AjaxHelper/AjaxParams.aspx?pagename=" + varPagename + "&function=ChangeCity" + "&stateID=" + varStateID + "&numb=" + Math.random();
    xmlHttp.onreadystatechange = StateChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}



function SavePoll(QuestionId, AnsId) {

    var AnswerId = getRadioValue(AnsId);
    var responseValue = ""

    //Declare variable
    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }


    function PollChange() {

        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {
            //show div control

            ResponseValue = xmlHttp.responseText
            if (ResponseValue == "0") {
                alert("Your session has expired. Please re-login");
            }
            else {
                document.getElementById(strvalue).src = xmlHttp.responseText;
            }
        }
    }
    var url = ""
    url = "../AjaxHelper/AjaxParams.aspx.aspx?function=polls" + "&QuestionID=" + QuestionId + "&AnswerID=" + AnswerId + "&numb=" + Math.random();

    xmlHttp.onreadystatechange = PollChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}


function getRadioValue(AnswerId) {
    var list = document.getElementById(AnswerId); //Client ID of the radiolist
    var inputs = list.getElementsByTagName("input");
    var selected;
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].checked) {
            selected = inputs[i].value;
            break;
        }
    }
    return selected;
}




function LoginFacebook(FInfo) {

    var responseValue = ""

    //Declare variable
    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    function FChange() {
        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {
           return xmlHttp.responseText;
        }
    }
    var url = ""
    url = "AjaxParams.aspx?function=flogin&" + FInfo + "&numb=" + Math.random();
    xmlHttp.onreadystatechange = FChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}




function LogoutFacebook() {

    var responseValue = ""

    //Declare variable
    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    function FChange() {
        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {

        }
    }
    var url = ""
    url = "AjaxParams.aspx?function=flogout&numb=" + Math.random();
    xmlHttp.onreadystatechange = FChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}



function CheckAvailability(username) {

    if (Trim(username) == '')
    {return false;}

    //Declare variable
    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    function StateChange() {
        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {
            document.getElementById('imgavailable').src = "images/no.jpg";
            if (xmlHttp.responseText == "1") {
                document.getElementById('imgavailable').src = "images/yes.jpg";
            } 
        }
    }
    var url = ""
    url = "AjaxParams.aspx?function=checkavailability&username=" + username + "&numb=" + Math.random();
    xmlHttp.onreadystatechange = StateChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}

function Order_Star(purchaseId, Img) {
    var starStatus=0;
    var IndexOfUnstar = Img.src.indexOf("unstar"); //Img.substring(Img.lastIndexOf("/") + 1, Img.lastIndexOf("."));

    if (IndexOfUnstar != -1) {
        starStatus = 1;
    }

    //Declare variable
    var xmlHttp = null;
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    function StateChange() {
        if (xmlHttp.readyState == 4 && xmlHttp.status != "404") {
            Img.src = 'images/unstar.png';
            if (starStatus == 1) {
                Img.src = 'images/star.png'; 
            }
        }
    }
    var url = ""
    url = "AjaxParams.aspx?function=order_star&pid=" + purchaseId + "&starstatus=" + starStatus + "&numb=" + Math.random();
    xmlHttp.onreadystatechange = StateChange;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return false;
}
