﻿/// <reference name="MicrosoftAjax.js"/>

//Sys.Application.add_load(AppInit);
        var clientID;

        function SendTypoReport(url, typo, context) {
            if ($get("ctl00_MainSection_tbxTypo").value == "" && $get("ctl00_MainSection_tbxContext").value == "")
                return;
                
            Sys.Net.WebServiceProxy.invoke("/Services/GTYServices.asmx", "SendTypoReport", false, {"URL":url, "Typo":typo, "Context":context}, OnSuccess, OnFailure, null, 1000000);
            $get("ctl00_MainSection_tbxTypo").value = "";
            $get("ctl00_MainSection_tbxContext").value = "";

        }
        
        
        function OnSuccess(result) {
            //do nothing  
            alert("Thank you for helping us improve the quality of our resources!");      
        }
        
        function OnFailure(error) {
         // Error handling
          //alert(error.get_message());
        }

        function FillExposure(clientid) {
        
            clientID = clientid.replace(/__Page/, "") + "_";
          
            var countryddl = $get(clientID + "ddlCountry");
            var countryid = countryddl == null ? 1 : countryddl.options[countryddl.selectedIndex].value;
            
            var stateddl = $get(clientID + "ddlState");
            var stateid = stateddl == null ? 0 : stateddl.options[stateddl.selectedIndex].value;
            
                
            Sys.Net.WebServiceProxy.invoke("/Services/GTYServices.asmx", "GetRadioStations", false, {"CountryID":countryid, "StateID":stateid}, SuccessExposure, ErrorExposure, null, 1000000);

        }
        
        function SuccessExposure(_list)
        {
            var exposureddl = $get(clientID.replace(/addBilling/, "eExposure") + "ddlExposure");
         
            var i;
            for (i=exposureddl.options.length-1; i >= 5; i--)
                exposureddl.remove(i);
            
            for(i = 0; i < _list.length; i++)
            {
                var _option = document.createElement("option");
                _option.text = _list[i].split("|")[0];
                _option.value = _list[i].split("|")[1];
                exposureddl.options.add(_option);
            }
        }
        
        function ErrorExposure(error)
        {
            //alert(error.get_message());
        }
        
        
        function FillStates(clientid)
        {
            var countryid;
            clientID = clientid.replace(/__Page/, "") + "_";

            var countryddl = $get(clientID + "ddlCountry");
            var stateddl = $get(clientID + "ddlState");
                
            countryid = countryddl.options[countryddl.selectedIndex].value;

            var i;
            for (i=stateddl.options.length-1; i >= 0; i--)
                stateddl.remove(i);

            var _option = document.createElement("option");
            _option.value = 0;
            _option.text = countryid == 2 ? "Please Select Province" : "Please Select State";
            stateddl.options.add(_option);

            if(countryid != "1" && stateddl.onblur != null)
            {
                if(countryddl.onblur == null)
                {
                    FillExposure(clientid);
                    countryddl.onblur = function() { FillExposure(clientid); };
                }
                
                stateddl.onblur = null;
            }
            else if(countryid == "1" && countryddl.onblur != null)
            {
                countryddl.onblur = null;
                stateddl.onblur = function() { FillExposure(clientid); };
            }

            Sys.Net.WebServiceProxy.invoke("/Services/GTYServices.asmx", "GetStatesByCountryID", false, {"CountryID":countryid}, FillStateList, ErrorGettingStates, null, 1000000);

        }
        
        function FillStateList(_list)
        {
            var countryddl = $get(clientID + "ddlCountry");
            var countryid = countryddl.options[countryddl.selectedIndex].value;

            var stateddl = $get(clientID + "ddlState");
                
            
            for(i = 0; i < _list.length; i++)
            {
                var _option = document.createElement("option");
                _option.text = _list[i].split("|")[0];
                _option.value = _list[i].split("|")[1];
                stateddl.options.add(_option);
            }
            
            var tbxState = $get(clientID + "tbxState");
            var hfState = $get(clientID + "hfState");
            tbxState.className = stateddl.options.length == 1 ? "ShowElement" : "HideElement";
            hfState.value = stateddl.options.length == 1 ? "none" : "";
            stateddl.className = stateddl.options.length > 1 ? "ShowElement" : "HideElement";
            
            var pnlState = $get(clientID + "pnlState");
            var lblState = $get(clientID + "lblState");
            switch (countryid)
            {
                case "1":
                case "3":
                case "236": pnlState.className = "ShowElement"; lblState.innerHTML = "State"; lblState.style.fontWeight = "bold"; break;
                case "2": pnlState.className = "ShowElement"; lblState.innerHTML = "Province"; lblState.style.fontWeight = "bold"; break;
                case "10":
                case "11":
                case "237":
                case "6": pnlState.className = "HideElement"; hfState.value = "none"; lblState.style.fontWeight = "normal"; break;
                default: pnlState.className = "ShowElement"; lblState.innerHTML = "State"; lblState.style.fontWeight = "normal"; break;
            }
            
            // ASSGIN ZIP TITLE
            var lblZip = $get(clientID + "lblZip");
            switch (countryid)
            {
                case "3": // Australia
                case "6": // United Kingdom
                case "11": // New Zealand
                case "237": // Singapore
                case "10": lblZip.innerHTML = "Postal Code"; break; // South Africa
                default: lblZip.innerHTML = "Zip Code"; break;
            }
        }
        
        function CopyStateToTextbox(clientID)
        {
            var stateddl = $get(clientID + "_ddlState");
            var hfState = $get(clientID + "_hfState");
            hfState.value = stateddl.options[stateddl.selectedIndex].text + "|" + stateddl.options[stateddl.selectedIndex].value;
        }

        function ErrorGettingStates(error)
        {
            alert(error.get_message());
        }
        
        function VerifyEmailAddress(ClientID, tbxEmail)
        {
            var _email = $get(tbxEmail);
            clientID = ClientID;
            Sys.Net.WebServiceProxy.invoke("/Services/GTYServices.asmx", "VerifyEmailAddress", false, {"Email":_email.value}, EmailResult, EmailError, null, 1000000);
        }
        
        function EmailResult(result)
        {
            if(result == true)
                $get(clientID + "EmailVerified").value = "Email already exists.";
            else
                $get(clientID + "EmailVerified").value = "";

        }
        
        function EmailError(error)
        {
            alert(error.get_message());
        }