var siteurl = 'http://www.nextstepchina.org';
//var siteurl = 'http://localhost/wp3beta1';

function getAjaxObject() {
    var ajaxRequest;  // The variable that makes Ajax possible! 
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
        return ajaxRequest;
    } catch(e){
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            return ajaxRequest;
        } catch(e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                return ajaxRequest;
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return "";
            }
        }
    }
}

function setCities(program_id) {
    var ajax = getAjaxObject();
    if(ajax) {
        ajax.onreadystatechange = function() {
	        if(ajax.readyState == 4) {
	            var response_text = ajax.responseText;
	            var citiesSelect = document.getElementById("PricingCities");
	            citiesSelect.length = 0;
	            appendOptionLast(citiesSelect, 'Select', '');

	            eval(response_text);
	            
	            if(cities.length > 0) {
		            for(var id in cities) {
                        if(isInt(id))
		                  appendOptionLast(citiesSelect, cities[id], id);
		            }
		        }
	        }
	    }
	    if(!program_id) program_id = -1;
        ajax.open("GET", siteurl + "/update-pricing-form.php?data=city&id=" + program_id, true);
        ajax.send(null);
    }
}

function setUniversities(city_id) {
    var ajax = getAjaxObject();
    if(ajax) {
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                var response_text = ajax.responseText;
                var universitiesSelect = document.getElementById("PricingUniversities");
                universitiesSelect.length = 0;
                appendOptionLast(universitiesSelect, 'Select', '');

                eval(response_text);
                
                if(universities.length > 0) {
                    for(var id in universities) {
                        if(isInt(id))
                            appendOptionLast(universitiesSelect, universities[id], id);
                    }
                }
            }
        }
        var program_id = document.getElementById('PricingPrograms').value;
        if(!city_id) city_id = -1;
        ajax.open("GET", siteurl + "/update-pricing-form.php?data=university&cid=" + city_id + "&pid=" + program_id, true);
        ajax.send(null);
    }
}

function setDurations(university_id) {
    var ajax = getAjaxObject();
    if(ajax) {
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                var response_text = ajax.responseText;
                var durationsSelect = document.getElementById("PricingDurations");
                durationsSelect.length = 0;
                appendOptionLast(durationsSelect, 'Select', '');

                eval(response_text);
                
                for(var id in durations) {
                    if(id == durations[id])
                        appendOptionLast(durationsSelect, durations[id], id);
                }
            }
        }
        var program_id = document.getElementById('PricingPrograms').value;
        var city_id = document.getElementById('PricingCities').value;
        if(!university_id) university_id = -1;
        ajax.open("GET", siteurl + "/update-pricing-form.php?data=duration&uid=" + university_id + "&cid=" + city_id + "&pid=" + program_id, true);
        ajax.send(null);
    }
}

function setHousing(duration) {
    var ajax = getAjaxObject();
    if(ajax) {
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                var response_text = ajax.responseText;
                var housingSelect = document.getElementById("PricingHousing");
                housingSelect.length = 0;
                appendOptionLast(housingSelect, 'Select', '');

                eval(response_text);
                
                for(var id in housing) {
                    var val = String(housing[id]);
                    if(val.indexOf('function') < 0)
                        appendOptionLast(housingSelect, housing[id], id);
                }
            }
        }
        var program_id = document.getElementById('PricingPrograms').value;
        var city_id = document.getElementById('PricingCities').value;
        var university_id = document.getElementById('PricingUniversities').value;
        ajax.open("GET", siteurl + "/update-pricing-form.php?data=housing&d=" + duration + "&uid=" + university_id + "&cid=" + city_id + "&pid=" + program_id, true);
        ajax.send(null);
    }
}

function setTutoring(duration) {
    var ajax = getAjaxObject();
    if(ajax) {
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                var response_text = ajax.responseText;
                var tutoringSelect = document.getElementById("PricingTutoring");
                tutoringSelect.length = 0;
                //appendOptionLast(PricingTutoring, 'Select', '');

                eval(response_text);

                for(var key in tutoring) {
                    var val = String(tutoring[key]);
                    if(val.indexOf('function') < 0)
                        appendOptionLast(tutoringSelect, tutoring[key], key);
                }
            }
        }
        var program_id = document.getElementById('PricingPrograms').value;
        var city_id = document.getElementById('PricingCities').value;
        var university_id = document.getElementById('PricingUniversities').value;
		var duration = document.getElementById('PricingDurations').value;
        ajax.open("GET", siteurl + "/update-pricing-form.php?data=tutoring&d=" + duration + "&uid=" + university_id + "&cid=" + city_id + "&pid=" + program_id, true);
        ajax.send(null);
    }
}

function appendOptionLast(selectElement, the_caption, the_value) {
    var elOptNew = document.createElement('option');
    elOptNew.text = the_caption;
    elOptNew.value = the_value;

    try {
        selectElement.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        selectElement.add(elOptNew); // IE only
    }
}

function popupPriceQuote() {
    var program = document.getElementById('PricingPrograms').value;
    var city = document.getElementById('PricingCities').value;
    var school = document.getElementById('PricingUniversities').value;
    var duration = document.getElementById('PricingDurations').value;
    var housing = document.getElementById('PricingHousing').value;
    var tutoring = document.getElementById('PricingTutoring').value;
    
    if(!program) {
        alert('Please select a program!');
        return false;
    } else if(!city) {
        alert('Please select a city!');
        return false;
    } else if(!school) {
        alert('Please select a school!');
        return false;
    } else if(!duration) {
        alert('Please select the duration of the program!')
        return false;
    } else if(!housing) {
        alert('Please select a housing option!');
        return false;
    }
    
    //var url = siteurl + '/fast-price-popup?program=' + program + '&city=' + city + '&school=' + school + '&duration=' + duration + '&housing=' + housing + '&tutoring=' + tutoring;
    //window.open(url, "_blank", "width=620,height=800,status=0,toolbar=0,resizable=0,scrollbars=0");
    return true;
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 }

