/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function () {
    var countryID = $('#countryID').val();
    var city = $('#cityhidValue').val();
    var state = $('#stateIDhid').val();
    //listCities(countryID, city);
    listStates(countryID, state);

    //validate date of birth
    if ($("#dobMonth").val() != '' && $("#dobYear").val() != '' && $("#dobDay").val() != '') {
        validateDOB()
    }

    $('.div_upload_sub_userProfilePicture_edit').addClass('edit_image');
    $('.div_upload_sub_userProfilePicture_edit a').html('Edit');
   
    $(document).on('change',".dob-valid", function () {
        if ($("#dobMonth").val() != '' && $("#dobYear").val() != '' && $("#dobDay").val() != '') {
            validateDOB();
        }
    })
});
//ajax city populating
function listCities(countryID, userCity)
{
    $('#cityhidValue').val(userCity);
	if(countryID!=''){
		var data={
			"countryID" : countryID ,
			"userCity" : userCity
		};
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH + "/ajax/ajaxCity/",
			data: data,
			beforeSend:function(){
				$("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2" height="16" width="16" alt="loader" />');
			},
			success: function (msg) {
				$("#loadProcessImage").empty();
				$(".choosen").chosen();
				if (msg != '') {
					$("#cityInput").show();
					$('#userCity').empty();
					$("#cityProvinceInput").hide();
					$("#userCity").append('<option value="">' + lblSelect + '</option>' + msg);
					$('#userCity').trigger("liszt:updated");
					$("#citySelectedType").val("city");
					$("#userProvince").val('');
					$("#div_err_frmBusiness_provincehidden").css("display", "none");
				} else {
					$("#cityInput").hide();
					$("#cityProvinceInput").show();
					$("#citySelectedType").val("province");
					$("#userCity").val('');
					$("#div_err_frmBusiness_cityhidden").css("display", "none");
				}
			}
		});
	}
}
function getCityHiddenValue(val)
{
    $('#cityhidValue').val(val);
}
function validateDOB() {
    var month = $("#dobMonth").val();
    var year = $("#dobYear").val();
    var days = Math.round(((new Date(year, month)) - (new Date(year, month - 1))) / 86400000);
    var dobDay = $("#dobDay").val();
    if (dobDay > days) {
        $("#hiddenDob").val(1);
    } else {
        $("#hiddenDob").val(0);
    }
}
//ajax state populating
function listStates(countryID,stateID){
	
	if(countryID!=''){
		var data=new Object();
		data.countryID=countryID;
		data.stateID=stateID;
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH+"/ajax/ajaxState/",
			data: data,
			beforeSend:function(){
				$("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
			},
			success: function(msg){  
				if(msg!=''){
					$("#stateInput").show();
					$('#stateID').empty();
					$("#stateProvinceInput").hide();
					$("#stateID").append('<option value="">'+lblSelect+'</option>'+msg);
					$("#stateSelectedType").val("state");	
					$("#billingProvince").val('');
				} else {
					$("#stateInput").hide();
					$("#stateProvinceInput").show();
					if(stateID==0) { 
						stateID	=	'';
					}
					if(!(stateID>=0 || stateID==''))
					$("#stateIDhid").val(stateID);
					$("#stateSelectedType").val("province");
						$("#billingState").empty()
						$("#billingStateID").val('');
				}
				$("#loadProcessImage").empty();
			}
		});
	}
}
