function switchTab(strSwitchTo) {
	
	switch (strSwitchTo) {
	
		case 'content':
		
			$('switchCategories').className = 'inactive';
			$('switchContent').className = '';
			
			$('tabCategories').style.display = 'none';		
			$('tabContent').style.display = 'block';
			
			break;
			
		case 'categories':
		
			$('switchContent').className = 'inactive';
			$('switchCategories').className = '';
			
			$('tabContent').style.display = 'none';		
			$('tabCategories').style.display = 'block';
			
			break;				
	}
}

function toggleSpecs() {
  if ($('specificity').options[$('specificity').selectedIndex].value == 'none') {
    $('specificities').disabled = 'disabled';
  } else {
    $('specificities').disabled = false;
  }
}

function insertCategory() {
	
	intProductId = $F('product');
	
	strTitle = prompt('Neue Kategorie', '');
	
	if (strTitle) {
		new Ajax.Request('/verwaltung/shop_inhalt.php?funct=insertCategory', {
			method: 'post',
			postBody: 'product=' + intProductId + '&title=' + strTitle,
			onComplete: onInsertCategoryComplete
		});	
	}	
}

function updateCategory(intCategoryId, strTitle) {
	
	intProductId = $F('product');
	
	strTitle = prompt('Kategorie bearbeiten', strTitle);
	
	if (strTitle) {
		new Ajax.Request('/verwaltung/shop_inhalt.php?funct=updateCategory', {
			method: 'post',
			postBody: 'product=' + intProductId + '&category=' + intCategoryId + '&title=' + strTitle,
			onComplete: onInsertCategoryComplete
		});	
	}	
}

function deleteCategory(intCategoryId) {
	
	intProductId = $F('product');
	
	new Ajax.Request('/verwaltung/shop_inhalt.php?funct=deleteCategory', {
		method: 'post',
		postBody: 'product=' + intProductId + '&category=' + intCategoryId,
		onComplete: onInsertCategoryComplete
	});	
}

function onInsertCategoryComplete(objReq) {
	
	arrResponse = objReq.responseText.evalJSON();
	
	if (arrResponse['error'].length > 0) {
	
		for (i = 0; i < arrResponse['error'].length; i++) {
			alert(arrResponse['error'][i]);
		}
	} else {		
		strHTML = '';
		for (i = 0; i < arrResponse['success']['categories'].length; i++) {
			strHTML += '<p class="event"><span class="edit"><a href="#" onclick="updateCategory(' + arrResponse['success']['categories'][i]['id'] + ', \'' + arrResponse['success']['categories'][i]['title'] + '\');">editieren</a> | <a href="#" onclick="deleteCategory(' + arrResponse['success']['categories'][i]['id'] + ');">löschen</a></span><input name="categories[]" type="checkbox" value="' + arrResponse['success']['categories'][i]['id'] + '" '; 
			if (arrResponse['success']['categories'][i]['checked'] == true) {
				strHTML += 'checked="checked" ';			
			}
			strHTML += 'style="margin-right: 15px; vertical-align: middle;" /><strong>' + arrResponse['success']['categories'][i]['title'] + '</strong></p>';
		}
		$('listCategories').innerHTML = strHTML;
	}	 	
}

function submitProduct() {
	
	new Ajax.Request('/verwaltung/shop_inhalt.php?funct=submitProduct', {
		method: 'post',
		postBody: Form.serialize('frm_product'),
		onComplete: onSubmitProductComplete
	});	
}

function onSubmitProductComplete(objReq) {
	
	arrResponse = objReq.responseText.evalJSON();
	
	if (arrResponse['error'].length > 0) {

		strHTML = '';	
		for (i = 0; i < arrResponse['error'].length; i++) {
			strHTML += '<li>' + arrResponse['error'][i] + '</li>';
		}
		
		$('error').innerHTML = strHTML;
		$('error').style.display = 'block';
	} else {		
		window.location.href = 'shop_index.php';
	}	
}

function deleteProduct(intProductId) {
		
	new Ajax.Request('/verwaltung/shop_index.php?funct=deleteProduct', {
		method: 'post',
		postBody: 'product=' + intProductId,
		onComplete: onDeleteProductComplete
	});	
}

