var http_dropdowns = createRequestObject();
var http_school_dropdowns = createRequestObject();

function createRequestObject()
{
        var httpRequest;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch(e) {}
                }
            }
        }
        return httpRequest;
}

var prev_input_div;
function highlight_input_div(obj)
{
        if(prev_input_div)
        {
                prev_input_div.style.backgroundColor = '#3da2bf';
        }

        if(obj)
        {
                obj.style.backgroundColor = '#4388A3';
                prev_input_div = obj;
        }
}

function toggle_country_regions()
{
        var country = document.getElementById("country").value;
        if(country == 'CA')
        {
                document.getElementById("canada_a").style.display="";
                document.getElementById("canada_b").style.display="";
                document.getElementById("us_a").style.display="none";
                document.getElementById("us_b").style.display="none";
        }
        else
        {
                document.getElementById("us_a").style.display="";
                document.getElementById("us_b").style.display="";
                document.getElementById("canada_a").style.display="none";
                document.getElementById("canada_b").style.display="none";
        }
        
        clearDistrict();
}

function get_state_text()
{
        if(document.getElementById("country").value == "CA")
        {
               return "Province";
        }
        else
        {
               return "State";
        }
}

function change_district_school_labels()
{
        if(document.getElementById("district_type"))
        {
                var district_type = document.getElementById("district_type").value;
                
                var district_label = 'District';
                var school_label = 'School';
                
                if(district_type == 'private')
                {
                        district_label = 'School';
                        school_label = 'Campus';
                }
                
                if(document.getElementById("district_label")) document.getElementById("district_label").innerHTML = district_label;
                if(document.getElementById("not_listed_district_label")) document.getElementById("not_listed_district_label").innerHTML = district_label;
                
                if(document.getElementById("school_label")) document.getElementById("school_label").innerHTML = school_label;
                if(document.getElementById("not_listed_school_label")) document.getElementById("not_listed_school_label").innerHTML = school_label;
        }
}

function get_state_districts()
{
	var country = document.getElementById("country").value;
        var state;
        var district_type = "";
	var show_upload_generic_leads = "";
	
	if(document.getElementById("district_type"))
	{
		district_type = "&district_type=" + document.getElementById("district_type").value;
	}

	if(what_wizard == "leads")
	{
		show_upload_generic_leads = "&show_upload_generic_leads=true";
	}
        else
        {
		show_upload_generic_leads = "";
	}

	if(country == 'CA')
	{
		state = document.getElementById("province").value;
	}
	else
	{
		state = document.getElementById("state").value;
	}

	if(state != "")
	{
		http_dropdowns = createRequestObject();
		
                http_dropdowns.open('get', document.getElementById("http_self").value + '/includes/common_district_school_ajax.php?action=get_state_districts&country=' + country + '&state=' + state + district_type + show_upload_generic_leads, true);
                
		http_dropdowns.onreadystatechange = handleGetStateDistrictsData;

		if(http_dropdowns.readyState > 0)
		{
			http_dropdowns.send(null);
		}
	}
	
	clearDistrict();

	custom_get_state_districts();
}

function handleGetStateDistrictsData()
{
        if(http_dropdowns.readyState == 4)
        {
                if(http_dropdowns.status == 200)
                {
                        var response = http_dropdowns.responseText;

                        document.getElementById("district_div").innerHTML = response;
                }
        }
}

function get_district_schools()
{
        var district = "";
        if(document.getElementById("district"))
        {
                district = document.getElementById("district").value;

                clearSchool();
                
                if(http_school_dropdowns) http_school_dropdowns.abort();
                
                if(district != '' && district != 'not_listed')
                {
                        clearDistrictNotListed();
                        clearSchoolNotListed();
                        
                        var district_type = "";
                        
                	if(document.getElementById("district_type"))
                	{
                		district_type = "&district_type=" + document.getElementById("district_type").value;
                	}
                        
                        http_school_dropdowns = createRequestObject();
                        //alert(document.getElementById("http_self").value + '/includes/common_district_school_ajax.php?action=get_district_schools&district=' + district + district_type);
                        
                        http_school_dropdowns.open('get', document.getElementById("http_self").value + '/includes/common_district_school_ajax.php?action=get_district_schools&district=' + district + district_type, true);

                        http_school_dropdowns.onreadystatechange = handleGetDistrictSchoolsData;

                        if(http_dropdowns.readyState > 0)
                        {
                                http_school_dropdowns.send(null);
                        }
                }
                else if(district == 'not_listed')
                {
                        showDistrictNotListed();
                }
                else
                {
                        clearDistrictNotListed();
                        clearSchoolNotListed();
                }
        }
}

