/*************************************************************************
	Desc  : Dependent drop down menu, v.0.2
	Author: cengmezo (Mezahir Efendiyev)
	E-Mail: cengmezo@yahoo.com

**************************************************************************/


var dependentDropDownDependencies		= new Array();
var dependentDropDownArrayForms			= new Array();
var dependentDropDownContent			= new Array();
var dependentDropDownPleaseSelect		= new Array();
var dependentDropDownPleaseSelectLabel	= new Array();

function j_dependentDropDownOnChange ( parentTag , currTagObject , isLaunch ) {

	if ( typeof(currTagObject) == 'undefined' ) {
		currTagObject = false;
	}

	if ( typeof(isLaunch) == 'undefined' ) {
		isLaunch = false;
	}

	if ( dependentDropDownArrayForms[parentTag] ) {
		j_dependentDropDownOnChange_withArray ( parentTag , currTagObject , isLaunch );
		return true;
	}


	if ( dependentDropDownDependencies[parentTag] && dependentDropDownDependencies[parentTag].length ) {

		var i,d,o,p,l,j,k;

		for ( i=0; i < dependentDropDownDependencies[parentTag].length; i ++ ) {

			d = dependentDropDownDependencies[parentTag][i];
			o = j_common_getElement (d);

			o.length = 0;
			k = 0;

			if ( dependentDropDownPleaseSelect[d] !== false ) {
				o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownPleaseSelectLabel[d]),j_dependentDropDown_encodeChar(dependentDropDownPleaseSelect[d]));
				k++;
			}

			if ( dependentDropDownContent[parentTag] && dependentDropDownContent[parentTag].length ) {

				p = parseInt ( j_common_getElement(parentTag).value );
				l = dependentDropDownContent[parentTag].length;
				for ( j=0; j < l ; j ++ ) {
					if ( ( dependentDropDownContent[parentTag][j][0] == d ) && ( dependentDropDownContent[parentTag][j][1] == p ) ) {
						o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][3]),j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][2]));
						k++;
					}
				}
			}

			if ( ! k ) {
				o.options[0] = new Option('---' , '' );
			}

			j_dependentDropDownOnChange ( d );
		}
	}
}


function j_dependentDropDownOnChange_withArray ( parentTag , currTagObject , isLaunch ) {

	if ( dependentDropDownDependencies[parentTag] && dependentDropDownDependencies[parentTag].length ) {

		var formObject = document.forms[dependentDropDownArrayForms[parentTag]];

		var elms;
		var elms_count;

		var currSelectedTagOrder = -1;

		var f;

		elms = formObject.elements[parentTag + '[]'];

		if ( ! elms ) {
			return false;
		}

		elms_count  = (typeof(elms.length) != 'undefined') ? elms.length : 0;

		for ( f=0; f<elms_count; f++ ) {
			if ( currTagObject ==  elms[f] ) {
				currSelectedTagOrder = f;
				break;
			}
		}

		if ( currSelectedTagOrder < 0 ) {

			if ( currTagObject = j_common_getElement(parentTag + '[]') ) {

				//-------------------------------------------------------

				var i,d,o,p,l,j,k,s;

				for ( i=0; i < dependentDropDownDependencies[parentTag].length; i ++ ) {

					d = dependentDropDownDependencies[parentTag][i];

					o = j_common_getElement(d + '[]');

					if ( ! o ) {
						return false;
					}

					s = 0;

					if ( isLaunch && o.selectedIndex ) {
						s = o.selectedIndex;
					}

					o.length = 0;
					k = 0;

					if ( dependentDropDownPleaseSelect[d] !== false ) {
						o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownPleaseSelectLabel[d]),j_dependentDropDown_encodeChar(dependentDropDownPleaseSelect[d]));
						k++;
					}

					if ( dependentDropDownContent[parentTag] && dependentDropDownContent[parentTag].length ) {

						p = parseInt ( currTagObject.value );
						l = dependentDropDownContent[parentTag].length;
						for ( j=0; j < l ; j ++ ) {
							if ( ( dependentDropDownContent[parentTag][j][0] == d ) && ( dependentDropDownContent[parentTag][j][1] == p ) ) {
								o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][3]),j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][2]));
								k++;
							}
						}
					}

					if ( ! k ) {
						o.options[0] = new Option('---' , '' );
					}

					if ( o.length > s ) {
						o.selectedIndex = s;
					}

					//j_dependentDropDownOnChange ( d );
				}
				//-------------------------------------------------------

			}

		} else {

			//-------------------------------------------------------

			var i,d,o,p,l,j,k;

			for ( i=0; i < dependentDropDownDependencies[parentTag].length; i ++ ) {

				d = dependentDropDownDependencies[parentTag][i];


				elms = formObject.elements[d + '[]'];
				elms_count  = (typeof(elms.length) != 'undefined') ? elms.length : 0;

				if ( ! elms_count ) {
					continue;
				}

				if (typeof(elms[currSelectedTagOrder]) == 'undefined') {
					continue;
				}

				o = elms[currSelectedTagOrder];
				o.length = 0;
				k = 0;

				if ( dependentDropDownPleaseSelect[d] !== false ) {
					o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownPleaseSelectLabel[d]),j_dependentDropDown_encodeChar(dependentDropDownPleaseSelect[d]));
					k++;
				}

				if ( dependentDropDownContent[parentTag] && dependentDropDownContent[parentTag].length ) {

					p = parseInt ( currTagObject.value );
					l = dependentDropDownContent[parentTag].length;
					for ( j=0; j < l ; j ++ ) {
						if ( ( dependentDropDownContent[parentTag][j][0] == d ) && ( dependentDropDownContent[parentTag][j][1] == p ) ) {
							o.options[k] = new Option(j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][3]),j_dependentDropDown_encodeChar(dependentDropDownContent[parentTag][j][2]));
							k++;
						}
					}
				}

				if ( ! k ) {
					o.options[0] = new Option('---' , '' );
				}

				//j_dependentDropDownOnChange ( d );
			}
			//-------------------------------------------------------
		}
	}
}


function j_dependentDropDown_encodeChar (s) {
	obj = j_common_getElement('tagID_class_validation_encoding');
	if (obj) {
		obj.innerHTML = s;
		s = obj.innerHTML;
	}
	return s;
}
