var XmlHttp;
var Flashstate

function CreateXmlHttp() 
{
    document.getElementById("addressdata").innerHTML = "";
    //document.getElementById("lblmap").style.visibility = "hidden";
    //document.getElementById('mapimg1').href = "javascript://;";
    try 
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) 
    {
        try 
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) 
        {
            XmlHttp = null;
        }
    }
    if (!XmlHttp && typeof XMLHttpRequest != "undefined") 
    {
        document.body.style.cursor = 'wait';
        XmlHttp = new XMLHttpRequest();
    }
}

function LocationOnChange() 
{
//    document.getElementById("lblmap").style.visibility = "hidden";
//    document.getElementById("mapimg1").disabled = true;
//    document.getElementById("mapimg1").removeAttribute('href');
    
    var LocationList = document.getElementById("ddllocation");
    var selectedLocation = LocationList.options[LocationList.selectedIndex].value;
    var BusinessList = document.getElementById("ddlbusiness");
    var CountryList = document.getElementById("ddlcountry");
    for (var count = BusinessList.options.length - 1; count > 0; count--) 
    {
        BusinessList.options[count] = null;
    }
    for (var count = CountryList.options.length - 1; count > 0; count--) 
    {
        CountryList.options[count] = null;
    }
    if (LocationList.options[LocationList.selectedIndex].value == "2" || LocationList.options[LocationList.selectedIndex].value == "5" || LocationList.options[LocationList.selectedIndex].value == "1" || LocationList.options[LocationList.selectedIndex].value == "4") 
    {
        document.getElementById("ddlbusiness").disabled = true;
        var optionItem = new Option("India", "India", false, false);
        CountryList.options[1] = optionItem;
        document.getElementById("ddlcountry").selectedIndex = 1;
        document.getElementById("addressdata").innerHTML = "";
       
    }
    else 
    {
        document.getElementById("ddlbusiness").disabled = false;
        var requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(selectedLocation);
        CreateXmlHttp();
        if (XmlHttp) 
        {
            XmlHttp.onreadystatechange = HandleResponse;
            XmlHttp.open("GET", requestUrl, true);
            document.body.style.cursor = 'auto';
            XmlHttp.send(null);
            
        }
    }

    if (Flashstate == "UnZoom") 
    {
        flashBack();
        document.getElementById("addressdata").innerHTML = "";

    }
//    document.getElementById('lblmap').style.visibility = "hidden";
//    document.getElementById('mapimg1').style.visibility = "hidden";
//    //document.getElementById('mapimg1').href = "javascript: return false;";
//    
//    document.getElementById('mapimg1').style.cursor = "default";
//    document.getElementById('mapimg1').setAttribute('onmousedown', 'javascript://');
//    document.getElementById('mapimg1').disabled = true;
//    document.getElementById('mapimg1').style.display = "none";
    
}