function onDeleteProductComplete(objReq) {
	
	arrResponse = objReq.responseText.evalJSON();
	
	if (arrResponse['error'].length > 0) {
	
		for (i = 0; i < arrResponse['error'].length; i++) {
			alert(arrResponse['error'][i]);
		}
	} else {		
		strHTML = '';
		for (i = 0; i < arrResponse['success']['products'].length; i++) {
			strHTML += '<p class="event"><span class="edit"><a href="shop_inhalt.php?product=' + arrResponse['success']['products'][i]['id'] + '">editieren</a> | <a href="#" onclick="deleteProduct(' + arrResponse['success']['products'][i]['id'] + ');">löschen</a></span><strong>' + arrResponse['success']['products'][i]['title'] + '</strong> (' + arrResponse['success']['products'][i]['id'] + ')</p>'; 
		}
		$('listProducts').innerHTML = strHTML;
	}	 	
}


function submitForm() {

	$('response').style.display = 'none';
	$('loading').style.display = 'block';

	arrElements = document.getElementsByTagName('label');
	
	for (i = 0; i < arrElements.length; i++) {
		if (arrElements[i].className == 'error') {
			arrElements[i].className = '';
		}
	}
		
	new Ajax.Request('/Shop/Formular.html?funct=submitForm', {
		method: 'post',
		postBody: Form.serialize('frm_order'),
		onComplete: onSubmitFormComplete
	});	
}

function onSubmitFormComplete(objReq) {
	
	arr_response = objReq.responseText.evalJSON();

	if (arr_response['error'].length > 0) {
		$('response').className = 'error';

		for (i = 0; i < arr_response['error'].length; i++) {
			if ($(arr_response['error'][i]) != null) {
				$(arr_response['error'][i]).className = 'error';
			}			
		}
		
		$('response').innerHTML = 'Bitte alle benötigten Felder korrekt ausfüllen.';
		$('loading').style.display = 'none';
		$('response').style.display = 'block';		

	} else {		
    window.location.href = '/Shop/Kontrolle.html';
	}	 	
}

function submitOrder() {

	if ($('agb').checked == true) {
		
		$('response').style.display = 'none';
		$('loading').style.display = 'block';
			
		new Ajax.Request('/Shop/Kontrolle.html?funct=submitOrder', {
			method: 'get',
			onComplete: onSubmitOrderComplete
		});			

	} else {
		
		$('response').style.display = 'block';
	}
}

function onSubmitOrderComplete(objReq) {
	
	arr_response = objReq.responseText.evalJSON();

	if (arr_response['error'].length > 0) {
		$('response').className = 'error';
		$('response').innerHTML = 'Ein Fehler ist aufgetreten, bitte versuchen Sie es erneut.';
		$('loading').style.display = 'none';
		$('response').style.display = 'block';		

	} else {		
    window.location.href = '/Shop/Danke.html?orderId=' + arr_response['success'];
	}	 	
}

function deleteOrder(intOrderId) {
	
	new Ajax.Request('/verwaltung/shop_order_index.php?funct=deleteOrder', {
		method: 'post',
		postBody: 'order=' + intOrderId,
		onComplete: onDeleteOrderComplete
	});	
}

function onDeleteOrderComplete(objReq) {
	
	arrResponse = objReq.responseText.evalJSON();
	
	if (arrResponse['error'].length > 0) {
	
		for (i = 0; i < arrResponse['error'].length; i++) {
			alert(arrResponse['error'][i]);
		}
	} else {		
		var objDate = new Date();

		strHTML = '';
		for (i = 0; i < arrResponse['success']['orders'].length; i++) {
					
			strHTML += '<p class="event"><span class="edit"><a href="shop_order_inhalt.php?order=' + arrResponse['success']['orders'][i]['id'] + '">anzeigen</a> | <a href="#" onclick="deleteOrder(' + arrResponse['success']['orders'][i]['id'] + ');">löschen</a></span><strong>' + arrResponse['success']['orders'][i]['timestamp'] + '</strong> (' + arrResponse['success']['orders'][i]['id'] + ')</p>';
		}
		$('listOrders').innerHTML = strHTML;
	}	 	
}