﻿// JavaScript Document
var currentLink;
var top_item = 100;
var currentOpenedItem = 0;
// when chaning amount through chart then this will be set to 1
var changingChartAmount = 0;

var actionStringAfterConfirm = false;

function addNotesToProduct(value) {
	var e = document.getElementById("product_notes");
	if(e != null) {
		e.value = e.value +"\n"+ value;
	}
}

function askForConfirm(value) {
	showElement('confirm_choice');
	hideElement("confirm_list");
	actionStringAfterConfirm = value;
}

function confirmAction(value) {
	if(value) {
		if(actionStringAfterConfirm) {
			getContentAfterLoad(actionStringAfterConfirm, "page_content", "getContent('chart=1','content_chart')");
		}
	} else {
		hideElement("confirm_choice");
		showElement("confirm_list");
	}
	actionStringAfterConfirm = false;
}

function updatePersonalData(value,field) {
	//alert(value +" "+ field);
	getContent("chart_personal_data=1&field="+ encodeURIComponent(field) +"&value="+ encodeURIComponent(value),"verify_"+ field);
}

function chkAgreeConditions() {
	submitContent("chart_confirm","chart_confirm=1&agree_conditions=1","page_content");
}

function chkJoinMailinglist() {
	submitContent("chart_confirm","chart_confirm=1&join_mailinglist=1","page_content");
}

function removeDelivery(delivery) {
	getContentAfterLoad("chart=1&delivery="+ delivery +"&remove=1","content_chart","getContent('chart_confirm=1','page_content')");
}

function setDelivery(delivery, type) {
	getContentAfterLoad("chart=1&delivery="+ delivery +"&type="+ type,"content_chart", "getContent('chart_confirm=1','page_content')");
}

function setService(delivery, service) {
	getContentAfterLoad("chart=1&delivery="+ delivery +"&service="+ service,"content_chart","getContent('chart_confirm=1','page_content')");
}

function changeChartAmount(id) {
	changingChartAmount = 1;
	showElement("product_amount_change");
	getContent("shop=1&shop_product_id="+ id, "product_amount_change");
}

function showGalleryPicture(id, gallery) {
	//showElement("gallery_picture_container");
	//getContent("show_picture="+ id, "gallery_picture");

	getContent("show_picture="+ id +"&gallery="+ gallery, "picture_box");
	showElement("picture_box_container");
}

function showProductPicture(id) {
	getContent("shop=1&show_picture=1&shop_product_id="+ id, "picture_box");
	showElement("picture_box_container");
}

function showProduct(id) {
	changingChartAmount = 0;
	showElement("product_box");
	getContent("shop=1&shop_product_id="+ id, "product_box");
}

function loadWebPage(id) {
	getContent("navigation_top=1&id="+ id, "navigation_top");
	getContent("page_content=1&id="+ id, "page_content");
	getContent("navigation_left=1&id="+ id, "navigation_left");
	//getContent("<?= ($_GET['shop_id'] ? 'shop_id='. $_GET['shop_id'] .'&standalone=1' : ($shop_str_id ? 'shop_str_id='. $shop_str_id : 'content=1&id='. $id)) ?>", "page_content");
	//getContent("chart=1", "chart");
}

function loadShopPage(id) {
	getContent("navigation_left=1&shop_str_id="+id,"navigation_left");
	getContent("shop=1&shop_str_id="+ id,"page_content");
}

function clearField(objField, strDefaultValue) {
    if(objField.value == strDefaultValue) objField.value = '';
}

function restoreField(objField, strDefaultValue) {
    if(objField.value == '') objField.value = strDefaultValue;
}

function submitContent(formName, conditions, destination) {
	var formObj = new DHTMLSuite.form({
		formRef: formName,
		action: "load.php?"+ conditions,
		responseEl: destination});

	formObj.submit();
}

function hideElement(id) {
	var e = document.getElementById(id);
	if(e != null) {
		e.style.display = "none";
	}
}

function showElement(id) {
	var e = document.getElementById(id)
	if(e != null) {
		e.style.display = "block";
	}
}

function getContentAfterLoad(conditions, destination, afterLoad) {
	ajax_loadContent(destination, "load.php?"+ conditions, afterLoad);
}

function getContent(conditions, destination) {
	ajax_loadContent(destination, "load.php?"+ conditions, false);
}
 
function taust(a, change) {
	a.style.backgroundColor = change;
}

function toggleElement(id) {
	var e = document.getElementById(id);
	if(e != null) {
		if(e.style.display == "none") {
			showElement(id);
		} else {
			hideElement(id);
		}
	}
}

function shopAddAmount(add) {
	var e = document.getElementById("count_product");
	var total;
	if(e != null) {
		total = parseInt(e.value) + add;
		e.value = (total < 0 ? 0 : total);
	}
}

function shopSetProductsChart(id) {
	var e = document.getElementById("count_product");
	var c = document.getElementById("product_notes");
	if(e != null) {
		getContentAfterLoad("chart=1&shop_id="+ id +"&amount="+ e.value + (c != null ? "&notes="+ c.value : ""),"content_chart", (changingChartAmount ? "getContent('chart_confirm=1','page_content')" : false));
	}
	// when changing amounts through chart window then we refresh the chart main list as well!
	if(changingChartAmount) {
		changingChartAmount = 0;
	}
}