//Called when response comes back from server
function HandleResponse() 
{
    if (XmlHttp.readyState == 4) 
    {
        if (XmlHttp.status == 200) 
        {
            var LocationStr = XmlHttp.responseText;
            ClearAndSetBusinessListItems(XmlHttp.responseText);
        }
        else 
        {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

function ClearAndSetBusinessListItems(LocationStr) 
{
    var dropdownbusiness = document.getElementById("ddlbusiness");
    var dropdowncountry = document.getElementById("ddlcountry");
    var listBusiness = LocationStr;
    var listBusinessArray = listBusiness.split('|');

    for (var count = dropdownbusiness.options.length - 1; count > -1; count--) 
    {
        dropdownbusiness.options[count] = null;
    }

    var textValue;
    var optionItem;

    for (var count = 0; count < listBusinessArray.length - 1; count++) 
    {
        textValue = listBusinessArray[count];
        optionItem = new Option(textValue, textValue, false, false);
        dropdownbusiness.options[dropdownbusiness.length] = optionItem;
    }
}


function BusinessOnChange() 
{
    var dropdownbusiness = document.getElementById("ddlbusiness");
    var dropdowncountry = document.getElementById("ddlcountry");
    var dropdownlocation = document.getElementById("ddllocation");
    var selectedlocation = dropdownlocation.options[dropdownlocation.selectedIndex].value;
    var selectedbusiness = dropdownbusiness.options[dropdownbusiness.selectedIndex].value;
        for (var count = dropdowncountry.options.length - 1; count > 0; count--) 
        {
            dropdowncountry.options[count] = null;
        }
    var requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(selectedlocation) + "&SelectedBusiness=" + encodeURIComponent(selectedbusiness);

    CreateXmlHttp();
    if (XmlHttp) 
    {
        XmlHttp.onreadystatechange = HandleResponse2;
        XmlHttp.open("GET", requestUrl, true);
        document.body.style.cursor = 'auto';
        XmlHttp.send(null);
        
    }
}

function HandleResponse2() 
{
    if (XmlHttp.readyState == 4) 
    {
        if (XmlHttp.status == 200) 
        {
            ClearAndSetCountryListItems(XmlHttp.responseText);
        }
        else 
        {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

function ClearAndSetCountryListItems(BussinessStr) 
{
    var dropdowncountry = document.getElementById("ddlcountry");
    var ListCountry = BussinessStr;
    var listCountryArray = ListCountry.split('|');

    for (var count = dropdowncountry.options.length - 1; count > -1; count--) 
    {
        dropdowncountry.options[count] = null;
    }
    var textValue;
    var optionItem;
    for (var count = 0; count < listCountryArray.length - 1; count++) 
    {
        textValue = listCountryArray[count];
        optionItem = new Option(textValue, textValue, false, false);
        dropdowncountry.options[dropdowncountry.length] = optionItem;
    }
    if (listCountryArray.length == 3)
    {
        document.getElementById("ddlcountry").selectedIndex = 1;
    }
}


function address(param1, param2, param3,param4)
{
    //alert(param1 + ":" + param2 + ":" + param3 + ":" + param4);

       document.getElementById("ddllocation").selectedIndex = 0;
       document.getElementById("ddlbusiness").selectedIndex = 0;
        if (document.getElementById("ddlbusiness").disabled == true) 
        {
            document.getElementById("ddlbusiness").disabled = false;
        }
       document.getElementById("ddlcountry").selectedIndex = 0;
    
    if (param1 != null && param2 != null && param3 != null && param4 != null) 
    {
        //document.getElementById("mapimage").value = param4;
        document.getElementById("mapimage").value = "Map_Image";
        
        requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(param1) + "&SelectedBusiness=" + encodeURIComponent(param2) + "&SelectedCountry=" + encodeURIComponent(param3);

        CreateXmlHttp();
        if (XmlHttp) 
        {
            XmlHttp.onreadystatechange = HandleResponse3;
            XmlHttp.open("GET", requestUrl, true);
            document.body.style.cursor = 'auto';
            XmlHttp.send(null);

        }
        scroll(0, 950);
        //document.body.
       // ="250px";
//        document.getElementById('mapimg1').style.display = "block";
//        document.getElementById("lblmap").style.visibility = "visible";
//        //document.getElementById("mapimg1").visible = true;
//        document.getElementById("mapimg1").style.visibility = "visible";
//        //document.getElementById("mapimg1").style.cursor = "pointer";
//        document.getElementById("mapimg1").disabled = false;
//        document.getElementById("mapimg1").href = "Images/FlashLocationImage/Map_Image.jpg"; 
        
    }
}

function anchor_test() 
{
    //alert("1");
    if (document.getElementById("mapimage").value != null) 
    {
         document.getElementById("mapimg1").href = "Images/FlashLocationImage/" + document.getElementById("mapimage").value + ".jpg";
        //document.getElementById("mapimg1").href = "Images/FlashLocationImage/Map_Image.jpg";
        document.getElementById("lblmap").Text = "Map";
    }
    else
    {
        document.getElementById("mapimg1").href = "javascript://";
        document.getElementById("lblmap").style.visibility = "hidden";
    }
    // alert(document.getElementById("mapimg1").href);
   
}

function send(val1)
{
   // alert(val1);
   
    
    if (val1 == "0")
    {
        Flashstate = "Zoom"
//        document.getElementById("mapimg1").style.visibility = "visible";
        if (document.getElementById("ddllocation").selectedIndex == 0) 
        {
           // document.getElementById("addressdata").innerHTML = "<b>Corporate Office</b><br/>4th Floor Godrej Millennium<br/>9, Koregaon Road, Pune 411 001<br/>Maharashtra, INDIA<br/>Phone: +91-20-30514000<br/>Fax: +91-20-26138083<br/>E-mail: communications@sterlite.com";
        }
       // document.getElementById("mapimg1").style.cursor = "hand";
       // document.getElementById("mapimg1").onclick = "Javascript: return true;"
        // document.getElementById("mapimg1").href = "Images/FlashLocationImage/Map_Image.jpg";

    }
    else 
    {
//        document.getElementById("lblmap").style.visibility = "hidden";
//        document.getElementById("mapimg1").style.visibility = "hidden";
//        document.getElementById("mapimg1").href = "javascript://";
//        document.getElementById("mapimg1").disabled = true;
        //document.getElementById("mapimg1").style.cursor = "default";
        document.getElementById("addressdata").innerHTML = "";
        Flashstate = "UnZoom"

        document.getElementById("ddllocation").selectedIndex = 0;
        document.getElementById("ddlbusiness").selectedIndex = 0;
        if (document.getElementById("ddlbusiness").disabled == true) {
            document.getElementById("ddlbusiness").disabled = false;
        }
        document.getElementById("ddlcountry").selectedIndex = 0;
    }
}
function GetAddressOnSubmit() 
{

    //document.getElementById("mapimg1").style.cursor = "default";
    if (Flashstate == "UnZoom") 
    {
        flashBack();
        document.getElementById("addressdata").innerHTML = "";
//        document.getElementById("lblmap").style.visibility = "hidden";
       // document.getElementById("lblmap").onclick = "javascript://";
//        document.getElementById("mapimg1").disabled = true;
    }
//    else 
//    {
        var dropdownlocation = document.getElementById("ddllocation");
        var dropdownbusiness = document.getElementById("ddlbusiness");
        var dropdowncountry = document.getElementById("ddlcountry");

        var selectedlocation = dropdownlocation.options[dropdownlocation.selectedIndex].value;
        var selectedbusiness = dropdownbusiness.options[dropdownbusiness.selectedIndex].value;
        var selectedcountry = dropdowncountry.options[dropdowncountry.selectedIndex].value;
        var requestUrl = "";

        if (dropdownlocation.selectedIndex == 0) 
        {
            alert("Please select a Location");
            document.getElementById("ddllocation").focus();
        }
        else if ((selectedlocation != "2" && selectedlocation != "5" && selectedlocation != "4" && selectedlocation != "1") && dropdownbusiness.selectedIndex == 0) {
            alert("Please select a Business");
            document.getElementById("ddlbusiness").focus();
        }
        else if (dropdowncountry.selectedIndex == 0) 
        {
            alert("Please select a Country");
            document.getElementById("ddlcountry").focus();
        }
        else if(selectedlocation == "2")
        {
            selectedbusiness = "Corporate Business";
            requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(selectedlocation) + "&SelectedBusiness=" + encodeURIComponent(selectedbusiness) + "&SelectedCountry=" + encodeURIComponent(selectedcountry);
            CreateXmlHttp();
            if (XmlHttp) {
                XmlHttp.onreadystatechange = HandleResponse3;
                XmlHttp.open("GET", requestUrl, true);
                document.body.style.cursor = 'auto';
                XmlHttp.send(null);

            }
//            document.getElementById("lblmap").style.visibility = "visible";
//            document.getElementById("mapimg1").visible = true;
//            document.getElementById("mapimg1").style.visibility = "visible";
//            document.getElementById('mapimg1').style.display = "block";
//            //document.getElementById("mapimg1").style.cursor = "pointer";
//            document.getElementById("mapimg1").disabled = false;
//            document.getElementById("mapimg1").href = "Images/FlashLocationImage/Map_Image.jpg"; 
        }
        else if (selectedlocation == "5" || selectedlocation == "1" || selectedlocation == "4") 
        {
            selectedbusiness = "Corporate Business";
            requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(selectedlocation) + "&SelectedBusiness=" + encodeURIComponent(selectedbusiness) + "&SelectedCountry=" + encodeURIComponent(selectedcountry);
            CreateXmlHttp();
            if (XmlHttp) 
            {
                XmlHttp.onreadystatechange = HandleResponse3;
                XmlHttp.open("GET", requestUrl, true);
                document.body.style.cursor = 'auto';
                XmlHttp.send(null);
                
            }
        }
        else 
        {
            // document.getElementById("ddlbusiness").disabled = false;

            requestUrl = AjaxServerPageName + "?SelectedLocation=" + encodeURIComponent(selectedlocation) + "&SelectedBusiness=" + encodeURIComponent(selectedbusiness) + "&SelectedCountry=" + encodeURIComponent(selectedcountry);

            CreateXmlHttp();
            if (XmlHttp) 
            {
                XmlHttp.onreadystatechange = HandleResponse3;
                XmlHttp.open("GET", requestUrl, true);
                document.body.style.cursor = 'auto';
                XmlHttp.send(null);
                
            }
        }
//    }
}

function HandleResponse3() 
{
    if (XmlHttp.readyState == 4) 
    {
        if (XmlHttp.status == 200) 
        {
            var CountryAddres = XmlHttp.responseText;
            displayaddresslist(XmlHttp.responseText);
        }
        else 
        {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

function displayaddresslist(AddressList) 
{
    document.getElementById("addressdata").innerHTML = '<span class="submit_sendsms"><input name="submit1" id="submit1" tabindex="6" value="Submit" alt="Send as SMS" title="Send as SMS" src="images/sendsms-button.jpg" onclick=\'return SendAddress();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("submit1", "", true, "", "", false, false))\' style="border-width: 0px; float: left; font-size: 0px;" type="image"></span>'+AddressList;
}

function getFlashMovie(movieName) 
{
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? document[movieName] : document[movieName];
}

function flashBack() 
{
    getFlashMovie("map").sendToFlash();
}

function ValidateBusiness()
{
    var dropdownlocation = document.getElementById("ddllocation");

    if (dropdownlocation.selectedIndex == 0) 
    {
        alert("Please select a Location first");
        document.getElementById("ddllocation").focus();
        return false;
    }
}

function ValidateCountry() 
{
    var dropdownlocation = document.getElementById("ddllocation");
    var dropdownbusiness = document.getElementById("ddlbusiness");

    if (dropdownlocation.selectedIndex == 0) 
    {
        alert("Please select a Location first");
        document.getElementById("ddllocation").focus();
        return false;
    }
    else if (dropdownlocation.selectedIndex != 0 && dropdownbusiness.selectedIndex == 0 && document.getElementById("ddlbusiness").disabled == false) 
    {
        alert("Please select a Business");
        document.getElementById("ddlbusiness").focus();
        return false;
    }
}
