/**
 * Called on add/delete event
 */
function switchControls(enable) {
	if (enable) {
		jQuery('#btnSavePostRegEduExp').removeAttr('disabled');
		jQuery('#lnk-less-info').show();
	}
	else {
		jQuery('#btnSavePostRegEduExp').attr('disabled', 'disabled');
		jQuery('#lnk-less-info').hide();
	}
}

/**
 * Entered location is invalid
 */
function eduLocationError() {
	jQuery('#frm-main-education-location-error').show();
	jQuery('#frm-main-education-location-error').prev('p').hide();
}

/**
 * Edit education event
 */
var educationEditLinkBind = function(event) {
	educationId = parseInt(event.target.id);
	if (educationId > 0) {
		jQuery(event.target).mrAjax({
			bind: 'none',
			data: {
				handler:       'AJAX_JSON_UserProfileManager',
				__callHandler: 'loadAddEducationForm',
				educationId :  educationId
			},
			callback: onEducationFormCreated
		});
	}
	return false;
};

/**
 * Hide education form
 */
var onEducationFromCancel = function(event) {
	jQuery('#pnlUniversityAdd > div').remove();
	jQuery('#pnlUniversityAdd').hide();
	jQuery('#lnkShowAddEducationForm').show();
	jQuery('#lnkShowAddEducationHighForm').show();
   	jQuery('#pnlEducationIntro').show();
};

/**
 * Education saved
 */
var onEducationDataSaved = function(req, stat, data) {
	if (stat.Response) {
		onEducationFromCancel();
		jQuery('.edit-education').click(educationEditLinkBind);
	}
};

/**
 * Delete education callback
 */
var onEducationDeleted = function(event) {
	onEducationFromCancel();
	jQuery('.edit-education').click(educationEditLinkBind);
};

/**
 * Inits handlers after education form was created
 */
var initAJAXHandlersForLoadedEducationForm = function() {
	/**
	 * Save data event
	 */
	jQuery('#frm-main-education-addpos').click(function(event) {
		if (!validateEducationForm()) {
			return false;
		}
		var $this = jQuery(this);
		$this.mrAjax({
			bind: 'none',
			data: {
				handler:           'AJAX_JSON_UserProfileManager',
				__callHandler:     'saveEducationData',
				cityId:            jQuery('#location_id').val(),
				cityName:          jQuery('#locationJQ').val(),
				universityId:      jQuery('#university_id').val(),
				universityName:    jQuery('#universityJQ').val(),
				collegeName:       jQuery('#frm-main-education-college').val(),
				txtDegree:         jQuery('#frm-main-education-degree').val(),
				txtFields:         jQuery('#frm-main-education-concetrat').val(),
				txtNotes:          jQuery('#frm-main-education-notes').val(),
				ddlAttendFor:      jQuery('#frm-main-education-attendFor').val(),
				ddlAttendTo:       jQuery('#frm-main-education-attendTo').val(),
				educationId:       jQuery('#frm-main-education-id').val(),
				universityType:    jQuery('#frm-main-education-type').val()
			},
			callback: onEducationDataSaved
		});
	});

	/**
	 * Delete education event
	 */
	jQuery('#lnkDeleteEducation').click(function(event) {
		if (!confirm('DELETE_EDUCATION_CONFIRM'.translate())) {
			return false;
		}
		var educationId = jQuery('#frm-main-education-id').val();
		if (parseInt(educationId) <=0 ) {
			return false;
		}
		var $this = jQuery(this);
		jQuery(this).attr('href','#');
		$this.mrAjax({
			bind: 'none',
			data: {
				handler:       'AJAX_JSON_UserProfileManager',
				__callHandler: 'deleteEducationData',
				recordId:      educationId
			},
			callback: onEducationDeleted
		});
	});

	Location.defaultAcParams.width = 291;
	jQuery(Location.init);
	if (jQuery('#frm-main-education-type').val() == 'UNIVERSITY') {
		jQuery(University.init);
	}

	/**
	 * Location link
	 */
	jQuery('#edLocationLink .moreLink').click(function(event) {
		jQuery('#edLocation').show();
		jQuery('#edLocationLink').hide();
	});

	/**
	 * Add comment click
	 */
	jQuery('#lnkAddCommEdu .moreLink').click(function(event) {
		jQuery('#AddCommEdu').show();
		jQuery('#lnkAddCommEdu').hide();
	});
};