function handleGetDistrictSchoolsData()
{
        if(http_school_dropdowns.readyState == 4)
        {
                if(http_school_dropdowns.status == 200)
                {
                        var response = http_school_dropdowns.responseText;
                        
                        document.getElementById("school_div").innerHTML = response;
                }
        }
}

function clearDistrict()
{
        if(document.getElementById("district"))
        {
                var state_text = get_state_text();
                
                document.getElementById("district_div").innerHTML = '<select id="district" name="district" disabled="disabled"><option value="">Choose a ' + state_text + ' first</option></select>';
                
                clearDistrictNotListed();
        }
}

function clearDistrictNotListed()
{
        if(document.getElementById("not_listed_district_input")) document.getElementById("not_listed_district_input").style.display = 'none';
        if(document.getElementById("not_listed_district_message")) document.getElementById("not_listed_district_message").style.display = 'none';
        if(document.getElementById("not_listed_district_name")) document.getElementById("not_listed_district_name").value = '';
        if(document.getElementById("not_listed_district_submit")) Util.addClass(document.getElementById("not_listed_district_submit"), 'disabled');
}

function showDistrictNotListed()
{
        if(document.getElementById("not_listed_district_input")) document.getElementById("not_listed_district_input").style.display = '';
        if(document.getElementById("not_listed_district_message")) document.getElementById("not_listed_district_message").style.display = '';
        
        var district_type;
        
        if(document.getElementById("district_type"))
        {
                district_type = document.getElementById("district_type").value;
        }
        
        if(district_type != 'private' && document.getElementById("not_listed_school_input")) document.getElementById("not_listed_school_input").style.display = '';
}

function clearSchool()
{
	if(document.getElementById("school"))
	{
		var district_label = 'District';
		
                if(document.getElementById("district_type"))
                {
                        if(document.getElementById("district_type").value == 'private')
                        {
                                district_label = 'School';
                        }
                }
		
                //document.getElementById("school_result").innerHTML = '';

		document.getElementById("school_div").innerHTML = '<select id="school" name="school" disabled="disabled"><option value="">Choose a ' + district_label + ' first</option></select>';
		
		clearSchoolNotListed();
	}
}

var Util = {
        removeClass: function (el, sRemove) {
                if (el) {
                        el.className = el.className.replace(new RegExp("(\\s"+sRemove+"|"+sRemove+"\\s|"+sRemove+")"),"");
                        return true;
                }
                return false;
        },
        addClass: function (el, sNewClass) {
                if (el && el.className.indexOf(sNewClass) < 0) {
                        el.className = (el.className.length > 0) ? el.className+" "+sNewClass : sNewClass;
                        return true;
                }
                return false;
        }
}


function clearSchoolNotListed()
{
        if(document.getElementById("not_listed_school_input")) document.getElementById("not_listed_school_input").style.display = 'none';
        if(document.getElementById("not_listed_school_message")) document.getElementById("not_listed_school_message").style.display = 'none';
        if(document.getElementById("not_listed_school_name")) document.getElementById("not_listed_school_name").value = '';
        if(document.getElementById("not_listed_school_submit")) Util.addClass(document.getElementById("not_listed_school_submit"), 'disabled');
}

function showSchoolNotListed()
{
        if(document.getElementById("not_listed_school_input")) document.getElementById("not_listed_school_input").style.display = '';
        if(document.getElementById("not_listed_school_message")) document.getElementById("not_listed_school_message").style.display = '';
}

function not_listed_district_check()
{
        if(document.getElementById("not_listed_district_name"))
        {
                if(document.getElementById("not_listed_district_name").value != '')
                {
                        not_listed_school_check();
                }
                else
                {
                        alert('Please enter a district name');
                }
        }
        else
        {
                document.getElementById("finish_button").click();
        }
}

function not_listed_school_check()
{
        if(document.getElementById("not_listed_school_name"))
        {
                if(document.getElementById("not_listed_school_name").value != '')
                {
                        document.getElementById("finish_button").click();
                }
                else
                {
                        alert('Please enter a school name');
                }
        }
        else
        {
                document.getElementById("finish_button").click();
        }
}
