var matEditTdId = '';
var loadingAnimation = false;
var loadingAnimation1 = false;
var loadingAnimation2 = false;

function updateMainPicture(url) {
	new Ajax.Request(url,
	{
	  method:'post',
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	if (response == 'Ok') updatePicturesList(TCparserURL);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  }
	});
}

function deletePicture(url) {
	var r=confirm("Napewno usunąć?")
	if (r==true)
	{
	  new Ajax.Request(url,
	  {
	    method:'post',
	    onSuccess: function(transport){
	    	var response = transport.responseText;
	    	if (response == 'Ok') updatePicturesList(TCparserURL);
	    },
	    onFailure: function() {
	    	alert('Something went wrong...');
	    }
	  });
	}
	else
	{
	  ;
	}
}

function deletePicture2(url) {
	var r=confirm("Napewno usunąć?")
	if (r==true)
	{
	  new Ajax.Request(url,
	  {
	    method:'post',
	    onSuccess: function(transport){
	    	var response = transport.responseText;
	    	if (response == 'Ok') updatePicturesList2('http://'+givDomain+'admin/module_proc.'+docExt+'?name=textileProduct&action=ajax_displayfotos2');
	    },
	    onFailure: function() {
	    	alert('Something went wrong...');
	    }
	  });
	}
	else
	{
	  ;
	}
}

function deleteSelectedPictures() {
	var output = '';
	var pureID = '';
	var r=confirm("Napewno usunąć?");
	if (r==true)
	{
		var picChk = document.getElementsByName('picChk');
		var picChkCount = picChk.length;
		for (var i = 0; i < picChkCount; i++) {
			//alert(picChk[i].id);
			if (picChk[i].checked == true) {
				pureID = picChk[i].id.replace(/chkImg_/,'');
				output = output+pureID+'|';
		  }
	  }
	  if (output != '') {
		  new Ajax.Request(delSelPicUrl,
		  {
		    method:'post',
		    parameters: {picids: output},
		    onSuccess: function(transport){
		    	var response = transport.responseText;
		    	if (response == 'Ok') updatePicturesList(TCparserURL);
		    },
		    onFailure: function() {
		    	alert('Something went wrong...');
		    }
		  });
	  }
	}
	else
	{
	  ;
	}
}

function deleteSelectedPictures2() {
	var output = '';
	var pureID = '';
	var r=confirm("Napewno usunąć?");
	if (r==true)
	{
		var matChk = document.getElementsByName('matChk');
		var matChkCount = matChk.length;
		for (var i = 0; i < matChkCount; i++) {
			if (matChk[i].checked == true) {
				//alert(matChk[i].id);
				pureID = matChk[i].id.replace(/chkMat_/,'');
				output = output+pureID+'|';
		  }
	  }
	  if (output != '') {
		  new Ajax.Request(delSelPicUrl2,
		  {
		    method:'post',
		    parameters: {picids: output},
		    onSuccess: function(transport){
		    	var response = transport.responseText;
		    	if (response == 'Ok') updatePicturesList2(TCparserURL2);
		    },
		    onFailure: function() {
		    	alert('Something went wrong...');
		    }
		  });
	  }
	}
	else
	{
	  ;
	}
}

function deleteSelectedSizes() {
	var output = '';
	var pureID = '';
	var r=confirm("Napewno usunąć?");
	if (r==true)
	{
		var picChk = document.getElementsByName('prodSizeChkBox');
		var picChkCount = picChk.length;
		for (var i = 0; i < picChkCount; i++) {
			//alert(picChk[i].id);
			if (picChk[i].checked == true) {
				pureID = picChk[i].id.replace(/prodSizeChkBox_/,'');
				output = output+pureID+'|';
		  }
	  }
	  if (output != '') {
		  new Ajax.Request(delSelSizesUrl,
		  {
		    method:'post',
		    parameters: {picids: output},
		    onSuccess: function(transport){
		    	var response = transport.responseText;
		    	if (response == 'Ok') updateSizesList(updatePicturesListUrl);
		    },
		    onFailure: function() {
		    	alert('Something went wrong...');
		    }
		  });
	  }
	}
	else
	{
	  ;
	}
}

