// JavaScript Document

/**
 * Bind the drop down for change event
 * Text box visible on option other
 */
function bindOtherOption() {

	/* Bind change event for other option for drop down list */
	$("select.other-option").change(function() {
//		alert(this);
		if(this[this.selectedIndex].text == 'Other') {
			$("#" + this.id + "_other").css("visibility", 'visible');
			$("#" + this.id + "_other").attr("class", 'required');
			$("#" + this.id + "_other").focus();
		} else {
			$("#" + this.id + "_other").css("visibility", 'hidden');
			$("#" + this.id + "_other").attr({"class": '', "value": ''});
			$("#" + this.id + "_other").valid();
		}
	});

	/**
	 * Bind click event for radio option 
	 * Show textbox for required option
	 */
	$("input.text-option").click(function() {
//		alert(this.value);
		if(this.value == $(this).attr('text_show_for')) {
			$("#" + $(this).attr('text_id')).css("visibility", 'visible');
			$("#" + $(this).attr('text_id')).attr("class", 'required');
			
			if($(this).attr('dropdown_id')) {
				$("#" + $(this).attr('dropdown_id')).css("visibility", 'visible');
			}
		} else {
			$("#" + $(this).attr('text_id')).css("visibility", 'hidden');
			$("#" + $(this).attr('text_id')).attr({"class": '', "value": ''});
			$("#" + $(this).attr('text_id')).valid();
			if($(this).attr('dropdown_id')) {
				$("#" + $(this).attr('dropdown_id')).css("visibility", 'hidden');
			}
		}
	});
}

(function($) {
	$.fn.extend({
		/**
		 * Bind click event for radio option with Yes/No
		 * Disable / Enable relative control for rel attribute
		 */
		YesNoDependency: function() {
			
			this.each(function() {
							   
				/* Bind change */
				$(this).bind("change", function() {
					//alert(this);
					if($(this).is(":checked")) {
						
						if($(this).attr('container')) {
							var els = $('#' + $(this).attr('rel')).find("input[type='text'],select,textarea");
							var buts = $('#' + $(this).attr('rel')).find("input[type='button']");
							var but = $('#' + $(this).attr('button'));
							var multi = true;
						}else {
							var els = $(this).attr('rel').split('|');
							var multi = false;
						}

						if(this.value == 'Yes') {
							
							for (var i=0; i < els.length; i++) {
								
								el = $(multi ? els[i] : "[name^='"+ els[i] + "']");
								
								el.removeAttr("disabled");
								
								/* Only make required, if set initially */
								if(el.is(".optional"))
									el.addClass("required").removeClass("optional");

								if(multi) {
									buts.removeAttr("disabled");
									but.removeAttr("disabled");
								}
							}
						} else {
							for (var i=0; i < els.length; i++) {
								
								el = $(multi ? els[i] : "[name^='"+ els[i] + "']");
								
								/* Disable field */
								el.attr("disabled", "disabled");
								
								/* Do some control wise cleanup */
								if(el.is(":checkbox"))
									el.attr("checked", false);
								else if(el.is(":radio")) {
									// Do nothing
								}else if(el.is(":select"))
									el.attr("selectedIndex", 0);
								else
									el.attr("value", "");
										
								/**
								 * Only remove required, if set initially 
								 * Add optional class so we get required back
								 */
								if(el.is(".required"))
									el.removeClass("required").addClass("optional");
								
								/* Give call to validation, so message get hidden */
								el.valid();
								
								if(multi) {
									buts.attr("disabled", "disabled");
									but.attr("disabled", "disabled");
								}
							}
						}
					}
				});

				/* Call change to initialize control base on selected option */
				$(this).change();
			});
		},
		
		/**
		 * Bind click event for radio option with Yes/No
		 * Disable / Enable relative control for rel attribute
		 */
		IsCheckedDependency: function() {
			
			this.each(function() {
							   
				/* Bind change */
				$(this).bind("change", function() {

					if($(this).attr('container')) {
						var els = $('#' + $(this).attr('rel')).find("input[type='text'],input[type='checkbox'],select,textarea");
						var buts = $('#' + $(this).attr('rel')).find("input[type='button']");
						var but = $('#' + $(this).attr('button'));
						var multi = true;
					}else {
						var els = $(this).attr('rel').split('|');
						var multi = false;
					}

					if(!$(this).is(":checked")) {
						
						for (var i=0; i < els.length; i++) {
							
							el = $(multi ? els[i] : "[name^='"+ els[i] + "']");
							
							el.removeAttr("disabled");
							
							/* Only make required, if set initially */
							if(el.is(".optional"))
								el.addClass("required").removeClass("optional");

							if(multi) {
								buts.removeAttr("disabled");
								but.removeAttr("disabled");
							}
						}
					} else {
						for (var i=0; i < els.length; i++) {
							
							el = $(multi ? els[i] : "[name^='"+ els[i] + "']");
							
							/* Disable field */
							el.attr("disabled", "disabled");
							
							/* Do some control wise cleanup */
							if(el.is(":checkbox"))
								el.attr("checked", false);
							else if(el.is(":radio")) {
								// Do nothing
							}else if(el.is(":select"))
								el.attr("selectedIndex", 0);
							else
								el.attr("value", "");
									
							/**
							 * Only remove required, if set initially 
							 * Add optional class so we get required back
							 */
							if(el.is(".required"))
								el.removeClass("required").addClass("optional");
							
							/* Give call to validation, so message get hidden */
							el.valid();
							
							if(multi) {
								buts.attr("disabled", "disabled");
								but.attr("disabled", "disabled");
							}
						}
					}
				});

				/* Call chnage to initialize control base on selected option */
				$(this).change();
				
			});
		},
		
		/**
		 * Bind click event for change event for select box
		 * Disable / Enable dependent control for specified list of values
		 */
		IsSelectedDependency: function() {
			
			this.each(function() {
							   
				/* Bind change */
				$(this).bind("change", function() {

					var values = $(this).attr('valuelist');
					var container = $(this).attr('container').split('|');
					var selectedValue = this[this.selectedIndex].text;
					var disabled = true;
					
					if(values.indexOf(selectedValue) !== -1)
						disabled = true;
					else
						disabled = false;
					
					/* Loop through each container and enable / disable control */
					for (var i=0; i < container.length; i++) {
						
						var els = $('#' + container[i]).find("input[type='text'],input[type='radio'],input[type='checkbox'],select,textarea");
						var buts = $('#' + container[i]).find("input[type='button']");
						
						/* Disabled all control */
						if(disabled) {
							
							els.attr("disabled", "disabled");
							buts.attr("disabled", "disabled");
							
							/* Do some control wise */
							els.each(function() {

								if($(this).is(":checkbox") || $(this).is(":radio")) {
									$(this).attr("checked", false);
								}else if($(this).is(":text") || $(this).is(":textarea")) {
									$(this).attr("value", "");
								}else if($(this).is(":select")) {
									$(this).attr("selectedIndex", 0);
								}else{
									$(this).attr("value", "");
								}
								
								/* Add optional class to required field, so it can reset to required if needed */
								if($(this).is(".required"))
									$(this).addClass("optional").removeClass("required");
							});

						/* Enabled all control */
						} else {
							
							els.removeAttr("disabled");
							buts.removeAttr("disabled");
							
							/* Do some control wise */
							els.each(function() {

								/* Only make required, if set initially */
								if($(this).is(".optional"))
									$(this).addClass("required").removeClass("optional");
							});
						}
					}
				});

				/* Call change to initialize control base on selected option */
				$(this).change();
			});
		}
				
	});
})(jQuery);

