/***************************************************************
*  Copyright notice
*
*  (c) 2008 Alexander Kellner <alexander.kellner@wunschtacho.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/


/**
 * Function pmcond_main() shows or hides elements if a value is equal to or is set etc...
 *
 *	// EXPLANATION
 *	// valueFromField: Do something if this value is set (e.g.: 'value')
 * 	// targetIDs: ID of target element (field or fieldset) (e.g.: 'uid42')
 *	// baseID: ID of own field element (e.g.: 'uid41')
 *	// targetFunction: What should happened (e.g.: 'hide' or 'show')
 *	// possibilities: hide, show
 *	// targetCondition: When should be done anything (e.g.: 'ifValue')
 *	// possibilities: ifValue, ifNotValue, ifSet, ifNotSet
 *
 */
// Function pmcond_main() shows or hides elements if a value is equal to or is set etc...
function pmcond_main(valueFromField, targetID, baseID, targetFunction, targetCondition) {
	// EXPLANATION
	// valueFromField: Do something if this value is set (e.g.: 'value')
	// targetIDs: ID of target element (field or fieldset) (e.g.: 'uid42')
	// baseID: ID of own field element (e.g.: 'uid41')
	// targetFunction: What should happened (e.g.: 'hide' or 'show')
		// possibilities: hide, show
	// targetCondition: When should be done anything (e.g.: 'ifValue')
		// possibilities: ifValue, ifNotValue, ifSet, ifNotSet


	// CONFIG
	var target = new Array(); // init array
	var base = new Array(); // init array
	var doit = new Array(); // init array
	var functionName = 'pmcond_main'; // name of current js function
	var array_ValueFromField = valueFromField.split(","); // explode value at ,
	var array_TargetID = targetID.split(","); // explode ids of target at ,
	var array_baseID = baseID.split(","); // explode ids of target at ,
	var array_targetFunction = targetFunction.split(","); // explode function name at ,
	var array_targetCondition = targetCondition.split(","); // explode conditions at ,

	// LET'S GO
	// check if length of all given variables are the same
	if (array_ValueFromField.length != array_TargetID.length || array_ValueFromField.length != array_baseID.length || array_ValueFromField.length != array_targetFunction.length || array_ValueFromField.length != array_targetCondition.length) { // If array length is not equal
		alert ('Error in Function ' + functionName + ': Length of array is not equal (' + array_ValueFromField.length + '/' + array_TargetID.length + '/' + array_baseID.length + '/' + array_targetFunction.length + '/' + array_targetCondition.length + ')!'); // errormessage
		return true; // stop function
	}


	// start loop
	for (i=0; i < array_ValueFromField.length; i++) { // one loop for every value
		var target = document.getElementById(array_TargetID[i]); // current target element
		var base = document.getElementById(array_baseID[i]); // current base element
		var current_target = array_TargetID[i]; // current targetid

		// check if target and base elements are existing
		if (!target || !base) {
			alert ('Error in Function ' + functionName + ': Target or base ID don\'t exists (' + getElementById(arrayTargetID[i]) + '/' + getElementById(baseID) + ')');
			return true;
		}

		// 1. get value of basefield
		ty = base.type.toLowerCase();
		switch(ty) {
			case 'text': // if current field is a text field
				var base_value = base.value;
				break;
			case 'checkbox': // if current field is a select field
				if (base.checked == true) { // if checkbox is checked
					var base_value = base.value; // value of checkbox
				} else {
					var base_value = ''; // clear value of checkbox if not checked
				}
				break;
			case 'radio': // if current field is a radio button
				if (base.checked == true) { // if radio button is checked
					var base_value = base.value; // value of radio button
				} else {
					var base_value = ''; // clear value of radio button if not checked
				}
				break;
			case 'select-one': // if current field is a select field
				var base_value = base.options[base.selectedIndex].value;
				break;
			default: // default
				alert ('Error in Function ' + functionName + ': base field (' + ty + ') is not allowed');
				break;
		}

		// 2. condition part: set doit to 1 or 0
		if (doit[current_target] != 1) { // search for something to do only if there is no calltoaction in one of the last loops
			switch(array_targetCondition[i]) {
				case 'ifValue': // if current value is an especially value
					//alert (current_target+'x');
					if (base_value == array_ValueFromField[i]) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifNotValue': // if current value is not an especially value
					if (base_value != array_ValueFromField[i]) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifSet': // if current value is set
					if (base_value != '') {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifNotSet': // if current value is not set
					if (base_value == '') {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				default: // default
					alert ('Error in Function ' + functionName + ': condition (' + array_targetCondition[i] + ') is not allowed');
					doit[current_target] = 0; // do nothing
			}
		}

		// 3. do something if doit == 1
		switch(array_targetFunction[i]) {
			case 'show': // show target
				if (doit[current_target]) { // of conditions is 1
					target.style.display="block"; // show element
				} else {
					target.style.display="none"; // hide element
				}
				break;
			case 'hide': // hide target
				if (doit[current_target]) { // of conditions is 1
					target.style.display="none"; // show element
				} else {
					target.style.display="block"; // hide element
				}
				break;
			default: // default
				alert ('Error in Function ' + functionName + ': function (' + array_targetFunction[i] + ') is not allowed');
		}

	}
	return true; // after loop

}


/**
 * Function pmcond_main() shows or hides elements if a value is equal to or is set etc...
 *
 *	// EXPLANATION
 *	// valueFromField: Do something if this value is set (e.g.: 'value')
 * 	// targetIDs: ID of target element (field or fieldset) (e.g.: 'uid42')
 *	// baseID: ID of own field element (e.g.: 'uid41')
 *	// targetFunction: What should happened (e.g.: 'hide' or 'show')
 *	// possibilities: hide, show
 *	// targetCondition: When should be done anything (e.g.: 'ifValue')
 *	// possibilities: ifValue, ifNotValue, ifSet, ifNotSet
 *
 */
// Function pmcond_main() shows or hides elements if a value is equal to or is set etc...
function pmcond_main_inner(valueFromField, targetID, baseID, targetFunction, targetCondition) {
	// EXPLANATION
	// valueFromField: Do something if this value is set (e.g.: 'value')
	// targetIDs: ID of target element (field or fieldset) (e.g.: 'uid42')
	// baseID: ID of own field element (e.g.: 'uid41')
	// targetFunction: What should happened (e.g.: 'hide' or 'show')
		// possibilities: hide, show
	// targetCondition: When should be done anything (e.g.: 'ifValue')
		// possibilities: ifValue, ifNotValue, ifSet, ifNotSet


	// CONFIG
	var target = new Array(); // init array
	var base = new Array(); // init array
	var doit = new Array(); // init array
	var functionName = 'pmcond_main_inner'; // name of current js function
	var array_ValueFromField = valueFromField.split(","); // explode value at ,
	var array_TargetID = targetID.split(","); // explode ids of target at ,
	var array_baseID = baseID.split(","); // explode ids of target at ,
	var array_targetFunction = targetFunction.split(","); // explode function name at ,
	var array_targetCondition = targetCondition.split(","); // explode conditions at ,

	// LET'S GO
	// check if length of all given variables are the same
	if (array_ValueFromField.length != array_TargetID.length || array_ValueFromField.length != array_baseID.length || array_ValueFromField.length != array_targetFunction.length || array_ValueFromField.length != array_targetCondition.length) { // If array length is not equal
		alert ('Error in Function ' + functionName + ': Length of array is not equal (' + array_ValueFromField.length + '/' + array_TargetID.length + '/' + array_baseID.length + '/' + array_targetFunction.length + '/' + array_targetCondition.length + ')!'); // errormessage
		return true; // stop function
	}


	// start loop
	for (i=0; i < array_ValueFromField.length; i++) { // one loop for every value
		var target = document.getElementById(array_TargetID[i]); // current target element
		var base = document.getElementById(array_baseID[i]); // current base element
		var current_target = array_TargetID[i]; // current targetid

		// check if target and base elements are existing
		if (!target || !base) {
			alert ('Error in Function ' + functionName + ': Target or base ID don\'t exists (' + array_TargetID[i] +':'+ document.getElementById(array_TargetID[i]) + '/' + array_baseID[i] +':'+ document.getElementById(array_baseID[i]) + ')');
			return true;
		}

		// get all input elements inside the element marked with the base id
		var inputs = base.getElementsByTagName('INPUT');

		// init flags whether values are found
		var valueFound = false;
		var anyValueFound = false;

		for (j=0; j < inputs.length; j++) {
			ty = inputs[j].type.toLowerCase();
			switch(ty) {
				case 'text': // if current field is a text field
					if(inputs[j].value == array_ValueFromField[i]){
						valueFound = true;
					}
					if(inputs[j].value != ''){
						anyValueFound = true;
					}
					break;

				case 'checkbox': // if current field is a select field
				case 'radio': // if current field is a radio button

					if(inputs[j].checked == true){
						anyValueFound = true;
						if(inputs[j].value == array_ValueFromField[i]) { // if checkbox is checked
							valueFound = true;
						}
					}
					break;
				default: // default
					alert ('Error in Function ' + functionName + ': base field (' + ty + ') is not allowed');
					break;
			}
		}

		// 2. condition part: set doit to 1 or 0
		if (doit[current_target] != 1) { // search for something to do only if there is no calltoaction in one of the last loops
			switch(array_targetCondition[i]) {
				case 'ifValue': // if current value is an especially value
					//alert (current_target+'x');
					if (valueFound == true) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifNotValue': // if current value is not an especially value
					if (valueFound == false) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifSet': // if current value is set
					if (anyValueFound == true) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				case 'ifNotSet': // if current value is not set
					if (anyValueFound == false) {
						doit[current_target] = 1; // do something
					} else {
						doit[current_target] = 0; // do nothing
					}
					break;
				default: // default
					alert ('Error in Function ' + functionName + ': condition (' + array_targetCondition[i] + ') is not allowed');
					doit[current_target] = 0; // do nothing
			}
		}

		// 3. do something if doit == 1
		switch(array_targetFunction[i]) {
			case 'show': // show target
				if (doit[current_target]) { // of conditions is 1
					target.style.display="block"; // show element
					/* Modified by MK */
          $$(".skintest_heading h2").invoke('setStyle', { height:'16px' });
          $$(".skintest_heading h2").invoke('setStyle', { width:'100%' });
          $$(".skintest_heading h2 embed").invoke('setStyle', { height:'16px' });
          $$(".skintest_heading h2 embed").invoke('setStyle', { width:'100%' });
					var headlines = target.getElementsByTagName('H2');
					for(h = 0; h < headlines.length; h++){
						headlines[h].style.height=16;
					}
					var embeds = target.getElementsByTagName('embed');
					for(e = 0; e < embeds.length; e++){

						embeds[e].setAttribute("width","653");
						embeds[e].setAttribute("height","16");
						embeds[e].style.height=16;
						embeds[e].style.width=653;
						var flashvars = embeds[e].getAttribute("flashvars");
						flashvars = flashvars.replace(/w=0/,'w=653');
						flashvars = flashvars.replace(/h=0/,'h=16');
						embeds[e].setAttribute("flashvars",flashvars);
						embeds[e].style.display='none';
						embeds[e].style.display='block';
					}
				} else {
					target.style.display="none"; // hide element
					var headlines = target.getElementsByTagName('H2');
					for(h = 0; h < headlines.length; h++){
						headlines[h].style.height=0;
					}
					var hideInputs = target.getElementsByTagName('INPUT');
					for(hIndex = 0; hIndex < hideInputs.length; hIndex++){
						hideInputs[hIndex].checked = false;
					}
				}
				break;
			case 'hide': // hide target
				if (doit[current_target]) { // of conditions is 1
					target.style.display="none"; // show element
				} else {
					target.style.display="block"; // hide element
				}
				break;
			default: // default
				alert ('Error in Function ' + functionName + ': function (' + targetFunction + ') is not allowed');
		}

	}
	return true; // after loop

}

/**
 * Function pmcond_main() shows or hides elements if a value is equal to or is set etc...
 *
 *	// EXPLANATION
 *	// valueFromField: Do something if this value is set (e.g.: 'value')
 * 	// targetIDs: ID of target element (field or fieldset) (e.g.: 'uid42')
 *	// baseID: ID of own field element (e.g.: 'uid41')
 *	// targetFunction: What should happened (e.g.: 'hide' or 'show')
 *	// possibilities: hide, show
 *	// targetCondition: When should be done anything (e.g.: 'ifValue')
 *	// possibilities: ifValue, ifNotValue, ifSet, ifNotSet
 *
 */
function pmcond_main_old(valueFromField, targetIDs, baseID, targetFunction, targetCondition) {

	// CONFIG
	var doit = new Array(); // init array
	var functionName = 'pmcond_main_inner'; // name of current js function
	var array_ValueFromField = valueFromField.split(","); // explode value at ,
	var targetId = (targetIDs.split(","))[0]; // explode ids of target at ,
	var target = document.getElementById(targetId); // current target element
	var targetFunction = (targetFunction.split(","))[0];
	var targetCondition = (targetCondition.split(","))[0];

	// get all input elements inside the element marked with the base id
	var inputs = document.getElementById(baseID).getElementsByTagName('INPUT');

	// init flags whether values are found
	var valueFound = false;
	var anyValueFound = false;

	// start loop
	for (i=0; i < array_ValueFromField.length; i++) { // one loop for every value

		for (j=0; j < inputs.length; j++) {
			ty = inputs[j].type.toLowerCase();
			switch(ty) {
				case 'text': // if current field is a text field
					if(inputs[j].value == array_ValueFromField[i]){
						valueFound = true;
					}
					if(inputs[j].value != ''){
						anyValueFound = true;
					}
					break;

				case 'checkbox': // if current field is a select field
				case 'radio': // if current field is a radio button

					if(inputs[j].checked == true){
						anyValueFound = true;
						if(inputs[j].value == array_ValueFromField[i]) { // if checkbox is checked
							valueFound = true;
						}
					}
					break;
				default: // default
					alert ('Error in Function ' + functionName + ': base field (' + ty + ') is not allowed');
					break;
			}
		}

		// 2. condition part: set doit to 1 or 0
		if (doit[targetId] != 1) { // search for something to do only if there is no calltoaction in one of the last loops
			switch(targetCondition) {
				case 'ifValue': // if current value is an especially value
					//alert (current_target+'x');
					if (valueFound == true) {
						doit[targetId] = 1; // do something
					} else {
						doit[targetId] = 0; // do nothing
					}
					break;
				case 'ifNotValue': // if current value is not an especially value
					if (valueFound == false) {
						doit[targetId] = 1; // do something
					} else {
						doit[targetId] = 0; // do nothing
					}
					break;
				case 'ifSet': // if current value is set
					if (anyValueFound == true) {
						doit[targetId] = 1; // do something
					} else {
						doit[targetId] = 0; // do nothing
					}
					break;
				case 'ifNotSet': // if current value is not set
					if (anyValueFound == false) {
						doit[targetId] = 1; // do something
					} else {
						doit[targetId] = 0; // do nothing
					}
					break;
				default: // default
					alert ('Error in Function ' + functionName + ': condition (' + targetCondition + ') is not allowed');
					doit[targetId] = 0; // do nothing
			}
		}

		// 3. do something if doit == 1
		switch(targetFunction) {
			case 'show': // show target
				if (doit[targetId]) { // of conditions is 1
					target.style.display="block"; // show element
					var headlines = target.getElementsByTagName('H2');
					for(h = 0; h < headlines.length; h++){
						headlines[h].style.height=16;
					}
					var embeds = target.getElementsByTagName('embed');
					for(e = 0; e < embeds.length; e++){

						embeds[e].setAttribute("width","653");
						embeds[e].setAttribute("height","16");
						embeds[e].style.height=16;
						embeds[e].style.width=653;
						var flashvars = embeds[e].getAttribute("flashvars");
						flashvars = flashvars.replace(/w=0/,'w=653');
						flashvars = flashvars.replace(/h=0/,'h=16');
						embeds[e].setAttribute("flashvars",flashvars);
						embeds[e].style.display='none';
						embeds[e].style.display='block';
					}
				} else {
					target.style.display="none"; // hide element
					var headlines = target.getElementsByTagName('H2');
					for(h = 0; h < headlines.length; h++){
						headlines[h].style.height=0;
					}
				}
				break;
			case 'hide': // hide target
				if (doit[targetId]) { // of conditions is 1
					target.style.display="none"; // show element
				} else {
					target.style.display="block"; // hide element
				}
				break;
			default: // default
				alert ('Error in Function ' + functionName + ': function (' + targetFunction + ') is not allowed');
		}

	}
	return true; // after loop

}