function saveDescription() {
	with ($('descriptionForm')) {
		price.value = price.value.replace(/,/,'.');
	}
	new Ajax.Request(editDescriptionUrl,
	{
	  method:'post',
	  parameters: $('descriptionForm').serialize(true),
	  onLoading: function() {
	  	activateLoadingAnimation1(true);
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	tinyMCEUpdeted = false;
	  	//alert(response);
	  	//activateLoadingAnimation1(false);
	  	activateSaveIcon(1);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  	activateLoadingAnimation1(false);
	  }
	});
}

function saveProductDescription() {
	new Ajax.Request(editDescriptionUrl,
	{
	  method:'post',
	  parameters: $('descriptionForm').serialize(true),
	  onLoading: function() {
	  	activateLoadingAnimation1(true);
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	tinyMCEUpdeted = false;
	  	//alert(response);
	  	//activateLoadingAnimation1(false);
	  	activateSaveIcon(1);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  	activateLoadingAnimation1(false);
	  }
	});
}

function editProductSize(ID) {
	$('size_labour_'+ID).value = $('size_labour_'+ID).value.replace(/,/,'.');
	$('size_material_size_'+ID).value = $('size_material_size_'+ID).value.replace(/,/,'.');
	new Ajax.Request('module_proc.'+docExt+'?name=textileProduct&action=saveProductSize&id='+ID,
	{
	  method:'post',
	  parameters: {code: $('size_code_'+ID).value, labour: $('size_labour_'+ID).value, size: $('size_size_'+ID).value, material_size: $('size_material_size_'+ID).value},
	  onLoading: function() {
	  	activateLoadingAnimation2(true);
	  	$('size_submit_btn_'+ID).disabled = true;
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	$('size_submit_btn_'+ID).disabled = false;
	  	if (response == 'Ok') activateSaveIcon(2);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  	activateLoadingAnimation2(false);
	  }
	});
}

function updatePicturesList(url) {
	new Ajax.Request(url,
  {
    method:'post',
    parameters: {groupid: collectionRowId},
    onLoading: function() {
    	activateLoadingAnimation(true);
    },
    onSuccess: function(transport){
    	var response = transport.responseText;
    	$('collectionPictures').innerHTML = response;
    	activateLoadingAnimation(false);
    },
    onFailure: function() {
    	activateLoadingAnimation(false);
    	alert('Something went wrong...');
    }
  });
  //activateLoadingAnimation(false);
}

function updatePicturesList2(url) {
	new Ajax.Request(url,
  {
    method:'post',
    parameters: {groupid: collectionRowId},
    onLoading: function() {
    	activateLoadingAnimation2(true);
    },
    onSuccess: function(transport){
    	var response = transport.responseText;
    	$('collectionPictures2').innerHTML = response;
    	highlightMatImgByVal();
    	activateLoadingAnimation2(false);
    	//alert(response);
    },
    onFailure: function() {
    	activateLoadingAnimation2(false);
    	alert('Something went wrong...');
    }
  });
  //activateLoadingAnimation2(false);
}

function submitEditMaterialForm(url) {
	with ($('matEditForm')) {
		price.value = price.value.replace(/,/,'.');
	}
	new Ajax.Request(url,
  {
    method:'post',
    parameters: $('matEditForm').serialize(true),
    onLoading: function() {
    	activateLoadingAnimation2(true);
    },
    onSuccess: function(transport){
    	var response = transport.responseText;
    	activateSaveIcon(2);
    },
    onFailure: function() {
    	activateLoadingAnimation2(false);
    }
  });
}