/**
 * Generate another set of input from last row of table
 * Each tr contain unique set of input,
 * so clone the last tr, blank out all input and
 * append as new set of input
 *
 * @param	elHolder	id of element holder
 */
function addDataRow(elHolder) {

	/* Clone row */
	var clonedData = $('#'+ elHolder + ' tbody tr:last').clone(true);
	
	var index = parseInt('0' + jQuery.data(elHolder, 'Index'));

	if(index == 0)
		index = $('#'+ elHolder + ' tbody').children('tr').size();
	else
		index++;

	/* Change array index */
	clonedData.html(clonedData.html().replace(/\[\d+\]/g, '['+ index +']'));
	/* Should not done */
//	clonedData.html(clonedData.html().replace(/_\d+_/g, '_'+ index +'_'));
	
	/* Added on Monday, 14 December 2009 */
	clonedData.html(clonedData.html().replace(/_\d+_/g, '_'+ index +'_'));

	/**
	 * Clean all inputs values 
	 * Make sure autocomplete attribute cleared, otherwise binding will not take place
	 */
	clonedData.find("input[type='text'],select,textarea").attr({"value": "", "autocomplete": ""});
	clonedData.find("input[type='hidden']").attr("value", "");
	clonedData.find("input[type='checkbox']").attr({"selected": false, "checked": false});
	
	/* Append it */
	$('#'+ elHolder + ' tbody').append(clonedData);

	/* Call AutoComplete to bind auto complete fields */
	bindAutoComplete();

	/* Save element index */
	jQuery.data(elHolder, 'Index', index);
}

/**
 * Remove row from table body
 *
 * @param	elHolder	id of element holder
 */
function removeDataRow(el, opt) {

	/* Get row count */
	var cnt = $(el).parent().parent().parent().children('tr').size();

	/* Row with title*/
	cnt--;
	
	/* If greter than minium size, then remove it */
	if(cnt > opt.Mimimum)
		$(el).parent().parent().remove();
	/* Show supplied message */
	else if (opt.Message)
		alert(opt.Message);
}

/**
 * Generate another set of input from last fieldset of container
 * Each fieldset contain unique set of input,
 * so clone the last fieldset, blank out all input and
 * append as new set of input
 *
 * @param	elHolder	id of fieldset holder
 */
