
function clearSelect(selectorname) {
    var selector;
    if (selectorname == 'state') {
        selector = $('state');
    } else if (selectorname == 'city') {
        selector = $('city');
    }
    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');
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_states_by_country';
	var pars = 'country=' + country.value;
	$('state_message').innerHTML = "<img src=\"http://img.zpcdn.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
	var myAjax = new Ajax.Request( url, {
	    method: 'get',
	    parameters: pars,
	    onSuccess: showCountryResponse,
	    onFailure: reportCountryError
	} );
}

// 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');
    if (country.value == 0) return false;
    $('state_message').innerHTML = "<img src=\"http://img.zpcdn.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
    eval('state = ' + state_str);
    eval('states = state.c' + country.value);

    if (states == undefined) {
        $('state').disabled = true;
        State();
	$('state_message').innerHTML = "";
        return;
    } else {
        $('state').disabled = false;
        $('city').disabled  = true;
    }
    
    for (i = 0; i < states.length; i++) {
        NewOption = new Option(states[i].state_name, states[i].state_id);
		$('state').options.add(NewOption);
    }
    $('state_message').innerHTML = "";
}

function showCountryResponse(request) {
    response  = request.responseText;

    var xotree = new XML.ObjTree();
    xotree.force_array = [ "state" ];
    var tree = xotree.parseXML( response ); 

    $('state_message').innerHTML = "";
    
    if ( ! tree.root) {
        $('state').disabled = true;
        State();
        return;
    } else {
        $('state').disabled = false;
        $('city').disabled  = true;
    }
    
    for (i = 0; i < tree.root.state.length; i++) {
        NewOption = new Option(tree.root.state[i].state_name, tree.root.state[i].state_id);
		$('state').options.add(NewOption);
    }
}
function reportCountryError(request) {
    $('state_message').innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}

function State() {

    clearSelect('city');

    var country = $('country');
    var state = $('state');
    if (country.value == 0) return false;
    var url = '/ajax/myprofile/get_cities_by_state';
	var pars = 'country=' + country.value + '&state=' + state.value;
	$('city_message').innerHTML = "<img src=\"http://img.zpcdn.com/loader.gif\" width=\"16\" height=\"16\" hspace=5>";
	var myAjax = new Ajax.Request( url, {
	    method: 'get',
	    parameters: pars,
	    onSuccess: showStateResponse,
	    onFailure: reportStateError
	} );
}
function showStateResponse(request) {
    response  = request.responseText;

    var xotree = new XML.ObjTree();
    xotree.force_array = [ "city" ];
    var tree = xotree.parseXML( response ); 

    $('city_message').innerHTML = "";

    if($('not_in_list')){
	    Element.show('not_in_list');
	}
    
    if ( ! tree.root) {
        $('city').disabled  = true;
        return;
    } else {
        $('city').disabled  = false;
    }

    for (i = 0; i < tree.root.city.length; i++) {
        NewOption = new Option(tree.root.city[i].city_name, tree.root.city[i].city_id);
		$('city').options.add(NewOption);
    }
    
    if (typeof(default_city) != 'undefined') {
        $('city').value = default_city;
    }
}
function reportStateError(request) {
    $('city_message').innerHTML = '<p>'+ZORPIA_JS_LANG.ENCOUTER_SOME_ERROR+'</p>';
}


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');
		var year;
		if ($('YYYY').value) {
		    year = $('YYYY').value;
		} else {
		    year = 2006;
		}
		DaysForThisSelection = DaysInMonth($('MM').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;
	}