function openEditMaterialBox(url) {
	new Ajax.Request(url,
  {
    method:'post',
    onLoading: function() {
    	activateLoadingAnimation2(true);
    },
    onSuccess: function(transport){
    	var response = transport.responseText;
    	activateLoadingAnimation2(false);
    	$('materialEditBox').innerHTML = response;
    },
    onFailure: function() {
    	activateLoadingAnimation2(false);
    }
  });
}

function activateEditFrame(url) {
	var box = $('materialEditBox');
	var boxHeight = 0;
	box.innerHTML = '';
	if (box.style.display != 'block') {
		box.setStyle({display: 'block'});
		new PeriodicalExecuter(function(pe) {
			boxHeight = boxHeight + 5;
			box.setStyle({height: boxHeight+'px'});
			if (boxHeight >= 95) {
				pe.stop();
				openEditMaterialBox(url);
			}
		}, 0.02);
	}
	else {
		openEditMaterialBox(url);
	}
}

function addProductSize(url) {
	with ($('addProductSizeForm')) {
		labour.value = labour.value.replace(/,/,'.');
		material_size.value = material_size.value.replace(/,/,'.');
	}
	new Ajax.Request(url,
	{
	  method:'post',
	  parameters: $('addProductSizeForm').serialize(true),
	  onLoading: function() {
	  	$('addProductSizeForm_SubmitBtn').disabled = true;
	  	activateLoadingAnimation2(true);
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	//alert(response);
	  	//activateLoadingAnimation1(false);
	  	with ($('addProductSizeForm')) {
	  		code.value = '';
	  		labour.value = '';
				size.value = '';
				material_size.value = '';
	  	}
	  	$('addProductSizeForm_SubmitBtn').disabled = false;
	  	//activateSaveIcon(2);
	  	updateSizesList(updatePicturesListUrl);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  	$('addProductSizeForm_SubmitBtn').disabled = false;
	  	activateLoadingAnimation2(false);
	  }
	});
}

function updateSizesList(url) {
	new Ajax.Request(url,
  {
    method:'post',
    parameters: {groupid: collectionRowId},
    onLoading: function() {
    	activateLoadingAnimation2(true);
    },
    onSuccess: function(transport){
    	var response = transport.responseText;
    	//alert(response);
    	$('productSizesDiv').innerHTML = response;
    	activateLoadingAnimation2(false);
    },
    onFailure: function() {
    	activateLoadingAnimation2(false);
    	alert('Something went wrong...');
    }
  });
  //activateLoadingAnimation(false);
}

function deactivateEditFrame() {
	var box = $('materialEditBox');
	var boxHeight = 95;
	box.innerHTML = '';
	new PeriodicalExecuter(function(pe) {
		boxHeight = boxHeight - 5;
		box.setStyle({height: boxHeight+'px'});
		if (boxHeight <= 0) {
			pe.stop();
			box.setStyle({display: 'none'});
			darkenMatImg();
		}
	}, 0.02);
}

function darkenMatImg() {
	$$('.materialImgTd').each(function(element){
		element.setStyle({backgroundColor: '#ffffff'});
	});
	matEditTdId = '';
}

function highlightMatImg(tdID) {
	$$('.materialImgTd').each(function(element){
		if (element.id != tdID) {
			element.setStyle({backgroundColor: '#ffffff'});
		}
		else {
			element.setStyle({backgroundColor: '#FF5E23'});
		}
	});
	matEditTdId = tdID;
}

function highlightMatImgByVal() {
	if (matEditTdId != '')
		$(matEditTdId).setStyle({backgroundColor: '#FF5E23'});
}

function activateLoadingAnimation(boolean) {
	if (boolean == true) {
		if (loadingAnimation == false) $('actionImg').src = 'images/ajaxaction.gif';
		loadingAnimation = true;
	}
	else {
		if (loadingAnimation == true) $('actionImg').src = 'images/noaction.gif';
		loadingAnimation = false;
	}
}

function activateLoadingAnimation1(boolean) {
	if (boolean == true) {
		if (loadingAnimation1 == false) $('actionImg1').src = 'images/ajaxaction.gif';
		loadingAnimation1 = true;
	}
	else {
		if (loadingAnimation1 == true) $('actionImg1').src = 'images/noaction.gif';
		loadingAnimation1 = false;
	}
}