function addDataFieldset(elHolder) {

	/* Clone row */
	var clonedData = $('#'+ elHolder + ' fieldset:last').clone(true);

	var index = parseInt('0' + jQuery.data(elHolder, 'Index'));

	if(index == 0)
		index = $('#'+ elHolder).children('fieldset').size();
	else
		index++;

	/* Change array index */
	clonedData.html(clonedData.html().replace(/\[\d+\]/g, '['+ index +']'));
	clonedData.html(clonedData.html().replace(/_\d+_/g, '_'+ index +'_'));

	/**
	 * Clean all inputs values 
	 * Make sure autocomplete attribute cleared, otherwise binding will not take place
	 */
	clonedData.find("input[type='text'],select,textarea").attr({"value": "", "autocomplete": ""}).removeClass("hasDatepicker");
	clonedData.find("input[type='hidden']").attr("value", "");
	clonedData.find("input[type='checkbox']").attr({"selected": false, "checked": false});
//	clonedData.find("input[type='radio']").attr("checked", "");
	
	/* Let's do some animation, so first hide it */
	clonedData.hide();
	
	/* Append it */
	$('#'+ elHolder).append(clonedData);
	
	/* Show new data form with animation */
	clonedData.slideDown("slow");

	/* Call AutoComplete to bind auto complete fields */
	bindAutoComplete();
	
	/* Bind change event for other option */
	bindOtherOption();
	
	/* Save element index */
	jQuery.data(elHolder, 'Index', index);
}

/**
 * Remove data form with fieldset
 *
 * @param	elHolder	id of element holder
 */
function removeDataFieldset(el, opt) {
	/* Get row count */
	var parentEle = $(el).parent().parent();
	var cnt = $(el).parent().parent().parent().children('fieldset').size();

//	alert(parentEle.get(0).tagName);
//	alert(parentEle.get(0).id);
//alert(opt.Message);
	/* If greter than minium size, then remove it */
	if(cnt > opt.Mimimum) {
		/* First hide through animation and then remove it */
//		alert($(el).parent().parent().parent().parent().parent().get(0));
//		var fld = $(el).parent().parent().parent().parent().parent();
//		alert(fld);
//		parentEle.slideIn("slow");
//		$(el).parent().parent().parent().parent().slideDown("slow", function() {$(el).parent().parent().parent().parent().parent().remove()});
		parentEle.remove();
	/* Show supplied message */
	} else if (opt.Message) {
		alert(opt.Message);
	}
}

/**
 * Generate another set of input from last table of container
 * Each table contain unique set of input,
 * so clone the last table, blank out all input and
 * append as new set of input
 *
 * @param	elHolder	id of table holder
 */
function addDataTable(elHolder) {

	/* Clone table */
	var clonedData = $('#'+ elHolder + '>div:last').clone(true);

	var index = parseInt('0' + jQuery.data(elHolder, 'Index'));

	if(index == 0)
		index = $('#'+ elHolder).children('div').size();
	else
		index++;

	/* Change array index */
	clonedData.html(clonedData.html().replace(/\[\d+\]/g, '['+ index +']'));
	clonedData.html(clonedData.html().replace(/_\d+_/g, '_'+ index +'_'));

	/**
	 * Clean all inputs values 
	 * Make sure autocomplete attribute cleared, otherwise binding will not take place
	 */
	clonedData.find("input[type='text'],select,textarea").attr({"value": "", "autocomplete": ""}).removeClass("hasDatepicker");
	clonedData.find("input[type='hidden']").attr("value", "");
	clonedData.find("input[type='checkbox']").attr({"selected": false, "checked": false});

//	clonedData.find("input[type='radio']").attr("checked", "");
	
	/* Let's do some animation, so first hide it */
	clonedData.hide();
//	clonedData.slideUp("slow");
	
	/* Append it */
	$('#'+ elHolder).append(clonedData);
	
	/* Show new data form with animation */
	clonedData.slideDown("slow");
//	clonedData.show();

	/* Call AutoComplete to bind auto complete fields */
	bindAutoComplete();

	/* Bind change event for other option */
	bindOtherOption();

	/* Save element index */
	jQuery.data(elHolder, 'Index', index);
}

/*
 * Remove data form with table
 *
 * @param	elHolder	id of element holder
 */
function removeDataTable(el, opt) {
	/* Get row count */
	var parentEle = $(el).parent().parent();	
	var cnt = $(el).parent().parent().parent().children('div').size();

//	alert(parentEle.get(0).tagName);
//	alert(parentEle.get(0).id);
//alert(opt.Message);
	/* If greter than minium size, then remove it */
	if(cnt > opt.Mimimum) {
		/* First hide through animation and then remove it */
//		alert($(el).parent().parent().parent().parent().parent().get(0));
//		var fld = $(el).parent().parent().parent().parent().parent();
//		alert(fld);
//		parentEle.slideIn("slow");
//		$(el).parent().parent().parent().parent().slideDown("slow", function() {$(el).parent().parent().parent().parent().parent().remove()});
		parentEle.remove();
	/* Show supplied message */
	} else if (opt.Message) {
		alert(opt.Message);
	}
}
