/**
 *  Gets the id of a panel and toggles its display state
 *
 */
function togglepanel(panelid)
{
	var panel = document.getElementById(panelid);
	if(panel.style.display == 'none')
	{
		panel.style.display = 'inline';
	} else {
		panel.style.display = 'none';
	}
}

/**
 *  Gets an object and 2 texts. If the object innerHTML is equals
 * the 2nd, prints the 1st. Else, prints the 2nd.
 *
 *  It is used to toggle texts from boxes
 *
 */
function toggletext(span, text1, text2)
{

	if(span.innerHTML == text2)
	{
		span.innerHTML = text1;
	} else {
		span.innerHTML = text2;
	}
}

/**
 *  Gets an object and 2 texts. If the object innerHTML is equals
 * the 2nd, prints the 1st. Else, prints the 2nd.
 *
 *  It is used to toggle texts from boxes
 *
 */
function togglereplace(span, text1, text2)
{

	if(span.innerHTML.indexOf(text2) > 0)
	{
		span.innerHTML = span.innerHTML.replace(text2, text1);
	} else {
		span.innerHTML = span.innerHTML.replace(text1, text2);
	}
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 15.12.2005
 * Fetches all form elements
 * Array:
 * 1: order_id
 * 2: product_id
 * 3: resize_flag
 * 4: surface_flag
 * 5: (undefined - this should be fixed to be quantity, then change 'x+6' to 'x+5' in /include/xajax/functions/update_cart.php)
 * 6: quantity
**/
function getFormElements(form)
{
  	var values = new Array();
  	var i=0;
	for(var x=1;x<document.change_order.elements.length;x++)
  	{
		if(document.change_order.elements[x].type=="radio" && !document.change_order.elements[x].checked)
		{
			//do nothing
		}
		else
		{
	  		if((x+4)%5==0)
	  		{
	  			values[i] = document.change_order.elements[x].name;
	  			i++;
	  		}
	  		values[i] = document.change_order.elements[x].value;
		}
		i++;
  	}
  	return values;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 22.12.2005
 * This function toggles the displayed options container in bildProfi
 * option: container to be displayed
**/
function toggle_view(option)
{	
	var options = new Array("order","borders","effects","optimization");
	var active = new String();
	var inactive = new String();
	var temp = new String();
	var i=0;
	
	active = active.concat(option,"_nav");
	temp = document.getElementById(option); 
	temp.style.display ='inline';
	temp = document.getElementById(active);
	temp.style.color = menuActive;
	temp.style.textDecoration = "underline";
	for(i=0;i<4;i++)
	{	
		if(option!=options[i])
		{
			inactive = '';
			inactive = inactive.concat(options[i],"_nav");
			temp = document.getElementById(options[i]);
			temp.style.display = 'none';
			temp = document.getElementById(inactive);
			temp.style.color = menuInactive;
			temp.style.textDecoration = "none";
		}
	}
	document.getElementById('navigation').value = option;
}


/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 03.01.2006
 * This function toggles the displayed effect picture in bildProfi
 * option: effect of picture to be displayed
**/
function toggle_effect_preview(option)
{
	var radio = new String();
	var preview = document.getElementById(option);
	radio = radio.concat(option,"_radio");
	var hl = document.getElementById(radio);
	if (preview.style.display == 'none')
	{
		hl.style.textDecoration = 'underline';
		hl.style.cursor = 'default';
		show_active('off','coloreffect');
		preview.style.display = 'inline';
	}
	else
	{
		preview.style.display = 'none';
		show_active('on','coloreffect');
		hl.style.textDecoration = 'none';
		hl.style.cursor = 'default';
	}
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 03.01.2006
 * This function toggles the displayed border picture in bildProfi
 * option: border of picture to be displayed
**/
function toggle_border_preview(option)
{
	var radio = new String();
	var preview = document.getElementById(option);
	radio = radio.concat(option,"_radio");
	var hl = document.getElementById(radio);
	if (preview.style.display == 'none')
	{
		hl.style.textDecoration = 'underline';
		hl.style.cursor = 'default';
		show_active('off','rand');
		preview.style.display = 'inline';
	}
	else
	{
		preview.style.display = 'none';
		show_active('on','rand');
		hl.style.textDecoration = 'none';
		hl.style.cursor = 'default';
	}
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 03.01.2006
 * This function toggles the display mode of the actual choice preview picture
 * option: 'on' or 'off'
 * type: 'rand' or 'coloreffect'
**/
function show_active(option,type)
{
	var active = new String();
	var radio_options = new Array;
	radio_options = document.getElementsByName(type);
	for(i=0;i<radio_options.length; i++)
	{
		if(radio_options[i].checked)
		{
			active = document.getElementById(radio_options[i].value);
		}
	}
	if(active=='')
	{
		if(type=='coloreffect')
		{
			active = document.getElementById('original');
		}
		else
		{
			active = document.getElementById('0');
		}
	}
	if(option=='on')
	{
		active.style.display= 'inline';
	}
	else
	{
		active.style.display= 'none';
	}
	
	return false;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 05.01.2006
 * This function checks if a given radio is checked
 * type: 'rand' or 'coloreffect'
 * value: value of the radio which shall be checked
**/
function check_radio_on(type,value)
{
	var active = new String();
	var radio_options = new Array;
	var status = 'false';
	radio_options = document.getElementsByName(type);
	for(i=0;i<radio_options.length; i++)
	{
		if((radio_options[i].checked) && (radio_options[i].value == value))
		{
			status = 'true';
		}
	}
	return status;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 03.01.2006
 * This function sets the border/effect preview on select of an option
**/
function select_radio(type)
{
	var i;
	var radio_options = new Array;
	radio_options = document.getElementsByName(type);
	for(i=0; i<radio_options.length; i++)
	{
		document.getElementById(radio_options[i].value).style.display = 'none';
	}
	show_active('on', type);
	//document.getElementById('uncommited_changes').style.display = 'inline';
	return true;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 03.01.2006
 * This function sets the navigation chapter color back if the chapter is not displayed
 * chapter: object id
**/
function recolor(chapter)
{
	var active = new String();
	active = active.concat(document.getElementById('navigation').value,"_nav");
	if(chapter != active)
	{
		document.getElementById(chapter).style.color = menuInactive;
	}
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 11.01.2006
 * This function changes the preview pictures urls in multi mode to force reload
**/
function reload()
{
	var i=0;
	document.getElementById('preview_isnot_uptodate').style.display = 'none';
	document.getElementById('progress').style.display = 'inline';
	for(i=0; i < picture_ids.length; i++)
	{
		document.getElementById("preview_" + picture_ids[i]).src = "/preview/preview.php?type=album&picid=" + picture_ids[i] + "&width=40&height=40&cop=" + Math.random();
	}
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 11.01.2006
 * This function determines the actually selected product option value and saves it to selected_product
**/
function set_selected_product()
{
	selected_product = document.getElementById('product_id').options[document.getElementById('product_id').selectedIndex].value;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 11.01.2006
 * This function assembles an array of default product id and actually chosen product id
 * used with option 'rand' in bP_picture_update to set back the selected product if not allowed anymore
**/
function get_pid_array()
{
	var pid_array = new Array();
	pid_array[0] = default_product;
	pid_array[1] = selected_product;
	return pid_array;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 11.01.2006
 * This sets own icc selected when pulldown changes and then calls the xajax function
**/
function icc_select(sid,mode,value)
{
	if(value!='auto' && value!='off')
	{
		document.getElementById('icc_own').checked='checked';
	}
	
	xajax_bP_picture_update(sid,mode,picture_ids.toString(),'profil',value);
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 06.02.2006
 * This function assembles an array of order relevant values
 * used with option 'order' in bP_picture_update
**/
function get_buy_values()
{
	var buy = new Array();
	var radio_options = new Array();
	var i = 0;
	var active;
	radio_options = document.getElementsByName('resizing');
	// get quantity
	buy[0] = document.getElementById('quantity').value;
	// get surface
	if (check_radio_on('surface',1)=='true')
	{
		buy[1] = 1;
	}
	else
	{
		buy[1] = 2;
	}
	// get product_id
    buy[2] = selected_product;
	for(i=0;i<radio_options.length; i++)
	{
		if(radio_options[i].checked)
		{
			buy[3] = radio_options[i].value;
		}
	}
	if(buy[3]==null)
	{
		buy[3]='auto';
	}
	buy[4] = album_id;
	return buy;
}

/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 25.01.2006
 * This function checks the current preview_isnot_uptodate text and toggles it
**/
function toggle_uptodate_text()
{
	if(document.getElementById('preview_isnot_uptodate').style.display=='none')
	{
		if(is_original=='no')
		{
			document.getElementById('preview_isnot_uptodate').style.display='inline';
		}
	}
	else
	{
		if(is_original=='yes')
		{
			document.getElementById('preview_isnot_uptodate').style.display='none';
		}
	}
	is_original='yes';
}



/**
 * Author: Moritz Nöck Lungershausen
 * Last Changes: 22.12.2005
 * This function toggles the displayed options container in bildProfi
 * option: container to be displayed
**/
function ol_gv_toggle_view(option)
{	
	var options = new Array("borders","effects","cropping");
	var active = new String();
	var inactive = new String();
	var temp = new String();
	var i=0;
	
	active = active.concat(option,"_nav");
	temp = document.getElementById(option); 
	temp.style.display ='inline';
	temp = document.getElementById(active);
	//temp.style.color = "#CF6F09";
		temp.style.textDecoration = "underline";
		for(i=0;i<4;i++)
		{	
			if(option!=options[i])
			{
				inactive = '';
				inactive = inactive.concat(options[i],"_nav");
				temp = document.getElementById(options[i]);
				// check if we found an item before applying style
				if(temp != null)
				{
					temp.style.display = 'none';
					temp = document.getElementById(inactive);
					//temp.style.color = "#375FA4";
					temp.style.textDecoration = "none";
				}
			}
		}
	document.getElementById('navigation').value = option;
	switch(option) {
		case 'cropping':
			// activeate cropControll
			switchCropControll('active');
			break;
		default:
			// reload galleryPicture with cropping applied
			switchCropControll('inactive');
			reloadGalleryPicture(false);
			break;
	} 
}

/** online labor: hide noxajax tip **/
function hide_noxajax()
{
	document.getElementById('noxajax').style.display = 'none';
	document.cookie = "hide_noxajax=1; path=/";
}

/**  check if noxajax tip shall be displayed **/
function show_noxajax()
{
	if(document.cookie.indexOf("hide_noxajax=1") > -1)
	{
		document.getElementById('noxajax').style.display = 'none';
	}
}

/**
 * This function assembles an array of order relevant values
 * used with option 'order' in ol_gv_picture_update
**/
function ol_gv_get_buy_values()
{
	var buy = new Array();
	var radio_options = new Array();
	var i = 0;
	var active;
	buy[0] = document.getElementById('qty').value;
	// get surface
	if (check_radio_on('surface',1)=='true')
	{
		buy[1] = 1;
	}
	else
	{
		buy[1] = 2;
	}
	// get product_id
	buy[2] = selected_product;
	buy[3]='auto';
	buy[4] = album_id;
	return buy;
}

function ol_gv_buy()
{
	document.getElementById('result_text').innerHTML = "Das Bild wird dem Warenkorb hinzugef&uuml;gt.<br />"
	document.getElementById('result_text').style.display = 'inline';
	xajax_ol_gv_picture_update(picture_ids.toString(),'buy',ol_gv_get_buy_values());
}