function activateLoadingAnimation2(boolean) {
	if (boolean == true) {
		if (loadingAnimation2 == false) $('actionImg2').src = 'images/ajaxaction.gif';
		loadingAnimation2 = true;
	}
	else {
		if (loadingAnimation2 == true) $('actionImg2').src = 'images/noaction.gif';
		loadingAnimation2 = false;
	}
}

function activateSaveIcon(num) {
	if (num == 0) {
		$('actionImg').src = 'images/ajaxactionsaved.gif';
		new PeriodicalExecuter(function(pe) { activateLoadingAnimation(false); pe.stop(); }, 5); 
	}
	else if (num == 1) {
		$('actionImg1').src = 'images/ajaxactionsaved.gif';
		new PeriodicalExecuter(function(pe) { activateLoadingAnimation1(false); pe.stop(); }, 5); 
	}
	else if (num == 2) {
		$('actionImg2').src = 'images/ajaxactionsaved.gif';
		new PeriodicalExecuter(function(pe) { activateLoadingAnimation2(false); pe.stop(); }, 5); 
	}
}

function selectAllPicChk() {
	var picChk = document.getElementsByName('picChk');
	var picChkCount = picChk.length;
	for (var i = 0; i < picChkCount; i++) {
		picChk[i].checked = true;
	}
}

function selectAllPicChk2() {
	var matChk = document.getElementsByName('matChk');
	var matChkCount = matChk.length;
	for (var i = 0; i < matChkCount; i++) {
		matChk[i].checked = true;
	}
}

function selectAllSizesChk() {
	var picChk = document.getElementsByName('prodSizeChkBox');
	var picChkCount = picChk.length;
	for (var i = 0; i < picChkCount; i++) {
		picChk[i].checked = true;
	}
}

function deselectAllPicChk() {
	var picChk = document.getElementsByName('picChk');
	var picChkCount = picChk.length;
	for (var i = 0; i < picChkCount; i++) {
		picChk[i].checked = false;
	}
}

function deselectAllPicChk2() {
	var matChk = document.getElementsByName('matChk');
	var matChkCount = matChk.length;
	for (var i = 0; i < matChkCount; i++) {
		matChk[i].checked = false;
	}
}

function deselectAllSizesChk() {
	var matChk = document.getElementsByName('prodSizeChkBox');
	var matChkCount = matChk.length;
	for (var i = 0; i < matChkCount; i++) {
		matChk[i].checked = false;
	}
}

function setProductCompleteOnOff(checkbox, prodID) {
	new Ajax.Request('module_proc.php5?name=textileProduct&action=setProductCompleteOnOff&productid='+prodID,
	{
	  method:'post',
	  onLoading: function() {
	  	activateLoadingAnimation1(true);
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText;
	  	//alert(response);
	  	activateLoadingAnimation1(false);
	  },
	  onFailure: function() {
	  	activateLoadingAnimation1(false);
	  }
	});
}

// Sekcja strony WWW //
function updateProductMaterial(matID, prodID, returnURL, template) {
	new Ajax.Request('/modules/textileProduct/module_proc.php5?action=updateProductMaterial&module_name=textileProduct&matid='+matID+'&prodid='+prodID+'&template='+template+'&url='+returnURL,
	{
	  method:'post',
	  onLoading: function() {
	  	$('ajax_info').style.display = 'block';
	  },
	  onSuccess: function(transport){
	  	var response = transport.responseText.evalJSON(true);
	  	$('materialData').innerHTML = response.matData;
	  	$('materialSizes').innerHTML = response.sizesTable;
	  	$('materialMainPic').innerHTML = response.mainImage;
	  	$('ajax_info').style.display = 'none';
	  	//alert(response);
	  },
	  onFailure: function() {
	  	alert('Something went wrong...');
	  }
	});
}