
function clearSelect(selectorname) {
    var selector;
    if (selectorname == 'state') {
        selector = $('#state')[0];
    } else if (selectorname == 'city') {
        selector = $('#city')[0];
    }else{
        selector = $('#'+selectorname)[0];
    }
    if (! selector) return false;
    for (i = selector.options.length-1; i >0 ; i--) {
	    selector.options[i] = null;
	}
	selector.selectedIndex == 0;
	selector.disabled  = true;
}

function Country() {
    
    clearSelect('state');
    clearSelect('city');
    
    var country = $('#country')[0];
    if (! country) return false;
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_states_by_country';
	url += '?country=' + country.value;
	$('#state_message')[0].innerHTML = "<img src=\"" + IMG_DOMAIN + "/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
	$.get(url, function(data) {

        $('#state_message')[0].innerHTML = "";
    
        if ( ! $('state', data).size()) {
            $('#state')[0].disabled = true;
            State();
            return;
        } else {
        if($('#not_in_list')[0]){
	        $('#not_in_list')[0].style.display = 'none';
	    }

            $('#state')[0].disabled = false;
            $('#city')[0].disabled  = true;
        }
	    
	    $('state', data).each(function() {
            var state_name = $('state_name', this).text();
            var state_id = $('state_id', this).text();
                
            NewOption = new Option(state_name, state_id);
		    $('#state')[0].options.add(NewOption);
        } );
    } );
}

// to reduce the traffic, we use a static JS for four countries.
// see state.js for more details
function Country_JS() {
    
    clearSelect('state');
    clearSelect('city');

    var country = $('#country')[0];
    if (! country) return false;
    if (country.value == 0) return false;
    $('#state_message')[0].innerHTML = "<img src=\"" + IMG_DOMAIN + "/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
    eval('state = ' + state_str);
    eval('states = state.c' + country.value);

    if (states == undefined) {
        $('#state')[0].disabled = true;
        State();
	    $('#state_message')[0].innerHTML = "";
        return;
    } else {
        
        if($('#not_in_list')[0]){
	        $('#not_in_list')[0].style.display = 'none';
	    }
	    
        $('#state')[0].disabled = false;
        $('#city')[0].disabled  = true;
    }
    
    for (i = 0; i < states.length; i++) {
       var NewOption = new Option(states[i].state_name, states[i].state_id);
		$('#state')[0].options.add(NewOption);
    }
    $('#state_message')[0].innerHTML = "";    
    if (typeof(default_state) != 'undefined') {
        $('#state')[0].value = default_state;
        default_state = undefined;
    }
}



function State() {
    
    clearSelect('city');
    
    var country = $('#country')[0];
    var state = $('#state')[0];
    if (! country) return false;
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_cities_by_state';
	url += '?country=' + country.value + '&state=' + state.value;
	$('#city_message')[0].innerHTML = "<img src=\"" + IMG_DOMAIN + "/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
	
	$.get(url, function(data) {

        $('#city_message')[0].innerHTML = "";

        if($('#not_in_list')[0]){
            var country_id = country.value;
            if (country_id=='184' || country_id =='95' || country_id ==''||country_id=='121'  || country_id =='95'){
                $('#not_in_list')[0].style.display = 'none';
            }else{
	            $('#not_in_list')[0].style.display = 'inline';
	        }
	    }

        if ( ! $('city', data).size()) {
            if (!(typeof(need_school) == 'undefined')){
                City(0);
            }
                if($('#not_in_list')[0]){
	            $('#not_in_list')[0].style.display = 'none';
	            }

            if($('#state')[0].disabled && !(country_id=='184' || country_id =='95' || country_id ==''||country_id=='121'  || country_id =='95')){
                if($('#add_new_city')[0]){
                    $('#add_new_city').show();
                }
            }
            $('#city')[0].disabled  = true;
            return;
        } else {
            $('#city')[0].disabled  = false;
        }

        $('city', data).each(function() {
            var city_name = $('city_name', this).text();
            var city_id = $('city_id', this).text();

            var NewOption = new Option(city_name, city_id);
		    $('#city')[0].options.add(NewOption);
        } );
    
        if (typeof(default_city) != 'undefined') {
            $('#city')[0].value = default_city;
            default_city = undefined;
        }
    } );
}

function getAge(dateString,dateType) {
/*
   function getAge
   parameters: dateString dateType
   returns: value

   dateString is a date passed as a string in the following
   formats:

   type 1 : 19970529
   type 2 : 970529
   type 3 : 29/05/1997
   type 4 : 29/05/97
   
   document.write(getAge("19650104",1)+'<BR>');
   document.write(getAge("650104",2)+'<BR>');
   document.write(getAge("04/01/1965",3)+'<BR>');
   document.write(getAge("04/01/65",4)+'<BR>');

   dateType is a numeric integer from 1 to 4, representing
   the type of dateString passed, as defined above.

   Returns string containing the age in years, months and days
   in the format yyy years mm months dd days.
   Returns empty string if dateType is not one of the expected
   values.
*/

    var now = new Date();
    var today = new Date(now.getYear(),now.getMonth(),now.getDate());

    var yearNow = now.getYear();
    var monthNow = now.getMonth();
    var dateNow = now.getDate();

    if (dateType == 1)
        var dob = new Date(dateString.substring(0,4),
                            dateString.substring(4,6)-1,
                            dateString.substring(6,8));
    else if (dateType == 2)
        var dob = new Date(dateString.substring(0,2),
                            dateString.substring(2,4)-1,
                            dateString.substring(4,6));
    else if (dateType == 3)
        var dob = new Date(dateString.substring(6,10),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else if (dateType == 4)
        var dob = new Date(dateString.substring(6,8),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else
        return '';

    var yearDob = dob.getYear();
    var monthDob = dob.getMonth();
    var dateDob = dob.getDate();

    yearAge = yearNow - yearDob;

    if (monthNow >= monthDob)
        var monthAge = monthNow - monthDob;
    else {
        yearAge--;
        var monthAge = 12 + monthNow -monthDob;
    }

    if (dateNow >= dateDob)
        var dateAge = dateNow - dateDob;
    else {
        monthAge--;
        var dateAge = 31 + dateNow - dateDob;

        if (monthAge < 0) {
            monthAge = 11;
            yearAge--; 
        }
    }

    if (yearAge > 1900) { yearAge = yearAge - 1900; }

    return yearAge;
}


    function DaysInMonth(WhichMonth, WhichYear) {
		var DaysInMonth = 31;
		if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11) DaysInMonth = 30;
		if (WhichMonth == 2 && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
		if (WhichMonth == 2 && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
		return DaysInMonth;
	}
	function ChangeOptionDays() {
		var obj;
		DaysObject = $('#DD')[0];
		var year;
		if ($('#YYYY')[0].value) {
		    year = $('#YYYY')[0].value;
		} else {
		    year = 2006;
		}
		DaysForThisSelection = DaysInMonth($('#MM')[0].value, year);
			
		CurrentDaysInSelection = DaysObject.length - 1;
		if (CurrentDaysInSelection > DaysForThisSelection) {
			for(i=CurrentDaysInSelection;i>DaysForThisSelection;i--){
				DaysObject.options[i] = null
			}
		}
		if (DaysForThisSelection > CurrentDaysInSelection) {
			for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++) {
				NewOption = new Option(DaysObject.options.length);
				DaysObject.options.add(NewOption);
			}
		}
		if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
	}