/**
 * On education form created
 */
var onEducationFormCreated = function(req, stat, data) {
	onExperienceFormCancel();
	isHigh = jQuery('#frm-main-education-type').val() == 'UNIVERSITY';
	if (isHigh) {
		jQuery('#lnkShowAddEducationHighForm').hide();
		jQuery('#lnkShowAddEducationForm').show();
	}
	else {
		jQuery('#lnkShowAddEducationForm').hide();
		jQuery('#lnkShowAddEducationHighForm').show();
	}
	jQuery('#pnlUniversityAdd').show();
	jQuery('#pnlEducationIntro').hide();
	jQuery('#lnkEducationManagePanelCancel').attr('href','#').click(onEducationFromCancel);
	/**
	* hide containers with errors
	*/
	jQuery('#frm-main-education .warning').hide();
	initAJAXHandlersForLoadedEducationForm();
};

/**
 * validate education form
 */
var validateEducationForm = function() {
	isHigh = jQuery('#frm-main-education-type').val() == 'UNIVERSITY';
	jQuery('#frm-main-education .warning').hide();
	jQuery('#frm-main-education .warning').prev('p').show();

	isValid = true;

	if (!jQuery('#location_id').val() || !jQuery('#locationJQ').val()) {
		eduLocationError();
		isValid = false;
	}

	if (isHigh && !jQuery('#frm-main-education-degree').val()) {
		jQuery('#frm-main-education-degree-error').show();
		jQuery('#frm-main-education-degree-error').prev('p').hide();
		isValid = false;
	}

	isDateValid = true;
	if (parseInt(jQuery('#frm-main-education-attendFor').val()) <= 0) {
		jQuery('#frm-main-education-attend-error').show();
		jQuery('#frm-main-education-attend-error').prev('p').hide();
		isValid = isDateValid = false;
	}
	if (parseInt(jQuery('#frm-main-education-attendTo').val()) <= 0) {
		jQuery('#frm-main-education-attend-error').show();
		jQuery('#frm-main-education-attend-error').prev('p').hide();
		isValid = isDateValid = false;
	}
	if (isDateValid && parseInt(jQuery('#frm-main-education-attendFor').val()) > parseInt(jQuery('#frm-main-education-attendTo').val())) {
		jQuery('#frm-main-education-attend-error').show();
		jQuery('#frm-main-education-attend-error').prev('p').hide();
		isValid = false;
	}

	if (!isHigh && !jQuery('#frm-main-education-college').val()) {
		jQuery('#frm-main-education-college-error').show();
		jQuery('#frm-main-education-college-error').prev('div').prev('p').hide();
		isValid = false;
	}
	if (isHigh && !jQuery('#universityJQ').val()) {
		jQuery('#frm-main-education-university-error').show();
		jQuery('#frm-main-education-university-error').prev('p').hide();
		isValid = false;
	}

	return isValid;
};

jQuery(function(){
	/**
	 * Init edit handlers
	 */
	jQuery('.edit-education').click(educationEditLinkBind);

	/**
	 * Binding onclick event to the "Add Education" link
	 */
	jQuery('#lnkShowAddEducationForm').mrAjax({
		bind: 'click',
		data: {
			handler:       'AJAX_JSON_UserProfileManager',
			__callHandler: 'loadAddEducationForm',
			universityType:'COLLEGE'
		},
		callback: onEducationFormCreated
	});
	/**
	 * Binding onclick event to the "Add High Education" link
	 */
	jQuery('#lnkShowAddEducationHighForm').mrAjax({
		bind: 'click',
		data: {
			handler:       'AJAX_JSON_UserProfileManager',
			__callHandler: 'loadAddEducationForm',
			universityType:'UNIVERSITY'
		},
		callback: onEducationFormCreated
	});

	jQuery('#lnkEducationViewAccess').click(function(){		
		jQuery('#divEducationViewAccessLink').addClass('hidden'); 
		jQuery('#divEducationViewAccessForm').removeClass('hidden'); 
	});
});