﻿function init_CustomDropDown(sUniqueID) {
	var oDropContainer = jQuery(sUniqueID);

	//Local Objects
	var oDisplayField = oDropContainer.find('.display');
	var oList = oDropContainer.find('ul');
	var oHidden = oDropContainer.find('input');
	var oSlide = oDropContainer.find('.sliding').css({ display: 'block' }); ;
	var sHeight = getNegativeMargin(oList.height()) + 'px';
	oSlide.css({ display: 'none' });

	//Init
	oList.css({ display: 'block', 'margin-top': sHeight });

	//Expanding Button
	oDropContainer.find('.expand').click(function (event) {
		event.stopPropagation();
		if (oList.hasClass('expanding'))
			hideThisDropDown();
		else
			expThisDropDown();
	});

	//Clicking outside
	jQuery(document).click(function () {
		hideThisDropDown();
	});

	//Selecting
	jQuery.each(oList.find('li'), function (indexItem, valueItem) {
		//Hovering
		jQuery(this).hover(function () {
			jQuery(this).addClass('hover');
		}, function () {
			jQuery(this).removeClass('hover');
		});

		//Clicking
		jQuery(this).click(function () {
			//Set Val
			oHidden.val(jQuery(this).find('span').text());

			//Display
			clearThisDropDownSelection();
			oDisplayField.html(jQuery(this).html());
			jQuery(this).addClass('on');

			hideThisDropDown();
		});
	});

	//Hiding/Expanding Functions
	function expThisDropDown() {
		oList.removeClass('hiding');
		oList.addClass('expanding');
		oSlide.css({ display: 'block' });
		oList.stop().animate({ 'margin-top': '0px' }, { duration: 500 });
	}
	function hideThisDropDown() {
		oList.removeClass('expanding');
		oList.addClass('hiding');
		oList.stop().animate({ 'margin-top': sHeight }, { duration: 500, complete: function () { oSlide.css({ display: 'none' }); } });
	}

	//Clearing
	function clearThisDropDownSelection() {
		oList.find('li').removeClass('on');
	}
}
function customDropAddItem(sObject, sText, sValue) {
	var oListItem = jQuery(document.createElement('li'));
	oListItem.html(sText + '<span>' + sValue + '</span>');

	jQuery(sObject).append(oListItem);
}

function init_FormManagement() {
	//Remove Enter keys on all
	jQuery('input').keypress(function (e) {
		var code = null;
		code = (e.keyCode ? e.keyCode : e.which);
		return (code == 13) ? false : true;
	});

	//add enter keys for forms that can be defined
	jQuery.each(jQuery('fieldset'), function () {
		var oFieldset = jQuery(this);

		//Locate submit button
		var oSubmitButton = oFieldset.find('input.submit_button,img.submit_button');
		if (oSubmitButton && oSubmitButton != null && oSubmitButton.length) {
			//Each field
			jQuery.each(oFieldset.find('input'), function () {
				jQuery(this).keypress(function (e) {
					var code = null;
					code = (e.keyCode ? e.keyCode : e.which);
					if (code == 13)
						oSubmitButton.trigger('click');
					//return (code == 13) ? false : true;
				});
			});
		}
	});

	//Overlays
	jQuery('fieldset input.submit_button,img.submit_button').click(function () {
		//trigger_Processing_Overlay();
	});
}

//Text replace
function setTextBoxToggle(sBoxId, sDefaultValue, onBlurValidate) {
	jQuery('#' + sBoxId).focus(function () {
		if (sDefaultValue && sDefaultValue.length > 0)
			if (jQuery(this).val() == sDefaultValue) { jQuery(this).val(''); }
//		var oError = jQuery(this).next();
//		if (oError)
//			oError.css({ display: 'none' });
	}).blur(function () {
		if (jQuery(this).val() == '') { jQuery(this).val(sDefaultValue); }
		else {
			if (onBlurValidate)
				onBlurValidate();
		}
	});
}
function setDefaultTextBoxToggle(oBoxObject, onBlurValidate) {
	var sDefaultValue = oBoxObject.val();
	oBoxObject.focus(function () {
		if (sDefaultValue && sDefaultValue.length > 0)
			if (jQuery(this).val() == sDefaultValue) { jQuery(this).val(''); }
		var oError = jQuery(this).next();
		if (oError)
			oError.css({ display: 'none' });
	}).blur(function () {
		if (jQuery(this).val() == '') { jQuery(this).val(sDefaultValue); }
		else {
			if (onBlurValidate)
				onBlurValidate();
		}
	});
}


//Validation
function validateField(oField, sDefault, onSuccessNextValidate) {
	var sVal = oField.val();
	var isValid = (jQuery.trim(sVal).length > 0 && sDefault != sVal);

	if (!isValid) {
		oField.next().html('required').css({ display: 'block' });
	}
	else {
		if (onSuccessNextValidate) {
			isValid = onSuccessNextValidate();
		}
		else {
			oField.next().css({ display: 'none' });
		}
	}

	return isValid;
}
function validateIsEmail(oField) {
	var isValid = jQuery.trim(oField.val()).match(/^[a-zA-Z0-9].+\@.+\.[a-zA-Z0-9]{2,3}$/);
	
	if (!isValid) {
		oField.next().html('invalid').css({ display: 'block' });
		isValid = false;	
	}
	else {
		oField.next().css({ display: 'none' });
		isValid = true;
	}
	return isValid;
}
